svZeroDSolver
Loading...
Searching...
No Matches
Block.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/**
5 * @file Block.h
6 * @brief model::Block source file
7 */
8#ifndef SVZERODSOLVER_MODEL_BLOCK_HPP_
9#define SVZERODSOLVER_MODEL_BLOCK_HPP_
10
11#include <Eigen/Core>
12#include <list>
13#include <map>
14#include <vector>
15
16#include "ActivationFunction.h"
17#include "BlockType.h"
18#include "SphereMaterial.h"
19#include "DOFHandler.h"
20#include "Parameter.h"
21#include "SparseSystem.h"
22#include "State.h"
23
24/**
25 * @brief The number of triplets that the element contributes
26 * to the global system.
27 */
28struct TripletsContributions {
29 TripletsContributions(){};
30 /**
31 * @brief Set the number of triplets that the element contributes
32 * to the global system.
33 * @param F Contributions to F matrix
34 * @param E Contributions to E matrix
35 * @param D Contributions to dC/dy matrix
36 */
37 TripletsContributions(int F, int E, int D) : F(F), E(E), D(D){};
38 /**
39 * @brief Set the number of triplets that the element contributes
40 * to the global system.
41 * @param other TripletsContributions object to add to the
42 * number of contributions
43 * @return The number of triplets
44 */
45 TripletsContributions operator+=(const TripletsContributions& other) {
46 F += other.F;
47 E += other.E;
48 D += other.D;
49 return *this;
50 };
51
52 /**
53 * @brief Contributions to F matrix
54 */
55 int F{0};
56 /**
57 * @brief Contributions to E matrix
58 */
59 int E{0};
60 /**
61 * @brief Contributions to dC/dy matrix
62 */
63 int D{0};
64};
65
66class Node;
67class Model;
68
69/**
70 * @brief Base class for 0D model components.
71 *
72 * A Block is the base class of 0D model elements. It is the place
73 * where the contribution of an element to the global system is controlled.
74 * It defines all relevant attributes and methods of an element and a few
75 * common helpers for setting it up.
76 */
77class Block {
78 public:
79 const int id; ///< Global ID of the block
80 const Model* model; ///< The model to which the block belongs
81 const BlockType block_type; ///< Type of this block
82 const BlockClass block_class; ///< Class of this block
83 VesselType vessel_type = VesselType::neither; ///< Vessel type of this block
84 const std::vector<std::pair<std::string, InputParameter>>
85 input_params; ///< Map from name to input parameter
86
87 std::vector<Node*> inlet_nodes; ///< Inlet nodes
88 std::vector<Node*> outlet_nodes; ///< Outlet nodes
89
90 bool steady = false; ///< Toggle steady behavior
91 bool input_params_list = false; ///< Are input parameters given as a list?
92
93 /**
94 * @brief Construct a new Block object
95 *
96 * @param id Global ID of the block
97 * @param model The model to which the block belongs
98 * @param block_type The specific type of block
99 * @param block_class The class the block belongs to (e.g. vessel, junction)
100 * @param input_params The parameters the block takes from the input file
101 */
103 std::vector<std::pair<std::string, InputParameter>> input_params)
104 : id(id),
105 model(model),
109
110 /**
111 * @brief Destroy the Block object
112 *
113 */
114 ~Block();
115
116 /**
117 * @brief Copy the Block object
118 *
119 */
120 Block(const Block&) = delete;
121 /**
122 * @brief Global IDs for the block parameters.
123 *
124 */
125 std::vector<int> global_param_ids; ///< IDs of the parameters
126
127 /**
128 * @brief Global variable indices of the local element contributions
129 *
130 * Determines where the local element contributions are written to in
131 * the global system during assembly. The order of indices is:
132 *
133 * \f[
134 * [P_{in}^1, Q_{in}^1, \dots, P_{in}^n, Q_{in}^n, P_{out}^1, Q_{out}^1,
135 * \dots, P_{out}^m, Q_{out}^m, V^{1}, \dots, V^{p}] \f]
136 *
137 * with \f$P_{in} \f$, \f$Q_{in} \f$, \f$P_{out} \f$, \f$Q_{out} \f$, and \f$V
138 * \f$ denoting inlet pressure, inlet flow, outlet pressure, outlet flow and
139 * an internal variable of the element, respectively.
140 *
141 * Variable indices correspond to columns in the global system.
142 *
143 */
144 std::vector<int> global_var_ids;
145
146 /**
147 * @brief Global equation indices of the local element contributions
148 *
149 * Equation indices correspond to rows in the global system.
150 */
151 std::vector<int> global_eqn_ids;
152
153 /**
154 * @brief Get the name of the block
155 *
156 * @return std::string Name of the block
157 */
158 std::string get_name();
159
160 /**
161 * @brief Update vessel type of the block
162 *
163 * @param type Type of vessel
164 */
166
167 /**
168 * @brief Setup parameter IDs for the block
169 * @param param_ids Global IDs of the block parameters
170 */
171 void setup_params_(const std::vector<int>& param_ids);
172
173 /**
174 * @brief Set up the degrees of freedom (DOF) of the block
175 *
176 * Set \ref global_var_ids and \ref global_eqn_ids of the element based on the
177 * number of equations and the number of internal variables of the
178 * element.
179 *
180 * @param dofhandler Degree-of-freedom handler to register variables and
181 * equations at
182 * @param num_equations Number of equations of the block
183 * @param internal_var_names Number of internal variables of the block
184 */
185
186 void setup_dofs_(DOFHandler& dofhandler, int num_equations,
187 const std::list<std::string>& internal_var_names);
188
189 /**
190 * @brief Set up the degrees of freedom (DOF) of the block
191 *
192 * Set \ref global_var_ids and \ref global_eqn_ids of the element based on the
193 * number of equations and the number of internal variables of the
194 * element.
195 *
196 * @param dofhandler Degree-of-freedom handler to register variables and
197 * equations at
198 */
199 virtual void setup_dofs(DOFHandler& dofhandler);
200
201 /**
202 * @brief Setup parameters that depend on the model
203 *
204 */
205 virtual void setup_model_dependent_params();
206
207 /**
208 * @brief Setup parameters that depend on the initial state
209 *
210 * @param initial_state The initial state of the system
211 * @param parameters The parameter values vector (at time 0)
212 */
214 State initial_state, std::vector<double>& parameters);
215
216 /**
217 * @brief Update the constant contributions of the element in a sparse system
218 *
219 * @param system System to update contributions at
220 * @param parameters Parameters of the model
221 */
222 virtual void update_constant(SparseSystem& system,
223 std::vector<double>& parameters);
224 /**
225 * @brief Update the time-dependent contributions of the element in a sparse
226 * system
227 *
228 * @param system System to update contributions at
229 * @param parameters Parameters of the model
230 */
231 virtual void update_time(SparseSystem& system,
232 std::vector<double>& parameters);
233
234 /**
235 * @brief Update the solution-dependent contributions of the element in a
236 * sparse system
237 *
238 * @param system System to update contributions at
239 * @param parameters Parameters of the model
240 * @param y Current solution
241 * @param dy Current derivate of the solution
242 */
243 virtual void update_solution(
244 SparseSystem& system, std::vector<double>& parameters,
245 const Eigen::Matrix<double, Eigen::Dynamic, 1>& y,
246 const Eigen::Matrix<double, Eigen::Dynamic, 1>& dy);
247
248 /**
249 * @brief Modify the solution after solving it
250 *
251 * @param y Current solution
252 */
253 virtual void post_solve(Eigen::Matrix<double, Eigen::Dynamic, 1>& y);
254
255 /**
256 * @brief Set the gradient of the block contributions with respect to the
257 * parameters
258 *
259 * @param jacobian Jacobian with respect to the parameters
260 * @param alpha Current parameter vector
261 * @param residual Residual with respect to the parameters
262 * @param y Current solution
263 * @param dy Time-derivative of the current solution
264 */
265 virtual void update_gradient(
266 Eigen::SparseMatrix<double>& jacobian,
267 Eigen::Matrix<double, Eigen::Dynamic, 1>& residual,
268 Eigen::Matrix<double, Eigen::Dynamic, 1>& alpha, std::vector<double>& y,
269 std::vector<double>& dy);
270
271 /**
272 * @brief Number of triplets of element
273 *
274 * Number of triplets that the element contributes to the global system
275 * (relevant for sparse memory reservation)
276 */
278
279 /**
280 * @brief Get number of triplets of element
281 *
282 * Number of triplets that the element contributes to the global system
283 * (relevant for sparse memory reservation)
284 *
285 * @return TripletsContributions Number of triplets of element
286 */
288
289 /**
290 * @brief Set activation function (for chamber blocks that use one).
291 *
292 * Default no-op. Overridden by ChamberElastanceInductor and
293 * LinearElastanceChamber to take ownership of the activation function.
294 *
295 * @param af Unique pointer to the activation function (caller transfers
296 * ownership)
297 */
298 virtual void set_activation_function(std::unique_ptr<ActivationFunction> af) {
299 (void)af; // Included to avoid unused parameter warning
300 }
301
302 /**
303 * @brief Set wall material (for ChamberSphere blocks).
304 *
305 * Default no-op. Overridden by ChamberSphere to take ownership of the
306 * material.
307 *
308 * @param m Unique pointer to the material (caller transfers ownership)
309 */
310 virtual void set_material(std::unique_ptr<SphereMaterial> m) {
311 (void)m; // Included to avoid unused parameter warning
312 }
313};
314
315#endif
Activation function classes for cardiac chamber models.
Specifies the types of blocks and their parameters.
BlockType
The types of blocks supported by the solver.
Definition BlockType.h:15
BlockClass
The classes/categories of blocks supported. Some classes require special handling (e....
Definition BlockType.h:44
VesselType
The types of vessel blocks supported.
Definition BlockType.h:57
model::DOFHandler source file
model::Parameter source file
SparseSystem source file.
Material models for the ChamberSphere block.
State source file.
bool input_params_list
Are input parameters given as a list?
Definition Block.h:91
void setup_params_(const std::vector< int > &param_ids)
Setup parameter IDs for the block.
Definition Block.cpp:14
Block(const Block &)=delete
Copy the Block object.
std::vector< int > global_param_ids
Global IDs for the block parameters.
Definition Block.h:125
~Block()
Destroy the Block object.
Definition Block.cpp:12
Block(int id, Model *model, BlockType block_type, BlockClass block_class, std::vector< std::pair< std::string, InputParameter > > input_params)
Construct a new Block object.
Definition Block.h:102
const std::vector< std::pair< std::string, InputParameter > > input_params
Map from name to input parameter.
Definition Block.h:85
std::string get_name()
Get the name of the block.
Definition Block.cpp:8
virtual void setup_initial_state_dependent_params(State initial_state, std::vector< double > &parameters)
Setup parameters that depend on the initial state.
Definition Block.cpp:48
virtual void update_solution(SparseSystem &system, std::vector< double > &parameters, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &y, const Eigen::Matrix< double, Eigen::Dynamic, 1 > &dy)
Update the solution-dependent contributions of the element in a sparse system.
Definition Block.cpp:57
const BlockClass block_class
Class of this block.
Definition Block.h:82
virtual void setup_dofs(DOFHandler &dofhandler)
Set up the degrees of freedom (DOF) of the block.
Definition Block.cpp:44
const int id
Global ID of the block.
Definition Block.h:79
const Model * model
The model to which the block belongs.
Definition Block.h:80
TripletsContributions num_triplets
Number of triplets of element.
Definition Block.h:277
virtual void update_time(SparseSystem &system, std::vector< double > &parameters)
Update the time-dependent contributions of the element in a sparse system.
Definition Block.cpp:54
std::vector< int > global_eqn_ids
Global equation indices of the local element contributions.
Definition Block.h:151
std::vector< Node * > outlet_nodes
Outlet nodes.
Definition Block.h:88
virtual TripletsContributions get_num_triplets()
Get number of triplets of element.
Definition Block.cpp:72
virtual void update_constant(SparseSystem &system, std::vector< double > &parameters)
Update the constant contributions of the element in a sparse system.
Definition Block.cpp:51
virtual void set_material(std::unique_ptr< SphereMaterial > m)
Set wall material (for ChamberSphere blocks).
Definition Block.h:310
std::vector< Node * > inlet_nodes
Inlet nodes.
Definition Block.h:87
virtual void update_gradient(Eigen::SparseMatrix< double > &jacobian, Eigen::Matrix< double, Eigen::Dynamic, 1 > &residual, Eigen::Matrix< double, Eigen::Dynamic, 1 > &alpha, std::vector< double > &y, std::vector< double > &dy)
Set the gradient of the block contributions with respect to the parameters.
Definition Block.cpp:64
virtual void setup_model_dependent_params()
Setup parameters that depend on the model.
Definition Block.cpp:46
VesselType vessel_type
Vessel type of this block.
Definition Block.h:83
void update_vessel_type(VesselType type)
Update vessel type of the block.
Definition Block.cpp:10
std::vector< int > global_var_ids
Global variable indices of the local element contributions.
Definition Block.h:144
virtual void set_activation_function(std::unique_ptr< ActivationFunction > af)
Set activation function (for chamber blocks that use one).
Definition Block.h:298
virtual void post_solve(Eigen::Matrix< double, Eigen::Dynamic, 1 > &y)
Modify the solution after solving it.
Definition Block.cpp:62
bool steady
Toggle steady behavior.
Definition Block.h:90
const BlockType block_type
Type of this block.
Definition Block.h:81
void setup_dofs_(DOFHandler &dofhandler, int num_equations, const std::list< std::string > &internal_var_names)
Set up the degrees of freedom (DOF) of the block.
Definition Block.cpp:18
Degree-of-freedom handler.
Definition DOFHandler.h:21
Model of 0D elements.
Definition Model.h:55
Node.
Definition Node.h:26
Sparse system.
Definition SparseSystem.h:30
State of the system.
Definition State.h:19
The number of triplets that the element contributes to the global system.
Definition Block.h:28
int E
Contributions to E matrix.
Definition Block.h:59
TripletsContributions(int F, int E, int D)
Set the number of triplets that the element contributes to the global system.
Definition Block.h:37
int F
Contributions to F matrix.
Definition Block.h:55
TripletsContributions operator+=(const TripletsContributions &other)
Set the number of triplets that the element contributes to the global system.
Definition Block.h:45
int D
Contributions to dC/dy matrix.
Definition Block.h:63