agentleFS
90184479018447/julia-pro-p/skills/numerical-methods-implementation/SKILL.md

numerical-methods-implementation

Skill0 starsChanged 2 days agoMITView on GitHub

Write the discretisation yourself: stencil and scheme choice, CFL and stability analysis, direct versus iterative solves with preconditioning, and convergence order measured by manufactured solution. Use when the solver is yours to write.

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
  1. Distinctive:

    Check stability: before trusting any long run — CFL for explicit hyperbolic, von Neumann for linear constant-coefficient, energy method for …

    line 22
  2. Very specific:

    Refining only the time step: If dt is not tied to h (or the scheme is IMEX), the spatial order never shows.

    line 78
  3. Distinctive:

    A preconditioner that changes the answer: Check the iterative solve against a direct one on a small instance.

    line 80

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Writing the discretisation yourself

This page is for the case where no library owns the stepper: a custom scheme, a bespoke boundary condition, a solver inside a performance-critical loop. If a library can solve it, use differential-equations and spend the effort on the model instead.

Depth

TopicWhere
ODE/DAE/SDE solver choice and optionsdifferential-equations
PDE in symbolic form, method of linesmodeling-toolkit
FEM specificallyfem-fea
making the compiled loop fastperformance-tuning

The order of work

  1. Discretise the continuous equation, including boundaries, before writing a time loop. Done when every term of the PDE has a discrete counterpart and each boundary condition is imposed by a named method (ghost cell, algebraic elimination, weak form).
  2. Check consistency with a manufactured solution: pick a smooth u, substitute into the PDE to get a source term, solve, and compare. Done when the error falls at the designed rate as h and dt shrink.
  3. Check stability before trusting any long run — CFL for explicit hyperbolic, von Neumann for linear constant-coefficient, energy method for the rest. Done when the stable step size is derived and the code refuses to exceed it.
  4. Measure the order, do not assume it. Two grids minimum, three preferred. Done when the observed order matches the design order within 0.1–0.2 and the deviation is explained.
  5. Check what must be conserved, at every output. Done when mass/energy drift is a measured rate, not an eyeball.

Discretisation choice

SchemeBuysCosts
finite differencesimplest on tensor grids, easy high ordergeometry is a rectangle; conservation needs a conservative form
finite volumelocal conservation by constructionreconstruction and limiters are where the bugs live
spectral / pseudospectralexponential convergence for smooth solutionsGibbs ringing at shocks; global transform cost
FEMarbitrary geometry, weak handling of natural BCsassembly and quadrature code, mass-matrix solves
method of linesreuse an adaptive ODE solverthe solver sees a stiff system and will pick implicit steps

For transport, write the flux form (∂_t u + ∂_x f(u) = 0) so the discrete sum telescopes to boundary fluxes; the non-conservative form loses conservation at the level of rounding and the loss is invisible until long times. For advection–diffusion, a central difference on the advective term is unstable above cell Péclet number 2 — upwind, or a flux limiter.

Time stepping

RegimeChoiceWhy
explicit, hyperbolicSSP-RK2/RK3 with CFL ≤ 1 (dimension-dependent)stability region covers the imaginary axis
stiff from diffusion or chemistryBDF or an implicit RK (Radau)stability region covers the negative real axis far out
both at onceIMEX: implicit diffusion, explicit advectionone linear solve per step instead of a nonlinear one
multiple physical processesStrang splitting, second ordereach sub-solver keeps its own stable step; splitting error is second order only if symmetric

Explicit stability bounds are dimension-dependent: the diffusion limit is dt ≤ h²/(2d·D) for d dimensions, and forgetting the d is the classic factor-of-two-to-three surprise.

Linear solves inside the step

MatrixMethodNote
dense, one-offLU with partial pivotingcond estimate from the factorisation is nearly free
SPDCholeskyhalf the work, and failure is a gift: it proves non-SPD
sparse, one-offsparse LU / UMFPACKfill-in dominates; reorder before blaming the solver
sparse, many right-hand sidesfactorise once, solve manyrebuilding the factorisation per step is the usual 100×
large SPD, iterativeCG with a preconditionerwithout one, CG on a Poisson matrix takes O(1/h) iterations
nonsymmetric, iterativeGMRES with restartwatch the restart length; a stalled residual is often the preconditioner

An implicit step is a linear solve plus a Newton loop when the operator is nonlinear, so the cost is (Newton iterations) × (solve). Report both, and check the Newton residual against the step tolerance: a Newton loop that stops early makes the scheme first order regardless of its design.

Measuring convergence

# manufactured solution: u(x,t) = sin(pi*x)*exp(-t), source added to the RHS
errs = Float64[]
for n in (40, 80, 160, 320)
    h = 1 / n
    push!(errs, maximum(abs.(solve_mms(n) .- exact.(grid(n), t_end))))
end
orders = log2.(errs[1:end-1] ./ errs[2:end])   # expect ~2 for a second-order scheme

log2(err_i/err_{i+1}) is the observed order. A value below the design order means a boundary treatment, a limiter, or a non-smooth coefficient is eating it; a value above means the grids are in the asymptotic range not yet, or the error is dominated by the time step rather than h.

Traps

  • Refining only the time step. If dt is not tied to h (or the scheme is IMEX), the spatial order never shows.
  • Measuring against an exact solution on a coarse grid. The asymptotic regime starts when the error is smooth; two grids is the minimum, three is the honest number.
  • A preconditioner that changes the answer. Check the iterative solve against a direct one on a small instance; a converged-to-the-wrong-operator solve looks identical to a good one.
  • Stability tested only on the linearised problem. Nonlinear instability (aliasing, negative concentrations) appears where the linear analysis says nothing — add a positivity or boundedness check on the output.

Done when

The condition number of every linear solve is known (cond(A), and a preconditioner when it is large); the timestep satisfies a derived bound for the scheme in use; the conserved quantity is checked at every output, not only at the end; the observed convergence order is measured on at least three grids against a manufactured solution and matches the design order; and tightening every tolerance once leaves the answer unchanged.

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.