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