ModelingToolkit → WebAssembly GC · no Julia runtime

A ModelingToolkit model, solved in your browser

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.

ω₀ = √(k/m)–
ζ = c/2√(km)–
ω / ω₀–
Underdamped
Solve–
Steps–
Points–
Loading the module…

Trajectory x(t), m v(t), m/s

Phase portrait t = 0 30 s

Energy E(t), J (observed)

Symbolic at build time

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.

Objects in the module

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!.

This module

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.