Debug.cc 15.4 KB
Newer Older
1
2
#include <iostream>
#include <fstream>
3
4
5
6
7
#include <boost/lexical_cast.hpp>
#include "Debug.h"
#include "DOFVector.h"
#include "MacroElement.h"
#include "VtkWriter.h"
8
#include "ElementFileWriter.h"
9
#include "ElementDofIterator.h"
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

namespace AMDiS {

  namespace debug {

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    void writeLocalElementDofs(int rank, int elIdx, FiniteElemSpace *feSpace)
    {
      using boost::lexical_cast;
      
      if (MPI::COMM_WORLD.Get_rank() == rank) {
	DOFVector<double> tmp(feSpace, "tmp");
	colorDofVectorByLocalElementDofs(tmp, feSpace->getMesh(), elIdx);
	VtkWriter::writeFile(tmp, "tmp" + lexical_cast<std::string>(elIdx) + ".vtu");
      }
    }
26

27
28
29
30
31
32
33
34
35
36
37
38
    
    void writeDofMesh(int rank, DegreeOfFreedom dof, FiniteElemSpace *feSpace)
    {
      using boost::lexical_cast;

      if (MPI::COMM_WORLD.Get_rank() == rank) {
	DOFVector<double> tmp(feSpace, "tmp");
	tmp.set(0.0);
	tmp[dof] = 1.0;
	VtkWriter::writeFile(tmp, "dofmesh" + lexical_cast<std::string>(rank) + ".vtu");
      }    
    }
39

40
41
42
43
44
45
46
47
    
    void writeMesh(FiniteElemSpace *feSpace, int rank, std::string filename)
    {
      using boost::lexical_cast;
      
      int myRank = MPI::COMM_WORLD.Get_rank();
      if (rank == -1 || myRank == rank) {
	DOFVector<double> tmp(feSpace, "tmp");
48
49
	VtkWriter::writeFile(tmp, filename + lexical_cast<std::string>(myRank) + ".vtu", 
			     false);
50
51
52
      }
    }
#endif
53

54
    
55
56
57
58
59
60
61
62
63
64
    void writeDofIndexMesh(FiniteElemSpace *feSpace)
    {
      DOFVector<double> tmp(feSpace, "tmp");
      DOFIterator<double> it(&tmp, USED_DOFS);
      for (it.reset(); !it.end(); ++it)
	*it = it.getDOFIndex();
      VtkWriter::writeFile(tmp, "dofindex.vtu");
    }


65
    void writeElementIndexMesh(Mesh *mesh, std::string filename)
66
67
68
    {
      std::map<int, double> vec;    
      TraverseStack stack;
69
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
70
71
72
73
74
75
76
      
      while (elInfo) {		  
	int index = elInfo->getElement()->getIndex();
	vec[index] = index;
	elInfo = stack.traverseNext(elInfo);
      }

77
      ElementFileWriter::writeFile(vec, mesh, filename);
78
79
80
    }


81
    void highlightElementIndexMesh(Mesh *mesh, int idx, std::string filename)
82
83
84
    {
      std::map<int, double> vec;    
      TraverseStack stack;
85
86
87
88
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      bool markChildren = false;
      int markLevel = -1;

89
90
      while (elInfo) {		  
	int index = elInfo->getElement()->getIndex();
91
92
93
94
95
96
97
98
99
100
101
	if (index == idx) {
	  markChildren = true;
	  markLevel = elInfo->getLevel();
	}

	if (markChildren && elInfo->getLevel() < markLevel)
	  markChildren = false;
	  
	if (elInfo->getElement()->isLeaf())
	  vec[index] = (markChildren ? 1.0 : 0.0);

102
103
104
	elInfo = stack.traverseNext(elInfo);
      }

105
      ElementFileWriter::writeFile(vec, mesh, filename);
106
107
108
    }


109
110
111
112
    void colorDofVectorByLocalElementDofs(DOFVector<double>& vec, Element *el)
    {
      // === Get local indices of the given element. ===
      
113
      const BasisFunction *basisFcts = vec.getFeSpace()->getBasisFcts();
114
115
      int nBasisFcts = basisFcts->getNumber();
      std::vector<DegreeOfFreedom> localDofs(nBasisFcts);
116
      basisFcts->getLocalIndices(el, vec.getFeSpace()->getAdmin(), localDofs);
117
118
119
120
121
122
123
      
      // === Set the values of the dof vector. ===
      
      vec.set(0.0);
      for (int i = 0; i < nBasisFcts; i++)
	vec[localDofs[i]] = static_cast<double>(i);
    }
124

125
126
127
128
    
    bool colorDofVectorByLocalElementDofs(DOFVector<double>& vec, Mesh *mesh, 
					  int elIndex)
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
129
      FUNCNAME("debug::colorDofVectorByLocalElementDofs()");
130
131
132
133
134
135
136
137
138
139
140
141
142
      
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
      while (elInfo) {
	if (elInfo->getElement()->getIndex() == elIndex) {
	  colorDofVectorByLocalElementDofs(vec, elInfo->getElement());
	  return true;
	}
	elInfo = stack.traverseNext(elInfo);
      }
      
      return false;
    }
143

144
145
146
147
148
149
150
151
    
    Element* getDofIndexElement(FiniteElemSpace *feSpace, DegreeOfFreedom dof)
    {
      const BasisFunction* basFcts = feSpace->getBasisFcts();
      int nBasFcts = basFcts->getNumber();
      std::vector<DegreeOfFreedom> dofVec(nBasFcts);
      
      TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
152
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, Mesh::CALL_LEAF_EL);
153
154
155
156
      while (elInfo) {
	basFcts->getLocalIndices(elInfo->getElement(), feSpace->getAdmin(), dofVec);
	for (int i = 0; i < nBasFcts; i++) 
	  if (dofVec[i] == dof)
157
	    return elInfo->getElement();	  
158
159
160
161
162
163
	
	elInfo = stack.traverseNext(elInfo);
      }
      
      return NULL;
    }
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
    
    Element* getLevel0ParentElement(Mesh *mesh, Element *el)
    {    
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	if (elInfo->getElement() == el)
	  return elInfo->getMacroElement()->getElement();      
	
	elInfo = stack.traverseNext(elInfo);
      }
      
      return NULL;
    }
179

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

    Element* getParentElement(Mesh *mesh, Element *el)
    {
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	if (elInfo->getElement()->getChild(0) == el || 
	    elInfo->getElement()->getChild(1) == el)
	  return elInfo->getElement();      
	
	elInfo = stack.traverseNext(elInfo);
      }
      
      return NULL;

    }


    Element* getParentElement(Mesh *mesh, int elIndex)
    {
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	if (elInfo->getElement()->isLeaf() == false) 
	  if (elInfo->getElement()->getChild(0)->getIndex() == elIndex || 
	      elInfo->getElement()->getChild(1)->getIndex() == elIndex)
	    return elInfo->getElement();
	
	elInfo = stack.traverseNext(elInfo);
      }
      
      return NULL;

    }


    void printElementInfo(Element *el)
    {
      FUNCNAME("printElementInfo()");
      
      if (el->isLeaf()) {
	MSG("el %d is leaf!\n", el->getIndex());
      } else {
	MSG("el child0 %d    child1 %d\n", 
	    el->getChild(0)->getIndex(),
	    el->getChild(1)->getIndex());
	printElementInfo(el->getChild(0));
	printElementInfo(el->getChild(1));
      }
    }

231
232
233
234
235
236
    
    void printInfoByDof(FiniteElemSpace *feSpace, DegreeOfFreedom dof)
    {
      Element *el = getDofIndexElement(feSpace, dof);
      Element *parEl = getLevel0ParentElement(feSpace->getMesh(), el);
      
237
      std::cout << "[DBG] DOF-INFO:  dof = " << dof
238
239
240
241
242
243
244
245
		<< "  elidx = " << el->getIndex()
		<< "  pelidx = " << parEl->getIndex() << std::endl;	 
      
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, 
					   Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	if (elInfo->getElement()->getIndex() == parEl->getIndex())
246
	  std::cout << "[DBG] EL INFO TO " << parEl->getIndex() << ": " 
247
		    << " type = " << elInfo->getType() << std::endl;
248
249
250
251
252
	
	elInfo = stack.traverseNext(elInfo);
      }	  
    }

253

Thomas Witkowski's avatar
Thomas Witkowski committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    void printMatValuesStatistics(Matrix<DOFMatrix*> *mat)
    {
      std::map<int, int> counter;
      int counter0 = 0;

      for (int i = 0; i < mat->getNumRows(); i++) {
	for (int j = 0; j < mat->getNumCols(); j++) {
	  DOFMatrix *dofMat = (*mat)[i][j];
	  if (dofMat) {

	    using mtl::tag::major; using mtl::tag::nz; using mtl::begin; using mtl::end;
	    namespace traits = mtl::traits;
	    typedef DOFMatrix::base_matrix_type base_matrix_type;
	    
	    traits::const_value<base_matrix_type>::type value(dofMat->getBaseMatrix());
	    typedef traits::range_generator<major, base_matrix_type>::type cursor_type;
	    typedef traits::range_generator<nz, cursor_type>::type icursor_type;
    
	    for (cursor_type cursor = begin<major>(dofMat->getBaseMatrix()), 
		   cend = end<major>(dofMat->getBaseMatrix()); cursor != cend; ++cursor)
	      for (icursor_type icursor = begin<nz>(cursor), 
		     icend = end<nz>(cursor); icursor != icend; ++icursor) {
		if (value(*icursor) == 0.0) {
		  counter0++;
		} else {
		  int a = static_cast<int>(floor(log10(fabs(value(*icursor)))));
		  counter[a]++;
		}
	      }
		
	  }
	}
      }

      std::cout << "value = 0: " << counter0 << std::endl;
      for (std::map<int, int>::iterator it = counter.begin(); it != counter.end(); ++it)
	std::cout << pow(10, it->first) << " <= values <= " 
		  << pow(10, it->first + 1) << ": " 
		  << it->second << std::endl;
    }

295

296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    void printAllDofCoords(FiniteElemSpace *feSpace)
    {
      FUNCNAME("printAllDofCoords()");

      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, 
					   Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
      while (elInfo) {
	Element *el = elInfo->getElement();
	for (int i = 0; i <= feSpace->getMesh()->getDim(); i++) {
	  MSG("Coords for DOF %d = %f %f\n", 
	      *(el->getDOF(i)), (elInfo->getCoord(i))[0], (elInfo->getCoord(i))[1]);
	}
	elInfo = stack.traverseNext(elInfo);
      }      
    }


    void getAllDofs(FiniteElemSpace *feSpace, std::set<const DegreeOfFreedom*>& dofs)
    {
      FUNCNAME("getAllDofs()");

      ElementDofIterator elDofIter(feSpace);
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, Mesh::CALL_LEAF_EL);
      while (elInfo) {	
	elDofIter.reset(elInfo->getElement());
	do {
	  dofs.insert(elDofIter.getDofPtr());
	} while (elDofIter.next());
	elInfo = stack.traverseNext(elInfo);
      }      
    }


331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
    void writeMatlabMatrix(DOFMatrix &mat, std::string filename)
    {
      using mtl::tag::major; using mtl::tag::nz; using mtl::begin; using mtl::end;
      namespace traits = mtl::traits;
      typedef DOFMatrix::base_matrix_type Matrix;

      traits::row<Matrix>::type row(mat.getBaseMatrix());
      traits::col<Matrix>::type col(mat.getBaseMatrix());
      traits::const_value<Matrix>::type value(mat.getBaseMatrix());

      typedef traits::range_generator<major, Matrix>::type cursor_type;
      typedef traits::range_generator<nz, cursor_type>::type icursor_type;

      std::ofstream out;
      out.open(filename.c_str());
    
      for (cursor_type cursor = begin<major>(mat.getBaseMatrix()), 
	     cend = end<major>(mat.getBaseMatrix()); cursor != cend; ++cursor)
	for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor); icursor != icend; ++icursor)
	  out << row(*icursor) + 1 << " " 
	      << col(*icursor) + 1 << " " 
	      << value(*icursor) << "\n";

Thomas Witkowski's avatar
Thomas Witkowski committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
      out.close();
    }


    void writeMatlabMatrix(Matrix<DOFMatrix*> &mat, std::string filename)
    {
      using mtl::tag::major; using mtl::tag::nz; using mtl::begin; using mtl::end;
      namespace traits = mtl::traits;
      typedef DOFMatrix::base_matrix_type Matrix;

      std::ofstream out;
      out.open(filename.c_str());

      for (int i = 0; i < mat.getNumRows(); i++) {
	for (int j = 0; j < mat.getNumCols(); j++) {
	  if (!mat[i][j])
	    continue;

	  traits::row<Matrix>::type row(mat[i][j]->getBaseMatrix());
	  traits::col<Matrix>::type col(mat[i][j]->getBaseMatrix());
	  traits::const_value<Matrix>::type value(mat[i][j]->getBaseMatrix());
	  
	  typedef traits::range_generator<major, Matrix>::type cursor_type;
	  typedef traits::range_generator<nz, cursor_type>::type icursor_type;	  

	  for (cursor_type cursor = begin<major>(mat[i][j]->getBaseMatrix()), 
		 cend = end<major>(mat[i][j]->getBaseMatrix()); cursor != cend; ++cursor)
	    for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor); icursor != icend; ++icursor)
	      out << i * num_rows(mat[i][j]->getBaseMatrix()) + row(*icursor) + 1 << " " 
		  << j * num_cols(mat[i][j]->getBaseMatrix()) + col(*icursor) + 1 << " " 
		  << value(*icursor) << "\n";
	}
      }
    
388
389
390
      out.close();
    }

391

392
393
394
395
396
397
398
    void writeMatlabVector(DOFVector<double> &vec, std::string filename)
    {
      std::ofstream out;    
      out.open(filename.c_str());

      DOFIterator<double> it(&vec, USED_DOFS);
      for (it.reset(); !it.end(); ++it)
399
	out << *it << "\n";
400
401
402
403

      out.close();      
    }

404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
    
    void writeMatlabVector(SystemVector &vec, std::string filename)
    {
      std::ofstream out;    
      out.open(filename.c_str());

      for (int i = 0; i < vec.getSize(); i++) {
	DOFIterator<double> it(vec.getDOFVector(i), USED_DOFS);
	for (it.reset(); !it.end(); ++it)
	  out << *it << "\n";
      }

      out.close();            
    }

419

Thomas Witkowski's avatar
Thomas Witkowski committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    void writeCoordsFile(const FiniteElemSpace* feSpace, std::string filename)
    {
      FUNCNAME("debug::writeCoordsFile()");

      DOFVector<WorldVector<double> > coords(feSpace, "tmp");
      feSpace->getMesh()->getDofIndexCoords(feSpace, coords);

      std::ofstream file;
      file.open(filename.c_str());
      file << coords.getUsedSize() << "\n";
      DOFIterator<WorldVector<double> > it(&coords, USED_DOFS);
      for (it.reset(); !it.end(); ++it) {
	file << it.getDOFIndex();
	for (int i = 0; i < feSpace->getMesh()->getDim(); i++)
	  file << " " << (*it)[i];
	file << "\n";
      }
      file.close();
    }


441
442
    void createSortedDofs(Mesh *mesh, ElementIdxToDofs &elMap)
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
443
      FUNCNAME("debug::dbgCreateElementMap()");
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
      
      int dim = mesh->getDim();
      elMap.clear();
      
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
      while (elInfo) {
	Element *el = elInfo->getElement();
	switch (dim) {
	case 2:
	  sortDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), elMap[el->getIndex()]);
	  break;
	case 3:
	  sortDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), el->getDOF(3), elMap[el->getIndex()]);
	  break;
	default:
	  ERROR_EXIT("What is this?\n");
	}
	elInfo = stack.traverseNext(elInfo);
      }
    }


    void testSortedDofs(Mesh *mesh, ElementIdxToDofs &elMap)
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
469
      FUNCNAME("debug::dbgTestElementMap()");
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
      
      int dim = mesh->getDim();
      int nVertex = Global::getGeo(VERTEX, dim);
      DofContainer vec(nVertex);
      
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
      while (elInfo) {
	Element *el = elInfo->getElement();
	
	switch (dim) {
	case 2:
	  sortDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), vec);
	  break;
	case 3:
	  sortDofs(el->getDOF(0), el->getDOF(1), el->getDOF(2), el->getDOF(3), vec);
	  break;
	default:
	  ERROR_EXIT("What is this?\n");
	}
	
	for (int i = 0; i < nVertex; i++) {
	  if (elMap[el->getIndex()][i] != vec[i]) {
	    MSG("[DBG] Wrong new dof numeration in element = %d\n!", el->getIndex());
	    std::cout << "[DBG]: Old numeration was: ";
	    for (int j = 0; j < nVertex; j++)
	      std::cout << elMap[el->getIndex()][j] << " = " 
			<< *(elMap[el->getIndex()][j]) << "  ";
	    std::cout << std::endl;
	    std::cout << "[DBG]: New numeration is:  ";
	    for (int j = 0; j < nVertex; j++)
	      std::cout << vec[j] << " = "  << *(vec[j]) << "  ";
	    std::cout << std::endl;
	    ERROR_EXIT("WRONG NEW DOF NUMERATION!\n");
	  }
	}
	elInfo = stack.traverseNext(elInfo);
      }
    }


    void sortDofs(const DegreeOfFreedom* dof0,
		  const DegreeOfFreedom* dof1,
		  const DegreeOfFreedom* dof2,
		  DofContainer &vec)
    {
      DofPtrSortFct dofPtrSort;
      vec.resize(3);
      vec[0] = dof0; 
      vec[1] = dof1; 
      vec[2] = dof2;
      sort(vec.begin(), vec.end(), dofPtrSort);
    }

    void sortDofs(const DegreeOfFreedom* dof0,
		  const DegreeOfFreedom* dof1,
		  const DegreeOfFreedom* dof2,
		  const DegreeOfFreedom* dof3,
		  DofContainer &vec)
    {
      DofPtrSortFct dofPtrSort;
      vec.resize(4);
      vec[0] = dof0; 
      vec[1] = dof1; 
      vec[2] = dof2;
      vec[3] = dof3;
      sort(vec.begin(), vec.end(), dofPtrSort);
    }


540
541
542
  } // namespace debug
  
} // namespace AMDiS