DOFMatrix.cc 10.6 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()");
Thomas Witkowski's avatar
Thomas Witkowski committed
75

76
    if (rowFESpace && rowFESpace->getAdmin())
77
      (const_cast<DOFAdmin*>(rowFESpace->getAdmin()))->removeDOFIndexed(this);
Thomas Witkowski's avatar
Thomas Witkowski committed
78
79
80
81
    if (boundaryManager) 
      delete boundaryManager;
    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

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

    /// 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
158
    if (rhs.boundaryManager)
Thomas Witkowski's avatar
Thomas Witkowski committed
159
      boundaryManager = new BoundaryManager(*rhs.boundaryManager);
Thomas Witkowski's avatar
Thomas Witkowski committed
160
    else
Thomas Witkowski's avatar
Thomas Witkowski committed
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

    return *this;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
170
  void DOFMatrix::addElementMatrix(const ElementMatrix& elMat, 
171
				   const BoundaryType *bound,
172
				   ElInfo* rowElInfo,
Thomas Witkowski's avatar
Thomas Witkowski committed
173
				   ElInfo* colElInfo)
174
  {
175
    FUNCNAME("DOFMatrix::addElementMatrix()");
176

177
    TEST_EXIT_DBG(inserter)("DOFMatrix is not in insertion mode");
178

179
    // === Get indices mapping from local to global matrix indices. ===
180

Thomas Witkowski's avatar
Thomas Witkowski committed
181
182
183
    rowFESpace->getBasisFcts()->getLocalIndices(rowElInfo->getElement(),
						rowFESpace->getAdmin(),
						rowIndices);
184
    if (rowFESpace == colFESpace) {
Thomas Witkowski's avatar
Thomas Witkowski committed
185
      *inserter << element_matrix(elMat, rowIndices, rowIndices);
186
187
    } else {
      if (colElInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
188
189
190
	colFESpace->getBasisFcts()->getLocalIndices(colElInfo->getElement(),
						    colFESpace->getAdmin(),
						    colIndices);
191
192
      } else {
	// If there is no colElInfo pointer, use rowElInfo the get the indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
193
194
195
	colFESpace->getBasisFcts()->getLocalIndices(rowElInfo->getElement(),
						    colFESpace->getAdmin(),
						    colIndices);
196
      }
197

Thomas Witkowski's avatar
Thomas Witkowski committed
198
199
      *inserter << element_matrix(elMat, rowIndices, colIndices);
    }
200

201

Thomas Witkowski's avatar
Thomas Witkowski committed
202
    for (int i = 0; i < nRow; i++)  {
203
204
205
206
      BoundaryCondition *condition = 
	bound ? boundaryManager->getBoundaryCondition(bound[i]) : NULL;

      if (condition && condition->isDirichlet()) {
207
208
	if (condition->applyBoundaryCondition()) {
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
209
210
	  if (rankDofs[rowIndices[i]]) 
	    applyDBCs.insert(static_cast<int>(rowIndices[i]));
211
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
212
	  applyDBCs.insert(static_cast<int>(rowIndices[i]));
213
214
#endif
	}
215
      }
216
217
218
    }
  }

219
  double DOFMatrix::logAcc(DegreeOfFreedom a, DegreeOfFreedom b) const
220
  {
221
    return matrix[a][b];
222
223
  }

224
  void DOFMatrix::freeDOFContent(int index)
225
  {}
226

227
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, const BoundaryType *bound)
228
  {
229
    FUNCNAME("DOFMatrix::assemble()");
230

231
    set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
232

233
234
    std::vector<Operator*>::iterator it = operators.begin();
    std::vector<double*>::iterator factorIt = operatorFactor.begin();
235
236
237
    for (; it != operators.end(); ++it, ++factorIt)
      if ((*it)->getNeedDualTraverse() == false)
	(*it)->getElementMatrix(elInfo,	elementMatrix, *factorIt ? **factorIt : 1.0);
238

Thomas Witkowski's avatar
Thomas Witkowski committed
239
240
241
242
    if (factor != 1.0)
      elementMatrix *= factor;

    addElementMatrix(elementMatrix, bound, elInfo, NULL);   
243
244
  }

245
246
247
248
249
250
251
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, const BoundaryType *bound,
			   Operator *op)
  {
      FUNCNAME("DOFMatrix::assemble()");

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

252
      set_to_zero(elementMatrix);
Thomas Witkowski's avatar
Thomas Witkowski committed
253
      op->getElementMatrix(elInfo, elementMatrix, factor);
Thomas Witkowski's avatar
Thomas Witkowski committed
254
255
256
257
258

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

      addElementMatrix(elementMatrix, bound, elInfo, NULL);
259
260
  }

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

268
    if (!op && operators.size() == 0)
269
270
      return;

271
272
    set_to_zero(elementMatrix);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
289
290
    if (factor != 1.0)
      elementMatrix *= factor;

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

  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
300
    if (!op && operators.size() == 0)
Thomas Witkowski's avatar
Thomas Witkowski committed
301
302
      return;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
325
326
327
328
    if (factor != 1.0)
      elementMatrix *= factor;

    addElementMatrix(elementMatrix, bound, mainElInfo, NULL);       
329
330
  }

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

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

347
348
349
    return fillFlag;
  }

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

355

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

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

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

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

    rows->clear();
381
382
  }

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

}