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

18

19
20
namespace AMDiS {

21
22
  using namespace mtl;

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

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

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

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

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

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

    applyDBCs.clear();
60
61
62
  }

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

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

  DOFMatrix::~DOFMatrix()
  {
75
    FUNCNAME("DOFMatrix::~DOFMatrix()");
Thomas Witkowski's avatar
Thomas Witkowski committed
76

77
    if (rowFESpace && rowFESpace->getAdmin())
78
      (const_cast<DOFAdmin*>(rowFESpace->getAdmin()))->removeDOFIndexed(this);
Thomas Witkowski's avatar
Thomas Witkowski committed
79
80
81
82
    if (boundaryManager) 
      delete boundaryManager;
    if (inserter) 
      delete inserter;
83
84
85
86
87
  }

  void DOFMatrix::print() const
  {
    FUNCNAME("DOFMatrix::print()");
88
89
90
91

    if (inserter) 
      inserter->print();

92
  /*  using mtl::tag::major; using mtl::tag::nz; using mtl::begin; using mtl::end;
93
94
    namespace traits= mtl::traits;
    typedef base_matrix_type Matrix;
95

96
97
98
    traits::row<Matrix>::type                                 row(matrix);
    traits::col<Matrix>::type                                 col(matrix);
    traits::const_value<Matrix>::type                         value(matrix);
99

100
101
    typedef traits::range_generator<major, Matrix>::type      cursor_type;
    typedef traits::range_generator<nz, cursor_type>::type    icursor_type;
102
103

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

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

117
    double tol = 1e-5;
118

119
120
121
    using mtl::tag::major; using mtl::tag::nz; using mtl::begin; using mtl::end;
    namespace traits= mtl::traits;
    typedef base_matrix_type   Matrix;
122

123
124
125
    traits::row<Matrix>::type                                 row(matrix);
    traits::col<Matrix>::type                                 col(matrix);
    traits::const_value<Matrix>::type                         value(matrix);
126

127
128
129
130
131
132
133
    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)
134
135
136
137
138
139
140
141
	  return false;
    return true;
  }

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

142
    int non_symmetric = !symmetric();
143

Thomas Witkowski's avatar
Thomas Witkowski committed
144
    if (non_symmetric)
145
      MSG("matrix `%s' not symmetric.\n", name.data());
Thomas Witkowski's avatar
Thomas Witkowski committed
146
    else
147
148
149
150
151
152
153
154
155
      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;
156
    coupleMatrix = rhs.coupleMatrix;
Thomas Witkowski's avatar
Thomas Witkowski committed
157
158
159
160
161

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

Thomas Witkowski's avatar
Thomas Witkowski committed
162
    if (rhs.boundaryManager)
Thomas Witkowski's avatar
Thomas Witkowski committed
163
      boundaryManager = new BoundaryManager(*rhs.boundaryManager);
Thomas Witkowski's avatar
Thomas Witkowski committed
164
    else
Thomas Witkowski's avatar
Thomas Witkowski committed
165
      boundaryManager = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
166
    
167
168
169
    nRow = rhs.nRow;
    nCol = rhs.nCol;
    elementMatrix.change_dim(nRow, nCol);
170
171
172
173

    return *this;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
174
  void DOFMatrix::addElementMatrix(const ElementMatrix& elMat, 
175
				   const BoundaryType *bound,
176
				   ElInfo* rowElInfo,
Thomas Witkowski's avatar
Thomas Witkowski committed
177
				   ElInfo* colElInfo)
178
  {
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

Thomas Witkowski's avatar
Thomas Witkowski committed
186
187
188
    rowFESpace->getBasisFcts()->getLocalIndices(rowElInfo->getElement(),
						rowFESpace->getAdmin(),
						rowIndices);
189
    if (rowFESpace == colFESpace) {
190
      colIndices = rowIndices;
191
192
    } else {
      if (colElInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
193
194
195
	colFESpace->getBasisFcts()->getLocalIndices(colElInfo->getElement(),
						    colFESpace->getAdmin(),
						    colIndices);
196
197
      } else {
	// If there is no colElInfo pointer, use rowElInfo the get the indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
198
199
200
	colFESpace->getBasisFcts()->getLocalIndices(rowElInfo->getElement(),
						    colFESpace->getAdmin(),
						    colIndices);
201
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
202
    }
203

Thomas Witkowski's avatar
Thomas Witkowski committed
204
    for (int i = 0; i < nRow; i++)  {
205
206
      DegreeOfFreedom row = rowIndices[i];

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

      if (condition && condition->isDirichlet()) {
211
212
	if (condition->applyBoundaryCondition()) {
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
213
214
	  if (rankDofs[rowIndices[i]]) 
	    applyDBCs.insert(static_cast<int>(rowIndices[i]));
215
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
216
	  applyDBCs.insert(static_cast<int>(rowIndices[i]));
217
218
#endif
	}
219
220
221
222
223
224
225
      } else {
	for (int j = 0; j < nCol; j++) {
	  DegreeOfFreedom col = colIndices[j];
	  double entry = elMat[i][j];
  
	  ins[row][col] += entry;
	}
226
      }
227
    }
228
  }
229

230
  double DOFMatrix::logAcc(DegreeOfFreedom a, DegreeOfFreedom b) const
231
  {
232
    return matrix[a][b];
233
234
  }

235
  void DOFMatrix::freeDOFContent(int index)
236
  {}
237

238
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, const BoundaryType *bound)
239
  {
240
    FUNCNAME("DOFMatrix::assemble()");
241

242
    set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
243

244
245
    std::vector<Operator*>::iterator it = operators.begin();
    std::vector<double*>::iterator factorIt = operatorFactor.begin();
246
247
248
    for (; it != operators.end(); ++it, ++factorIt)
      if ((*it)->getNeedDualTraverse() == false)
	(*it)->getElementMatrix(elInfo,	elementMatrix, *factorIt ? **factorIt : 1.0);
249

Thomas Witkowski's avatar
Thomas Witkowski committed
250
251
    if (factor != 1.0)
      elementMatrix *= factor;
252

Thomas Witkowski's avatar
Thomas Witkowski committed
253
    addElementMatrix(elementMatrix, bound, elInfo, NULL);   
254
255
  }

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

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

263
      set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
264
      op->getElementMatrix(elInfo, elementMatrix, factor);
Thomas Witkowski's avatar
Thomas Witkowski committed
265
266
267
268
269

      if (factor != 1.0)
	elementMatrix *= factor;

      addElementMatrix(elementMatrix, bound, elInfo, NULL);
270
271
  }

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

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

282
283
    set_to_zero(elementMatrix);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
298
299
300
301
    if (factor != 1.0)
      elementMatrix *= factor;

    addElementMatrix(elementMatrix, bound, rowElInfo, colElInfo);       
Thomas Witkowski's avatar
Thomas Witkowski committed
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()");

Thomas Witkowski's avatar
Thomas Witkowski committed
311
    if (!op && operators.size() == 0)
Thomas Witkowski's avatar
Thomas Witkowski committed
312
313
      return;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
336
337
338
339
    if (factor != 1.0)
      elementMatrix *= factor;

    addElementMatrix(elementMatrix, bound, mainElInfo, NULL);       
340
341
  }

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

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

358
359
360
    return fillFlag;
  }

361
  void DOFMatrix::axpy(double a, const DOFMatrix& x, const DOFMatrix& y)
362
  {
363
    matrix+= a * x.matrix + y.matrix;
364
365
  }

366

367
368
  void DOFMatrix::scal(double b) 
  {
369
    matrix*= b;
370
371
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
372
373
374
375
376
377
378
  void DOFMatrix::addOperator(Operator *op, double* factor, double* estFactor) 
  { 
    operators.push_back(op);
    operatorFactor.push_back(factor);
    operatorEstFactor.push_back(estFactor);
  }

379
380
  void DOFMatrix::copy(const DOFMatrix& rhs) 
  {
381
    matrix= rhs.matrix;
382
383
  }

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

    rows->clear();
392
393
  }

394
395
  int DOFMatrix::memsize() 
  {   
396
397
    return (num_rows(matrix) + matrix.nnz()) * sizeof(base_matrix_type::size_type)
      + matrix.nnz() * sizeof(base_matrix_type::value_type);
398
399
400
  }

}