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