svZeroDSolver
Loading...
Searching...
No Matches
Model.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 Model.h
5 * @brief model::Model source file
6 */
7
8#ifndef SVZERODSOLVER_MODEL_MODEL_HPP_
9#define SVZERODSOLVER_MODEL_MODEL_HPP_
10
11#include <algorithm>
12#include <list>
13#include <map>
14#include <memory>
15#include <string>
16#include <vector>
17
18#include "Block.h"
19#include "BlockFactory.h"
20#include "BloodVessel.h"
21#include "BloodVesselCRL.h"
22#include "BloodVesselJunction.h"
24#include "ChamberSphere.h"
28#include "ClosedLoopRCRBC.h"
29#include "DOFHandler.h"
30#include "FlowReferenceBC.h"
31#include "Junction.h"
32#include "Node.h"
33#include "OpenLoopCoronaryBC.h"
34#include "Parameter.h"
36#include "PiecewiseValve.h"
37#include "PressureReferenceBC.h"
38#include "ResistanceBC.h"
39#include "ResistiveJunction.h"
40#include "State.h"
41#include "ValveTanh.h"
42#include "WindkesselBC.h"
43#include "debug.h"
44
45/**
46 * @brief Model of 0D elements
47 *
48 * This class represents a full 0D model. It contains attributes and
49 * methods to store and modify 0D elements.
50 *
51 */
52class Model {
53 public:
54 /// Factory that holds all implemented blocks
55 std::map<std::string_view, BlockFactoryFunc> block_factory_map;
56
57 /**
58 * @brief Construct a new Model object
59 *
60 */
61 Model();
62
63 /**
64 * @brief Destroy the Model object
65 *
66 */
67 ~Model();
68
69 DOFHandler dofhandler; ///< Degree-of-freedom handler of the model
70
71 double cardiac_cycle_period = -1.0; ///< Cardiac cycle period
72 double time = 0.0; ///< Current time
73
74 /**
75 * @brief Create a new block
76 *
77 * @param block_name The block name (defined in block_factory_map)
78 * @return int Global ID of the block
79 */
80 Block* create_block(const std::string& block_name);
81
82 /**
83 * @brief Add a block to the model (without parameters)
84 *
85 * @param block The block to add
86 * @param name The name of the block
87 * @param block_param_ids Global IDs of the parameters of the block
88 * @param internal Toggle whether block is internal
89 * @return int Global ID of the block
90 */
91 int add_block(Block* block, const std::string_view& name,
92 const std::vector<int>& block_param_ids, bool internal = false);
93
94 /**
95 * @brief Add a block to the model (with parameters)
96 *
97 * @param block_name Type of the block
98 * @param block_param_ids Global IDs of the parameters of the block
99 * @param name The name of the block
100 * @param internal Toggle whether block is internal
101 * @return int Global ID of the block
102 */
103 int add_block(const std::string& block_name,
104 const std::vector<int>& block_param_ids,
105 const std::string_view& name, bool internal = false);
106
107 /**
108 * @brief Check if a block with given name exists
109 *
110 * @param name Name of the Block
111 * @return bool whether block exists
112 */
113 bool has_block(const std::string& name) const;
114
115 /**
116 * @brief Get a block by its name
117 *
118 * @param name Name of the Block
119 * @return Block* The block
120 */
121 Block* get_block(const std::string_view& name) const;
122
123 /**
124 * @brief Get a block by its global ID
125 *
126 * @param block_id Global ID of the Block
127 * @return Block* The block
128 */
129 Block* get_block(int block_id) const;
130
131 /**
132 * @brief Get a block type by its name
133 *
134 * @param name The name of the block
135 * @return BlockType The block type
136 */
137 BlockType get_block_type(const std::string_view& name) const;
138
139 /**
140 * @brief Get the name of a block by it's ID
141 *
142 * @param block_id Global ID of the block
143 * @return std::string Name of the block
144 */
145 std::string get_block_name(int block_id) const;
146
147 /**
148 * @brief Add a node to the model
149 *
150 * @param inlet_eles Inlet blocks of the node
151 * @param outlet_eles Outlet blocks of the node
152 * @param name Name of node
153 * @return int Global ID of the node
154 */
155 int add_node(const std::vector<Block*>& inlet_eles,
156 const std::vector<Block*>& outlet_eles,
157 const std::string_view& name);
158
159 /**
160 * @brief Get the name of a node by it's ID
161 *
162 * @param node_id Global ID of the node
163 * @return std::string Name of the node
164 */
165 std::string get_node_name(int node_id) const;
166
167 /**
168 * @brief Add a constant model parameter
169 *
170 * @param value Value of the parameter
171 * @return int Global ID of the parameter
172 */
173 int add_parameter(double value);
174
175 /**
176 * @brief Add a time-dependent model parameter
177 *
178 * @param times Times corresponding to the parameter values
179 * @param values Values of the parameter
180 * @param periodic Toggle whether parameter is periodic
181 * @return int Global ID of the parameter
182 */
183 int add_parameter(const std::vector<double>& times,
184 const std::vector<double>& values, bool periodic = true);
185
186 /**
187 * @brief Get a parameter by its global ID
188 *
189 * @param param_id Global ID of the parameter
190 * @return Parameter* The parameter
191 */
192 Parameter* get_parameter(int param_id);
193
194 /**
195 * @brief Get the current value of a parameter
196 *
197 * @param param_id Global ID of the parameter
198 * @return T Current value of the parameter
199 */
200 double get_parameter_value(int param_id) const;
201
202 /**
203 * @brief Update the current value of a parameter in the `parameter_values`
204 * vector. Note that this is different from updating the value within each
205 * parameter object, which is done in Parameter::update()
206 *
207 * @param param_id Global ID of the parameter
208 * @param param_value The new parameter value
209 */
210 void update_parameter_value(int param_id, double param_value);
211
212 /**
213 * @brief Finalize the model after all blocks, nodes and parameters have been
214 * added
215 *
216 */
217 void finalize();
218
219 /**
220 * @brief Update the constant contributions of all elements in a sparse system
221 *
222 * @param system System to update contributions at
223 */
224 void update_constant(SparseSystem& system);
225
226 /**
227 * @brief Update the time-dependent contributions of all elements in a sparse
228 * system
229 *
230 * @param system System to update contributions at
231 * @param time Current time
232 */
233 void update_time(SparseSystem& system, double time);
234
235 /**
236 * @brief Update the solution-dependent contributions of all elements in a
237 * sparse system
238 *
239 * @param system System to update contributions at
240 * @param y Current solution
241 * @param dy Current derivate of the solution
242 */
243 void update_solution(SparseSystem& system,
244 Eigen::Matrix<double, Eigen::Dynamic, 1>& y,
245 Eigen::Matrix<double, Eigen::Dynamic, 1>& dy);
246
247 /**
248 * @brief Modify the solution after solving it
249 *
250 * @param y Current solution
251 */
252 void post_solve(Eigen::Matrix<double, Eigen::Dynamic, 1>& y);
253
254 /**
255 * @brief Convert the blocks to a steady behavior
256 *
257 */
258 void to_steady();
259
260 /**
261 * @brief Convert the blocks to an unsteady behavior
262 *
263 */
264 void to_unsteady();
265
266 /**
267 * @brief Get number of triplets all elements
268 *
269 * Get the number of triplets the elements contribute to the global system
270 * (relevant for sparse memory reservation)
271 *
272 * @return Number of triplets that are used in each system matrix
273 */
274 // std::map<std::string, int> get_num_triplets();
276
277 /**
278 * @brief Get the number of blocks in the model
279 *
280 * @param internal Toggle whether to return internal/hidden blocks
281 *
282 * @return int Number of blocks
283 */
284 int get_num_blocks(bool internal = false) const;
285
286 /**
287 * @brief Specify if model has at least one Windkessel boundary condition
288 *
289 * @param has_windkessel Toggle if model has at least one Windkessel boundary
290 * condition
291 */
292 void update_has_windkessel_bc(bool has_windkessel);
293
294 /**
295 * @brief Update model with largest time constant among all Windkessel
296 * boundary conditions present in model
297 *
298 * @param time_constant Largest Windkessel time constant
299 */
300 void update_largest_windkessel_time_constant(double time_constant);
301
302 /**
303 * @brief Check if model has at least one Windkessel boundary condition
304 *
305 * @return bool True if model has at least one Windkessel boundary condition
306 */
308
309 /**
310 * @brief Get largest Windkessel time constant in model
311 *
312 * @return double Largest Windkessel time constant of model
313 */
315 /**
316 * @brief Setup model parameters that depend on the initial state
317 *
318 * @param initial_state The initial state vector
319 */
321
322 private:
323 int block_count = 0;
324 int node_count = 0;
325 int parameter_count = 0;
326 std::map<int, double> param_value_cache;
327
328 std::vector<std::shared_ptr<Block>> blocks; ///< Blocks of the model
329 std::vector<BlockType> block_types; ///< Types of the blocks
330 std::vector<std::string> block_names; ///< Names of the blocks
331 std::map<std::string, int>
332 block_index_map; ///< Map between block name and index
333
334 std::vector<std::shared_ptr<Block>>
335 hidden_blocks; ///< Hidden blocks of the model
336
337 std::vector<std::shared_ptr<Node>> nodes; ///< Nodes of the model
338 std::vector<std::string> node_names; ///< Names of the nodes
339
340 std::vector<Parameter>
341 parameters; ///< Parameters of the model. This vector stores the
342 ///< parameter objects and is primarily used to update
343 ///< `parameter_values` at each time-step for time-dependent
344 ///< parameters and also for steady initial conditions.
345 std::vector<double>
346 parameter_values; ///< Current values of the parameters. This is passed
347 ///< to blocks to set up the linear system in
348 ///< `update_constant`, `update_time` and
349 ///< `update_solution`.
350
351 bool has_windkessel_bc = false;
352 double largest_windkessel_time_constant = 0.0;
353};
354
355#endif // SVZERODSOLVER_MODEL_MODEL_HPP_
model::Block source file
Define the block factory functional.
BlockType
The types of blocks supported by the solver.
Definition BlockType.h:15
model::BloodVessel source file
model::BloodVesselCRL source file
model::BloodVesselJunction source file
model::ChamberElastanceInductor source file
model::ChamberSphere source file
Left side of ClosedLoopCoronaryBC.
Right side of ClosedLoopCoronaryBC.
model::ClosedLoopHeartPulmonary source file
model::ClosedLoopRCRBC source file
model::DOFHandler source file
model::FlowReferenceBC source file
model::Junction source file
model::Node source file
model::OpenLoopCoronaryBC source file
model::Parameter source file
model::PiecewiseCosineChamber source file
model::PiecewiseValve source file
model::PressureReferenceBC source file
model::ResistanceBC source file
model::ResistiveJunction source file
State source file.
model::ValveTanh source file
model::WindkesselBC source file
Base class for 0D model components.
Definition Block.h:75
Degree-of-freedom handler.
Definition DOFHandler.h:21
int add_node(const std::vector< Block * > &inlet_eles, const std::vector< Block * > &outlet_eles, const std::string_view &name)
Add a node to the model.
Definition Model.cpp:119
void finalize()
Finalize the model after all blocks, nodes and parameters have been added.
Definition Model.cpp:167
void update_parameter_value(int param_id, double param_value)
Update the current value of a parameter in the parameter_values vector. Note that this is different f...
Definition Model.cpp:163
double get_parameter_value(int param_id) const
Get the current value of a parameter.
Definition Model.cpp:159
void update_largest_windkessel_time_constant(double time_constant)
Update model with largest time constant among all Windkessel boundary conditions present in model.
Definition Model.cpp:277
void setup_initial_state_dependent_parameters(State initial_state)
Setup model parameters that depend on the initial state.
Definition Model.cpp:265
void update_time(SparseSystem &system, double time)
Update the time-dependent contributions of all elements in a sparse system.
Definition Model.cpp:198
std::string get_node_name(int node_id) const
Get the name of a node by it's ID.
Definition Model.cpp:131
void update_constant(SparseSystem &system)
Update the constant contributions of all elements in a sparse system.
Definition Model.cpp:192
BlockType get_block_type(const std::string_view &name) const
Get a block type by its name.
Definition Model.cpp:105
double get_largest_windkessel_time_constant()
Get largest Windkessel time constant in model.
Definition Model.cpp:283
void update_has_windkessel_bc(bool has_windkessel)
Specify if model has at least one Windkessel boundary condition.
Definition Model.cpp:273
int add_block(Block *block, const std::string_view &name, const std::vector< int > &block_param_ids, bool internal=false)
Add a block to the model (without parameters)
Definition Model.cpp:49
void to_unsteady()
Convert the blocks to an unsteady behavior.
Definition Model.cpp:242
int add_parameter(double value)
Add a constant model parameter.
Definition Model.cpp:135
double cardiac_cycle_period
Cardiac cycle period.
Definition Model.h:71
Block * get_block(const std::string_view &name) const
Get a block by its name.
Definition Model.cpp:87
void post_solve(Eigen::Matrix< double, Eigen::Dynamic, 1 > &y)
Modify the solution after solving it.
Definition Model.cpp:218
TripletsContributions get_num_triplets() const
Get number of triplets all elements.
Definition Model.cpp:255
bool has_block(const std::string &name) const
Check if a block with given name exists.
Definition Model.cpp:79
double time
Current time.
Definition Model.h:72
void to_steady()
Convert the blocks to a steady behavior.
Definition Model.cpp:224
Block * create_block(const std::string &block_name)
Create a new block.
Definition Model.cpp:39
~Model()
Destroy the Model object.
Definition Model.cpp:37
Model()
Construct a new Model object.
Definition Model.cpp:12
void update_solution(SparseSystem &system, Eigen::Matrix< double, Eigen::Dynamic, 1 > &y, Eigen::Matrix< double, Eigen::Dynamic, 1 > &dy)
Update the solution-dependent contributions of all elements in a sparse system.
Definition Model.cpp:210
std::map< std::string_view, BlockFactoryFunc > block_factory_map
Factory that holds all implemented blocks.
Definition Model.h:55
std::string get_block_name(int block_id) const
Get the name of a block by it's ID.
Definition Model.cpp:115
int get_num_blocks(bool internal=false) const
Get the number of blocks in the model.
Definition Model.cpp:182
Parameter * get_parameter(int param_id)
Get a parameter by its global ID.
Definition Model.cpp:157
bool get_has_windkessel_bc()
Check if model has at least one Windkessel boundary condition.
Definition Model.cpp:281
DOFHandler dofhandler
Degree-of-freedom handler of the model.
Definition Model.h:69
Model Parameter.
Definition Parameter.h:25
Sparse system.
Definition SparseSystem.h:30
State of the system.
Definition State.h:19
DEBUG_MSG source file.
The number of triplets that the element contributes to the global system.
Definition Block.h:26