Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
grid_test.cpp
Go to the documentation of this file.
1#include "dccrg.hpp"
2#include "mpi.h"
3#include <iostream>
4#include <fstream>
5#include <vector>
6#include <iterator>
7#include <algorithm>
8#include "../../definitions.h"
9#include <list>
10#include "cpu_sort_ids.hpp"
11#include <map>
12
13using namespace std;
14
15struct grid_data {
16
17 int value = 0;
18
19 std::tuple<void*, int, MPI_Datatype> get_mpi_datatype()
20 {
21 return std::make_tuple(this, 0, MPI_BYTE);
22 }
23
24};
25
26
28
29 uint N; // Number of pencils in the set
30 std::vector<uint> lengthOfPencils; // Lengths of pencils
31 std::vector<CellID> ids; // List of cells
32 std::vector<Real> x,y; // x,y - position (Maybe replace with uint width?)
33
35 N = 0;
36 }
37
38 void addPencil(vector<CellID> idsIn, Real xIn, Real yIn) {
39
40 N += 1;
41 lengthOfPencils.push_back(idsIn.size());
42 ids.insert(ids.end(),idsIn.begin(),idsIn.end());
43 x.push_back(xIn);
44 y.push_back(yIn);
45
46 }
47
48};
49
50
51
52void insertVectorIntoVector(vector<CellID> &v1,vector<CellID> v2,uint i) {
53
54 vector<CellID> tmp(v1.begin(),v1.begin() + i);
55 tmp.insert(tmp.end(),v2.begin(),v2.end());
56 tmp.insert(tmp.end(),v1.begin() + i, v1.end());
57 v1.clear();
58 v1 = tmp;
59 v2.clear();
60
61}
62
63vector<CellID> getMyChildren(vector<CellID> &children,
64 uint dimension, bool up, bool left ) {
65
66 bool down = !up;
67 bool right = !left;
68
69 uint i1 = 999;
70 uint i2 = 999;
71
72 switch(dimension) {
73 case 0 :
74 if (up && left) {
75 i1 = 0;
76 i2 = 1;
77 break;
78 }
79 if (down && left) {
80 i1 = 2;
81 i2 = 3;
82 break;
83 }
84 if (up && right) {
85 i1 = 4;
86 i2 = 5;
87 break;
88 }
89 if (down && right) {
90 i1 = 6;
91 i2 = 7;
92 break;
93 }
94 case 1 :
95 if (up && left) {
96 i1 = 0;
97 i2 = 2;
98 break;
99 }
100 if (down && left) {
101 i1 = 1;
102 i2 = 3;
103 break;
104 }
105 if (up && right) {
106 i1 = 4;
107 i2 = 6;
108 break;
109 }
110 if (down && right) {
111 i1 = 5;
112 i2 = 7;
113 break;
114 }
115 case 2 :
116 if (up && left) {
117 i1 = 0;
118 i2 = 4;
119 break;
120 }
121 if (down && left) {
122 i1 = 1;
123 i2 = 5;
124 break;
125 }
126 if (up && right) {
127 i1 = 2;
128 i2 = 6;
129 break;
130 }
131 if (down && right) {
132 i1 = 3;
133 i2 = 7;
134 break;
135 }
136 default:
137 break;
138
139 }
140
141 vector<CellID> myChildren {children[i1],children[i2]};
142 return myChildren;
143
144}
145
146void printVector(vector<CellID> v) {
147
148 for (auto k = v.begin(); k != v.end(); ++k)
149 std::cout << *k << ' ';
150 std::cout << "\n";
151
152}
153
154setOfPencils buildPencils( dccrg::Dccrg<grid_data> grid,
155 setOfPencils &pencils, vector<CellID> idsOut,
156 vector<CellID> idsIn, int dimension,
157 vector<pair<bool,bool>> path) {
158
159 // Not necessary since c++ passes a copy by default.
160 // Copy the input ids to a working set of ids
161 // vector<int> ids( idsIn );
162 // Copy the already computed pencil to the output list
163 // vector<int> idsOut( idsInPencil );
164
165 uint i = 0;
166 uint length = idsIn.size();
167
168 // Walk along the input pencil. Initially length is equal to the length of the
169 // Unrefined pencil. When refined cells are encountered, the length is increased
170 // accordingly to go through the entire pencil.
171 while (i < length) {
172
173 uint i1 = i + 1;
174 uint id = idsIn[i];
175
176
177 std::array<uint64_t, 8> children = mpiGrid.mapping.get_all_children(id);
178 bool hasChildren = ( grid.mapping.get_parent(children[0]) == id );
179
180 // Check if the current cell contains refined cells
181 if (hasChildren) {
182
183 // Check if we have encountered this refinement level before and stored
184 // the path this builder followed
185 if (path.size() > grid.get_refinement_level(id)) {
186
187 // Get children using the stored path
188 vector<CellID> myChildren = getMyChildren(children,dimension,
189 path[grid.get_refinement_level(id)].first,
190 path[grid.get_refinement_level(id)].second);
191
192 // Add the children to the working set at index i1
193
194 insertVectorIntoVector(idsIn,myChildren,i1);
195 length += myChildren.size();
196
197 } else {
198
199 // Spawn new builders to construct pencils at the new refinement level
200
201 for (bool left : { true, false }) {
202 for (bool up : { true, false }) {
203
204 // Store the path this builder has chosen
205 vector < pair <bool,bool>> myPath = path;
206 myPath.push_back(pair<bool, bool>(up,left));
207
208 // Get children along my path.
209 vector<CellID> myChildren = getMyChildren(children,dimension,up,left);
210 // Get the ids that have not been processed yet.
211 vector<CellID> remainingIds(idsIn.begin() + i1, idsIn.end());
212
213 // The current builder continues along the bottom-right path.
214 // Other paths will spawn a new builder.
215 if (!up && !left) {
216
217 // Add the children to the working set. Next iteration of the
218 // main loop (over idsIn) will start on the first child
219
220 // Add the children to the working set at index i1
221 insertVectorIntoVector(idsIn,myChildren,i1);
222 length += myChildren.size();
223 path = myPath;
224
225 } else {
226
227 // Create a new working set by adding the remainder of the old
228 // working set to the end of the current children list
229
230 myChildren.insert(myChildren.end(),remainingIds.begin(),remainingIds.end());
231
232 buildPencils(grid,pencils,idsOut,myChildren,dimension,myPath);
233
234 };
235
236 };
237 };
238 };
239
240 } else {
241
242 // Add unrefined cells to the pencil directly
243
244 idsOut.push_back(id);
245
246 }; // closes if(isRefined)
247
248 // Move to the next cell
249 i++;
250
251 }; // closes loop over ids
252
253 pencils.addPencil(idsOut,0.0,0.0);
254 return pencils;
255
256} // closes function
257
258int main(int argc, char* argv[]) {
259
260 if (MPI_Init(&argc, &argv) != MPI_SUCCESS) {
261 // cerr << "Coudln't initialize MPI." << endl;
262 abort();
263 }
264
265 MPI_Comm comm = MPI_COMM_WORLD;
266
267 int rank = 0, comm_size = 0;
268 MPI_Comm_rank(comm, &rank);
269 MPI_Comm_size(comm, &comm_size);
270
271 dccrg::Dccrg<grid_data> grid;
272
273 const uint xDim = 9;
274 const uint yDim = 3;
275 const uint zDim = 1;
276 const std::array<uint64_t, 3> grid_size = {{xDim,yDim,zDim}};
277
278 grid.initialize(grid_size, comm, "RANDOM", 1);
279
280 grid.balance_load();
281
282 bool doRefine = true;
283 const std::array<uint,4> refinementIds = {{10,14,64,72}};
284 if(doRefine) {
285 for(uint i = 0; i < refinementIds.size(); i++) {
286 if(refinementIds[i] > 0) {
287 grid.refine_completely(refinementIds[i]);
288 grid.stop_refining();
289 }
290 }
291 }
292
293 grid.balance_load();
294
295 auto cells = grid.cells;
296 sort(cells.begin(), cells.end());
297
298 vector<CellID> ids;
299
300 std::cout << "Grid size at 0 refinement is " << xDim << " x " << yDim << " x " << zDim << std::endl;
301 for (const auto& cell: cells) {
302 // std::cout << "Data of cell " << cell.id << " is stored at " << cell.data << std::endl;
303 // Collect a list of cell ids.
304 ids.push_back(cell.id);
305
306 // Add parent cells of refined cells to the list of cell ids.
307 CellID parent = grid.mapping.get_parent(cell.id);
308 if (parent > 0 &&
309 !(std::find(ids.begin(), ids.end(), parent) != ids.end())) {
310 ids.push_back(parent);
311 std::cout << "Cell " << parent << " at refinement level " << grid.get_refinement_level(parent) << " has been refined into ";
312 for (const auto& child: grid.mapping.get_all_children(parent)) {
313 std::cout << child << " ";
314 }
315 std::cout << "\n";
316 }
317 }
318
319 sort(ids.begin(),ids.end());
320
321 uint ibeg = 0;
322 uint iend = 0;
323
324 uint dimension = 0;
325 vector<uint> dims;
326
327 switch( dimension ) {
328 case 0 : {
329 dims = {zDim,yDim,xDim};
330 }
331 break;
332 case 1 : {
333 dims = {zDim,xDim,yDim};
334 }
335 break;
336 case 2 : {
337 dims = {yDim,xDim,zDim};
338 }
339 break;
340 default : {
341 dims = {0,0,0};
342 }
343 };
344
345 map <CellID,CellID> mapping;
346 sortIds< CellID, dccrg::Grid_Length::type >(dimension, grid_size, ids, mapping);
347
348 list < vector < CellID >> unrefinedPencils;
349 std::cout << "The unrefined pencils along dimension " << dimension << " are:\n";
350 for (uint i = 0; i < dims[0]; i++) {
351 for (uint j = 0; j < dims[1]; j++) {
352 vector <CellID> unrefinedIds;
353 ibeg = 1 + i * dims[2] * dims[1] + j * dims[2];
354 iend = 1 + i * dims[2] * dims[1] + (j + 1) * dims[2];
355 for (uint k = ibeg; k < iend; k++) {
356 std::cout << mapping[k] << " ";
357 unrefinedIds.push_back(mapping[k]);
358 //unrefinedIds.push_back(ids[k]);
359 }
360 unrefinedPencils.push_back(unrefinedIds);
361 std::cout << "\n";
362 }
363 }
364
365 ibeg = 0;
366 iend = 0;
367
368 setOfPencils pencilInitial;
369 vector<CellID> idsInitial;
370 vector<pair<bool,bool>> path;
371
372 setOfPencils pencils;
373 for ( auto &unrefinedIds : unrefinedPencils ) {
374 pencils = buildPencils(grid, pencilInitial, idsInitial, unrefinedIds, dimension, path);
375 }
376
377 std::cout << "I have created " << pencils.N << " pencils along dimension " << dimension << ":\n";
378 for (uint i = 0; i < pencils.N; i++) {
379 iend += pencils.lengthOfPencils[i];
380 for (auto j = pencils.ids.begin() + ibeg; j != pencils.ids.begin() + iend; ++j) {
381 std::cout << *j << " ";
382 }
383 ibeg = iend;
384 std::cout << "\n";
385 }
386
387 std::ofstream outfile;
388
389 grid.write_vtk_file("test.vtk");
390
391 outfile.open("test.vtk", std::ofstream::app);
392 // write each cells id
393 outfile << "CELL_DATA " << cells.size() << std::endl;
394 outfile << "SCALARS id int 1" << std::endl;
395 outfile << "LOOKUP_TABLE default" << std::endl;
396 for (const auto& cell: cells) {
397 outfile << cell.id << std::endl;
398 }
399 outfile.close();
400
401 MPI_Finalize();
402
403 return 0;
404
405}
for i
Definition Dispersion.m:24
Parameters length
Definition Dispersion.m:36
void insertVectorIntoVector(vector< CellID > &v1, vector< CellID > v2, uint i)
Definition grid_test.cpp:52
void printVector(vector< CellID > v)
setOfPencils buildPencils(dccrg::Dccrg< grid_data > grid, setOfPencils &pencils, vector< CellID > idsOut, vector< CellID > idsIn, int dimension, vector< pair< bool, bool > > path)
vector< CellID > getMyChildren(vector< CellID > &children, uint dimension, bool up, bool left)
Definition grid_test.cpp:63
void sortIds(const uint dimension, const LENGTH meshSize, const std::vector< ID > &ids, std::map< ID, ID > &mapping)
float Real
Definition definitions.h:41
uint64_t CellID
Definition definitions.h:54
const int j
const int k
std::tuple< void *, int, MPI_Datatype > get_mpi_datatype()
Definition grid_test.cpp:19
std::vector< Real > x
Definition grid_test.cpp:32
std::vector< Real > y
Definition grid_test.cpp:32
std::vector< CellID > ids
Definition grid_test.cpp:31
void addPencil(vector< CellID > idsIn, Real xIn, Real yIn)
Definition grid_test.cpp:38
std::vector< uint > lengthOfPencils
Definition grid_test.cpp:30
int main()