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