PABLO  0.1
PArallel Balanced Linear Octree
 All Classes Functions Variables Pages
ioFunct.cpp
1 #include <ioFunct.hpp>
2 
3 // ----------------------------------------------------------------------------------- //
4 
5 using namespace std;
6 
7 // ----------------------------------------------------------------------------------- //
8 
9 void writeLocalTree(const u32vector2D& nodes, const u32vector2D& connectivity,
10  const u32vector2D& ghostNodes, const u32vector2D& ghostConnectivity,
11  const Class_Para_Tree<2> & ptree, string filename) {
12 
13  stringstream name;
14  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-p" << std::setfill('0') << std::setw(4) << ptree.rank << "-" << filename << ".vtu";
15 
16  ofstream out(name.str().c_str());
17  if(!out.is_open()){
18  stringstream ss;
19  ss << filename << "*.vtu cannot be opened and it won't be written.";
20  writeLog(ss.str());
21  return;
22  }
23  int nofNodes = nodes.size();
24  int nofGhostNodes = ghostNodes.size();
25  int nofOctants = connectivity.size();
26  int nofGhosts = ghostConnectivity.size();
27  int nofAll = nofGhosts + nofOctants;
28  out << "<?xml version=\"1.0\"?>" << endl
29  << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
30  << " <UnstructuredGrid>" << endl
31  << " <Piece NumberOfCells=\"" << connectivity.size() + ghostConnectivity.size() << "\" NumberOfPoints=\"" << nodes.size() + ghostNodes.size() << "\">" << endl;
32  out << " <Points>" << endl
33  << " <DataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\""<< 3 <<"\" format=\"ascii\">" << endl
34  << " " << std::fixed;
35  for(int i = 0; i < nofNodes; i++)
36  {
37  for(int j = 0; j < 3; ++j)
38  out << std::setprecision(6) << nodes[i][j] << " ";
39  if((i+1)%4==0 && i!=nofNodes-1)
40  out << endl << " ";
41  }
42  for(int i = 0; i < nofGhostNodes; i++)
43  {
44  for(int j = 0; j < 3; ++j)
45  out << std::setprecision(6) << ghostNodes[i][j] << " ";
46  if((i+1)%4==0 && i!=nofNodes-1)
47  out << endl << " ";
48  }
49  out << endl << " </DataArray>" << endl
50  << " </Points>" << endl
51  << " <Cells>" << endl
52  << " <DataArray type=\"UInt64\" Name=\"connectivity\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
53  << " ";
54  for(int i = 0; i < nofOctants; i++)
55  {
56  for(int j = 0; j < global2D.nnodes; j++)
57  {
58  int jj;
59  if (j<2){
60  jj = j;
61  }
62  else if(j==2){
63  jj = 3;
64  }
65  else if(j==3){
66  jj = 2;
67  }
68  out << connectivity[i][jj] << " ";
69  }
70  if((i+1)%3==0 && i!=nofOctants-1)
71  out << endl << " ";
72  }
73  for(int i = 0; i < nofGhosts; i++)
74  {
75  for(int j = 0; j < global2D.nnodes; j++)
76  {
77  int jj;
78  if (j<2){
79  jj = j;
80  }
81  else if(j==2){
82  jj = 3;
83  }
84  else if(j==3){
85  jj = 2;
86  }
87  out << ghostConnectivity[i][jj] + nofNodes << " ";
88  }
89  if((i+1)%3==0 && i!=nofGhosts-1)
90  out << endl << " ";
91  }
92  out << endl << " </DataArray>" << endl
93  << " <DataArray type=\"UInt64\" Name=\"offsets\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
94  << " ";
95  for(int i = 0; i < nofAll; i++)
96  {
97  out << (i+1)*global2D.nnodes << " ";
98  if((i+1)%12==0 && i!=nofAll-1)
99  out << endl << " ";
100  }
101  out << endl << " </DataArray>" << endl
102  << " <DataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
103  << " ";
104  for(int i = 0; i < nofAll; i++)
105  {
106  int type;
107  type = 9;
108  out << type << " ";
109  if((i+1)%12==0 && i!=nofAll-1)
110  out << endl << " ";
111  }
112  out << endl << " </DataArray>" << endl
113  << " </Cells>" << endl
114  << " </Piece>" << endl
115  << " </UnstructuredGrid>" << endl
116  << "</VTKFile>" << endl;
117 
118 
119  if(ptree.rank == 0){
120  name.str("");
121  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-" << filename << ".pvtu";
122  ofstream pout(name.str().c_str());
123  if(!pout.is_open()){
124  stringstream ss;
125  ss << filename << "*.pvtu cannot be opened and it won't be written.";
126  writeLog(ss.str());
127  return;
128  }
129 
130  pout << "<?xml version=\"1.0\"?>" << endl
131  << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
132  << " <PUnstructuredGrid GhostLevel=\"0\">" << endl
133  << " <PPointData>" << endl
134  << " </PPointData>" << endl
135  << " <PCellData Scalars=\"\">" << endl;
136  pout << " </PCellData>" << endl
137  << " <PPoints>" << endl
138  << " <PDataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\"3\"/>" << endl
139  << " </PPoints>" << endl;
140  for(int i = 0; i < ptree.nproc; i++)
141  pout << " <Piece Source=\"s" << std::setw(4) << std::setfill('0') << ptree.nproc << "-p" << std::setw(4) << std::setfill('0') << i << "-" << filename << ".vtu\"/>" << endl;
142  pout << " </PUnstructuredGrid>" << endl
143  << "</VTKFile>";
144 
145  pout.close();
146 
147  }
148 #if NOMPI==0
149  MPI_Barrier(MPI_COMM_WORLD);
150 #endif
151 
152 }
153 
154 // ----------------------------------------------------------------------------------- //
155 
156 void writePhysicalTree(const vector<vector<double> >& nodes, const u32vector2D& connectivity,
157  const vector<vector<double> >& ghostNodes, const u32vector2D& ghostConnectivity,
158  const Class_Para_Tree<2> & ptree, string filename) {
159 
160  stringstream name;
161  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-p" << std::setfill('0') << std::setw(4) << ptree.rank << "-" << filename << ".vtu";
162 
163  ofstream out(name.str().c_str());
164  if(!out.is_open()){
165  stringstream ss;
166  ss << filename << "*.vtu cannot be opened and it won't be written.";
167  writeLog(ss.str());
168  return;
169  }
170  int nofNodes = nodes.size();
171  int nofGhostNodes = ghostNodes.size();
172  int nofOctants = connectivity.size();
173  int nofGhosts = ghostConnectivity.size();
174  int nofAll = nofGhosts + nofOctants;
175  out << "<?xml version=\"1.0\"?>" << endl
176  << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
177  << " <UnstructuredGrid>" << endl
178  << " <Piece NumberOfCells=\"" << connectivity.size() + ghostConnectivity.size() << "\" NumberOfPoints=\"" << nodes.size() + ghostNodes.size() << "\">" << endl;
179  out << " <Points>" << endl
180  << " <DataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\""<< 3 <<"\" format=\"ascii\">" << endl
181  << " " << std::fixed;
182  for(int i = 0; i < nofNodes; i++)
183  {
184  for(int j = 0; j < 3; ++j)
185  out << std::setprecision(6) << nodes[i][j] << " ";
186  if((i+1)%4==0 && i!=nofNodes-1)
187  out << endl << " ";
188  }
189  for(int i = 0; i < nofGhostNodes; i++)
190  {
191  for(int j = 0; j < 3; ++j)
192  out << std::setprecision(6) << ghostNodes[i][j] << " ";
193  if((i+1)%4==0 && i!=nofNodes-1)
194  out << endl << " ";
195  }
196  out << endl << " </DataArray>" << endl
197  << " </Points>" << endl
198  << " <Cells>" << endl
199  << " <DataArray type=\"UInt64\" Name=\"connectivity\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
200  << " ";
201  for(int i = 0; i < nofOctants; i++)
202  {
203  for(int j = 0; j < global2D.nnodes; j++)
204  {
205  int jj;
206  if (j<2){
207  jj = j;
208  }
209  else if(j==2){
210  jj = 3;
211  }
212  else if(j==3){
213  jj = 2;
214  }
215  out << connectivity[i][jj] << " ";
216  }
217  if((i+1)%3==0 && i!=nofOctants-1)
218  out << endl << " ";
219  }
220  for(int i = 0; i < nofGhosts; i++)
221  {
222  for(int j = 0; j < global2D.nnodes; j++)
223  {
224  int jj;
225  if (j<2){
226  jj = j;
227  }
228  else if(j==2){
229  jj = 3;
230  }
231  else if(j==3){
232  jj = 2;
233  }
234  out << ghostConnectivity[i][jj] + nofNodes << " ";
235  }
236  if((i+1)%3==0 && i!=nofGhosts-1)
237  out << endl << " ";
238  }
239  out << endl << " </DataArray>" << endl
240  << " <DataArray type=\"UInt64\" Name=\"offsets\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
241  << " ";
242  for(int i = 0; i < nofAll; i++)
243  {
244  out << (i+1)*global2D.nnodes << " ";
245  if((i+1)%12==0 && i!=nofAll-1)
246  out << endl << " ";
247  }
248  out << endl << " </DataArray>" << endl
249  << " <DataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
250  << " ";
251  for(int i = 0; i < nofAll; i++)
252  {
253  int type;
254  type = 9;
255  out << type << " ";
256  if((i+1)%12==0 && i!=nofAll-1)
257  out << endl << " ";
258  }
259  out << endl << " </DataArray>" << endl
260  << " </Cells>" << endl
261  << " </Piece>" << endl
262  << " </UnstructuredGrid>" << endl
263  << "</VTKFile>" << endl;
264 
265 
266  if(ptree.rank == 0){
267  name.str("");
268  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-" << filename << ".pvtu";
269  ofstream pout(name.str().c_str());
270  if(!pout.is_open()){
271  stringstream ss;
272  ss << filename << "*.pvtu cannot be opened and it won't be written.";
273  writeLog(ss.str());
274  return;
275  }
276 
277  pout << "<?xml version=\"1.0\"?>" << endl
278  << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
279  << " <PUnstructuredGrid GhostLevel=\"0\">" << endl
280  << " <PPointData>" << endl
281  << " </PPointData>" << endl
282  << " <PCellData Scalars=\"\">" << endl;
283  pout << " </PCellData>" << endl
284  << " <PPoints>" << endl
285  << " <PDataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\"3\"/>" << endl
286  << " </PPoints>" << endl;
287  for(int i = 0; i < ptree.nproc; i++)
288  pout << " <Piece Source=\"s" << std::setw(4) << std::setfill('0') << ptree.nproc << "-p" << std::setw(4) << std::setfill('0') << i << "-" << filename << ".vtu\"/>" << endl;
289  pout << " </PUnstructuredGrid>" << endl
290  << "</VTKFile>";
291 
292  pout.close();
293 
294  }
295 #if NOMPI==0
296  MPI_Barrier(MPI_COMM_WORLD);
297 #endif
298 
299 }
300 
301 // ----------------------------------------------------------------------------------- //
302 
303 void writeLocalTree(const u32vector2D& nodes, const u32vector2D& connectivity,
304  const u32vector2D& ghostNodes, const u32vector2D& ghostConnectivity,
305  const Class_Para_Tree<3> & ptree, string filename) {
306 
307  stringstream name;
308  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-p" << std::setfill('0') << std::setw(4) << ptree.rank << "-" << filename << ".vtu";
309 
310  ofstream out(name.str().c_str());
311  if(!out.is_open()){
312  stringstream ss;
313  ss << filename << "*.vtu cannot be opened and it won't be written.";
314  writeLog(ss.str());
315  return;
316  }
317  int nofNodes = nodes.size();
318  int nofGhostNodes = ghostNodes.size();
319  int nofOctants = connectivity.size();
320  int nofGhosts = ghostConnectivity.size();
321  int nofAll = nofGhosts + nofOctants;
322  out << "<?xml version=\"1.0\"?>" << endl
323  << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
324  << " <UnstructuredGrid>" << endl
325  << " <Piece NumberOfCells=\"" << connectivity.size() + ghostConnectivity.size() << "\" NumberOfPoints=\"" << nodes.size() + ghostNodes.size() << "\">" << endl;
326  out << " <Points>" << endl
327  << " <DataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\""<< 3 <<"\" format=\"ascii\">" << endl
328  << " " << std::fixed;
329  for(int i = 0; i < nofNodes; i++)
330  {
331  for(int j = 0; j < 3; ++j)
332  out << std::setprecision(6) << nodes[i][j] << " ";
333  if((i+1)%4==0 && i!=nofNodes-1)
334  out << endl << " ";
335  }
336  for(int i = 0; i < nofGhostNodes; i++)
337  {
338  for(int j = 0; j < 3; ++j)
339  out << std::setprecision(6) << ghostNodes[i][j] << " ";
340  if((i+1)%4==0 && i!=nofNodes-1)
341  out << endl << " ";
342  }
343  out << endl << " </DataArray>" << endl
344  << " </Points>" << endl
345  << " <Cells>" << endl
346  << " <DataArray type=\"UInt64\" Name=\"connectivity\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
347  << " ";
348  for(int i = 0; i < nofOctants; i++)
349  {
350  for(int j = 0; j < global3D.nnodes; j++)
351  {
352  out << connectivity[i][j] << " ";
353  }
354  if((i+1)%3==0 && i!=nofOctants-1)
355  out << endl << " ";
356  }
357  for(int i = 0; i < nofGhosts; i++)
358  {
359  for(int j = 0; j < global3D.nnodes; j++)
360  {
361  out << ghostConnectivity[i][j] + nofNodes << " ";
362  }
363  if((i+1)%3==0 && i!=nofGhosts-1)
364  out << endl << " ";
365  }
366  out << endl << " </DataArray>" << endl
367  << " <DataArray type=\"UInt64\" Name=\"offsets\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
368  << " ";
369  for(int i = 0; i < nofAll; i++)
370  {
371  out << (i+1)*global2D.nnodes << " ";
372  if((i+1)%12==0 && i!=nofAll-1)
373  out << endl << " ";
374  }
375  out << endl << " </DataArray>" << endl
376  << " <DataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
377  << " ";
378  for(int i = 0; i < nofAll; i++)
379  {
380  int type;
381  type = 11;
382  out << type << " ";
383  if((i+1)%12==0 && i!=nofAll-1)
384  out << endl << " ";
385  }
386  out << endl << " </DataArray>" << endl
387  << " </Cells>" << endl
388  << " </Piece>" << endl
389  << " </UnstructuredGrid>" << endl
390  << "</VTKFile>" << endl;
391 
392 
393  if(ptree.rank == 0){
394  name.str("");
395  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-" << filename << ".pvtu";
396  ofstream pout(name.str().c_str());
397  if(!pout.is_open()){
398  stringstream ss;
399  ss << filename << "*.pvtu cannot be opened and it won't be written.";
400  writeLog(ss.str());
401  return;
402  }
403 
404  pout << "<?xml version=\"1.0\"?>" << endl
405  << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
406  << " <PUnstructuredGrid GhostLevel=\"0\">" << endl
407  << " <PPointData>" << endl
408  << " </PPointData>" << endl
409  << " <PCellData Scalars=\"\">" << endl;
410  pout << " </PCellData>" << endl
411  << " <PPoints>" << endl
412  << " <PDataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\"3\"/>" << endl
413  << " </PPoints>" << endl;
414  for(int i = 0; i < ptree.nproc; i++)
415  pout << " <Piece Source=\"s" << std::setw(4) << std::setfill('0') << ptree.nproc << "-p" << std::setw(4) << std::setfill('0') << i << "-" << filename << ".vtu\"/>" << endl;
416  pout << " </PUnstructuredGrid>" << endl
417  << "</VTKFile>";
418 
419  pout.close();
420 
421  }
422 #if NOMPI==0
423  MPI_Barrier(MPI_COMM_WORLD);
424 #endif
425 
426 }
427 
428 // ----------------------------------------------------------------------------------- //
429 
430 void writePhysicalTree(const vector<vector<double> >& nodes, const u32vector2D& connectivity,
431  const vector<vector<double> >& ghostNodes, const u32vector2D& ghostConnectivity,
432  const Class_Para_Tree<3> & ptree, string filename) {
433 
434  stringstream name;
435  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-p" << std::setfill('0') << std::setw(4) << ptree.rank << "-" << filename << ".vtu";
436 
437  ofstream out(name.str().c_str());
438  if(!out.is_open()){
439  stringstream ss;
440  ss << filename << "*.vtu cannot be opened and it won't be written.";
441  writeLog(ss.str());
442  return;
443  }
444  int nofNodes = nodes.size();
445  int nofGhostNodes = ghostNodes.size();
446  int nofOctants = connectivity.size();
447  int nofGhosts = ghostConnectivity.size();
448  int nofAll = nofGhosts + nofOctants;
449  out << "<?xml version=\"1.0\"?>" << endl
450  << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
451  << " <UnstructuredGrid>" << endl
452  << " <Piece NumberOfCells=\"" << connectivity.size() + ghostConnectivity.size() << "\" NumberOfPoints=\"" << nodes.size() + ghostNodes.size() << "\">" << endl;
453  out << " <Points>" << endl
454  << " <DataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\""<< 3 <<"\" format=\"ascii\">" << endl
455  << " " << std::fixed;
456  for(int i = 0; i < nofNodes; i++)
457  {
458  for(int j = 0; j < 3; ++j)
459  out << std::setprecision(6) << nodes[i][j] << " ";
460  if((i+1)%4==0 && i!=nofNodes-1)
461  out << endl << " ";
462  }
463  for(int i = 0; i < nofGhostNodes; i++)
464  {
465  for(int j = 0; j < 3; ++j)
466  out << std::setprecision(6) << ghostNodes[i][j] << " ";
467  if((i+1)%4==0 && i!=nofNodes-1)
468  out << endl << " ";
469  }
470  out << endl << " </DataArray>" << endl
471  << " </Points>" << endl
472  << " <Cells>" << endl
473  << " <DataArray type=\"UInt64\" Name=\"connectivity\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
474  << " ";
475  for(int i = 0; i < nofOctants; i++)
476  {
477  for(int j = 0; j < global3D.nnodes; j++)
478  {
479  out << connectivity[i][j] << " ";
480  }
481  if((i+1)%3==0 && i!=nofOctants-1)
482  out << endl << " ";
483  }
484  for(int i = 0; i < nofGhosts; i++)
485  {
486  for(int j = 0; j < global3D.nnodes; j++)
487  {
488  out << ghostConnectivity[i][j] + nofNodes << " ";
489  }
490  if((i+1)%3==0 && i!=nofGhosts-1)
491  out << endl << " ";
492  }
493  out << endl << " </DataArray>" << endl
494  << " <DataArray type=\"UInt64\" Name=\"offsets\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
495  << " ";
496  for(int i = 0; i < nofAll; i++)
497  {
498  out << (i+1)*global2D.nnodes << " ";
499  if((i+1)%12==0 && i!=nofAll-1)
500  out << endl << " ";
501  }
502  out << endl << " </DataArray>" << endl
503  << " <DataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\" format=\"ascii\">" << endl
504  << " ";
505  for(int i = 0; i < nofAll; i++)
506  {
507  int type;
508  type = 11;
509  out << type << " ";
510  if((i+1)%12==0 && i!=nofAll-1)
511  out << endl << " ";
512  }
513  out << endl << " </DataArray>" << endl
514  << " </Cells>" << endl
515  << " </Piece>" << endl
516  << " </UnstructuredGrid>" << endl
517  << "</VTKFile>" << endl;
518 
519 
520  if(ptree.rank == 0){
521  name.str("");
522  name << "s" << std::setfill('0') << std::setw(4) << ptree.nproc << "-" << filename << ".pvtu";
523  ofstream pout(name.str().c_str());
524  if(!pout.is_open()){
525  stringstream ss;
526  ss << filename << "*.pvtu cannot be opened and it won't be written.";
527  writeLog(ss.str());
528  return;
529  }
530 
531  pout << "<?xml version=\"1.0\"?>" << endl
532  << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">" << endl
533  << " <PUnstructuredGrid GhostLevel=\"0\">" << endl
534  << " <PPointData>" << endl
535  << " </PPointData>" << endl
536  << " <PCellData Scalars=\"\">" << endl;
537  pout << " </PCellData>" << endl
538  << " <PPoints>" << endl
539  << " <PDataArray type=\"Float64\" Name=\"Coordinates\" NumberOfComponents=\"3\"/>" << endl
540  << " </PPoints>" << endl;
541  for(int i = 0; i < ptree.nproc; i++)
542  pout << " <Piece Source=\"s" << std::setw(4) << std::setfill('0') << ptree.nproc << "-p" << std::setw(4) << std::setfill('0') << i << "-" << filename << ".vtu\"/>" << endl;
543  pout << " </PUnstructuredGrid>" << endl
544  << "</VTKFile>";
545 
546  pout.close();
547 
548  }
549 #if NOMPI==0
550  MPI_Barrier(MPI_COMM_WORLD);
551 #endif
552  // ----------------------------------------------------------------------------------- //
553 
554 }
Parallel Octree Manager Class - 3D specialization.
Parallel Octree Manager Class - 2D specialization.