1 | /* |
2 | * grid_dist_id_dlb_unit_test.cpp |
3 | * |
4 | * Created on: May 4, 2018 |
5 | * Author: i-bird |
6 | */ |
7 | |
8 | |
9 | #define BOOST_TEST_DYN_LINK |
10 | #include <boost/test/unit_test.hpp> |
11 | |
12 | #include "Point_test.hpp" |
13 | #include "Grid/grid_dist_id.hpp" |
14 | #include "data_type/aggregate.hpp" |
15 | #include "grid_dist_id_util_tests.hpp" |
16 | #include "Vector/vector_dist.hpp" |
17 | |
18 | BOOST_AUTO_TEST_SUITE( grid_dist_id_dlb_test ) |
19 | |
20 | template<typename grid, typename vector> |
21 | void test_vector_grid_dlb() |
22 | { |
23 | // Domain |
24 | Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0}); |
25 | |
26 | Ghost<3,long int> g(1); |
27 | |
28 | size_t sz[3] = {37,37,37}; |
29 | |
30 | grid gdist(sz,domain3,g,DEC_GRAN(128)); |
31 | |
32 | |
33 | aggregate<long int,long int,long int> bck; |
34 | bck.template get<0>() = -57; |
35 | bck.template get<1>() = -90; |
36 | bck.template get<2>() = -123; |
37 | |
38 | gdist.setBackgroundValue(bck); |
39 | |
40 | vector vd(gdist.getDecomposition(),0); |
41 | |
42 | // we fill the grid with a gaussian |
43 | |
44 | auto it = gdist.getGridIterator(); |
45 | |
46 | while (it.isNext()) |
47 | { |
48 | auto p = it.get_dist(); |
49 | auto gkey = it.get(); |
50 | |
51 | gdist.template insert<0>(p) = gkey.get(0); |
52 | gdist.template insert<1>(p) = gkey.get(1); |
53 | gdist.template insert<2>(p) = gkey.get(2); |
54 | |
55 | ++it; |
56 | } |
57 | |
58 | // fill a sphere of particles |
59 | |
60 | auto it2 = vd.getGridIterator(sz); |
61 | |
62 | while (it2.isNext()) |
63 | { |
64 | auto gkey = it2.get(); |
65 | |
66 | vd.add(); |
67 | |
68 | float x = 0.2*gkey.get(0)*it2.getSpacing(0) + 0.05; |
69 | float y = 0.2*gkey.get(1)*it2.getSpacing(1) + 0.05; |
70 | float z = 0.2*gkey.get(2)*it2.getSpacing(2) + 0.05; |
71 | |
72 | vd.getLastPos()[0] = x; |
73 | vd.getLastPos()[1] = y; |
74 | vd.getLastPos()[2] = z; |
75 | |
76 | ++it2; |
77 | } |
78 | |
79 | size_t n_step = 50; |
80 | for (size_t i = 0; i < n_step ; i++) |
81 | { |
82 | vd.map(); |
83 | vd.addComputationCosts(); |
84 | vd.getDecomposition().decompose(); |
85 | vd.map(); |
86 | |
87 | gdist.getDecomposition() = vd.getDecomposition(); |
88 | gdist.map(); |
89 | |
90 | // Check |
91 | |
92 | bool check = true; |
93 | auto it = gdist.getDomainIterator(); |
94 | |
95 | |
96 | while (it.isNext()) |
97 | { |
98 | auto p = it.get(); |
99 | auto gkey = it.getGKey(p); |
100 | |
101 | check &= gdist.template get<0>(p) == gkey.get(0); |
102 | check &= gdist.template get<1>(p) == gkey.get(1); |
103 | check &= gdist.template get<2>(p) == gkey.get(2); |
104 | |
105 | ++it; |
106 | } |
107 | |
108 | BOOST_REQUIRE_EQUAL(check,true); |
109 | |
110 | // Calculate shift vector |
111 | |
112 | double t2 = 6.28*(double)(i+1)/n_step; |
113 | double t = 6.28*(double)i/n_step; |
114 | double v[3]; |
115 | v[0] = 0.7*fabs(sin(t2)) - 0.7*fabs(sin(t)); |
116 | v[1] = 0.7*fabs(sin(1.7*t2)) - 0.7*fabs(sin(1.7*t)); |
117 | v[2] = 0.7*fabs(sin(2.5*t2)) - 0.7*fabs(sin(2.5*t)); |
118 | |
119 | auto it2 = vd.getDomainIterator(); |
120 | |
121 | while (it2.isNext()) |
122 | { |
123 | auto p = it2.get(); |
124 | |
125 | vd.getPos(p)[0] += v[0]; |
126 | vd.getPos(p)[1] += v[1]; |
127 | vd.getPos(p)[2] += v[2]; |
128 | |
129 | ++it2; |
130 | } |
131 | vd.map(); |
132 | } |
133 | } |
134 | |
135 | struct GaussianDLB |
136 | { |
137 | double t = 0.0; |
138 | |
139 | size_t resolution(const Point<3,float> & p) |
140 | { |
141 | float x0 = fabs(sin(t)); |
142 | float y0 = fabs(cos(t)); |
143 | float z0 = fabs(sin(3.0*t)); |
144 | |
145 | return 100 * exp( - ((p.get(0) - x0)*(p.get(0) - x0) + (p.get(1) - y0)*(p.get(1) - y0) + (p.get(2) - z0)*(p.get(2) - z0)) / 0.3); |
146 | } |
147 | |
148 | double distributionTol() |
149 | { |
150 | return 1.01; |
151 | } |
152 | }; |
153 | |
154 | template<typename grid, typename vector> |
155 | void test_vector_grid_dlb_resolution() |
156 | { |
157 | // Domain |
158 | Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0}); |
159 | |
160 | Ghost<3,long int> g(1); |
161 | |
162 | size_t sz[3] = {37,37,37}; |
163 | |
164 | grid gdist(sz,domain3,g,DEC_GRAN(128)); |
165 | |
166 | |
167 | aggregate<long int,long int,long int> bck; |
168 | bck.template get<0>() = -57; |
169 | bck.template get<1>() = -90; |
170 | bck.template get<2>() = -123; |
171 | |
172 | gdist.setBackgroundValue(bck); |
173 | |
174 | // we fill the grid with a gaussian |
175 | |
176 | auto it = gdist.getGridIterator(); |
177 | |
178 | while (it.isNext()) |
179 | { |
180 | auto p = it.get_dist(); |
181 | auto gkey = it.get(); |
182 | |
183 | gdist.template insert<0>(p) = gkey.get(0); |
184 | gdist.template insert<1>(p) = gkey.get(1); |
185 | gdist.template insert<2>(p) = gkey.get(2); |
186 | |
187 | ++it; |
188 | } |
189 | |
190 | GaussianDLB gdlb; |
191 | gdlb.t = 0.0; |
192 | |
193 | size_t n_step = 50; |
194 | for (size_t i = 0; i < n_step ; i++) |
195 | { |
196 | gdlb.t = (float)i/n_step; |
197 | gdist.addComputationCosts(gdlb); |
198 | gdist.getDecomposition().decompose(); |
199 | gdist.map(); |
200 | |
201 | gdist.write_frame("sgrid" ,i); |
202 | |
203 | // Check |
204 | |
205 | bool check = true; |
206 | auto it = gdist.getDomainIterator(); |
207 | |
208 | |
209 | while (it.isNext()) |
210 | { |
211 | auto p = it.get(); |
212 | auto gkey = it.getGKey(p); |
213 | |
214 | check &= gdist.template get<0>(p) == gkey.get(0); |
215 | check &= gdist.template get<1>(p) == gkey.get(1); |
216 | check &= gdist.template get<2>(p) == gkey.get(2); |
217 | |
218 | ++it; |
219 | } |
220 | |
221 | BOOST_REQUIRE_EQUAL(check,true); |
222 | } |
223 | } |
224 | |
225 | BOOST_AUTO_TEST_CASE( grid_dist_dlb_test ) |
226 | { |
227 | typedef sgrid_dist_id<3,float,aggregate<long int,long int,long int>> grid_sparse; |
228 | typedef vector_dist<3,float,aggregate<long int, long int> > particles; |
229 | |
230 | test_vector_grid_dlb<grid_sparse,particles>(); |
231 | } |
232 | |
233 | BOOST_AUTO_TEST_CASE( grid_dist_dlb_test_resolution ) |
234 | { |
235 | typedef sgrid_dist_id<3,float,aggregate<long int,long int,long int>> grid_sparse; |
236 | typedef vector_dist<3,float,aggregate<long int, long int> > particles; |
237 | |
238 | test_vector_grid_dlb_resolution<grid_sparse,particles>(); |
239 | } |
240 | |
241 | BOOST_AUTO_TEST_SUITE_END() |
242 | |
243 | |