A compiler for physical systems

Write a Lagrangian.
Get a simulation.

MechanicsDSL is a domain-specific language for computational physics. You write a Lagrangian or Hamiltonian in LaTeX-inspired syntax; the symbolic engine derives the equations of motion, and the compiler emits production-ready code in 13 target languages — from Python and C++ to CUDA, Rust, WebAssembly, and Arduino.

pip install mechanicsdsl-core
pendulum.mdsl
% A simple pendulum, written as a Lagrangian
\system{pendulum}
\parameter{m}{1.0}{kg}
\parameter{l}{1.0}{m}
\parameter{g}{9.81}{m/s^2}
\defvar{theta}{angle}{rad}

\lagrangian{
  \frac{1}{2} m l^2 \dot{theta}^2
  - m g l (1 - \cos{theta})
}

\initial{theta=0.5, theta_dot=0}
13Code targets
9Physics domains
44Worked examples
MITLicense

Interactive playground

Edit the DSL on the left, watch the system evolve on the right. Pyodide runs the real SymPy/SciPy pipeline in your browser — toggle Python mode for the authoritative integrator.

simulation.mdsl
Parameters
Visualization
Press Run to start the simulation
Time 0.00 s
Energy 0.00 J
FPS 60
Drift 0.00%
Phase portrait

How it works

Three stages, all symbolic. No hand-derived equations, no template hacks.

  1. 1

    Write the physics

    A LaTeX-inspired DSL: declare parameters, coordinates, and the Lagrangian (or Hamiltonian). What you'd write in a notebook is what the compiler reads.

  2. 2

    Derive symbolically

    SymPy applies the Euler–Lagrange (or Hamilton's) equations, simplifies, and produces a closed-form ODE system. Nothing is approximated yet.

  3. 3

    Compile or simulate

    Run it immediately with SciPy or JAX, or emit a working reference implementation in any of 13 target languages — including CUDA kernels and Arduino sketches.

13 code-generation targets

Pick a language. Get a reference implementation that compiles and runs.

C++ CMake
Python SciPy
CUDA GPU
Rust Cargo
Julia DifferentialEquations.jl
Fortran F90
MATLAB .m
JavaScript browser
OpenMP multicore
WebAssembly portable
Arduino embedded
ARM Pi, NEON
Modelica interop
// Generated C++ for a simple pendulum
#include <cmath>
#include <vector>

const double g = 9.81;
const double l = 1.0;

void compute_derivatives(const double* y, double* dydt, double t) {
    double theta     = y[0];
    double theta_dot = y[1];

    dydt[0] = theta_dot;
    dydt[1] = -g / l * std::sin(theta);
}

void rk4_step(double* y, double t, double dt) {
    // ... classical fourth-order Runge–Kutta
}

Example gallery

Click a system to load it into the playground.

Simple pendulum

Classic single-degree-of-freedom harmonic motion.

Beginner

Double pendulum

Sensitive dependence on initial conditions. Chaotic.

Intermediate

Spring oscillator

Damped harmonic motion with a viscous term.

Beginner

Orbital mechanics

Kepler orbits under inverse-square gravity.

Advanced

Rigid body

Quaternion-based rotational dynamics, singularity-free.

Advanced

Fluid (SPH)

Smoothed-particle hydrodynamics dam-break.

Advanced

What's in the box

Built for both classroom physics and production simulations.

Symbolic engine

Built on SymPy. Derives Euler–Lagrange or Hamilton's equations, simplifies, and produces a closed-form ODE system you can inspect.

JAX backend

Optional GPU/TPU acceleration with JIT compilation and automatic differentiation for inverse problems.

Inverse problems

Parameter estimation, sensitivity analysis, and MCMC uncertainty quantification — fit physical parameters to measured data.

Jupyter integration

%%mechanicsdsl cell magic for interactive notebook workflows; %mdsl_params line magic for live parameter sweeps.

Embedded targets

Run on Raspberry Pi and ARM devices; generate Arduino sketches for hardware-in-the-loop experiments.

Plugin architecture

Add custom physics domains, integrators, or code generators without forking the core.

LSP & VS Code

Language server for completions, diagnostics, and hover docs in any LSP-aware editor; first-party VS Code extension.

Docker & K8s

CPU and CUDA container images; reference Helm charts for cluster deployment.

Note on generated code. The code generators produce working reference implementations, not production-tuned binaries. Treat them as a starting point for performance-critical work.

Get started in one line

Requires Python 3.9+. NumPy, SciPy, SymPy, and Matplotlib install automatically.

Core
pip install mechanicsdsl-core
+ GPU / autodiff
pip install mechanicsdsl-core[jax]
+ Notebooks
pip install mechanicsdsl-core[jupyter]
Everything
pip install mechanicsdsl-core[all]