A Domain-Specific Language and Transpiler for Classical Mechanics
Project description
MechanicsDSL
MechanicsDSL is a computational physics framework that lets you define physical systems in a natural, LaTeX-inspired syntax and automatically generates high-performance simulations. From pendulums to planetary orbits, from Lagrangian mechanics to fluid dynamics—describe it once, simulate it anywhere.
Why MechanicsDSL?
| Feature | Description |
|---|---|
| Symbolic Engine | Automatically derives equations of motion from Lagrangians or Hamiltonians |
| Fluid Dynamics | Built-in SPH solver for dam breaks, waves, and liquid physics |
| High Performance | Generates optimized C++, OpenMP, and WebAssembly code |
| Rich Visualization | Phase space plots, energy analysis, and smooth animations |
| Research Ready | Validated against analytical solutions and conservation laws |
Installation
pip install mechanicsdsl-core
Requirements: Python 3.8+ with NumPy, SciPy, SymPy, and Matplotlib (installed automatically).
Quick Start
The Famous Figure-8 Three-Body Orbit
Define a gravitational three-body system and watch it trace the celebrated Figure-8 periodic orbit:
from mechanics_dsl import PhysicsCompiler
# Define the system using LaTeX-inspired DSL
figure8_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}
}
"""
# Compile and simulate
compiler = PhysicsCompiler()
compiler.compile_dsl(figure8_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)
Dam Break Fluid Simulation
Simulate fluid dynamics with the integrated SPH solver:
from mechanics_dsl import PhysicsCompiler
fluid_code = r"""
\system{dam_break}
\parameter{h}{0.04}{m}
\parameter{g}{9.81}{m/s^2}
\fluid{water}
\region{rectangle}{x=0.0 .. 0.4, y=0.0 .. 0.8}
\particle_mass{0.02}
\equation_of_state{tait}
\boundary{walls}
\region{line}{x=-0.05, y=0.0 .. 1.5}
\region{line}{x=1.5, y=0.0 .. 1.5}
\region{line}{x=-0.05 .. 1.5, y=-0.05}
"""
compiler = PhysicsCompiler()
compiler.compile_dsl(fluid_code)
compiler.compile_to_cpp("dam_break.cpp", target="standard", compile_binary=True)
Core Capabilities
Classical Mechanics (17 Modules)
- Lagrangian & Hamiltonian formulations with automatic EOM derivation
- Constraints: Holonomic, non-holonomic, rolling, knife-edge (Baumgarte stabilization)
- Dissipation: Rayleigh function, viscous/Coulomb/Stribeck friction
- Stability Analysis: Equilibrium points, linearization, eigenvalue analysis
- Noether's Theorem: Symmetry detection, conservation laws, cyclic coordinates
- Central Forces: Effective potential, Kepler problem, orbital mechanics
- Canonical Transformations: Generating functions, action-angle, Hamilton-Jacobi
- Normal Modes: Mass/stiffness matrices, coupled oscillators, modal decomposition
- Rigid Body: Euler angles, quaternions, gyroscopes, symmetric top
- Perturbation Theory: Lindstedt-Poincaré, averaging, multi-scale analysis
- Collisions: Elastic/inelastic, impulse, center of mass frame
- Scattering: Rutherford, cross-sections, impact parameter
- Variable Mass: Tsiolkovsky rocket equation, conveyor belts
- Continuous Systems: Vibrating strings, membranes, field equations
Code Generation
- Native C++ with optimized numerics
- OpenMP for parallel simulations
- WebAssembly for browser-based physics
Analysis Tools
- Energy conservation monitoring
- Phase space visualization & Poincaré sections
- 534 passing tests for reliability
Examples
The examples/ directory contains 30 progressive tutorials:
| Level | Examples |
|---|---|
| Beginner | Harmonic oscillator, Simple pendulum, Plotting basics |
| Intermediate | Double pendulum, Coupled oscillators, 2D motion, Damping |
| Advanced | 3D gyroscope, Hamiltonian formulation, Phase space, Energy analysis |
| Expert | C++ export, WebAssembly targets, SPH fluid dynamics |
Documentation
Full documentation with tutorials, API reference, and DSL syntax guide:
Contributing
We welcome contributions! See CONTRIBUTING.md for guidelines.
License
MIT License — see LICENSE for details.
Built with ❤️ for physicists, engineers, and curious minds.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mechanicsdsl_core-1.2.2.tar.gz.
File metadata
- Download URL: mechanicsdsl_core-1.2.2.tar.gz
- Upload date:
- Size: 160.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06d5db9481a4eb677402db9ca6ee5656d0dfa312943b261d7cbd0036fcfdb280
|
|
| MD5 |
bdb6bf52ab9b9f36edf0c086db93b9c7
|
|
| BLAKE2b-256 |
2f4de34f50624de6266c13f4e90bde527e831046407fdbab77b3e00299f7c1c0
|
Provenance
The following attestation bundles were made for mechanicsdsl_core-1.2.2.tar.gz:
Publisher:
publish.yml on MechanicsDSL/mechanicsdsl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mechanicsdsl_core-1.2.2.tar.gz -
Subject digest:
06d5db9481a4eb677402db9ca6ee5656d0dfa312943b261d7cbd0036fcfdb280 - Sigstore transparency entry: 747589021
- Sigstore integration time:
-
Permalink:
MechanicsDSL/mechanicsdsl@2852e26f14fc5a3fa9748129641504f647be3cd6 -
Branch / Tag:
refs/tags/v1.2.2 - Owner: https://github.com/MechanicsDSL
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2852e26f14fc5a3fa9748129641504f647be3cd6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file mechanicsdsl_core-1.2.2-py3-none-any.whl.
File metadata
- Download URL: mechanicsdsl_core-1.2.2-py3-none-any.whl
- Upload date:
- Size: 194.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d45db43bc21988409de129c1712d3343dc140f8c786fdb62d1165192e8e4b082
|
|
| MD5 |
e6f0790544ac646bff4826042a00eb7c
|
|
| BLAKE2b-256 |
7bfb1a2d356de3cc2200f3a747ac2a3c80c9f1956d11537e632ac5cb93347708
|
Provenance
The following attestation bundles were made for mechanicsdsl_core-1.2.2-py3-none-any.whl:
Publisher:
publish.yml on MechanicsDSL/mechanicsdsl
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
mechanicsdsl_core-1.2.2-py3-none-any.whl -
Subject digest:
d45db43bc21988409de129c1712d3343dc140f8c786fdb62d1165192e8e4b082 - Sigstore transparency entry: 747589023
- Sigstore integration time:
-
Permalink:
MechanicsDSL/mechanicsdsl@2852e26f14fc5a3fa9748129641504f647be3cd6 -
Branch / Tag:
refs/tags/v1.2.2 - Owner: https://github.com/MechanicsDSL
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2852e26f14fc5a3fa9748129641504f647be3cd6 -
Trigger Event:
release
-
Statement type: