DOFMatrix.cc 10.7 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
#include "Utilities.h"

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
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
62
63
    applicationOrdering = NULL;
#endif
64
65
66
  }

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

    TEST_EXIT(rhs.inserter == 0)("Cannot copy during insertion");
    inserter= 0;
75
76
77
78
  }

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

83
    if (boundaryManager) delete boundaryManager;
84
    if (inserter) delete inserter;
85
86
87
88
89
90
  }

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

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

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

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

    std::cout.precision(10);
103
104
105
106
107
    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)
	std::cout << "(" << row(*icursor) << "," << col(*icursor) << "," << value(*icursor) << ") ";
      std::cout << "\n";
    }
108
109
110
111
112
113
  }

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

114
    double tol = 1e-5;
115

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

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

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

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

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

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

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

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

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

    return *this;
  }

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

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

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

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

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

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

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

      if (condition && condition->isDirichlet()) {
214
	if (condition->applyBoundaryCondition())
215
	  applyDBCs.insert(static_cast<int>(row));
216
      } else
217
	for (int j = 0; j < nCol; j++) {  // for all columns
218
219
	  DegreeOfFreedom col = colIndices[j];
	  double entry = elMat[i][j];
220

221
222
223
224
225
	  if (add)
	    ins[row][col]+= sign * entry;
	  else
	    ins[row][col]= sign * entry;
	}   
226
227
228
    }
  }

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

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

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

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

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

249
    addElementMatrix(factor, elementMatrix, bound, elInfo, NULL);   
250
251
  }

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

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

259
      set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
260
      op->getElementMatrix(elInfo, elementMatrix, factor);
261
      addElementMatrix(factor, elementMatrix, bound, elInfo, NULL);
262
263
  }

264
265
266
  void DOFMatrix::assemble(double factor, 
			   ElInfo *rowElInfo, ElInfo *colElInfo,
			   ElInfo *smallElInfo, ElInfo *largeElInfo,
267
268
269
270
			   const BoundaryType *bound, Operator *op)
  {
    FUNCNAME("DOFMatrix::assemble()");

271
    if (!op && operators.size() == 0)
272
273
      return;

274
275
    set_to_zero(elementMatrix);

276
    if (op) {
277
      op->getElementMatrix(rowElInfo, colElInfo, smallElInfo, largeElInfo, 
278
			   elementMatrix);
279
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
280
281
282
      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
283
284
285
286
287
288
289
	(*it)->getElementMatrix(rowElInfo, colElInfo,
				smallElInfo, largeElInfo,
				elementMatrix, 
				*factorIt ? **factorIt : 1.0);	
      }      
    }

290
    addElementMatrix(factor, elementMatrix, bound, rowElInfo, colElInfo);       
Thomas Witkowski's avatar
Thomas Witkowski committed
291
292
293
294
295
296
297
298
299
300
301
302
303
  }

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

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

326
    addElementMatrix(factor, elementMatrix, bound, mainElInfo, NULL);       
327
328
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
329
330
331
332
333
  void DOFMatrix::finishAssembling()
  {
    // call the operatos cleanup procedures
    for (std::vector<Operator*>::iterator it = operators.begin();
	 it != operators.end();
334
	 ++it)
Thomas Witkowski's avatar
Thomas Witkowski committed
335
336
337
      (*it)->finishAssembling();
  }

338
  // Should work as before
339
340
341
  Flag DOFMatrix::getAssembleFlag()
  {
    Flag fillFlag(0);
342
343
    for (std::vector<Operator*>::iterator op = operators.begin(); 
	 op != operators.end(); ++op)
344
      fillFlag |= (*op)->getFillFlag();
Thomas Witkowski's avatar
Thomas Witkowski committed
345

346
347
348
    return fillFlag;
  }

349
  void DOFMatrix::axpy(double a, const DOFMatrix& x, const DOFMatrix& y)
350
  {
351
    matrix+= a * x.matrix + y.matrix;
352
353
  }

354

355
356
  void DOFMatrix::scal(double b) 
  {
357
    matrix*= b;
358
359
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
360
361
362
363
364
365
366
  void DOFMatrix::addOperator(Operator *op, double* factor, double* estFactor) 
  { 
    operators.push_back(op);
    operatorFactor.push_back(factor);
    operatorEstFactor.push_back(estFactor);
  }

367
368
  void DOFMatrix::copy(const DOFMatrix& rhs) 
  {
369
    matrix= rhs.matrix;
370
371
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
372
  void DOFMatrix::removeRowsWithDBC(std::set<int> *rows)
373
  {      
374
375
376
377
378
379
    inserter_type &ins= *inserter;
   
    for (std::set<int>::iterator it = rows->begin(); it != rows->end(); ++it)
      ins[*it][*it] = 1.0;

    rows->clear();
380
381
  }

382
383
  int DOFMatrix::memsize() 
  {   
384
385
    return (num_rows(matrix) + matrix.nnz()) * sizeof(base_matrix_type::size_type)
      + matrix.nnz() * sizeof(base_matrix_type::value_type);
386
387
388
  }

}