Making the mesh finer where it matters
Refine makes the mesh finer on what a selection picks (a hole, an edge, a face), in a box or round a
point, and lets the size grow back to the part’s over reach. A plate with a bolt hole, held at one end
and pulled at the other, is solved with the hole refined further each time: the peak stress at the hole
settles (within 1 % from 2 to 1 mm) while the rest of the mesh stays coarse, and lands on the hand
check. A mesh is converged when refining it again no longer changes the answer.

The model before it is solved: its parts, supports (red) and loads (blue).
![The result: the deformed shape coloured by stress (vonMises [MPa])](/examples/core-concepts/geometry-and-meshing/refine-the-mesh-results.webp)
The deformed shape, coloured by stress (vonMises [MPa]), in xfea’s 3D viewer.
Open in 3D turn it, cut it, pick any field: in xfea's viewer, in your browser
The script
Section titled “The script”examples/scripts/02_core_concepts/geometry_and_meshing/refine_the_mesh.py. Run it from the repository with uv run python examples/scripts/02_core_concepts/geometry_and_meshing/refine_the_mesh.py, or, with xfea-examples installed, xfea-examples run 02_core_concepts/geometry_and_meshing/refine_the_mesh.py.
from xfea.meshing import MeshingSettingsfrom xfea.meshing import Refinefrom xfea.meshing import Selectfrom xfea.model import ElasticIsotropicfrom xfea.model import Modelfrom xfea.model import Partfrom xfea.model import SolidSectionfrom xfea.problem import Problemfrom xfea.problem import StaticStepfrom xfea.results import StressFieldResultsfrom xfea_examples import get_assetfrom xfea_examples import out_dirfrom xfea_examples import show_viewer
path = get_asset("plate_hole_pin.stp") # 100 x 60 x 20 mm, a 20 mm bolt hole at (30, 30)steel = ElasticIsotropic(E=210_000, v=0.3, density=7.85e-9) # N, mm, tpull = 60_000.0 # [N] on the end face x = 100: 50 MPa on the plate's 60 x 20 section
peaks = []for size in (4.0, 2.0, 1.0): # the element size at the hole; 8 mm elsewhere model = Model(name=f"refine_the_mesh_{size:g}") plate = model.add_part(Part(name="plate")) plate.base_geometry = path plate.mesh( settings=MeshingSettings(max=8, element_order=2, refine=[Refine(Select.holes(), size=size, reach=8 * size)]), section=SolidSection(material=steel), groups={"held": Select.faces(normal="-x"), "pulled": Select.faces(normal="+x"), "hole": Select.holes()}, ) held, pulled = plate.find_group_by_name("held"), plate.find_group_by_name("pulled") model.add_fix_bc(nodes=held)
problem = model.add_problem(Problem(name="pull")) step = problem.add_step(StaticStep(name="pull")) step.add_uniform_forcefield(nodes=pulled, x=pull / len(pulled)) # the total, shared over the face's nodes stress = StressFieldResults() step.add_field_outputs([stress]) problem.analyse_and_extract(path=out_dir(model.name))
# The largest von Mises stress in the elements touching the hole's wall (a stress is an element's). hole_nodes = {node.key for node in plate.find_group_by_name("hole")} near_hole = [r.von_mises_stress for r in stress.get_results() if any(node.key in hole_nodes for node in r.location.nodes)] peaks.append(max(near_hole)) change = f"{100 * (peaks[-1] / peaks[-2] - 1):+.1f} %" if len(peaks) > 1 else "" print(f"hole elements {size:>3g} mm: {len(plate.nodes):>6} nodes, peak von Mises at the hole " f"{peaks[-1]:6.1f} MPa {change}")
# A hand check: the net section (60 - 20 mm wide) carries 75 MPa, and for a hole a third of the width across the# stress concentration factor on it is about 2 + (1 - d/W)^3 (Heywood), 2.30.net = pull / ((60 - 20) * 20)kt = 2 + (1 - 20 / 60) ** 3print(f"\nHand check: {net:.0f} MPa on the net section x {kt:.2f} = {net * kt:.0f} MPa; " f"the finest mesh is {100 * (peaks[-1] / (net * kt) - 1):+.1f} % from it.")
show_viewer(model, results={stress: "von_mises_stress"}, units="mm")© 2026 Francesco Ranaudo · Text and pictures CC BY 4.0 · Code Elastic License 2.0 · how to reuse them