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