Skip to content

A cantilever beam

A first analysis from start to end: a steel cantilever 2 m long, 50 mm by 100 mm, with 1 kN at its tip. It is small enough to check by hand, which is the point.

The blocks below, one after the other, are the whole script. Save them as cantilever.py and run it with python cantilever.py. Units are N, mm, MPa and tonnes throughout.

Before you start: xfea installed with Gmsh meshing, and solving set up. See Setup.

A model holds parts. A part holds nodes and elements, and each element has a section, which has a material.

from pathlib import Path
from xfea.geometry import Line
from xfea.meshing import MeshingSettings
from xfea.model import BeamElement, ElasticIsotropic, Model, Part, RectangularSection
length = 2000.0 # mm
load = -1000.0 # N, down
model = Model(name="cantilever")
steel = ElasticIsotropic(E=210_000, v=0.3, density=7.85e-9)
section = RectangularSection(w=50, h=100, material=steel)

The part gets its geometry, a line along x, and is meshed into beam elements about 100 mm long.

part = model.add_part(Part(name="beam"))
part.base_geometry = Line((0, 0, 0), (length, 0, 0))
part.mesh(
settings=MeshingSettings(max=100),
section=section,
element_cls=BeamElement,
orientation=[0, 1, 0],
)
print(len(part.nodes), "nodes,", len(part.elements), "elements")

orientation=[0, 1, 0] is the beams’ local y axis, the axis the section’s Ixx is about. With it, the section’s 100 mm is vertical and Ixx = w h³ / 12 carries the vertical load.

Supports go on nodes, found by where they are.

start = part.find_closest_nodes_to_point((0, 0, 0))
tip = part.find_closest_nodes_to_point((length, 0, 0))
model.add_fix_bc(nodes=start)
for warning in model.check():
print(warning)

add_fix_bc holds the three translations and the three rotations. model.check() lists what a solver would get wrong without saying so; here it has nothing to say.

A problem is what happens to the model: one static step, with the load and the results to keep.

from xfea.problem import Problem, StaticStep
from xfea.results import DisplacementFieldResults, ReactionFieldResults
problem = model.add_problem(Problem(name="tip_load"))
step = problem.add_step(StaticStep(name="load"))
step.add_uniform_forcefield(nodes=tip, z=load)
disp = DisplacementFieldResults()
react = ReactionFieldResults()
step.add_field_outputs([disp, react])
output = Path(__file__).resolve().parent / "output"
problem.analyse_and_extract(path=output)

The solver’s input is written through the conversion service, the solver runs on your machine, and the results are read into output/tip_load/tip_load-results.db.

deflection = disp.get_min_result("z").z
by_hand = load * length**3 / (3 * steel.E * section.Ixx)
print(f"tip deflection: {deflection:.3f} mm") # -3.038
print(f"by hand, P L³ / 3 E I: {by_hand:.3f} mm") # -3.048
print(f"reaction: {react.get_max_result('z').z:.1f} N") # 1000.0

The two deflections differ by 0.3 %: the solver’s twenty beam elements are slightly stiffer than the formula’s beam. The reaction equals the load.

problem.record_result("tip deflection", deflection, "mm", note=f"by hand {by_hand:.3f} mm")
print(problem.path)

Open report.html in that folder: the result you just kept, the solve’s convergence, the model’s make-up and the warnings, on one page.

  • Loads: loads along the beam, self-weight, pressure, imposed displacements.
  • Analysis: large displacements, natural frequencies, buckling.
  • Viewers: the model and its results in your browser.

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