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