// physics_native.cpp -- Native Physics subject library. // // Synthesizes the __NativePhysics class: physical constants (C, H, HBAR, // E_CHARGE, K_B, G, G0, N_A, R) and executable formula families for mechanics, // gravitation, relativity, electricity, gases, and waves. Physics depends on // Math (e.g. sqrt, tau) and requires `import math`. #include "native_add.h" #include "native_builders.h" namespace sleela { namespace native { using namespace sleela::native::builders; void addPhysics(Program&p){ClassDecl c;c.name="__NativePhysics";auto k=[&](const char*n,double v){auto m=M0("double",n);m.body->stmts.push_back(Ret(D(v)));c.methods.push_back(std::move(m));};k("__native_physics_C",299792458.0);k("__native_physics_H",6.62607015e-34);k("__native_physics_HBAR",1.054571817e-34);k("__native_physics_E_CHARGE",1.602176634e-19);k("__native_physics_K_B",1.380649e-23);k("__native_physics_G",6.67430e-11);k("__native_physics_G0",9.80665);k("__native_physics_N_A",6.02214076e23);k("__native_physics_R",8.31446261815324); auto m=M2("double","__native_physics_velocity","double","dx","double","dt");m.body->stmts.push_back(Ret(Bn("/",V("dx"),V("dt"))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_acceleration","double","dv","double","dt");m.body->stmts.push_back(Ret(Bn("/",V("dv"),V("dt"))));c.methods.push_back(std::move(m));m=M4("double","__native_physics_kinematic_position","double","x0","double","v0","double","a","double","t");m.body->stmts.push_back(Ret(Bn("+",Bn("+",V("x0"),Bn("*",V("v0"),V("t"))),Bn("*",D(.5),Bn("*",V("a"),Bn("*",V("t"),V("t")))))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_force","double","mass","double","accel");m.body->stmts.push_back(Ret(Bn("*",V("mass"),V("accel"))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_kinetic_energy","double","mass","double","velocity");m.body->stmts.push_back(Ret(Bn("*",D(.5),Bn("*",V("mass"),Bn("*",V("velocity"),V("velocity"))))));c.methods.push_back(std::move(m));m=M3("double","__native_physics_gravitational_force","double","m1","double","m2","double","r");m.body->stmts.push_back(Ret(Bn("/",Bn("*",C0("__native_physics_G"),Bn("*",V("m1"),V("m2"))),Bn("*",V("r"),V("r")))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_escape_velocity","double","mass","double","radius");m.body->stmts.push_back(Ret(C1("__native_math_sqrt",Bn("/",Bn("*",D(2),Bn("*",C0("__native_physics_G"),V("mass"))),V("radius")))));c.methods.push_back(std::move(m));m=M1("double","__native_physics_lorentz_gamma","double","v");m.body->stmts.push_back(Ret(Bn("/",D(1),C1("__native_math_sqrt",Bn("-",D(1),Bn("/",Bn("*",V("v"),V("v")),Bn("*",C0("__native_physics_C"),C0("__native_physics_C"))))))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_relativistic_energy","double","mass","double","v");m.body->stmts.push_back(Ret(Bn("*",C1("__native_physics_lorentz_gamma",V("v")),Bn("*",V("mass"),Bn("*",C0("__native_physics_C"),C0("__native_physics_C"))))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_momentum","double","mass","double","v");m.body->stmts.push_back(Ret(Bn("*",C1("__native_physics_lorentz_gamma",V("v")),Bn("*",V("mass"),V("v")))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_ohms_voltage","double","current","double","resistance");m.body->stmts.push_back(Ret(Bn("*",V("current"),V("resistance"))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_electric_power","double","voltage","double","current");m.body->stmts.push_back(Ret(Bn("*",V("voltage"),V("current"))));c.methods.push_back(std::move(m));m=M3("double","__native_physics_ideal_gas_pressure","double","n","double","R","double","T");m.body->stmts.push_back(Ret(Bn("/",Bn("*",V("n"),Bn("*",V("R"),V("T"))),D(1))));c.methods.push_back(std::move(m));m=M2("double","__native_physics_wave_frequency","double","velocity","double","wavelength");m.body->stmts.push_back(Ret(Bn("/",V("velocity"),V("wavelength"))));c.methods.push_back(std::move(m));m=M1("double","__native_physics_angular_frequency","double","frequency");m.body->stmts.push_back(Ret(Bn("*",C0("__native_math_tau"),V("frequency"))));c.methods.push_back(std::move(m));m=M1("double","__native_physics_wavenumber","double","wavelength");m.body->stmts.push_back(Ret(Bn("/",C0("__native_math_tau"),V("wavelength"))));c.methods.push_back(std::move(m));// Two-flavor vacuum oscillation: // P = sin^2(2 theta) * sin^2(1.267 * dm2 * L / E) // with dm2 in eV^2, L in km, E in GeV (the 1.267 constant carries those units). // Both factors are SQUARED (a previous version used a single sine on each and // divided by 1 instead of E, which was not the physical formula). m=M4("double","__native_physics_neutrino_oscillation_probability","double","theta","double","dm2","double","L","double","E"); m.body->stmts.push_back(Decl("double","s1",C1("__native_math_sin",Bn("*",D(2),V("theta"))))); m.body->stmts.push_back(Decl("double","s2",C1("__native_math_sin",Bn("/",Bn("*",D(1.267),Bn("*",V("dm2"),V("L"))),V("E"))))); m.body->stmts.push_back(Ret(Bn("*",Bn("*",V("s1"),V("s1")),Bn("*",V("s2"),V("s2"))))); c.methods.push_back(std::move(m)); // The synthesized physics helpers are pure functions with no instance state; // mark them static so a qualified call (physics.velocity(...)) matches the // method's parameter count exactly. Without this they are instance methods // that expect a `this` receiver the call site never pushes, which misaligns // the VM call frame and corrupts the operand stack on the 2nd+ call. (Math // already does this at the end of addMath.) for(auto& method:c.methods)method.isStatic=true; p.classes.push_back(std::move(c));} }} // namespace sleela::native