Skip to content

Commit 0e424a1

Browse files
committed
Write vector function page
1 parent 587b125 commit 0e424a1

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

docs/prerequisites.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ using namespace daecpp;
2929
| `daecpp::int_type` | `uint32_t` (default), <br> `uint64_t` if `DAECPP_LONG` is defined | Unsigned integer type, used for sparse matrix indices |
3030
| `daecpp::float_type` | `double` (default), <br> `float` if `DAECPP_SINGLE` is defined | Floating point scalar, used for sparse matrix coefficients |
3131
| `daecpp::state_vector` | `std::vector<float_type>` | State vector, used, for example, to define the initial condition |
32-
| `daecpp::state_type` | `autodiff::VectorXreal` | State vector, used for the [vector function](vector-function.html) definition so that it can be automatically (algorithmically) differentiated using `autodiff` package |
32+
| `daecpp::state_type` | `autodiff::VectorXreal` | State vector, used for the [vector function](vector-function.html) definition so that it can be automatically (algorithmically) differentiated using [`autodiff`](https://autodiff.github.io/) package |

docs/vector-function.md

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

7-
Work in progress
8-
{: .label .label-red }
7+
# Vector Function class
98

10-
# Vector Function (the RHS) class
9+
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:
1110

12-
Text
11+
$$\mathbf{M}(t) \frac{\mathrm{d}\mathbf{x}}{\mathrm{d}t} = \mathbf{f}(\mathbf{x}, t).$$
1312

14-
## Subtitle
13+
The vector function depends on the state vector $$\mathbf{x}$$ and time $$t$$.
1514

16-
Text
15+
## Vector function definition
16+
17+
The main vector function class `daecpp::VectorFunction` is an abstract class that provides an interface to define the vector function $$\mathbf{f}(\mathbf{x}, t)$$.
18+
In order to make use of this class, the user should inherit it and overload the `()` operator:
19+
20+
```cpp
21+
class UserDefinedVectorFunction : public daecpp::VectorFunction
22+
{
23+
public:
24+
void operator()(daecpp::state_type &f, const daecpp::state_type &x, const double t) const
25+
{
26+
// Vector function definition
27+
}
28+
};
29+
```
30+
31+
The operator `()` takes the state vector `x` at time `t` and updates the vector function `f`.
32+
Vector `f` is already pre-allocated with `f.size() == x.size()` and should be used in the function directly (without pre-allocation and `push_back`-like methods).
33+
The elements of vectors `x` and `f` can be accessed using square brackets `[]`.
34+
35+
{: .note }
36+
The type of vectors `f` and `x` is `daecpp::state_type`, which is an [`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.
37+
38+
## Examples
39+
40+
Consider the following vector function $$\mathbf{f}(\mathbf{x}, t)$$, where $$\mathbf{x} = \{x,y,z\}$$, as an example:
41+
42+
$$
43+
\mathbf{f}(\mathbf{x}, t) =
44+
\begin{vmatrix}
45+
z + 1 \\
46+
x^2 + y \\
47+
2t
48+
\end{vmatrix}.
49+
$$
50+
51+
In the code, this vector function can be defined as
52+
53+
```cpp
54+
class UserDefinedVectorFunction : public daecpp::VectorFunction
55+
{
56+
public:
57+
void operator()(daecpp::state_type &f, const daecpp::state_type &x, const double t) const
58+
{
59+
f[0] = x[2] + 1.0; // z + 1
60+
f[1] = x[0] * x[0] + x[1]; // x*x + y
61+
f[2] = 2.0 * t; // 2*t
62+
}
63+
};
64+
```
65+
66+
Inhereting the `daecpp::VectorFunction` class is a good practice (it serves as a blueprint), however, the user is allowed to define vector functions using their own custom classes, for example:
67+
68+
```cpp
69+
struct UserDefinedVectorFunction
70+
{
71+
void operator()(daecpp::state_type &f, const daecpp::state_type &x, const double t)
72+
{
73+
f[0] = x[2] + 1.0; // z + 1
74+
f[1] = x[0] * x[0] + x[1]; // x*x + y
75+
f[2] = 2.0 * t; // 2*t
76+
}
77+
};
78+
```

0 commit comments

Comments
 (0)