Skip to content

Meshing

A part is meshed from its geometry with one call, part.mesh(settings, section, ...). What the geometry is decides what you get: a line gives beams or bars, a surface shells, a STEP solid tetrahedra. The meshing is done by Gmsh, installed with xfea-mcp and with pip install "xfea[all]".

Give the part its geometry, then mesh it with a section. For a line, element_cls says what the elements are, and orientation is the beams’ local y axis.

from xfea.geometry import Line
from xfea.meshing import MeshingSettings
from xfea.model import BeamElement, ElasticIsotropic, Model, Part, RectangularSection
model = Model(name="meshing")
steel = ElasticIsotropic(E=210_000, v=0.3, density=7.85e-9) # N, mm, t
beam = model.add_part(Part(name="beam"))
beam.base_geometry = Line((0, 0, 0), (2000, 0, 0))
beam.mesh(
settings=MeshingSettings(max=100), # elements of 100 mm at most
section=RectangularSection(w=50, h=100, material=steel),
element_cls=BeamElement,
orientation=[0, 1, 0],
)
print(len(beam.nodes), len(beam.elements)) # 21 nodes, 20 elements

MeshingSettings(divisions=8) cuts each line into exactly 8 elements instead. base_geometry also takes a polyline, or a list of lines or polylines. TrussElement, TieElement and StrutElement are meshed the same way, with their sections.

A surface is given as a mesh of its faces. There are two ways to mesh it:

from xfea.geometry import Mesh
from xfea.model import ShellSection
concrete = ElasticIsotropic(E=30_000, v=0.2, density=2.4e-9)
slab = model.add_part(Part(name="slab"))
slab.base_geometry = Mesh.from_meshgrid(4000, 8, 2000, 4) # 4 m by 2 m in the xy plane, 8 x 4 quads
# Keep the faces: each quad becomes 2 x 2 quadratic quads
slab.mesh(settings=MeshingSettings(divisions=2, element_order=2), section=ShellSection(t=200, material=concrete))
# Or mesh the surface again in triangles of the size asked for
slab.mesh(settings=MeshingSettings(max=250, element_order=2), section=ShellSection(t=200, material=concrete))

divisions keeps the faces you gave, each cut into divisions x divisions elements, quads staying quads: divisions=1 means the mesh given is the mesh. Without it, the surface is remeshed. Mesh(vertices, faces) takes any vertices and faces of your own.

from xfea.model import SolidSection
block = model.add_part(Part(name="block"))
block.base_geometry = "bracket.step" # the path of a STEP file
block.mesh(
settings=MeshingSettings(max=10, element_order=2),
section=SolidSection(material=steel),
)

A STEP part has solids, faces, edges and points, each with properties: a face is a plane with a normal, or a cylinder with an axis and a radius; an edge is a straight line with a direction, or a circle. Select picks them by those properties, and groups turns each selection into a group of the meshed part’s nodes, to put supports and loads on:

from xfea.meshing import Select
block.mesh(
settings=MeshingSettings(max=10, element_order=2),
section=SolidSection(material=steel),
groups={
"base": Select.faces(normal="-z"), # the faces facing down
"bolt_holes": Select.holes(radius_max=9), # the walls of holes up to 18 mm across
"front_edge": Select.edges(near=(50, 0, 20)), # the edge nearest that point
},
)
model.add_fix_bc(nodes=block.find_group_by_name("base"))

What a selection can say:

Selection Picks
Select.faces(normal="+z") Plane faces whose outward normal is +z ("z": either way).
Select.faces(on_plane=((0, 0, 20), "z")) Faces lying in the plane z = 20.
Select.faces(type="cylinder", along="x", radius_max=12) Cylinders along x, of radius 12 at most.
Select.holes() Holes’ walls: round faces with their material away from the axis, going at least half way round. Not pins, not fillets.
Select.edges(near=(x, y, z)) The edge nearest a point (count=3: the 3 nearest; within=5: all within 5).
Select.edges(type="line", along="z") Every straight vertical edge.
Select.edges(type="circle", touching=Select.holes()) The holes’ rims: edges of what another selection picks (a hole’s wall also has a straight seam).
Select.points(near=(x, y, z)) The geometry’s corner nearest a point.
Select.solids(named="BasePlate") A solid by its name in the STEP file (a CAD tool’s body label).

Every condition given must hold, and selections pick by properties, never by numbers, which change when the CAD file does. A surface mesh and lines have faces, edges and points too: Select.points(near=...) names a beam’s end.

describe lists a STEP part’s entities with their properties, or only what a selection picks: the way to check a selection before meshing with it.

from xfea.meshing import describe
for face in describe("bracket.step", what="faces"):
print(face)
# face 3, plane, area 5485, normal (0, 0, 1), centre (50.2, 30, 20)
# face 7, cylinder, area 1257, radius 10, axis (0, 0, 1), hole wall, centre (30, 30, 10)
# face 8, cylinder, area 754, radius 8, axis (0, 0, 1), outer, centre (75, 30, 27.5)
print(describe("bracket.step", select=Select.holes()))

Your AI assistant does the same with its describe_geometry tool: ask it to “refine along the top front edge” and it lists the candidates, shows them to you, and writes the selection into the script.

refine makes the mesh finer on what a selection picks, in a box or in a ball, growing back to the part’s size over reach (4 x size by default). The rest stays coarse.

from xfea.meshing import Refine
block.mesh(
settings=MeshingSettings(max=10, element_order=2, refine=[
Refine(Select.holes(), size=1.5, reach=8), # around the holes
Refine(Select.edges(near=(50, 0, 20)), size=2), # along one edge
Refine.in_box((0, 0, 0), (100, 60, 5), size=4), # a slice near the base
Refine.around((30, 30, 20), radius=15, size=2), # a ball round a point
]),
section=SolidSection(material=steel),
)

The same works on lines (Refine.around(tip, radius=50, size=5) shortens a beam’s elements near its tip) and on surfaces that are remeshed. To refine everywhere, lower max; there is no splitting of an existing mesh: mesh again with the new settings, and compare the results to see that they have converged.

Setting What it is
min, max The smallest and the largest element size, in the model’s length unit.
element_order 1 for linear elements, 2 for quadratic ones, with midside nodes.
refine Where the mesh is finer: a list of Refine.
divisions For a surface mesh or lines: keep the faces (segments), each cut into this many.
optimize Gmsh’s mesh optimisation setting.

Gmsh’s own options go through its settings class, which MeshingSettings would drop:

from xfea_gmsh import GmshMeshingSettings
block.mesh(settings=GmshMeshingSettings(max=10, element_order=2, algorithm_3d=1), section=SolidSection(material=steel))
  • model.add_parts_from_step(path, parts=[...], refine=[...]) meshes the solids of a STEP assembly, each into a part of its own; refine works across the parts as above.
  • model.add_parts_from_mesh(path, parts=[...]) reads a mesh made elsewhere: Gmsh .msh, Abaqus .inp, VTK, Salome .med.

Both are in the API reference. Parts meshed apart are joined by interfaces, found with model.detect_interfaces(...).

Besides the groups a mesh is given, nodes can be found by where they are:

from xfea.geometry import Plane
start = beam.find_closest_nodes_to_point((0, 0, 0)) # the node nearest a point
edge = slab.find_nodes_on_plane(Plane((0, 0, 0), (1, 0, 0))) # the nodes on the plane x = 0
top = block.nodes.subgroup(lambda n: n.z > 19.9) # any rule of your own

Nodes and elements can also be made one by one, with part.add_node(Node(xyz=...)) and part.add_element(BeamElement(nodes=[a, b], section=section)): the model on the Analysis page is built that way.

  • “A refinement selects nothing” or “The group … selects nothing”: check the selection with describe(path, select=...).
  • To see Gmsh’s own output, set settings.MESH_VERBOSE = True (from xfea import settings).
  • xfea.meshing.failed_algorithms() gives the meshing plugins that failed to load, each with its error.

© 2026 Francesco Ranaudo · Text and pictures CC BY 4.0 · Code Elastic License 2.0 · how to reuse them