OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
Vandermonde_unit_tests.cpp
1//
2// Created by tommaso on 22/03/19.
3//
4
5#define BOOST_TEST_DYN_LINK
6
7#include <boost/test/unit_test.hpp>
8#include <DCPSE/MonomialBasis.hpp>
9#include <DCPSE/VandermondeRowBuilder.hpp>
10#include <DCPSE/Vandermonde.hpp>
11#include "../../openfpm_numerics/src/DMatrix/EMatrix.hpp"
12
13BOOST_AUTO_TEST_SUITE(Vandermonde_tests)
14
15// If EIGEN is not present, EMatrix is not available and we don't need to build this test
16#ifdef HAVE_EIGEN
17
18 BOOST_AUTO_TEST_CASE(VandermondeRowBuilder_AllOnes_test)
19 {
20 MonomialBasis<2> mb({1, 0}, 2);
21 EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> row(1, mb.size());
22 Point<2, double> x({2, 2});
23 double eps = 2;
25 vrb.buildRow(row, 0, x, eps);
26 // For the way the row has been constructed, it should be composed of only 1s
27 bool isRowAllOnes = true;
28 for (int i = 0; i < mb.size(); ++i)
29 {
30 isRowAllOnes = isRowAllOnes && (row(0, i) == 1);
31 }
32 BOOST_REQUIRE(isRowAllOnes);
33 }
34
35 BOOST_AUTO_TEST_CASE(VandermondeRowBuilder_OneZero_test)
36 {
37 MonomialBasis<2> mb({1, 0}, 2);
38 EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> row(1, mb.size());
39 Point<2, double> x({1, 0});
40 double eps = 1;
42 vrb.buildRow(row, 0, x, eps);
43 // For the way the row has been constructed, it should be composed of only 1s
44 bool areValuesOk = true;
45 for (int i = 0; i < mb.size(); ++i)
46 {
47 bool isThereY = mb.getElement(i).getExponent(1) > 0;
48 bool curCheck = (row(0, i) == !isThereY);
49 areValuesOk = areValuesOk && curCheck;
50 }
51 BOOST_REQUIRE(areValuesOk);
52 }
53
54 BOOST_AUTO_TEST_CASE(VandermondeRowBuilder_ZeroOne_test)
55 {
56 MonomialBasis<2> mb({1, 0}, 2);
57 EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> row(1, mb.size());
58 Point<2, double> x({0, 1});
59 double eps = 1;
61 vrb.buildRow(row, 0, x, eps);
62 // For the way the row has been constructed, it should be composed of only 1s
63 bool areValuesOk = true;
64 for (int i = 0; i < mb.size(); ++i)
65 {
66 bool isThereX = mb.getElement(i).getExponent(0) > 0;
67 bool curCheck = (row(0, i) == !isThereX);
68 areValuesOk = areValuesOk && curCheck;
69 }
70 BOOST_REQUIRE(areValuesOk);
71 }
72
73 BOOST_AUTO_TEST_CASE(Vandermonde_KnownValues_test)
74 {
75 MonomialBasis<2> mb({1, 0}, 2);
76// std::cout << mb << std::endl; // Debug
77 Point<2, double> x({0, 0});
78 std::vector<Point<2, double>> neighbours;
79 EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> V(mb.size(), mb.size());
80 EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> ExpectedV(mb.size(), mb.size());
81
83
84 // Now push 4 points on diagonal as neighbours
85 parts.add();
86 parts.getLastPos()[0]=0;
87 parts.getLastPos()[1]=0;
88 parts.add();
89 parts.getLastPos()[0]=1;
90 parts.getLastPos()[1]=1;
91 parts.add();
92 parts.getLastPos()[0]=-1;
93 parts.getLastPos()[1]=-1;
94 parts.add();
95 parts.getLastPos()[0]=1;
96 parts.getLastPos()[1]=-1;
97 parts.add();
98 parts.getLastPos()[0]=-1;
99 parts.getLastPos()[1]=1;
100 parts.add();
101 parts.getLastPos()[0]=0;
102 parts.getLastPos()[1]=1;
103 parts.add();
104 parts.getLastPos()[0]=0;
105 parts.getLastPos()[1]=-1;
106
107 const std::vector<size_t> keys({1,2,3,4,5,6});
108
109 Support s(0,keys);
110
111 // ...and get the matrix V
113 vandermonde.getMatrix(V);
114
115 // Now build the matrix of expected values
116 ExpectedV(0, 0) = 1;
117 ExpectedV(0, 1) = -0.3;
118 ExpectedV(0, 2) = 0.09;
119 ExpectedV(0, 3) = -0.3;
120 ExpectedV(0, 4) = +0.09;
121 ExpectedV(0, 5) = 0.09;
122 ExpectedV(1, 0) = 1;
123 ExpectedV(1, 1) = +0.3;
124 ExpectedV(1, 2) = 0.09;
125 ExpectedV(1, 3) = +0.3;
126 ExpectedV(1, 4) = +0.09;
127 ExpectedV(1, 5) = 0.09;
128 ExpectedV(2, 0) = 1;
129 ExpectedV(2, 1) = -0.3;
130 ExpectedV(2, 2) = 0.09;
131 ExpectedV(2, 3) = +0.3;
132 ExpectedV(2, 4) = -0.09;
133 ExpectedV(2, 5) = 0.09;
134 ExpectedV(3, 0) = 1;
135 ExpectedV(3, 1) = +0.3;
136 ExpectedV(3, 2) = 0.09;
137 ExpectedV(3, 3) = -0.3;
138 ExpectedV(3, 4) = -0.09;
139 ExpectedV(3, 5) = 0.09;
140 ExpectedV(4, 0) = 1;
141 ExpectedV(4, 1) = 0;
142 ExpectedV(4, 2) = 0;
143 ExpectedV(4, 3) = -0.3;
144 ExpectedV(4, 4) = 0;
145 ExpectedV(4, 5) = 0.09;
146 ExpectedV(5, 0) = 1;
147 ExpectedV(5, 1) = 0;
148 ExpectedV(5, 2) = 0;
149 ExpectedV(5, 3) = +0.3;
150 ExpectedV(5, 4) = 0;
151 ExpectedV(5, 5) = 0.09;
152
153 // Now check that the values in V are all the expected ones
154 for (int i = 0; i < mb.size(); ++i)
155 {
156// std::cout << "N[" << i << "] = " << neighbours[i].toString() << std::endl;
157 for (int j = 0; j < mb.size(); ++j)
158 {
159// std::cout << ">> V[" << i << "," << j << "] = " << V(i,j) << std::endl;
160 BOOST_REQUIRE_CLOSE(V(i, j), ExpectedV(i, j), 1e-6);
161 }
162 }
163 }
164
165 BOOST_AUTO_TEST_CASE(Vandermonde_TranslatedSetup_test)
166 {
167 MonomialBasis<2> mb({1, 0}, 2);
168// std::cout << mb << std::endl; // Debug
169 Point<2, double> x({1, 1});
170 EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> V(mb.size(), mb.size());
171 EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> ExpectedV(mb.size(), mb.size());
172 // Now push 4 points on diagonal as neighbours
173
175
176 // Now push 4 points on diagonal as neighbours
177 parts.add();
178 parts.getLastPos()[0]=1;
179 parts.getLastPos()[1]=1;
180 parts.add();
181 parts.getLastPos()[0]=2;
182 parts.getLastPos()[1]=2;
183 parts.add();
184 parts.getLastPos()[0]=0;
185 parts.getLastPos()[1]=0;
186 parts.add();
187 parts.getLastPos()[0]=2;
188 parts.getLastPos()[1]=0;
189 parts.add();
190 parts.getLastPos()[0]=0;
191 parts.getLastPos()[1]=2;
192 parts.add();
193 parts.getLastPos()[0]=1;
194 parts.getLastPos()[1]=2;
195 parts.add();
196 parts.getLastPos()[0]=1;
197 parts.getLastPos()[1]=0;
198
199 const std::vector<size_t> keys({1,2,3,4,5,6});
200
201 Support s(0,keys);
202
203 // ...and get the matrix V
205 vandermonde.getMatrix(V);
206
207 // Now build the matrix of expected values
208 ExpectedV(0, 0) = 1;
209 ExpectedV(0, 1) = -0.3;
210 ExpectedV(0, 2) = 0.09;
211 ExpectedV(0, 3) = -0.3;
212 ExpectedV(0, 4) = +0.09;
213 ExpectedV(0, 5) = 0.09;
214 ExpectedV(1, 0) = 1;
215 ExpectedV(1, 1) = +0.3;
216 ExpectedV(1, 2) = 0.09;
217 ExpectedV(1, 3) = +0.3;
218 ExpectedV(1, 4) = +0.09;
219 ExpectedV(1, 5) = 0.09;
220 ExpectedV(2, 0) = 1;
221 ExpectedV(2, 1) = -0.3;
222 ExpectedV(2, 2) = 0.09;
223 ExpectedV(2, 3) = +0.3;
224 ExpectedV(2, 4) = -0.09;
225 ExpectedV(2, 5) = 0.09;
226 ExpectedV(3, 0) = 1;
227 ExpectedV(3, 1) = +0.3;
228 ExpectedV(3, 2) = 0.09;
229 ExpectedV(3, 3) = -0.3;
230 ExpectedV(3, 4) = -0.09;
231 ExpectedV(3, 5) = 0.09;
232 ExpectedV(4, 0) = 1;
233 ExpectedV(4, 1) = 0;
234 ExpectedV(4, 2) = 0;
235 ExpectedV(4, 3) = -0.3;
236 ExpectedV(4, 4) = 0;
237 ExpectedV(4, 5) = 0.09;
238 ExpectedV(5, 0) = 1;
239 ExpectedV(5, 1) = 0;
240 ExpectedV(5, 2) = 0;
241 ExpectedV(5, 3) = +0.3;
242 ExpectedV(5, 4) = 0;
243 ExpectedV(5, 5) = 0.09;
244
245 // Now check that the values in V are all the expected ones
246 for (int i = 0; i < mb.size(); ++i)
247 {
248// std::cout << "N[" << i << "] = " << neighbours[i].toString() << std::endl;
249 for (int j = 0; j < mb.size(); ++j)
250 {
251// std::cout << ">> V[" << i << "," << j << "] = " << V(i,j) << std::endl;
252 BOOST_REQUIRE_CLOSE(V(i, j), ExpectedV(i, j), 1e-6);
253 }
254 }
255 }
256
257#endif // HAVE_EIGEN
258
259BOOST_AUTO_TEST_SUITE_END()
This class implement the point shape in an N-dimensional space.
Definition Point.hpp:28
Distributed vector.
auto getLastPos() -> decltype(v_pos.template get< 0 >(0))
Get the position of the last element.
void add()
Add local particle.