diff --git a/fipy/meshes/builders/utilityClasses.py b/fipy/meshes/builders/utilityClasses.py index 3e8b1aea0e..da4ff19c16 100644 --- a/fipy/meshes/builders/utilityClasses.py +++ b/fipy/meshes/builders/utilityClasses.py @@ -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 @@ -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()