-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
hermitian_dense_output_test.cc
368 lines (327 loc) · 15.8 KB
/
hermitian_dense_output_test.cc
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "drake/systems/analysis/hermitian_dense_output.h"
#include <gtest/gtest.h>
#include "drake/common/eigen_types.h"
#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/common/test_utilities/expect_no_throw.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/common/trajectories/piecewise_polynomial.h"
namespace drake {
namespace systems {
namespace analysis {
namespace {
template <typename T>
class HermitianDenseOutputTest : public ::testing::Test {
protected:
const double kInvalidTime{-1.0};
const double kInitialTime{0.0};
const double kMidTime{0.5};
const double kFinalTime{1.0};
const double kTimeStep{0.1};
const MatrixX<double> kInitialState{
(MatrixX<double>(3, 1) << 0., 0., 0.).finished()};
const MatrixX<double> kMidState{
(MatrixX<double>(3, 1) << 0.5, 5., 50.).finished()};
const MatrixX<double> kFinalState{
(MatrixX<double>(3, 1) << 1., 10., 100.).finished()};
const MatrixX<double> kFinalStateWithFewerDimensions{
(MatrixX<double>(2, 1) << 1., 10.).finished()};
const MatrixX<double> kFinalStateWithMoreDimensions{
(MatrixX<double>(4, 1) << 1., 10., 100., 1000.).finished()};
const MatrixX<double> kFinalStateNotAVector{
(MatrixX<double>(2, 2) << 1., 10., 100., 1000.).finished()};
const MatrixX<double> kInitialStateDerivative{
(MatrixX<double>(3, 1) << 0., 1., 0.).finished()};
const MatrixX<double> kMidStateDerivative{
(MatrixX<double>(3, 1) << 0.5, 0.5, 0.5).finished()};
const MatrixX<double> kFinalStateDerivative{
(MatrixX<double>(3, 1) << 1., 0., 1.).finished()};
const MatrixX<double> kFinalStateDerivativeWithFewerDimensions{
(MatrixX<double>(2, 1) << 1., 0.).finished()};
const MatrixX<double> kFinalStateDerivativeWithMoreDimensions{
(MatrixX<double>(4, 1) << 1., 0., 1., 0.).finished()};
const MatrixX<double> kFinalStateDerivativeNotAVector{
(MatrixX<double>(2, 2) << 0., 1., 0., 1.).finished()};
const int kValidElementIndex{0};
const int kInvalidElementIndex{10};
const std::string kEmptyOutputErrorMessage{
".*[Dd]ense output.*empty.*"};
const std::string kZeroLengthStepErrorMessage{
".*step has zero length.*"};
const std::string kCannotRollbackErrorMessage{
"No updates to rollback."};
const std::string kCannotConsolidateErrorMessage{
"No updates to consolidate."};
const std::string kInvalidElementIndexErrorMessage{
".*out of.*dense output.*range.*"};
const std::string kInvalidTimeErrorMessage{
".*[Tt]ime.*out of.*dense output.*domain.*"};
const std::string kTimeMismatchErrorMessage{
".*start time.*end time.*differ.*"};
const std::string kStateMismatchErrorMessage{
".*start state.*end state.*differ.*"};
const std::string kStateDerivativeMismatchErrorMessage{
".*start state derivative.*end state derivative.*differ.*"};
const std::string kStepBackwardsErrorMessage{
".*cannot be extended backwards.*"};
const std::string kDimensionMismatchErrorMessage{
".*dimensions do not match.*"};
const std::string kNotAVectorErrorMessage{".*not a column matrix.*"};
};
// HermitianDenseOutput types to test.
typedef ::testing::Types<double, AutoDiffXd> OutputTypes;
TYPED_TEST_SUITE(HermitianDenseOutputTest, OutputTypes);
// Checks that HermitianDenseOutput consistency is ensured.
TYPED_TEST(HermitianDenseOutputTest, OutputConsistency) {
// Instantiates dense output.
HermitianDenseOutput<TypeParam> dense_output;
// Verifies that the dense output is empty and API behavior
// is consistent with that fact.
ASSERT_TRUE(dense_output.is_empty());
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.Evaluate(this->kInitialTime),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.EvaluateNth(
this->kInitialTime, this->kValidElementIndex),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.start_time(),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.end_time(),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.size(),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.Rollback(),
this->kCannotRollbackErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.Consolidate(),
this->kCannotConsolidateErrorMessage);
// Verifies that trying to update the dense output with
// a zero length step fails.
typename HermitianDenseOutput<TypeParam>::IntegrationStep first_step(
this->kInitialTime, this->kInitialState, this->kInitialStateDerivative);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.Update(first_step),
this->kZeroLengthStepErrorMessage);
// Verifies that trying to update the dense output with
// a valid step succeeds.
first_step.Extend(this->kMidTime, this->kMidState,
this->kMidStateDerivative);
dense_output.Update(first_step);
// Verifies that an update does not imply a consolidation and thus
// the dense output remains empty.
ASSERT_TRUE(dense_output.is_empty());
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.Evaluate(this->kMidTime),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.EvaluateNth(this->kMidTime, this->kValidElementIndex),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.start_time(),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.end_time(),
this->kEmptyOutputErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.size(),
this->kEmptyOutputErrorMessage);
// Consolidates all previous updates.
dense_output.Consolidate();
// Verifies that it is not possible to roll back updates after consolidation.
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.Rollback(),
this->kCannotRollbackErrorMessage);
// Verifies that it is not possible to consolidate again.
DRAKE_EXPECT_THROWS_MESSAGE(dense_output.Consolidate(),
this->kCannotConsolidateErrorMessage);
// Verifies that the dense output is not empty and that it
// reflects the data provided on updates.
ASSERT_FALSE(dense_output.is_empty());
EXPECT_EQ(dense_output.start_time(), first_step.start_time());
EXPECT_EQ(dense_output.end_time(), first_step.end_time());
EXPECT_EQ(dense_output.size(), first_step.size());
DRAKE_EXPECT_NO_THROW(dense_output.Evaluate(this->kMidTime));
DRAKE_EXPECT_NO_THROW(dense_output.EvaluateNth(
this->kMidTime, this->kValidElementIndex));
// Verifies that invalid evaluation arguments generate errors.
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.EvaluateNth(this->kMidTime, this->kInvalidElementIndex),
this->kInvalidElementIndexErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.EvaluateNth(this->kInvalidTime, this->kValidElementIndex),
this->kInvalidTimeErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.Evaluate(this->kInvalidTime),
this->kInvalidTimeErrorMessage);
// Verifies that step updates that would disrupt the output continuity
// fail.
typename HermitianDenseOutput<TypeParam>::IntegrationStep second_step(
(this->kFinalTime + this->kMidTime) / 2.,
this->kMidState, this->kMidStateDerivative);
second_step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivative);
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.Update(second_step),
this->kTimeMismatchErrorMessage);
typename HermitianDenseOutput<TypeParam>::IntegrationStep third_step(
this->kMidTime, this->kMidState * 2., this->kMidStateDerivative);
third_step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivative);
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.Update(third_step),
this->kStateMismatchErrorMessage);
typename HermitianDenseOutput<TypeParam>::IntegrationStep fourth_step(
this->kMidTime, this->kMidState, this->kMidStateDerivative * 2.);
fourth_step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivative);
DRAKE_EXPECT_THROWS_MESSAGE(
dense_output.Update(fourth_step),
this->kStateDerivativeMismatchErrorMessage);
}
// Checks that HermitianDenseOutput::Step consistency is ensured.
TYPED_TEST(HermitianDenseOutputTest, StepsConsistency) {
// Verifies that zero length steps are properly constructed.
typename HermitianDenseOutput<TypeParam>::IntegrationStep step(
this->kInitialTime, this->kInitialState, this->kInitialStateDerivative);
ASSERT_EQ(step.get_times().size(), 1);
EXPECT_EQ(step.start_time(), this->kInitialTime);
EXPECT_EQ(step.end_time(), this->kInitialTime);
EXPECT_EQ(step.size(), this->kInitialState.rows());
ASSERT_EQ(step.get_states().size(), 1);
EXPECT_TRUE(CompareMatrices(step.get_states().front(), this->kInitialState));
ASSERT_EQ(step.get_state_derivatives().size(), 1);
EXPECT_TRUE(CompareMatrices(step.get_state_derivatives().front(),
this->kInitialStateDerivative));
// Verifies that any attempt to break step consistency fails.
DRAKE_EXPECT_THROWS_MESSAGE(
step.Extend(this->kInvalidTime, this->kFinalState,
this->kFinalStateDerivative),
this->kStepBackwardsErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
step.Extend(this->kFinalTime, this->kFinalStateWithFewerDimensions,
this->kFinalStateDerivative),
this->kDimensionMismatchErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
step.Extend(this->kFinalTime, this->kFinalStateWithMoreDimensions,
this->kFinalStateDerivative),
this->kDimensionMismatchErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
step.Extend(this->kFinalTime, this->kFinalStateNotAVector,
this->kFinalStateDerivative),
this->kNotAVectorErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivativeWithFewerDimensions),
this->kDimensionMismatchErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivativeWithMoreDimensions),
this->kDimensionMismatchErrorMessage);
DRAKE_EXPECT_THROWS_MESSAGE(
step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivativeNotAVector),
this->kNotAVectorErrorMessage);
// Extends the step with appropriate values.
step.Extend(this->kFinalTime, this->kFinalState, this->kFinalStateDerivative);
// Verifies that the step was properly extended.
EXPECT_EQ(step.get_times().size(), 2);
EXPECT_EQ(step.start_time(), this->kInitialTime);
EXPECT_EQ(step.end_time(), this->kFinalTime);
EXPECT_EQ(step.size(), this->kInitialState.rows());
EXPECT_EQ(step.get_states().size(), 2);
EXPECT_TRUE(CompareMatrices(step.get_states().back(), this->kFinalState));
EXPECT_EQ(step.get_state_derivatives().size(), 2);
EXPECT_TRUE(CompareMatrices(step.get_state_derivatives().back(),
this->kFinalStateDerivative));
}
// Checks that HermitianDenseOutput properly supports stepwise
// construction.
TYPED_TEST(HermitianDenseOutputTest, CorrectConstruction) {
// Instantiates dense output.
HermitianDenseOutput<TypeParam> dense_output;
// Updates output for the first time.
typename HermitianDenseOutput<TypeParam>::IntegrationStep first_step(
this->kInitialTime, this->kInitialState, this->kInitialStateDerivative);
first_step.Extend(this->kMidTime, this->kMidState, this->kMidStateDerivative);
dense_output.Update(first_step);
// Updates output a second time.
typename HermitianDenseOutput<TypeParam>::IntegrationStep second_step(
this->kMidTime, this->kMidState, this->kMidStateDerivative);
second_step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivative);
dense_output.Update(second_step);
// Rolls back the last update.
dense_output.Rollback(); // `second_step`
// Consolidates existing updates.
dense_output.Consolidate(); // only `first_step`
// Verifies that the dense output only reflects the first step.
EXPECT_FALSE(dense_output.is_empty());
EXPECT_EQ(dense_output.start_time(), first_step.start_time());
EXPECT_EQ(dense_output.end_time(), first_step.end_time());
EXPECT_EQ(dense_output.size(), first_step.size());
EXPECT_TRUE(CompareMatrices(dense_output.Evaluate(this->kInitialTime),
first_step.get_states().front()));
EXPECT_TRUE(CompareMatrices(dense_output.Evaluate(this->kMidTime),
first_step.get_states().back()));
}
// Checks that HermitianDenseOutput properly implements and evaluates
// an Hermite interpolator.
TYPED_TEST(HermitianDenseOutputTest, CorrectEvaluation) {
// Creates an Hermite cubic spline with times, states and state
// derivatives.
const std::vector<double> spline_times{
this->kInitialTime, this->kMidTime, this->kFinalTime};
const std::vector<MatrixX<double>> spline_states{
this->kInitialState, this->kMidState, this->kFinalState};
const std::vector<MatrixX<double>> spline_state_derivatives{
this->kInitialStateDerivative, this->kMidStateDerivative,
this->kFinalStateDerivative};
const trajectories::PiecewisePolynomial<double> hermite_spline =
trajectories::PiecewisePolynomial<double>::CubicHermite(
spline_times, spline_states, spline_state_derivatives);
// Instantiates dense output.
HermitianDenseOutput<TypeParam> dense_output;
// Updates output for the first time.
typename HermitianDenseOutput<TypeParam>::IntegrationStep first_step(
this->kInitialTime, this->kInitialState, this->kInitialStateDerivative);
first_step.Extend(this->kMidTime, this->kMidState, this->kMidStateDerivative);
dense_output.Update(first_step);
// Updates output a second time.
typename HermitianDenseOutput<TypeParam>::IntegrationStep second_step(
this->kMidTime, this->kMidState, this->kMidStateDerivative);
second_step.Extend(this->kFinalTime, this->kFinalState,
this->kFinalStateDerivative);
dense_output.Update(second_step);
// Consolidates all previous updates.
dense_output.Consolidate();
// Verifies that dense output and Hermite spline match.
const double kAccuracy{1e-12};
EXPECT_FALSE(dense_output.is_empty());
for (TypeParam t = this->kInitialTime;
t <= this->kFinalTime; t += this->kTimeStep) {
const MatrixX<double> matrix_value =
hermite_spline.value(ExtractDoubleOrThrow(t));
const VectorX<TypeParam> vector_value =
matrix_value.col(0).template cast<TypeParam>();
EXPECT_TRUE(CompareMatrices(dense_output.Evaluate(t),
vector_value, kAccuracy));
}
}
// Construct a HermitianDenseOutput<T> from PiecewisePolynomial<U>.
template <typename T>
void TestScalarType() {
const Vector3<T> breaks(0.0, 1.0, 2.0);
const RowVector3<T> samples(6.0, 5.0, 4.0);
const auto foh =
trajectories::PiecewisePolynomial<T>::FirstOrderHold(breaks, samples);
const HermitianDenseOutput<T> hdo(foh);
EXPECT_EQ(ExtractDoubleOrThrow(hdo.start_time()),
ExtractDoubleOrThrow(breaks(0)));
EXPECT_EQ(ExtractDoubleOrThrow(hdo.end_time()),
ExtractDoubleOrThrow(breaks(2)));
for (const T& time : {T(0.1), T(0.4), T(1.6)}) {
EXPECT_NEAR(ExtractDoubleOrThrow(hdo.Evaluate(time)(0)),
ExtractDoubleOrThrow(foh.value(time)(0)), 1e-14);
}
}
GTEST_TEST(HermitianDenseOutputTest, ConstructFromPiecewisePolynomialTest) {
TestScalarType<double>();
TestScalarType<AutoDiffXd>();
TestScalarType<symbolic::Expression>();
}
} // namespace
} // namespace analysis
} // namespace systems
} // namespace drake