Debug.cc 7.32 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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

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");
      }
    }
    
    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");
      }    
    }
    
    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");
	VtkWriter::writeFile(tmp, filename + lexical_cast<std::string>(myRank) + ".vtu");
      }
    }
#endif
    
    void colorDofVectorByLocalElementDofs(DOFVector<double>& vec, Element *el)
    {
      // === Get local indices of the given element. ===
      
      const BasisFunction *basisFcts = vec.getFESpace()->getBasisFcts();
      int nBasisFcts = basisFcts->getNumber();
      std::vector<DegreeOfFreedom> localDofs(nBasisFcts);
      basisFcts->getLocalIndices(el, vec.getFESpace()->getAdmin(), localDofs);
      
      // === 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);
    }
    
    bool colorDofVectorByLocalElementDofs(DOFVector<double>& vec, Mesh *mesh, 
					  int elIndex)
    {
      FUNCNAME("colorDofVectorByLocalElementDofs()");
      
      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;
    }
    
    Element* getDofIndexElement(FiniteElemSpace *feSpace, DegreeOfFreedom dof)
    {
      const BasisFunction* basFcts = feSpace->getBasisFcts();
      int nBasFcts = basFcts->getNumber();
      std::vector<DegreeOfFreedom> dofVec(nBasFcts);
      
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, 
					   Mesh::CALL_EVERY_EL_PREORDER);
      while (elInfo) {
	basFcts->getLocalIndices(elInfo->getElement(), feSpace->getAdmin(), dofVec);
	for (int i = 0; i < nBasFcts; i++) 
	  if (dofVec[i] == dof)
	    return elInfo->getElement();
	
	elInfo = stack.traverseNext(elInfo);
      }
      
      return NULL;
    }
    
    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;
    }
    
    void printInfoByDof(FiniteElemSpace *feSpace, DegreeOfFreedom dof)
    {
      Element *el = getDofIndexElement(feSpace, dof);
      Element *parEl = getLevel0ParentElement(feSpace->getMesh(), el);
      
      std::cout << "DOF-INFO:  dof = " << dof
		<< "  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())
	  std::cout << "EL INFO TO " << parEl->getIndex() << ": " 
		    << elInfo->getType() << std::endl;
	
	elInfo = stack.traverseNext(elInfo);
      }	  
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    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;
    }

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    void writeElementIndexMesh(FiniteElemSpace *feSpace, std::string filename)
    {
      std::map<int, double> vec;    
      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(feSpace->getMesh(), -1, Mesh::CALL_LEAF_EL);
      
      while (elInfo) {		  
	int index = elInfo->getElement()->getIndex();
	vec[index] = index;
	elInfo = stack.traverseNext(elInfo);
      }

      ElementFileWriter::writeFile(vec, feSpace, filename);
    }

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
231
232
233
234
    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";

      out.close();
    }

    void writeMatlabVector(DOFVector<double> &vec, std::string filename)
    {
      std::ofstream out;    
      out.open(filename.c_str());

      DOFIterator<double> it(&vec, USED_DOFS);
      int counter = 1;
      for (it.reset(); !it.end(); ++it)
	out << counter++ << " " << *it << "\n";

      out.close();      
    }

235
236
237
  } // namespace debug
  
} // namespace AMDiS