svZeroDSolver
Loading...
Searching...
No Matches
Block.h
Go to the documentation of this file.
1// Copyright (c) Stanford University, The Regents of the University of
2// California, and others.
3//
4// All Rights Reserved.
5//
6// See Copyright-SimVascular.txt for additional details.
7//
8// Permission is hereby granted, free of charge, to any person obtaining
9// a copy of this software and associated documentation files (the
10// "Software"), to deal in the Software without restriction, including
11// without limitation the rights to use, copy, modify, merge, publish,
12// distribute, sublicense, and/or sell copies of the Software, and to
13// permit persons to whom the Software is furnished to do so, subject
14// to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included
17// in all copies or substantial portions of the Software.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30/**
31 * @file Block.h
32 * @brief model::Block source file
33 */
34#ifndef SVZERODSOLVER_MODEL_BLOCK_HPP_
35#define SVZERODSOLVER_MODEL_BLOCK_HPP_
36
37#include <Eigen/Core>
38#include <list>
39#include <map>
40#include <vector>
41
42#include "BlockType.h"
43#include "DOFHandler.h"
44#include "Parameter.h"
45#include "SparseSystem.h"
46#include "State.h"
47
48/**
49 * @brief The number of triplets that the element contributes
50 * to the global system.
51 */
54 /**
55 * @brief Set the number of triplets that the element contributes
56 * to the global system.
57 * @param F Contributions to F matrix
58 * @param E Contributions to E matrix
59 * @param D Contributions to dC/dy matrix
60 */
61 TripletsContributions(int F, int E, int D) : F(F), E(E), D(D){};
62 /**
63 * @brief Set the number of triplets that the element contributes
64 * to the global system.
65 * @param other TripletsContributions object to add to the
66 * number of contributions
67 * @return The number of triplets
68 */
70 F += other.F;
71 E += other.E;
72 D += other.D;
73 return *this;
74 };
75
76 /**
77 * @brief Contributions to F matrix
78 */
79 int F{0};
80 /**
81 * @brief Contributions to E matrix
82 */
83 int E{0};
84 /**
85 * @brief Contributions to dC/dy matrix
86 */
87 int D{0};
88};
89
90class Node;
91class Model;
92
93/**
94 * @brief Base class for 0D model components.
95 *
96 * A Block is the base class of 0D model elements. It is the place
97 * where the contribution of an element to the global system is controlled.
98 * It defines all relevant attributes and methods of an element and a few
99 * common helpers for setting it up.
100 */
101class Block {
102 public:
103 const int id; ///< Global ID of the block
104 const Model *model; ///< The model to which the block belongs
105 const BlockType block_type; ///< Type of this block
106 const BlockClass block_class; ///< Class of this block
107 VesselType vessel_type = VesselType::neither; ///< Vessel type of this block
108 const std::vector<std::pair<std::string, InputParameter>>
109 input_params; ///< Map from name to input parameter
110
111 std::vector<Node *> inlet_nodes; ///< Inlet nodes
112 std::vector<Node *> outlet_nodes; ///< Outlet nodes
113
114 bool steady = false; ///< Toggle steady behavior
115 bool input_params_list = false; ///< Are input parameters given as a list?
116
117 /**
118 * @brief Construct a new Block object
119 *
120 * @param id Global ID of the block
121 * @param model The model to which the block belongs
122 * @param block_type The specific type of block
123 * @param block_class The class the block belongs to (e.g. vessel, junction)
124 * @param input_params The parameters the block takes from the input file
125 */
127 std::vector<std::pair<std::string, InputParameter>> input_params)
128 : id(id),
129 model(model),
133
134 /**
135 * @brief Destroy the Block object
136 *
137 */
138 ~Block();
139
140 /**
141 * @brief Copy the Block object
142 *
143 */
144 Block(const Block &) = delete;
145 /**
146 * @brief Global IDs for the block parameters.
147 *
148 */
149 std::vector<int> global_param_ids; ///< IDs of the parameters
150
151 /**
152 * @brief Global variable indices of the local element contributions
153 *
154 * Determines where the local element contributions are written to in
155 * the global system during assembly. The order of indices is:
156 *
157 * \f[
158 * [P_{in}^1, Q_{in}^1, \dots, P_{in}^n, Q_{in}^n, P_{out}^1, Q_{out}^1,
159 * \dots, P_{out}^m, Q_{out}^m, V^{1}, \dots, V^{p}] \f]
160 *
161 * with \f$P_{in} \f$, \f$Q_{in} \f$, \f$P_{out} \f$, \f$Q_{out} \f$, and \f$V
162 * \f$ denoting inlet pressure, inlet flow, outlet pressure, outlet flow and
163 * an internal variable of the element, respectively.
164 *
165 * Variable indices correspond to columns in the global system.
166 *
167 */
168 std::vector<int> global_var_ids;
169
170 /**
171 * @brief Global equation indices of the local element contributions
172 *
173 * Equation indices correspond to rows in the global system.
174 */
175 std::vector<int> global_eqn_ids;
176
177 /**
178 * @brief Get the name of the block
179 *
180 * @return std::string Name of the block
181 */
182 std::string get_name();
183
184 /**
185 * @brief Update vessel type of the block
186 *
187 * @param type Type of vessel
188 */
190
191 /**
192 * @brief Setup parameter IDs for the block
193 * @param param_ids Global IDs of the block parameters
194 */
195 void setup_params_(const std::vector<int> &param_ids);
196
197 /**
198 * @brief Set up the degrees of freedom (DOF) of the block
199 *
200 * Set \ref global_var_ids and \ref global_eqn_ids of the element based on the
201 * number of equations and the number of internal variables of the
202 * element.
203 *
204 * @param dofhandler Degree-of-freedom handler to register variables and
205 * equations at
206 * @param num_equations Number of equations of the block
207 * @param internal_var_names Number of internal variables of the block
208 */
209
210 void setup_dofs_(DOFHandler &dofhandler, int num_equations,
211 const std::list<std::string> &internal_var_names);
212
213 /**
214 * @brief Set up the degrees of freedom (DOF) of the block
215 *
216 * Set \ref global_var_ids and \ref global_eqn_ids of the element based on the
217 * number of equations and the number of internal variables of the
218 * element.
219 *
220 * @param dofhandler Degree-of-freedom handler to register variables and
221 * equations at
222 */
223 virtual void setup_dofs(DOFHandler &dofhandler);
224
225 /**
226 * @brief Setup parameters that depend on the model
227 *
228 */
229 virtual void setup_model_dependent_params();
230
231 /**
232 * @brief Setup parameters that depend on the initial state
233 *
234 * @param initial_state The initial state of the system
235 * @param parameters The parameter values vector (at time 0)
236 */
238 State initial_state, std::vector<double> &parameters);
239
240 /**
241 * @brief Update the constant contributions of the element in a sparse system
242 *
243 * @param system System to update contributions at
244 * @param parameters Parameters of the model
245 */
246 virtual void update_constant(SparseSystem &system,
247 std::vector<double> &parameters);
248 /**
249 * @brief Update the time-dependent contributions of the element in a sparse
250 * system
251 *
252 * @param system System to update contributions at
253 * @param parameters Parameters of the model
254 */
255 virtual void update_time(SparseSystem &system,
256 std::vector<double> &parameters);
257
258 /**
259 * @brief Update the solution-dependent contributions of the element in a
260 * sparse system
261 *
262 * @param system System to update contributions at
263 * @param parameters Parameters of the model
264 * @param y Current solution
265 * @param dy Current derivate of the solution
266 */
267 virtual void update_solution(
268 SparseSystem &system, std::vector<double> &parameters,
269 const Eigen::Matrix<double, Eigen::Dynamic, 1> &y,
270 const Eigen::Matrix<double, Eigen::Dynamic, 1> &dy);
271
272 /**
273 * @brief Modify the solution after solving it
274 *
275 * @param y Current solution
276 */
277 virtual void post_solve(Eigen::Matrix<double, Eigen::Dynamic, 1> &y);
278
279 /**
280 * @brief Set the gradient of the block contributions with respect to the
281 * parameters
282 *
283 * @param jacobian Jacobian with respect to the parameters
284 * @param alpha Current parameter vector
285 * @param residual Residual with respect to the parameters
286 * @param y Current solution
287 * @param dy Time-derivative of the current solution
288 */
289 virtual void update_gradient(
290 Eigen::SparseMatrix<double> &jacobian,
291 Eigen::Matrix<double, Eigen::Dynamic, 1> &residual,
292 Eigen::Matrix<double, Eigen::Dynamic, 1> &alpha, std::vector<double> &y,
293 std::vector<double> &dy);
294
295 /**
296 * @brief Number of triplets of element
297 *
298 * Number of triplets that the element contributes to the global system
299 * (relevant for sparse memory reservation)
300 */
302
303 /**
304 * @brief Get number of triplets of element
305 *
306 * Number of triplets that the element contributes to the global system
307 * (relevant for sparse memory reservation)
308 *
309 * @return TripletsContributions Number of triplets of element
310 */
312};
313
314#endif
Specifies the types of blocks and their parameters.
BlockType
The types of blocks supported by the solver.
Definition BlockType.h:42
BlockClass
The classes/categories of blocks supported. Some classes require special handling (e....
Definition BlockType.h:64
VesselType
The types of vessel blocks supported.
Definition BlockType.h:77
model::DOFHandler source file
model::Parameter source file
SparseSystem source file.
State source file.
Base class for 0D model components.
Definition Block.h:101
bool input_params_list
Are input parameters given as a list?
Definition Block.h:115
void setup_params_(const std::vector< int > &param_ids)
Setup parameter IDs for the block.
Definition Block.cpp:41
Block(const Block &)=delete
Copy the Block object.
std::vector< int > global_param_ids
Global IDs for the block parameters.
Definition Block.h:149
~Block()
Destroy the Block object.
Definition Block.cpp:39
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:126
const std::vector< std::pair< std::string, InputParameter > > input_params
Map from name to input parameter.
Definition Block.h:109
std::string get_name()
Get the name of the block.
Definition Block.cpp:35
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:75
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:84
const BlockClass block_class
Class of this block.
Definition Block.h:106
virtual void setup_dofs(DOFHandler &dofhandler)
Set up the degrees of freedom (DOF) of the block.
Definition Block.cpp:71
const int id
Global ID of the block.
Definition Block.h:103
const Model * model
The model to which the block belongs.
Definition Block.h:104
TripletsContributions num_triplets
Number of triplets of element.
Definition Block.h:301
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:81
std::vector< int > global_eqn_ids
Global equation indices of the local element contributions.
Definition Block.h:175
virtual TripletsContributions get_num_triplets()
Get number of triplets of element.
Definition Block.cpp:99
virtual void update_constant(SparseSystem &system, std::vector< double > &parameters)
Update the constant contributions of the element in a sparse system.
Definition Block.cpp:78
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:91
virtual void setup_model_dependent_params()
Setup parameters that depend on the model.
Definition Block.cpp:73
VesselType vessel_type
Vessel type of this block.
Definition Block.h:107
void update_vessel_type(VesselType type)
Update vessel type of the block.
Definition Block.cpp:37
std::vector< int > global_var_ids
Global variable indices of the local element contributions.
Definition Block.h:168
virtual void post_solve(Eigen::Matrix< double, Eigen::Dynamic, 1 > &y)
Modify the solution after solving it.
Definition Block.cpp:89
std::vector< Node * > outlet_nodes
Outlet nodes.
Definition Block.h:112
bool steady
Toggle steady behavior.
Definition Block.h:114
std::vector< Node * > inlet_nodes
Inlet nodes.
Definition Block.h:111
const BlockType block_type
Type of this block.
Definition Block.h:105
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:45
Degree-of-freedom handler.
Definition DOFHandler.h:48
Model of 0D elements.
Definition Model.h:75
Node.
Definition Node.h:53
Sparse system.
Definition SparseSystem.h:57
State of the system.
Definition State.h:46
The number of triplets that the element contributes to the global system.
Definition Block.h:52
int E
Contributions to E matrix.
Definition Block.h:83
TripletsContributions(int F, int E, int D)
Set the number of triplets that the element contributes to the global system.
Definition Block.h:61
int F
Contributions to F matrix.
Definition Block.h:79
TripletsContributions operator+=(const TripletsContributions &other)
Set the number of triplets that the element contributes to the global system.
Definition Block.h:69
int D
Contributions to dC/dy matrix.
Definition Block.h:87