|
svZeroDSolver
|
Source file to read simulation configuration. More...
#include <list>#include <nlohmann/json.hpp>#include <stdexcept>#include <string>#include "ActivationFunction.h"#include "ActiveStress.h"#include "Model.h"#include "SphereMaterial.h"#include "State.h"#include "debug.h"Go to the source code of this file.
Classes | |
| struct | SimulationParameters |
| Simulation parameters. More... | |
| class | JsonWrapper |
| Wrapper class for nlohmann:json with error checking. More... | |
Functions | |
| int | generate_block (Model &model, const nlohmann::json &block_params_json, const std::string &block_type, const std::string_view &name, bool internal=false, bool periodic=true) |
| Generate a new block and add its parameters to the model. | |
| std::unique_ptr< ActivationFunction > | generate_activation_function (Model &model, const nlohmann::json &j, const std::string &chamber_name) |
| Create an activation function from JSON (analogous to generate_block). | |
| std::unique_ptr< SphereMaterial > | generate_material (const nlohmann::json &j, const std::string &chamber_name) |
| Create a SphereMaterial from JSON (analogous to generate_activation_function). | |
| std::unique_ptr< ActiveStress > | generate_active_stress (const nlohmann::json &j, const std::string &chamber_name) |
| Create an ActiveStress model from JSON (analogous to generate_material). | |
| State | load_initial_condition (const nlohmann::json &config, Model &model) |
| Load initial conditions from a JSON configuration. | |
| SimulationParameters | load_simulation_params (const nlohmann::json &config) |
| Load the simulation parameters from a JSON configuration. | |
| void | resolve_cardiac_cycle_period (Model &model, const SimulationParameters ¶ms) |
| Reconcile the cardiac cycle period between the model and parameters. | |
| void | load_simulation_model (const nlohmann::json &config, Model &model) |
| Load the 0D block in the model from a configuration. | |
| void | validate_input (const nlohmann::json &config) |
| Check that the JSON configuration has the required inputs. | |
| void | create_vessels (Model &model, std::vector< std::tuple< std::string, std::string > > &connections, const nlohmann::json &config, const std::string &component, std::map< int, std::string > &vessel_id_map) |
| Handle the creation of vessel blocks and connections with boundary conditions. | |
| void | create_external_coupling (Model &model, std::vector< std::tuple< std::string, std::string > > &connections, const nlohmann::json &config, const std::string &component, std::map< int, std::string > &vessel_id_map, std::map< std::string, std::string > &bc_type_map) |
| Handle the creation of external coupling blocks and connections with other blocks. | |
| void | create_boundary_conditions (Model &model, const nlohmann::json &config, const std::string &component, std::map< std::string, std::string > &bc_type_map, std::vector< std::string > &closed_loop_bcs) |
| Handle the creation of boundary condition blocks. | |
| void | create_junctions (Model &model, std::vector< std::tuple< std::string, std::string > > &connections, const nlohmann::json &config, const std::string &component, std::map< int, std::string > &vessel_id_map) |
| Handle the creation of junctions and their connections. | |
| void | create_closed_loop (Model &model, std::vector< std::tuple< std::string, std::string > > &connections, const nlohmann::json &config, const std::string &component, std::vector< std::string > &closed_loop_bcs) |
| Handle the creation of closed-loop blocks and associated connections. | |
| void | create_valves (Model &model, std::vector< std::tuple< std::string, std::string > > &connections, const nlohmann::json &config, const std::string &component) |
| Handle the creation of valves and their associated connections. | |
| void | create_chambers (Model &model, std::vector< std::tuple< std::string, std::string > > &connections, const nlohmann::json &config, const std::string &component) |
| Handle the creation of chambers. | |
Source file to read simulation configuration.
| void create_boundary_conditions | ( | Model & | model, |
| const nlohmann::json & | config, | ||
| const std::string & | component, | ||
| std::map< std::string, std::string > & | bc_type_map, | ||
| std::vector< std::string > & | closed_loop_bcs ) |
Handle the creation of boundary condition blocks.
The block type is read from bc_type. Valid values are the blocks of class BlockClass::boundary_condition or BlockClass::closed_loop, except ClosedLoopHeartAndPulmonary, which is configured under closed_loop_blocks (see create_closed_loop()). Currently these are:
| bc_type | Block |
|---|---|
| FLOW | FlowReferenceBC: prescribed flow rate |
| PRESSURE | PressureReferenceBC: prescribed pressure |
| RESISTANCE | ResistanceBC |
| RCR | WindkesselBC |
| IMPEDANCE | ImpedanceBC |
| CORONARY | OpenLoopCoronaryBC |
| CORONARY_VAR_RES | OpenLoopCoronaryVarResBC |
| ClosedLoopRCR | ClosedLoopRCRBC |
| ClosedLoopCoronaryLeft | ClosedLoopCoronaryLeftBC |
| ClosedLoopCoronaryRight | ClosedLoopCoronaryRightBC |
bc_type values are validated in load_simulation_model(), before this is called.
| model | The model the block is associated with |
| config | The JSON configuration |
| component | Name of the component to retrieve from config |
| bc_type_map | Map between boundary condition names and their types |
| closed_loop_bcs | List of boundary conditions that should be connected to a closed loop heart block |
| void create_chambers | ( | Model & | model, |
| std::vector< std::tuple< std::string, std::string > > & | connections, | ||
| const nlohmann::json & | config, | ||
| const std::string & | component ) |
Handle the creation of chambers.
The block type is read from type and must be a block of class BlockClass::chamber. Currently these are ChamberSphere (ChamberSphere), ChamberElastanceInductor (ChamberElastanceInductor), ChamberElastanceInductorExponential (ChamberElastanceInductorExponential), and LinearElastanceChamber (LinearElastanceChamber).
| model | The model the block is associated with |
| connections | Vector storing the connections between blocks |
| config | The JSON configuration containing all the closed loop blocks |
| component | Name of the component to retrieve from config |
| void create_closed_loop | ( | Model & | model, |
| std::vector< std::tuple< std::string, std::string > > & | connections, | ||
| const nlohmann::json & | config, | ||
| const std::string & | component, | ||
| std::vector< std::string > & | closed_loop_bcs ) |
Handle the creation of closed-loop blocks and associated connections.
The block type is read from closed_loop_type. The only valid value is ClosedLoopHeartAndPulmonary (ClosedLoopHeartPulmonary), and at most one may be included.
| model | The model the block is associated with |
| connections | Vector storing the connections between blocks |
| config | The JSON configuration |
| component | Name of the component to retrieve from config |
| closed_loop_bcs | List of boundary conditions that should be connected to a closed loop heart block |
< Flag to check if heart block is present (requires different handling)
| void create_external_coupling | ( | Model & | model, |
| std::vector< std::tuple< std::string, std::string > > & | connections, | ||
| const nlohmann::json & | config, | ||
| const std::string & | component, | ||
| std::map< int, std::string > & | vessel_id_map, | ||
| std::map< std::string, std::string > & | bc_type_map ) |
Handle the creation of external coupling blocks and connections with other blocks.
An external coupling block is the boundary through which an external solver (e.g. a 3D solver such as svMultiPhysics) exchanges flow and pressure with the 0D model. Its type accepts the same values as bc_type (see create_boundary_conditions()), but use FLOW or PRESSURE: those are the blocks whose time series the coupling interface updates (update_block_params() in interface.cpp), and the error for an invalid type says so. Besides name, type, and values, each block has:
Only some connected blocks are supported at each location. A connected block's type is its bc_type for a boundary condition, or BloodVessel for any vessel. These lists are hard-coded in the implementation (supported_types), so update both together:
An invalid type or location, an unknown connected_block, or an unsupported connected block type fails at load time with an error naming the coupling block.
| model | The model the block is associated with |
| connections | Vector storing the connections between blocks |
| config | The JSON configuration |
| component | Name of the component to retrieve from config |
| vessel_id_map | Map between vessel names and IDs |
| bc_type_map | Map between boundary condition names and their types |
| void create_junctions | ( | Model & | model, |
| std::vector< std::tuple< std::string, std::string > > & | connections, | ||
| const nlohmann::json & | config, | ||
| const std::string & | component, | ||
| std::map< int, std::string > & | vessel_id_map ) |
Handle the creation of junctions and their connections.
The block type is read from junction_type and must be a block of class BlockClass::junction. Currently these are NORMAL_JUNCTION (Junction), resistive_junction (ResistiveJunction), and BloodVesselJunction (BloodVesselJunction).
| model | The model the block is associated with |
| connections | Vector storing the connections between blocks |
| config | The JSON configuration |
| component | Name of the component to retrieve from config |
| vessel_id_map | Map between vessel names and IDs |
| void create_valves | ( | Model & | model, |
| std::vector< std::tuple< std::string, std::string > > & | connections, | ||
| const nlohmann::json & | config, | ||
| const std::string & | component ) |
Handle the creation of valves and their associated connections.
The block type is read from type and must be a block of class BlockClass::valve. Currently these are ValveTanh (ValveTanh) and PiecewiseValve (PiecewiseValve).
| model | The model the block is associated with |
| connections | Vector storing the connections between blocks |
| config | The JSON configuration |
| component | Name of the component to retrieve from config |
| void create_vessels | ( | Model & | model, |
| std::vector< std::tuple< std::string, std::string > > & | connections, | ||
| const nlohmann::json & | config, | ||
| const std::string & | component, | ||
| std::map< int, std::string > & | vessel_id_map ) |
Handle the creation of vessel blocks and connections with boundary conditions.
The block type is read from zero_d_element_type and must be a block of class BlockClass::vessel. Currently these are BloodVessel (BloodVessel), BloodVesselCRL (BloodVesselCRL), and BloodVesselRC (BloodVesselRC).
| model | The model the block is associated with |
| connections | Vector storing the connections between blocks |
| config | The JSON configuration |
| component | Name of the component to retrieve from config |
| vessel_id_map | Map between vessel names and IDs |
| std::unique_ptr< ActivationFunction > generate_activation_function | ( | Model & | model, |
| const nlohmann::json & | j, | ||
| const std::string & | chamber_name ) |
Create an activation function from JSON (analogous to generate_block).
Validates keys against the activation type's params, reads scalar parameters, and returns a created ActivationFunction. Caller associates it with a chamber block via set_activation_function().
| model | Model (for cardiac_cycle_period) |
| j | JSON object (e.g. chamber_config["activation_function"]) |
| chamber_name | Chamber name for error messages |
| std::unique_ptr< ActiveStress > generate_active_stress | ( | const nlohmann::json & | j, |
| const std::string & | chamber_name ) |
Create an ActiveStress model from JSON (analogous to generate_material).
Validates keys against the active stress type's params, reads scalar parameters, and returns a created ActiveStress. Caller associates it with a ChamberSphere block via set_active_stress().
| j | JSON object (e.g. chamber_config["active_stress"]) |
| chamber_name | Chamber name for error messages |
| int generate_block | ( | Model & | model, |
| const nlohmann::json & | block_params_json, | ||
| const std::string & | block_type, | ||
| const std::string_view & | name, | ||
| bool | internal = false, | ||
| bool | periodic = true ) |
Generate a new block and add its parameters to the model.
| model | The model that the block is added to |
| block_params_json | The JSON configuration containing the block parameter values |
| block_type | The type of block |
| name | The name of the block |
| internal | Is this an internal block? This is relevant for the calibrator |
| periodic | Is this block periodic with the cardiac cycle? This is relevant for coupling with external solvers |
| std::unique_ptr< SphereMaterial > generate_material | ( | const nlohmann::json & | j, |
| const std::string & | chamber_name ) |
Create a SphereMaterial from JSON (analogous to generate_activation_function).
Validates keys against the material type's params, reads scalar parameters, and returns a created SphereMaterial. Caller associates it with a ChamberSphere block via set_material().
| j | JSON object (e.g. chamber_config["material"]) |
| chamber_name | Chamber name for error messages |
Load initial conditions from a JSON configuration.
| config | The JSON configuration |
| model | The model |
| void load_simulation_model | ( | const nlohmann::json & | config, |
| Model & | model ) |
Load the 0D block in the model from a configuration.
Every block is created from a type string, read from a key that depends on the configuration section the block is in:
| Section | Type key | Valid values documented in |
|---|---|---|
| vessels | zero_d_element_type | create_vessels() |
| junctions | junction_type | create_junctions() |
| boundary_conditions | bc_type | create_boundary_conditions() |
| external_solver_coupling_blocks | type | create_external_coupling() |
| closed_loop_blocks | closed_loop_type | create_closed_loop() |
| valves | type | create_valves() |
| chambers | type | create_chambers() |
Each loader checks its type key before creating the block. An invalid type fails at load time with an error that names the block, section, and key, and lists the valid values. The loaders derive those values from the block factory (Model::block_factory_map) filtered by BlockClass, so a newly registered block is accepted in the section that matches its class without changes to the loader code. Type strings are case-sensitive.
The type lists in the comments of the individual loaders are written out by hand as a reference and can fall behind the registered blocks; the list in the error message is authoritative.
External coupling blocks are created before boundary conditions, so bc_type values are validated first, while building the map of boundary condition types. Otherwise a misspelled bc_type on a boundary condition that a coupling block connects to would be reported as an unrelated coupling error.
| config | The json configuration |
| model | The 0D model |
| SimulationParameters load_simulation_params | ( | const nlohmann::json & | config | ) |
Load the simulation parameters from a JSON configuration.
| config | The JSON configuration |
| void resolve_cardiac_cycle_period | ( | Model & | model, |
| const SimulationParameters & | params ) |
Reconcile the cardiac cycle period between the model and parameters.
The period may be defined by a model block (e.g. a closed-loop block or a chamber) or by simulation_parameters.cardiac_period. This resolves model.cardiac_cycle_period from both sources, throws if the two disagree, and falls back to a default of 1.0 if neither defines it. Must be called by every entry point (solver and coupled interface) before blocks that depend on the period are used.
| model | The 0D model |
| params | Simulation parameters read from the configuration |
| void validate_input | ( | const nlohmann::json & | config | ) |
Check that the JSON configuration has the required inputs.
| config | The JSON configuration |