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