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
scripts/requirements.txt for dependencies
Inputs to Gather
| Input |
Description |
Example |
Grid spacing dx |
Spatial discretization |
0.01 m |
Time step dt |
Temporal discretization |
1e-4 s |
Velocity v |
Advection speed |
1.0 m/s |
Diffusivity D |
Thermal/mass diffusivity |
1e-5 m²/s |
Reaction rate k |
First-order rate constant |
100 s⁻¹ |
| Dimensions |
1D, 2D, or 3D |
2 |
| Scheme type |
Explicit or implicit |
explicit |
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 |
C = v·dt/dx |
| Diffusion |
Fourier |
Fo ≤ 0.5 |
Fo = D·dt/dx² |
| Reaction |
Reaction |
R ≤ 1 |
R = k·dt |
Multi-dimensional correction: For d dimensions, diffusion limit is Fo ≤ 1/(2d).
Script Outputs (JSON Fields)
| Script |
Key Outputs |
scripts/cfl_checker.py |
metrics.cfl, metrics.fourier, recommended_dt, stable |
scripts/von_neumann_analyzer.py |
results.max_amplification, results.stable |
scripts/matrix_condition.py |
results.condition_number, results.is_symmetric |
scripts/stiffness_detector.py |
results.stiffness_ratio, results.stiff, results.recommendation |
Workflow
- Identify dominant physics (advection vs diffusion vs reaction)
- Run CFL checker with
scripts/cfl_checker.py
- Compare to limits and adjust
dt if needed
- Check stiffness with
scripts/stiffness_detector.py if multiple scales
- Analyze custom schemes with
scripts/von_neumann_analyzer.py
- Check conditioning with
scripts/matrix_condition.py for implicit solves
- 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!
- Recommend fix:
- Reduce dt to
2.5e-5 (to get Fo = 0.25)
- Or increase dx, or switch to implicit
Pre-Simulation Stability Checklist
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 |
dx and dt must be positive |
Zero or negative values |
Provide valid positive numbers |
No stability criteria applied |
Missing velocity/diffusivity |
Provide at least one physics parameter |
Matrix file not found |
Invalid path |
Check matrix file exists |
Could not compute eigenvalues |
Singular or ill-formed matrix |
Check matrix validity |
Interpretation Guidance
| Scenario |
Meaning |
Action |
stable: true |
All checked criteria satisfied |
Proceed with simulation |
stable: false |
At least one limit violated |
Reduce dt or change scheme |
stable: null |
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, dimensions) are validated as finite positive numbers before any computation
--dimensions is restricted to {1, 2, 3}
- Comma-separated eigenvalue lists (
--eigs) are capped at 10,000 entries and validated as finite numbers
- Stencil coefficient lists (
--coeffs) are length-limited and validated as finite floats
File Access
matrix_condition.py reads a single matrix file (.npy format) specified by --matrix; no directory traversal beyond the given path
- Matrix files are rejected if they exceed 500 MB before parsing
np.load() is called with allow_pickle=False to prevent arbitrary code execution via crafted .npy files
- 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, stiffness_detector.py) with explicit argument lists
- 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(), exec(), or dynamic code generation
- All subprocess calls use explicit argument lists (no
shell=True)
- Matrix dimension limits (100,000 per dimension) prevent memory exhaustion
- JSON output mode (
--json) produces structured, parseable results without shell-interpretable content
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
references/stability_criteria.md - Decision thresholds and formulas
references/common_pitfalls.md - Frequent failure modes and fixes
references/scheme_catalog.md - Stability properties of common schemes
Version History
- v1.1.0 (2024-12-24): Enhanced documentation, decision guidance, examples
- v1.0.0: Initial release with 4 stability analysis scripts
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: heshamfs-materials-simulation-skills-numerical-stability3description: Numerical Stability4---56# Numerical Stability78## Goal910Provide a repeatable checklist and script-driven checks to keep time-dependent simulations stable and defensible.1112## Requirements1314- Python 3.8+15- NumPy (for matrix_condition.py and von_neumann_analyzer.py)16- See `scripts/requirements.txt` for dependencies1718## Inputs to Gather1920| Input | Description | Example |21|-------|-------------|---------|22| Grid spacing `dx` | Spatial discretization | `0.01 m` |23| Time step `dt` | Temporal discretization | `1e-4 s` |24| Velocity `v` | Advection speed | `1.0 m/s` |25| Diffusivity `D` | Thermal/mass diffusivity | `1e-5 m²/s` |26| Reaction rate `k` | First-order rate constant | `100 s⁻¹` |27| Dimensions | 1D, 2D, or 3D | `2` |28| Scheme type | Explicit or implicit | `explicit` |2930## Decision Guidance3132### Choosing Explicit vs Implicit3334```35Is the problem stiff (fast + slow dynamics)?36├── YES → Use implicit or IMEX scheme37│ └── Check conditioning with matrix_condition.py38└── NO → Is CFL/Fourier satisfied with reasonable dt?39 ├── YES → Use explicit scheme (cheaper per step)40 └── NO → Consider implicit or reduce dx41```4243### Stability Limit Quick Reference4445| Physics | Number | Explicit Limit (1D) | Formula |46|---------|--------|---------------------|---------|47| Advection | CFL | C ≤ 1 | `C = v·dt/dx` |48| Diffusion | Fourier | Fo ≤ 0.5 | `Fo = D·dt/dx²` |49| Reaction | Reaction | R ≤ 1 | `R = k·dt` |5051**Multi-dimensional correction**: For d dimensions, diffusion limit is `Fo ≤ 1/(2d)`.5253## Script Outputs (JSON Fields)5455| Script | Key Outputs |56|--------|-------------|57| `scripts/cfl_checker.py` | `metrics.cfl`, `metrics.fourier`, `recommended_dt`, `stable` |58| `scripts/von_neumann_analyzer.py` | `results.max_amplification`, `results.stable` |59| `scripts/matrix_condition.py` | `results.condition_number`, `results.is_symmetric` |60| `scripts/stiffness_detector.py` | `results.stiffness_ratio`, `results.stiff`, `results.recommendation` |6162## Workflow63641. **Identify dominant physics** (advection vs diffusion vs reaction)652. **Run CFL checker** with `scripts/cfl_checker.py`663. **Compare to limits** and adjust `dt` if needed674. **Check stiffness** with `scripts/stiffness_detector.py` if multiple scales685. **Analyze custom schemes** with `scripts/von_neumann_analyzer.py`696. **Check conditioning** with `scripts/matrix_condition.py` for implicit solves707. **Document** the stability verdict and recommended time step7172## Conversational Workflow Example7374**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.7576**Agent workflow**:771. Check stability criteria:78 ```bash79 python3 scripts/cfl_checker.py --dx 0.01 --dt 1e-4 --diffusivity 1e-3 --dimensions 2 --json80 ```812. Interpret results:82 - Fourier number: `Fo = 1e-3 × 1e-4 / (0.01)² = 1.0`83 - 2D limit: `Fo ≤ 0.25`84 - **Violation**: Fo = 1.0 > 0.25, unstable!853. Recommend fix:86 - Reduce dt to `2.5e-5` (to get Fo = 0.25)87 - Or increase dx, or switch to implicit8889## Pre-Simulation Stability Checklist9091- [ ] Identify dominant physics and nondimensional groups92- [ ] Compute CFL/Fourier/Reaction numbers with `cfl_checker.py`93- [ ] If explicit and limit violated, reduce `dt` or change scheme94- [ ] If stiffness ratio > 1000, select implicit/stiff integrator95- [ ] For custom schemes, verify amplification factor ≤ 196- [ ] Document stability reasoning with inputs and outputs9798## CLI Examples99100```bash101# Check CFL/Fourier for 2D diffusion-advection102python3 scripts/cfl_checker.py --dx 0.1 --dt 0.01 --velocity 1.0 --diffusivity 0.1 --dimensions 2 --json103104# Von Neumann analysis for custom 3-point stencil105python3 scripts/von_neumann_analyzer.py --coeffs 0.2,0.6,0.2 --dx 1.0 --nk 128 --json106107# Detect stiffness from eigenvalue estimates108python3 scripts/stiffness_detector.py --eigs=-1,-1000 --json109110# Check matrix conditioning for implicit system111python3 scripts/matrix_condition.py --matrix A.npy --norm 2 --json112```113114## Error Handling115116| Error | Cause | Resolution |117|-------|-------|------------|118| `dx and dt must be positive` | Zero or negative values | Provide valid positive numbers |119| `No stability criteria applied` | Missing velocity/diffusivity | Provide at least one physics parameter |120| `Matrix file not found` | Invalid path | Check matrix file exists |121| `Could not compute eigenvalues` | Singular or ill-formed matrix | Check matrix validity |122123## Interpretation Guidance124125| Scenario | Meaning | Action |126|----------|---------|--------|127| `stable: true` | All checked criteria satisfied | Proceed with simulation |128| `stable: false` | At least one limit violated | Reduce dt or change scheme |129| `stable: null` | No criteria could be applied | Provide more physics inputs |130| Stiffness ratio > 1000 | Problem is stiff | Use implicit integrator |131| Condition number > 10⁶ | Ill-conditioned | Use scaling/preconditioning |132133## Security134135### Input Validation136- All numeric parameters (`dx`, `dt`, `velocity`, `diffusivity`, `dimensions`) are validated as finite positive numbers before any computation137- `--dimensions` is restricted to `{1, 2, 3}`138- Comma-separated eigenvalue lists (`--eigs`) are capped at 10,000 entries and validated as finite numbers139- Stencil coefficient lists (`--coeffs`) are length-limited and validated as finite floats140141### File Access142- `matrix_condition.py` reads a single matrix file (`.npy` format) specified by `--matrix`; no directory traversal beyond the given path143- Matrix files are rejected if they exceed 500 MB before parsing144- `np.load()` is called with `allow_pickle=False` to prevent arbitrary code execution via crafted `.npy` files145- Scripts write only to stdout (JSON output); no files are created unless the agent explicitly uses the Write tool146147### Tool Restrictions148- **Read**: Used to inspect script source, references, and user configuration files149- **Bash**: Used to execute the four Python analysis scripts (`cfl_checker.py`, `von_neumann_analyzer.py`, `matrix_condition.py`, `stiffness_detector.py`) with explicit argument lists150- **Write**: Used to save analysis results or generated reports; writes are scoped to the user's working directory151- **Grep/Glob**: Used to locate relevant files and search references152153### Safety Measures154- No `eval()`, `exec()`, or dynamic code generation155- All subprocess calls use explicit argument lists (no `shell=True`)156- Matrix dimension limits (100,000 per dimension) prevent memory exhaustion157- JSON output mode (`--json`) produces structured, parseable results without shell-interpretable content158159## Limitations160161- **Explicit schemes only** for CFL/Fourier checks (implicit is unconditionally stable)162- **Von Neumann analysis** assumes linear, constant-coefficient, periodic BCs163- **Stiffness detection** requires eigenvalue estimates from user164165## References166167- `references/stability_criteria.md` - Decision thresholds and formulas168- `references/common_pitfalls.md` - Frequent failure modes and fixes169- `references/scheme_catalog.md` - Stability properties of common schemes170171## Version History172173- **v1.1.0** (2024-12-24): Enhanced documentation, decision guidance, examples174- **v1.0.0**: Initial release with 4 stability analysis scripts175176---177> Converted and distributed by [TomeVault](https://tomevault.io/claim/heshamfs) — claim your Tome and manage your conversions.178<!-- tomevault:4.0:skill_md:2026-04-11 -->