A mass on a spring with damping and a periodic driving force. ModelingToolkit simplified the model and generated its code ahead of time; every change here re-solves it with OrdinaryDiffEq's Tsit5 over t ∈ [0, 30] s in WebAssembly, and reads the energy back through the model's own observed function.
@variables x(t) v(t) Fs(t) a(t) E(t) D(x) ~ v Fs ~ -k*x - c*v + F*sin(ω*t) a ~ Fs / m D(v) ~ a E ~ m*v^2/2 + k*x^2/2
mtkcompile keeps two states, x and v; Fs, a and E become observed variables.
ModelingToolkit runs when the module is built: mtkcompile eliminates the
algebraic equations, and with eval_expression = true it generates the
right-hand side and the observed functions as ordinary Julia code, which WasmCodegen compiles
with trim = :safe.
The integrator is built with init when the module is compiled and serialized into
it: 206 Julia objects, its MTKParameters included. The page calls MTK's
parameter setter (setp), then reinit! and solve!.
105 KB of WebAssembly, 89 functions, no imports. Native Julia on the build machine solves the default case in about 0.33 ms; the timing on this page is your browser's.