How-to guides
Read and run the C# behind a model
Updated 3 November 2026 · 2 min read
Every run is a C# program, written from your diagram and run by SepalSolver. You can read it, copy it, and take it to the terminal to change and run by hand. If you are learning SepalSolver, it is a worked example of its solvers on a problem you understand.
Read it#
Open the C# Code tab above the canvas. It shows the Run program and, if the model has data and constants ticked for fitting, the Fit program, exactly as they are sent to the runner. The values of your constants are written into the code where they are declared, so it reads as an ordinary program.
- 1The constants, with your values written in.
- 2Model: each stock’s rate of change, from its flows.
The run program is laid out the same way for every model:
- Constants, one line each, with their units and names as comments.
- Tables, blocks and helpers: your lookup tables, your C# blocks, and small functions like
Step. - Evaluate, which works out every variable and flow from the stocks at time
t, in order. - Model, which turns the flows into each stock’s rate of change. This is the function the solver integrates.
- The solve: one call to the solver, such as
Ode45(Model, Start(), grid, Odeset(RelTol: 1e-8)), over the time grid. - The result, printed as one line of JSON that the builder reads back to draw the charts.
The fit program is the same model followed by a call to Lsqcurvefit, the residuals it minimises, and the standard errors behind the ± values.
Run it yourself#
Press Open in the Terminal, or Open in Terminal (C#) under the side panel. The program opens in the C# terminal, where you can change any line and run it. Nothing you change there affects the model.
This is the quickest way to try something the builder doesn’t offer: a different tolerance, printing an extra value at every step, or calling another SepalSolver function on the result.
See also
- What the Model Builder isStocks, flows and C#: what you draw, what runs it, and what you can do with the answer.
- Write a rule in C#When a formula can't say it: ifs, loops and local variables in a block of your own C#.
- SolversThe nine ODE solvers, what each is for, and the accuracy they hold.
- Fit to DataLsqcurvefit: the constants that make the model match the measurements best.