Debug.cc 24.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
23
#include <iostream>
#include <fstream>
24
25
26
27
#include <boost/lexical_cast.hpp>
#include "Debug.h"
#include "DOFVector.h"
#include "MacroElement.h"
28
#include "ElementDofIterator.h"
29
30
#include "io/VtkWriter.h"
#include "io/ElementFileWriter.h"
31
32
33
34
35
36

namespace AMDiS {

  namespace debug {

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
37
38
    void writeLocalElementDofs(int rank, int elIdx, 
			       const FiniteElemSpace *feSpace)
39
40
41
42
43
44
    {
      using boost::lexical_cast;
      
      if (MPI::COMM_WORLD.Get_rank() == rank) {
	DOFVector<double> tmp(feSpace, "tmp");
	colorDofVectorByLocalElementDofs(tmp, feSpace->getMesh(), elIdx);
45
	io::VtkWriter::writeFile(tmp, "tmp" + lexical_cast<std::string>(elIdx) + ".vtu");
46
47
      }
    }
48

49
    
50
51
    void writeDofMesh(int rank, DegreeOfFreedom dof, 
		      const FiniteElemSpace *feSpace)
52
53
54
55
56
57
58
    {
      using boost::lexical_cast;

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

63
    
64
    void writeMesh(const FiniteElemSpace *feSpace, int rank, std::string filename)
65
66
67
68
69
70
    {
      using boost::lexical_cast;
      
      int myRank = MPI::COMM_WORLD.Get_rank();
      if (rank == -1 || myRank == rank) {
	DOFVector<double> tmp(feSpace, "tmp");
71
	io::VtkWriter::writeFile(tmp, filename + lexical_cast<std::string>(myRank) + ".vtu", 
Praetorius, Simon's avatar
Praetorius, Simon committed
72
				 false,
73
			     false);
74
75
76
      }
    }
#endif
77

78
    
79
    void writeDofIndexMesh(const FiniteElemSpace *feSpace,
80
			   std::string filename)
81
82
83
84
85
    {
      DOFVector<double> tmp(feSpace, "tmp");
      DOFIterator<double> it(&tmp, USED_DOFS);
      for (it.reset(); !it.end(); ++it)
	*it = it.getDOFIndex();
86
      io::VtkWriter::writeFile(tmp, filename);
87
88
89
    }


90
    void colorEdgeInMesh(const FiniteElemSpace *feSpace,
91
92
93
94
95
96
97
98
			 Element *el, 
			 int localEdgeNo, 
			 std::string filename)
    {
      DOFVector<double> tmp(feSpace, "tmp");
      tmp.set(0.0);
      tmp[el->getEdge(localEdgeNo).first] = 1.0;
      tmp[el->getEdge(localEdgeNo).second] = 1.0;
99
      io::VtkWriter::writeFile(tmp, filename);
100
101
102
    }


103
    void writeElementIndexMesh(Mesh *mesh, std::string filename, int level)
104
105
106
    {
      std::map<int, double> vec;    
      TraverseStack stack;
107
108
109
110
      ElInfo *elInfo = 
	level == -1 ? 
	stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL) :
	stack.traverseFirst(mesh, level, Mesh::CALL_EL_LEVEL);
111
112
113
114
115
116
117
      
      while (elInfo) {		  
	int index = elInfo->getElement()->getIndex();
	vec[index] = index;
	elInfo = stack.traverseNext(elInfo);
      }

118
      io::ElementFileWriter::writeFile(vec, mesh, filename, ".vtu", level);
119
120
121
    }


122
123
124
125
126
127
128
129
130
131
132
133
134
135
    void writeMacroElementIndexMesh(Mesh *mesh,
				    std::string filename)
    {
      std::map<int, double> vec;    
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);

      while (elInfo) {		  
	int index = elInfo->getElement()->getIndex();
	int macroIndex = elInfo->getMacroElement()->getIndex();
	vec[index] = macroIndex;
	elInfo = stack.traverseNext(elInfo);
      }

136
      io::ElementFileWriter::writeFile(vec, mesh, filename, ".vtu");
137
138
139
    }


140
    void highlightElementIndexMesh(Mesh *mesh, int idx, std::string filename)
141
142
143
    {
      std::map<int, double> vec;    
      TraverseStack stack;
144
145
146
147
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      bool markChildren = false;
      int markLevel = -1;

148
      while (elInfo) {		  
Thomas Witkowski's avatar
Thomas Witkowski committed
149
150
151
	if (markChildren && elInfo->getLevel() <= markLevel)
	  markChildren = false;

152
	int index = elInfo->getElement()->getIndex();
153
154
155
156
	if (index == idx) {
	  markChildren = true;
	  markLevel = elInfo->getLevel();
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
157
  
158
159
160
	if (elInfo->getElement()->isLeaf())
	  vec[index] = (markChildren ? 1.0 : 0.0);

161
162
163
	elInfo = stack.traverseNext(elInfo);
      }

164
      io::ElementFileWriter::writeFile(vec, mesh, filename);
165
166
167
    }


168
169
170
171
172
173
174
175
176
177
178
179
    void colorMeshByMacroIndex(Mesh *mesh, std::string filename)
    {
      std::map<int, double> vec;    
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_LEAF_EL);
      
      while (elInfo) {		  
	int index = elInfo->getElement()->getIndex();
	vec[index] = elInfo->getMacroElement()->getIndex();
	elInfo = stack.traverseNext(elInfo);
      }

180
      io::ElementFileWriter::writeFile(vec, mesh, filename);
181
182
183
    }


184
185
186
187
    void colorDofVectorByLocalElementDofs(DOFVector<double>& vec, Element *el)
    {
      // === Get local indices of the given element. ===
      
188
      const BasisFunction *basisFcts = vec.getFeSpace()->getBasisFcts();
189
190
      int nBasisFcts = basisFcts->getNumber();
      std::vector<DegreeOfFreedom> localDofs(nBasisFcts);
191
      basisFcts->getLocalIndices(el, vec.getFeSpace()->getAdmin(), localDofs);
192
193
194
195
196
197
198
      
      // === 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);
    }
199

200
201
202
    
    bool colorDofVectorByLocalElementDofs(DOFVector<double>& vec, Mesh *mesh, 
					  int elIndex)
203
    {      
204
205
206
207
208
209
210
211
212
213
214
215
      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;
    }
216

217
    
218
219
    Element* getDofIndexElement(const FiniteElemSpace *feSpace, 
				DegreeOfFreedom dof)
220
221
222
223
224
225
    {
      const BasisFunction* basFcts = feSpace->getBasisFcts();
      int nBasFcts = basFcts->getNumber();
      std::vector<DegreeOfFreedom> dofVec(nBasFcts);
      
      TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
226
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, Mesh::CALL_LEAF_EL);
227
228
229
230
      while (elInfo) {
	basFcts->getLocalIndices(elInfo->getElement(), feSpace->getAdmin(), dofVec);
	for (int i = 0; i < nBasFcts; i++) 
	  if (dofVec[i] == dof)
231
	    return elInfo->getElement();	  
232
233
234
235
	
	elInfo = stack.traverseNext(elInfo);
      }
      
236
      return nullptr;
237
    }
238

239
240
241
242
243
244
245
246
247
248
249
250
    
    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);
      }
      
251
      return nullptr;
252
    }
253

254

255
256
257
258
259
260
261
262
263
264
265
    Element* getLevel0ParentElement(Mesh *mesh, int elIndex)
    {    
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	if (elInfo->getElement()->getIndex() == elIndex)
	  return elInfo->getMacroElement()->getElement();      
	
	elInfo = stack.traverseNext(elInfo);
      }
      
266
      return nullptr;
267
268
269
    }


270
271
272
273
274
275
276
277
278
279
280
281
    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);
      }
      
282
      return nullptr;
283
284
285
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
286
287
288
289
290
291
292
293
294
295
296
    Element* getElement(Mesh *mesh, int elIndex)
    {
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	if (elInfo->getElement()->getIndex() == elIndex)
	  return elInfo->getElement();      
	
	elInfo = stack.traverseNext(elInfo);
      }
      
297
      return nullptr;      
Thomas Witkowski's avatar
Thomas Witkowski committed
298
299
300
    }


301
302
303
304
305
306
307
308
309
310
311
312
313
    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);
      }
      
314
      return nullptr;
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

    }


    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));
      }
    }

334
    
335
336
337
338
339
340
341
    void printElementCoords(const FiniteElemSpace *feSpace, Element *el)
    {
      FUNCNAME("debug:printElementCoords()");
      
      Mesh *mesh = feSpace->getMesh();

      for (int i = 0; i <= mesh->getDim(); i++) {
342
	DegreeOfFreedom dof = el->getDof(i, 0);
343
344
345
346
347
348
349
350
	WorldVector<double> coords;
	mesh->getDofIndexCoords(dof, feSpace, coords);
	MSG("%d-th DOF of element %d: %f %f %f\n", 
	    i, el->getIndex(), coords[0], coords[1], coords[2]);
      }
    }


351
    void printInfoByDof(const FiniteElemSpace *feSpace, DegreeOfFreedom dof)
352
    {
353
354
      FUNCNAME("debug::printInfoByDof()");

Thomas Witkowski's avatar
Thomas Witkowski committed
355
356
357
      WorldVector<double> coords;
      feSpace->getMesh()->getDofIndexCoords(dof, feSpace, coords);

358
359
360
      Element *el = getDofIndexElement(feSpace, dof);
      Element *parEl = getLevel0ParentElement(feSpace->getMesh(), el);
      
Thomas Witkowski's avatar
Thomas Witkowski committed
361
362
363
364
365
      if (coords.getSize() == 2)
	MSG("[DBG] DOF-INFO:  dof = %d  coords: %e %e\n", dof, coords[0], coords[1]);
      else 
	MSG("[DBG] DOF-INFO:  dof = %d  coords: %e %e %e\n", dof, coords[0], coords[1], coords[2]);

366
367
      MSG("[DBG] DOF-INFO:  dof = %d  elidx = %d  pelidx = %d\n", 
	  dof, el->getIndex(), parEl->getIndex());
368
369
370
371
372
      
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, 
					   Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
373
374
	if (elInfo->getElement()->getIndex() == parEl->getIndex()) {
	  MSG("[DBG] EL INFO TO %d: type = %d\n", parEl->getIndex(), elInfo->getType());
Thomas Witkowski's avatar
Thomas Witkowski committed
375
	  break;
376
377
	}

378
379
380
381
	elInfo = stack.traverseNext(elInfo);
      }	  
    }

382

Thomas Witkowski's avatar
Thomas Witkowski committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
    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)
419
420
	std::cout << pow(10.0, it->first) << " <= values <= " 
		  << pow(10.0, it->first + 1.0) << ": " 
421
		  << it->second << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
422
423
    }

424

425
    void printAllDofCoords(const FiniteElemSpace *feSpace)
426
427
428
429
430
431
432
433
434
435
    {
      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", 
436
	      *(el->getDof(i)), (elInfo->getCoord(i))[0], (elInfo->getCoord(i))[1]);
437
438
439
440
441
442
	}
	elInfo = stack.traverseNext(elInfo);
      }      
    }


443
444
    void getAllDofs(const FiniteElemSpace *feSpace, 
		    std::set<const DegreeOfFreedom*>& dofs)
445
446
447
448
449
450
451
452
453
454
455
456
457
458
    {
      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);
      }      
    }


459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
    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());
474
      out.precision(20);
475
476
477
478
479
480
481
482
    
      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
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
      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";
	}
      }
    
517
518
519
      out.close();
    }

520

521
522
523
524
525
526
527
    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)
528
	out << *it << "\n";
529
530
531
532

      out.close();      
    }

533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
    
    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();            
    }

548

Thomas Witkowski's avatar
Thomas Witkowski committed
549
550
551
    void writeCoordsFile(const FiniteElemSpace* feSpace, std::string filename)
    {
      DOFVector<WorldVector<double> > coords(feSpace, "tmp");
Thomas Witkowski's avatar
Thomas Witkowski committed
552
      feSpace->getMesh()->getDofIndexCoords(coords);
Thomas Witkowski's avatar
Thomas Witkowski committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

      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();
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    void printElementHierarchie(Mesh *mesh, int elIndex)
    {
      FUNCNAME("debug::printElementHierarchie()");

      bool printInfo = false;
      int elLevel = -1;

      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	if (printInfo && elInfo->getLevel() <= elLevel)
	  return;

	if (elInfo->getElement()->getIndex() == elIndex) {
	  printInfo = true;
	  elLevel = elInfo->getLevel();
	  MSG(" Hierarchie for elIdx = %d\n", elIndex);
	} else {
	  if (printInfo) {
	    std::stringstream oss;
	    for (int i = 0; i < (elInfo->getLevel() - elLevel); i++)
	      oss << "   ";
	    oss << "|--" << elInfo->getElement()->getIndex();
591
592
593
594
595
	    if (oss.str().length() >= 255) {
	      WARNING("String is to long! Element index is %d.\n", elInfo->getElement()->getIndex());
	    } else {
	      MSG("%s\n", oss.str().c_str());
	    }
Thomas Witkowski's avatar
Thomas Witkowski committed
596
597
598
599
600
	  }
	}

	elInfo = stack.traverseNext(elInfo);
      }
601
602
603
604

      if (!printInfo) {
	MSG("Could not find element with index %d\n", elIndex);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
605
606
607
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
608
    void printElementRefinementSequence(Mesh *mesh, Element *el)
609
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
610
      FUNCNAME("printElementRefinementSequence()");
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633

      int elIndex = el->getIndex();
      std::vector<int> refSeq;
      Element *parent = getParentElement(mesh, el);

      while (parent) {
	if (parent->getChild(0) == el)
	  refSeq.push_back(0);
	else
	  refSeq.push_back(1);

	el = parent;
	parent = getParentElement(mesh, el);
      }

      std::stringstream oss;
      for (int i = refSeq.size() - 1; i >= 0; i--)
	oss << refSeq[i];
      
      MSG("Ref-Seq for element %d: %s\n", elIndex, oss.str().c_str());
    }


634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
    int getLocalNeighbourIndex(Mesh *mesh, int elIndex, int neighIndex)
    {
      FUNCNAME("debug::getLocalNeighbourIndex()");

      TraverseStack stack;
      ElInfo *elInfo = 
	stack.traverseFirst(mesh, -1, Mesh::CALL_EVERY_EL_PREORDER | Mesh::FILL_NEIGH);
      while (elInfo) {
	if (elInfo->getElement()->getIndex() == elIndex) {
	  int returnValue = -1;
	  for (int i = 0; i <= mesh->getDim(); i++) {
	    if (elInfo->getNeighbour(i)) {
	      MSG("NEIGH %d of El-Idx %d: %d\n", i, elIndex, elInfo->getNeighbour(i)->getIndex());
	    } else {
	      MSG("NEIGH %d of El-Idx %d: %d\n", i, elIndex, -1);
	    }
	    
	    if (elInfo->getNeighbour(i) && 
		elInfo->getNeighbour(i)->getIndex() == neighIndex)
	      returnValue = i;
	  }

	  return returnValue;	  
	}
	elInfo = stack.traverseNext(elInfo);
      }      

      return -2;
    }


665
666
667
    void importDofVectorByCoords(DOFVector<double>* vec, std::string filename)
    { 
      DOFVector<WorldVector<double> > coords(vec->getFeSpace(), "dofCoords");
Thomas Witkowski's avatar
Thomas Witkowski committed
668
      vec->getFeSpace()->getMesh()->getDofIndexCoords(coords);
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
      int dim = vec->getFeSpace()->getMesh()->getDim();

      std::ifstream file;
      file.open(filename.c_str());
      
      int nEntries;
      file >> nEntries;

      for (int i = 0; i < nEntries; i++) {
	WorldVector<double> dofCoords;
	for (int j = 0; j < dim; j++)
	  file >> dofCoords[j];

	double value;
	file >> value;

      
	DOFIterator<WorldVector<double> > it(&coords, USED_DOFS);
	for (it.reset(); !it.end(); ++it) {
	  bool found = true;
	  for (int j = 0; j < dim; j++) {
	    if (fabs((*it)[j] - dofCoords[j]) > 1e-8) {
	      found = false;
	      break;
	    }
	  }

	  if (found) {
	    (*vec)[it.getDOFIndex()] = value;
	    break;
	  }
	}	
      }

      file.close();
    }


707
708
709
710
    void exportDofVectorByCoords(const DOFVector<double>* vec, 
				 std::string filename)
    {
      DOFVector<WorldVector<double> > coords(vec->getFeSpace(), "dofCoords");
Thomas Witkowski's avatar
Thomas Witkowski committed
711
      vec->getFeSpace()->getMesh()->getDofIndexCoords(coords);
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
      int dim = vec->getFeSpace()->getMesh()->getDim();

      std::ofstream file;
      file.open(filename.c_str());
      file << vec->getUsedSize() << "\n";

      DOFIterator<WorldVector<double> > it(&coords, USED_DOFS);
      for (it.reset(); !it.end(); ++it) {
        for (int i = 0; i < dim; i++)
	  file << (*it)[i] << " ";
        file << (*vec)[it.getDOFIndex()] << "\n";
      }
    
      file.close();
    }


729
730
    void createSortedDofs(Mesh *mesh, ElementIdxToDofs &elMap)
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
731
      FUNCNAME("debug::dbgCreateElementMap()");
732
733
734
735
736
737
738
739
740
741
      
      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:
742
	  sortDofs(el->getDof(0), el->getDof(1), el->getDof(2), elMap[el->getIndex()]);
743
744
	  break;
	case 3:
745
	  sortDofs(el->getDof(0), el->getDof(1), el->getDof(2), el->getDof(3), elMap[el->getIndex()]);
746
747
748
749
750
751
752
753
754
755
756
	  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
757
      FUNCNAME("debug::dbgTestElementMap()");
758
759
760
761
762
763
764
765
766
767
768
769
      
      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:
770
	  sortDofs(el->getDof(0), el->getDof(1), el->getDof(2), vec);
771
772
	  break;
	case 3:
773
	  sortDofs(el->getDof(0), el->getDof(1), el->getDof(2), el->getDof(3), vec);
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
	  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);
    }


828
    void testDofsByCoords(const FiniteElemSpace *feSpace,
829
830
831
832
833
834
835
836
837
			  DofContainer &dofs0, DofContainer &dofs1)
    {
      FUNCNAME("debug::testDofsByCoords()");

      TEST_EXIT(dofs0.size() == dofs1.size())
	("The dof containers have different sizes %d %d!\n",
	 dofs0.size(), dofs1.size());

      DOFVector<WorldVector<double> > coords(feSpace, "dofCorrds");
Thomas Witkowski's avatar
Thomas Witkowski committed
838
      feSpace->getMesh()->getDofIndexCoords(coords);
839
840
841
842
843
844
845
846
847
848
849
850
851

      for (unsigned int i = 0; i < dofs0.size(); i++) {
	WorldVector<double> tmp = coords[*(dofs0[i])];
	tmp -= coords[*(dofs1[i])];

	TEST_EXIT(norm(tmp) < 1e-13)
	  ("DOFs %d and %d (i = %d) have different coords!\n", 
	   *(dofs0[i]), *(dofs1[i]), i);
      }
      
    }


852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
    void testDofsByCoords(DOFVector<WorldVector<double> > &coords,
			  DofContainer &dofs0, 
			  DofContainer &dofs1)
    {
      FUNCNAME("debug::testDofsByCoords()");

      TEST_EXIT(dofs0.size() == dofs1.size())
	("The dof containers have different sizes %d %d!\n",
	 dofs0.size(), dofs1.size());

      for (unsigned int i = 0; i < dofs0.size(); i++) {
	WorldVector<double> tmp = coords[*(dofs0[i])];
	tmp -= coords[*(dofs1[i])];

	TEST_EXIT(norm(tmp) < 1e-13)
	  ("DOFs %d and %d (i = %d) have different coords!\n", 
	   *(dofs0[i]), *(dofs1[i]), i);
      }
      
    }

873
874
875
  } // namespace debug
  
} // namespace AMDiS