Model Generation and Simulation
Prerequisites
- A Python environment with libcuflynx installed (see Getting Started); OpenCOR, to open the generated model by hand.
- A
user_inputs.yamlfile configured withfile_prefix,resources_dir, andgenerated_models_dir. *_module_array.json(or.csv) and*_parameters.jsonfiles in your resources directory.
Software Outline
The Circulatory_Autogen project ([project_dir]) contains five folders as presented below:
- resources: Contains example files that define models (
[file_prefix]_module_array.csv), parameter values ([file_prefix]_parameters.json), parameters to calibrate ([file_prefix]_params_for_id.json), and ground truth data to calibrate towards ([file_prefix]_obs_data.json) for generating and calibrating models. - src: Containts the source code for autogeneration, parameter id, and other utilities.
- user_run_files: Includes bash run files for the user and the
user_inputs.yamlfile, which is the main config file for the run settings. - funcs_user: Where you put your own functions for calculating output features from model outputs (operations), your own cost functions, and your own modifier functions. Name the file in
user_inputs.yamlwithoperation_funcs_external_path/cost_funcs_external_path/modifier_funcs_external_path; copy one of the*_funcs_example.pytemplates to start, and seefuncs_user/README.md. The built-in operations and costs ship inside the package, at[project_dir]/src/libcuflynx/param_id/operation_funcs.pyand[project_dir]/src/libcuflynx/funcs/. - module_config_user: Contains user defined units, CellML modules, and configuration files for those modules. This allows the user to create their own modules and specify how they can be coupled with other modules. The corresponding source directory, which contains all built-in modules and configs, is
[project_dir]/src/libcuflynx/generators/resources/in a checkout, and ships inside the installed package aslibcuflynx/generators/resources/.
Note
For recommended use, the user should create a separate [CA_user_dir] for the specific model they are creating. In this dir there should be the following:
- [file_prefix]_user_inputs.yaml
- resources: Contains the files that define the model connection network ([file_prefix]_module_array.csv) and parameter values ([file_prefix]_parameters.json) that will be generated and config files to prescribe the parameters to calibrate ([file_prefix]_params_for_id.json) and the ground truth to calibrate towards ([file_prefix]_obs_data.json).
Note
Set external_modules_dir to a directory where you store additional *_modules.cellml and *_modules_config.json files if you want modules external to the repo. This path can be relative to your user_inputs.yaml location. To load a whole module library (one module per subdirectory), use module_library_dirs, and set use_builtin_modules: false to use it instead of the built-in modules; see Designing a model.
The following folders will be generated in [CA_user_dir] (or [project_dir] if user_inputs_path_override isn't defined) after running model autogeneration and parameter identification.
- generated_models: Includes the generated code for the models that have been automatically generated. It also contains the generated models with parameters that have been fit with the parameter identification code. These models can be opened and run in OpenCOR, or simulated from Python with libcuflynx.
- param_id_output: Includes all outputs from parameter identification runs, including predicted parameter values, minimum costs, standard deviations of parameters (if doing MCMC), and plots of the fitting results and parameter distributions.
Model Generation
This section shows how to generate your desired model. There are several examples to show the generality of the circulatory_autogen software.
The following are the steps for model autogeneration.
-
Create the module_array and parameters files for the intended model. Standard names are [model name]_module_array.csv and [model name]_parameters.json (nested JSON; see Designing a model), respectively. The module array may also be a JSON file, [model name]_module_array.json (see Designing a model).
Those files should be added to your
resourcesdirectory which is set withresources_dirin your[CA_user_dir]/[file_prefix]_user_inputs.yaml(or[project_dir]/user_run_files/user_inputs.yamlifuser_inputs_path_overrideisn't defined).
Note
The standard location for the resources dir is [CA_user_dir]/resources.
Info
If the name of your model is 3compartment, the user files needed for generation are:
3compartment_module_array.csv3compartment_parameters.json
Note
You can refer to the section Designing a new model for more details on creating module_array and parameters files.
-
Go to the
[CA_user_dir]and open the[file_prefix]_user_inputs.yamlto edit. You can use your editor of choice.file_prefixshould be the name of your model, andinput_param_fileshould be[file_prefix]_parameters.json. If you keep the defaultuser_run_files/user_inputs.yaml, setuser_inputs_path_overrideto point to your[CA_user_dir]/[file_prefix]_user_inputs.yaml. Setmodel_typetocellml(default),python, orcppdepending on the output you want. In place offile_prefixandresources_diryou can name the module array file itself,module_array_path: resources/[file_prefix]_module_array.json, and in place ofinput_param_filea parameters file anywhere,parameters_path: path/to/[file_prefix]_parameters.json. A key libcuflynx does not read is refused, with the key you probably meant.
-
To run autogeneration, navigate to the
user_run_filesdirectory and run:./run_autogeneration.shAs shown below, this will create CellML files for the generated model and test that the simulation runs. Consequently, If there are no errors, it shows the "Model generation has been successful." message at the end.
Note
Alternatively, use an IDE, set the Python interpreter to your venv's (see Getting Started), and run
python -m libcuflynx.scripts.script_generate_with_new_architecture(the module lives at[project_dir]/src/libcuflynx/scripts/in a checkout).
-
Generated files are located in
[generated_models_dir]/[file_prefix]. (generated_models_dirdefaults to[project_dir]/generated_modelsunless you set it inuser_inputs.yaml.)For
model_type: cellml, five CellML files and a JSON file are generated: the main model[file_prefix].cellml, the modules, parameters and units/constants files it imports,[file_prefix]_flat.cellml(the same model in one file) and[file_prefix]_parameters.jsonwith the model's parameter values (see the note below).For
model_type: python, a Python module is also generated alongside the CellML files. Formodel_type: cpp, the model equations are generated by libCellML as C code (model0d_core.c/.h) and wrapped by a C++Model0dclass, amain0ddriver and aCMakeLists.txt. These files are written tocpp_generated_models_dir, or to the model folder when that is not set. Build them withcmake -S <dir> -B <dir>/build && cmake --build <dir>/build, adding-DSUNDIALS_DIR=<prefix>for the CVODE solver if CMake can't find SUNDIALS (versions 5-7 are supported). The solvers areCVODEand the fixed-stepRK4,Heun,midpointandexplEul. A cpp model needssolver_info.dt_solver, the step of the generated solver (forCVODE, the interval at which it returns), inuser_inputs.yaml; generation stops with a message naming the key when it is missing. PETSC is not supported by the current generator. The generated C names every index, e.g.rates[S_heart_module_q_lv]andvariables[V_parameters_r_pvn]. The names match the generated Python's (var.parameters_r_pvn), andmodel0d_core.hlists them with their units.
Info
The generated
[file_prefix]_parameters.json, beside the generated model, holds every value the model uses, in the form of a model's parameters file (aortic_root/C,heart/lv/E_A, globals underglobal), each with its units and data_reference: the values from your[file_prefix]_parameters.jsonin the resources directory and the parameterisations. A model generated with identified parameters has the identified values there. It is the only parameters file generation writes beside the model; the generated model itself names a parameterC_aortic_root(<variable>_<vessel>, a nested vessel's levels joined with__:E_A_heart__lv).A value your
[file_prefix]_parameters.jsondoes not set (a supermodule instance written"heart": {}, or a module's default parameterisation) names where it came from at the start of its data_reference,from <module_type>/<module_subtype> parameterisation <name>: <its own data_reference>:"heart": { "lv": { "E_A": {"value": 366636512, "units": "J_per_m6", "data_reference": "from chamber/Liang2009NoVisco_v02 parameterisation lv: liang2009multi; Table 2 (p. 748), ..."} } }It is the run's complete record: generating again with it as the model's parameters file, and the same module array, gives the same model (a data_reference already starting
fromis not prefixed again), without needing the module library's parameterisations to say the same.
Note
There is a test for the autogeneration running. To run the test, navigate to user_run_files and run the below command.
./run_test_autogeneration.sh
Model Simulation
Once you have generated the models, open OpenCOR and open the generated [file_prefix].cellml, which is the main CellML file. This file automatically incorporates the [file_prefix]_modules.cellml, [file_prefix]_parameters.cellml and [file_prefix]_units.cellml files.
When it is opened, click on the Simulation tab (highlighted with a yellow box in the below image). If there is no error, OpenCOR shows you a new page where models can be simulated. (If there is an error specific to the cellml code then it will be shown here.)

Several individual parts on this page are:
- Simulation settings
- ODE solver settings
- Parameters and variables
- Run control
- Run diagnostics
- Graphs and results
You should set the simulation's starting, ending, and data output step size. Also, if you have a stiff problem you may need to set the maximum_time_step to a small value.
ODE solver settings contains many settings related to the solver such as maximum step size, iteration method, absolute and relative tolerance, name of solver, etc. (shown in the blue box in the above image.)
The parameters and variables section shows all constant and variable parameters that are used in the model (see Designing a model for more information on setting up parameters). You can plot variables by right-clicking each parameter you want for the y-axis and then choosing the x-axis variable (e.g., time).
The run control is on the top left section, as shown in the purple color box in the image. Click on the triangle button to run. For further control, see the OpenCOR Tutorial.
Expected outcome
You should have a generated model in [generated_models_dir]/[file_prefix] and be able to run it in OpenCOR or via Python.
The results will be shown after running the model. These results include run-time, settings, and other related parameters, as shown in the yellow box at the bottom of the image.