From 76eba51836cd8e40faed7cab6454ab5e6f318775 Mon Sep 17 00:00:00 2001 From: Andreas Dutzler Date: Sat, 15 Feb 2025 23:44:46 +0100 Subject: [PATCH] Add `MeshContainer.as_vertex_mesh()` --- src/felupe/__init__.py | 2 ++ src/felupe/element/__init__.py | 2 ++ src/felupe/element/_vertex.py | 47 +++++++++++++++++++++++++++++++++ src/felupe/mesh/_container.py | 5 ++++ src/felupe/region/__init__.py | 2 ++ src/felupe/region/_templates.py | 11 ++++++++ 6 files changed, 69 insertions(+) create mode 100644 src/felupe/element/_vertex.py diff --git a/src/felupe/__init__.py b/src/felupe/__init__.py index a5e16468..f5c10429 100644 --- a/src/felupe/__init__.py +++ b/src/felupe/__init__.py @@ -113,6 +113,7 @@ RegionTriangleMINI, RegionTriQuadraticHexahedron, RegionTriQuadraticHexahedronBoundary, + RegionVertex, ) from .tools import hello_world, newtonrhapson, project, runs_on, save, topoints from .view import ViewField, ViewMesh @@ -235,6 +236,7 @@ "RegionTriangleMINI", "RegionTriQuadraticHexahedron", "RegionTriQuadraticHexahedronBoundary", + "RegionVertex", "newtonrhapson", "project", "save", diff --git a/src/felupe/element/__init__.py b/src/felupe/element/__init__.py index 9ecbc8f7..461d54ff 100644 --- a/src/felupe/element/__init__.py +++ b/src/felupe/element/__init__.py @@ -15,6 +15,7 @@ from ._quad import BiQuadraticQuad, ConstantQuad, Quad, QuadraticQuad from ._tetra import QuadraticTetra, Tetra, TetraMINI from ._triangle import QuadraticTriangle, Triangle, TriangleMINI +from ._vertex import Vertex __all__ = [ "Element", @@ -37,4 +38,5 @@ "lagrange_line", "lagrange_quad", "lagrange_hexahedron", + "Vertex", ] diff --git a/src/felupe/element/_vertex.py b/src/felupe/element/_vertex.py new file mode 100644 index 00000000..b180c353 --- /dev/null +++ b/src/felupe/element/_vertex.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +""" +This file is part of FElupe. + +FElupe is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +FElupe is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with FElupe. If not, see . +""" + +import numpy as np + +from ._base import Element + + +class Vertex(Element): + r"""A vertex element formulation with constant shape functions. + + Notes + ----- + The vertex element is defined by one point. + """ + + def __init__(self): + self.points = np.array([[0.0]]) + self.cells = np.arange(len(self.points)).reshape(1, -1) + self.cell_type = "vertex" + + def function(self, r): + "Return the shape functions at given coordinate (r)." + return np.ones(1) + + def gradient(self, r): + "Return the gradient of shape functions at given coordinate (r)." + return np.zeros((1, 1)) + + def hessian(self, rs): + "Return the hessian of shape functions at given coordinate (r)." + return np.zeros((1, 1, 1)) diff --git a/src/felupe/mesh/_container.py b/src/felupe/mesh/_container.py index 17baa2ad..da17d445 100644 --- a/src/felupe/mesh/_container.py +++ b/src/felupe/mesh/_container.py @@ -279,6 +279,11 @@ def as_meshio(self, combined=True, **kwargs): return meshio.Mesh(self.points, cells, **kwargs) + def as_vertex_mesh(self): + "Return a merged vertex-mesh." + cells = np.unique([mesh.cells.ravel() for mesh in self.meshes]).reshape(-1, 1) + return Mesh(self.points, cells, cell_type=None) + def copy(self): "Return a deepcopy of the mesh container." return deepcopy(self) diff --git a/src/felupe/region/__init__.py b/src/felupe/region/__init__.py index 8569957b..4086467b 100644 --- a/src/felupe/region/__init__.py +++ b/src/felupe/region/__init__.py @@ -22,6 +22,7 @@ RegionTriangleMINI, RegionTriQuadraticHexahedron, RegionTriQuadraticHexahedronBoundary, + RegionVertex, ) __all__ = [ @@ -48,4 +49,5 @@ "RegionTriangleMINI", "RegionTriQuadraticHexahedron", "RegionTriQuadraticHexahedronBoundary", + "RegionVertex", ] diff --git a/src/felupe/region/_templates.py b/src/felupe/region/_templates.py index 3cdac385..ec25f8cb 100644 --- a/src/felupe/region/_templates.py +++ b/src/felupe/region/_templates.py @@ -32,6 +32,7 @@ Triangle, TriangleMINI, TriQuadraticHexahedron, + Vertex, ) from ..mesh import Mesh from ..quadrature import GaussLegendre, GaussLegendreBoundary @@ -852,3 +853,13 @@ class RegionQuadraticTetra(Region): def __init__(self, mesh, quadrature=TetraQuadrature(order=2), grad=True, **kwargs): element = QuadraticTetra() super().__init__(mesh, element, quadrature, grad=grad, **kwargs) + + +class RegionVertex(Region): + "A region with a vertex element." + + def __init__( + self, mesh, quadrature=GaussLegendre(order=0, dim=1), grad=False, **kwargs + ): + element = Vertex() + super().__init__(mesh, element, quadrature, grad=grad, **kwargs)