Three stacked cubes with frictional contact
Demonstrates how to build a multi-part assembly by copying and translating a meshed solid part, detecting contact interfaces between adjacent blocks, and running a gravity + compression analysis.
The interface detection uses method="face_intersection" which works
correctly for non-conforming meshes (the copies have different node positions
at the contact faces).

The model before it is solved: its parts, supports (red) and loads (blue).
![The result: the model coloured by stress (vonMises [MPa])](/examples/workflows/assemblies/stacked-cubes-results.webp)
The model, 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/03_workflows/assemblies/stacked_cubes.py. Run it from the repository with uv run python examples/scripts/03_workflows/assemblies/stacked_cubes.py, or, with xfea-examples installed, xfea-examples run 03_workflows/assemblies/stacked_cubes.py.
from xfea.geometry import Translation
from xfea.model import HardContactFrictionPenaltyfrom xfea.problem import Problemfrom xfea.problem import StaticStepfrom xfea.results import DisplacementFieldResultsfrom xfea.results import StressFieldResultsfrom xfea_examples import out_dirfrom xfea_examples import show_viewerfrom xfea_examples.models import solid_box_model
# Model — three identical blocks stacked along Z# The box.stp asset is a 1000 × 1000 × 1000 mm cube.cube_edge = 1000 # [mm] cube edge length (matches box.stp asset)mu = 0.6 # [-] friction coefficient at interfacesg = 9810 # [mm/s^2] gravitytotal_top_load = -1_000 # [N] additional compression on top block (-1 kN)
model, part_a, _, _, _ = solid_box_model(name="stacked_cubes")print(f"{part_a.name} added to the model")
part_b = part_a.copy()part_b.name = "cube_2"part_b.transform(Translation.from_vector([0, 0, cube_edge]))model.add_part(part_b)
part_c = part_a.copy()part_c.name = "cube_3"part_c.transform(Translation.from_vector([0, 0, 2 * cube_edge]))model.add_part(part_c)
print(f"{part_b.name} and {part_c.name} added to the model")# Contact interfaces# Only adjacent cubes share a contact face. Providing explicit part_pairs# avoids checking the non-touching pair (part_a ↔ part_c) and is also a good# pattern to follow for larger assemblies where it gives a significant speedup.# face_intersection handles non-conforming node positions at the interfaces.contact = HardContactFrictionPenalty(mu=mu, tol=1)model.detect_interfaces( behavior=contact, method="face_intersection", part_pairs=[(part_a, part_b), (part_b, part_c)],)
print(f"Interfaces detected: {len(model.interfaces)}")
# Boundary conditionsbase_nodes = part_a.find_nodes_on_plane(part_a.bottom_plane)model.add_fix_bc(base_nodes)
# Problemproblem = model.add_problem(Problem(name="stacked_cubes"))step = problem.add_step( StaticStep( name="gravity_and_load", nlgeom=True, initial_inc_size=0.1, min_inc_size=1e-4, max_inc_size=0.5, ))
# Gravity self-weight on all blocksstep.add_gravity_field(g=g)
# Additional compression on top of the third blocktop_nodes = part_c.find_nodes_on_plane(part_c.top_plane)step.add_uniform_forcefield( nodes=top_nodes, z=total_top_load / max(1, len(top_nodes)), load_case="LL",)
disp = DisplacementFieldResults()stress = StressFieldResults()step.add_field_outputs([disp, stress])
# Analysisproblem.analyse_and_extract(path=out_dir(model.name), verbose=True, cpus=4)
print(f"Maximum vertical displacement: {disp.get_min_result('z').z}")
show_viewer(model, results={disp: "z"}, units="mm")© 2026 Francesco Ranaudo · Text and pictures CC BY 4.0 · Code Elastic License 2.0 · how to reuse them