lmphysics guide
Project Chrono contact formulations, integrator and solver selection, mechanism modeling, pose export, and the standard lmcode_std_v3 library for lmphysics.
Overview
lmphysics embeds Project Chrono, Bullet collision detection, Eigen linear algebra, an expression evaluator for time-dependent loads and motors, and the lmcode-cpp runtime into a single executable. Scripts construct a System world using either non-smooth contact complementarity (nsc) or smooth penalty contact (smc), attach bodies and joints, step forward at a fixed time step, verify conservation or closed-form oracles, and export CSV time series.
Contact formulations, integrators, and solvers
Choosing the right combination of contact mode, time integrator, and solver is essential for physical accuracy and numerical stability:
| Scenario | Recommended configuration | Behavior and trade-offs |
|---|---|---|
| Collisions, sliding friction, and stacks (NSC) | phys.system("nsc"), default integrator("euler"), solver("psor", 100) or solver("apgd", 200) | Handles non-smooth impacts and Coulomb friction cone complementarity; use apgd with higher iterations for tall stacks. |
| Smooth mechanisms and energy conservation | integrator("hht") (presets sparse_lu, 30 Newton iterations, rtol 1e-6, atol 1e-9) | Conserves pendulum and gyroscope mechanical energy closely; cannot be used with active NSC contacts or over-constrained 3D planar loops. |
| Constant acceleration, springs, and redundant planar linkages | integrator("trapezoidal") with default PSOR solver | Exact for constant forces and stable for planar four-bar or slider-crank linkages built from 3D joints. |
| Penalty contact with material stiffness (SMC) | phys.system("smc") with material(name, mu, e) and stiffness(name, young, poisson) | Resolves contact via Hertzian spring-dashpot forces using Young's modulus (default 2e7 Pa) and Poisson ratio (default 0.3). |
Practical contact and mechanism rules
- Collision margins and restitution: Chrono's default NSC collision envelope (0.03 m) and margin (0.01 m) stabilize stacked boxes, but early contact detection reduces isolated rebound height below e^2 h. Call System.collision_margins(0.0, 0.0) for exact restitution impacts, or System.collision_margins(0.002, 0.001) for sharp edge-tipping balance problems (such as the harmonic overhang puzzle).
- Avoiding the first-step friction hop: NSC cone complementarity maintains a sliding gap of mu * |v| * dt above the floor. Starting a moving block at z = 0 causes an initial upward hop; start sliding blocks a few millimeters above the surface or reduce dt.
- Cylinder orientation and rolling: cylinder local axes lie along the y axis for both inertia tensors and collision shapes. Because Bullet's cylinder-box contact rolls 2% to 5% off the analytic acceleration on ramps, use a sphere with custom cylinder or hoop inertia via Body.mass_props(m, ixx, iyy, izz) when testing exact rolling races.
- Joints and time expressions: set Body.pos and Body.rot_axis before creating joints. Rotational and linear motors (motor_speed, motor_angle, motor_torque, motor_position, motor_linear_speed, motor_force) and applied loads (Body.force, Body.torque) accept math expressions in t such as "2.5" or "0.5*sin(3.0*t)".
Multi-host plotting and 3D pose rendering
System.record(name, columns) logs comma-separated body or joint columns at t = 0 and after every recorded step (controlled by System.record_every(n)), and System.save_csv(path) flushes the table to disk. Body columns include mass, x, y, z, qw, qx, qy, qz (Hamilton quaternion, scalar first), vx, vy, vz, wx, wy, wz, lx, ly, lz, speed, and ke. Joint columns include angle, travel, length, force, torque, effort, fx, fy, fz, tx, ty, and tz. Subsequent _lmcode_ blocks in the same LMScript file can plot curves with lmplot, draw free-body diagrams with lmdraw, or pass recorded positions and quaternions to lmcad and lmblender.
Standard lmcode_std_v3 library
Alongside context phys: Phys, lmphysics includes the standard lmcode_std_v3 catalog (cpp/std/lmcode-std.schema.json) of string, encoding, math, collection (IntList, FloatList, StrList, IntMap, FloatMap, StrMap, stacks, queues, priority queues), JSON, template, file, and return-framing functions. The pinned catalog in this repository does not include the REST and CLI interfaces (RestGet, CliCommand, rest_serve and related functions).