1---2name: differentiation-schemes3description: Select and apply numerical differentiation schemes for PDE and ODE discretization — generate finite-difference stencils at arbitrary order and accuracy, choose between central, upwind, compact (Pade), and spectral methods, handle boundary stencils, and estimate truncation error scaling. Use when discretizing spatial derivatives, picking a scheme for advection- or diffusion-dominated problems, building custom stencils for nonstandard operators, or comparing dispersion and dissipation properties of candidate schemes, even if the user just says "how do I approximate this derivative" or "my solution is too diffusive."4---56# Differentiation Schemes78## Goal910Provide a reliable workflow to select a differentiation scheme, generate stencils, and assess accuracy for simulation discretization.1112## Requirements1314- Python 3.10+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 scripts