|
Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
|
A dimensionally safe C++ header-only mechanics and electromagnetics simulation library.
Phyth is a C++ simulation library designed for numerical method researchers. While maintaining lightweight and safety, it breaks down the simulation process into replaceable independent components, allowing you to change integrators or constraint solvers like building blocks, without being troubled by the complex architecture of large engines.
1_kg, 1_m make code concise and readable, while the type system catches dimension mismatches at compile time—assigning meters to seconds, or using force as mass, fails before runtime, eliminating unit-confusion bugs.J from N*m—no redundant re-declaration, no ambiguity.DistanceConstraint) and force models (e.g., GravityFormula) are designed as pluggable, independent components—swap integrators or implement custom algorithms without restructuring the entire framework.GetTotalEnergy()) are available out of the box, making it easy to verify physical conservation laws during simulation and simplifying debugging and experiment validation.Existing physics engines sit at two extremes:
| Industrial Engines | Scientific Engines | |
|---|---|---|
| Speed | ✅ Fast | ⚠️ Slow |
| Robustness | ✅ Battle-tested | ⚠️ Academic |
| Transparency | ❌ Black box (stages fused) | ✅ Exposed internals |
| Modifiability | ❌ Fixed solver, hard to swap | ⚠️ Heavy codebase, hard to modify |
| Dependencies | ✅ Minimal | ❌ Heavy dependency graph |
Phyth sits right in the middle:
Transparent like a scientific engine, lightweight like an industrial one.
It gives researchers the freedom to develop new numerical methods, without the friction of production-level complexity or academic-level baggage.
In short, Phyth gives researchers the freedom to develop new numerical methods, without the friction of production-level complexity or academic-level baggage.
The first image is the analysis (based on the Quick Start code with detailed output):

The latest image below shows the data analysis of the double pendulum simulation (dt=2e-4 s and sample 1000 points), and the calculated relative fluctuation is about 1.37e-3%(1.37e-5):

Phyth is a header-only library with no third-party dependencies and no platform-specific system calls. It relies exclusively on the C++ standard library, making it portable across platforms.
| Platform | Compiler | Status |
|---|---|---|
| Linux | GCC 9+, Clang 10+ | ✅ CI-tested |
| Windows | MSVC 19.20+, MinGW | ✅ Verified |
| macOS | Apple Clang 13+ | ⚠️ Expected to work |
Note: If you use Phyth on an untested platform and encounter issues, please file an issue — we'll help investigate.
Add the include/ directory to your compiler's header search path:
Done. No compilation required.
The following code demonstrates single pendulum motion under gravity. The output results consist of the particle and total mechanical energy for each time step, which are used to verify numerical stability.
Expected output example
Note: The above output is formatted for readability. Actual raw output may show slight variations in the last 3–4 significant digits due to floating-point rounding.
Verification method If the above code is run, the total energy at each moment should be approximately 9.80665 J.
In fact, after running and analysis the data, it can be concluded that the relative fluctuation within 2.73s is approximately 1.32e-5, which confirms that the integrator and constraint solver are working properly.
For specific analysis of the above program, see Accuracy Verification.
Config::dt (time step)Integrate() updates every elementCorrect() applies Velocity Correct to particles.Below is the project tree diagram and introduction:
example/ — Examples, like SinglePendulum.cppPhyth/include/Core/ — Compile-time dimensional analysis systemElements/ — Physics building blocks (mechanics + electromagnetics)Physical/ — Constants like g, G, epsilon_0Tools/ — Tools such as Vector3 and helper functionsQ: Is dimensionality checking performed during compilation or at runtime? Is it zero-cost?
A: Compile-time. In Release mode, Quantity<T, Dim> has a memory layout identical to that of a native double, with zero runtime overhead.
Q: Can units in a parallel relationship be transformed into each other?
A: Yes. For example, for N*m and J, the dimensional system will automatically deduce the units, while the unit labels will retain the original semantics. The display format is managed by UnitsRegistry to ensure that the physical meaning is not lost during output.
Q: Is electromagnetic field an analytical solution or a numerical solution?
Numerical integration. The electric field and potential are approximately calculated by dividing a rectangular prism into volume elements and superimposing Coulomb fields on each volume element. The accuracy is comparable to the analytical formula, but the speed is not very fast. This is the implementation decision of the current version - prioritizing code clarity over computational efficiency.
Q: What physics can be considered at present?
We welcome contributions from researchers, students, and developers!
See CONTRIBUTING.md for full details.
Phyth's dimensional analysis system draws inspiration from:
The validation framework was inspired by best practices in computational physics education.