1 /* Siconos is a program dedicated to modeling, simulation and control
2  * of non smooth dynamical systems.
3  *
4  * Copyright 2021 INRIA.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17 */
18 #include "FirstOrderNonLinearDSTest.hpp"
19 #include "SiconosAlgebraProd.hpp"
20 #include "SimpleMatrixFriends.hpp"
21 
22 
23 #define CPPUNIT_ASSERT_NOT_EQUAL(message, alpha, omega)      \
24             if ((alpha) == (omega)) CPPUNIT_FAIL(message);
25 
26 // test suite registration
27 CPPUNIT_TEST_SUITE_REGISTRATION(FirstOrderNonLinearDSTest);
28 
29 
setUp()30 void FirstOrderNonLinearDSTest::setUp()
31 {
32   xnull.reset(new SiconosVector(3));
33   x0.reset(new SiconosVector(3));
34   (*x0)(0) = 1;
35   (*x0)(1) = 2;
36   (*x0)(2) = 3;
37 
38   J0.reset(new SimpleMatrix("matJ0.dat", true));
39   M.reset(new SimpleMatrix("matM.dat", true));
40 
41 }
42 
tearDown()43 void FirstOrderNonLinearDSTest::tearDown()
44 {}
45 
46 
47 // initial state only : \dot x = r
testBuildFirstOrderNonLinearDS1()48 void FirstOrderNonLinearDSTest::testBuildFirstOrderNonLinearDS1()
49 {
50   std::cout << "--> Test: constructor 1." <<std::endl;
51   SP::FirstOrderNonLinearDS ds(new FirstOrderNonLinearDS(x0));
52 
53   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", Type::value(*ds) == Type::FirstOrderNonLinearDS, true);
54   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->n() == 3, true);
55   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->getX0() == *x0, true);
56   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->f() == nullptr, true);
57   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->jacobianfx() == nullptr, true);
58   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->M() == nullptr, true);
59   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->invM() == nullptr, true);
60   double time = 1.5;
61 
62   ds->computef(time, ds->x());
63   ds->computeJacobianfx(time, ds->x());
64   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->f() == nullptr, true);
65   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->jacobianfx() == nullptr, true);
66 
67   SiconosVector zero(3);
68   SimpleMatrix m0(3,3);
69   ds->update(time);
70   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->rhs()) == zero, true);
71   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", ds->jacobianRhsx() == nullptr, true);
72 
73   ds->initRhs(time);
74   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->rhs()) == zero, true);
75   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->jacobianRhsx()) == m0, true);
76 
77   ds->setComputeFFunction("TestPlugin", "computef");
78   ds->setComputeJacobianfxFunction("TestPlugin", "computeJacobianfx");
79   time = 2.;
80   ds->computef(time, ds->x());
81   ds->computeJacobianfx(time, ds->x());
82   ds->setComputeMFunction("TestPlugin", "computeM");
83   ds->computeM(time);
84   SimpleMatrix Mref(3,3);
85   Mref(0,0) = 1. * time;
86   Mref(1,1) = 2. * time;
87   Mref(2,2) = 3. * time;
88   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->f()) == time* *x0, true);
89   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->jacobianfx()) == *J0, true);
90   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->M()) == Mref, true);
91   ds->initRhs(time);
92   SimpleMatrix invM(3,3);
93   invM(0,0) = 1. / time;
94   invM(1,1) = 1./ (2. * time);
95   invM(2,2) = 1./(3. * time);
96   SiconosVector tmp(3);
97   prod(invM, *x0, tmp);
98   prod(invM, *J0, Mref);
99   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->rhs()) == time * tmp, true);
100   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS1 : ", *(ds->jacobianRhsx()) == Mref, true);
101 
102   std::cout << "--> Constructor 2 test ended with success." <<std::endl;
103 }
104 
105 // copy
testBuildFirstOrderNonLinearDS2()106 void FirstOrderNonLinearDSTest::testBuildFirstOrderNonLinearDS2()
107 {
108   std::cout << "--> Test: constructor 1." <<std::endl;
109   SP::FirstOrderNonLinearDS source(new FirstOrderNonLinearDS(x0));
110   SP::FirstOrderNonLinearDS ds(new FirstOrderNonLinearDS(*source));
111 
112   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", Type::value(*ds) == Type::FirstOrderNonLinearDS, true);
113   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", ds->n() == 3, true);
114   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", ds->getX0() == *x0, true);
115   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", ds->f() == nullptr, true);
116   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", ds->jacobianfx() == nullptr, true);
117   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", ds->M() == nullptr, true);
118   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", ds->invM() == nullptr, true);
119   SiconosVector zero(3);
120   double time = 1.5;
121   ds->update(time);
122   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", *(ds->rhs()) == zero, true);
123   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", ds->jacobianRhsx() == nullptr, true);
124 
125   ds->initRhs(time);
126   SimpleMatrix m0(3,3);
127   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", *(ds->rhs()) == zero, true);
128   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS2 : ", *(ds->jacobianRhsx()) == m0, true);
129 
130   std::cout << "--> Constructor 2 test ended with success." <<std::endl;
131 }
132 
133 
134 // x0 + plugins for f and its gradient
testBuildFirstOrderNonLinearDS3()135 void FirstOrderNonLinearDSTest::testBuildFirstOrderNonLinearDS3()
136 {
137   std::cout << "--> Test: constructor 3." <<std::endl;
138   SP::FirstOrderNonLinearDS ds(new FirstOrderNonLinearDS(x0, "TestPlugin:computef", "TestPlugin:computeJacobianfx"));
139 
140   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", Type::value(*ds) == Type::FirstOrderNonLinearDS, true);
141   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", ds->n() == 3, true);
142   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", ds->getX0() == *x0, true);
143   double time = 1.5;
144   ds->computef(time, ds->x());
145   ds->computeJacobianfx(time, ds->x());
146   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", *(ds->f()) == time* *x0, true);
147   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", *(ds->jacobianfx()) == *J0, true);
148   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", ds->M() == nullptr, true);
149   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", ds->invM() == nullptr, true);
150 
151   ds->initRhs(time);
152   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", *(ds->rhs()) == time* *x0, true);
153   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", *(ds->jacobianRhsx()) == *J0, true);
154 
155   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", *(ds->f()) == time* *x0, true);
156   CPPUNIT_ASSERT_EQUAL_MESSAGE("testBuildFirstOrderNonLinearDS3 : ", *(ds->jacobianfx()) == *J0, true);
157   std::cout << "--> Constructor 3 test ended with success." <<std::endl;
158 }
159 
160 // setX0
testSetX0()161 void FirstOrderNonLinearDSTest::testSetX0()
162 {
163   std::cout << "--> Test: setX0." <<std::endl;
164   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
165   ds1->setX0(*x0);
166   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSetX0 : ", ds1->getX0() == *x0, true);
167   std::cout << "--> setX0 test ended with success." <<std::endl;
168 }
169 
170 // setX0Ptr
testSetX0Ptr()171 void FirstOrderNonLinearDSTest::testSetX0Ptr()
172 {
173   std::cout << "--> Test: setX0Ptr." <<std::endl;
174   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
175   ds1->setX0Ptr(x0);
176   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSetX0Ptr : ", ds1->getX0() == *x0, true);
177   std::cout << "--> setX0Ptr test ended with success." <<std::endl;
178 }
179 
180 // setX
testSetx()181 void FirstOrderNonLinearDSTest::testSetx()
182 {
183   std::cout << "--> Test: setX." <<std::endl;
184   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
185   ds1->setX(*x0);
186   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSetX : ", ds1->getx() == *x0, true);
187   std::cout << "--> setX test ended with success." <<std::endl;
188 }
189 
190 // setXPtr
testSetxPtr()191 void FirstOrderNonLinearDSTest::testSetxPtr()
192 {
193   std::cout << "--> Test: setXPtr." <<std::endl;
194   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
195   ds1->setXPtr(x0);
196   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSetXPtr : ", ds1->getx() == *x0, true);
197   std::cout << "--> setXPtr test ended with success." <<std::endl;
198 }
199 
200 // setR
testSetR()201 void FirstOrderNonLinearDSTest::testSetR()
202 {
203   std::cout << "--> Test: setR." <<std::endl;
204   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
205   ds1->setR(*x0);
206   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSetR : ", ds1->getR() == *x0, true);
207   std::cout << "--> setR test ended with success." <<std::endl;
208 }
209 
210 // setRPtr
testSetRPtr()211 void FirstOrderNonLinearDSTest::testSetRPtr()
212 {
213   std::cout << "--> Test: setRPtr." <<std::endl;
214   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
215   ds1->setRPtr(x0);
216   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSetRPtr : ", ds1->getR() == *x0, true);
217   std::cout << "--> setRPtr test ended with success." <<std::endl;
218 }
219 
220 // setJacobianXPtr
testSetJacobianfxPtr()221 void FirstOrderNonLinearDSTest::testSetJacobianfxPtr()
222 {
223   std::cout << "--> Test: setJacobianfxPtr." <<std::endl;
224   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
225   ds1->setJacobianfxPtr(J0);
226   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSetJacobianfxPtr : ", *(ds1->jacobianfx()) == *J0, true);
227   std::cout << "--> setJacobianfxPtr test ended with success." <<std::endl;
228 }
229 
230 // init
testInitMemory()231 void FirstOrderNonLinearDSTest::testInitMemory()
232 {
233   std::cout << "--> Test: initMemory." <<std::endl;
234   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
235   ds1->initMemory(2);
236   CPPUNIT_ASSERT_EQUAL_MESSAGE("testInitMem1 : ", ds1->xMemory().size() == 2, true);
237   CPPUNIT_ASSERT_EQUAL_MESSAGE("testInitMem3 : ", ds1->rMemory().size() == 2, true);
238 
239   CPPUNIT_ASSERT_EQUAL_MESSAGE("testInitMem4 : ", ds1->xMemory().nbVectorsInMemory() == 0, true);
240   CPPUNIT_ASSERT_EQUAL_MESSAGE("testInitMem6 : ", ds1->rMemory().nbVectorsInMemory() == 0, true);
241   std::cout << "--> initMemory test ended with success." <<std::endl;
242 }
243 
244 
245 // swap
testSwap()246 void FirstOrderNonLinearDSTest::testSwap()
247 {
248   std::cout << "--> Test: swap." <<std::endl;
249   SP::FirstOrderNonLinearDS ds1(new FirstOrderNonLinearDS(xnull));
250   ds1->setX(*x0);
251   ds1->setR(*x0);
252   ds1->initMemory(1);
253   ds1->swapInMemory();
254   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSwap1 : ", ds1->xMemory().getSiconosVector(0) == *x0, true);
255   CPPUNIT_ASSERT_EQUAL_MESSAGE("testSwap3 : ", ds1->rMemory().getSiconosVector(0) == *x0, true);
256   std::cout << "--> swap test ended with success." <<std::endl;
257 }
258 
259 
260 // plugins: plugins loading is already in testBuildFirstOrderNonLinearDS2
261