|
| 1 | +@page jacobian Jacobian Generator for svZeroDSolver |
| 2 | + |
| 3 | +This tool generates C++ code for block implementations in the svZeroDSolver framework using symbolic mathematics. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The script `script/jacobian.py` reads block definitions from YAML files and generates C++ code for implementing the mathematical models in the solver. It uses symbolic differentiation (via SymPy) to automatically derive the necessary Jacobian matrices and system contributions. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +```bash |
| 12 | +python jacobian.py <yaml_file> |
| 13 | +``` |
| 14 | + |
| 15 | +## YAML File Format |
| 16 | + |
| 17 | +The YAML files define the mathematical model of a block with the following structure: |
| 18 | + |
| 19 | +### Required Sections |
| 20 | + |
| 21 | +- `variables`: List of variable names in the model |
| 22 | +- `derivatives`: List of derivative names (must match variables with `_dt` suffix) |
| 23 | +- `constants`: List of parameter names |
| 24 | +- `residuals`: List of residual equations that define the system |
| 25 | + |
| 26 | +### Optional Sections |
| 27 | + |
| 28 | +- `time_dependent`: List of parameters that depend on time (e.g., activation functions) |
| 29 | +- `helper_functions`: Python code defining helper functions used in the residuals |
| 30 | + |
| 31 | +### Example |
| 32 | + |
| 33 | +```yaml |
| 34 | +variables: |
| 35 | + - Pin |
| 36 | + - Qin |
| 37 | + - Pout |
| 38 | + - Qout |
| 39 | + |
| 40 | +derivatives: |
| 41 | + - dPin_dt |
| 42 | + - dQin_dt |
| 43 | + - dPout_dt |
| 44 | + - dQout_dt |
| 45 | + |
| 46 | +constants: |
| 47 | + - R |
| 48 | + - C |
| 49 | + - L |
| 50 | + - S |
| 51 | + |
| 52 | +residuals: |
| 53 | + - Pin - Pout - (R + S * abs(Qin)) * Qin - L * dQout_dt |
| 54 | + - Qin - Qout - C * dPin_dt + C * (R + 2 * S * abs(Qin)) * dQin_dt |
| 55 | +``` |
| 56 | +
|
| 57 | +## Output |
| 58 | +
|
| 59 | +The script generates three C++ function implementations: |
| 60 | +
|
| 61 | +1. `update_constant` - Sets up constant matrix coefficients for the system |
| 62 | +2. `update_time` - Updates time-dependent parameters |
| 63 | +3. `update_solution` - Computes solution-dependent terms and Jacobians |
| 64 | + |
| 65 | +## Workflow |
| 66 | + |
| 67 | +1. Create a YAML file defining your block's mathematical model |
| 68 | +2. Run `jacobian.py` on this file to generate C++ code |
| 69 | +3. Copy the generated code to your block implementation file |
| 70 | +4. Complete the implementation with necessary boilerplate code |
| 71 | + |
| 72 | +## Tips |
| 73 | + |
| 74 | +- Ensure the number of variables equals the number of derivatives |
| 75 | +- The number of residuals should be equal to the number of variables minus 2 |
| 76 | +- Use helper functions for complex expressions to improve readability |
| 77 | +- Define time-dependent constants separately |
| 78 | + |
| 79 | +## Examples |
| 80 | + |
| 81 | +See the provided YAML examples: |
| 82 | +- `ChamberSphere.yaml` - Spherical heart chamber model |
| 83 | +- `BloodVessel.yaml` - Blood vessel model with optional stenosis |
| 84 | +- `ClosedLoopCoronaryBC.yaml` - Coronary boundary condition model |
0 commit comments