Materials-simulation-skills numerical-stability
install
source · Clone the upstream repo
git clone https://github.com/HeshamFS/materials-simulation-skills
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HeshamFS/materials-simulation-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/core-numerical/numerical-stability" ~/.claude/skills/heshamfs-materials-simulation-skills-numerical-stability && rm -rf "$T"
manifest:
skills/core-numerical/numerical-stability/SKILL.mdsource content
Numerical Stability
Goal
Provide a repeatable checklist and script-driven checks to keep time-dependent simulations stable and defensible.
Requirements
- Python 3.8+
- NumPy (for matrix_condition.py and von_neumann_analyzer.py)
- See
for dependenciesscripts/requirements.txt
Inputs to Gather
| Input | Description | Example |
|---|---|---|
Grid spacing | Spatial discretization | |
Time step | Temporal discretization | |
Velocity | Advection speed | |
Diffusivity | Thermal/mass diffusivity | |
Reaction rate | First-order rate constant | |
| Dimensions | 1D, 2D, or 3D | |
| Scheme type | Explicit or implicit | |
Decision Guidance
Choosing Explicit vs Implicit
Is the problem stiff (fast + slow dynamics)? ├── YES → Use implicit or IMEX scheme │ └── Check conditioning with matrix_condition.py └── NO → Is CFL/Fourier satisfied with reasonable dt? ├── YES → Use explicit scheme (cheaper per step) └── NO → Consider implicit or reduce dx
Stability Limit Quick Reference
| Physics | Number | Explicit Limit (1D) | Formula |
|---|---|---|---|
| Advection | CFL | C ≤ 1 | |
| Diffusion | Fourier | Fo ≤ 0.5 | |
| Reaction | Reaction | R ≤ 1 | |
Multi-dimensional correction: For d dimensions, diffusion limit is
Fo ≤ 1/(2d).
Script Outputs (JSON Fields)
| Script | Key Outputs |
|---|---|
| , , , |
| , |
| , |
| , , |
Workflow
- Identify dominant physics (advection vs diffusion vs reaction)
- Run CFL checker with
scripts/cfl_checker.py - Compare to limits and adjust
if neededdt - Check stiffness with
if multiple scalesscripts/stiffness_detector.py - Analyze custom schemes with
scripts/von_neumann_analyzer.py - Check conditioning with
for implicit solvesscripts/matrix_condition.py - Document the stability verdict and recommended time step
Conversational Workflow Example
User: My phase-field simulation is blowing up after 100 steps. I'm using explicit Euler with dx=0.01, dt=1e-4, and diffusivity D=1e-3.
Agent workflow:
- Check stability criteria:
python3 scripts/cfl_checker.py --dx 0.01 --dt 1e-4 --diffusivity 1e-3 --dimensions 2 --json - Interpret results:
- Fourier number:
Fo = 1e-3 × 1e-4 / (0.01)² = 1.0 - 2D limit:
Fo ≤ 0.25 - Violation: Fo = 1.0 > 0.25, unstable!
- Fourier number:
- Recommend fix:
- Reduce dt to
(to get Fo = 0.25)2.5e-5 - Or increase dx, or switch to implicit
- Reduce dt to
Pre-Simulation Stability Checklist
- Identify dominant physics and nondimensional groups
- Compute CFL/Fourier/Reaction numbers with
cfl_checker.py - If explicit and limit violated, reduce
or change schemedt - If stiffness ratio > 1000, select implicit/stiff integrator
- For custom schemes, verify amplification factor ≤ 1
- Document stability reasoning with inputs and outputs
CLI Examples
# Check CFL/Fourier for 2D diffusion-advection python3 scripts/cfl_checker.py --dx 0.1 --dt 0.01 --velocity 1.0 --diffusivity 0.1 --dimensions 2 --json # Von Neumann analysis for custom 3-point stencil python3 scripts/von_neumann_analyzer.py --coeffs 0.2,0.6,0.2 --dx 1.0 --nk 128 --json # Detect stiffness from eigenvalue estimates python3 scripts/stiffness_detector.py --eigs=-1,-1000 --json # Check matrix conditioning for implicit system python3 scripts/matrix_condition.py --matrix A.npy --norm 2 --json
Error Handling
| Error | Cause | Resolution |
|---|---|---|
| Zero or negative values | Provide valid positive numbers |
| Missing velocity/diffusivity | Provide at least one physics parameter |
| Invalid path | Check matrix file exists |
| Singular or ill-formed matrix | Check matrix validity |
Interpretation Guidance
| Scenario | Meaning | Action |
|---|---|---|
| All checked criteria satisfied | Proceed with simulation |
| At least one limit violated | Reduce dt or change scheme |
| No criteria could be applied | Provide more physics inputs |
| Stiffness ratio > 1000 | Problem is stiff | Use implicit integrator |
| Condition number > 10⁶ | Ill-conditioned | Use scaling/preconditioning |
Security
Input Validation
- All numeric parameters (
,dx
,dt
,velocity
,diffusivity
) are validated as finite positive numbers before any computationdimensions
is restricted to--dimensions{1, 2, 3}- Comma-separated eigenvalue lists (
) are capped at 10,000 entries and validated as finite numbers--eigs - Stencil coefficient lists (
) are length-limited and validated as finite floats--coeffs
File Access
reads a single matrix file (matrix_condition.py
format) specified by.npy
; no directory traversal beyond the given path--matrix- Matrix files are rejected if they exceed 500 MB before parsing
is called withnp.load()
to prevent arbitrary code execution via craftedallow_pickle=False
files.npy- Scripts write only to stdout (JSON output); no files are created unless the agent explicitly uses the Write tool
Tool Restrictions
- Read: Used to inspect script source, references, and user configuration files
- Bash: Used to execute the four Python analysis scripts (
,cfl_checker.py
,von_neumann_analyzer.py
,matrix_condition.py
) with explicit argument listsstiffness_detector.py - Write: Used to save analysis results or generated reports; writes are scoped to the user's working directory
- Grep/Glob: Used to locate relevant files and search references
Safety Measures
- No
,eval()
, or dynamic code generationexec() - All subprocess calls use explicit argument lists (no
)shell=True - Matrix dimension limits (100,000 per dimension) prevent memory exhaustion
- JSON output mode (
) produces structured, parseable results without shell-interpretable content--json
Limitations
- Explicit schemes only for CFL/Fourier checks (implicit is unconditionally stable)
- Von Neumann analysis assumes linear, constant-coefficient, periodic BCs
- Stiffness detection requires eigenvalue estimates from user
References
- Decision thresholds and formulasreferences/stability_criteria.md
- Frequent failure modes and fixesreferences/common_pitfalls.md
- Stability properties of common schemesreferences/scheme_catalog.md
Version History
- v1.1.0 (2024-12-24): Enhanced documentation, decision guidance, examples
- v1.0.0: Initial release with 4 stability analysis scripts