Skip to content

Commit

Permalink
Raise a TypeError if a container is used in a region (#939)
Browse files Browse the repository at this point in the history
  • Loading branch information
adtzlr authored Feb 15, 2025
1 parent 0b3a637 commit 46b5093
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. The format
- Add a new argument to apply a callable on the assembled vector/matrix of a solid body, `SolidBody(..., apply=None)`. This may be used to sum the list of sub-blocks instead of stacking them together, `SolidBody(..., block=False, apply=None)`. This is useful for mixed formulations where both the deformation gradient and the displacement values are required.
- Add support for non-symmetric bilinear mixed forms in `IntegralForm`.
- Add `element.Element` to the top-level package namespace.
- Raise a TypeError if a mesh container is used as the `mesh`-argument in a region. The error message explains that the container is not supported. A mesh must be used instead.

### Changed
- The first Piola-Kirchhoff stress tensor is evaluated if `ViewSolid(stress_type=None)`.
Expand Down
6 changes: 6 additions & 0 deletions src/felupe/region/_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ def reload(
region = self

if mesh is not None:

if "container" in type(mesh).__name__.lower():
raise TypeError(
"A mesh container is not supported by a region, use a mesh instead."
)

region.mesh = mesh

if element is not None:
Expand Down
30 changes: 30 additions & 0 deletions src/felupe/region/_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class RegionQuad(Region):
def __init__(
self, mesh, quadrature=GaussLegendre(order=1, dim=2), grad=True, **kwargs
):
if "container" in type(mesh).__name__.lower():
raise TypeError(
"A mesh container is not supported by a region, use a mesh instead."
)

element = Quad()

if len(mesh.cells.T) > 4:
Expand Down Expand Up @@ -117,6 +122,11 @@ class RegionQuadraticQuad(Region):
def __init__(
self, mesh, quadrature=GaussLegendre(order=2, dim=2), grad=True, **kwargs
):
if "container" in type(mesh).__name__.lower():
raise TypeError(
"A mesh container is not supported by a region, use a mesh instead."
)

element = QuadraticQuad()

if len(mesh.cells.T) > 8:
Expand Down Expand Up @@ -358,6 +368,11 @@ class RegionHexahedron(Region):
def __init__(
self, mesh, quadrature=GaussLegendre(order=1, dim=3), grad=True, **kwargs
):
if "container" in type(mesh).__name__.lower():
raise TypeError(
"A mesh container is not supported by a region, use a mesh instead."
)

element = Hexahedron()

if len(mesh.cells.T) > 8:
Expand Down Expand Up @@ -443,6 +458,11 @@ class RegionQuadraticHexahedron(Region):
def __init__(
self, mesh, quadrature=GaussLegendre(order=2, dim=3), grad=True, **kwargs
):
if "container" in type(mesh).__name__.lower():
raise TypeError(
"A mesh container is not supported by a region, use a mesh instead."
)

element = QuadraticHexahedron()

if len(mesh.cells.T) > 20:
Expand Down Expand Up @@ -647,6 +667,11 @@ class RegionTriangle(Region):
def __init__(
self, mesh, quadrature=TriangleQuadrature(order=1), grad=True, **kwargs
):
if "container" in type(mesh).__name__.lower():
raise TypeError(
"A mesh container is not supported by a region, use a mesh instead."
)

element = Triangle()

if len(mesh.cells.T) > 3:
Expand Down Expand Up @@ -682,6 +707,11 @@ class RegionTetra(Region):
"""

def __init__(self, mesh, quadrature=TetraQuadrature(order=1), grad=True, **kwargs):
if "container" in type(mesh).__name__.lower():
raise TypeError(
"A mesh container is not supported by a region, use a mesh instead."
)

element = Tetra()

if len(mesh.cells.T) > 4:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,40 @@ def test_region_negative_volumes_cells():
region = fem.RegionQuad(mesh)


def test_container_warning():

mesh = fem.MeshContainer([fem.Rectangle()])
element = fem.Quad()
quadrature = fem.GaussLegendre(order=1, dim=2)
with pytest.raises(TypeError):
region = fem.Region(mesh, element, quadrature)

mesh = fem.MeshContainer([fem.Rectangle()])
with pytest.raises(TypeError):
region = fem.RegionQuad(mesh)

mesh = fem.MeshContainer([fem.Rectangle().add_midpoints_edges()])
with pytest.raises(TypeError):
region = fem.RegionQuadraticQuad(mesh)

mesh = fem.MeshContainer([fem.Rectangle().triangulate()])
with pytest.raises(TypeError):
region = fem.RegionTriangle(mesh)

mesh = fem.MeshContainer([fem.Cube()])
with pytest.raises(TypeError):
region = fem.RegionHexahedron(mesh)

mesh = fem.MeshContainer([fem.Cube().add_midpoints_edges()])
with pytest.raises(TypeError):
region = fem.RegionQuadraticHexahedron(mesh)

mesh = fem.MeshContainer([fem.Cube().triangulate()])
with pytest.raises(TypeError):
region = fem.RegionTetra(mesh)


if __name__ == "__main__":
test_region()
test_region_negative_volumes_cells()
test_container_warning()

0 comments on commit 46b5093

Please sign in to comment.