agentleFS
90184479018447/julia-pro-p/skills/differential-equations/SKILL.md

differential-equations

Skill0 starsChanged 4 days agoMITView on GitHub

Solve ODE, SDE, DAE, DDE, and jump problems with DifferentialEquations.jl: solver choice, stiffness, callbacks, event handling, ensembles, and error control. Use when setting up or fixing a time integration.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

DifferentialEquations.jl

The order is fixed: name the problem type, then the solver, then the callbacks. Most "the integrator is wrong" reports are a stiff system under a non-stiff solver, a tolerance set tighter than the model's own error, or an event that fires repeatedly because the callback modified the state.

Problem type

The constructor decides which solvers are legal. A DAE written as an ODE silently solves the wrong equations.

EquationsConstructor
du/dt = f(u,p,t)ODEProblem(f, u0, tspan, p)
M du/dt = fODEProblem(ODEFunction(f; mass_matrix=M), u0, tspan, p)
0 = f(du,u,p,t)DAEProblem(f, du0, u0, tspan, p)
du/dt = f + g dWSDEProblem(f, g, u0, tspan, p)
du/dt = f(u, u(t-\tau))DDEProblem(f, h, u0, tspan, p)
discrete state changes at ratesJumpProblem over a DiscreteProblem or GillespieProblem
function lv!(du, u, p, t)
    du[1] = p[1] * u[1] - p[2] * u[1] * u[2]
    du[2] = -p[3] * u[2] + p[4] * u[1] * u[2]
end
prob = ODEProblem(lv!, [1.0, 1.0], (0.0, 10.0), [1.5, 1.0, 3.0, 1.0])

Solver choice

SolverWhen
Tsit5()default for non-stiff; start here
Vern7(), Vern9()non-stiff at tight tolerance
Rodas5P()stiff with tight tolerance; needs a Jacobian
TRBDF2(), KenCarp4()stiff, large systems, cheap Jacobian
AutoTsit5(Rosenbrock23())stiffness unknown or changing along the trajectory
Euler(), RK4()fixed step, reproducible output, or a paper's stated scheme

Stiffness shows as a step count collapsing against tspan and a solve time far above the model's cost. Switching solver is the first move; before that, confirm the model is not the problem — a term that changes on a timescale 10^6 shorter than the phenomenon is stiff physics, and a smaller tolerance will not fix it.

using SparseDiffTools, Symbolics, SparseArrays
jac = Symbolics.jacobian_sparsity(lv!, zeros(2), [1.0, 1.0], [1.0, 1.0, 1.0, 1.0], 0.0)
f = ODEFunction(lv!; jac_prototype = float.(jac))
solve(ODEProblem(f, [1.0, 1.0], (0.0, 10.0), p), Rodas5P())

Callbacks

A ContinuousCallback fires on a zero crossing of a scalar function; DiscreteCallback fires on a condition checked once per step; PeriodicCallback on a time grid. The one bug to avoid is a callback that leaves the state on the wrong side of its own condition, which fires the same event on every subsequent step — the solve slows to a crawl and the output repeats. Use terminate! to stop, and save_positions=(true,false) to record where it fired.

# Poincaré section: record every crossing of u[1] = 0, change nothing
section(u, t, integrator) = u[1]
record!(integrator) = nothing
cb = ContinuousCallback(section, record!, save_positions = (true, false))

# stop at a threshold
stop(u, t, integrator) = u[2] - 1e-6
cb_stop = ContinuousCallback(stop, terminate!)

solve(prob, Tsit5(), callback = CallbackSet(cb, cb_stop), save_everystep = false)

Ensembles

prob_func(prob, i, repeat) = remake(prob; u0 = rand(2))
solve(EnsembleProblem(prob; prob_func), Tsit5(), EnsembleThreads(); trajectories = 100)

Per-trajectory memory is the usual failure: EnsembleThreads allocates a solution only if save_everystep=true; for 10^5 trajectories, save reductions instead (save_everystep=false plus an output_func).

Sensitivity

ForwardDiffSensitivityInterpolatingAdjoint
parametersfewer than ~100more than ~100
time spanshortlong
memoryO(N·p)O(N)
typical usesmall kinetic modelsneural ODEs, UDEs
solve(prob, Tsit5(); sensealg = ForwardDiffSensitivity())
solve(prob, Tsit5(); sensealg = InterpolatingAdjoint(autojacvec = ZygoteVJP()))

Continuation

BifurcationKit.jl may fail to install on Julia 1.12 because MiniQhull does not build. Pin a Julia version where that dependency builds. nonlinear-dynamics carries the continuation and bifurcation workflow; the snippet below is the API shape.

using BifurcationKit
F(u, p) = @. u^3 - p.mu * u
prob = BifurcationProblem(F, [0.0], (mu = 0.0,), (@optic _.mu))
br = continuation(prob, PALC(), ContinuationPar(p_min = -1.0, p_max = 2.0, ds = 0.01))

Traps

  • Tolerance tighter than the model. abstol=1e-14 on a model whose parameters are known to 5% buys time, not accuracy. Set the tolerance from the error you can defend.
  • Fixed step for a stiff problem. Euler() on a stiff system diverges or oscillates; the step size that works is the stability limit, not the accuracy limit.
  • Saving every step by default. A 10^6-step solve held in memory for a plot that needs 500 points; use saveat or SavingCallback.
  • An event condition that is never exactly zero. With floating point, u[1] == 0 may never trigger; give the condition a sign change and let the solver interpolate.
  • Units in the parameters. A rate constant in the wrong unit makes a stiff system look non-stiff; dimensional analysis first, solver second.

Done when

Two solver families agree at ten times tighter tolerance, a conserved quantity holds to a stated tolerance, every callback fires at the expected state (and once), and the wall time is within an order of magnitude of the model's own cost times the step count.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.