Skip to content

Commit 587b125

Browse files
committed
Write mass matrix page
1 parent 9e744c4 commit 587b125

File tree

1 file changed

+89
-6
lines changed

1 file changed

+89
-6
lines changed

docs/mass-matrix.md

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,96 @@ layout: default
44
nav_order: 3
55
---
66

7-
Work in progress
8-
{: .label .label-red }
9-
107
# Mass Matrix class
118

12-
Text
9+
Mass matrix class defines the mass matrix $$\mathbf{M}(t)$$ of the DAE system written in the matrix-vector form:
10+
11+
$$\mathbf{M}(t) \frac{\mathrm{d}\mathbf{x}}{\mathrm{d}t} = \mathbf{f}(\mathbf{x}, t).$$
12+
13+
Note that in `dae-cpp`, the mass matrix can depend on time $$t$$ (but not on the state $$\mathbf{x}$$) and can be singular, which means that one or more of the rows of the matrix can contain zeros only, i.e., some of the equations in the system can be without time derivatives at all (nonlinear algebraic equations).
14+
15+
## Mass matrix definition
16+
17+
The main mass matrix class `daecpp::MassMatrix` is an abstract class that provides an interface to define the mass matrix $$\mathbf{M}(t)$$.
18+
In order to make use of this class, the user should inherit it and overload the `()` operator:
19+
20+
```cpp
21+
class UserDefinedMassMatrix : public daecpp::MassMatrix
22+
{
23+
public:
24+
void operator()(daecpp::sparse_matrix &M, const double t) const
25+
{
26+
// Mass matrix definition
27+
}
28+
};
29+
```
30+
31+
In the code above, the mass matrix `M` should be defined in the [three-array sparse format](sparse-matrix.html) and can depend on time `t`.
32+
Initially, matrix `M` is empty and should be filled with non-zero elements.
33+
34+
## Examples
35+
36+
Consider the following mass matrix $$\mathbf{x}$$:
37+
38+
$$
39+
\mathbf{M} =
40+
\begin{vmatrix}
41+
1 & 0 & 0 \\
42+
0 & 2t & 0 \\
43+
0 & 0 & 0
44+
\end{vmatrix}.
45+
$$
46+
47+
In the code, this mass matrix can be defined as
48+
49+
```cpp
50+
class UserDefinedMassMatrix : public daecpp::MassMatrix
51+
{
52+
public:
53+
void operator()(daecpp::sparse_matrix &M, const double t) const
54+
{
55+
M.reserve(2); // Pre-allocate memory for 2 non-zero elements
56+
M(0, 0, 1.0); // Row 0, column 0, non-zero element 1.0
57+
M(1, 1, 2.0 * t); // Row 1, column 1, non-zero element (2 * t)
58+
}
59+
};
60+
```
61+
62+
Inhereting the `daecpp::MassMatrix` class is a good practice (it serves as a blueprint), however, the user is allowed to define mass matrices using their own custom classes, for example:
63+
64+
```cpp
65+
struct UserDefinedMassMatrix
66+
{
67+
void operator()(daecpp::sparse_matrix &M, const double t)
68+
{
69+
M.reserve(2); // Pre-allocate memory for 2 non-zero elements
70+
M(0, 0, 1.0); // Row 0, column 0, non-zero element 1.0
71+
M(1, 1, 2.0 * t); // Row 1, column 1, non-zero element (2 * t)
72+
}
73+
};
74+
```
75+
76+
{: .note }
77+
It is recommended to pre-allocate memory for the mass matrix using `reserve(N_elements)` method, where `N_elements` is the number of non-zero elements in the matrix. If the number of non-zeros is difficult to estimate, it is better to overestimate `N_elements` than underestimate it to avoid unnecessary copying and memory reallocations.
78+
79+
For more information about defining the matrix in sparse format, refer to the [Sparse Matrix](sparse-matrix.html) section.
80+
81+
## Identity mass matrix
82+
83+
A helper class `daecpp::MassMatrixIdentity` can be used to construct an identity matrix `N` by `N`:
84+
85+
```cpp
86+
daecpp::MassMatrixIdentity identityMatrix(16); // Creates identity mass matrix 16x16
87+
```
88+
89+
In the example above, we created an object `identityMatrix` which holds an identity matrix of size 16x16 in sparse format suitable for the solver.
90+
91+
## Zero mass matrix
92+
93+
Similar ot the example above, a helper class `daecpp::MassMatrixZero` can be used to construct a zero matrix (a matrix filled by zeros only). This matrix can be useful to solve pure algebraic systems without time derivatives.
1394

14-
## Subtitle
95+
```cpp
96+
daecpp::MassMatrixZero zeroMatrix; // Creates empty mass matrix for algebraic system
97+
```
1598

16-
Text
99+
Note that `daecpp::MassMatrixZero` does not require the matrix size (unlike `MassMatrixIdentity`), because zero matrix does not contain any non-zero elements. It's an empty sparse matrix, and its size will be derived from the initial state vector size.

0 commit comments

Comments
 (0)