Skip to content

Commit

Permalink
Add optional in-place cast Region.astype(dtype, copy=True) (#888)
Browse files Browse the repository at this point in the history
* Add optional in-place cast `Region.astype(dtype, copy=True)`

* Update test_dtype.py
  • Loading branch information
adtzlr authored Nov 6, 2024
1 parent da62927 commit 706a638
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ All notable changes to this project will be documented in this file. The format
- Add `constitution.jax.Material(..., jacobian=None)` with JAX as backend. A custom jacobian-callable may be passed to switch between forward- and backward-mode automatic differentiation.
- Add material models for JAX-based materials: `felupe.constitution.jax.models.hyperelastic.mooney_rivlin()`, `felupe.constitution.jax.models.hyperelastic.yeoh()`, `felupe.constitution.jax.models.hyperelastic.third_order_deformation()`, `felupe.constitution.jax.models.lagrange.morph()`, `felupe.constitution.jax.models.lagrange.morph_representative_directions()`.
- Add `felupe.constitution.jax.total_lagrange()`, `felupe.constitution.jax.updated_lagrange()` and `felupe.constitution.jax.isochoric_volumetric_split()` function decorators for the JAX hyperelastic material class.
- Add an optional keyword-argument `Region.astype(copy=True)` to modify the data types of the arrays of the region in-place if `copy=False`.

### Changed
- Change default `np.einsum(..., order="K")` to `np.einsum(..., order="C")` in the methods of `Field`, `FieldAxisymmetric`, `FieldPlaneStrain` and `FieldContainer`.
- Change supported Python versions to 3.9 - 3.12.
- Change the `dtype`-argument in `Region.astype(dtype)` from an optional to a required argument.

### Fixed
- Fix the number of points for non-disconnected dual meshes. This reduces the assembled (sparse) vector- and matrix-shapes, which are defined on mixed-fields.
Expand Down
21 changes: 16 additions & 5 deletions src/felupe/region/_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,17 @@ def __init__(self, mesh, element, quadrature, grad=True, hess=False, uniform=Fal
self.evaluate_hessian = hess
self.reload(mesh=mesh, element=element, quadrature=quadrature, uniform=uniform)

def astype(self, dtype=None):
def astype(self, dtype, copy=True):
"""Copy the region and cast the arrays to a specified type.
Parameters
----------
dtype : data-type or None, optional
The data-type of the arrays of the Region. If None, a copy of the Region is
returned.
dtype : str or dtype
Typecode or data-type to which the arrays of the region are cast.
copy : bool, optional
By default, astype always returns a copy of the region with newly allocated
arrays. If False, the arrays of the input region are modified and the input
region is returned. Default is True.
Returns
-------
Expand All @@ -168,7 +171,11 @@ def astype(self, dtype=None):
felupe.region.copy : Return a copy of the region and reload it if necessary.
"""

region = self.copy(uniform=self.uniform)
region = self

if copy:
region = region.copy(uniform=self.uniform)

region.h = region.h.astype(dtype)
region.dhdr = region.dhdr.astype(dtype)

Expand All @@ -178,6 +185,10 @@ def astype(self, dtype=None):
region.dhdX = region.dhdX.astype(dtype)
region.dV = region.dV.astype(dtype)

if region.evaluate_hessian:
region.d2hdrdr = region.d2hdrdr.astype(dtype)
region.d2hdXdX = region.d2hdXdX.astype(dtype)

return region

def copy(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

def test_dtype(dtype=np.float32, tol=1e-3):
mesh = fem.Cube(n=3)
region = fem.RegionHexahedron(mesh).astype(dtype)
region = fem.RegionHexahedron(mesh).astype(dtype, copy=False)
displacement = fem.Field(region, dim=3, dtype=dtype)
field = fem.FieldContainer([displacement])

Expand Down Expand Up @@ -62,7 +62,7 @@ def test_dtype_axi():

def test_dtype_planestrain():
mesh = fem.Rectangle(n=3)
region = fem.RegionQuad(mesh).astype(np.float32)
region = fem.RegionQuad(mesh, hess=True).astype(np.float32)
displacement = fem.FieldPlaneStrain(region, dtype=np.float32)
field = fem.FieldContainer([displacement])

Expand Down

0 comments on commit 706a638

Please sign in to comment.