First simulation¶
A soft cantilever beam, clamped at one end, sags under gravity for one second and is then shown in the polyscope viewer:
import numpy as np
import polyscope as ps
import trusty
mesh = trusty.make_beam_hex_mesh(size=(1.0, 0.1, 0.1), res=(20, 2, 2))
material = trusty.StableNeoHookean(youngs_modulus=1e6, poisson_ratio=0.3)
world = trusty.World(timestep=1.0 / 60.0)
beam = trusty.fem.add_hex_solid(world, mesh, material, density=1000.0)
X = np.asarray(mesh.vertices)
clamp = np.flatnonzero(np.abs(X[:, 0]) < 1e-6).tolist() # the x = 0 end
trusty.constraints.add_pin(world, beam, clamp)
for _ in range(60): # one second
world.step()
ps.init()
ps.set_up_dir("z_up")
ps.register_volume_mesh("beam", trusty.fem.positions(world, beam),
hexes=trusty.fem.elements(world, beam))
ps.show()
The script builds a hex mesh of the beam and a hyperelastic material for it, adds the beam to a
World stepping at 60 Hz, and pins the vertices at its x = 0 end. Gravity is on by default, so
each world.step() advances the beam one timestep under its own weight, and
trusty.fem.positions and trusty.fem.elements give the viewer the sagged mesh. Every scene follows the same
pattern: make a world, add bodies with a module's add_* call, add constraints, loads and contact
between them, then step.
Run it¶
This example is
examples/quickstart.py
in the trusty-docs repository, along with every other
example on these pages. Clone it:
With the environment from Install active, add the packages the examples
use, then run any of them with python from the repository root:
Run any example with uv run from the repository root, in place of the
python the other pages show. (uv run installs the CPU build; for the GPU
build, use the pip route above.)
The first uv run makes an environment in the repository with
trusty-sim, the polyscope viewer and everything else the examples use;
later runs reuse it.
Each script's docstring lists its options.
Next¶
Modules describes each kind of body and what couples them, Examples shows larger scenes built from several modules, and Guides covers tasks that apply to any scene, such as saving a run and restarting it.