svZeroDSolver
Loading...
Searching...
No Matches
SparseSystem.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 SparseSystem.h
32 * @brief SparseSystem source file
33 */
34#ifndef SVZERODSOLVER_ALGREBRA_SPARSESYSTEM_HPP_
35#define SVZERODSOLVER_ALGREBRA_SPARSESYSTEM_HPP_
36
37#include <Eigen/Sparse>
38#include <Eigen/SparseLU>
39#include <iostream>
40#include <memory>
41
42// Forward declaration of Model
43class Model;
44
45/**
46 * @brief Sparse system
47 *
48 * This class contains all attributes and methods to create, modify, and
49 * solve sparse systems.
50 *
51 * Flow rate, pressure, and other hemodynamic quantities in 0D models of
52 * vascular anatomies are governed by a system of nonlinear
53 * differential-algebraic equations (DAEs):
54 *
55 * \f[
56 * \mathbf{r}(\boldsymbol{\alpha}, \mathbf{y},\dot{\mathbf{y}}, t) =
57 * \mathbf{E}(\boldsymbol{\alpha}) \cdot \dot{\mathbf{y}} +
58 * \mathbf{F}(\boldsymbol{\alpha}) \cdot \mathbf{y} +
59 * \mathbf{c}(\mathbf{y},\dot{\mathbf{y}}, t) = \mathbf{0}
60 * \f]
61 *
62 * where \f$\mathbf{r},\textbf{y},\textbf{c} \in \mathbb{R}^{N}\f$ and
63 * \f$\textbf{E},\textbf{F} \in \mathbb{R}^{N \times N}\f$. Here,
64 * \f$\textbf{r}\f$ is the residual, \f$\textbf{y}\f$ is the vector of solution
65 * quantities and \f$\dot{\textbf{y}}\f$ is its time derivative. \f$N\f$ is the
66 * total number of equations and the total number of global unknowns. The DAE
67 * system is solved implicitly using the generalized-\f$\alpha\f$ method in
68 * Integrator. We then use the Newton-Raphson method to iteratively solve
69 *
70 * \f[
71 * \mathbf{K}^{i} \cdot \Delta\dot{\mathbf{y}}^{i} = - \mathbf{r}^{i},
72 * \f]
73 *
74 * with solution increment \f$\Delta\dot{\mathbf{y}}^{i}\f$ in iteration
75 * \f$i\f$. The linearization of the time-discretized system is
76 *
77 * \f[
78 * \mathbf{K} =
79 * \frac{\partial \mathbf{r}}{\partial \mathbf{y}} =
80 * c_{\dot{\mathbf{y}}} \left( \mathbf{E} + \frac{\partial \mathbf{c}}{\partial
81 * \dot{\mathbf{y}}} \right) +
82 * c_{\mathbf{y}} \left( \mathbf{F} + \frac{\partial \mathbf{c}}{\partial
83 * \mathbf{y}} \right), \f]
84 *
85 * with time factors \f$c_{\dot{\mathbf{y}}}=\alpha_m\f$ and
86 * \f$c_{\mathbf{y}}=\alpha_f\gamma\Delta t\f$ provided by Integrator.
87 */
89 public:
90 /**
91 * @brief Construct a new Sparse System object
92 *
93 */
95
96 /**
97 * @brief Construct a new Sparse System object
98 *
99 * @param n Size of the system
100 */
101 SparseSystem(int n);
102
103 /**
104 * @brief Destroy the Sparse System object
105 *
106 */
108
109 Eigen::SparseMatrix<double> F; ///< System matrix F
110 Eigen::SparseMatrix<double> E; ///< System matrix E
111 Eigen::SparseMatrix<double> dC_dy; ///< System matrix dC/dy
112 Eigen::SparseMatrix<double> dC_dydot; ///< System matrix dC/dydot
113 Eigen::Matrix<double, Eigen::Dynamic, 1> C; ///< System vector C
114
115 Eigen::SparseMatrix<double> jacobian; ///< Jacobian of the system
116 Eigen::Matrix<double, Eigen::Dynamic, 1>
117 residual; ///< Residual of the system
118 Eigen::Matrix<double, Eigen::Dynamic, 1>
119 dydot; ///< Solution increment of the system
120
121 std::shared_ptr<Eigen::SparseLU<Eigen::SparseMatrix<double>>> solver =
122 std::shared_ptr<Eigen::SparseLU<Eigen::SparseMatrix<double>>>(
123 new Eigen::SparseLU<Eigen::SparseMatrix<double>>()); ///< Linear
124 ///< solver
125
126 /**
127 * @brief Reserve memory in system matrices based on number of triplets
128 *
129 * @param model The model to reserve space for in the system
130 */
131 void reserve(Model *model);
132
133 /**
134 * @brief Update the residual of the system
135 *
136 * @param y Vector of current solution quantities
137 * @param ydot Derivate of y
138 */
139 void update_residual(Eigen::Matrix<double, Eigen::Dynamic, 1> &y,
140 Eigen::Matrix<double, Eigen::Dynamic, 1> &ydot);
141
142 /**
143 * @brief Update the jacobian of the system
144 *
145 * @param time_coeff_ydot Coefficent ydot-dependent part of jacobian
146 * @param time_coeff_y Coefficent ydot-dependent part of jacobian
147 */
148 void update_jacobian(double time_coeff_ydot, double time_coeff_y);
149
150 /**
151 * @brief Solve the system
152 */
153 void solve();
154
155 /**
156 * @brief Delete dynamically allocated memory (class member
157 * Eigen::SparseLU<Eigen::SparseMatrix> *solver)
158 */
159 void clean();
160};
161
162#endif // SVZERODSOLVER_ALGREBRA_SPARSESYSTEM_HPP_
Model of 0D elements.
Definition Model.h:75
Sparse system.
Definition SparseSystem.h:88
Eigen::SparseMatrix< double > dC_dy
System matrix dC/dy.
Definition SparseSystem.h:111
void reserve(Model *model)
Reserve memory in system matrices based on number of triplets.
Definition SparseSystem.cpp:57
void clean()
Delete dynamically allocated memory (class member Eigen::SparseLU<Eigen::SparseMatrix> *solver)
Definition SparseSystem.cpp:51
Eigen::SparseMatrix< double > E
System matrix E.
Definition SparseSystem.h:110
Eigen::SparseMatrix< double > dC_dydot
System matrix dC/dydot.
Definition SparseSystem.h:112
Eigen::Matrix< double, Eigen::Dynamic, 1 > C
System vector C.
Definition SparseSystem.h:113
Eigen::Matrix< double, Eigen::Dynamic, 1 > residual
Residual of the system.
Definition SparseSystem.h:117
SparseSystem()
Construct a new Sparse System object.
Definition SparseSystem.cpp:35
void solve()
Solve the system.
Definition SparseSystem.cpp:101
Eigen::SparseMatrix< double > F
System matrix F.
Definition SparseSystem.h:109
void update_residual(Eigen::Matrix< double, Eigen::Dynamic, 1 > &y, Eigen::Matrix< double, Eigen::Dynamic, 1 > &ydot)
Update the residual of the system.
Definition SparseSystem.cpp:85
void update_jacobian(double time_coeff_ydot, double time_coeff_y)
Update the jacobian of the system.
Definition SparseSystem.cpp:94
Eigen::Matrix< double, Eigen::Dynamic, 1 > dydot
Solution increment of the system.
Definition SparseSystem.h:119
~SparseSystem()
Destroy the Sparse System object.
Definition SparseSystem.cpp:49
Eigen::SparseMatrix< double > jacobian
Jacobian of the system.
Definition SparseSystem.h:115
std::shared_ptr< Eigen::SparseLU< Eigen::SparseMatrix< double > > > solver
Definition SparseSystem.h:121