source: src/comm/comm_serial.cpp@ dfed1c

Last change on this file since dfed1c was dfed1c, checked in by Julian Iseringhausen <isering@…>, 14 years ago

Major vmg update.

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1136 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 23.9 KB
Line 
1/**
2 * @file comm_serial.cpp
3 * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
4 * @date Mon Apr 18 12:28:12 2011
5 *
6 * @brief VMG::CommSerial
7 *
8 */
9
10#ifdef HAVE_CONFIG_H
11#include <config.h>
12#endif
13
14#ifdef HAVE_BOOST_FILESYSTEM
15#include <boost/filesystem.hpp>
16namespace fs = boost::filesystem;
17#endif
18
19#include <cstdarg>
20#include <cstdio>
21#include <sstream>
22
23#include "base/discretization.hpp"
24#include "base/helper.hpp"
25#include "base/index.hpp"
26#include "base/linked_cell_list.hpp"
27#include "base/stencil.hpp"
28#include "base/vector.hpp"
29#include "comm/comm_serial.hpp"
30#include "comm/comm.hpp"
31#include "grid/multigrid.hpp"
32#include "grid/tempgrid.hpp"
33#include "mg.hpp"
34
35using namespace VMG;
36
37static char print_buffer[512];
38
39void CommSerial::InitCommSerial()
40{
41 error_norm_count = 0;
42 residual_count = 0;
43}
44
45void CommSerial::CommSubgrid(Grid& grid_old, Grid& grid_new)
46{
47 Grid::iterator iter;
48
49 if (&grid_old == &grid_new)
50 return;
51
52 for (iter = grid_old.Iterators().CompleteGrid().Begin(); iter != grid_old.Iterators().CompleteGrid().End(); ++iter)
53 grid_new(*iter) = grid_old.GetVal(*iter);
54}
55
56void CommSerial::CommAddSubgrid(Grid& grid_old, Grid& grid_new)
57{
58 Grid::iterator iter;
59
60 if (&grid_old == &grid_new)
61 return;
62
63 for (iter = grid_old.Iterators().CompleteGrid().Begin(); iter != grid_old.Iterators().CompleteGrid().End(); ++iter)
64 grid_new(*iter) += grid_old.GetVal(*iter);
65}
66
67Grid& CommSerial::GetFinerGrid(Multigrid& multigrid)
68{
69 return multigrid(multigrid.Level()+1);
70}
71
72Grid& CommSerial::GetCoarserGrid(Multigrid& multigrid)
73{
74 return multigrid(multigrid.Level()-1);
75}
76
77void CommSerial::CommFromGhosts(Grid& mesh)
78{
79 Grid::iterator iter;
80
81 const GridIteratorSuite& iterators = mesh.Iterators();
82 const Index& size = mesh.Local().Size();
83
84 if (BoundaryConditions().X() == Periodic) {
85
86 for (iter = iterators.Halo1().X().Begin(); iter != iterators.Halo1().X().End(); ++iter)
87 mesh((*iter).X()+size.X(), (*iter).Y(), (*iter).Z()) += mesh.GetVal(*iter);
88
89 for (iter = iterators.Halo2().X().Begin(); iter != iterators.Halo2().X().End(); ++iter)
90 mesh((*iter).X()-size.X(), (*iter).Y(), (*iter).Z()) += mesh.GetVal(*iter);
91 }
92
93 if (BoundaryConditions().Y() == Periodic) {
94
95 for (iter = iterators.Halo1().Y().Begin(); iter != iterators.Halo1().Y().End(); ++iter)
96 mesh((*iter).X(), (*iter).Y()+size.Y(), (*iter).Z()) += mesh.GetVal(*iter);
97
98 for (iter = iterators.Halo2().Y().Begin(); iter != iterators.Halo2().Y().End(); ++iter)
99 mesh((*iter).X(), (*iter).Y()-size.Y(), (*iter).Z()) += mesh.GetVal(*iter);
100
101 }
102
103 if (BoundaryConditions().Z() == Periodic) {
104
105 for (iter = iterators.Halo1().Z().Begin(); iter != iterators.Halo1().Z().End(); ++iter)
106 mesh((*iter).X(), (*iter).Y(), (*iter).Z()+size.Z()) += mesh.GetVal(*iter);
107
108 for (iter = iterators.Halo2().Z().Begin(); iter != iterators.Halo2().Z().End(); ++iter)
109 mesh((*iter).X(), (*iter).Y(), (*iter).Z()-size.Z()) += mesh.GetVal(*iter);
110
111 }
112
113#ifdef DEBUG_MATRIX_CHECKS
114 mesh.IsConsistent();
115#endif
116}
117
118void CommSerial::CommToGhosts(Grid& mesh)
119{
120 Grid::iterator iter;
121
122 const GridIteratorSuite& iterators = mesh.Iterators();
123 const Index& size = mesh.Local().Size();
124
125 if (BoundaryConditions().Z() == Periodic) {
126
127 for (iter = iterators.Halo1().Z().Begin(); iter != iterators.Halo1().Z().End(); ++iter)
128 mesh(*iter) = mesh.GetVal((*iter).X(), (*iter).Y(), (*iter).Z()+size.Z());
129
130 for (iter = iterators.Halo2().Z().Begin(); iter != iterators.Halo2().Z().End(); ++iter)
131 mesh(*iter) = mesh.GetVal((*iter).X(), (*iter).Y(), (*iter).Z()-size.Z());
132
133 }
134
135 if (BoundaryConditions().Y() == Periodic) {
136
137 for (iter = iterators.Halo1().Y().Begin(); iter != iterators.Halo1().Y().End(); ++iter)
138 mesh(*iter) = mesh.GetVal((*iter).X(), (*iter).Y()+size.Y(), (*iter).Z());
139
140 for (iter = iterators.Halo2().Y().Begin(); iter != iterators.Halo2().Y().End(); ++iter)
141 mesh(*iter) = mesh.GetVal((*iter).X(), (*iter).Y()-size.Y(), (*iter).Z());
142
143 }
144
145 if (BoundaryConditions().X() == Periodic) {
146
147 for (iter = iterators.Halo1().X().Begin(); iter != iterators.Halo1().X().End(); ++iter)
148 mesh(*iter) = mesh.GetVal((*iter).X()+size.X(), (*iter).Y(), (*iter).Z());
149
150 for (iter = iterators.Halo2().X().Begin(); iter != iterators.Halo2().X().End(); ++iter)
151 mesh(*iter) = mesh.GetVal((*iter).X()-size.X(), (*iter).Y(), (*iter).Z());
152
153 }
154
155#ifdef DEBUG_MATRIX_CHECKS
156 mesh.IsConsistent();
157#endif
158}
159
160Grid& CommSerial::GetGlobalCoarseGrid(Multigrid& multigrid)
161{
162 return multigrid(multigrid.MinLevel());
163}
164
165void CommSerial::CommParticles(const Grid& grid, std::list<Particle::Particle>& particles)
166{
167 Factory& factory = MG::GetFactory();
168 const vmg_int& num_particles_local = factory.GetObjectStorageVal<vmg_int>("PARTICLE_NUM_LOCAL");
169 vmg_float* x = factory.GetObjectStorageArray<vmg_float>("PARTICLE_POS_ARRAY");
170 vmg_float* q = factory.GetObjectStorageArray<vmg_float>("PARTICLE_CHARGE_ARRAY");
171
172 particles.clear();
173
174 for (vmg_int i=0; i<num_particles_local; ++i)
175 particles.push_back(Particle::Particle(&x[3*i], q[i], 0.0, 0.0, 0, i));
176}
177
178void CommSerial::CommLCListGhosts(const Grid& grid, Particle::LinkedCellList& lc)
179{
180 std::list<Particle::Particle>::iterator p_iter;
181 Grid::iterator g_iter;
182
183 for (int i=2; i>=0; --i) {
184
185 if (BoundaryConditions()[i] == Periodic) {
186
187 for (g_iter=lc.Iterators().Boundary1()[i].Begin(); g_iter!=lc.Iterators().Boundary1()[i].End(); ++g_iter)
188 lc(*g_iter).clear();
189
190 for (g_iter=lc.Iterators().NearBoundary1()[i].Begin(); g_iter!=lc.Iterators().NearBoundary1()[i].End(); ++g_iter)
191 for (p_iter=lc(*g_iter).begin(); p_iter!=lc(*g_iter).end(); ++p_iter)
192 lc.AddParticleToComplete(p_iter->Pos() + grid.Extent().Size(), p_iter->Charge(),
193 p_iter->Pot(), p_iter->Force(),
194 p_iter->Rank(), p_iter->Index());
195
196 for (g_iter=lc.Iterators().Boundary2()[i].Begin(); g_iter!=lc.Iterators().Boundary2()[i].End(); ++g_iter)
197 lc(*g_iter).clear();
198
199 for (g_iter=lc.Iterators().NearBoundary2()[i].Begin(); g_iter!=lc.Iterators().NearBoundary2()[i].End(); ++g_iter)
200 for (p_iter=lc(*g_iter).begin(); p_iter!=lc(*g_iter).end(); ++p_iter)
201 lc.AddParticleToComplete(p_iter->Pos() - grid.Extent().Size(), p_iter->Charge(),
202 p_iter->Pot(), p_iter->Force(),
203 p_iter->Rank(), p_iter->Index());
204
205 }
206 }
207}
208
209void CommSerial::CommAddPotential(std::list<Particle::Particle>& particles)
210{
211 std::list<Particle::Particle>::iterator iter;
212 vmg_float* p = MG::GetFactory().GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
213
214 for (iter=particles.begin(); iter!=particles.end(); ++iter)
215 p[iter->Index()] += iter->Pot();
216}
217
218void CommSerial::CommAddPotential(Particle::LinkedCellList& lc)
219{
220 Grid::iterator lc_iter;
221 std::list<Particle::Particle>::iterator p_iter;
222 vmg_float* p = MG::GetFactory().GetObjectStorageArray<vmg_float>("PARTICLE_POTENTIAL_ARRAY");
223
224 for (lc_iter=lc.Iterators().Local().Begin(); lc_iter!=lc.Iterators().Local().End(); ++lc_iter)
225 for (p_iter=lc(*lc_iter).begin(); p_iter!=lc(*lc_iter).end(); ++p_iter)
226 p[p_iter->Index()] += p_iter->Pot();
227}
228
229void CommSerial::PrintString(const char* format, ...)
230{
231 va_list args;
232 va_start(args, format);
233 vsprintf(print_buffer, format, args);
234 printf("VMG: %s\n", print_buffer);
235 va_end(args);
236}
237
238void CommSerial::PrintStringOnce(const char* format, ...)
239{
240 va_list args;
241 va_start(args, format);
242 vsprintf(print_buffer, format, args);
243 printf("VMG: %s\n", print_buffer);
244 va_end(args);
245}
246
247void CommSerial::PrintXML(const std::string& filename, const std::string& xml_data)
248{
249 pugi::xml_document doc;
250 doc.load(xml_data.c_str());
251
252 pugi::xml_node node_global = doc.prepend_child("Global");
253 pugi::xml_node node_num_processes = node_global.append_child("NumProcesses");
254 pugi::xml_node node_num_processes_data = node_num_processes.append_child(pugi::node_pcdata);
255 node_num_processes_data.set_value(Helper::ToString(GlobalProcs().Product()).c_str());
256
257 doc.save_file(filename.c_str());
258}
259
260void CommSerial::PrintXMLAll(const std::string& filename, const std::string& xml_data)
261{
262 PrintXML(filename, xml_data);
263}
264
265void CommSerial::OpenFileAndPrintHeader(std::ofstream& out, const Grid& mesh, const char* information, bool inner)
266{
267 char path_str[129];
268 int count = OutputCount();
269
270 sprintf(path_str, "%s%04d.dat", OutputPath().c_str(), count);
271
272 out.open(path_str, std::ios::trunc);
273
274 if (!out.good()) {
275#ifdef DEBUG_OUTPUT
276 printf("Multigrid: File %s not accessible.\n", path_str);
277#endif /* DEBUG_OUTPUT */
278 return;
279 }
280
281 out << "# vtk DataFile Version 2.0" << std::endl
282 << count << ": " << information << std::endl
283 << "ASCII" << std::endl
284 << "DATASET STRUCTURED_POINTS" << std::endl;
285
286 if (inner) {
287 out << "DIMENSIONS "
288 << mesh.Local().Size().X() << " "
289 << mesh.Local().Size().Y() << " "
290 << mesh.Local().Size().Z() << std::endl;
291 }else {
292 out << "DIMENSIONS "
293 << mesh.Local().SizeTotal().X() << " "
294 << mesh.Local().SizeTotal().Y() << " "
295 << mesh.Local().SizeTotal().Z() << std::endl;
296 }
297
298 out << "ORIGIN 0 0 0" << std::endl
299 << "SPACING " << mesh.MeshWidth() << " "
300 << mesh.MeshWidth() << " "
301 << mesh.MeshWidth() << std::endl;
302
303 if (inner)
304 out << "POINT_DATA " << mesh.Local().Size().Product() << std::endl;
305 else
306 out << "POINT_DATA " << mesh.Local().SizeTotal().Product() << std::endl;
307
308 out << "SCALARS residual double 1" << std::endl
309 << "LOOKUP_TABLE default" << std::endl;
310}
311
312void CommSerial::PrintDefect(Grid& sol, Grid& rhs, const char* information)
313{
314 std::ofstream out_file;
315 std::stringstream out;
316
317 TempGrid *temp = MG::GetTempGrid();
318 temp->SetProperties(sol);
319 temp->ImportFromResidual(sol, rhs);
320
321#ifdef DEBUG_MATRIX_CHECKS
322 temp->IsConsistent();
323#endif
324
325 OpenFileAndPrintHeader(out_file, *temp, information, true);
326
327 assert(out_file.good());
328
329 if (!out_file.good())
330 return;
331
332 for (int k=temp->Local().Begin().Z(); k<temp->Local().End().Z(); k++)
333 for (int j=temp->Local().Begin().Y(); j<temp->Local().End().Y(); j++)
334 for (int i=temp->Local().Begin().X(); i<temp->Local().End().X(); i++)
335 out << std::scientific << temp->GetVal(i,j,k) << std::endl;
336
337 out_file << out.str();
338
339 out_file.close();
340}
341
342void CommSerial::PrintGrid(Grid& mesh, const char* information)
343{
344 std::stringstream out;
345 std::ofstream out_file;
346
347 CommToGhosts(mesh);
348
349 OpenFileAndPrintHeader(out_file, mesh, information, false);
350
351 for (int k=0; k<mesh.Local().SizeTotal().Z(); k++)
352 for (int j=0; j<mesh.Local().SizeTotal().Y(); j++)
353 for (int i=0; i<mesh.Local().SizeTotal().X(); i++)
354 out << std::scientific << mesh.GetVal(i,j,k) << std::endl;
355
356 out_file << out.str();
357 out_file.close();
358}
359
360void CommSerial::PrintCorrectedGrid(Grid& mesh, const char* information)
361{
362 std::ofstream out_file;
363 std::stringstream out;
364
365 CommToGhosts(mesh);
366
367 OpenFileAndPrintHeader(out_file, mesh, information, false);
368
369 for (int k=0; k<mesh.Local().SizeTotal().Z(); k++)
370 for (int j=0; j<mesh.Local().SizeTotal().Y(); j++)
371 for (int i=0; i<mesh.Local().SizeTotal().X(); i++)
372 out << std::scientific << mesh.GetCorrectedVal(i,j,k) << std::endl;
373
374 out_file << out.str();
375
376 out_file.close();
377}
378
379void CommSerial::PrintInnerGrid(Grid& mesh, const char* information)
380{
381 std::ofstream out_file;
382 std::stringstream out;
383
384 CommToGhosts(mesh);
385
386 OpenFileAndPrintHeader(out_file, mesh, information, true);
387
388 for (int k=mesh.Local().Begin().Z(); k<mesh.Local().End().Z(); k++)
389 for (int j=mesh.Local().Begin().Y(); j<mesh.Local().End().Y(); j++)
390 for (int i=mesh.Local().Begin().X(); i<mesh.Local().End().X(); i++)
391 out << std::scientific << mesh.GetVal(i,j,k) << std::endl;
392
393 out_file << out.str();
394
395 out_file.close();
396}
397
398void CommSerial::PrintInnerCorrectedGrid(Grid& mesh, const char* information)
399{
400 std::ofstream out_file;
401 std::stringstream out;
402
403 CommToGhosts(mesh);
404
405 OpenFileAndPrintHeader(out_file, mesh, information, true);
406
407 for (int k=mesh.Local().Begin().Z(); k<mesh.Local().End().Z(); k++)
408 for (int j=mesh.Local().Begin().Y(); j<mesh.Local().End().Y(); j++)
409 for (int i=mesh.Local().Begin().X(); i<mesh.Local().End().X(); i++)
410 out << std::scientific << mesh.GetCorrectedVal(i,j,k) << std::endl;
411
412 out_file << out.str();
413
414 out_file.close();
415}
416
417void CommSerial::PrintAllSettings()
418{
419 Multigrid* mg = MG::GetFactory().Get("SOL")->Cast<Multigrid>();
420 std::stringstream buf;
421 std::ofstream out;
422
423 buf << OutputPath() << "settings.txt";
424
425 out.open(buf.str().c_str(), std::ios::trunc);
426
427 for (int i=mg->MinLevel(); i<=mg->MaxLevel(); ++i) {
428
429 out << "GRID LEVEL " << i << std::endl
430
431 << "GLOBAL" << std::endl
432 << "BEGINFINEST: " << (*mg)(i).Global().BeginFinest() << std::endl
433 << "ENDFINEST: " << (*mg)(i).Global().EndFinest() << std::endl
434 << "SIZEFINEST: " << (*mg)(i).Global().SizeFinest() << std::endl
435 << "BEGINLOCAL: " << (*mg)(i).Global().BeginLocal() << std::endl
436 << "ENDLOCAL: " << (*mg)(i).Global().EndLocal() << std::endl
437 << "SIZELOCAL: " << (*mg)(i).Global().SizeLocal() << std::endl
438 << "SIZEGLOBAL: " << (*mg)(i).Global().SizeGlobal() << std::endl
439 << "BOUNDARYTYPE: " << (*mg)(i).Global().BoundaryType() << std::endl
440
441 << "LOCAL" << std::endl
442 << "SIZE: " << (*mg)(i).Local().Size() << std::endl
443 << "SIZETOTAL: " << (*mg)(i).Local().SizeTotal() << std::endl
444 << "BEGIN: " << (*mg)(i).Local().Begin() << std::endl
445 << "END: " << (*mg)(i).Local().End() << std::endl
446 << "HALOBEGIN1: " << (*mg)(i).Local().HaloBegin1() << std::endl
447 << "HALOEND1: " << (*mg)(i).Local().HaloEnd1() << std::endl
448 << "HALOBEGIN2: " << (*mg)(i).Local().HaloBegin2() << std::endl
449 << "HALOEND2: " << (*mg)(i).Local().HaloEnd2() << std::endl
450 << "BOUNDARYBEGIN1: " << (*mg)(i).Local().BoundaryBegin1() << std::endl
451 << "BOUNDARYEND1: " << (*mg)(i).Local().BoundaryEnd1() << std::endl
452 << "BOUNDARYBEGIN2: " << (*mg)(i).Local().BoundaryBegin2() << std::endl
453 << "BOUNDARYEND2: " << (*mg)(i).Local().BoundaryEnd2() << std::endl
454 << "ALIGNMENTBEGIN: " << (*mg)(i).Local().AlignmentBegin() << std::endl
455 << "ALIGNMENTEND: " << (*mg)(i).Local().AlignmentEnd() << std::endl
456
457 << "EXTENT" << std::endl
458 << "SIZE: " << (*mg)(i).Extent().Size() << std::endl
459 << "SIZEFACTOR: " << (*mg)(i).Extent().SizeFactor() << std::endl
460 << "BEGIN: " << (*mg)(i).Extent().Begin() << std::endl
461 << "END: " << (*mg)(i).Extent().End() << std::endl
462 << "MESHWIDTH: " << (*mg)(i).Extent().MeshWidth() << std::endl;
463
464 }
465
466 assert(out.good());
467 out.close();
468}
469
470void CommSerial::DebugPrintError(const Grid& sol, const char* information)
471{
472 Index i;
473 std::ofstream out_file;
474 std::stringstream out;
475
476 OpenFileAndPrintHeader(out_file, sol, information, false);
477
478 for (i.Z()=0; i.Z()<sol.Local().SizeTotal().Z(); ++i.Z())
479 for (i.Y()=0; i.Y()<sol.Local().SizeTotal().Y(); ++i.Y())
480 for (i.X()=0; i.X()<sol.Local().SizeTotal().X(); ++i.X())
481 out << std::scientific << sol.DebugKnownSolution(i) - sol.GetVal(i) << std::endl;
482
483 out_file << out.str();
484
485 out_file.close();
486}
487
488void CommSerial::DebugPrintErrorNorm(Grid& sol)
489{
490#ifdef HAVE_LIBGSL
491 char path_str[129];
492 std::ofstream out;
493 const vmg_float sp = sol.MeshWidth();
494 const vmg_float sp_3_inv = 1.0/(sp*sp*sp);
495 const int numsamples = 1e6;
496 const vmg_float numsamples_inv = 1.0 / numsamples;
497 vmg_float val1, val2, diff;
498
499 vmg_float norm_L1 = 0.0;
500 vmg_float norm_L2 = 0.0;
501 vmg_float norm_Linfty = 0.0;
502 vmg_float norm_u_L1 = 0.0;
503 vmg_float norm_u_L2 = 0.0;
504 vmg_float norm_u_Linfty = 0.0;
505
506 Vector x, x_le, x_ri;
507 Index ind;
508
509 gsl_qrng *q = gsl_qrng_alloc(gsl_qrng_niederreiter_2, 3);
510
511 sprintf(path_str, "%serror.txt", defects_path_str.c_str());
512
513 out.open(path_str, std::ios::app);
514
515 assert(out.good());
516
517 CommToGhosts(sol);
518
519 for (int i=0; i<numsamples; i++) {
520
521 gsl_qrng_get(q, x.vec());
522
523 ind = x / sp;
524
525 x_le = x - sp * static_cast<Vector>(ind);
526 x_ri = sp - x_le;
527
528 for (int j=0; j<3; ++j)
529 if (this->BoundaryConditions()[j] == Periodic)
530 ind[j] += sol.Local().Begin()[j];
531
532 val1 = ( sol.GetVal(ind) * x_ri[0] * x_ri[1] * x_ri[2] +
533 sol.GetVal(ind[0]+1, ind[1] , ind[2] ) * x_le[0] * x_ri[1] * x_ri[2] +
534 sol.GetVal(ind[0] , ind[1]+1, ind[2] ) * x_ri[0] * x_le[1] * x_ri[2] +
535 sol.GetVal(ind[0] , ind[1] , ind[2]+1) * x_ri[0] * x_ri[1] * x_le[2] +
536 sol.GetVal(ind[0]+1, ind[1]+1, ind[2] ) * x_le[0] * x_le[1] * x_ri[2] +
537 sol.GetVal(ind[0]+1, ind[1] , ind[2]+1) * x_le[0] * x_ri[1] * x_le[2] +
538 sol.GetVal(ind[0] , ind[1]+1, ind[2]+1) * x_ri[0] * x_le[1] * x_le[2] +
539 sol.GetVal(ind[0]+1, ind[1]+1, ind[2]+1) * x_le[0] * x_le[1] * x_le[2] ) * sp_3_inv;
540
541 val2 = sol.DebugKnownSolution(x);
542
543 diff = fabs(val1 - val2);
544
545 norm_L1 += diff * numsamples_inv;
546 norm_L2 += diff*diff * numsamples_inv;
547 norm_Linfty = std::max(norm_Linfty, diff);
548
549 norm_u_L1 += fabs(val2) * numsamples_inv;
550 norm_u_L2 += val2*val2 * numsamples_inv;
551 norm_u_Linfty = std::max(norm_u_Linfty, fabs(val2));
552 }
553
554 norm_L2 = sqrt(norm_L2);
555 norm_u_L2 = sqrt(norm_u_L2);
556
557 std::cout << std::scientific << std::endl
558 << "Error L1-norm: " << norm_L1 << std::endl
559 << "Error L2-norm: " << norm_L2 << std::endl
560 << "Error Linfty-norm: " << norm_Linfty << std::endl
561 << "Relative error L1-norm: " << norm_L1 / norm_u_L1 << std::endl
562 << "Relative error L2-norm: " << norm_L2 / norm_u_L2 << std::endl
563 << "Relative error Linfty-norm: " << norm_Linfty / norm_u_Linfty << std::endl;
564
565 gsl_qrng_free(q);
566
567 out << std::scientific << error_norm_count++ << " "
568 << norm_L1 << " "
569 << norm_L2 << " "
570 << norm_Linfty << " "
571 << norm_L1/norm_u_L1 << " "
572 << norm_L2/norm_u_L2 << " "
573 << norm_Linfty/norm_u_Linfty << std::endl;
574
575 out.close();
576
577#endif
578}
579
580inline int GetIndex(const Grid& grid, int i, int j, int k)
581{
582 if (grid.Global().BoundaryType() == LocallyRefined)
583 return k + grid.Local().Size().Z() * (j + grid.Local().Size().Y() * i);
584 else
585 return k + grid.Local().SizeTotal().Z() * (j + grid.Local().SizeTotal().Y() * i);
586}
587
588void CommSerial::PrintGridStructureLevel(Grid& grid, std::ofstream& out)
589{
590 const vmg_float sp = grid.MeshWidth();
591 int numLines;
592
593 if (grid.Global().BoundaryType() == LocallyRefined)
594 numLines = grid.Local().Size().X() * grid.Local().Size().Y() +
595 grid.Local().Size().Y() * grid.Local().Size().Z() +
596 grid.Local().Size().X() * grid.Local().Size().Z();
597 else
598 numLines = grid.Local().SizeTotal().X() * grid.Local().SizeTotal().Y() +
599 grid.Local().SizeTotal().Y() * grid.Local().SizeTotal().Z() +
600 grid.Local().SizeTotal().X() * grid.Local().SizeTotal().Z();
601
602 out << " <Piece";
603
604 if (grid.Global().BoundaryType() == LocallyRefined) {
605 out << " NumberOfPoints=\"" << grid.Local().Size().Product() << "\""
606 << " NumberOfVerts=\"" << grid.Local().Size().Product() << "\"";
607 }else {
608 out << " NumberOfPoints=\"" << grid.Local().SizeTotal().Product() << "\""
609 << " NumberOfVerts=\"" << grid.Local().SizeTotal().Product() << "\"";
610 }
611
612 out << " NumberOfLines=\"" << numLines << "\""
613 << " NumberOfStrips=\"0\""
614 << " NumberOfPolys=\"0\"" << ">" << std::endl
615 << " <Points>" << std::endl
616 << " <DataArray type=\"Float32\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl
617 << " ";
618
619 if (grid.Global().BoundaryType() == LocallyRefined) {
620 for (int i=0; i<grid.Local().Size().X(); i++)
621 for (int j=0; j<grid.Local().Size().Y(); j++)
622 for (int k=0; k<grid.Local().Size().Z(); k++)
623 out << grid.Extent().Begin().X() + i * sp << " "
624 << grid.Extent().Begin().Y() + j * sp << " "
625 << grid.Extent().Begin().Z() + k * sp << " ";
626 }else {
627 for (int i=0; i<grid.Local().SizeTotal().X(); i++)
628 for (int j=0; j<grid.Local().SizeTotal().Y(); j++)
629 for (int k=0; k<grid.Local().SizeTotal().Z(); k++)
630 out << grid.Extent().Begin().X() + i * sp << " "
631 << grid.Extent().Begin().Y() + j * sp << " "
632 << grid.Extent().Begin().Z() + k * sp << " ";
633 }
634
635 out << std::endl
636 << " </DataArray>" << std::endl
637 << " </Points>" << std::endl
638 << " <Verts>" << std::endl
639 << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">" << std::endl
640 << " ";
641
642 if (grid.Global().BoundaryType() == LocallyRefined) {
643 for (int i=0; i<grid.Local().Size().Product(); i++)
644 out << i << " ";
645 }else {
646 for (int i=0; i<grid.Local().SizeTotal().Product(); i++)
647 out << i << " ";
648 }
649
650 out << std::endl
651 << " </DataArray>" << std::endl
652 << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">" << std::endl
653 << " ";
654
655 if (grid.Global().BoundaryType() == LocallyRefined) {
656 for (int i=1; i<=grid.Local().Size().Product(); i++)
657 out << i << " ";
658 }else {
659 for (int i=1; i<=grid.Local().SizeTotal().Product(); i++)
660 out << i << " ";
661 }
662
663 out << std::endl
664 << " </DataArray>" << std::endl
665 << " </Verts>" << std::endl
666 << " <Lines>" << std::endl
667 << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">" << std::endl
668 << " ";
669
670 if (grid.Global().BoundaryType() == LocallyRefined) {
671 for (int i=0; i<grid.Local().Size().X(); i++)
672 for (int j=0; j<grid.Local().Size().Y(); j++)
673 out << GetIndex(grid,i,j,0) << " "
674 << GetIndex(grid,i,j,grid.Local().Size().Z()-1) << " ";
675
676 for (int j=0; j<grid.Local().Size().Y(); j++)
677 for (int k=0; k<grid.Local().Size().Z(); k++)
678 out << GetIndex(grid,0,j,k) << " "
679 << GetIndex(grid,grid.Local().Size().X()-1,j,k) << " ";
680
681 for (int i=0; i<grid.Local().Size().X(); i++)
682 for (int k=0; k<grid.Local().Size().Z(); k++)
683 out << GetIndex(grid,i,0,k) << " "
684 << GetIndex(grid,i,grid.Local().Size().Y()-1,k) << " ";
685 }else {
686 for (int i=0; i<grid.Local().SizeTotal().X(); i++)
687 for (int j=0; j<grid.Local().SizeTotal().Y(); j++)
688 out << GetIndex(grid,i,j,0) << " "
689 << GetIndex(grid,i,j,grid.Local().SizeTotal().Z()-1) << " ";
690
691 for (int j=0; j<grid.Local().SizeTotal().Y(); j++)
692 for (int k=0; k<grid.Local().SizeTotal().Z(); k++)
693 out << GetIndex(grid,0,j,k) << " "
694 << GetIndex(grid,grid.Local().SizeTotal().X()-1,j,k) << " ";
695
696 for (int i=0; i<grid.Local().SizeTotal().X(); i++)
697 for (int k=0; k<grid.Local().SizeTotal().Z(); k++)
698 out << GetIndex(grid,i,0,k) << " "
699 << GetIndex(grid,i,grid.Local().SizeTotal().Y()-1,k) << " ";
700 }
701
702 out << std::endl
703 << " </DataArray>" << std::endl
704 << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">" << std::endl
705 << " ";
706
707 for (int i=1; i<=numLines; i++)
708 out << 2*i << " ";
709
710 out << std::endl
711 << " </DataArray>" << std::endl
712 << " </Lines>" << std::endl
713 << " </Piece>" << std::endl;
714}
715
716void CommSerial::DebugPrintGridStructure(Multigrid& grid)
717{
718 std::ofstream out;
719 char path_str[129];
720
721 sprintf(path_str, "%sgrid.vtp", OutputPath().c_str());
722
723 out.open(path_str, std::ios::trunc);
724
725 if (!out.good()) {
726#ifdef DEBUG_OUTPUT
727 printf("Multigrid: File %s not accessible.\n", path_str);
728#endif /* DEBUG_OUTPUT */
729 return;
730 }
731
732 out << "<?xml version=\"1.0\"?>" << std::endl
733 << "<VTKFile type=\"PolyData\">" << std::endl
734 << " <PolyData>" << std::endl;
735
736 for (int i=grid.MinLevel(); i<=grid.MaxLevel(); i++)
737 PrintGridStructureLevel(grid(i), out);
738
739 out << " </PolyData>" << std::endl
740 << "</VTKFile>" << std::endl;
741
742 out.close();
743}
744
745std::string CommSerial::CreateOutputDirectory()
746{
747#ifdef HAVE_BOOST_FILESYSTEM
748 std::string path, unique_path;
749 std::stringstream unique_suffix;
750 int suffix_counter = 0;
751 char buffer[129];
752 time_t rawtime;
753 struct tm *timeinfo;
754
755 time(&rawtime);
756 timeinfo = localtime(&rawtime);
757 strftime(buffer, 128, "./output/%Y_%m_%d_%H_%M_%S/", timeinfo);
758 path = buffer;
759
760 if (!fs::exists("output"))
761 fs::create_directory("output");
762
763 unique_path = path;
764
765 while (fs::exists(unique_path.c_str())) {
766
767 unique_suffix.str("");
768 unique_suffix << "_" << suffix_counter++ << "/";
769
770 unique_path = path;
771 unique_path.replace(unique_path.size()-1, 1, unique_suffix.str());
772
773 }
774
775 fs::create_directory(unique_path.c_str());
776
777 return unique_path;
778
779#else
780
781 return "./";
782
783#endif
784}
Note: See TracBrowser for help on using the repository browser.