Code Generation Overview
MechanicsDSL can generate high-performance code in multiple target languages, enabling deployment beyond Python for production applications.
Supported Targets
MechanicsDSL supports 15 code generation targets across native, embedded, GPU, browser, and game engine platforms.
Target |
Use Case |
Performance |
|---|---|---|
C++ |
Native applications, embedded systems |
~10-100x faster than Python |
Python |
NumPy-accelerated standalone scripts |
Portable, easy to integrate |
Rust |
Memory-safe native applications, |
~10-100x faster than Python |
Julia |
Scientific computing with DifferentialEquations.jl |
Near-C performance with high-level syntax |
Fortran |
Legacy HPC and numerical computing |
Excellent vectorization |
MATLAB |
MATLAB/Octave engineering workflows |
Familiar to engineers |
JavaScript |
Browser and Node.js applications |
Web-native |
CUDA |
GPU acceleration, batch parameter sweeps |
Massive parallelism |
OpenMP |
Multi-core parallelization |
Scales with CPU cores |
WebAssembly |
Browser-based simulations |
Near-native in browsers |
Arduino |
Microcontroller and IoT applications |
Minimal footprint |
ARM |
Raspberry Pi, Jetson Nano, embedded Linux |
NEON-optimized |
Unity |
Game engine integration (C#) |
Real-time physics in games |
Unreal |
Game engine integration (C++) |
AAA game engine physics |
Modelica |
Standards-based multi-domain simulation |
Interoperable with Simulink, OpenModelica |
Basic Usage
Generate C++ code from a compiled system:
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 \cos{theta}}
\initial{theta=0.5, theta_dot=0}
''')
# Generate C++ source
compiler.compile_to_cpp("pendulum.cpp", target="standard")
# Or compile to binary directly
compiler.compile_to_cpp("pendulum.cpp", target="standard", compile_binary=True)
Generated Code Structure
The generated C++ code includes:
Header with dependencies: Eigen, standard library
State vector definition: Position and velocity arrays
Derivatives function: Right-hand side of ODEs
Integrator: RK4 or Velocity Verlet
Main function: I/O and time stepping
Example output structure:
#include <Eigen/Dense>
#include <fstream>
#include <cmath>
using State = Eigen::VectorXd;
// Parameters (inlined as constexpr)
constexpr double m = 1.0;
constexpr double l = 1.0;
constexpr double g = 9.81;
// Derivatives: returns d(state)/dt
State derivatives(double t, const State& y) {
State dydt(2);
double theta = y[0];
double theta_dot = y[1];
// Derived equations of motion
dydt[0] = theta_dot;
dydt[1] = -(g/l) * sin(theta);
return dydt;
}
// RK4 integrator
State rk4_step(double t, const State& y, double dt) { ... }
int main() { ... }
Compilation Requirements
C++ Standard: Requires C++17 or later.
Dependencies:
Eigen (header-only linear algebra)
Standard library only (no external dependencies)
Compiler options:
# GCC/Clang
g++ -O3 -march=native -std=c++17 pendulum.cpp -o pendulum
# MSVC
cl /O2 /std:c++17 pendulum.cpp
Performance Tips
Use -O3 optimization: Essential for inlining and vectorization
Enable -march=native: Uses CPU-specific instructions
Link-time optimization: Add
-fltofor cross-file inliningProfile first: Use profilers before micro-optimizing
See Also
C++ Code Generation - Detailed C++ code generation
Python Code Generation - NumPy-accelerated Python
CUDA Code Generation - CUDA GPU acceleration
WebAssembly Code Generation - WebAssembly for browsers