Coverage for model.py: 100%
10 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""" Simplistic 1-dimensional diffusion model """
3def energy(density):
4 """
5 Energy associated with the diffusion model
7 :Parameters:
9 density: array of positive integers
10 Number of particles at each position i in the array/geometry
11 """
12 from numpy import array, any, sum
14 # Make sure input is an numpy array
15 density = array(density)
17 # ...of the right kind (integer). Unless it is zero length,
18 # in which case type does not matter.
20 if density.dtype.kind != 'i' and len(density) > 0:
21 raise TypeError("Density should be a array of *integers*.")
22 # and the right values (positive or null)
23 if any(density < 0):
24 raise ValueError("Density should be an array of *positive* integers.")
25 if density.ndim != 1:
26 raise ValueError("Density should be an a *1-dimensional*" +
27 "array of positive integers.")
29 return sum(density * (density - 1))