DOFMatrix.cc 11.4 KB
Newer Older
1
2
#include <algorithm>
#include <png.h>
3
#include <boost/numeric/mtl/mtl.hpp>
4
#include "DOFMatrix.h"
5
6
7
8
9
10
11
12
13
14
15
16
#include "QPsiPhi.h"
#include "BasisFunction.h"
#include "Boundary.h"
#include "DOFAdmin.h"
#include "ElInfo.h"
#include "FiniteElemSpace.h"
#include "Mesh.h"
#include "DOFVector.h"
#include "Operator.h"
#include "BoundaryCondition.h"
#include "BoundaryManager.h"
#include "Assembler.h"
17

18
19
namespace AMDiS {

20
21
  using namespace mtl;

22
23
24
  DOFMatrix *DOFMatrix::traversePtr = NULL;

  DOFMatrix::DOFMatrix()
25
26
    : rowFESpace(NULL),
      colFESpace(NULL),
27
28
29
      elementMatrix(3, 3),
      nRow(0),
      nCol(0),
30
31
      inserter(NULL)
  {}
32
33
34

  DOFMatrix::DOFMatrix(const FiniteElemSpace* rowFESpace_,
		       const FiniteElemSpace* colFESpace_,
35
		       std::string name_)
36
37
38
    : rowFESpace(rowFESpace_),
      colFESpace(colFESpace_),
      name(name_), 
39
40
      coupleMatrix(false),
      inserter(NULL)
41
42
43
  {
    TEST_EXIT(rowFESpace)("no rowFESpace\n");
  
Thomas Witkowski's avatar
Thomas Witkowski committed
44
    if (!colFESpace)
45
46
47
      colFESpace = rowFESpace;

    if (rowFESpace && rowFESpace->getAdmin())
Thomas Witkowski's avatar
Thomas Witkowski committed
48
      (const_cast<DOFAdmin*>(rowFESpace->getAdmin()))->addDOFIndexed(this);
49

Thomas Witkowski's avatar
Thomas Witkowski committed
50
    boundaryManager = new BoundaryManager(rowFESpace_);
51
52
53
54
55
56

    nRow = rowFESpace->getBasisFcts()->getNumber();
    nCol = colFESpace->getBasisFcts()->getNumber();
    elementMatrix.change_dim(nRow, nCol);
    rowIndices.resize(nRow);
    colIndices.resize(nCol);
Thomas Witkowski's avatar
Thomas Witkowski committed
57
58

    applyDBCs.clear();
59
60
61
  }

  DOFMatrix::DOFMatrix(const DOFMatrix& rhs)
Thomas Witkowski's avatar
Thomas Witkowski committed
62
    : name(rhs.name + "copy")
63
  {
64
    *this = rhs;
65
66
    if (rowFESpace && rowFESpace->getAdmin())
      (const_cast<DOFAdmin*>( rowFESpace->getAdmin()))->addDOFIndexed(this);
67
68
69

    TEST_EXIT(rhs.inserter == 0)("Cannot copy during insertion");
    inserter= 0;
70
71
72
73
  }

  DOFMatrix::~DOFMatrix()
  {
74
    FUNCNAME("DOFMatrix::~DOFMatrix()");
75
    if (rowFESpace && rowFESpace->getAdmin())
76
      (const_cast<DOFAdmin*>(rowFESpace->getAdmin()))->removeDOFIndexed(this);
Thomas Witkowski's avatar
Thomas Witkowski committed
77

78
    if (boundaryManager) delete boundaryManager;
79
    if (inserter) delete inserter;
80
81
82
83
84
85
  }

  void DOFMatrix::print() const
  {
    FUNCNAME("DOFMatrix::print()");

86
87
88
    using mtl::tag::major; using mtl::tag::nz; using mtl::begin; using mtl::end;
    namespace traits= mtl::traits;
    typedef base_matrix_type Matrix;
89

90
91
92
    traits::row<Matrix>::type                                 row(matrix);
    traits::col<Matrix>::type                                 col(matrix);
    traits::const_value<Matrix>::type                         value(matrix);
93

94
95
    typedef traits::range_generator<major, Matrix>::type      cursor_type;
    typedef traits::range_generator<nz, cursor_type>::type    icursor_type;
96
97

    std::cout.precision(10);
98
99
    for (cursor_type cursor = begin<major>(matrix), cend = end<major>(matrix); cursor != cend; ++cursor) {
      for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor); icursor != icend; ++icursor)
100
	if (value(*icursor) != 0.0)
Thomas Witkowski's avatar
nix.    
Thomas Witkowski committed
101
102
	    std::cout << "(" << row(*icursor) << "," << col(*icursor) << "," << value(*icursor) << ") ";
      
103
104
      std::cout << "\n";
    }
105
106
107
108
109
110
  }

  bool DOFMatrix::symmetric()
  {
    FUNCNAME("DOFMatrix::symmetric()");

111
    double tol = 1e-5;
112

113
114
115
    using mtl::tag::major; using mtl::tag::nz; using mtl::begin; using mtl::end;
    namespace traits= mtl::traits;
    typedef base_matrix_type   Matrix;
116

117
118
119
    traits::row<Matrix>::type                                 row(matrix);
    traits::col<Matrix>::type                                 col(matrix);
    traits::const_value<Matrix>::type                         value(matrix);
120

121
122
123
124
125
126
127
    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>(matrix), cend = end<major>(matrix); cursor != cend; ++cursor)
      for (icursor_type icursor = begin<nz>(cursor), icend = end<nz>(cursor); icursor != icend; ++icursor)
	// Compare each non-zero entry with its transposed
	if (abs(value(*icursor) - matrix[col(*icursor)][row(*icursor)]) > tol)
128
129
130
131
132
133
134
135
	  return false;
    return true;
  }

  void DOFMatrix::test()
  {
    FUNCNAME("DOFMatrix::test()");

136
    int non_symmetric = !symmetric();
137
138
139
140
141
142
143
144
145
146
147
148
149
150

    if (non_symmetric) {
      MSG("matrix `%s' not symmetric.\n", name.data());
    } else {
      MSG("matrix `%s' is symmetric.\n", name.data());
    }
  }

  DOFMatrix& DOFMatrix::operator=(const DOFMatrix& rhs)
  {
    rowFESpace = rhs.rowFESpace;
    colFESpace = rhs.colFESpace;
    operators = rhs.operators;
    operatorFactor = rhs.operatorFactor;
151
    coupleMatrix = rhs.coupleMatrix;
Thomas Witkowski's avatar
Thomas Witkowski committed
152
153
154
155
156

    /// The matrix values may only be copyed, if there is no active insertion.
    if (rhs.inserter == 0 && inserter == 0)
      matrix = rhs.matrix;

157
    if (rhs.boundaryManager) {
Thomas Witkowski's avatar
Thomas Witkowski committed
158
      boundaryManager = new BoundaryManager(*rhs.boundaryManager);
159
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
160
161
      boundaryManager = NULL;
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
162

163
164
165
    nRow = rhs.nRow;
    nCol = rhs.nCol;
    elementMatrix.change_dim(nRow, nCol);
166
167
168
169
170

    return *this;
  }

  void DOFMatrix::addElementMatrix(double sign, 
171
				   const ElementMatrix& elMat, 
172
				   const BoundaryType *bound,
173
174
				   ElInfo* rowElInfo,
				   ElInfo* colElInfo,
175
176
				   bool add)
  {
177
    FUNCNAME("DOFMatrix::addElementMatrix()");
178

179
    TEST_EXIT_DBG(inserter)("DOFMatrix is not in insertion mode");
180
181
    inserter_type &ins= *inserter;

182
    // === Get indices mapping from local to global matrix indices. ===
183

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
    rowFESpace->getBasisFcts()->getLocalIndicesVec(rowElInfo->getElement(),
						   rowFESpace->getAdmin(),
						   &rowIndices);
    if (rowFESpace == colFESpace) {
      colIndices = rowIndices;
    } else {
      if (colElInfo) {
	colFESpace->getBasisFcts()->getLocalIndicesVec(colElInfo->getElement(),
						       colFESpace->getAdmin(),
						       &colIndices);
      } else {
	// If there is no colElInfo pointer, use rowElInfo the get the indices.
	colFESpace->getBasisFcts()->getLocalIndicesVec(rowElInfo->getElement(),
						       colFESpace->getAdmin(),
						       &colIndices);
      }
    }
201

202
203
204
    // === Add element matrix to the global matrix using the indices mapping. ===

    for (int i = 0; i < nRow; i++)  {   // for all rows of element matrix
205
      DegreeOfFreedom row = rowIndices[i];
206

207
208
209
210
      BoundaryCondition *condition = 
	bound ? boundaryManager->getBoundaryCondition(bound[i]) : NULL;

      if (condition && condition->isDirichlet()) {
211
212
213
214
215
	if (condition->applyBoundaryCondition()) {
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
	  if (isRankDOF[row]) 
	    applyDBCs.insert(static_cast<int>(row));
#else
216
	  applyDBCs.insert(static_cast<int>(row));
217
218
#endif
	}
219
220
      } else {

221
	for (int j = 0; j < nCol; j++) {  // for all columns
222
223
	  DegreeOfFreedom col = colIndices[j];
	  double entry = elMat[i][j];
224

225
226
227
228
229
230
231
232
233
234
235
	  /*
	  if (MPI::COMM_WORLD.Get_rank() == 0 && (row >= 156 || col >= 156))
	    std::cout << "PROB 0: " << row << " " << col << std::endl;
	  if (MPI::COMM_WORLD.Get_rank() == 1 && (row >= 151 || col >= 151))
	    std::cout << "PROB 1: " << row << " " << col << std::endl;
	  if (MPI::COMM_WORLD.Get_rank() == 2 && (row >= 146 || col >= 146))
	    std::cout << "PROB 2: " << row << " " << col << std::endl;
	  if (MPI::COMM_WORLD.Get_rank() == 3 && (row >= 153 || col >= 153))
	    std::cout << "PROB 3: " << row << " " << col << std::endl;
	  */

236
	  if (add)
237
	    ins[row][col] += sign * entry;
238
	  else
239
	    ins[row][col] = sign * entry;	
240
	}   
241
      }
242
243
244
    }
  }

245
  double DOFMatrix::logAcc(DegreeOfFreedom a, DegreeOfFreedom b) const
246
  {
247
    return matrix[a][b];
248
249
  }

250
  void DOFMatrix::freeDOFContent(int index)
251
  {}
252

253
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, const BoundaryType *bound)
254
  {
255
    FUNCNAME("DOFMatrix::assemble()");
256

257
    set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
258

259
260
    std::vector<Operator*>::iterator it = operators.begin();
    std::vector<double*>::iterator factorIt = operatorFactor.begin();
261
262
263
    for (; it != operators.end(); ++it, ++factorIt)
      if ((*it)->getNeedDualTraverse() == false)
	(*it)->getElementMatrix(elInfo,	elementMatrix, *factorIt ? **factorIt : 1.0);
264

265
    addElementMatrix(factor, elementMatrix, bound, elInfo, NULL);   
266
267
  }

268
269
270
271
272
273
274
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, const BoundaryType *bound,
			   Operator *op)
  {
      FUNCNAME("DOFMatrix::assemble()");

      TEST_EXIT_DBG(op)("No operator!\n");

275
      set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
276
      op->getElementMatrix(elInfo, elementMatrix, factor);
277
      addElementMatrix(factor, elementMatrix, bound, elInfo, NULL);
278
279
  }

280
281
282
  void DOFMatrix::assemble(double factor, 
			   ElInfo *rowElInfo, ElInfo *colElInfo,
			   ElInfo *smallElInfo, ElInfo *largeElInfo,
283
284
285
286
			   const BoundaryType *bound, Operator *op)
  {
    FUNCNAME("DOFMatrix::assemble()");

287
    if (!op && operators.size() == 0)
288
289
      return;

290
291
    set_to_zero(elementMatrix);

292
    if (op) {
293
      op->getElementMatrix(rowElInfo, colElInfo, smallElInfo, largeElInfo, 
294
			   elementMatrix);
295
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
296
297
298
      std::vector<Operator*>::iterator it = operators.begin();
      std::vector<double*>::iterator factorIt = operatorFactor.begin();
      for (; it != operators.end(); ++it, ++factorIt) {
Thomas Witkowski's avatar
Thomas Witkowski committed
299
300
301
302
303
304
305
	(*it)->getElementMatrix(rowElInfo, colElInfo,
				smallElInfo, largeElInfo,
				elementMatrix, 
				*factorIt ? **factorIt : 1.0);	
      }      
    }

306
    addElementMatrix(factor, elementMatrix, bound, rowElInfo, colElInfo);       
Thomas Witkowski's avatar
Thomas Witkowski committed
307
308
309
310
311
312
313
314
315
316
317
318
319
  }

  void DOFMatrix::assemble2(double factor, 
			    ElInfo *mainElInfo, ElInfo *auxElInfo,
			    ElInfo *smallElInfo, ElInfo *largeElInfo,			    
			   const BoundaryType *bound, Operator *op)
  {
    FUNCNAME("DOFMatrix::assemble2()");

    if (!op && operators.size() == 0) {
      return;
    }

320
    set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
321
    
322
    if (op) {
Thomas Witkowski's avatar
Thomas Witkowski committed
323
324
325
326
327
328
329
      ERROR_EXIT("TODO");
//       op->getElementMatrix(rowElInfo, colElInfo, 
// 			   smallElInfo, largeElInfo,
// 			   elementMatrix);
    } else {
      std::vector<Operator*>::iterator it;
      std::vector<double*>::iterator factorIt;
330
      for(it = operators.begin(), factorIt = operatorFactor.begin();	
Thomas Witkowski's avatar
Thomas Witkowski committed
331
332
333
334
	   it != operators.end(); 
	   ++it, ++factorIt) {
	if ((*it)->getNeedDualTraverse()) {
	  (*it)->getElementMatrix(mainElInfo, auxElInfo,
335
336
337
338
				  smallElInfo, largeElInfo,
				  elementMatrix, 
				  *factorIt ? **factorIt : 1.0);
	}
339
340
      }      
    }
341

342
    addElementMatrix(factor, elementMatrix, bound, mainElInfo, NULL);       
343
344
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
345
346
347
348
349
  void DOFMatrix::finishAssembling()
  {
    // call the operatos cleanup procedures
    for (std::vector<Operator*>::iterator it = operators.begin();
	 it != operators.end();
350
	 ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
351
352
353
      (*it)->finishAssembling();
  }

354
  // Should work as before
355
356
357
  Flag DOFMatrix::getAssembleFlag()
  {
    Flag fillFlag(0);
358
359
    for (std::vector<Operator*>::iterator op = operators.begin(); 
	 op != operators.end(); ++op)
360
      fillFlag |= (*op)->getFillFlag();
Thomas Witkowski's avatar
Thomas Witkowski committed
361

362
363
364
    return fillFlag;
  }

365
  void DOFMatrix::axpy(double a, const DOFMatrix& x, const DOFMatrix& y)
366
  {
367
    matrix+= a * x.matrix + y.matrix;
368
369
  }

370

371
372
  void DOFMatrix::scal(double b) 
  {
373
    matrix*= b;
374
375
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
376
377
378
379
380
381
382
  void DOFMatrix::addOperator(Operator *op, double* factor, double* estFactor) 
  { 
    operators.push_back(op);
    operatorFactor.push_back(factor);
    operatorEstFactor.push_back(estFactor);
  }

383
384
  void DOFMatrix::copy(const DOFMatrix& rhs) 
  {
385
    matrix= rhs.matrix;
386
387
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
388
  void DOFMatrix::removeRowsWithDBC(std::set<int> *rows)
389
  {      
390
391
392
393
394
395
    inserter_type &ins= *inserter;
   
    for (std::set<int>::iterator it = rows->begin(); it != rows->end(); ++it)
      ins[*it][*it] = 1.0;

    rows->clear();
396
397
  }

398
399
  int DOFMatrix::memsize() 
  {   
400
401
    return (num_rows(matrix) + matrix.nnz()) * sizeof(base_matrix_type::size_type)
      + matrix.nnz() * sizeof(base_matrix_type::value_type);
402
403
404
  }

}