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