Skip to content

Commit 307cebf

Browse files
committed
Write quick-start page
1 parent 3411011 commit 307cebf

File tree

2 files changed

+159
-7
lines changed

2 files changed

+159
-7
lines changed

docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ nav_order: 1.2
1010

1111
- Download the library from `dae-cpp` repository on GitHub: [https://github.com/dae-cpp/dae-cpp](https://github.com/dae-cpp/dae-cpp).
1212
- The solver is header-only, with all dependencies included. There is no need to pre-compile the sources, just copy `dae-cpp`, `Eigen`, and `autodiff` folders into your project.
13-
- Optionally: examples and tests can be compiled using CMake (see [Testing](#testing)).
13+
- *Optionally:* examples and tests can be compiled using CMake (see [Testing](#testing)).
1414

1515
## Testing
1616

docs/quick-start.md

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

7-
Work in progress
8-
{: .label .label-red }
9-
107
# Quick Start
118

12-
Text
9+
## Example
10+
11+
Consider the following (trivial) DAE system as a quick example:
12+
13+
$$
14+
\left\{
15+
\begin{alignedat}{3}
16+
\dot x & = y, \\
17+
y & = \cos(t),
18+
\end{alignedat}
19+
\right.
20+
$$
21+
22+
with the initial condition:
23+
24+
$$
25+
\left\{
26+
\begin{alignedat}{3}
27+
x\rvert_{t=0} & = 0, \\
28+
y\rvert_{t=0} & = 1.
29+
\end{alignedat}
30+
\right.
31+
$$
32+
33+
This system contains one simple differential equation and one algebraic equation. The analytic solution is the following:
34+
35+
$$
36+
\left\{
37+
\begin{alignedat}{3}
38+
x(t) & = \sin(t), \\
39+
y(t) & = \cos(t).
40+
\end{alignedat}
41+
\right.
42+
$$
43+
44+
Below is a simplified procedure of defining and solving the given DAE system using `dae-cpp`.
45+
46+
## Solving the system
47+
48+
### Step 0. Include `dae-cpp` header into the project
49+
50+
```cpp
51+
#include <dae-cpp/solver.hpp>
52+
```
53+
54+
### Step 1. Define the mass matrix of the system
55+
56+
Tha mass matrix contains only one non-zero element:
57+
58+
$$
59+
\mathbf{M} =
60+
\begin{vmatrix}
61+
1 & 0 \\
62+
0 & 0
63+
\end{vmatrix}.
64+
$$
65+
66+
```cpp
67+
struct MyMassMatrix
68+
{
69+
void operator()(sparse_matrix &M, const double t)
70+
{
71+
M(0, 0, 1.0); // Row 0, column 0, non-zero element 1.0
72+
}
73+
};
74+
```
75+
76+
For more information about defining the mass matrix, see [Mass Matrix class](mass-matrix.html) description.
77+
78+
### Step 2. Define the vector function (RHS) of the system
79+
80+
```cpp
81+
struct MyRHS
82+
{
83+
void operator()(state_type &f, const state_type &x, const double t)
84+
{
85+
f[0] = x[1]; // y
86+
f[1] = cos(t) - x[1]; // cos(t) - y
87+
}
88+
};
89+
```
90+
91+
For more information about defining the vector function (RHS) of the system, see [Vector Function class](vector-function.html) description.
92+
93+
### Step 3. Set up the DAE system
94+
95+
```cpp
96+
MyMassMatrix mass; // Mass matrix object
97+
MyRHS rhs; // Vector-function object
98+
99+
System my_system(mass, rhs); // Defines the DAE system object
100+
```
101+
102+
For more information about the `System` class, see [Solving DAE system](solve.html) section.
103+
104+
### Step 4. Solve the system
105+
106+
```cpp
107+
state_vector x0{0, 1}; // The initial state vector (initial condition)
108+
double t{1.0}; // The integration interval: t = [0, 1.0]
109+
110+
my_system.solve(x0, t); // Solves the system with the given initial condition `x0` and time `t`
111+
```
112+
113+
or simply
114+
115+
```cpp
116+
my_system.solve({0, 1}, 1.0);
117+
```
118+
119+
Solution vector of vectors `x` and the corresponding vector of times `t` will be stored in `my_system.sol.x` and `my_system.sol.t`, respectively.
120+
121+
The entire C++ code is provided in the [Quick Start example](https://github.com/dae-cpp/dae-cpp/blob/master/examples/quick_start/quick_start.cpp).
122+
123+
For more information about using `solve` functions, see [Solving DAE system](solve.html) section.
124+
125+
### (Optional) Step 5. Define the Jacobian matrix to boost the computation speed
126+
127+
Differentiating the RHS w.r.t. $$x$$ and $$y$$ gives the following Jacobian matrix:
128+
129+
$$
130+
\mathbf{J} =
131+
\begin{vmatrix}
132+
0 & 1 \\
133+
0 & -1
134+
\end{vmatrix}.
135+
$$
136+
137+
This matrix can be defined in the code as
138+
139+
```cpp
140+
struct MyJacobian
141+
{
142+
void operator()(sparse_matrix &J, const state_vector &x, const double t)
143+
{
144+
J.reserve(2); // Pre-allocates memory for 2 non-zero elements (optional)
145+
J(0, 1, 1.0); // Row 0, column 1, non-zero element 1.0
146+
J(1, 1, -1.0); // Row 1, column 1, non-zero element -1.0
147+
}
148+
};
149+
```
150+
151+
Then add the user-defined Jacobian to the DAE system definition:
152+
153+
```cpp
154+
System my_system(mass, rhs, MyJacobian()); // Defines the DAE system with Jacobian
155+
```
156+
157+
For more information about defining the Jacobian matrix, see [Jacobian Matrix class](jacobian-matrix.html) description.
158+
159+
### (Optional) Step 6. Tweak the solver options
160+
161+
For example, restrict the maximum time step:
13162
14-
## Subtitle
163+
```cpp
164+
my_system.opt.dt_max = 0.1; // Update `dt_max`
165+
my_system.solve({0, 1}, 1.0); // Restart the computation
166+
```
15167

16-
Text
168+
See [Solver Options class](solver-options.html) description for more information.

0 commit comments

Comments
 (0)