forked from FloatingArrayDesign/MoorDyn
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmath_tests.cpp
More file actions
212 lines (172 loc) · 5.59 KB
/
math_tests.cpp
File metadata and controls
212 lines (172 loc) · 5.59 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
#include <iostream>
#include <sstream>
#include "Misc.hpp"
#include <catch2/catch_test_macros.hpp>
#include "catch2/catch_tostring.hpp"
#include "catch2/matchers/catch_matchers_templated.hpp"
#include "util.h"
namespace Catch {
template<typename T, int N>
struct StringMaker<Eigen::Vector<T, N>>
{
static std::string convert(const Eigen::Vector<T, N>& value)
{
Eigen::IOFormat testFmt(4, Eigen::DontAlignCols, ", ", "\n", "[", "]");
std::stringstream ss;
ss << (value.transpose()).format(testFmt);
return ss.str();
}
};
}
template<typename DerivedA>
struct IsCloseMatcher : Catch::Matchers::MatcherGenericBase
{
IsCloseMatcher(
const DerivedA& a,
const typename DerivedA::RealScalar rtol =
Eigen::NumTraits<typename DerivedA::RealScalar>::dummy_precision(),
const typename DerivedA::RealScalar atol =
Eigen::NumTraits<typename DerivedA::RealScalar>::epsilon())
: _a(a)
, _rtol(rtol)
, _atol(atol)
{
}
template<typename DerivedB>
bool match(const DerivedB& b) const
{
return ((_a.derived() - b.derived()).array().abs() <=
(_atol + _rtol * b.derived().array().abs()))
.all();
}
std::string describe() const override
{
std::stringstream ss;
ss << "Is close to: " << Catch::StringMaker<DerivedA>::convert(_a)
<< "\nrtol = " << _rtol << ", atol = " << _atol;
return ss.str();
}
private:
const DerivedA _a;
const typename DerivedA::RealScalar _rtol;
const typename DerivedA::RealScalar _atol;
};
template<typename T>
IsCloseMatcher<T>
IsClose(T value)
{
return IsCloseMatcher<T>(value, 1e-10, 1e-12);
}
// md::real is faster to type
namespace md = moordyn;
using namespace md;
TEST_CASE("getH gives the cross product matrix")
{
vec testVec{ 1.0, 2.0, 3.0 };
vec v{ 0.3, 0.2, 0.1 };
// getH() should create a matrix that replicates the behavior of the cross
// product such that getH(v) * a == -v.cross(a)
vec ref = v.cross(-testVec);
REQUIRE_THAT(getH(v) * testVec, IsClose(ref));
}
TEST_CASE("translateMass linear acceleration")
{
/**
* This test imagines that we have some point whose center of mass
* is 1 meter in the x direction away from our reference point.
*
* A force applied in the y direction through this center of mass should
* result in no rotation and a acceleration in the y direction according to
* F = ma
*
* In our local coordinate system, this force will result in a torque.
*
* The mass matrix produced by translateMass should be such that we can
* correctly predict the acceleration in this situation
*
*/
md::real m = 1.0;
mat mass = m * mat::Identity();
mat sphereI = ((2.0 / 5.0) * m) * mat::Identity();
vec offset{ 1.0, 1.0, 1.0 };
mat6 M6 = translateMass(offset, mass);
mat sphereIRef = sphereI - mass * getH(offset) * getH(offset);
M6.bottomRightCorner<3, 3>() += sphereIRef;
vec6 F = vec6::Zero();
vec f3{ 0, 10, 0 };
F.head<3>() = f3;
F.tail<3>() = offset.cross(f3);
// std::cout << "F = " << F.transpose() << std::endl;
// std::cout << "M6 = \n" << M6 << std::endl;
vec6 acc = solveMat6(M6, F);
// linear acceleration by F = ma
// no angular acceleration
vec6 expectedAcc = vec6::Zero();
expectedAcc.head<3>() = f3 / m;
REQUIRE_THAT(acc, IsClose(expectedAcc));
}
TEST_CASE("translateMass6 linear acceleration")
{
/**
* Like the testTranslateMass test except we do a series of two offsets
* We verify both that the acceleration is computed correctly,
* and that the mass matrix we get matched what we get by translating
* the mass by both offsets simultaneously.
*/
md::real m = 1.0;
mat mass = m * mat::Identity();
mat6 mass6 = mat6::Zero();
mass6.topLeftCorner<3, 3>() = mass;
vec offset{ 1.0, 0.0, 0.0 };
vec offset2{ 2, 1, 0.2 };
mat6 M6 = translateMass(offset, mass);
M6 = translateMass6(offset2, M6);
REQUIRE_THAT(translateMass((offset + offset2), mass), IsClose(M6));
// we add some moment of inertia to prevent a singular matrix.
// presume it's a sphere with radius 1
mat sphereI = ((2.0 / 5.0) * m) * mat::Identity();
mat sphereIRef =
sphereI - mass * getH(offset + offset2) * getH(offset + offset2);
M6.bottomRightCorner<3, 3>() += sphereIRef;
vec6 F = vec6::Zero();
vec f3{ 0, 10, 0 };
F.head<3>() = f3;
F.tail<3>() = (offset + offset2).cross(f3);
// std::cout << "F = " << F.transpose() << std::endl;
// std::cout << "M6 = \n" << M6 << std::endl;
// std::cout << "det(M6) = " << M6.determinant() << std::endl;
vec6 acc = solveMat6(M6, F);
// linear acceleration by F = ma
// no angular acceleration
vec6 expectedAcc = vec6::Zero();
expectedAcc.head<3>() = f3 / m;
REQUIRE_THAT(acc, IsClose(expectedAcc));
}
TEST_CASE("rotateMass simple")
{
md::real m = 1.0;
mat mass = m * mat::Identity();
mat sphereI = ((2.0 / 5.0) * m) * mat::Identity();
vec offset{ 1.0, 1.0, 0.0 };
mat6 M6 = translateMass(offset, mass);
vec3 axis{ 0, 0, 1 };
axis.normalize();
// rotate -90 degrees around the z axis
Eigen::AngleAxisd rot(-pi / 2, axis);
mat6 rotatedMass = rotateMass6(rot.toRotationMatrix(), M6);
// this offset represents the offset after rotation
vec newOffset{ 1, -1, 0.0 };
REQUIRE_THAT(rot.toRotationMatrix() * offset, IsClose(newOffset));
REQUIRE_THAT(translateMass(newOffset, mass), IsClose(rotatedMass));
// add some moment of inertia to prevent singular mass matrix
mat sphereIRef = sphereI - mass * getH(newOffset) * getH(newOffset);
rotatedMass.bottomRightCorner<3, 3>() += sphereIRef;
vec6 F = vec6::Zero();
vec f3{ 0, 10, 0 };
F.head<3>() = f3;
F.tail<3>() = newOffset.cross(f3);
vec6 acc = solveMat6(rotatedMass, F);
vec6 expectedAcc = vec6::Zero();
expectedAcc.head<3>() = f3 / m;
REQUIRE_THAT(acc, IsClose(expectedAcc));
}