pyoomph.meshes.gmsh module

class pyoomph.meshes.gmsh.GmshTemplate(loaded_from_mesh_file=None)[source]

Bases: MeshedMeshTemplate

A template for creating a mesh using Gmsh as backend. Specify the geometry in an overridden define_geometry() method, using the point(), line(), spline(), circle_arc(), plane_surface() and other methods.

Remeshing is done by recreation, i.e. define_geometry() is called again whenever remeshing is required. Use is_remeshing() there to tell the initial mesh from a remeshed one and get_boundary_coordinates() to rebuild the boundaries that have moved in the meantime.

class GmshFakeEntry(my_id, dim_tag)[source]

Bases: object

Just a fake entry to store the dimension and tag of an entity

add_mesh_size_field(typ, *, tag=-1, **kwargs)[source]

Adds a mesh size field of the given type with the given parameters. Returns the field id. See https://gmsh.info/doc/texinfo/#Gmsh-mesh-size-fields for more information on the available field types and their parameters.

circle_arc(startpt, endpt, *, center=None, through_point=None, name=None, with_macro_element=True)[source]

Adds a circlular arc to the mesh geometry.

Parameters:
  • startpt (Point | Sequence[Expression | GiNaC_GlobalParam | int | float]) – The starting point of the circle arc.

  • endpt (Point | Sequence[Expression | GiNaC_GlobalParam | int | float]) – The ending point of the circle arc.

  • center (Point | Sequence[Expression | GiNaC_GlobalParam | int | float] | None) – The center point of the circle arc. If not provided, it will be calculated based on the through_point argument.

  • through_point (Point | Sequence[Expression | GiNaC_GlobalParam | int | float] | None) – A point that the circle arc should pass through. If not provided, the circle arc requires a center.

  • name (str | None) – The name of the circle arc to identify it later e.g. as boundary.

  • with_macro_element (bool) – Whether to include a macro element. In that case, spatial refinements (on moving meshes only the initial refinement) will be mapped on the circle.

Return type:

Line | CircleArc | None

Returns:

The created circle arc or a line if the circle arc is degenerate.

Raises:
  • RuntimeError – If the geometry is not defined inside the ‘define_geometry’ function.

  • RuntimeError – If both center and through_point are provided.

create_lines(*args)[source]

Creates multiple lines with different names based on the given arguments. For line loop around a box, you can e.g. do

lines=self.create_lines((0,0), "left", (0,1), "top", (1,1), "right", (1,0), "bottom", (0,0))
self.plane_surface(*lines,name="box") # Create the surface of the box
Parameters:

*args (Point | list[Expression | GiNaC_GlobalParam | int | float] | tuple[Expression | GiNaC_GlobalParam | int | float, ...] | str) – Variable number of arguments representing the points and names of the lines. Each argument should be in the format: p1, <name>, p2, <name>, p3, <name>, p4, … If it stops with a name, not a point, it will close the line loop to the first point

Return type:

list[Line]

Returns:

A list of Line objects representing the created lines.

Raises:

ValueError – If the arguments are not in the correct format.

default_resolution: float | None

The default resolution for the mesh as a nondimensional typical element length scale

define_geometry()[source]

Override specifically to define the geometry of this mesh by adding points, lines, surfaces, etc.

ellipse_arc(startpt, endpt, center, pt_on_major_axis=None, name=None)[source]

Adds an ellipse arc to the mesh geometry.

Parameters:
  • startpt (Point | Sequence[Expression | GiNaC_GlobalParam | int | float]) – The starting point of the ellipse arc (must be on the major axis if ).

  • endpt (Point | Sequence[Expression | GiNaC_GlobalParam | int | float]) – The ending point of the ellipse arc.

  • center (Point | Sequence[Expression | GiNaC_GlobalParam | int | float]) – The center point of the ellipse arc.

  • pt_on_major_axis (Point | Sequence[Expression | GiNaC_GlobalParam | int | float] | None) – A point on the major axis of the ellipse. If not provided, startpt is assumed to be on the major axis.

  • name (str | None) – The name of the circle arc to identify it later e.g. as boundary.

Returns:

The created ellipse arc or a line if the ellipse arc is degenerate.

extrude(*args, shift=[0, 0, 1], recombine=False, start_name=<function GmshTemplate.<lambda>>, end_name=<function GmshTemplate.<lambda>>, layers=None)[source]

Extrudes the given entities by the given shift. The bulk surface name will become a volume and the line surfaces will become 2d surfaces. Additionally, the start and end surfaces of the extrusion will be named according to the given functions.

Parameters:
  • *args – Variable length arguments representing the entities to extrude. Can be names or the entities themselves.

  • shift (list[Expression | GiNaC_GlobalParam | int | float]) – The shift to extrude by. Can be a list of expressions or numbers.

  • recombine (bool) – Flag indicating whether to recombine the extruded entities.

  • start_name – Function to generate the start name of the extrusion.

  • end_name – Function to generate the end name of the extrusion.

  • layers (int | None) – Number of layers to extrude. If None, the extrusion will be determined by the mesh size.

extrude_generated_mesh: tuple[Expression | GiNaC_GlobalParam | int | float, int] | None

If set, the entire mesh will be extruded in the next dimension. The first entry in the tuple is the dimensional distance, the second the number of layers.

fix_2d_orientation: bool

If True (default), planar 2d elements that come out of Gmsh clockwise are relabelled during construction so that every element has a positive det(dx/ds). Gmsh orients the elements after the surface normal, i.e. after the winding of the curve loop, which for a loop assembled programmatically (e.g. by a Remesher2d) can come out either way. pyoomph integrates with sqrt(det(g_ab)), which is non-negative, so an inside-out mesh used to be harmless - but it makes set_detect_inverted_elements(True) flag the entire mesh. Also fixes the mirrored half of a mirror_mesh.

high_order_optimize: int

What Gmsh’s high-order optimizer is asked to do on an order=2 mesh (its Mesh.HighOrderOptimize): 0 not to run at all, 1 (the default) to optimize, 2 elastic analogy plus optimization, 3 elastic analogy, 4 fast curving. It improves the curving of elements next to a curved boundary, and on strongly anisotropic elements it can fail to reach its quality target - which used to end the process with an uncatchable SIGABRT and is now a warning (see _generate_without_aborting()). Set it to 0 if the warning shows up and the curving is of no concern.

kernel: Literal['geo', 'occ']

Selects the Gmsh geometry kernel. "geo" (default) is Gmsh’s built-in kernel; "occ" uses the OpenCASCADE kernel, which additionally supports boolean operations and CAD file import.

line(*args, name=None)[source]

Create a line (segment-wise) line for the mesh. When given a name, it can be used to identify the line later, e.g. for boundary conditions.

Parameters:
  • *args (Sequence[Expression | GiNaC_GlobalParam | int | float] | Point) – Variable-length argument list of points or sequences of points defining the line.

  • name (str | None) – Name of the line entity.

Return type:

Line | list[Line] | None

Returns:

The created line entity, or None if the line is degenerate. When more than two points are passed, one line per point pair is returned instead.

Raises:

RuntimeError – If the geometry is not defined inside the ‘define_geometry’ function.

make_lines_transfinite(*linesIn, numnodes='auto', mode='Progression', coeff=None, dry_run=False)[source]

Distributes the nodes on the given curves explicitly, instead of letting Gmsh size them from the point sizes.

Parameters:
  • *linesIn (Union[Line, Spline, BSpline, CircleArc, EllipseArc, str, None, Sequence[NestedGmshCurveArg]]) – The curves, by name or object, also as (nested) lists of these.

  • numnodes (Union[Literal['auto'], int]) – Number of nodes on each curve, or “auto” to take it from the point sizes of the individual curve.

  • mode (str) – Node distribution, e.g. “Progression” or “Bump”, passed on to Gmsh.

  • coeff (float | None) – Coefficient of the distribution, i.e. the grading. If None, it is calculated from the point sizes at both ends.

  • dry_run (bool) – Only calculate and return the numbers, without applying them to the curves.

Return type:

list[tuple[int, float]]

Returns:

One (numnodes, coeff) entry per curve, in the order the curves were resolved.

make_surface_transfinite(*surfsIn, corners=[], arrangement='')[source]

Meshes the given surfaces with a structured (transfinite) grid, i.e. by interpolating the node distribution of the bounding curves.

Parameters:
  • *surfsIn (Union[PlaneSurface, Surface, str, None, Sequence[NestedGmshSurfaceArg]]) – The surfaces, by name or object, also as (nested) lists of these, e.g. the return value of plane_surface.

  • corners (Sequence[Point]) – The four corner points spanning the grid. If empty, they are taken from the bounding curves, which requires a surface with exactly four edges and no holes.

  • arrangement (str) – How the elements are arranged, e.g. “Left”, “Right” or “Alternate”, passed on to Gmsh.

mesh_mode: Literal['quads', 'tris', 'SV', 'only_quads']

Selects the default element type of the mesh. Can be "quads" (try to create quads if possible), "tris" (only triangles), "SV" (Scott-Vogelius elements) or "only_quads" (only quadrilateral elements by splitting triangles)

mesh_size_factor: float

This factor is used to scale all size arguments (including the default resolution) by the given factor. Useful to e.g. increase the mesh resolution by a factor.

mirror_mesh: Literal['mirror_x', 'mirror_y'] | list[Literal['mirror_x', 'mirror_y']] | None

If set, the input mesh will be mirrored and copied along the given axis or axes. Useful to generate symmetric meshes for e.g. pitchfork tracking

num_flipped_2d_elements: int

How many elements the last mesh construction had to flip (see fix_2d_orientation). Read by dev_docs/examples/gmsh_orientation_matrix.py

order

The default order of the elements. Can be 1 or 2. Note that if only first order ("C1") elements are created, the mesh will be reduced to first order, even if the mesh is set to second order. Likewise, a first order mesh will be split to second order if second order elements ("C2") are created on it.

plane_surface(*args, name=None, holes=None, reversed_order=False)[source]

Creates a planar surface in the mesh. You must supply the enclosing boundaries (either by name or the line/circle_arc/spline/bspline objects) as arguments. If you give it a name, it can be used to add equations to the domain.

Parameters:
  • *args (Union[Line, Spline, BSpline, CircleArc, EllipseArc, str, None, Sequence[NestedGmshCurveArg]]) – Variable length arguments representing the lines or curves that define the boundary of the surface, in any order.

  • name (str | None) – Name of the surface for e.g. adding equations later on by the name.

  • holes (Sequence[Sequence[Union[Line, Spline, BSpline, CircleArc, EllipseArc, str, None, Sequence[Union[Line, Spline, BSpline, CircleArc, EllipseArc, str, None, Sequence[NestedGmshCurveArg]]]]]] | None) – List of holes within the surface, where each hole is defined by a sequence of lines or curves.

  • reversed_order (bool) – Flag indicating whether to reverse the order of the lines or curves.

Return type:

list[PlaneSurface]

Returns:

List of created plane surfaces. Can be multiple, if you have e.g. multiple disjunct domains circumscripted by the given lines/splines/circle_arcs.

point(x, y=0.0, z=0.0, size=None, *, name=None, consider_spatial_scale=None)[source]

Add a point to the geometry. Coordinates must be given in the spatial unit, e.g. in meter if the problem has a metric set_scaling(spatial=…) set. The size controls the mesh size and will default to self.default_resolution if not given. By a name, the point can be identified later, e.g. for a boundary condition.

Parameters:
  • x (Expression | GiNaC_GlobalParam | int | float) – The x-coordinate of the point.

  • y (Expression | GiNaC_GlobalParam | int | float) – The y-coordinate of the point. Defaults to 0.0.

  • z (Expression | GiNaC_GlobalParam | int | float) – The z-coordinate of the point. Defaults to 0.0.

  • size (Expression | GiNaC_GlobalParam | int | float | None) – The size of the point. Defaults to None, which means default_resolution. Negative sizes are in terms of default_resolution

  • name (str | None) – The name of the point. Defaults to None.

  • consider_spatial_scale (bool | None) – Whether to consider spatial scaling. Defaults to None.

Returns:

The created point. Can be used for e.g. creating lines, circle_arcs or splines.

Return type:

Point

Raises:
  • RuntimeError – If geometry is added outside the ‘define_geometry’ function.

  • RuntimeError – If the mesh resolution (size argument) is not a float.

revolve(*args, angle=0, axis=[0, 0, 1], center=[0, 0, 0], start_name=<function GmshTemplate.<lambda>>, end_name=<function GmshTemplate.<lambda>>, layers=None, recombine=False)[source]

Rotates the given entities by the given angle around the given axis and center.

Parameters:
  • *args – Variable length arguments representing the entities to rotate. Can be names or the entities themselves.

  • angle (Expression | GiNaC_GlobalParam | int | float) – The angle to rotate by. Can be an expression or a number.

  • axis (list[Expression | GiNaC_GlobalParam | int | float]) – The axis to rotate around. Can be a list of expressions or numbers.

  • center (list[Expression | GiNaC_GlobalParam | int | float]) – The center of rotation. Can be a list of expressions or numbers.

ruled_surface(*args, name=None, reversed_order=False, map_to_sphere=False)[source]

Adds a ruled surface spanned by the given bounding curves.

Parameters:
  • args (Union[Line, Spline, BSpline, CircleArc, EllipseArc, str, None, Sequence[NestedGmshCurveArg]]) – The bounding curves, by name or object.

  • name (str | None) – The name of the surface, to identify it later e.g. as boundary.

  • reversed_order (bool) – Whether to reverse the orientation of the curve loop.

  • map_to_sphere (bool | Sequence[float]) – Attach a spherical macro element to this surface, so that nodes created by spatial refinement are placed on the sphere rather than on the surface Gmsh actually meshed. Pass True to work the sphere out from the bounding curves, or a (cx, cy, cz) centre to state it explicitly. This is opt-in on purpose: a ruled surface is not in general a sphere – Gmsh’s built-in kernel does not produce an exact sphere from one even when the bounding curves are great-circle arcs – so pyoomph cannot assume it. Say so only when the surface really is meant to be spherical; the mesh’s own nodes on it are then also projected onto the exact sphere, which moves them slightly.

Return type:

list[Surface]

set_mesh_size_background_field(field_id)[source]

Sets the given mesh size field as the background mesh size field. See https://gmsh.info/doc/texinfo/#Gmsh-mesh-size-fields for more information on the available field types and their parameters.

set_mesh_size_boundary_layer_field(field_id)[source]

Registers the given field (which must be of type “BoundaryLayer”) as a boundary layer field, i.e. gmsh grows graded, wall-normal element layers from the curves the field was given. This is deliberately not the same as set_mesh_size_background_field: a boundary layer field does not describe a mesh size to be sampled, but a structured region to be extruded from the wall, so gmsh treats it separately. See https://gmsh.info/doc/texinfo/#Gmsh-mesh-size-fields for the available parameters.

set_recombined_surfaces(surfs, *more_surfs)[source]

Marks the given surfaces for recombination, i.e. Gmsh will merge the triangles it meshed them with into quadrilaterals.

Parameters:
  • surfs (Union[PlaneSurface, Surface, str, None, Sequence[NestedGmshSurfaceArg]]) – The surfaces, by name or object, also as a (nested) list of these, e.g. the return value of plane_surface.

  • *more_surfs (Union[PlaneSurface, Surface, str, None, Sequence[NestedGmshSurfaceArg]]) – Further surfaces in the same form.

sphere(origin, radius=1, surface_name=None, mesh_size=None, name=None, with_curved_entity=True)[source]

Adds a solid ball, i.e. a volume bounded by a sphere. Unlike ruled_surface() plus volume(), the ball is a single entity of the Gmsh kernel, so its boundary is one exact spherical surface instead of a patchwork of ruled surfaces.

Parameters:
  • origin (Point) – The centre of the ball.

  • radius (Expression | GiNaC_GlobalParam | int | float) – The radius of the ball, in the spatial unit - as for point(), i.e. in meter if the problem has a metric set_scaling(spatial=...).

  • surface_name (str | None) – Name of the bounding sphere, to identify it later as a boundary.

  • mesh_size (float | None) – Element size on the ball, defaulting to default_resolution. Negative sizes are relative to it, as for point().

  • name (str | None) – Name of the volume, i.e. the name of the bulk domain it will become.

  • with_curved_entity (bool) – Attach a spherical macro element to the bounding surface, so that nodes created by spatial refinement land on the sphere rather than on the facets Gmsh meshed it with. Unlike ruled_surface(map_to_sphere=...) this is on by default: the surface of a ball really is a sphere, so there is nothing to guess.

Return type:

Any

spline(ptlistIn, *, name=None, with_macro_element=True)[source]

Create a spline curve given by a list of points. If a name is supplied, it can be used for e.g. boundary conditions.

Parameters:
  • ptlistIn (Sequence[Point | Sequence[Expression | GiNaC_GlobalParam | int | float]]) – List of points defining the spline curve.

  • name (str | None) – Name of the spline curve. Default is None.

  • with_macro_element (bool) – Flag indicating whether to include a macro element for the spline curve. Default is True.

Returns:

The created spline curve.

Return type:

Spline

use_macro_elements: bool

If True, macro elements will be used for the mesh, i.e. curved elements will be considered