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