Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numpy 1.25 issues #930

Merged
merged 7 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/phase/symmetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
... x = dx * (i + 0.5)
... y = dx * (j + 0.5)
... testResult[i, j] = x * y
... bottomRight[i, j] = var(((L - x,), (y,)))
... topLeft[i, j] = var(((x,), (L - y,)))
... topRight[i, j] = var(((L - x,), (L - y,)))
... bottomRight[i, j] = var(((L - x,), (y,)))[0]
... topLeft[i, j] = var(((x,), (L - y,)))[0]
... topRight[i, j] = var(((L - x,), (L - y,)))[0]
>>> numerix.allclose(testResult, bottomRight, atol = 1e-10)
1
>>> numerix.allclose(testResult, topLeft, atol = 1e-10)
Expand Down
63 changes: 61 additions & 2 deletions fipy/meshes/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,13 @@ def __mul__(self, factor):
[[ 1.5 4.5 1.5 4.5]
[ 1. 1. 3. 3. ]]


but the vector must have the same dimensionality as the `Mesh`

>>> dilatedMesh = baseMesh * ((3,), (2,), (1,)) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: shape mismatch: objects cannot be broadcast to a single shape
"""
"""
newCoords = self.vertexCoords * factor
newmesh = Mesh(vertexCoords=newCoords,
faceVertexIDs=numerix.array(self.faceVertexIDs),
Expand Down Expand Up @@ -786,6 +785,66 @@ def _test(self):
... # doctest: +SERIAL
True



Check dilation of 1D meshes

>>> from fipy.meshes import Grid1D
>>> baseMesh = Grid1D(dx = 1.0, nx = 2)
>>> print(baseMesh.cellCenters)
[[ 0.5 1.5]]

The `factor` can be a scalar

>>> dilatedMesh = baseMesh * 3
>>> print(dilatedMesh.cellCenters)
[[ 1.5 4.5]]

or a vector

>>> dilatedMesh = baseMesh * ((3,),)
>>> print(dilatedMesh.cellCenters)
[[ 1.5 4.5]]

but the vector must have the same dimensionality as the `Mesh`

>>> dilatedMesh = baseMesh * ((3,), (2,), (1,)) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: shape mismatch: objects cannot be broadcast to a single shape


Check dilation of 3D meshes

>>> from fipy.meshes import Grid3D
>>> baseMesh = Grid3D(dx = 1.0, dy = 1.0, dz = 1.0, nx = 2, ny = 2, nz = 2)
>>> print(baseMesh.cellCenters)
[[ 0.5 1.5 0.5 1.5 0.5 1.5 0.5 1.5]
[ 0.5 0.5 1.5 1.5 0.5 0.5 1.5 1.5]
[ 0.5 0.5 0.5 0.5 1.5 1.5 1.5 1.5]]

The `factor` can be a scalar

>>> dilatedMesh = baseMesh * 3
>>> print(dilatedMesh.cellCenters)
[[ 1.5 4.5 1.5 4.5 1.5 4.5 1.5 4.5]
[ 1.5 1.5 4.5 4.5 1.5 1.5 4.5 4.5]
[ 1.5 1.5 1.5 1.5 4.5 4.5 4.5 4.5]]

or a vector

>>> dilatedMesh = baseMesh * ((3,), (2,), (1,))
>>> print(dilatedMesh.cellCenters)
[[ 1.5 4.5 1.5 4.5 1.5 4.5 1.5 4.5]
[ 1. 1. 3. 3. 1. 1. 3. 3. ]
[ 0.5 0.5 0.5 0.5 1.5 1.5 1.5 1.5]]

but the vector must have the same dimensionality as the `Mesh`

>>> dilatedMesh = baseMesh * ((3,), (2,)) #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: shape mismatch: objects cannot be broadcast to a single shape
"""

def _test():
Expand Down
5 changes: 4 additions & 1 deletion fipy/meshes/uniformGrid1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ def _translate(self, vector):
overlap=self.args['overlap'])

def __mul__(self, factor):
return UniformGrid1D(dx=self.dx * factor,
dx = numerix.array([[self.args['dx']]])
dx *= factor

return UniformGrid1D(dx=dx[0,0],
nx=self.args['nx'],
origin=self.args['origin'] * factor,
overlap=self.args['overlap'])
Expand Down
9 changes: 5 additions & 4 deletions fipy/meshes/uniformGrid2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,12 @@ def _translate(self, vector):
origin = numerix.array(self.args['origin']) + vector, overlap=self.args['overlap'])

def __mul__(self, factor):
if numerix.shape(factor) is ():
factor = numerix.resize(factor, (2, 1))
dxdy = numerix.array([[self.args['dx']],
[self.args['dy']]])
dxdy *= factor

return UniformGrid2D(dx=self.args['dx'] * numerix.array(factor[0]), nx=self.args['nx'],
dy=self.args['dy'] * numerix.array(factor[1]), ny=self.args['ny'],
return UniformGrid2D(dx=dxdy[0,0], nx=self.args['nx'],
dy=dxdy[1,0], ny=self.args['ny'],
origin=numerix.array(self.args['origin']) * factor, overlap=self.args['overlap'])

@property
Expand Down
13 changes: 9 additions & 4 deletions fipy/meshes/uniformGrid3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,15 @@ def _translate(self, vector):
origin = numerix.array(self.args['origin']) + vector, overlap=self.args['overlap'])

def __mul__(self, factor):
return UniformGrid3D(dx = self.dx * factor, nx = self.nx,
dy = self.dy * factor, ny = self.ny,
dz = self.dz * factor, nz = self.nz,
origin = self.origin * factor)
dxdydz = numerix.array([[self.args['dx']],
[self.args['dy']],
[self.args['dz']]])
dxdydz *= factor

return UniformGrid3D(dx=dxdydz[0,0], nx=self.nx,
dy=dxdydz[1,0], ny=self.ny,
dz=dxdydz[2,0], nz=self.nz,
origin=self.origin * factor)

@property
def _concatenableMesh(self):
Expand Down
58 changes: 29 additions & 29 deletions fipy/tools/dimensions/physicalField.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def _sum(self, other, sign1 = lambda a: a, sign2 = lambda b: b):
other = PhysicalField(value = other)

if not isinstance(other, PhysicalField):
if numerix.alltrue(other == 0):
if numerix.all(other == 0):
new_value = sign1(selfValue)
elif self.unit.isDimensionlessOrAngle() or self.unit.isInverseAngle():
new_value = sign1(selfValue) + sign2(other)
Expand Down Expand Up @@ -484,7 +484,7 @@ def _inMyUnits(self, other):
if not isinstance(other, PhysicalField):
if isinstance(other, string_types):
other = PhysicalField(other)
elif numerix.alltrue(other == 0) or self.unit.isDimensionlessOrAngle():
elif numerix.all(other == 0) or self.unit.isDimensionlessOrAngle():
other = PhysicalField(value = other, unit = self.unit)
else:
raise TypeError('Incompatible units')
Expand Down Expand Up @@ -654,7 +654,7 @@ def __float__(self):
As a special case, quantities with angular units are converted to
base units (radians) and then assumed dimensionless.

>>> print(numerix.round_(float(PhysicalField("2. deg")), 6))
>>> print(numerix.round(float(PhysicalField("2. deg")), 6))
0.034907

If the quantity is not dimensionless, the conversion fails.
Expand Down Expand Up @@ -908,7 +908,7 @@ def numericValue(self):
"""
Return the `PhysicalField` without units, after conversion to base SI units.

>>> print(numerix.round_(PhysicalField("1 inch").numericValue, 6))
>>> print(numerix.round(PhysicalField("1 inch").numericValue, 6))
0.0254
"""
return self.inSIUnits().value
Expand Down Expand Up @@ -971,7 +971,7 @@ def arccos(self):

The input `PhysicalField` must be dimensionless

>>> print(numerix.round_(PhysicalField("1 m").arccos(), 6))
>>> print(numerix.round(PhysicalField("1 m").arccos(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand All @@ -988,7 +988,7 @@ def arccosh(self):

The input `PhysicalField` must be dimensionless

>>> print(numerix.round_(PhysicalField("1. m").arccosh(), 6))
>>> print(numerix.round(PhysicalField("1. m").arccosh(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand All @@ -1004,7 +1004,7 @@ def arcsin(self):

The input `PhysicalField` must be dimensionless

>>> print(numerix.round_(PhysicalField("1 m").arcsin(), 6))
>>> print(numerix.round(PhysicalField("1 m").arcsin(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand Down Expand Up @@ -1065,9 +1065,9 @@ def cos(self):
"""
Return the cosine of the `PhysicalField`

>>> print(numerix.round_(PhysicalField(2*numerix.pi/6, "rad").cos(), 6))
>>> print(numerix.round(PhysicalField(2*numerix.pi/6, "rad").cos(), 6))
0.5
>>> print(numerix.round_(PhysicalField(60., "deg").cos(), 6))
>>> print(numerix.round(PhysicalField(60., "deg").cos(), 6))
0.5

The units of the `PhysicalField` must be an angle
Expand Down Expand Up @@ -1099,9 +1099,9 @@ def tan(self):
"""
Return the tangent of the `PhysicalField`

>>> numerix.round_(PhysicalField(numerix.pi/4, "rad").tan(), 6)
>>> numerix.round(PhysicalField(numerix.pi/4, "rad").tan(), 6)
1.0
>>> numerix.round_(PhysicalField(45, "deg").tan(), 6)
>>> numerix.round(PhysicalField(45, "deg").tan(), 6)
1.0

The units of the `PhysicalField` must be an angle
Expand Down Expand Up @@ -1133,15 +1133,15 @@ def arctan2(self, other):
"""
Return the arctangent of `self` divided by `other` in radians

>>> print(numerix.round_(PhysicalField(2.).arctan2(PhysicalField(5.)), 6))
>>> print(numerix.round(PhysicalField(2.).arctan2(PhysicalField(5.)), 6))
0.380506

The input `PhysicalField` objects must be in the same dimensions

>>> print(numerix.round_(PhysicalField(2.54, "cm").arctan2(PhysicalField(1., "inch")), 6))
>>> print(numerix.round(PhysicalField(2.54, "cm").arctan2(PhysicalField(1., "inch")), 6))
0.785398

>>> print(numerix.round_(PhysicalField(2.).arctan2(PhysicalField("5. m")), 6))
>>> print(numerix.round(PhysicalField(2.).arctan2(PhysicalField("5. m")), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand All @@ -1153,12 +1153,12 @@ def arctan(self):
"""
Return the arctangent of the `PhysicalField` in radians

>>> print(numerix.round_(PhysicalField(1).arctan(), 6))
>>> print(numerix.round(PhysicalField(1).arctan(), 6))
0.785398

The input `PhysicalField` must be dimensionless

>>> print(numerix.round_(PhysicalField("1 m").arctan(), 6))
>>> print(numerix.round(PhysicalField("1 m").arctan(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand All @@ -1174,7 +1174,7 @@ def arctanh(self):

The input `PhysicalField` must be dimensionless

>>> print(numerix.round_(PhysicalField("1 m").arctanh(), 6))
>>> print(numerix.round(PhysicalField("1 m").arctanh(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand All @@ -1186,12 +1186,12 @@ def log(self):
"""
Return the natural logarithm of the `PhysicalField`

>>> print(numerix.round_(PhysicalField(10).log(), 6))
>>> print(numerix.round(PhysicalField(10).log(), 6))
2.302585

The input `PhysicalField` must be dimensionless

>>> print(numerix.round_(PhysicalField("1. m").log(), 6))
>>> print(numerix.round(PhysicalField("1. m").log(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand All @@ -1202,12 +1202,12 @@ def log10(self):
"""
Return the base-10 logarithm of the `PhysicalField`

>>> print(numerix.round_(PhysicalField(10.).log10(), 6))
>>> print(numerix.round(PhysicalField(10.).log10(), 6))
1.0

The input `PhysicalField` must be dimensionless

>>> print(numerix.round_(PhysicalField("1. m").log10(), 6))
>>> print(numerix.round(PhysicalField("1. m").log10(), 6))
Traceback (most recent call last):
...
TypeError: Incompatible units
Expand Down Expand Up @@ -1415,7 +1415,7 @@ def _checkSame(self, other):
return self.isDimensionless()
else:
raise TypeError('PhysicalUnits can only be compared with other PhysicalUnits')
if not numerix.alltrue(self.powers == other.powers):
if not numerix.all(self.powers == other.powers):
raise TypeError('Incompatible units')

def __eq__(self, other):
Expand Down Expand Up @@ -1651,7 +1651,7 @@ def conversionFactorTo(self, other):

>>> a = PhysicalField("1. mm")
>>> b = PhysicalField("1. inch")
>>> print(numerix.round_(b.unit.conversionFactorTo(a.unit), 6))
>>> print(numerix.round(b.unit.conversionFactorTo(a.unit), 6))
25.4

Units must have the same fundamental SI units
Expand All @@ -1673,7 +1673,7 @@ def conversionFactorTo(self, other):
...
TypeError: Unit conversion (K to degF) cannot be expressed as a simple multiplicative factor
"""
if not numerix.alltrue(self.powers == other.powers):
if not numerix.all(self.powers == other.powers):
if self.isDimensionlessOrAngle() and other.isDimensionlessOrAngle():
return self.factor / other.factor
else:
Expand All @@ -1691,10 +1691,10 @@ def conversionTupleTo(self, other): # added 1998/09/29 GPW
>>> a = PhysicalField("1. K").unit
>>> b = PhysicalField("1. degF").unit
>>> from builtins import str
>>> [str(numerix.round_(element, 6)) for element in b.conversionTupleTo(a)]
>>> [str(numerix.round(element, 6)) for element in b.conversionTupleTo(a)]
['0.555556', '459.67']
"""
if not numerix.alltrue(self.powers == other.powers):
if not numerix.all(self.powers == other.powers):
raise TypeError('Incompatible units')

# let (s1,d1) be the conversion tuple from 'self' to base units
Expand Down Expand Up @@ -1886,17 +1886,17 @@ def _Scale(quantity, scaling):

`quantity` can be a `PhysicalField`

>>> print(numerix.round_(_Scale(PhysicalField("1. inch"), PhysicalField("1. mm")), 6))
>>> print(numerix.round(_Scale(PhysicalField("1. inch"), PhysicalField("1. mm")), 6))
25.4

or a value-unit string convertible to a `PhysicalField`

>>> print(numerix.round_(_Scale("1. inch", PhysicalField("1. mm")), 6))
>>> print(numerix.round(_Scale("1. inch", PhysicalField("1. mm")), 6))
25.4

or a dimensionless number. A dimensionless number is left alone.

>>> print(numerix.round_(_Scale(PhysicalField(2.), PhysicalField("1. mm")), 6))
>>> print(numerix.round(_Scale(PhysicalField(2.), PhysicalField("1. mm")), 6))
2.0

It is an error for the result to have dimensions.
Expand Down