DOFMatrix.cc 10.8 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

Thomas Witkowski's avatar
Thomas Witkowski committed
18
19
#include "mpi.h"

20
21
namespace AMDiS {

22
23
  using namespace mtl;

24
25
26
  DOFMatrix *DOFMatrix::traversePtr = NULL;

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

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

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

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

    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
59
60

    applyDBCs.clear();
61
62
63
  }

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

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

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

80
    if (boundaryManager) delete boundaryManager;
81
    if (inserter) delete inserter;
82
83
84
85
86
87
  }

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

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

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

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

    std::cout.precision(10);
100
101
    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)
102
	if (value(*icursor) != 0.0)
Thomas Witkowski's avatar
nix.  
Thomas Witkowski committed
103
104
	    std::cout << "(" << row(*icursor) << "," << col(*icursor) << "," << value(*icursor) << ") ";
      
105
106
      std::cout << "\n";
    }
107
108
109
110
111
112
  }

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

113
    double tol = 1e-5;
114

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

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

123
124
125
126
127
128
129
    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)
130
131
132
133
134
135
136
137
	  return false;
    return true;
  }

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

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

    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;
153
    coupleMatrix = rhs.coupleMatrix;
Thomas Witkowski's avatar
Thomas Witkowski committed
154
155
156
157
158

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

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

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

    return *this;
  }

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

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

184
    // === Get indices mapping from local to global matrix indices. ===
185

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    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);
      }
    }
203

204
205
206
    // === Add element matrix to the global matrix using the indices mapping. ===

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

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

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

223
	for (int j = 0; j < nCol; j++) {  // for all columns
224
225
	  DegreeOfFreedom col = colIndices[j];
	  double entry = elMat[i][j];
Thomas Witkowski's avatar
Thomas Witkowski committed
226
  
227
	  if (add)
228
	    ins[row][col] += sign * entry;
229
	  else
230
	    ins[row][col] = sign * entry;	
231
	}   
232
      }
233
234
235
    }
  }

236
  double DOFMatrix::logAcc(DegreeOfFreedom a, DegreeOfFreedom b) const
237
  {
238
    return matrix[a][b];
239
240
  }

241
  void DOFMatrix::freeDOFContent(int index)
242
  {}
243

244
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, const BoundaryType *bound)
245
  {
246
    FUNCNAME("DOFMatrix::assemble()");
247

248
    set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
249

250
251
    std::vector<Operator*>::iterator it = operators.begin();
    std::vector<double*>::iterator factorIt = operatorFactor.begin();
252
253
254
    for (; it != operators.end(); ++it, ++factorIt)
      if ((*it)->getNeedDualTraverse() == false)
	(*it)->getElementMatrix(elInfo,	elementMatrix, *factorIt ? **factorIt : 1.0);
255

256
    addElementMatrix(factor, elementMatrix, bound, elInfo, NULL);   
257
258
  }

259
260
261
262
263
264
265
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, const BoundaryType *bound,
			   Operator *op)
  {
      FUNCNAME("DOFMatrix::assemble()");

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

266
      set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
267
      op->getElementMatrix(elInfo, elementMatrix, factor);
268
      addElementMatrix(factor, elementMatrix, bound, elInfo, NULL);
269
270
  }

271
272
273
  void DOFMatrix::assemble(double factor, 
			   ElInfo *rowElInfo, ElInfo *colElInfo,
			   ElInfo *smallElInfo, ElInfo *largeElInfo,
274
275
276
277
			   const BoundaryType *bound, Operator *op)
  {
    FUNCNAME("DOFMatrix::assemble()");

278
    if (!op && operators.size() == 0)
279
280
      return;

281
282
    set_to_zero(elementMatrix);

283
    if (op) {
284
      op->getElementMatrix(rowElInfo, colElInfo, smallElInfo, largeElInfo, 
285
			   elementMatrix);
286
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
289
      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
290
291
292
293
294
295
296
	(*it)->getElementMatrix(rowElInfo, colElInfo,
				smallElInfo, largeElInfo,
				elementMatrix, 
				*factorIt ? **factorIt : 1.0);	
      }      
    }

297
    addElementMatrix(factor, elementMatrix, bound, rowElInfo, colElInfo);       
Thomas Witkowski's avatar
Thomas Witkowski committed
298
299
300
301
302
303
304
305
306
307
308
309
310
  }

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

311
    set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
312
    
313
    if (op) {
Thomas Witkowski's avatar
Thomas Witkowski committed
314
315
316
317
318
319
320
      ERROR_EXIT("TODO");
//       op->getElementMatrix(rowElInfo, colElInfo, 
// 			   smallElInfo, largeElInfo,
// 			   elementMatrix);
    } else {
      std::vector<Operator*>::iterator it;
      std::vector<double*>::iterator factorIt;
321
      for(it = operators.begin(), factorIt = operatorFactor.begin();	
Thomas Witkowski's avatar
Thomas Witkowski committed
322
323
324
325
	   it != operators.end(); 
	   ++it, ++factorIt) {
	if ((*it)->getNeedDualTraverse()) {
	  (*it)->getElementMatrix(mainElInfo, auxElInfo,
326
327
328
329
				  smallElInfo, largeElInfo,
				  elementMatrix, 
				  *factorIt ? **factorIt : 1.0);
	}
330
331
      }      
    }
332

333
    addElementMatrix(factor, elementMatrix, bound, mainElInfo, NULL);       
334
335
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
336
337
338
339
340
  void DOFMatrix::finishAssembling()
  {
    // call the operatos cleanup procedures
    for (std::vector<Operator*>::iterator it = operators.begin();
	 it != operators.end();
341
	 ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
342
343
344
      (*it)->finishAssembling();
  }

345
  // Should work as before
346
347
348
  Flag DOFMatrix::getAssembleFlag()
  {
    Flag fillFlag(0);
349
350
    for (std::vector<Operator*>::iterator op = operators.begin(); 
	 op != operators.end(); ++op)
351
      fillFlag |= (*op)->getFillFlag();
Thomas Witkowski's avatar
Thomas Witkowski committed
352

353
354
355
    return fillFlag;
  }

356
  void DOFMatrix::axpy(double a, const DOFMatrix& x, const DOFMatrix& y)
357
  {
358
    matrix+= a * x.matrix + y.matrix;
359
360
  }

361

362
363
  void DOFMatrix::scal(double b) 
  {
364
    matrix*= b;
365
366
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
367
368
369
370
371
372
373
  void DOFMatrix::addOperator(Operator *op, double* factor, double* estFactor) 
  { 
    operators.push_back(op);
    operatorFactor.push_back(factor);
    operatorEstFactor.push_back(estFactor);
  }

374
375
  void DOFMatrix::copy(const DOFMatrix& rhs) 
  {
376
    matrix= rhs.matrix;
377
378
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
379
  void DOFMatrix::removeRowsWithDBC(std::set<int> *rows)
380
  {      
381
382
383
384
385
386
    inserter_type &ins= *inserter;
   
    for (std::set<int>::iterator it = rows->begin(); it != rows->end(); ++it)
      ins[*it][*it] = 1.0;

    rows->clear();
387
388
  }

389
390
  int DOFMatrix::memsize() 
  {   
391
392
    return (num_rows(matrix) + matrix.nnz()) * sizeof(base_matrix_type::size_type)
      + matrix.nnz() * sizeof(base_matrix_type::value_type);
393
394
395
  }

}