svZeroDSolver
Loading...
Searching...
No Matches
SimulationParameters.h
Go to the documentation of this file.
1// Copyright (c) Stanford University, The Regents of the University of
2// California, and others.
3//
4// All Rights Reserved.
5//
6// See Copyright-SimVascular.txt for additional details.
7//
8// Permission is hereby granted, free of charge, to any person obtaining
9// a copy of this software and associated documentation files (the
10// "Software"), to deal in the Software without restriction, including
11// without limitation the rights to use, copy, modify, merge, publish,
12// distribute, sublicense, and/or sell copies of the Software, and to
13// permit persons to whom the Software is furnished to do so, subject
14// to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included
17// in all copies or substantial portions of the Software.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30/**
31 * @file SimulationParameters.h
32 * @brief Source file to read simulation configuration
33 */
34#ifndef SVZERODSOLVER_SIMULATIONPARAMETERS_HPP_
35#define SVZERODSOLVER_SIMULATIONPARAMETERS_HPP_
36
37#include <list>
38#include <nlohmann/json.hpp>
39#include <stdexcept>
40#include <string>
41
42#include "Model.h"
43#include "State.h"
44#include "debug.h"
45
46/**
47 * @brief Simulation parameters
48 *
49 */
51 // Negative value indicates this has not
52 // been read from config file yet.
53 double sim_time_step_size{0.0}; ///< Simulation time step size
54 double sim_abs_tol{0.0}; ///< Absolute tolerance for simulation
55
56 int sim_num_cycles{0}; ///< Number of cardiac cycles to simulate
57 int sim_pts_per_cycle{0}; ///< Number of time steps per cardiac cycle
59 false}; ///< If model does not have RCR boundary conditions, simulate
60 ///< model to convergence (based on cycle-to-cycle error of last
61 ///< two cardiac cycles); if it does, update number of cardiac
62 ///< cycles to simulate to be value estimated from equation 21 of
63 ///< Pfaller 2021
64 double sim_cycle_to_cycle_error{0}; ///< Cycle-to-cycle error
65 int sim_num_time_steps{0}; ///< Total number of time steps
66 int sim_nliter{0}; ///< Maximum number of non-linear iterations in time
67 ///< integration
68 double sim_rho_infty{0.0}; ///< Spectral radius of generalized-alpha
69 int output_interval{0}; ///< Interval of writing output
70
71 bool sim_steady_initial{0}; ///< Start from steady solution
73 false}; ///< Output variable based instead of vessel based
74 bool output_mean_only{false}; ///< Output only the mean value
75 bool output_derivative{false}; ///< Output derivatives
76 bool output_all_cycles{false}; ///< Output all cardiac cycles
77
79 false}; ///< Running 0D simulation coupled with external solver
80 double sim_external_step_size{0.0}; ///< Step size of external solver if
81 ///< running coupled
82};
83
84/// @brief Wrapper class for nlohmann:json with error checking
85class JsonWrapper : public nlohmann::json {
86 public:
87 /**
88 * @brief Wrap around JSON configuration with detailed error message in case
89 * key is not found in configuration
90 *
91 * @param json JSON configuration
92 * @param component Name of the JSON sub-list to be extracted
93 * @param name_str Name string of the JSON sub-list to be extracted
94 * @param id Index of JSON sub-list to be extracted
95 */
96 JsonWrapper(const nlohmann::json& json, const std::string& component,
97 const std::string& name_str, const int& id)
98 : nlohmann::json(json[component][id]),
99 component(component),
100 name_str(name_str),
101 block_id(id) {}
102
103 /**
104 * @brief Wrap error check around key retrieval (throws detailed error if key
105 * doesn't exist)
106 *
107 * @param key Key to retrieve from JSON object
108 * @return JSON entry of key
109 */
110 const nlohmann::json& operator[](const char* key) const {
111 if (!this->contains(key)) {
112 if (this->contains(name_str)) {
113 const std::string name = this->at(name_str);
114 throw std::runtime_error("Key " + std::string(key) +
115 " not found in element " + name +
116 " of component " + component);
117 } else {
118 throw std::runtime_error(
119 "Key " + std::string(key) + " not found in element number " +
120 std::to_string(block_id) + " of component " + component);
121 }
122 }
123 return this->at(key);
124 }
125
126 // Inherit functions
127 using nlohmann::json::contains;
128 using nlohmann::json::value;
129 using nlohmann::json::operator[];
130
131 private:
132 std::string component;
133 std::string name_str;
134 int block_id;
135};
136
137/**
138 * @brief Generate a new block and add its parameters to the model
139 *
140 * @param model The model that the block is added to
141 * @param block_params_json The JSON configuration containing the block
142 * parameter values
143 * @param block_type The type of block
144 * @param name The name of the block
145 * @param internal Is this an internal block? This is relevant for the
146 * calibrator
147 * @param periodic Is this block periodic with the cardiac cycle? This is
148 * relevant for coupling with external solvers
149 * @return int The block count
150 */
151int generate_block(Model& model, const nlohmann::json& block_params_json,
152 const std::string& block_type, const std::string_view& name,
153 bool internal = false, bool periodic = true);
154
155/**
156 * @brief Load initial conditions from a JSON configuration
157 *
158 * @param config The JSON configuration
159 * @param model The model
160 * @return State Initial configuration for the model
161 */
162State load_initial_condition(const nlohmann::json& config, Model& model);
163
164/**
165 * @brief Load the simulation parameters from a JSON configuration
166 *
167 * @param config The JSON configuration
168 * @return SimulationParameters Simulation parameters read from configuration
169 */
170SimulationParameters load_simulation_params(const nlohmann::json& config);
171
172/**
173 * @brief Load the 0D block in the model from a configuration
174 *
175 * @param config The json configuration
176 * @param model The 0D model
177 * @
178 */
179void load_simulation_model(const nlohmann::json& config, Model& model);
180
181/**
182 * @brief Check that the JSON configuration has the required inputs
183 *
184 * @param config The JSON configuration
185 */
186void validate_input(const nlohmann::json& config);
187
188/**
189 * @brief Handle the creation of vessel blocks and connections with boundary
190 * conditions
191 *
192 * @param model The model the block is associated with
193 * @param connections Vector storing the connections between blocks
194 * @param config The JSON configuration
195 * @param component Name of the component to retrieve from config
196 * @param vessel_id_map Map between vessel names and IDs
197 */
198void create_vessels(
199 Model& model,
200 std::vector<std::tuple<std::string, std::string>>& connections,
201 const nlohmann::json& config, const std::string& component,
202 std::map<int, std::string>& vessel_id_map);
203
204/**
205 * @brief Handle the creation of external coupling blocks and connections with
206 * other blocks
207 *
208 * @param model The model the block is associated with
209 * @param connections Vector storing the connections between blocks
210 * @param config The JSON configuration
211 * @param component Name of the component to retrieve from config
212 * @param vessel_id_map Map between vessel names and IDs
213 * @param bc_type_map Map between boundary condition names and their types
214 */
216 Model& model,
217 std::vector<std::tuple<std::string, std::string>>& connections,
218 const nlohmann::json& config, const std::string& component,
219 std::map<int, std::string>& vessel_id_map,
220 std::map<std::string, std::string>& bc_type_map);
221
222/**
223 * @brief Handle the creation of boundary condition blocks
224 *
225 * @param model The model the block is associated with
226 * @param config The JSON configuration
227 * @param component Name of the component to retrieve from config
228 * @param bc_type_map Map between boundary condition names and their types
229 * @param closed_loop_bcs List of boundary conditions that should be connected
230 * to a closed loop heart block
231 */
232void create_boundary_conditions(Model& model, const nlohmann::json& config,
233 const std::string& component,
234 std::map<std::string, std::string>& bc_type_map,
235 std::vector<std::string>& closed_loop_bcs);
236
237/**
238 * @brief Handle the creation of junctions and their connections
239 *
240 * @param model The model the block is associated with
241 * @param connections Vector storing the connections between blocks
242 * @param config The JSON configuration
243 * @param component Name of the component to retrieve from config
244 * @param vessel_id_map Map between vessel names and IDs
245 */
247 Model& model,
248 std::vector<std::tuple<std::string, std::string>>& connections,
249 const nlohmann::json& config, const std::string& component,
250 std::map<int, std::string>& vessel_id_map);
251
252/**
253 * @brief Handle the creation of closed-loop blocks and associated connections
254 *
255 * @param model The model the block is associated with
256 * @param connections Vector storing the connections between blocks
257 * @param config The JSON configuration
258 * @param component Name of the component to retrieve from config
259 * @param closed_loop_bcs List of boundary conditions that should be connected
260 * to a closed loop heart block
261 */
263 Model& model,
264 std::vector<std::tuple<std::string, std::string>>& connections,
265 const nlohmann::json& config, const std::string& component,
266 std::vector<std::string>& closed_loop_bcs);
267
268/**
269 * @brief Handle the creation of valves and their associated connections
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
274 * @param component Name of the component to retrieve from config
275 */
276void create_valves(
277 Model& model,
278 std::vector<std::tuple<std::string, std::string>>& connections,
279 const nlohmann::json& config, const std::string& component);
280
281/**
282 * @brief Handle the creation of chambers
283 *
284 * @param model The model the block is associated with
285 * @param connections Vector storing the connections between blocks
286 * @param config The JSON configuration containing all the closed loop blocks
287 * @param component Name of the component to retrieve from config
288 */
289void create_chambers(
290 Model& model,
291 std::vector<std::tuple<std::string, std::string>>& connections,
292 const nlohmann::json& config, const std::string& component);
293
294#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:567
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:451
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:331
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:549
State load_initial_condition(const nlohmann::json &config, Model &model)
Load initial conditions from a JSON configuration.
Definition SimulationParameters.cpp:580
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:296
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:493
void load_simulation_model(const nlohmann::json &config, Model &model)
Load the 0D block in the model from a configuration.
Definition SimulationParameters.cpp:222
SimulationParameters load_simulation_params(const nlohmann::json &config)
Load the simulation parameters from a JSON configuration.
Definition SimulationParameters.cpp:179
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:72
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:366
void validate_input(const nlohmann::json &config)
Check that the JSON configuration has the required inputs.
Definition SimulationParameters.cpp:170
State source file.
Wrapper class for nlohmann:json with error checking.
Definition SimulationParameters.h:85
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:110
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:96
Model of 0D elements.
Definition Model.h:75
State of the system.
Definition State.h:46
DEBUG_MSG source file.
Simulation parameters.
Definition SimulationParameters.h:50
bool sim_coupled
Running 0D simulation coupled with external solver.
Definition SimulationParameters.h:78
int sim_nliter
Definition SimulationParameters.h:66
bool output_all_cycles
Output all cardiac cycles.
Definition SimulationParameters.h:76
int sim_pts_per_cycle
Number of time steps per cardiac cycle.
Definition SimulationParameters.h:57
int sim_num_time_steps
Total number of time steps.
Definition SimulationParameters.h:65
double sim_external_step_size
Definition SimulationParameters.h:80
double sim_time_step_size
Simulation time step size.
Definition SimulationParameters.h:53
int sim_num_cycles
Number of cardiac cycles to simulate.
Definition SimulationParameters.h:56
bool output_mean_only
Output only the mean value.
Definition SimulationParameters.h:74
bool sim_steady_initial
Start from steady solution.
Definition SimulationParameters.h:71
int output_interval
Interval of writing output.
Definition SimulationParameters.h:69
bool output_variable_based
Output variable based instead of vessel based.
Definition SimulationParameters.h:72
double sim_abs_tol
Absolute tolerance for simulation.
Definition SimulationParameters.h:54
bool output_derivative
Output derivatives.
Definition SimulationParameters.h:75
double sim_cycle_to_cycle_error
Cycle-to-cycle error.
Definition SimulationParameters.h:64
bool use_cycle_to_cycle_error
Definition SimulationParameters.h:58
double sim_rho_infty
Spectral radius of generalized-alpha.
Definition SimulationParameters.h:68