svZeroDSolver
Loading...
Searching...
No Matches
SimulationParameters.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the
2// University of California, and others. SPDX-License-Identifier: BSD-3-Clause
3/**
4 * @file SimulationParameters.h
5 * @brief Source file to read simulation configuration
6 */
7#ifndef SVZERODSOLVER_SIMULATIONPARAMETERS_HPP_
8#define SVZERODSOLVER_SIMULATIONPARAMETERS_HPP_
9
10#include <list>
11#include <nlohmann/json.hpp>
12#include <stdexcept>
13#include <string>
14
15#include "ActivationFunction.h"
16#include "Model.h"
17#include "State.h"
18#include "debug.h"
19
20/**
21 * @brief Simulation parameters
22 *
23 * Each of these can be set from the .json configuration file as part of the
24 * dict which is keyed by "simulation_parameters".
25 *
26 * For example:
27 *
28 * "simulation_parameters": {
29 * "number_of_cardiac_cycles": 30,
30 * "number_of_time_pts_per_cardiac_cycle": 201,
31 * "output_all_cycles": true
32 * }
33 *
34 * each parameter in the documentation may be specified using the attribute name
35 * and a value which takes the type of the attribute.
36 *
37 */
39 // Negative value indicates this has not
40 // been read from config file yet.
41 double sim_time_step_size{0.0}; ///< Simulation time step size
42 double sim_abs_tol{0.0}; ///< Absolute tolerance for simulation
43 double sim_cardiac_period{-1.0}; ///< Cardiac period
44 int sim_num_cycles{0}; ///< Number of cardiac cycles to simulate
45 int sim_pts_per_cycle{0}; ///< Number of time steps per cardiac cycle
47 false}; ///< If model does not have RCR boundary conditions, simulate
48 ///< model to convergence (based on cycle-to-cycle error of last
49 ///< two cardiac cycles); if it does, update number of cardiac
50 ///< cycles to simulate to be value estimated from equation 21 of
51 ///< Pfaller 2021
52 double sim_cycle_to_cycle_error{0}; ///< Cycle-to-cycle error
53 int sim_num_time_steps{0}; ///< Total number of time steps
54 int sim_nliter{0}; ///< Maximum number of non-linear iterations in time
55 ///< integration
56 double sim_rho_infty{0.0}; ///< Spectral radius of generalized-alpha
57 int output_interval{0}; ///< Interval of writing output
58
59 bool sim_steady_initial{0}; ///< Start from steady solution
61 false}; ///< Output variable based instead of vessel based
62 bool output_mean_only{false}; ///< Output only the mean value
63 bool output_derivative{false}; ///< Output derivatives
64 bool output_all_cycles{false}; ///< Output all cardiac cycles
65
67 false}; ///< Running 0D simulation coupled with external solver
68 double sim_external_step_size{0.0}; ///< Step size of external solver if
69 ///< running coupled
71 false}; ///< If true, warn instead of throwing when max nonlinear
72 ///< iterations is reached
73};
74
75/// @brief Wrapper class for nlohmann:json with error checking
76class JsonWrapper : public nlohmann::json {
77 public:
78 /**
79 * @brief Wrap around JSON configuration with detailed error message in case
80 * key is not found in configuration
81 *
82 * @param json JSON configuration
83 * @param component Name of the JSON sub-list to be extracted
84 * @param name_str Name string of the JSON sub-list to be extracted
85 * @param id Index of JSON sub-list to be extracted
86 */
87 JsonWrapper(const nlohmann::json& json, const std::string& component,
88 const std::string& name_str, const int& id)
89 : nlohmann::json(json[component][id]),
90 component(component),
91 name_str(name_str),
92 block_id(id) {}
93
94 /**
95 * @brief Wrap error check around key retrieval (throws detailed error if key
96 * doesn't exist)
97 *
98 * @param key Key to retrieve from JSON object
99 * @return JSON entry of key
100 */
101 const nlohmann::json& operator[](const char* key) const {
102 if (!this->contains(key)) {
103 if (this->contains(name_str)) {
104 const std::string name = this->at(name_str);
105 throw std::runtime_error("Key " + std::string(key) +
106 " not found in element " + name +
107 " of component " + component);
108 } else {
109 throw std::runtime_error(
110 "Key " + std::string(key) + " not found in element number " +
111 std::to_string(block_id) + " of component " + component);
112 }
113 }
114 return this->at(key);
115 }
116
117 // Inherit functions
118 using nlohmann::json::contains;
119 using nlohmann::json::value;
120 using nlohmann::json::operator[];
121
122 private:
123 std::string component;
124 std::string name_str;
125 int block_id;
126};
127
128/**
129 * @brief Generate a new block and add its parameters to the model
130 *
131 * @param model The model that the block is added to
132 * @param block_params_json The JSON configuration containing the block
133 * parameter values
134 * @param block_type The type of block
135 * @param name The name of the block
136 * @param internal Is this an internal block? This is relevant for the
137 * calibrator
138 * @param periodic Is this block periodic with the cardiac cycle? This is
139 * relevant for coupling with external solvers
140 * @return int The block count
141 */
142int generate_block(Model& model, const nlohmann::json& block_params_json,
143 const std::string& block_type, const std::string_view& name,
144 bool internal = false, bool periodic = true);
145
146/**
147 * @brief Create an activation function from JSON (analogous to generate_block)
148 *
149 * Validates keys against the activation type's params, reads scalar
150 * parameters, and returns a created ActivationFunction. Caller associates
151 * it with a chamber block via set_activation_function().
152 *
153 * @param model Model (for cardiac_cycle_period)
154 * @param j JSON object (e.g. chamber_config["activation_function"])
155 * @param chamber_name Chamber name for error messages
156 * @return Unique pointer to the created activation function
157 */
158std::unique_ptr<ActivationFunction> generate_activation_function(
159 Model& model, const nlohmann::json& j, const std::string& chamber_name);
160
161/**
162 * @brief Load initial conditions from a JSON configuration
163 *
164 * @param config The JSON configuration
165 * @param model The model
166 * @return State Initial configuration for the model
167 */
168State load_initial_condition(const nlohmann::json& config, Model& model);
169
170/**
171 * @brief Load the simulation parameters from a JSON configuration
172 *
173 * @param config The JSON configuration
174 * @return SimulationParameters Simulation parameters read from configuration
175 */
176SimulationParameters load_simulation_params(const nlohmann::json& config);
177
178/**
179 * @brief Load the 0D block in the model from a configuration
180 *
181 * @param config The json configuration
182 * @param model The 0D model
183 * @
184 */
185void load_simulation_model(const nlohmann::json& config, Model& model);
186
187/**
188 * @brief Check that the JSON configuration has the required inputs
189 *
190 * @param config The JSON configuration
191 */
192void validate_input(const nlohmann::json& config);
193
194/**
195 * @brief Handle the creation of vessel blocks and connections with boundary
196 * conditions
197 *
198 * @param model The model the block is associated with
199 * @param connections Vector storing the connections between blocks
200 * @param config The JSON configuration
201 * @param component Name of the component to retrieve from config
202 * @param vessel_id_map Map between vessel names and IDs
203 */
204void create_vessels(
205 Model& model,
206 std::vector<std::tuple<std::string, std::string>>& connections,
207 const nlohmann::json& config, const std::string& component,
208 std::map<int, std::string>& vessel_id_map);
209
210/**
211 * @brief Handle the creation of external coupling blocks and connections with
212 * other blocks
213 *
214 * @param model The model the block is associated with
215 * @param connections Vector storing the connections between blocks
216 * @param config The JSON configuration
217 * @param component Name of the component to retrieve from config
218 * @param vessel_id_map Map between vessel names and IDs
219 * @param bc_type_map Map between boundary condition names and their types
220 */
222 Model& model,
223 std::vector<std::tuple<std::string, std::string>>& connections,
224 const nlohmann::json& config, const std::string& component,
225 std::map<int, std::string>& vessel_id_map,
226 std::map<std::string, std::string>& bc_type_map);
227
228/**
229 * @brief Handle the creation of boundary condition blocks
230 *
231 * @param model The model the block is associated with
232 * @param config The JSON configuration
233 * @param component Name of the component to retrieve from config
234 * @param bc_type_map Map between boundary condition names and their types
235 * @param closed_loop_bcs List of boundary conditions that should be connected
236 * to a closed loop heart block
237 */
238void create_boundary_conditions(Model& model, const nlohmann::json& config,
239 const std::string& component,
240 std::map<std::string, std::string>& bc_type_map,
241 std::vector<std::string>& closed_loop_bcs);
242
243/**
244 * @brief Handle the creation of junctions and their connections
245 *
246 * @param model The model the block is associated with
247 * @param connections Vector storing the connections between blocks
248 * @param config The JSON configuration
249 * @param component Name of the component to retrieve from config
250 * @param vessel_id_map Map between vessel names and IDs
251 */
253 Model& model,
254 std::vector<std::tuple<std::string, std::string>>& connections,
255 const nlohmann::json& config, const std::string& component,
256 std::map<int, std::string>& vessel_id_map);
257
258/**
259 * @brief Handle the creation of closed-loop blocks and associated connections
260 *
261 * @param model The model the block is associated with
262 * @param connections Vector storing the connections between blocks
263 * @param config The JSON configuration
264 * @param component Name of the component to retrieve from config
265 * @param closed_loop_bcs List of boundary conditions that should be connected
266 * to a closed loop heart block
267 */
269 Model& model,
270 std::vector<std::tuple<std::string, std::string>>& connections,
271 const nlohmann::json& config, const std::string& component,
272 std::vector<std::string>& closed_loop_bcs);
273
274/**
275 * @brief Handle the creation of valves and their associated connections
276 *
277 * @param model The model the block is associated with
278 * @param connections Vector storing the connections between blocks
279 * @param config The JSON configuration
280 * @param component Name of the component to retrieve from config
281 */
282void create_valves(
283 Model& model,
284 std::vector<std::tuple<std::string, std::string>>& connections,
285 const nlohmann::json& config, const std::string& component);
286
287/**
288 * @brief Handle the creation of chambers
289 *
290 * @param model The model the block is associated with
291 * @param connections Vector storing the connections between blocks
292 * @param config The JSON configuration containing all the closed loop blocks
293 * @param component Name of the component to retrieve from config
294 */
295void create_chambers(
296 Model& model,
297 std::vector<std::tuple<std::string, std::string>>& connections,
298 const nlohmann::json& config, const std::string& component);
299
300#endif
Activation function classes for cardiac chamber models.
model::Model source file
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.
Definition SimulationParameters.cpp:600
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.
Definition SimulationParameters.cpp:484
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.
Definition SimulationParameters.cpp:364
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.
Definition SimulationParameters.cpp:582
State load_initial_condition(const nlohmann::json &config, Model &model)
Load initial conditions from a JSON configuration.
Definition SimulationParameters.cpp:634
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.
Definition SimulationParameters.cpp:329
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.
Definition SimulationParameters.cpp:526
void load_simulation_model(const nlohmann::json &config, Model &model)
Load the 0D block in the model from a configuration.
Definition SimulationParameters.cpp:255
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).
Definition SimulationParameters.cpp:142
SimulationParameters load_simulation_params(const nlohmann::json &config)
Load the simulation parameters from a JSON configuration.
Definition SimulationParameters.cpp:210
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.
Definition SimulationParameters.cpp:44
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.
Definition SimulationParameters.cpp:399
void validate_input(const nlohmann::json &config)
Check that the JSON configuration has the required inputs.
Definition SimulationParameters.cpp:201
State source file.
const nlohmann::json & operator[](const char *key) const
Wrap error check around key retrieval (throws detailed error if key doesn't exist).
Definition SimulationParameters.h:101
JsonWrapper(const nlohmann::json &json, const std::string &component, const std::string &name_str, const int &id)
Wrap around JSON configuration with detailed error message in case key is not found in configuration.
Definition SimulationParameters.h:87
Model of 0D elements.
Definition Model.h:55
State of the system.
Definition State.h:19
DEBUG_MSG source file.
Simulation parameters.
Definition SimulationParameters.h:38
bool sim_coupled
Running 0D simulation coupled with external solver.
Definition SimulationParameters.h:66
int sim_nliter
Definition SimulationParameters.h:54
bool output_all_cycles
Output all cardiac cycles.
Definition SimulationParameters.h:64
int sim_pts_per_cycle
Number of time steps per cardiac cycle.
Definition SimulationParameters.h:45
int sim_num_time_steps
Total number of time steps.
Definition SimulationParameters.h:53
double sim_external_step_size
Definition SimulationParameters.h:68
double sim_time_step_size
Simulation time step size.
Definition SimulationParameters.h:41
int sim_num_cycles
Number of cardiac cycles to simulate.
Definition SimulationParameters.h:44
bool output_mean_only
Output only the mean value.
Definition SimulationParameters.h:62
bool sim_max_iter_error_to_warning
Definition SimulationParameters.h:70
bool sim_steady_initial
Start from steady solution.
Definition SimulationParameters.h:59
int output_interval
Interval of writing output.
Definition SimulationParameters.h:57
bool output_variable_based
Output variable based instead of vessel based.
Definition SimulationParameters.h:60
double sim_abs_tol
Absolute tolerance for simulation.
Definition SimulationParameters.h:42
double sim_cardiac_period
Cardiac period.
Definition SimulationParameters.h:43
bool output_derivative
Output derivatives.
Definition SimulationParameters.h:63
double sim_cycle_to_cycle_error
Cycle-to-cycle error.
Definition SimulationParameters.h:52
bool use_cycle_to_cycle_error
Definition SimulationParameters.h:46
double sim_rho_infty
Spectral radius of generalized-alpha.
Definition SimulationParameters.h:56