Skip to content

Commit

Permalink
compatible with NumPy 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MarDiehl committed Jun 19, 2024
1 parent 8ae3c2f commit 6bb914e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
16 changes: 8 additions & 8 deletions python/damask/_geomgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ def material(self,
material: np.ndarray):
if len(material.shape) != 3:
raise ValueError(f'invalid material shape {material.shape}')
if material.dtype not in np.sctypes['float'] and material.dtype not in np.sctypes['int']:
if material.dtype not in [np.float32,np.float64, np.int32,np.int64]:
raise TypeError(f'invalid material data type "{material.dtype}"')

self._material = np.copy(material)

if self.material.dtype in np.sctypes['float'] and \
if self.material.dtype in [np.float32,np.float64] and \
np.all(self.material == self.material.astype(np.int64).astype(float)):
self._material = self.material.astype(np.int64)

Expand All @@ -153,7 +153,7 @@ def size(self,
if len(size) != 3 or any(np.array(size) < 0):
raise ValueError(f'invalid size {size}')

self._size = np.array(size)
self._size = np.array(size,np.float64)

@property
def origin(self) -> np.ndarray:
Expand All @@ -166,7 +166,7 @@ def origin(self,
if len(origin) != 3:
raise ValueError(f'invalid origin {origin}')

self._origin = np.array(origin)
self._origin = np.array(origin,np.float64)

@property
def initial_conditions(self) -> Dict[str,np.ndarray]:
Expand Down Expand Up @@ -812,7 +812,7 @@ def save_ASCII(self,
'homogenization 1',
]

format_string = '%g' if self.material.dtype in np.sctypes['float'] else \
format_string = '%g' if self.material.dtype in [np.float32,np.float64] else \
'%{}i'.format(1+int(np.floor(np.log10(np.nanmax(self.material)))))
np.savetxt(fname,
self.material.reshape([self.cells[0],np.prod(self.cells[1:])],order='F').T,
Expand Down Expand Up @@ -1347,14 +1347,14 @@ def add_primitive(self,
"""
# radius and center
r = np.array(dimension)/2.0*self.size/self.cells if np.array(dimension).dtype in np.sctypes['int'] else \
r = np.array(dimension)/2.0*self.size/self.cells if np.issubdtype(np.array(dimension).dtype,np.integer) else \
np.array(dimension)/2.0
c = (np.array(center) + .5)*self.size/self.cells if np.array(center).dtype in np.sctypes['int'] else \
c = (np.array(center) + .5)*self.size/self.cells if np.issubdtype(np.array(center).dtype, np.integer) else \
(np.array(center) - self.origin)

coords = grid_filters.coordinates0_point(self.cells,self.size,
-(0.5*(self.size + (self.size/self.cells
if np.array(center).dtype in np.sctypes['int'] else
if np.issubdtype(np.array(center).dtype,np.integer) else
0)) if periodic else c))
coords_rot = R.broadcast_to(tuple(self.cells))@coords

Expand Down
12 changes: 8 additions & 4 deletions python/damask/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
from . import util
from ._typehints import FloatSequence, IntSequence, DADF5Dataset


h5py3 = h5py.__version__[0] == '3'

chunk_size = 1024**2//8 # for compression in HDF5

prefix_inc = 'increment_'


def _read(dataset: h5py._hl.dataset.Dataset) -> np.ndarray:
"""Read a dataset and its metadata into a numpy.ndarray."""
metadata = {k:(v.decode() if not h5py3 and type(v) is bytes else v) for k,v in dataset.attrs.items()}
Expand Down Expand Up @@ -59,7 +62,7 @@ def _empty_like(dataset: np.ma.core.MaskedArray,
fill_int: int) -> np.ma.core.MaskedArray:
"""Create empty numpy.ma.MaskedArray."""
return ma.array(np.empty((N_materialpoints,)+dataset.shape[1:],dataset.dtype),
fill_value = fill_float if dataset.dtype in np.sctypes['float'] else fill_int,
fill_value = fill_float if np.issubdtype(dataset.dtype,np.floating) else fill_int,
mask = True)


Expand Down Expand Up @@ -1783,9 +1786,10 @@ def export_XDMF(self,
attribute_type_map = defaultdict(lambda:'Matrix', ( ((),'Scalar'), ((3,),'Vector'), ((3,3),'Tensor')) )

def number_type_map(dtype):
if dtype in np.sctypes['int']: return 'Int'
if dtype in np.sctypes['uint']: return 'UInt'
if dtype in np.sctypes['float']: return 'Float'
if np.issubdtype(dtype,np.signedinteger): return 'Int'
if np.issubdtype(dtype,np.unsignedinteger): return 'UInt'
if np.issubdtype(dtype,np.floating): return 'Float'
raise TypeError(f'invalid type "{dtype}"')


xdmf = ET.Element('Xdmf')
Expand Down
6 changes: 4 additions & 2 deletions python/damask/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from . import version as _version
from ._typehints import FloatSequence as _FloatSequence, NumpyRngSeed as _NumpyRngSeed, FileHandle as _FileHandle


# https://svn.blender.org/svnroot/bf-blender/trunk/blender/build_files/scons/tools/bcolors.py
# https://stackoverflow.com/questions/287871
_colors = {
Expand Down Expand Up @@ -323,8 +324,9 @@ def lcm(a,b):

max_denominator = int(10**(N_significant-1))

if (v_ := _np.asarray(v)).dtype in _np.sctypes['float']:
v_ = _np.round(_np.asarray(v,'float64')/_np.max(_np.abs(v)),N_significant)
v_ = _np.asarray(v)
if _np.issubdtype(v_.dtype,_np.inexact):
v_ = _np.round(_np.asarray(v,_np.float64)/_np.max(_np.abs(v)),N_significant)
m = (v_ * _reduce(lcm, map(lambda x: int(get_square_denominator(x,max_denominator)),v_))**0.5).astype(_np.int64)
m = m//_reduce(_np.gcd,m)

Expand Down
4 changes: 2 additions & 2 deletions python/tests/test_GeomGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ def test_invalid_material_type(self):
GeomGrid(np.zeros((3,3,3),dtype='complex'),np.ones(3))

def test_cast_to_int(self):
g = GeomGrid(np.zeros((3,3,3)),np.ones(3))
assert g.material.dtype in np.sctypes['int']
g = GeomGrid(np.zeros((3,3,3),dtype=np.float64),np.ones(3))
assert g.material.dtype in [np.int32,np.int64]

def test_invalid_size(self,default):
with pytest.raises(ValueError):
Expand Down
6 changes: 3 additions & 3 deletions python/tests/test_grid_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ def test_div(self,field_def,div_def):
def test_ravel_index(self):
cells = np.random.randint(8,32,(3))

indices = np.block(np.meshgrid(np.arange(cells[0]),
np.arange(cells[1]),
np.arange(cells[2]),indexing='ij')).reshape(tuple(cells)+(3,),order='F')
indices = np.block(list(np.meshgrid(np.arange(cells[0]),
np.arange(cells[1]),
np.arange(cells[2]),indexing='ij'))).reshape(tuple(cells)+(3,),order='F')
x,y,z = map(np.random.randint,cells)
assert grid_filters.ravel_index(indices)[x,y,z] == np.arange(0,np.prod(cells)).reshape(cells,order='F')[x,y,z]

Expand Down

0 comments on commit 6bb914e

Please sign in to comment.