Differentiation Schemes
Goal
Provide a reliable workflow to select a differentiation scheme, generate stencils, and assess accuracy for simulation discretization.
Requirements
- Python 3.8+
- NumPy (for stencil computations)
- No heavy dependencies
Inputs to Gather
| Input |
Description |
Example |
| Derivative order |
First, second, etc. |
1 or 2 |
| Target accuracy |
Order of truncation error |
2 or 4 |
| Grid type |
Uniform, nonuniform |
uniform |
| Boundary type |
Periodic, Dirichlet, Neumann |
periodic |
| Smoothness |
Smooth or discontinuous |
smooth |
Decision Guidance
Scheme Selection Flowchart
Is the field smooth?
├── YES → Is domain periodic?
│ ├── YES → Use central differences or spectral
│ └── NO → Use central interior + one-sided at boundaries
└── NO → Are there shocks/discontinuities?
├── YES → Use upwind, TVD, or WENO
└── NO → Use central with limiters
Quick Reference
| Situation |
Recommended Scheme |
| Smooth, periodic |
Central, spectral |
| Smooth, bounded |
Central + one-sided BCs |
| Advection-dominated |
Upwind |
| Shocks/fronts |
TVD, WENO |
| High accuracy needed |
Compact (Padé), spectral |
Script Outputs (JSON Fields)
| Script |
Key Outputs |
scripts/stencil_generator.py |
offsets, coefficients, order, accuracy |
scripts/scheme_selector.py |
recommended, alternatives, notes |
scripts/truncation_error.py |
error_scale, order, notes |
Workflow
- Identify requirements - derivative order, accuracy, smoothness
- Select scheme - Run
scripts/scheme_selector.py
- Generate stencils - Run
scripts/stencil_generator.py
- Estimate error - Run
scripts/truncation_error.py
- Validate - Test with manufactured solutions or grid refinement
Conversational Workflow Example
User: I need to discretize a second derivative for a diffusion equation on a uniform grid. I want 4th-order accuracy.
Agent workflow:
- Select appropriate scheme:
python3 scripts/scheme_selector.py --smooth --periodic --order 2 --accuracy 4 --json
- Generate the stencil:
python3 scripts/stencil_generator.py --order 2 --accuracy 4 --scheme central --json
- Result: 5-point stencil with coefficients
[-1/12, 4/3, -5/2, 4/3, -1/12] / dx².
Pre-Discretization Checklist
CLI Examples
# Select scheme for smooth periodic problem
python3 scripts/scheme_selector.py --smooth --periodic --order 1 --accuracy 4 --json
# Generate central difference stencil for first derivative
python3 scripts/stencil_generator.py --order 1 --accuracy 2 --scheme central --json
# Generate 4th-order second derivative stencil
python3 scripts/stencil_generator.py --order 2 --accuracy 4 --scheme central --json
# Estimate truncation error
python3 scripts/truncation_error.py --dx 0.01 --order 2 --accuracy 2 --scale 1.0 --json
Error Handling
| Error |
Cause |
Resolution |
order must be positive |
Invalid derivative order |
Use 1, 2, 3, ... |
accuracy must be even for central |
Odd accuracy requested |
Use 2, 4, 6, ... |
Unknown scheme |
Invalid scheme type |
Use central, upwind, compact |
Interpretation Guidance
Stencil Properties
| Property |
Meaning |
| Symmetric offsets |
Central scheme (no directional bias) |
| Asymmetric offsets |
One-sided or upwind scheme |
| More points |
Higher accuracy but wider stencil |
Truncation Error Scaling
| Accuracy Order |
Error Scales As |
Refinement Factor |
| 2nd order |
O(dx²) |
2× refinement → 4× error reduction |
| 4th order |
O(dx⁴) |
2× refinement → 16× error reduction |
| 6th order |
O(dx⁶) |
2× refinement → 64× error reduction |
Common Stencils
| Derivative |
Accuracy |
Points |
Coefficients (× 1/dx or 1/dx²) |
| 1st |
2 |
3 |
[-1/2, 0, 1/2] |
| 1st |
4 |
5 |
[1/12, -2/3, 0, 2/3, -1/12] |
| 2nd |
2 |
3 |
[1, -2, 1] |
| 2nd |
4 |
5 |
[-1/12, 4/3, -5/2, 4/3, -1/12] |
Security
Input Validation
--order (derivative order) is validated as a positive integer with an upper bound
--accuracy is validated as a positive even integer for central schemes
--scheme is validated against a fixed allowlist (central, upwind, compact)
--dx and --scale are validated as finite positive numbers
- No user-supplied strings are interpolated into code paths or shell commands
File Access
- Scripts read no external files; all inputs are provided via CLI arguments
- 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 three Python scripts (
stencil_generator.py, scheme_selector.py, truncation_error.py) with explicit argument lists
- Write: Used to save generated stencil coefficients or scheme recommendations; 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)
- Stencil computation uses only NumPy linear algebra on small, bounded matrices (stencil width limited by accuracy order)
- All output is deterministic JSON with no shell-interpretable content
Limitations
- Boundary handling: Stencil generator provides interior stencils; boundaries need special treatment
- Nonuniform grids: Standard stencils assume uniform spacing
- Spectral: Not covered by stencil generator
References
references/stencil_catalog.md - Common stencils
references/boundary_handling.md - One-sided schemes
references/scheme_selection.md - FD/FV/spectral comparison
references/error_guidance.md - Truncation error scaling
Version History
- v1.1.0 (2024-12-24): Enhanced documentation, decision guidance, examples
- v1.0.0: Initial release with 3 differentiation scripts
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: heshamfs-materials-simulation-skills-differentiation-schemes3description: Differentiation Schemes4---56# Differentiation Schemes78## Goal910Provide a reliable workflow to select a differentiation scheme, generate stencils, and assess accuracy for simulation discretization.1112## Requirements1314- Python 3.8+15- NumPy (for stencil computations)16- No heavy dependencies1718## Inputs to Gather1920| Input | Description | Example |21|-------|-------------|---------|22| Derivative order | First, second, etc. | `1` or `2` |23| Target accuracy | Order of truncation error | `2` or `4` |24| Grid type | Uniform, nonuniform | `uniform` |25| Boundary type | Periodic, Dirichlet, Neumann | `periodic` |26| Smoothness | Smooth or discontinuous | `smooth` |2728## Decision Guidance2930### Scheme Selection Flowchart3132```33Is the field smooth?34├── YES → Is domain periodic?35│ ├── YES → Use central differences or spectral36│ └── NO → Use central interior + one-sided at boundaries37└── NO → Are there shocks/discontinuities?38 ├── YES → Use upwind, TVD, or WENO39 └── NO → Use central with limiters40```4142### Quick Reference4344| Situation | Recommended Scheme |45|-----------|-------------------|46| Smooth, periodic | Central, spectral |47| Smooth, bounded | Central + one-sided BCs |48| Advection-dominated | Upwind |49| Shocks/fronts | TVD, WENO |50| High accuracy needed | Compact (Padé), spectral |5152## Script Outputs (JSON Fields)5354| Script | Key Outputs |55|--------|-------------|56| `scripts/stencil_generator.py` | `offsets`, `coefficients`, `order`, `accuracy` |57| `scripts/scheme_selector.py` | `recommended`, `alternatives`, `notes` |58| `scripts/truncation_error.py` | `error_scale`, `order`, `notes` |5960## Workflow61621. **Identify requirements** - derivative order, accuracy, smoothness632. **Select scheme** - Run `scripts/scheme_selector.py`643. **Generate stencils** - Run `scripts/stencil_generator.py`654. **Estimate error** - Run `scripts/truncation_error.py`665. **Validate** - Test with manufactured solutions or grid refinement6768## Conversational Workflow Example6970**User**: I need to discretize a second derivative for a diffusion equation on a uniform grid. I want 4th-order accuracy.7172**Agent workflow**:731. Select appropriate scheme:74 ```bash75 python3 scripts/scheme_selector.py --smooth --periodic --order 2 --accuracy 4 --json76 ```772. Generate the stencil:78 ```bash79 python3 scripts/stencil_generator.py --order 2 --accuracy 4 --scheme central --json80 ```813. Result: 5-point stencil with coefficients `[-1/12, 4/3, -5/2, 4/3, -1/12]` / dx².8283## Pre-Discretization Checklist8485- [ ] Confirm derivative order and target accuracy86- [ ] Choose scheme appropriate to smoothness and boundaries87- [ ] Generate and inspect stencils at boundaries88- [ ] Estimate truncation error vs physics scales89- [ ] Verify with grid refinement study9091## CLI Examples9293```bash94# Select scheme for smooth periodic problem95python3 scripts/scheme_selector.py --smooth --periodic --order 1 --accuracy 4 --json9697# Generate central difference stencil for first derivative98python3 scripts/stencil_generator.py --order 1 --accuracy 2 --scheme central --json99100# Generate 4th-order second derivative stencil101python3 scripts/stencil_generator.py --order 2 --accuracy 4 --scheme central --json102103# Estimate truncation error104python3 scripts/truncation_error.py --dx 0.01 --order 2 --accuracy 2 --scale 1.0 --json105```106107## Error Handling108109| Error | Cause | Resolution |110|-------|-------|------------|111| `order must be positive` | Invalid derivative order | Use 1, 2, 3, ... |112| `accuracy must be even for central` | Odd accuracy requested | Use 2, 4, 6, ... |113| `Unknown scheme` | Invalid scheme type | Use central, upwind, compact |114115## Interpretation Guidance116117### Stencil Properties118119| Property | Meaning |120|----------|---------|121| Symmetric offsets | Central scheme (no directional bias) |122| Asymmetric offsets | One-sided or upwind scheme |123| More points | Higher accuracy but wider stencil |124125### Truncation Error Scaling126127| Accuracy Order | Error Scales As | Refinement Factor |128|----------------|-----------------|-------------------|129| 2nd order | O(dx²) | 2× refinement → 4× error reduction |130| 4th order | O(dx⁴) | 2× refinement → 16× error reduction |131| 6th order | O(dx⁶) | 2× refinement → 64× error reduction |132133### Common Stencils134135| Derivative | Accuracy | Points | Coefficients (× 1/dx or 1/dx²) |136|------------|----------|--------|-------------------------------|137| 1st | 2 | 3 | [-1/2, 0, 1/2] |138| 1st | 4 | 5 | [1/12, -2/3, 0, 2/3, -1/12] |139| 2nd | 2 | 3 | [1, -2, 1] |140| 2nd | 4 | 5 | [-1/12, 4/3, -5/2, 4/3, -1/12] |141142## Security143144### Input Validation145- `--order` (derivative order) is validated as a positive integer with an upper bound146- `--accuracy` is validated as a positive even integer for central schemes147- `--scheme` is validated against a fixed allowlist (`central`, `upwind`, `compact`)148- `--dx` and `--scale` are validated as finite positive numbers149- No user-supplied strings are interpolated into code paths or shell commands150151### File Access152- Scripts read no external files; all inputs are provided via CLI arguments153- Scripts write only to stdout (JSON output); no files are created unless the agent explicitly uses the Write tool154155### Tool Restrictions156- **Read**: Used to inspect script source, references, and user configuration files157- **Bash**: Used to execute the three Python scripts (`stencil_generator.py`, `scheme_selector.py`, `truncation_error.py`) with explicit argument lists158- **Write**: Used to save generated stencil coefficients or scheme recommendations; writes are scoped to the user's working directory159- **Grep/Glob**: Used to locate relevant files and search references160161### Safety Measures162- No `eval()`, `exec()`, or dynamic code generation163- All subprocess calls use explicit argument lists (no `shell=True`)164- Stencil computation uses only NumPy linear algebra on small, bounded matrices (stencil width limited by accuracy order)165- All output is deterministic JSON with no shell-interpretable content166167## Limitations168169- **Boundary handling**: Stencil generator provides interior stencils; boundaries need special treatment170- **Nonuniform grids**: Standard stencils assume uniform spacing171- **Spectral**: Not covered by stencil generator172173## References174175- `references/stencil_catalog.md` - Common stencils176- `references/boundary_handling.md` - One-sided schemes177- `references/scheme_selection.md` - FD/FV/spectral comparison178- `references/error_guidance.md` - Truncation error scaling179180## Version History181182- **v1.1.0** (2024-12-24): Enhanced documentation, decision guidance, examples183- **v1.0.0**: Initial release with 3 differentiation scripts184185---186> Converted and distributed by [TomeVault](https://tomevault.io/claim/heshamfs) — claim your Tome and manage your conversions.187<!-- tomevault:4.0:skill_md:2026-04-11 -->