OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
MonomialBasis_unit_tests.cpp
1 //
2 // Created by tommaso on 21/03/19.
3 //
4 
5 #define BOOST_TEST_DYN_LINK
6 
7 #include <boost/test/unit_test.hpp>
8 #include <Space/Shape/Point.hpp>
9 #include <DCPSE/MonomialBasis.hpp>
10 
11 BOOST_AUTO_TEST_SUITE(MonomialBasis_tests)
12 
13  BOOST_AUTO_TEST_CASE(Monomial_3D_test)
14  {
15  // Testing constructor from a Point
16  Point<3, unsigned int> p({1, 2, 3});
17  Monomial<3> mbe(p);
18  BOOST_REQUIRE_EQUAL(mbe.order(), 6);
19  BOOST_REQUIRE_EQUAL(mbe.getExponent(0), 1);
20  BOOST_REQUIRE_EQUAL(mbe.getExponent(1), 2);
21  BOOST_REQUIRE_EQUAL(mbe.getExponent(2), 3);
22 
23  // Testing copy constructor
24  Monomial<3> mbe2(mbe);
25  BOOST_REQUIRE_EQUAL(mbe2.order(), 6);
26  BOOST_REQUIRE_EQUAL(mbe2.getExponent(0), 1);
27  BOOST_REQUIRE_EQUAL(mbe2.getExponent(1), 2);
28  BOOST_REQUIRE_EQUAL(mbe2.getExponent(2), 3);
29 
30  // Testing copy assignment operator
31  Monomial<3> mbe3;
32  mbe3 = mbe;
33  BOOST_REQUIRE_EQUAL(mbe3.order(), 6);
34  BOOST_REQUIRE_EQUAL(mbe3.getExponent(0), 1);
35  BOOST_REQUIRE_EQUAL(mbe3.getExponent(1), 2);
36  BOOST_REQUIRE_EQUAL(mbe3.getExponent(2), 3);
37 
38  // Testing default constructor
39  Monomial<3> mbe0;
40  BOOST_REQUIRE_EQUAL(mbe0.order(), 0);
41  BOOST_REQUIRE_EQUAL(mbe0.getExponent(0), 0);
42  BOOST_REQUIRE_EQUAL(mbe0.getExponent(1), 0);
43  BOOST_REQUIRE_EQUAL(mbe0.getExponent(2), 0);
44 
45  // Testing one-by-one set values
46  mbe0.setExponent(0, 5);
47  mbe0.setExponent(1, 5);
48  mbe0.setExponent(2, 5);
49  BOOST_REQUIRE_EQUAL(mbe0.order(), 15);
50  BOOST_REQUIRE_EQUAL(mbe0.getExponent(0), 5);
51  BOOST_REQUIRE_EQUAL(mbe0.getExponent(1), 5);
52  BOOST_REQUIRE_EQUAL(mbe0.getExponent(2), 5);
53  }
54 
55  BOOST_AUTO_TEST_CASE(Monomial_evaluate_test)
56  {
57  // Test with uniform exponent and different coordinates
58  {
59  Monomial<3> monomial(Point<3, unsigned int>({1, 1, 1}));
60  double res = monomial.evaluate(Point<3, double>({5, 3, 2}));
61  double expected = 5 * 3 * 2;
62  BOOST_REQUIRE_CLOSE(res, expected, 1e-6);
63  }
64  // Test with different exponents and uniform coordinates
65  {
66  Monomial<3> monomial(Point<3, unsigned int>({5, 3, 2}));
67  double res = monomial.evaluate(Point<3, double>({2, 2, 2}));
68  double expected = pow(2,5) * pow(2,3) * pow(2,2);
69  BOOST_REQUIRE_CLOSE(res, expected, 1e-6);
70  }
71  }
72 
73  BOOST_AUTO_TEST_CASE(Monomial_derivative_test)
74  {
75  // Test differentiation along 3 dimensions independently
76  {
77  Monomial<3> monomial(Point<3, unsigned int>({6, 6, 6}));
78  Monomial<3> derivative = monomial.getDerivative(Point<3, double>({3, 0, 0}));
79  Monomial<3> expected(Point<3, unsigned int>({3, 6, 6}), 6*5*4);
80  BOOST_REQUIRE_EQUAL(derivative, expected);
81  }
82  {
83  Monomial<3> monomial(Point<3, unsigned int>({6, 6, 6}));
84  Monomial<3> derivative = monomial.getDerivative(Point<3, double>({0, 3, 0}));
85  Monomial<3> expected(Point<3, unsigned int>({6, 3, 6}), 6*5*4);
86  BOOST_REQUIRE_EQUAL(derivative, expected);
87  }
88  {
89  Monomial<3> monomial(Point<3, unsigned int>({6, 6, 6}));
90  Monomial<3> derivative = monomial.getDerivative(Point<3, double>({0, 0, 3}));
91  Monomial<3> expected(Point<3, unsigned int>({6, 6, 3}), 6*5*4);
92  BOOST_REQUIRE_EQUAL(derivative, expected);
93  }
94  // Test for correctly differentiating beyond degree
95  {
96  Monomial<3> monomial(Point<3, unsigned int>({6, 6, 6}));
97  Monomial<3> derivative = monomial.getDerivative(Point<3, double>({7, 0, 0}));
98  Monomial<3> expected(Point<3, unsigned int>({0, 6, 6}), 0);
99  BOOST_REQUIRE_EQUAL(derivative, expected);
100  }
101  {
102  Monomial<3> monomial(Point<3, unsigned int>({6, 6, 6}));
103  Monomial<3> derivative = monomial.getDerivative(Point<3, double>({0, 7, 0}));
104  Monomial<3> expected(Point<3, unsigned int>({6, 0, 6}), 0);
105  BOOST_REQUIRE_EQUAL(derivative, expected);
106  }
107  {
108  Monomial<3> monomial(Point<3, unsigned int>({6, 6, 6}));
109  Monomial<3> derivative = monomial.getDerivative(Point<3, double>({0, 0, 7}));
110  Monomial<3> expected(Point<3, unsigned int>({6, 6, 0}), 0);
111  BOOST_REQUIRE_EQUAL(derivative, expected);
112  }
113  }
114 
115  BOOST_AUTO_TEST_CASE(MonomialBasis_2D_test)
116  {
117  {
118  MonomialBasis<2> mb({1, 0}, 2);
119 
120  // Check that the basis contains all the elements it is supposed to contain
121  std::vector<Monomial<2>> control;
122  control.emplace_back(Point<2, unsigned int>({0, 0}));
123  control.emplace_back(Point<2, unsigned int>({1, 0}));
124  control.emplace_back(Point<2, unsigned int>({0, 1}));
125  control.emplace_back(Point<2, unsigned int>({1, 1}));
126  control.emplace_back(Point<2, unsigned int>({2, 0}));
127  control.emplace_back(Point<2, unsigned int>({0, 2}));
128 
129  auto first = mb.getElements().begin();
130  auto last = mb.getElements().end();
131  int foundElements = 0;
132  for (const auto &item : control)
133  {
134  auto pos = std::find(first, last, item);
135  foundElements += (pos != last);
136  }
137  BOOST_REQUIRE_EQUAL(foundElements, control.size());
138  }
139  {
140  MonomialBasis<2> mb({1, 1}, 2);
141 // std::cout << mb << std::endl;
142 // std::cout << mb.size() << std::endl;
143 
144  // Check that the basis contains all the elements it is supposed to contain
145  std::vector<Monomial<2>> control;
146  //control.emplace_back(Point<2, unsigned int>({0, 0}));
147  control.emplace_back(Point<2, unsigned int>({1, 0}));
148  control.emplace_back(Point<2, unsigned int>({0, 1}));
149  control.emplace_back(Point<2, unsigned int>({1, 1}));
150  control.emplace_back(Point<2, unsigned int>({2, 0}));
151  control.emplace_back(Point<2, unsigned int>({0, 2}));
152  control.emplace_back(Point<2, unsigned int>({2, 1}));
153  control.emplace_back(Point<2, unsigned int>({1, 2}));
154  control.emplace_back(Point<2, unsigned int>({3, 0}));
155  control.emplace_back(Point<2, unsigned int>({0, 3}));
156 
157  BOOST_REQUIRE_EQUAL(mb.size(), control.size());
158 
159  auto first = mb.getElements().begin();
160  auto last = mb.getElements().end();
161  int foundElements = 0;
162  for (const auto &item : control)
163  {
164  auto pos = std::find(first, last, item);
165  foundElements += (pos != last);
166  }
167  BOOST_REQUIRE_EQUAL(foundElements, control.size());
168  }
169  }
170 
171 BOOST_AUTO_TEST_SUITE_END()
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:27