Geometry Construction

Modules

Built-in Engine

Geometry

This class provides a Python interface for the Gmsh scripting language. It aims at working around some of Gmsh’s inconveniences (e.g., having to manually assign an ID for every entity created) and providing access to Python’s features.

In Gmsh, the user must manually provide a unique ID for every point, curve, volume created. This can get messy when a lot of entities are created and it isn’t clear which IDs are already in use. Some Gmsh commands even create new entities and silently reserve IDs in that way. This module tries to work around this by providing routines in the style of add_point(x) which _return_ the ID. To make variable names in Gmsh unique, keep track of how many points, circles, etc. have already been created. Variable names will then be p1, p2, etc. for points, c1, c2, etc. for circles and so on.

class pygmsh.built_in.geometry.Geometry(gmsh_major_version=3)

Bases: object

add_background_field(fields, aggregation_type='Min')

Create background field.

Parameters:
  • fields (expression) – General mesh size (non-uniform) background fields.
  • aggregation_type (expression) – Type of field (e.g. PostView, Min) with own options.
add_ball(x0, radius, lcar, with_volume=True, holes=None)

Create a ball with midpoint \(x_0\).

Parameters:
  • x0 (array-like[3]) – Midpoint coordinates of ball.
  • radius (float) – Radius of ball in all three coordinate directions.
  • lcar (float) – Characteristic length of ball.
  • with_volume (boolean) – Whether to construct a volume from the ball.
  • holes (expression) – Holes to add to ball.
add_boundary_layer(edges_list=None, faces_list=None, nodes_list=None, anisomax=None, hfar=None, hwall_n=None, ratio=None, thickness=None)

Create boundary layer field with associated options.

Parameters:
  • edges_list (list) – Indices of curves in the geometric model.
  • faces_list (list) – Indices of surfaces in the geometric model (Experimental feature).
  • nodes_list (list) – Indices of nodes in the geometric model.
  • anisomax (float) – Threshold angle for creating a mesh fan in the boundary layer.
  • hfar (float) – Element size far from the wall.
  • hwall_n (float) – Mesh size normal to the wall.
  • ratio (float) – Size ratio between two successive layers.
  • thickness (float) – Maximal thickness of the boundary layer.
add_box(x0, x1, y0, y1, z0, z1, lcar, with_volume=True, holes=None)

Create a box.

Parameters:
  • x0 (float) – First (either leftmost or rightmost) x-coordinate of box.
  • x1 (float) – Second (either leftmost or rightmost) x-coordinate of box.
  • y0 (float) – First (either highest or lowest) y-coordinate of box.
  • y1 (float) – Second (either highest or lowest) y-coordinate of box.
  • Z0 (float) – First (either deepest or shallowest) z-coordinate of box.
  • Z1 (float) – Second (either deepest or shallowest) z-coordinate of box.
  • lcar (float) – Characteristic length of ball.
  • with_volume (boolean) – Whether to construct a volume from the ball.
  • holes (expression) – Holes to add to ball.
add_bspline(*args, **kwargs)

Create a BSpline.

Parameters:*args

Variable length argument list of control_points.

**kwargs
Arbitrary keyword arguments describing control_points.
Returns:Bspline object according to control_points.
Return type:p
add_circle(x0, radius, lcar, R=None, compound=False, num_sections=3, holes=None, make_surface=True)

Add circle in the \(x\)-\(y\)-plane.

Parameters:
  • x0 (array-like[3]) – Midpoint coordinates of circle.
  • radius (float) – Radius of the circle.
  • lcar (float) – Characteristic length of the circle.
  • R (array-like[3]) – Transformation point coordinates.
  • compound (boolean) – Create compound line from circle arcs.
  • num_sections (number) – Number of circle arc sections.
  • holes (expression) – Holes to add to the circle.
  • make_surface (boolean) – Whether to make a surface of the circle.
add_circle_arc(*args, **kwargs)

Create a Circle arc.

Parameters:
  • *args – Variable length argument list of start, center and end points.
  • **kwargs – Arbitrary keyword arguments describing start, center and end points.
Returns:

CircleArc object according to start, center and end points.

Return type:

p

add_comment(string)

Create comment for Gmsh code.

Parameters:string (string) – Value to add as comment.
add_compound_line(*args, **kwargs)

Create a compound line.

Parameters:*args

Variable length argument list of lines.

**kwargs
Arbitrary keyword arguments describing lines.
Returns:CompoundLine object according to lines.
Return type:e
add_compound_surface(*args, **kwargs)

Create a compound surface.

Parameters:**kwargs (*args,) –
add_compound_volume(*args, **kwargs)

Create a compound volume.

Parameters:**kwargs (*args,) –
add_ellipse_arc(*args, **kwargs)

Create an elliptical arc.

Parameters:**kwargs (*args,) –
add_ellipsoid(x0, radii, lcar, with_volume=True, holes=None)

Create an ellipsoid with radii around a given midpoint \(x_0\) (not available in regular Gmsh).

Parameters:
  • x0 (array-like[3]) – Midpoint coordinates of ellipsoid.
  • radii (array-like[3]) – Radius of ellipsoid in three coordinate directions.
  • lcar (float) – Characteristic length of ellipsoid.
  • with_volume (boolean) – Whether to construct a volume from the ellipsoid.
  • holes (expression) – Holes to add to ellipsoid.
add_line(*args, **kwargs)

Create a line.

Parameters:**kwargs (*args,) –
add_line_loop(*args, **kwargs)

Create a line loop.

Parameters:**kwargs (*args,) –
add_physical_line(lines, label=None)

Create physical line.

Parameters:
  • lines (array[line]) – Lines to classify as physical lines.
  • label (string) – Label of physical line.
add_physical_point(points, label=None)

Create physical point.

Parameters:
  • points (array[point]) – Points to classify as physical points.
  • label (string) – Label of physical point.
add_physical_surface(surfaces, label=None)

Create physical surface.

Parameters:
  • surfaces (array[surface]) – Surfaces to classify as physical surfaces.
  • label (string) – Label of physical surface.
add_physical_volume(volumes, label=None)

Create physical volume.

Parameters:
  • volumes (array[volume]) – Volumes to classify as physical volumes.
  • label (string) – Label of physical volume.
add_pipe(outer_radius, inner_radius, length, R=array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]), x0=array([ 0., 0., 0.]), lcar=0.1, variant='rectangle_rotation')

Create a pipe.

Parameters:
  • outer_radius (float) – Outer radius of the pipe.
  • inner_radius (float) – Inner radius of the pipe.
  • length (float) – Length of the pipe.
  • R (array-like[N][3]) – Directional vector coordinates for pipe transformation.
  • x0 (array-like[3]) – Center point coordinates.
  • lcar (float) – Characteristic length of pipe.
  • variant (expression) – Type of pipe construction (e.g. rectangle_rotation, circle_extrusion).
add_plane_surface(*args, **kwargs)

Create a plane surface.

Parameters:**kwargs (*args,) –
add_point(*args, **kwargs)

Create a point.

Parameters:**kwargs (*args,) –
add_polygon(X, lcar, holes=None, make_surface=True)

Create a polygon.

Parameters:
  • X (array-like[N][3]) – Set of point coordinates for polygon.
  • lcar (float) – Characteristic length of polygon.
  • holes (expression) – Holes to add to polygon.
  • make_surface (boolean) – Whether to construct a surface on the polygonial plane.
add_raw_code(string_or_list)

Add raw Gmsh code.

Parameters:string_or_list (string or list) – Value to add code to Gmsh output code.
add_rectangle(xmin, xmax, ymin, ymax, z, lcar, holes=None, make_surface=True)

Create a rectangle using built-in geometry (not available in regular Gmsh).

Parameters:
  • xmin (float) – Leftmost x-coordinate.
  • xmax (float) – Rightmost x-coordinate.
  • ymin (float) – Lowest y-coordinate.
  • ymax (float) – Highest y-coordinate.
  • z (float) – z-plane coordinate.
  • lcar (float) – Characteristic length of rectangle.
  • holes (expression) – Holes to add to rectangle.
  • make_surface (boolean) – Whether to construct a surface on the rectangular plane.
add_spline(*args, **kwargs)

Create a spline.

Parameters:**kwargs (*args,) –
add_surface(*args, **kwargs)

Create a surface.

Parameters:**kwargs (*args,) –
add_surface_loop(*args, **kwargs)

Create a surface loop.

Parameters:**kwargs (*args,) –
add_torus(irad, orad, lcar, R=array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]), x0=array([ 0., 0., 0.]), variant='extrude_lines')

Create a torus in the x-y direction using .. math:: hat{x} = R x + x_0.

Parameters:
  • irad (float) – Inner radius of torus.
  • orad (float) – Outer radius of torus.
  • lcar (float) – Characteristic length of torus.
  • R (array-like[N][3]) – Directional vector coordinates for torus transformation.
  • x0 (array-like[3]) – Center coordinates of torus.
  • variant (expression) – Torus type description (e.g. extrude_lines, extrude_circle).
add_volume(*args, **kwargs)

Create a volume.

Parameters:**kwargs (*args,) –
extrude(input_entity, translation_axis=None, rotation_axis=None, point_on_axis=None, angle=None, num_layers=None, recombine=False)

Extrusion (translation + rotation) of any entity along a given translation_axis, around a given rotation_axis, about a given angle. If one of the entities is not provided, this method will produce only translation or rotation.

Parameters:
  • input_entity (object) – Entity to perform extrusion on.
  • translation_axis (array-like[3]) – Axis of translation coordinates.
  • rotation_axis (array-like[3]) – Axis of rotation coordinates.
  • point_on_axis (array-like[3]) – Coordinates of point on axis of translation and rotation.
  • angle (float) – Angle of rotation.
  • recombine (boolean) – Whether to recombine the extrusion layers.
get_code()

Return properly formatted Gmsh code.

translate(input_entity, vector)

Translate input_entity itself by vector. Changes the input object.

Parameters:
  • input_entity (expression) – The entity to perform translation on.
  • vector (vector) – The translation vector.

Bspline

class pygmsh.built_in.bspline.Bspline(control_points)

Bases: pygmsh.built_in.line_base.LineBase

Bspline of type LineBase.

Parameters:control_points (array-like[N][3]) – Coordinates of control points needed to construct b-spline.

CircleArc

class pygmsh.built_in.circle_arc.CircleArc(start, center, end)

Bases: pygmsh.built_in.line_base.LineBase

CircleArc of type LineBase.

Parameters:
  • start (array-like[3]) – Coordinates of start point needed to construct circle-arc.
  • center (array-like[3]) – Coordinates of center point needed to construct circle-arc.
  • end (array-like[3]) – Coordinates of end point needed to construct circle-arc.

CompoundLine

class pygmsh.built_in.compound_line.CompoundLine(lines)

Bases: pygmsh.built_in.line_base.LineBase

CompoundLine of type LineBase.

Parameters:lines (array-like[N]) – Lines to add to compound line.

CompoundSurface

class pygmsh.built_in.compound_surface.CompoundSurface(surfaces)

Bases: pygmsh.built_in.surface_base.SurfaceBase

CompoundSurface of type SurfaceBase.

Parameters:surfaces (array-like[N]) – Surfaces to add to compound surface.

EllipseArc

class pygmsh.built_in.ellipse_arc.EllipseArc(start, center, point_on_major_axis, end)

Bases: pygmsh.built_in.line_base.LineBase

EllipseArc of type LineBase.

Parameters:
  • start (array-like[3]) – Coordinates of start point needed to construct circle-arc.
  • center (array-like[3]) – Coordinates of center point needed to construct circle-arc.
  • point_on_major_axis (array-like[3]) – Point on the center axis of ellipse.
  • end (array-like[3]) – Coordinates of end point needed to construct circle-arc.

OpenCASCADE Engine

class pygmsh.opencascade.geometry.Geometry(characteristic_length_min=None, characteristic_length_max=None)

Bases: pygmsh.built_in.geometry.Geometry

add_ball(*args, **kwargs)

Create a ball.

Parameters:**kwargs (*args,) –
add_box(*args, **kwargs)

Create a box.

Parameters:**kwargs (*args,) –
add_cone(*args, **kwargs)

Create a cone.

Parameters:**kwargs (*args,) –
add_cylinder(*args, **kwargs)

Create a cylinder.

Parameters:**kwargs (*args,) –
add_disk(*args, **kwargs)

Create a disk.

Parameters:**kwargs (*args,) –
add_rectangle(*args, **kwargs)

Create a rectangle.

Parameters:**kwargs (*args,) –
add_torus(*args, **kwargs)

Create a torus.

Parameters:**kwargs (*args,) –
add_wedge(*args, **kwargs)

Create a wedge.

Parameters:**kwargs (*args,) –
boolean_difference(*args, **kwargs)

Boolean difference, see https://gmsh.info/doc/texinfo/gmsh.html#Boolean-operations input_entity and tool_entity are called object and tool in gmsh documentation.

boolean_fragments(*args, **kwargs)

Boolean fragments, see https://gmsh.info/doc/texinfo/gmsh.html#Boolean-operations input_entity and tool_entity are called object and tool in gmsh documentation.

boolean_intersection(entities, delete_first=True, delete_other=True)

Boolean intersection, see https://gmsh.info/doc/texinfo/gmsh.html#Boolean-operations input_entity and tool_entity are called object and tool in gmsh documentation.

boolean_union(entities, delete_first=True, delete_other=True)

Boolean union, see https://gmsh.info/doc/texinfo/gmsh.html#Boolean-operations input_entity and tool_entity are called object and tool in gmsh documentation.

extrude(input_entity, translation_axis)

Extrusion (translation + rotation) of any entity along a given translation_axis, around a given rotation_axis, about a given angle. If one of the entities is not provided, this method will produce only translation or rotation.

get_code()

Return properly formatted Gmsh code.

Functions

pygmsh Helpers

pygmsh.helpers.generate_mesh(geo_object, optimize=True, num_lloyd_steps=1000, verbose=True, dim=3, prune_vertices=True, gmsh_path=None, geom_order=1, geo_filename=None)
pygmsh.helpers.get_gmsh_major_version(gmsh_exe='gmsh')
pygmsh.helpers.rotation_matrix(u, theta)

Return matrix that implements the rotation around the vector \(u\) by the angle \(\theta\), cf. https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle.

Parameters:
  • u – rotation vector
  • theta – rotation angle