OpenFPM  5.2.0
Project that contain the implementation of distributed structures
regression_test.cpp
1 /*
2  * Unit tests for the regression module
3  * author : Sachin (sthekke@mpi-cbg.de)
4  * date : 28.04.2022
5  *
6  */
7 
8 #define BOOST_TEST_DYN_LINK
9 #include <boost/test/unit_test.hpp>
10 #include "regression.hpp"
11 #include "Vector/vector_dist.hpp"
12 #include "DMatrix/EMatrix.hpp"
13 
14 BOOST_AUTO_TEST_SUITE( Regression_test )
15 
16 
17 
18 BOOST_AUTO_TEST_CASE ( Regression_local )
19 {
20  Box<2,float> domain({0.0,0.0},{1.0,1.0});
21  // Here we define the boundary conditions of our problem
22  size_t bc[2]={PERIODIC,PERIODIC};
23  // extended boundary around the domain, and the processor domain
24  Ghost<2,float> g(0.1);
25 
26  using vectorType = vector_dist<2,float, aggregate<double> >;
27  vectorType vd(2048,domain,bc,g);
28  // the scalar is the element at position 0 in the aggregate
29  const int scalar = 0;
30 
31  auto it = vd.getDomainIterator();
32  while (it.isNext())
33  {
34  auto key = it.get();
35  double posx = (double)rand() / RAND_MAX;
36  double posy = (double)rand() / RAND_MAX;
37 
38  vd.getPos(key)[0] = posx;
39  vd.getPos(key)[1] = posy;
40 
41  // Use synthetic data x*y
42  vd.template getProp<scalar>(key) = sin(posx*posy);
43  ++it;
44  }
45  vd.map();
46 
47  auto it2 = vd.getDomainIterator();
48  auto NN = vd.getCellList(0.1);
49  /*
50  auto support = RegressionSupport<vectorType, decltype(NN)>(vd, it2, 10, N_PARTICLES, NN);
51 
52  std::cout<<"Initialized domain with "<<support.getNumParticles()<<" particles.\n";
53 
54  auto model = RegressionModel<2, 0, vectorType>(vd, dom, 6, 2.0);
55 
56  double max_err = -1.0;
57  for(double x = 0.75; x < 0.85;x+=0.01)
58  {
59  for(double y = 0.75; y < 0.85; y+=0.01)
60  {
61  Point<2, double> pos{x,y};
62  double val = model->eval(pos);
63  double actual = sin(x*y);
64  double err = std::abs(actual - val);
65  if (err > max_err) max_err = err;
66  }
67  }
68 
69  double tolerance = 1e-5;
70  bool check;
71  if (std::abs(max_err) < tolerance)
72  check = true;
73  else
74  check = false;
75  std::cout<<"Max err = "<<max_err<<"\n";
76  BOOST_TEST( check );
77  */
78 }
79 
80 
81 
82 BOOST_AUTO_TEST_CASE ( Regression_without_domain_initialization)
83 {
84  Box<2,float> domain({0.0,0.0},{1.0,1.0});
85  // Here we define the boundary conditions of our problem
86  size_t bc[2]={PERIODIC,PERIODIC};
87  // extended boundary around the domain, and the processor domain
88  Ghost<2,float> g(0.01);
89 
90  using vectorType = vector_dist<2,float, aggregate<double> >;
91 
92  vectorType vd_orig(1024,domain,bc,g);
93 
94  vectorType vd_test(vd_orig.getDecomposition(), 200);
95 
96  Vcluster<> & v_cl = create_vcluster();
97  long int N_prc = v_cl.getProcessingUnits();
98 
99  if (v_cl.getProcessUnitID() == 0)
100  std::cout<<"Running regression test with "<<N_prc<<" procs.\n";
101 
102  // the scalar is the element at position 0 in the aggregate
103  const int scalar = 0;
104 
105  // Creating a grid with synthetic data
106  {
107  auto it = vd_orig.getDomainIterator();
108  while (it.isNext())
109  {
110  auto key = it.get();
111  double posx = (double)rand() / RAND_MAX;
112  double posy = (double)rand() / RAND_MAX;
113 
114  vd_orig.getPos(key)[0] = posx;
115  vd_orig.getPos(key)[1] = posy;
116 
117  // Use synthetic data x*y
118  vd_orig.template getProp<scalar>(key) = sin(posx*posy);
119  ++it;
120  }
121  vd_orig.map();
122  }
123 
124  // Creating test points
125  {
126  auto it = vd_test.getDomainIterator();
127  while (it.isNext())
128  {
129  auto key = it.get();
130  double posx = (double)rand() / RAND_MAX;
131  double posy = (double)rand() / RAND_MAX;
132 
133  vd_test.getPos(key)[0] = posx;
134  vd_test.getPos(key)[1] = posy;
135 
136  vd_test.template getProp<scalar>(key) = 0.0;
137 
138  ++it;
139  }
140  vd_test.map();
141  }
142 
143  auto model = RegressionModel<2, 0>(vd_orig, 1e-6);
144 
145  double max_err = -1.0;
146  // Checking the error
147  {
148  auto it = vd_test.getDomainIterator();
149  while (it.isNext())
150  {
151  auto key = it.get();
152  Point<2, double> pos = {vd_test.getPos(key)[0], vd_test.getPos(key)[1]};
153  double val = model.eval(pos);
154  double actual = sin(pos[0]*pos[1]);
155  double err = std::abs(actual - val);
156  if (err > max_err) max_err = err;
157 
158  vd_test.template getProp<scalar>(key) = val;
159 
160  ++it;
161  }
162  vd_test.ghost_get<scalar>();
163  }
164 
165  v_cl.max(max_err);
166  v_cl.execute();
167  if (v_cl.getProcessUnitID() == 0)
168  std::cout << "Maximum error: " << max_err << "\n";
169 
170  double tolerance = 1e-5;
171  bool check;
172  if (std::abs(max_err) < tolerance)
173  check = true;
174  else
175  check = false;
176 
177  BOOST_TEST( check );
178 
179 
180 }
181 
182 
183 BOOST_AUTO_TEST_SUITE_END()
This class represent an N-dimensional box.
Definition: Box.hpp:60
Definition: Ghost.hpp:40
void execute()
Execute all the requests.
size_t getProcessUnitID()
Get the process unit id.
size_t getProcessingUnits()
Get the total number of processors.
void max(T &num)
Get the maximum number across all processors (or reduction with infinity norm)
Implementation of VCluster class.
Definition: VCluster.hpp:59
Distributed vector.