source: src/comm/comm_serial.cpp@ d24c2f

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

Fixed typo.

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

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