Materials-simulation-skills differentiation-schemes
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/differentiation-schemes" ~/.claude/skills/heshamfs-materials-simulation-skills-differentiation-schemes && rm -rf "$T"
manifest:
skills/core-numerical/differentiation-schemes/SKILL.mdsource content
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. | or |
| Target accuracy | Order of truncation error | or |
| Grid type | Uniform, nonuniform | |
| Boundary type | Periodic, Dirichlet, Neumann | |
| Smoothness | Smooth or discontinuous | |
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 |
|---|---|
| , , , |
| , , |
| , , |
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
/ dx².[-1/12, 4/3, -5/2, 4/3, -1/12]
Pre-Discretization Checklist
- Confirm derivative order and target accuracy
- Choose scheme appropriate to smoothness and boundaries
- Generate and inspect stencils at boundaries
- Estimate truncation error vs physics scales
- Verify with grid refinement study
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 |
|---|---|---|
| Invalid derivative order | Use 1, 2, 3, ... |
| Odd accuracy requested | Use 2, 4, 6, ... |
| 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
(derivative order) is validated as a positive integer with an upper bound--order
is validated as a positive even integer for central schemes--accuracy
is validated against a fixed allowlist (--scheme
,central
,upwind
)compact
and--dx
are validated as finite positive numbers--scale- 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
) with explicit argument liststruncation_error.py - 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()
, or dynamic code generationexec() - 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
- Common stencilsreferences/stencil_catalog.md
- One-sided schemesreferences/boundary_handling.md
- FD/FV/spectral comparisonreferences/scheme_selection.md
- Truncation error scalingreferences/error_guidance.md
Version History
- v1.1.0 (2024-12-24): Enhanced documentation, decision guidance, examples
- v1.0.0: Initial release with 3 differentiation scripts