Skip to content

Commit

Permalink
correct the validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sridhar-mani committed Dec 3, 2024
1 parent 24b1952 commit 5b4c1e4
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions fipy/meshes/builders/utilityClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,39 @@ class _NonuniformNumPts(_AbstractNumPts):

@staticmethod
def calcNs(ns, ds):
"""
Calculate the number of points for a non-uniform grid.
Parameters
----------
ns : list
Number of grid spacings in each direction, e.g., [nx, ny, nz].
ds : list
Spacing in each grid direction, e.g., [dx, dy, dz].
Returns
-------
newNs : list
Validated and computed number of points for each axis.
"""
axis = ["x", "y", "z"][:len(ns)]
newNs = []

for a, d, n in zip(axis, ds, ns):
if n is not None and (not isinstance(n, int) or n <= 0):
raise ValueError(f"Number of points along {a} (n{a}) must be a positive integer. Got: {n}")
newNs.append(_NonuniformNumPts._calcNumPts(d=d, n=n, axis=a))

return newNs

@staticmethod
def _calcNumPts(d, n = None, axis = "x"):
"""
Calculate the number of cells along the specified axis, based
on either the specified number or on the number elements in the
cell `d` spacings.
Used by the `Grid` meshes.
This tests a bug that was occurring with `PeriodicGrid1D` when
using a numpy float as the argument for the grid spacing.
>>> from fipy.meshes.periodicGrid1D import PeriodicGrid1D
>>> PeriodicGrid1D(nx=2, dx=numerix.float32(1.))
PeriodicGrid1D(dx=1.0, nx=2)
"""

def _calcNumPts(d, n=None, axis="x"):
if type(d) in [int, float] or numerix.shape(d) == ():
n = int(n or 1)
else:
n = int(n or len(d))
if n != len(d) and len(d) != 1:
raise IndexError("n%s != len(d%s)" % (axis, axis))
raise IndexError(f"n{axis} != len(d{axis})")

return n

Expand All @@ -129,8 +131,27 @@ class _UniformNumPts(_AbstractNumPts):

@staticmethod
def calcNs(ns, ds):
return [int(x) for x in ns]
"""
Calculate the number of points for a uniform grid.
Parameters
----------
ns : list
Number of grid spacings in each direction, e.g., [nx, ny, nz].
ds : list
Spacing in each grid direction, e.g., [dx, dy, dz].
Returns
-------
validatedNs : list
Validated and computed number of points for each axis.
"""
validatedNs = []
for i, n in enumerate(ns):
if n is not None and (not isinstance(n, int) or n <= 0):
raise ValueError(f"Number of points along dimension {i} must be a positive integer. Got: {n}")
validatedNs.append(int(n))
return validatedNs
if __name__ == '__main__':
import doctest
doctest.testmod()

0 comments on commit 5b4c1e4

Please sign in to comment.