1---2name: differentiation-schemes3description: >4 Select and apply numerical differentiation schemes for PDE and ODE5 discretization — generate finite-difference stencils at arbitrary order and6 accuracy, choose between central, upwind, compact (Pade), and spectral7 methods, handle boundary stencils, and estimate truncation error scaling.8 Use when discretizing spatial derivatives, picking a scheme for advection-9 or diffusion-dominated problems, building custom stencils for nonstandard10 operators, or comparing dispersion and dissipation properties of candidate11 schemes, even if the user just says "how do I approximate this derivative"12 or "my solution is too diffusive."13allowed-tools: Read, Bash, Write, Grep, Glob14metadata:15 source: https://skillmd.com/skills/zuoguyoupan2023/differentiation-schemes16 author: HeshamFS17 version: "1.1.0"18 security_tier: high19 security_reviewed: true20 tested_with:21 - claude-code22 - gemini-cli23 - vs-code-copilot24 eval_cases: 425 last_reviewed: "2026-03-26"26---2728# Differentiation Schemes2930## Goal3132Provide a reliable workflow to select a differentiation scheme, generate stencils, and assess accuracy for simulation discretization.3334## Requirements3536- Python 3.10+37- NumPy (for stencil computations)38- No heavy dependencies3940## Inputs to Gather4142| Input | Description | Example |43|-------|-------------|---------|44| Derivative order | First, second, etc. | `1` or `2` |45| Target accuracy | Order of truncation error | `2` or `4` |46| Grid type | Uniform, nonuniform | `uniform` |47| Boundary type | Periodic, Dirichlet, Neumann | `periodic` |48| Smoothness | Smooth or discontinuous | `smooth` |4950## Decision Guidance5152### Scheme Selection Flowchart5354```55Is the field smooth?56├── YES → Is domain periodic?57│ ├── YES → Use central differences or spectral58│ └── NO → Use central interior + one-sided at boundaries59└── NO → Are there shocks/discontinuities?60 ├── YES → Use upwind, TVD, or WENO61 └── NO → Use central with limiters62```6364### Quick Reference6566| Situation | Recommended Scheme |67|-----------|-------------------|68| Smooth, periodic | Central, spectral |69| Smooth, bounded | Central + one-sided BCs |70| Advection-dominated | Upwind |71| Shocks/fronts | TVD, WENO |72| High accuracy needed | Compact (Padé), spectral |7374## Script Outputs (JSON Fields)7576| Script | Key Outputs |77|--------|-------------|78| `scripts/stencil_generator.py` | `offsets`, `coefficients`, `order`, `accuracy` |79| `scripts/scheme_selector.py` | `recommended`, `alternatives`, `notes` |80| `scripts/truncation_error.py` | `error_scale`, `order`, `notes` |8182## Workflow83841. **Identify requirements** - derivative order, accuracy, smoothness852. **Select scheme** - Run `scripts/scheme_selector.py`863. **Generate stencils** - Run `scripts/stencil_generator.py`874. **Estimate error** - Run `scripts/truncation_error.py`885. **Validate** - Test with manufactured solutions or grid refinement8990## Conversational Workflow Example9192**User**: I need to discretize a second derivative for a diffusion equation on a uniform grid. I want 4th-order accuracy.9394**Agent workflow**:951. Select appropriate scheme:96 ```bash97 python3 scripts/scheme_selector.py --smooth --periodic --order 2 --accuracy 4 --json98 ```992. Generate the stencil:100 ```bash101 python3 scripts/stencil_generator.py --order 2 --accuracy 4 --scheme central --json102 ```1033. Result: 5-point stencil with coefficients `[-1/12, 4/3, -5/2, 4/3, -1/12]` / dx².104105## Pre-Discretization Checklist106107- [ ] Confirm derivative order and target accuracy108- [ ] Choose scheme appropriate to smoothness and boundaries109- [ ] Generate and inspect stencils at boundaries110- [ ] Estimate truncation error vs physics scales111- [ ] Verify with grid refinement study112113## CLI Examples114115```bash116# Select scheme for smooth periodic problem117python3 scripts/scheme_selector.py --smooth --periodic --order 1 --accuracy 4 --json118119# Generate central difference stencil for first derivative120python3 scripts/stencil_generator.py --order 1 --accuracy 2 --scheme central --json121122# Generate 4th-order second derivative stencil123python3 scripts/stencil_generator.py --order 2 --accuracy 4 --scheme central --json124125# Estimate truncation error126python3 scripts/truncation_error.py --dx 0.01 --order 2 --accuracy 2 --scale 1.0 --json127```128129## Error Handling130131| Error | Cause | Resolution |132|-------|-------|------------|133| `order must be positive` | Invalid derivative order | Use 1, 2, 3, ... |134| `accuracy must be even for central` | Odd accuracy requested | Use 2, 4, 6, ... |135| `Unknown scheme` | Invalid scheme type | Use central, upwind, compact |136137## Interpretation Guidance138139### Stencil Properties140141| Property | Meaning |142|----------|---------|143| Symmetric offsets | Central scheme (no directional bias) |144| Asymmetric offsets | One-sided or upwind scheme |145| More points | Higher accuracy but wider stencil |146147### Truncation Error Scaling148149| Accuracy Order | Error Scales As | Refinement Factor |150|----------------|-----------------|-------------------|151| 2nd order | O(dx²) | 2× refinement → 4× error reduction |152| 4th order | O(dx⁴) | 2× refinement → 16× error reduction |153| 6th order | O(dx⁶) | 2× refinement → 64× error reduction |154155### Common Stencils156157| Derivative | Accuracy | Points | Coefficients (× 1/dx or 1/dx²) |158|------------|----------|--------|-------------------------------|159| 1st | 2 | 3 | [-1/2, 0, 1/2] |160| 1st | 4 | 5 | [1/12, -2/3, 0, 2/3, -1/12] |161| 2nd | 2 | 3 | [1, -2, 1] |162| 2nd | 4 | 5 | [-1/12, 4/3, -5/2, 4/3, -1/12] |163164## Security165166### Input Validation167- `--order` (derivative order) is validated as a positive integer with an upper bound168- `--accuracy` is validated as a positive even integer for central schemes169- `--scheme` is validated against a fixed allowlist (`central`, `upwind`, `compact`)170- `--dx` and `--scale` are validated as finite positive numbers171- No user-supplied strings are interpolated into code paths or shell commands172173### File Access174- Scripts read no external files; all inputs are provided via CLI arguments175- Scripts write only to stdout (JSON output); no files are created unless the agent explicitly uses the Write tool176177### Tool Restrictions178- **Read**: Used to inspect script source, references, and user configuration files179- **Bash**: Used to execute the three Python scripts (`stencil_generator.py`, `scheme_selector.py`, `truncation_error.py`) with explicit argument lists180- **Write**: Used to save generated stencil coefficients or scheme recommendations; writes are scoped to the user's working directory181- **Grep/Glob**: Used to locate relevant files and search references182183### Safety Measures184- No `eval()`, `exec()`, or dynamic code generation185- All subprocess calls use explicit argument lists (no `shell=True`)186- Stencil computation uses only NumPy linear algebra on small, bounded matrices (stencil width limited by accuracy order)187- All output is deterministic JSON with no shell-interpretable content188189## Limitations190191- **Boundary handling**: Stencil generator provides interior stencils; boundaries need special treatment192- **Nonuniform grids**: Standard stencils assume uniform spacing193- **Spectral**: Not covered by stencil generator194195## References196197- `references/stencil_catalog.md` - Common stencils198- `references/boundary_handling.md` - One-sided schemes199- `references/scheme_selection.md` - FD/FV/spectral comparison200- `references/error_guidance.md` - Truncation error scaling201202## Version History203204- **v1.1.0** (2024-12-24): Enhanced documentation, decision guidance, examples205- **v1.0.0**: Initial release with 3 differentiation scripts