-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_solution-manager.cpp
More file actions
250 lines (190 loc) · 5.72 KB
/
Copy pathtest_solution-manager.cpp
File metadata and controls
250 lines (190 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Testing:
* class SolutionManager, SolutionHolder, Solution
*
* This file is part of dae-cpp.
*
* dae-cpp is licensed under the MIT license.
* A copy of the license can be found in the LICENSE file.
*
* Copyright (c) 2024-2025 Ivan Korotkin
*/
#include <dae-cpp/solver.hpp>
#include "gtest/gtest.h"
namespace
{
using namespace daecpp;
TEST(SolutionManager, Default)
{
SolutionManager mgr;
state_vector x(16);
EXPECT_EQ(mgr(x, 10.0), 0);
}
TEST(SolutionManager, Definition)
{
struct TestSolutionManager : SolutionManager
{
int operator()(const state_vector &x, const double t)
{
return 42;
}
};
TestSolutionManager mgr;
state_vector x(16);
EXPECT_EQ(mgr(x, 10.0), 42);
}
TEST(SolutionManager, SolutionHolder)
{
SolutionHolder sol;
ASSERT_EQ(sol.x.size(), 0);
ASSERT_EQ(sol.t.size(), 0);
sol.x.push_back({1.0, 2.0, 3.0});
sol.x.push_back({4.0, 5.0, 6.0});
sol.t.push_back(10.0);
sol.t.push_back(11.0);
ASSERT_EQ(sol.x.size(), 2);
ASSERT_EQ(sol.t.size(), 2);
EXPECT_DOUBLE_EQ(sol.x[0][0], 1.0);
EXPECT_DOUBLE_EQ(sol.x[0][1], 2.0);
EXPECT_DOUBLE_EQ(sol.x[0][2], 3.0);
EXPECT_DOUBLE_EQ(sol.x[1][0], 4.0);
EXPECT_DOUBLE_EQ(sol.x[1][1], 5.0);
EXPECT_DOUBLE_EQ(sol.x[1][2], 6.0);
EXPECT_DOUBLE_EQ(sol.t[0], 10.0);
EXPECT_DOUBLE_EQ(sol.t[1], 11.0);
}
TEST(SolutionManager, SolutionClass)
{
SolutionHolder sol;
Solution sol_obj(sol);
state_vector x = {1.5, 2.5};
sol_obj(x, 10.0);
x = {11.5, 12.5};
sol_obj(x, 11.0);
ASSERT_EQ(sol.x.size(), 2);
ASSERT_EQ(sol.t.size(), 2);
EXPECT_DOUBLE_EQ(sol.x[0][0], 1.5);
EXPECT_DOUBLE_EQ(sol.x[0][1], 2.5);
EXPECT_DOUBLE_EQ(sol.x[1][0], 11.5);
EXPECT_DOUBLE_EQ(sol.x[1][1], 12.5);
EXPECT_DOUBLE_EQ(sol.t[0], 10.0);
EXPECT_DOUBLE_EQ(sol.t[1], 11.0);
}
TEST(SolutionManager, SolutionClassList)
{
SolutionHolder sol;
Solution sol_obj(sol, {1.0, 2.0, 12.0, 1.0, 0.0, -5.0, 11.0, 11.0, 1e9}); // Duplicates and out of range values
state_vector x = {1.5, 2.5};
sol_obj(x, 10.0);
x = {11.5, 12.5};
sol_obj(x, 11.0);
x = {25.0, 35.0};
sol_obj(x, 12.0);
// Only two values should be written
ASSERT_EQ(sol.x.size(), 2);
ASSERT_EQ(sol.t.size(), 2);
EXPECT_DOUBLE_EQ(sol.x[0][0], 11.5);
EXPECT_DOUBLE_EQ(sol.x[0][1], 12.5);
EXPECT_DOUBLE_EQ(sol.x[1][0], 25.0);
EXPECT_DOUBLE_EQ(sol.x[1][1], 35.0);
EXPECT_DOUBLE_EQ(sol.t[0], 11.0);
EXPECT_DOUBLE_EQ(sol.t[1], 12.0);
}
struct MyRHS
{
void operator()(state_type &f, const state_type &x, const double t)
{
f[0] = -1; // dx/dt = -1
}
};
// Absolute error
constexpr double abs_err{1e-6};
class MySolutionManager
{
SolutionHolder &m_sol;
bool m_keep_reducing_time_step{false};
void m_save_solution(const state_vector &x, const double t)
{
m_sol.x.emplace_back(x);
m_sol.t.emplace_back(t);
}
public:
MySolutionManager(SolutionHolder &sol) : m_sol(sol) {}
/*
* Solution Manager functor will be called every time step providing the time `t` and
* the corresponding solution `x` for further post-processing.
*/
int operator()(const state_vector &x, const double t)
{
if (std::abs(x[0] - 1.0) < abs_err)
{
m_save_solution(x, t);
return solver_command::stop_integration;
}
if (x[0] < 1.0)
{
m_keep_reducing_time_step = true;
return solver_command::decrease_time_step_and_redo;
}
m_save_solution(x, t);
if (m_keep_reducing_time_step)
{
return solver_command::decrease_time_step;
}
return solver_command::continue_integration;
}
};
TEST(SolutionManager, SolverCommands)
{
MyRHS rhs; // The vector-function object
state_vector x0{2.0}; // Initial condition: x = 2
double t_end{100.0}; // Solution interval: t = [0, t_end] - should stop earlier
SolutionHolder sol;
auto status = solve(MassMatrixIdentity(x0.size()), rhs, x0, t_end, MySolutionManager(sol));
ASSERT_EQ(status, 0);
ASSERT_GT(sol.x.size(), 0);
ASSERT_GT(sol.t.size(), 0);
EXPECT_GT(sol.t.back(), 0.0);
EXPECT_NEAR(sol.x.back()[0], 1.0, abs_err); // Should stop at x = 1.0
EXPECT_NEAR(sol.t.back(), 1.0, abs_err); // Should stop at t = 1.0
}
class MySolutionManagerStop
{
SolutionHolder &m_sol;
void m_save_solution(const state_vector &x, const double t)
{
m_sol.x.emplace_back(x);
m_sol.t.emplace_back(t);
}
public:
MySolutionManagerStop(SolutionHolder &sol) : m_sol(sol) {}
/*
* Solution Manager functor will be called every time step providing the time `t` and
* the corresponding solution `x` for further post-processing.
*/
int operator()(const state_vector &x, const double t)
{
m_save_solution(x, t);
if (x[0] < 0.0)
{
return -1; // solver_command::stop_integration;
}
return 0; // solver_command::continue_integration;
}
};
TEST(SolutionManager, StopIntegration)
{
MyRHS rhs; // The vector-function object
state_vector x0{2.0}; // Initial condition: x = 2
double t_end{100.0}; // Solution interval: t = [0, t_end] - should stop earlier
SolutionHolder sol;
auto status = solve(MassMatrixIdentity(x0.size()), rhs, x0, t_end, MySolutionManagerStop(sol));
ASSERT_EQ(status, 0);
ASSERT_GT(sol.x.size(), 0);
ASSERT_GT(sol.t.size(), 0);
EXPECT_GT(sol.t.back(), 2.0);
EXPECT_LT(sol.t.back(), 3.0);
EXPECT_LT(sol.x.back()[0], 0.0); // Should stop after `x` passes 0.0
EXPECT_GT(sol.x.back()[0], -1.0); // But before `x` passes -1.0
}
} // namespace