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