Coverage for test_model.py: 100%
31 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 11:29 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 11:29 +0000
1""" Unit tests for a diffusion model """
3from pytest import raises
4from .model import energy
6def test_energy_fails_on_non_integer_density():
7 with raises(TypeError) as exception:
8 energy([1.0, 2, 3])
10def test_energy_fails_on_negative_density():
11 with raises(ValueError) as exception: energy(
12 [-1, 2, 3])
14def test_energy_fails_ndimensional_density():
15 with raises(ValueError) as exception: energy(
16 [[1, 2, 3], [3, 4, 5]])
18def test_zero_energy_cases():
19 # Zero energy at zero density
20 densities = [ [], [0], [0, 0, 0] ]
21 for density in densities:
22 assert energy(density) == 0
24def test_derivative():
25 from numpy.random import randint
27 # Loop over vectors of different sizes (but not empty)
28 for vector_size in randint(1, 1000, size=30):
30 # Create random density of size N
31 density = randint(50, size=vector_size)
33 # will do derivative at this index
34 element_index = randint(vector_size)
36 # modified densities
37 density_plus_one = density.copy()
38 density_plus_one[element_index] += 1
40 # Compute and check result
41 # d(n^2-1)/dn = 2n
42 expected = (2.0 * density[element_index]
43 if density[element_index] > 0
44 else 0 )
45 actual = energy(density_plus_one) - energy(density)
46 assert expected == actual
48def test_derivative_no_self_energy():
49 """ If particle is alone, then its participation to energy is zero """
50 from numpy import array
52 density = array([1, 0, 1, 10, 15, 0])
53 density_plus_one = density.copy()
54 density[1] += 1
56 expected = 0
57 actual = energy(density_plus_one) - energy(density)
58 assert expected == actual