Skip to content

Commit 9a2a6ee

Browse files
committed
Write the first part of the solution manager page
1 parent 95b6fcd commit 9a2a6ee

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

docs/jacobian-matrix.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ nav_order: 5
77

88
# Jacobian Matrix class
99

10-
Jacobian matrix class defines the Jacobian matrix $$\mathbf{J}(\mathbf{x}, t)$$ of the DAE system.
10+
The Jacobian matrix class defines the Jacobian matrix $$\mathbf{J}(\mathbf{x}, t)$$ of the DAE system.
1111
This matrix can be obtained by differentiating the vector function (the RHS) $$\mathbf{f}(\mathbf{x}, t)$$ of the DAE system
1212

1313
$$\mathbf{M}(t) \frac{\mathrm{d}\mathbf{x}}{\mathrm{d}t} = \mathbf{f}(\mathbf{x}, t)$$
@@ -40,6 +40,9 @@ Initially, matrix `J` is empty and should be filled with non-zero elements.
4040
{: .note }
4141
The type of vector `x` in the class definition above is `daecpp::state_vector`, which is an alias of `std::vector<float_type>` type. See [`dae-cpp` types](https://dae-cpp.github.io/prerequisites.html#dae-cpp-types) section for more information.
4242

43+
{: .important }
44+
The Jacobian Matrix class must not be used to save or post-process the solution vector `x` and time `t`. For this purpose, use the [Solution Manager](solution-manager.html) class.
45+
4346
## Examples
4447

4548
Consider the following vector function $$\mathbf{f}(\mathbf{x}, t)$$ from the [Vector Function class](vector-function.html#examples) section:

docs/mass-matrix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ nav_order: 3
77

88
# Mass Matrix class
99

10-
Mass matrix class defines the mass matrix $$\mathbf{M}(t)$$ of the DAE system written in the matrix-vector form:
10+
The Mass matrix class defines the mass matrix $$\mathbf{M}(t)$$ of the DAE system written in the matrix-vector form:
1111

1212
$$\mathbf{M}(t) \frac{\mathrm{d}\mathbf{x}}{\mathrm{d}t} = \mathbf{f}(\mathbf{x}, t).$$
1313

docs/solution-manager.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,79 @@ Work in progress
99

1010
# Solution Manager class
1111

12-
The Solution Manager class can serve as a solution observer and/or as an event function that stops computation when a certain event occurs. Solution Manager functor will be called every time step, providing the time `t` and the corresponding solution `x` for further post-processing. If the functor returns an integer which is not equal to `0` (i.e., `true`), the computation will immediately stop.
12+
The Solution Manager class can serve as a solution observer and/or as an event function that stops computation when a certain event occurs. Solution Manager functor will be called every time step, providing the time `t` and the corresponding solution `x` for the user for further post-processing. If the functor returns an integer which is not equal to `0` (i.e., `true`), the computation will immediately stop.
1313

14-
The solver provides a derived from the Solution Manager class called `daecpp::Solution`, which writes the solution vector `x` and time `t` every time step or every specific time defined by the user into the [solution holder](#solution-holder) object. The user can access this object for further post-processing, printing the solution on the screen or writing it to a file.
14+
The solver provides a derived from the Solution Manager class called [`daecpp::Solution`](#solution-class), which writes the solution vector `x` and time `t` every time step or every specific time defined by the user into the [solution holder](#solution-holder) object. The user can access this object for further post-processing, printing the solution on the screen or writing it to a file.
15+
16+
{: .important }
17+
By default, the DAE solver does nothing with the solution, i.e., it does not print it on the screen or save it anywhere. It is the user's responsibility to define a Solution Manager object that processes the solution (saves it to a vector, to a file, etc.). The solver, however, provides a basic Solution Manager called [`daecpp::Solution`](#solution-class).
18+
19+
## Solution Manager definition
20+
21+
The Solution Manager class `daecpp::SolutionManager` is a virtual class that provides an interface to define the Solution Manager.
22+
In order to make use of this class, the user should inherit it and overload the `()` operator:
23+
24+
```cpp
25+
class UserDefinedSolutionManager : public daecpp::SolutionManager
26+
{
27+
public:
28+
int operator()(const daecpp::state_vector &x, const double t)
29+
{
30+
// Solution Manager definition
31+
32+
return 0; // Returns 0 by default or an integer != 0 to stop the computation
33+
}
34+
};
35+
```
36+
37+
The operator `()` will be called every time step, providing the user with the current state `x` and the corresponding time `t`.
38+
If the functor returns non-zero integer, the computation will stop.
39+
The user is free to define the solution container and save the solution within the Solution Manager class. The solver provides [`SolutionHolder`](#solution-holder) helper class that stores solution vectors `x` and the corresponding times `t`.
40+
41+
{: .note }
42+
The type of vector `x` in the class definition above is `daecpp::state_vector`, which is an alias of `std::vector<float_type>` type. See [`dae-cpp` types](https://dae-cpp.github.io/prerequisites.html#dae-cpp-types) section for more information.
43+
44+
## Example
45+
46+
In the following example, we define a custom Solution Manager that performs the following 3 tasks:
47+
48+
- writes the element `x[0]` of the solution `x` and time `t` into `std::vector` every time step,
49+
- prints time `t` and `x[0]` on the screen every time step,
50+
- stops computation if `x[0]` becomes negative.
51+
52+
```cpp
53+
class UserDefinedSolutionManager : public daecpp::SolutionManager
54+
{
55+
daecpp::state_vector &m_x_sol; // Vector of solution x[0]
56+
std::vector<double> &m_t_sol; // Vector of the corresponding times t
57+
58+
public:
59+
UserDefinedSolutionManager(daecpp::state_vector &x_sol, std::vector<double> &t_sol)
60+
: m_x_sol(x_sol), m_t_sol(t_sol) {}
61+
62+
int operator()(const daecpp::state_vector &x, const double t)
63+
{
64+
const auto x_0 = x[0]; // Solution element of interest
65+
66+
m_x_sol.push_back(x_0); // Writes x[0] every time step
67+
m_t_sol.push_back(t); // Writes time t every time step
68+
69+
std::cout << t << '\t' << x_0 << '\n'; // Prints t and x[0] on screen
70+
71+
if (x_0 < 0.0)
72+
{
73+
return -1; // Stops computation if x[0] is negative
74+
}
75+
76+
return 0;
77+
}
78+
};
79+
```
80+
81+
{: .warning }
82+
Vectors `x_sol` and `t_sol` should be defined before calling the `UserDefinedSolutionManager` constructor and must live until the end of the computation. They shouldn't be passed to the constructor as temporary objects and must **not** go out of scope before finishing the system solving (otherwise, this will lead to dangling references `&m_x_sol` and `&m_t_sol` in the class definition above).
83+
84+
Similar to the mass matrix, vector function, Jacobian matrix definitions, inhereting the `daecpp::SolutionManager` class is a good practice (it serves as a blueprint), but it is not necessary. The user is allowed to define custom Solution Managers without inhereting `daecpp::SolutionManager`.
1585

1686
## Solution holder
1787

docs/vector-function.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ nav_order: 4
77

88
# Vector Function class
99

10-
Vector function class defines the (nonlinear) vector function (the RHS) $$\mathbf{f}(\mathbf{x}, t)$$ of the DAE system written in the matrix-vector form:
10+
The Vector function class defines the (nonlinear) vector function (the RHS) $$\mathbf{f}(\mathbf{x}, t)$$ of the DAE system written in the matrix-vector form:
1111

1212
$$\mathbf{M}(t) \frac{\mathrm{d}\mathbf{x}}{\mathrm{d}t} = \mathbf{f}(\mathbf{x}, t).$$
1313

@@ -36,6 +36,9 @@ The elements of vectors `x` and `f` can be accessed using square brackets `[]`.
3636
{: .note }
3737
The type of vectors `f` and `x` is `daecpp::state_type`, which is an alias of [`autodiff`](https://autodiff.github.io/)'s type used to perform automatic (algorithmic) differentiation of the vector function `f`. See [`dae-cpp` types](https://dae-cpp.github.io/prerequisites.html#dae-cpp-types) section.
3838

39+
{: .important }
40+
The Vector Function class must not be used to save or post-process the solution vector `x` and time `t`. For this purpose, use the [Solution Manager](solution-manager.html) class.
41+
3942
## Examples
4043

4144
Consider the following vector function $$\mathbf{f}(\mathbf{x}, t)$$, where $$\mathbf{x} = \{x,y,z\}$$, as an example:

0 commit comments

Comments
 (0)