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 Reconcile the cardiac cycle period between the model and parameters
196 *
197 * The period may be defined by a model block (e.g. a closed-loop block or a
198 * chamber) or by `simulation_parameters.cardiac_period`. This resolves
199 * `model.cardiac_cycle_period` from both sources, throws if the two disagree,
200 * and falls back to a default of 1.0 if neither defines it. Must be called by
201 * every entry point (solver and coupled interface) before blocks that depend
202 * on the period are used.
203 *
204 * @param model The 0D model
205 * @param params Simulation parameters read from the configuration
206 */
207void resolve_cardiac_cycle_period(Model& model,
208 const SimulationParameters& params);
209
210/**
211 * @brief Load the 0D block in the model from a configuration
212 *
213 * @param config The json configuration
214 * @param model The 0D model
215 * @
216 */
217void load_simulation_model(const nlohmann::json& config, Model& model);
218
219/**
220 * @brief Check that the JSON configuration has the required inputs
221 *
222 * @param config The JSON configuration
223 */
224void validate_input(const nlohmann::json& config);
225
226/**
227 * @brief Handle the creation of vessel blocks and connections with boundary
228 * conditions
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 */
236void create_vessels(
237 Model& model,
238 std::vector<std::tuple<std::string, std::string>>& connections,
239 const nlohmann::json& config, const std::string& component,
240 std::map<int, std::string>& vessel_id_map);
241
242/**
243 * @brief Handle the creation of external coupling blocks and connections with
244 * other blocks
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 * @param bc_type_map Map between boundary condition names and their types
252 */
253void create_external_coupling(
254 Model& model,
255 std::vector<std::tuple<std::string, std::string>>& connections,
256 const nlohmann::json& config, const std::string& component,
257 std::map<int, std::string>& vessel_id_map,
258 std::map<std::string, std::string>& bc_type_map);
259
260/**
261 * @brief Handle the creation of boundary condition blocks
262 *
263 * @param model The model the block is associated with
264 * @param config The JSON configuration
265 * @param component Name of the component to retrieve from config
266 * @param bc_type_map Map between boundary condition names and their types
267 * @param closed_loop_bcs List of boundary conditions that should be connected
268 * to a closed loop heart block
269 */
270void create_boundary_conditions(Model& model, const nlohmann::json& config,
271 const std::string& component,
272 std::map<std::string, std::string>& bc_type_map,
273 std::vector<std::string>& closed_loop_bcs);
274
275/**
276 * @brief Handle the creation of junctions and their connections
277 *
278 * @param model The model the block is associated with
279 * @param connections Vector storing the connections between blocks
280 * @param config The JSON configuration
281 * @param component Name of the component to retrieve from config
282 * @param vessel_id_map Map between vessel names and IDs
283 */
284void create_junctions(
285 Model& model,
286 std::vector<std::tuple<std::string, std::string>>& connections,
287 const nlohmann::json& config, const std::string& component,
288 std::map<int, std::string>& vessel_id_map);
289
290/**
291 * @brief Handle the creation of closed-loop blocks and 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 * @param closed_loop_bcs List of boundary conditions that should be connected
298 * to a closed loop heart block
299 */
300void create_closed_loop(
301 Model& model,
302 std::vector<std::tuple<std::string, std::string>>& connections,
303 const nlohmann::json& config, const std::string& component,
304 std::vector<std::string>& closed_loop_bcs);
305
306/**
307 * @brief Handle the creation of valves and their associated connections
308 *
309 * @param model The model the block is associated with
310 * @param connections Vector storing the connections between blocks
311 * @param config The JSON configuration
312 * @param component Name of the component to retrieve from config
313 */
314void create_valves(
315 Model& model,
316 std::vector<std::tuple<std::string, std::string>>& connections,
317 const nlohmann::json& config, const std::string& component);
318
319/**
320 * @brief Handle the creation of chambers
321 *
322 * @param model The model the block is associated with
323 * @param connections Vector storing the connections between blocks
324 * @param config The JSON configuration containing all the closed loop blocks
325 * @param component Name of the component to retrieve from config
326 */
327void create_chambers(
328 Model& model,
329 std::vector<std::tuple<std::string, std::string>>& connections,
330 const nlohmann::json& config, const std::string& component);
331
332#endif
Activation function classes for cardiac chamber models.
model::Model source file
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:56
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