-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathSquareDistanceMapping_test.cpp
More file actions
286 lines (223 loc) · 12.4 KB
/
SquareDistanceMapping_test.cpp
File metadata and controls
286 lines (223 loc) · 12.4 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
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
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/component/mapping/nonlinear/SquareDistanceMapping.h>
#include <sofa/component/mapping/testing/MappingTestCreation.h>
#include <sofa/component/topology/container/dynamic/EdgeSetTopologyContainer.h>
#include <sofa/core/behavior/LinearSolver.h>
#include <sofa/simpleapi/SimpleApi.h>
namespace sofa {
namespace {
/** Test suite for SquareDistanceMapping.
*
* @author Matthieu Nesme
*/
template <typename SquareDistanceMapping>
struct SquareDistanceMappingTest : public sofa::mapping_test::Mapping_test<SquareDistanceMapping>
{
typedef typename SquareDistanceMapping::In InDataTypes;
typedef typename InDataTypes::VecCoord InVecCoord;
typedef typename InDataTypes::Coord InCoord;
typedef typename SquareDistanceMapping::Out OutDataTypes;
typedef typename OutDataTypes::VecCoord OutVecCoord;
typedef typename OutDataTypes::Coord OutCoord;
bool test()
{
this->errorMax *= 10;
SquareDistanceMapping* map = static_cast<SquareDistanceMapping*>( this->mapping );
// map->d_computeDistance.setValue(true);
sofa::helper::getWriteAccessor(map->d_geometricStiffness)->setSelectedItem(1);
const component::topology::container::dynamic::EdgeSetTopologyContainer::SPtr edges = sofa::core::objectmodel::New<component::topology::container::dynamic::EdgeSetTopologyContainer>();
this->root->addObject(edges);
edges->addEdge( 0, 1 );
edges->addEdge( 2, 1 );
// parent positions
InVecCoord incoord(3);
InDataTypes::set( incoord[0], 0,0,0 );
InDataTypes::set( incoord[1], 1,1,1 );
InDataTypes::set( incoord[2], 6,3,-1 );
// expected child positions
OutVecCoord expectedoutcoord;
expectedoutcoord.push_back( type::Vec1( 3 ) );
expectedoutcoord.push_back( type::Vec1( 33 ) );
return this->runTest( incoord, expectedoutcoord );
}
// bool test_restLength()
// {
// this->errorMax *= 10;
// SquareDistanceMapping* map = static_cast<SquareDistanceMapping*>( this->mapping );
//// map->d_computeDistance.setValue(true);
// map->d_geometricStiffness.setValue(1);
// type::vector< SReal > restLength(2);
// restLength[0] = .5;
// restLength[1] = 2;
// map->d_restLengths.setValue( restLength );
// component::topology::container::dynamic::EdgeSetTopologyContainer::SPtr edges = modeling::addNew<component::topology::container::dynamic::EdgeSetTopologyContainer>(this->root);
// edges->addEdge( 0, 1 );
// edges->addEdge( 2, 1 );
// // parent positions
// InVecCoord incoord(3);
// InDataTypes::set( incoord[0], 0,0,0 );
// InDataTypes::set( incoord[1], 1,1,1 );
// InDataTypes::set( incoord[2], 6,3,-1 );
// // expected child positions
// OutVecCoord expectedoutcoord;
// expectedoutcoord.push_back( type::Vector1( (sqrt(3.)-.5) * (sqrt(3.)-.5) ) );
// expectedoutcoord.push_back( type::Vector1( (sqrt(33.)-2.) * (sqrt(33.)-2.) ) );
// return this->runTest( incoord, expectedoutcoord );
// }
};
// Define the list of types to instantiate.
using ::testing::Types;
typedef Types<
component::mapping::nonlinear::SquareDistanceMapping<defaulttype::Vec3Types,defaulttype::Vec1Types>
, component::mapping::nonlinear::SquareDistanceMapping<defaulttype::Rigid3Types,defaulttype::Vec1Types>
> DataTypes; // the types to instantiate.
// Test suite for all the instantiations
TYPED_TEST_SUITE( SquareDistanceMappingTest, DataTypes );
// test case
TYPED_TEST( SquareDistanceMappingTest , test )
{
ASSERT_TRUE(this->test());
}
//TYPED_TEST( SquareDistanceMappingTest , test_restLength )
//{
// ASSERT_TRUE(this->test_restLength());
//}
} // namespace
/**
* This test checks two different methods to simulate a triple pendulum using quadratic springs:
* 1) A SquaredDistanceMapping is used to transform the DOFs from the 3d space to a 1d space
* (representing the squared distances between the DOFs). Then a spring acts in the 1d space
* 2) A combination of two mappings: DistanceMapping and SquareMapping. It also transforms the 3d
* space to a 1d space where a spring is added.
*
* Two methods are supposed to lead to the same result. This test checks that both mechanical
* objects are at the same position, velocity and force.
* However, this test can fail easily with different parameters (number of strings, number of time
* steps etc).
*/
struct SquareDistanceMappingCompare_test : NumericTest<SReal>
{
simulation::Node::SPtr root;
simulation::Node::SPtr oneMapping;
simulation::Node::SPtr twoMappings;
void doSetUp() override
{
root = simulation::getSimulation()->createNewNode("root");
simpleapi::createObject(root, "RequiredPlugin", {{"pluginName", Sofa.Component}});
simpleapi::createObject(root, "DefaultAnimationLoop");
simpleapi::createObject(root, "StringMeshCreator", {{"name", "loader"}, {"resolution", "3"}});
oneMapping = simpleapi::createChild(root, "oneMapping");
twoMappings = simpleapi::createChild(root, "twoMappings");
for (const auto& node : {oneMapping, twoMappings})
{
simpleapi::createObject(node, "EulerImplicitSolver", {{"rayleighStiffness", "0.1"}, {"rayleighMass","0.1"}});
simpleapi::createObject(node, "EdgeSetTopologyContainer",
{{"position", "@../loader.position"}, {"edges", "@../loader.edges"}, {"name", "topology"}});
simpleapi::createObject(node, "MechanicalObject", {{"name", "defoDOF"}, {"template", "Vec3"}});
simpleapi::createObject(node, "EdgeSetGeometryAlgorithms");
simpleapi::createObject(node, "FixedProjectiveConstraint", {{"indices", "0"}});
simpleapi::createObject(node, "DiagonalMass", {{"totalMass", "1e-2"}});
}
const auto oneMappingExtension = simpleapi::createChild(oneMapping, "extensionsNode");
const auto twoMappingsExtension = simpleapi::createChild(twoMappings, "extensionsNode");
simpleapi::createObject(oneMappingExtension, "MechanicalObject", {{"name", "extensionsDOF"}, {"template", "Vec1"}});
simpleapi::createObject(twoMappingsExtension, "MechanicalObject", {{"name", "extensionsDOF"}, {"template", "Vec1"}});
simpleapi::createObject(oneMappingExtension, "SquareDistanceMapping",
{{"topology", "@../topology"}, {"input", "@../defoDOF"},
{"output", "@extensionsDOF"}, {"geometricStiffness", "1"},
{"applyRestPosition", "true"}});
simpleapi::createObject(oneMappingExtension, "FixedWeakConstraint", {{"template", "Vec1"}, {"stiffness", "10000"},{"fixAll", "true"}});
simpleapi::createObject(twoMappingsExtension, "DistanceMapping",
{{"topology", "@../topology"}, {"input", "@../defoDOF"},
{"output", "@extensionsDOF"}, {"geometricStiffness", "1"},
{"applyRestPosition", "true"}, {"computeDistance", "true"}});
const auto distanceMappingNode = simpleapi::createChild(twoMappingsExtension, "square");
simpleapi::createObject(distanceMappingNode, "MechanicalObject", {{"name", "squaredDOF"}, {"template", "Vec1"}});
simpleapi::createObject(distanceMappingNode, "SquareMapping",
{{"input", "@../extensionsDOF"},
{"output", "@squaredDOF"}, {"geometricStiffness", "1"},
{"applyRestPosition", "true"}});
simpleapi::createObject(distanceMappingNode, "FixedWeakConstraint", {{"template", "Vec1"}, {"stiffness", "10000"},{"fixAll", "true"}});
}
void compareMechanicalObjects(unsigned int timeStepCount, SReal epsilon)
{
core::behavior::BaseMechanicalState* mstate0 = oneMapping->getMechanicalState();
ASSERT_NE(mstate0, nullptr);
core::behavior::BaseMechanicalState* mstate1 = twoMappings->getMechanicalState();
ASSERT_NE(mstate1, nullptr);
//position
{
sofa::type::vector<SReal> mstatex0(mstate0->getMatrixSize());
mstate0->copyToBuffer(mstatex0.data(), core::vec_id::read_access::position, mstate0->getMatrixSize());
sofa::type::vector<SReal> mstatex1(mstate1->getMatrixSize());
mstate1->copyToBuffer(mstatex1.data(), core::vec_id::read_access::position, mstate1->getMatrixSize());
EXPECT_LT(this->vectorMaxDiff(mstatex0, mstatex1), epsilon) << "Time step " << timeStepCount
<< "\n" << mstatex0 << "\n" << mstatex1;
}
//velocity
{
sofa::type::vector<SReal> mstatev0(mstate0->getMatrixSize());
mstate0->copyToBuffer(mstatev0.data(), core::vec_id::read_access::velocity, mstate0->getMatrixSize());
sofa::type::vector<SReal> mstatev1(mstate1->getMatrixSize());
mstate1->copyToBuffer(mstatev1.data(), core::vec_id::read_access::velocity, mstate1->getMatrixSize());
EXPECT_LT(this->vectorMaxDiff(mstatev0, mstatev1), epsilon) << "Time step " << timeStepCount
<< "\n" << mstatev0 << "\n" << mstatev1;
}
//force
{
sofa::type::vector<SReal> mstatef0(mstate0->getMatrixSize());
mstate0->copyToBuffer(mstatef0.data(), core::vec_id::read_access::force, mstate0->getMatrixSize());
sofa::type::vector<SReal> mstatef1(mstate1->getMatrixSize());
mstate1->copyToBuffer(mstatef1.data(), core::vec_id::read_access::force, mstate1->getMatrixSize());
EXPECT_LT(this->vectorMaxDiff(mstatef0, mstatef1), epsilon) << "Time step " << timeStepCount
<< "\n" << mstatef0 << "\n" << mstatef1;
}
}
};
TEST_F(SquareDistanceMappingCompare_test, compareToDistanceMappingAndSquareMappingCG)
{
for (const auto& node : {oneMapping, twoMappings})
{
simpleapi::createObject(node, "CGLinearSolver", {{"iterations", "1e4"}, {"tolerance", "1.0e-9"}, {"threshold", "1.0e-9"}});
}
sofa::simulation::node::initRoot(root.get());
for (unsigned int i = 0 ; i < 100; ++i)
{
sofa::simulation::node::animate(root.get(), 0.01_sreal);
compareMechanicalObjects(i, 1e-7_sreal);
}
}
TEST_F(SquareDistanceMappingCompare_test, compareToDistanceMappingAndSquareMappingLU)
{
for (const auto& node : {oneMapping, twoMappings})
{
simpleapi::createObject(node, "EigenSparseLU", {{"template", "CompressedRowSparseMatrixMat3x3d"}});
}
sofa::simulation::node::initRoot(root.get());
for (unsigned int i = 0 ; i < 100; ++i)
{
sofa::simulation::node::animate(root.get(), 0.01_sreal);
compareMechanicalObjects(i, 1e-10_sreal);
}
}
} // namespace sofa