modeling-toolkit
Skill0 starsChanged 2 days agoMITView on GitHub
Model with ModelingToolkit.jl: symbolic equations, index reduction, consistent initialisation, Jacobians and sparsity, component composition, and code generation. Use when a model needs symbolic structure rather than a hand-written right-hand side.
In this repository55 other files it gives its agents.
- advanced-optimizationSkill
- bayesian-inferenceSkill
- bayesian-pinnSkill
- bayesian-ude-workflowSkill
- catalyst-reactionsSkill
- continuum-mechanics-and-rheologySkill
- control-theorySkill
- creating-julia-appSkill
- debugging-toolkitSkill
- differential-equationsSkill
- differentiationSkill
- equation-discoverySkill
- experiment-trackingSkill
- fem-feaSkill
- julia-graph-neural-networksSkill
- julia-languageSkill
- julia-ml-and-dlSkill
- julia-model-deploymentSkill
- julia-paper-implementationSkill
- julia-pro-helpSkill
- julia-pro-modeSkill
- julia-reinforcement-learningSkill
- neural-pdeSkill
- nonlinear-dynamicsSkill
- numerical-methods-implementationSkill
- package-development-workflowSkill
- performance-tuningSkill
- physical-learning-systemsSkill
- playbook-debugSkill
- playbook-model-validateSkill
- playbook-package-releaseSkill
- playbook-paper-implementSkill
- playbook-perfSkill
- playbook-test-specSkill
- principle-both-sidesSkill
- principle-breakingSkill
- principle-file-is-truthSkill
- principle-gateSkill
- principle-limitsSkill
- principle-loud-failureSkill
- principle-one-stepSkill
- principle-proof-listSkill
- principle-rebuttalSkill
- principle-runtime-surfaceSkill
- principle-three-boundsSkill
- principle-three-clocksSkill
- scientific-visualizationSkill
- sciml-and-diffeqSkill
- setup-julia-proSkill
- signal-processingSkill
- simulation-and-hpcSkill
- statistical-physicsSkill
- symbolic-mathSkill
- web-development-juliaSkill
- zarr-cloud-arraysSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 2 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 61
It can eliminate a variable you wanted to report: If T drops out of the unknowns, reconstruct it from the constraint afterwards instead of …
- Distinctive: line 62
Differentiating a constraint amplifies noise: when the constraint came from data.
- Distinctive: line 83
Run each component alone first: an isolated component that solves gives a reference for the composed one, and the difference between them …
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
ModelingToolkit.jl
When it earns its keep
| Situation | MTK | hand-written RHS |
| a handful of ODEs, one file, no reuse | no | yes, and the loop stays readable |
| algebraic constraints, conservation laws, index > 0 | yes | hand-coding a DAE is how index bugs get written |
| many components wired together (flowsheet, circuit, multi-domain) | yes — connectors and reuse | copy-paste becomes the model |
| symbolic Jacobians, sparsity, sensitivities, parameter sweeps | yes — generated once, reused | finite differences only |
| code generation to C or a standalone function | yes — build_function | not available |
The cost is compile latency and an abstraction you must debug through. For a three-equation model that cost is real; for a flowsheet it is the difference between a model and a script.
The pattern
using ModelingToolkit, DifferentialEquations
@variables t x(t) y(t)
@parameters α β
D = Differential(t)
eqs = [D(x) ~ α * x - β * x * y,
D(y) ~ β * x * y - 0.1 * y]
@mtkbuild sys = ODESystem(eqs, t) # simplifies structurally, then completes
prob = ODEProblem(sys, [x => 1.0, y => 1.0], (0.0, 10.0), [α => 0.5, β => 0.3])
sol = solve(prob, Tsit5())@mtkbuild is structural_simplify plus complete in one step; use it unless you need to inspect the simplification in between. Every state must be a function of t (x(t), not x) — a plain @variables x becomes an unknown of the wrong kind and the system silently solves a different model.
Units and initialisation
These two produce the failures that look like solver bugs.
- Units. Declare them with Unitful in the variable metadata, or keep every equation in SI by hand and say so in the model. A mixed-unit equation integrates without complaint and gives a wrong rate, and unit checking is the only automated defence.
- Consistent initial conditions. A DAE needs a
u0that satisfies the algebraic equations. Either supply one (compute the algebraic variables from the differential ones) or pass an initialization algorithm such asBrownFullBasicInit(); then checksol.retcode == ReturnCode.Successrather than trusting that a solution came back. AnInitialFailureor a step-size collapse on a system that should work is almost always an inconsistentu0.
DAE and index reduction
@variables t x(t) y(t) T(t)
@parameters k γ
D = Differential(t)
eqs = [D(x) ~ -k * x * y,
D(y) ~ k * x * y - γ * y,
0 ~ x + y + T - 1.0] # algebraic constraint (conservation)
@mtkbuild sys = ODESystem(eqs, t)structural_simplify eliminates algebraic variables by differentiating constraints and substituting back — exactly the index reduction you would do by hand. Three consequences:
- It can eliminate a variable you wanted to report. If
Tdrops out of the unknowns, reconstruct it from the constraint afterwards instead of disabling simplification. - Differentiating a constraint amplifies noise when the constraint came from data; keep the original constraint among the checks even after reduction.
- The simplification is structural, not physical. It cannot distinguish a conservation law from a badly scaled equation, so the reduced system still needs the balance check.
Components
function RC(; name, R = 100.0, C = 1e-6)
@variables t v(t) i(t)
@parameters R = R C = C
D = Differential(t)
eqs = [v ~ R * i, D(v) ~ i / C]
ODESystem(eqs, t; name)
end
@named stage1 = RC(R = 100.0)
@named stage2 = RC(R = 200.0)
connections = [stage1.v ~ stage2.v, stage1.i ~ -stage2.i] # series: shared current, voltages add
@named filter = ODESystem(connections, t; systems = [stage1, stage2])
@mtkbuild filter_sys = filterRun each component alone first: an isolated component that solves gives a reference for the composed one, and the difference between them is the connection logic, which is where composition bugs live.
Performance
- Sparse Jacobians past ten equations:
ODEProblem(sys, u0, tspan, p; jac = true, sparse = true), then a stiff solver such asTRBDF2orRodas5Puses the pattern. - Build the problem once, then
remake: parameter sweeps and sensitivity runs reuse the generated functions; rebuilding inside the loop re-pays compilation. build_functionfor a standalone right-hand side, generated C, or a loss function inside an optimiser.- No symbolic operations inside a time loop.
substitute,simplify, andstructural_simplifyare compile-time tools; the runtime object is the generated function.
Traps
xinstead ofx(t). The system solves, the unknowns are wrong, and the answer belongs to a different model.- No
structural_simplify. The solver then integrates the algebraic equations as if they were differential and drifts off the constraint manifold. - Ignoring the retcode. A partially converged DAE solution plots like a solution.
- Disabling simplification to keep a variable. Reconstruct it instead; the reduced system is the one with usable conditioning.
- Rebuilding the problem per parameter. Minutes of compilation for seconds of integration.
Done when
States are functions of the independent variable and parameters are separate from them; @mtkbuild (or an explicit structural_simplify) ran before the problem was built; units are declared or the SI convention is stated; every component ran alone before composition and the composed system reproduced the known limit; consistent initial conditions or an initialization algorithm were supplied and sol.retcode checked; and sparsity is enabled once the system passes ten equations.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

