svZeroDSolver
Loading...
Searching...
No Matches
SimulationParameters.h File Reference

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"
Include dependency graph for SimulationParameters.h:
This graph shows which files directly or indirectly include this file:

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 &params)
 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.

Detailed Description

Source file to read simulation configuration.

Function Documentation

◆ create_boundary_conditions()

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.

Parameters
modelThe model the block is associated with
configThe JSON configuration
componentName of the component to retrieve from config
bc_type_mapMap between boundary condition names and their types
closed_loop_bcsList of boundary conditions that should be connected to a closed loop heart block

◆ create_chambers()

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).

Parameters
modelThe model the block is associated with
connectionsVector storing the connections between blocks
configThe JSON configuration containing all the closed loop blocks
componentName of the component to retrieve from config

◆ create_closed_loop()

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.

Parameters
modelThe model the block is associated with
connectionsVector storing the connections between blocks
configThe JSON configuration
componentName of the component to retrieve from config
closed_loop_bcsList of boundary conditions that should be connected to a closed loop heart block

< Flag to check if heart block is present (requires different handling)

◆ create_external_coupling()

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:

  • connected_block: the 0D block on the other side of the coupling block, given as a boundary condition's bc_name, a vessel's vessel_name, or ClosedLoopHeartAndPulmonary.
  • location: inlet or outlet, the side of connected_block the coupling block attaches to. With inlet, flow goes from the coupling block into connected_block; with outlet, from connected_block into the coupling block. The connection to ClosedLoopHeartAndPulmonary is made by create_closed_loop() instead.

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:

  • inlet: RESISTANCE, RCR, IMPEDANCE, ClosedLoopRCR, CORONARY, ClosedLoopCoronaryLeft, ClosedLoopCoronaryRight, BloodVessel
  • outlet: ClosedLoopRCR, ClosedLoopHeartAndPulmonary, BloodVessel

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.

Parameters
modelThe model the block is associated with
connectionsVector storing the connections between blocks
configThe JSON configuration
componentName of the component to retrieve from config
vessel_id_mapMap between vessel names and IDs
bc_type_mapMap between boundary condition names and their types

◆ create_junctions()

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).

Parameters
modelThe model the block is associated with
connectionsVector storing the connections between blocks
configThe JSON configuration
componentName of the component to retrieve from config
vessel_id_mapMap between vessel names and IDs

◆ create_valves()

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).

Parameters
modelThe model the block is associated with
connectionsVector storing the connections between blocks
configThe JSON configuration
componentName of the component to retrieve from config

◆ create_vessels()

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).

Parameters
modelThe model the block is associated with
connectionsVector storing the connections between blocks
configThe JSON configuration
componentName of the component to retrieve from config
vessel_id_mapMap between vessel names and IDs

◆ generate_activation_function()

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().

Parameters
modelModel (for cardiac_cycle_period)
jJSON object (e.g. chamber_config["activation_function"])
chamber_nameChamber name for error messages
Returns
Unique pointer to the created activation function

◆ generate_active_stress()

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().

Parameters
jJSON object (e.g. chamber_config["active_stress"])
chamber_nameChamber name for error messages
Returns
Unique pointer to the created active stress model

◆ generate_block()

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.

Parameters
modelThe model that the block is added to
block_params_jsonThe JSON configuration containing the block parameter values
block_typeThe type of block
nameThe name of the block
internalIs this an internal block? This is relevant for the calibrator
periodicIs this block periodic with the cardiac cycle? This is relevant for coupling with external solvers
Returns
int The block count

◆ generate_material()

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().

Parameters
jJSON object (e.g. chamber_config["material"])
chamber_nameChamber name for error messages
Returns
Unique pointer to the created material

◆ load_initial_condition()

State load_initial_condition ( const nlohmann::json & config,
Model & model )

Load initial conditions from a JSON configuration.

Parameters
configThe JSON configuration
modelThe model
Returns
State Initial configuration for the model

◆ load_simulation_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.

Parameters
configThe json configuration
modelThe 0D model

◆ load_simulation_params()

SimulationParameters load_simulation_params ( const nlohmann::json & config)

Load the simulation parameters from a JSON configuration.

Parameters
configThe JSON configuration
Returns
SimulationParameters Simulation parameters read from configuration

◆ resolve_cardiac_cycle_period()

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.

Parameters
modelThe 0D model
paramsSimulation parameters read from the configuration

◆ validate_input()

void validate_input ( const nlohmann::json & config)

Check that the JSON configuration has the required inputs.

Parameters
configThe JSON configuration