forked from sofa-framework/BeamAdapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptiveBeamForceFieldAndMassTest.cpp
More file actions
226 lines (182 loc) · 9.52 KB
/
AdaptiveBeamForceFieldAndMassTest.cpp
File metadata and controls
226 lines (182 loc) · 9.52 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
/******************************************************************************
* BeamAdapter plugin *
* (c) 2006 Inria, University of Lille, CNRS *
* *
* 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: see Authors.md *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/testing/BaseSimulationTest.h>
#include <BeamAdapter/component/forcefield/AdaptiveBeamForceFieldAndMass.h>
#include <sofa/component/statecontainer/MechanicalObject.h>
#include <sofa/simulation/common/SceneLoaderXML.h>
#include <sofa/helper/system/thread/CTime.h>
#include <string>
using std::string;
namespace sofa
{
using sofa::component::forcefield::AdaptiveBeamForceFieldAndMass;
using sofa::component::statecontainer::MechanicalObject;
using sofa::helper::system::thread::ctime_t;
using sofa::simulation::Node;
using sofa::simulation::SceneLoaderXML;
struct AdaptiveBeamForceFieldAndMassTest : public sofa::testing::BaseSimulationTest
{
using Rigid3dTypes = defaulttype::Rigid3dTypes;
using VecCoord = MechanicalObject<Rigid3dTypes>::VecCoord;
void simpleSceneTest(){
string scene =
"<?xml version='1.0'?>"
"<Node name='Root' gravity='0 0 0' time='0' animate='0'>"
" <RequiredPlugin name='Sofa.Component.ODESolver.Backward' />"
" <RequiredPlugin name='Sofa.Component.LinearSolver.Iterative' />"
" <RequiredPlugin name='Sofa.Component.StateContainer' />"
" <RequiredPlugin name='Sofa.Component.Constraint.Projective' />"
" <RequiredPlugin name='Sofa.Component.Topology.Container.Constant' />"
" <RequiredPlugin name='BeamAdapter' />"
" <EulerImplicitSolver rayleighStiffness='0.08' rayleighMass='0.08' printLog='false' />"
" <CGLinearSolver iterations='100' threshold='1e-10' tolerance='1e-15' />"
" <MeshTopology name='meshSuture' edges='0 1' />"
" <MechanicalObject template='Rigid3d' name='DOFs' showIndices='0' position='0 0 0 0 0 0 1 1 0 0 0 0 0 1'/>"
" <BeamInterpolation name='Interpol' radius='0.1'/>"
" <AdaptiveBeamForceFieldAndMass name='ForceField' interpolation='@Interpol' massDensity='1.0'/>"
" <FixedConstraint indices='0' />"
"</Node> " ;
Node::SPtr root = SceneLoaderXML::loadFromMemory ( "test1", scene.c_str());
ASSERT_NE(root.get(), nullptr);
MechanicalObject<Rigid3dTypes>* mechanicalObject = nullptr;
root->getTreeObject(mechanicalObject);
ASSERT_NE(mechanicalObject, nullptr);
EXPECT_TRUE(mechanicalObject->getName() == "DOFs") ;
AdaptiveBeamForceFieldAndMass<Rigid3dTypes>* beamForceFieldMass = nullptr;
root->getTreeObject(beamForceFieldMass);
ASSERT_NE(beamForceFieldMass, nullptr);
ASSERT_NO_THROW(beamForceFieldMass->init());
ASSERT_NO_THROW(beamForceFieldMass->reinit());
}
Node::SPtr createSingleBeam()
{
string scene =
"<?xml version='1.0'?>"
"<Node name='Root' gravity='0 -9.81 0' dt='0.01'>"
" <RequiredPlugin name='Sofa.Component.ODESolver.Backward' />"
" <RequiredPlugin name='Sofa.Component.LinearSolver.Direct' />"
" <RequiredPlugin name='Sofa.Component.Constraint.Projective' />"
" <RequiredPlugin name='Sofa.Component.StateContainer' />"
" <RequiredPlugin name='Sofa.Component.Topology.Container.Constant' />"
" <RequiredPlugin name='Sofa.Component.Topology.Container.Grid' />"
" <RequiredPlugin name='BeamAdapter' />"
" <DefaultAnimationLoop />"
" <DefaultVisualManagerLoop />"
" <Node name='BeamModel'/>"
" <EulerImplicitSolver rayleighStiffness='0.0' rayleighMass='0.0' />"
" <BTDLinearSolver />"
" <RegularGridTopology name='MeshLines' nx='200' ny='1' nz='1' xmax='100' xmin='0' ymin='0' ymax='0' zmax='0' zmin='0'/>"
" <MechanicalObject template='Rigid3d' name='DOFs' />"
" <FixedConstraint indices='0' />"
" <BeamInterpolation name='Interpol' radius='0.1'/>"
" <AdaptiveBeamForceFieldAndMass name='ForceField' interpolation='@Interpol' massDensity='10.0'/>"
" </Node> "
"</Node> ";
Node::SPtr root = SceneLoaderXML::loadFromMemory("singleBeam", scene.c_str());
sofa::simulation::node::initRoot(root.get());
return root;
}
void checkCreation()
{
Node::SPtr root = createSingleBeam();
// Search for Beam FF
AdaptiveBeamForceFieldAndMass<Rigid3dTypes>* beamForceFieldMass = nullptr;
root->getTreeObject(beamForceFieldMass);
ASSERT_NE(beamForceFieldMass, nullptr);
// Check component state and Data default values
ASSERT_EQ(beamForceFieldMass->d_componentState.getValue(), sofa::core::objectmodel::ComponentState::Valid);
ASSERT_EQ(beamForceFieldMass->d_computeMass.getValue(), true);
const auto& massDensity = helper::getReadAccessor(beamForceFieldMass->d_massDensity);
ASSERT_EQ(massDensity.size(), 199);
for (const auto& m: massDensity)
ASSERT_FLOAT_EQ(m, 10.0);
ASSERT_FLOAT_EQ(beamForceFieldMass->m_defaultMassDensity, 10.0);
ASSERT_FLOAT_EQ(beamForceFieldMass->rayleighMass.getValue(), 0.0);
ASSERT_FLOAT_EQ(beamForceFieldMass->rayleighStiffness.getValue(), 0.0);
}
void checkValues()
{
int nbrStep = 500;
int nbrGrid = 200;
Node::SPtr root = createSingleBeam();
// Search for Beam FF
AdaptiveBeamForceFieldAndMass<Rigid3dTypes>* beamForceFieldMass = nullptr;
root->getTreeObject(beamForceFieldMass);
ASSERT_NE(beamForceFieldMass, nullptr);
// Access mstate
MechanicalObject<Rigid3dTypes>* dofs = nullptr;
root->getTreeObject(dofs);
// Access dofs
const VecCoord& positions = dofs->x.getValue();
ASSERT_EQ(positions.size(), nbrGrid);
// Check position at init
auto id = nbrGrid - 1;
EXPECT_NEAR(positions[id][0], 100, 1e-4);
EXPECT_NEAR(positions[id][1], 0, 1e-4);
EXPECT_NEAR(positions[id][2], 0, 1e-4);
// run some simulation steps
for (int i = 0; i < nbrStep; i++)
{
sofa::simulation::node::animate(root.get(), 0.01);
}
// Check position after simulation
EXPECT_NEAR(positions[id][0], -1.34228, 1e-4);
EXPECT_NEAR(positions[id][1], -110.274221, 1e-4);
EXPECT_NEAR(positions[id][2], 0, 1e-4);
}
void testPerformances()
{
Node::SPtr root = createSingleBeam();
int nbrStep = 1000;
int nbrTest = 10;
double timeMin = std::numeric_limits<double>::max();
double timeMax = std::numeric_limits<double>::min();
for (int i = 0; i < nbrTest; ++i)
{
ctime_t startTime = sofa::helper::system::thread::CTime::getRefTime();
for (int i = 0; i < nbrStep; i++)
{
sofa::simulation::node::animate(root.get(), 0.01);
}
ctime_t diffTime = sofa::helper::system::thread::CTime::getRefTime() - startTime;
double diffTimed = sofa::helper::system::thread::CTime::toSecond(diffTime);
if (timeMin > diffTimed)
timeMin = diffTimed;
if (timeMax < diffTimed)
timeMax = diffTimed;
sofa::simulation::node::reset(root.get());
}
}
};
TEST_F(AdaptiveBeamForceFieldAndMassTest, SimpleScene) {
ASSERT_NO_THROW(this->simpleSceneTest()) ;
}
TEST_F(AdaptiveBeamForceFieldAndMassTest, checkCreation) {
ASSERT_NO_THROW(this->checkCreation());
}
TEST_F(AdaptiveBeamForceFieldAndMassTest, checkValues) {
ASSERT_NO_THROW(this->checkValues());
}
TEST_F(AdaptiveBeamForceFieldAndMassTest, DISABLED_testPerformances) {
ASSERT_NO_THROW(this->testPerformances());
}
}