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