Simple solver for generated models¶
The theory behind how a numerical solver or integration program works is outlined in the Theory of ODE solvers page. This section describes how to use the simple solvers provided to run models generated with either the C or Python profiles.
Contents
C profile solver¶
In C++ you can use the code provided in the tutorials/solver directory to build your generated code into a runnable simulation.
To use the package just follow the instructions below.
You will need to download:
CMakeLists.txtThe CMake file which controls the building of the solver. Note that this is a little different from usual, as outlined below.
simpleSolver.cppThe source file.
1.a Assuming you’ve already generated code using the C profile, open a terminal window and navigate into the tutorials/solver directory.
cd your_base_path/tutorials/solver
Because the code you’ve generated needs to be built at the same time as the solver code is built, each different model requires rebuilding the solver to include the generated code.
1.b The next step is to build your generated code into the solver code.
From inside the tutorials/solver directory, use the CMake command line to point to your generated files.
NB It’s assumed that both of the header and source files have the same base filename (eg: baseFileName.c and baseFileName.h).
The general CMake command is below.
cmake -DINPUT=/path/to/your/files/baseFileName .
Note that the fullstop in the cmake command sets both the source and binary directories to the solver directory. This is because even though your generated files are elsewhere, the solver code and CMakeLists.txt file are in this directory.
If all has gone well you should see the output similar to:
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
1) First use 'make -j' to build the file for running
2) Then solve by running: ./solve_baseFileName with the arguments:
-n step_total
-dt step_size
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/your/stuff/tutorials/solver
1.c Following the instructions in the output, next you need to build the executable by entering:
make -j
1.d Finally you’re ready to solve your model.
The executable will have been given the prefix solve_ and then your baseFileName, and can be run using the command line flags -n to indicate the number of steps to run, and -dt to indicate the step size.
For example:
./solve_baseFileName -n 20000 -dt 0.001
The parameters read from the file, along with your command line arguments are printed to the terminal for checking, and the results of the simulation written to a tab-delimited file with the extension _solution.txt after your base file name.
Python profile solver¶
The solver script is a very simple implementation of the Euler stepping method in Python. The theory on which it’s based can be found on the Theory of ODE solver page. You will need to download:
simplesolver.pyThe solver script.
Once you’ve used the Generator to write a CellML model into Python format, you need to run it to produce the solution.
The script can be run from the command line as below:
1. Navigate into the “solver” directory
cd your_base_path/tutorials/solver
2. Run the solver. To do this you’ll need to enter:
-mthe path to the generated file to run, relative to the solver directory;
-dtthe step size for the integration variable to take; and
-nthe total number of steps to take.
python3 solveGeneratedModel.py -m path_to_your_file -n number_of_steps -dt step_size
You should see output to the terminal which echoes the settings and initial conditions, as below.
An example file for running is provided for you in the resources/tutorial3_PredatorPrey_generated.py file, which can be run for 2000 steps and a step size of 0.01.
Running this will give you the terminal output:
python3 solveGeneratedModel.py -m ../resources/tutorial3_PredatorPrey_generated.py -dt 0.01 -n 2000
====================================================================
SIMPLE SOLVER: ../resources/tutorial3_PredatorPrey_generated
--------------------------------------------------------------------
VARIABLE OF INTEGRATION (units, stepsize)
--------------------------------------------------------------------
time (day, 0.01)
2000 steps
STATE VARIABLES (units, initial value)
--------------------------------------------------------------------
y_s (number_of_sharks, 1.0)
y_f (thousands_of_fish, 2.0)
VARIABLES (units, initial value)
--------------------------------------------------------------------
a (per_day, -0.8)
b (per_shark_day, 0.3)
d (per_fish_day, -0.6)
c (per_day, -2.8)
SOLUTION written to ../resources/tutorial3_PredatorPrey_generated_solution.txt
====================================================================
The output is a tab delimited file with the ending _solution.txt after the input file name (note that it’s in the same directory as the running file too), which can be opened by the plotting program of your choice.