Skip to main content

MechanicsDSL Logo

MechanicsDSL

Python CI Python 3.9+ License: MIT DOI Documentation Status

Write a Lagrangian. Get a simulation.


MechanicsDSL is a domain-specific language and compiler for physical systems. You write a Lagrangian or Hamiltonian in a LaTeX-inspired syntax; the symbolic engine (built on SymPy) derives the equations of motion automatically, and the compiler generates simulation code in your choice of twelve target languages — from Python and C++ to CUDA, Rust, WebAssembly, and Arduino.

The goal is to collapse the distance between textbook physics and a running simulation, while keeping the path to lower-level, performance-tuned code open through code generation.

from mechanics_dsl import PhysicsCompiler

compiler = PhysicsCompiler()
compiler.compile_dsl(r"""
\system{pendulum}
\defvar{theta}{Angle}{rad}
\parameter{m}{1.0}{kg}
\parameter{l}{1.0}{m}
\parameter{g}{9.81}{m/s^2}

\lagrangian{\frac{1}{2} * m * l^2 * \dot{theta}^2 - m * g * l * (1 - \cos{theta})}
\initial{theta=0.5, theta_dot=0.0}
""")

solution = compiler.simulate(t_span=(0, 10), num_points=1000)
compiler.plot(solution)

What's in the box

Component Description
Symbolic engine Derives equations of motion from Lagrangians or Hamiltonians, built on SymPy
Code generation Twelve targets: C++, Python, Rust, Julia, CUDA, Fortran, MATLAB, JavaScript, OpenMP, WebAssembly, Arduino, ARM
JAX backend GPU acceleration with JIT compilation and automatic differentiation
Inverse problems Parameter estimation, sensitivity analysis, MCMC uncertainty quantification
Jupyter integration %%mechanicsdsl magic commands for interactive notebooks
Plugin architecture Custom physics domains and solvers without modifying the core

Note on generated code. The code generators produce working reference implementations, not production-tuned binaries. For high-performance or mission-critical work, treat the generated code as a starting point rather than a finished product.

Installation

pip install mechanicsdsl-core

Optional extras:

pip install mechanicsdsl-core[jax]      # GPU + autodiff
pip install mechanicsdsl-core[jupyter]  # Notebook magic
pip install mechanicsdsl-core[all]      # Everything

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

Example: Figure-8 three-body orbit

from mechanics_dsl import PhysicsCompiler

code = r"""
\system{figure8_orbit}
\defvar{x1}{Position}{m} \defvar{y1}{Position}{m}
\defvar{x2}{Position}{m} \defvar{y2}{Position}{m}
\defvar{x3}{Position}{m} \defvar{y3}{Position}{m}
\defvar{m}{Mass}{kg} \defvar{G}{Grav}{1}

\parameter{m}{1.0}{kg} \parameter{G}{1.0}{1}

\lagrangian{
    0.5 * m * (\dot{x1}^2 + \dot{y1}^2 + \dot{x2}^2 + \dot{y2}^2 + \dot{x3}^2 + \dot{y3}^2)
    + G*m^2/\sqrt{(x1-x2)^2 + (y1-y2)^2}
    + G*m^2/\sqrt{(x2-x3)^2 + (y2-y3)^2}
    + G*m^2/\sqrt{(x1-x3)^2 + (y1-y3)^2}
}
"""

compiler = PhysicsCompiler()
compiler.compile_dsl(code)
compiler.simulator.set_initial_conditions({
    'x1': 0.97000436,  'y1': -0.24308753, 'x1_dot': 0.466203685, 'y1_dot': 0.43236573,
    'x2': -0.97000436, 'y2': 0.24308753,  'x2_dot': 0.466203685, 'y2_dot': 0.43236573,
    'x3': 0.0,         'y3': 0.0,         'x3_dot': -0.93240737, 'y3_dot': -0.86473146
})
solution = compiler.simulate(t_span=(0, 6.326), num_points=2000)

The examples/ directory contains 40+ progressive examples, from harmonic oscillators to SPH fluid dynamics.

Code generation

Any compiled system can be exported as standalone code in any of the supported targets:

Target Output
C++ CMake project with solver
Python NumPy/SciPy standalone script
Rust Cargo project, no_std option
Julia DifferentialEquations.jl
CUDA GPU-parallel solver
Fortran F90 with LAPACK
MATLAB .m script with ode45
JavaScript Browser or Node.js
OpenMP Multi-threaded C++
WebAssembly Emscripten WASM
Arduino .ino embedded sketch
ARM Raspberry Pi / NEON, bare-metal Cortex-M

Twelve targets. Modelica is not among them: it is an integration (mechanics_dsl.integrations.modelica) that emits a .mo model for an external Modelica tool to compile, not a code generator registered with PhysicsCompiler.export().

from mechanics_dsl.codegen.rust import RustGenerator

gen = RustGenerator(
    system_name="pendulum",
    coordinates=compiler.get_coordinates(),
    parameters=compiler.simulator.parameters,
    initial_conditions=compiler.initial_conditions,
    equations=compiler.equations,
)
gen.generate("pendulum.rs")

Physics coverage

  • Classical mechanics — Lagrangian and Hamiltonian formulations; holonomic, non-holonomic, and rolling constraints; Rayleigh dissipation; stability analysis; Noether's theorem; central forces; canonical transformations; normal modes; rigid body dynamics; perturbation theory; collisions; scattering; variable-mass systems; continuous media.
  • Quantum mechanics — Bound states, scattering, tunneling, WKB approximation, hydrogen atom, Ehrenfest theorem.
  • Electromagnetism — Lorentz force, cyclotron motion, plane waves, antennas, waveguides, Penning traps.
  • Relativity — Special: Lorentz boosts, four-vectors, Doppler effect. General: Schwarzschild and Kerr metrics, geodesics, gravitational lensing, FLRW cosmology.
  • Statistical mechanics and thermodynamics — Microcanonical, canonical, and grand canonical ensembles; Boltzmann, Fermi-Dirac, and Bose-Einstein distributions; Ising model; heat engines; phase transitions.
  • Fluid dynamics — SPH solver with Poly6, Spiky, and viscosity kernels; Tait equation of state; boundary conditions.

Project status

MechanicsDSL is under active development. The v2.0.x line is stable; new features, additional backends, and broader validation are ongoing. Issues, pull requests, and use-case reports are all welcome — what the project becomes next depends in part on how people are using it.

Documentation

Full documentation, tutorials, and DSL reference at mechanicsdsl.readthedocs.io.

Contributing

Contributions are welcome. See CONTRIBUTING.md for guidelines.

License

MIT — see LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mechanicsdsl_core-2.1.3.tar.gz (394.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mechanicsdsl_core-2.1.3-py3-none-any.whl (469.4 kB view details)

Uploaded Python 3

File details

Details for the file mechanicsdsl_core-2.1.3.tar.gz.

File metadata

  • Download URL: mechanicsdsl_core-2.1.3.tar.gz
  • Upload date:
  • Size: 394.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for mechanicsdsl_core-2.1.3.tar.gz
Algorithm Hash digest
SHA256 17b7fbe7c037342cfc92698a9d4b2828aad45f1f314912984268e2a9677cc1ae
MD5 7c9119c3a6d44777f31fb6b6db2ab37a
BLAKE2b-256 8b0d976a7f9dad6aef05eb4aade0d29d78f2bf12de06b252817a40dde81f4de7

See more details on using hashes here.

Provenance

The following attestation bundles were made for mechanicsdsl_core-2.1.3.tar.gz:

Publisher: publish.yml on MechanicsDSL/mechanicsdsl

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mechanicsdsl_core-2.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for mechanicsdsl_core-2.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e437f5ff4532c3568f06a7712bd90f5b6fba92c18b93e0464841e59785092357
MD5 1839c391ff8159757280e573d353e6c2
BLAKE2b-256 184ede4cd0f93421d659f4414d76531bf069fa2fe4d7ea605ce9befa8b8b7a25

See more details on using hashes here.

Provenance

The following attestation bundles were made for mechanicsdsl_core-2.1.3-py3-none-any.whl:

Publisher: publish.yml on MechanicsDSL/mechanicsdsl

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

2.1.3 This release

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.6

2 files

2.0.5

2 files

2.0.1

2 files

2.0.0

2 files

1.6.1

2 files

1.6.0

2 files

1.5.1

2 files

1.5.0

2 files

1.4.0

2 files

1.3.1

2 files

1.3.0

2 files

1.2.2

2 files

1.2.1

2 files

1.1.0

2 files

1.0.0

2 files

0.6.2

2 files

0.6.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page