DOFMatrix.h 14.1 KB
Newer Older
1
2
3
4
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
5
// ==  http://www.amdis-fem.org                                              ==
6
7
// ==                                                                        ==
// ============================================================================
8
9
10
11
12
13
14
15
16
17
18
19
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


20
21
22
23
24
25
26

/** \file DOFMatrix.h */

#ifndef AMDIS_DOFMATRIX_H
#define AMDIS_DOFMATRIX_H

#include <vector>
Thomas Witkowski's avatar
Thomas Witkowski committed
27
#include <set>
28
29
#include <memory>
#include <list>
30
31
#include <boost/numeric/mtl/mtl.hpp>
#include "AMDiS_fwd.h"
32
33
34
35
36
37
38
39
40
41
#include "Global.h"
#include "Flag.h"
#include "RCNeighbourList.h"
#include "DOFAdmin.h"
#include "DOFIndexed.h"
#include "Boundary.h"
#include "Serializable.h"

namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
42
43
  using namespace std;

44
45
46
  /** \ingroup DOFAdministration
   * \brief
   * A DOFMatrix is a sparse matrix representation for matrices that work
47
48
   * on DOFVectors. The underlying matrix type 
   * is a CRS matrix of double.
49
   */
50
  class DOFMatrix : public DOFIndexed<bool>,
51
52
53
                    public Serializable
  {
  public:
54
    /// Type of scalars in the underlying matrix
55
    typedef double value_type;
56

57
58
59
    typedef unsigned size_type;
    typedef mtl::matrix::parameters<mtl::row_major, mtl::index::c_index, mtl::non_fixed::dimensions, false, size_type> para;

60
    /// Type of underlying matrix
61
    typedef mtl::compressed2D<value_type, para> base_matrix_type;
62
63

    /// Type of inserter for the base matrix;
Thomas Witkowski's avatar
Thomas Witkowski committed
64
    typedef mtl::matrix::inserter<base_matrix_type, mtl::operations::update_plus<value_type> >  inserter_type;
65
66

  private:
Thomas Witkowski's avatar
Thomas Witkowski committed
67
    /// Symbolic constant for an unused matrix entry
68
69
    static const int UNUSED_ENTRY = -1;
    
Thomas Witkowski's avatar
Thomas Witkowski committed
70
71
    /// Symbolic constant for an unused entry which is not followed by any
    /// used entry in this row
72
73
74
75
76
    static const int NO_MORE_ENTRIES = -2;

  public:    
    DOFMatrix();

77
    /// Constructs a DOFMatrix with name n and the given row and olumn FeSpaces.
Thomas Witkowski's avatar
Thomas Witkowski committed
78
79
80
    DOFMatrix(const FiniteElemSpace* rowFeSpace, 
	      const FiniteElemSpace* colFeSpace,
	      string n = "");
81

Thomas Witkowski's avatar
Thomas Witkowski committed
82
    /// Copy-Constructor
83
84
    DOFMatrix(const DOFMatrix& rhs);

Thomas Witkowski's avatar
Thomas Witkowski committed
85
    /// Destructor
86
87
    virtual ~DOFMatrix();
  
Thomas Witkowski's avatar
Thomas Witkowski committed
88
    /// Assignment operator.
89
90
91
92
    DOFMatrix& operator=(const DOFMatrix& rhs);

    void copy(const DOFMatrix& rhs);

93
94
95
96
    /// Access underlying matrix directly
    base_matrix_type& getBaseMatrix()
    {
	return matrix;
97
    }
98

99
100
101
102
    /// Access underlying matrix directly (const)
    const base_matrix_type& getBaseMatrix() const
    {
	return matrix;
103
    }
104
105

    // Only to get rid of the abstract functions, I hope they are not used
Thomas Witkowski's avatar
Thomas Witkowski committed
106
    vector<bool>::iterator begin() 
Thomas Witkowski's avatar
Thomas Witkowski committed
107
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
108
      ERROR_EXIT("Shouldn't be used, only fake."); vector<bool> v; 
Thomas Witkowski's avatar
Thomas Witkowski committed
109
110
111
      return v.begin();
    }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
112
    vector<bool>::iterator end() 
Thomas Witkowski's avatar
Thomas Witkowski committed
113
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
114
      ERROR_EXIT("Shouldn't be used, only fake."); vector<bool> v; 
Thomas Witkowski's avatar
Thomas Witkowski committed
115
116
      return v.end();
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
117
118
119
120
121
122
123
124
125
126
127
128
129

    // Only to get rid of the abstract functions, I hope they are not used
    vector<bool>::const_iterator begin() const
    {
      ERROR_EXIT("Shouldn't be used, only fake."); vector<bool> v;
      return v.begin();
    }

    vector<bool>::const_iterator end() const
    {
      ERROR_EXIT("Shouldn't be used, only fake."); vector<bool> v;
      return v.end();
    }
130
131
    
    bool dummy; // Must be deleted later
Thomas Witkowski's avatar
Thomas Witkowski committed
132
133
134
135
136
137
138
139
140
141
142
    bool& operator[](int i) 
    {
      ERROR_EXIT("Shouldn't be used, only fake."); 
      return dummy;
    }

    const bool& operator[](int i) const 
    {
      ERROR_EXIT("Shouldn't be used, only fake."); 
      return dummy;
    }
143
 
144
    /// DOFMatrix does not need to be compressed before assembling, when using MTL4.
Thomas Witkowski's avatar
Thomas Witkowski committed
145
    void compressDOFIndexed(int first, int last, vector<DegreeOfFreedom> &newDOF) 
Thomas Witkowski's avatar
Thomas Witkowski committed
146
    {}
147

148
    /// Implements DOFIndexedBase::freeDOFContent()
149
150
    virtual void freeDOFContent(int index);

151
    /// Returns \ref coupleMatrix.
Thomas Witkowski's avatar
Thomas Witkowski committed
152
153
    inline bool isCoupleMatrix() 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
154
      return coupleMatrix; 
155
    }
156

157
    /// Returns \ref coupleMatrix.
Thomas Witkowski's avatar
Thomas Witkowski committed
158
159
    inline void setCoupleMatrix(bool c) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
160
      coupleMatrix = c; 
161
    }
162

163
    /// a * x + y
Thomas Witkowski's avatar
Thomas Witkowski committed
164
    void axpy(double a, const DOFMatrix& x, const DOFMatrix& y);
165

166
    /// Multiplication with a scalar.
167
168
    void scal(double s);

Thomas Witkowski's avatar
Thomas Witkowski committed
169
170
171
172
173
    /// Adds an operator to the DOFMatrix. A factor, that is multipled to the 
    /// operator, and a multilier factor for the estimator may be also given.
    void addOperator(Operator *op, 
		     double* factor = NULL, 
		     double* estFactor = NULL);
174

175
176
177
    ///
    void clearOperators();

Thomas Witkowski's avatar
Thomas Witkowski committed
178
    inline vector<double*>::iterator getOperatorFactorBegin() 
Thomas Witkowski's avatar
Thomas Witkowski committed
179
    {
180
      return operatorFactor.begin();
181
    }
182

Thomas Witkowski's avatar
Thomas Witkowski committed
183
    inline vector<double*>::iterator getOperatorFactorEnd() 
Thomas Witkowski's avatar
Thomas Witkowski committed
184
    {
185
      return operatorFactor.end();
186
    }
187

Thomas Witkowski's avatar
Thomas Witkowski committed
188
    inline vector<double*>::iterator getOperatorEstFactorBegin() 
Thomas Witkowski's avatar
Thomas Witkowski committed
189
    {
190
      return operatorEstFactor.begin();
191
    }
192

Thomas Witkowski's avatar
Thomas Witkowski committed
193
    inline vector<double*>::iterator getOperatorEstFactorEnd() 
Thomas Witkowski's avatar
Thomas Witkowski committed
194
    {
195
      return operatorEstFactor.end();
196
    }
197

Thomas Witkowski's avatar
Thomas Witkowski committed
198
    inline vector<Operator*>::iterator getOperatorsBegin() 
Thomas Witkowski's avatar
Thomas Witkowski committed
199
    {
200
      return operators.begin();
201
    }
202

Thomas Witkowski's avatar
Thomas Witkowski committed
203
    inline vector<Operator*>::iterator getOperatorsEnd() 
Thomas Witkowski's avatar
Thomas Witkowski committed
204
    {
205
      return operators.end();
206
    }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

    Flag getAssembleFlag();

    /** \brief
     * Updates the matrix matrix by traversing the underlying mesh and assembling
     * the element contributions into the matrix. Information about the 
     * computation of element matrices and connection of local and global DOFs is
     * stored in minfo; the flags for the mesh traversal are stored at 
     * minfo->fill_flags which specifies the elements to be visited and 
     * information that should be present on the elements for the calculation of 
     * the element matrices and boundary information (if minfo->boundBas is not
     * NULL). On the elements, information about the row DOFs is accessed by 
     * minfo->rowBas using info->row_admin; this vector is also used for the 
     * column DOFs if minfo->nCol is less or equal zero, or minfo->col_admin or 
     * minfo->colBas is a NULL pointer; if row and column DOFs are the same, the 
     * boundary type of the DOFs is accessed by minfo->boundBas if 
     * minfo->boundBas is not a NULL pointer; then the element matrix is 
     * computed by minfo->fillElementMatrix(el info, minfo->myFill); these 
     * contributions, multiplied by minfo->factor, are eventually added to matrix
     * by a call of addElementMatrix() with all information about row and column 
     * DOFs, the element matrix, and boundary types, if available;
     * updateMatrix() only adds element contributions; this makes several calls 
     * for the assemblage of one matrix possible; before the first call, the 
     * matrix should be cleared by calling clear dof matrix().
     */
  
233
    void assemble(double factor, ElInfo *elInfo, const BoundaryType *bound);
234

235
236
    void assemble(double factor, ElInfo *elInfo, const BoundaryType *bound,
		  Operator *op);
237

238
239
240
    void assemble(double factor, 
		  ElInfo *rowElInfo, ElInfo *colElInfo,
		  ElInfo *smallElInfo, ElInfo *largeElInfo,
Thomas Witkowski's avatar
Thomas Witkowski committed
241
242
		  const BoundaryType *bound,
		  Operator *op = NULL);
243

Thomas Witkowski's avatar
Thomas Witkowski committed
244
245
246
247
248
249
250
    void assemble2(double factor, 
		   ElInfo *mainElInfo, ElInfo *auxElInfo,
		   ElInfo *smallElInfo, ElInfo *largeElInfo,
		   const BoundaryType *bound, 
		   Operator *op = NULL);

    /// Adds an element matrix to \ref matrix
Thomas Witkowski's avatar
Thomas Witkowski committed
251
    void addElementMatrix(const ElementMatrix& elMat, 
252
			  const BoundaryType *bound,
253
			  ElInfo* rowElInfo,
Thomas Witkowski's avatar
Thomas Witkowski committed
254
			  ElInfo* colElInfo);
255

256
257
258
    ///
    void assembleOperator(Operator &op);

Thomas Witkowski's avatar
Thomas Witkowski committed
259
260
261
    /// That function must be called after the matrix assembling has been 
    /// finished. This makes it possible to start some cleanup or matrix 
    /// data compressing procedures.
Thomas Witkowski's avatar
Thomas Witkowski committed
262
263
    void finishAssembling();

Thomas Witkowski's avatar
Thomas Witkowski committed
264
265
266
267
    /// Enable insertion for assembly. You can optionally give an upper limit for
    /// entries per row (per column for CCS matrices).  Choosing this parameter
    /// too small can induce perceivable overhead for compressed matrices. Thus,
    /// it's better to choose a bit too large than too small.
Thomas Witkowski's avatar
Thomas Witkowski committed
268
    void startInsertion(int nnz_per_row = 10);
269

Thomas Witkowski's avatar
Thomas Witkowski committed
270
271
    /// Finishes insertion. For compressed matrix types, this is where the
    /// compression happens.
272
273
    void finishInsertion()
    {
274
275
276
      FUNCNAME("DOFMatrix::finishInsertion()");

      TEST_EXIT(inserter)("Inserter wasn't used or is already finished.\n");
277
278
279
      
      delete inserter;
      inserter= 0;
280
    }
281

282
#if 0
Thomas Witkowski's avatar
Thomas Witkowski committed
283
284
    /// Returns whether restriction should be performed after coarsening
    /// (false by default)
Thomas Witkowski's avatar
Thomas Witkowski committed
285
286
    virtual bool coarseRestrict() 
    {
287
      return false;
288
    }
289
#endif
290

291
292
    /// Returns const \ref rowFeSpace
    const FiniteElemSpace* getRowFeSpace() const 
Thomas Witkowski's avatar
Thomas Witkowski committed
293
    { 
294
      return rowFeSpace; 
295
    }
296

297
298
    /// Returns const \ref colFeSpace
    const FiniteElemSpace* getColFeSpace() const 
Thomas Witkowski's avatar
Thomas Witkowski committed
299
    { 
300
      return colFeSpace; 
301
    }
302

303
304
    /// Returns const \ref rowFeSpace
    const FiniteElemSpace* getFeSpace() const 
Thomas Witkowski's avatar
Thomas Witkowski committed
305
    { 
306
      return rowFeSpace; 
307
    }
308

309
    /// Returns number of rows (\ref matrix.size())
Thomas Witkowski's avatar
Thomas Witkowski committed
310
311
    inline int getSize() const 
    { 
312
      return num_rows(matrix);
313
    }
314

Thomas Witkowski's avatar
Thomas Witkowski committed
315
316
    /// Returns the number of used rows (equal to number of used DOFs in
    /// the row FE space).
Thomas Witkowski's avatar
Thomas Witkowski committed
317
318
    inline int getUsedSize() const 
    {
319
      return rowFeSpace->getAdmin()->getUsedSize();
320
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
321

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
322
323
324
325
326
    std::set<DegreeOfFreedom>& getDirichletRows()
    {
      return dirichletDofs;
    }

327
    /// Returns \ref name
Thomas Witkowski's avatar
Thomas Witkowski committed
328
    inline string getName() const 
Thomas Witkowski's avatar
Thomas Witkowski committed
329
    { 
330
      return name; 
331
    }
332

333
    /// Resizes \ref matrix to n rows
Thomas Witkowski's avatar
Thomas Witkowski committed
334
335
    inline void resize(int n) 
    { 
336
      TEST_EXIT_DBG(n >= 0)("Can't resize DOFMatrix to negative size\n");
337
    }
338

339
    /// Returns value at logical indices a,b
340
    double logAcc(DegreeOfFreedom a, DegreeOfFreedom b) const;
341

342
    /// Changes col at logical indices a,b to c 
343
    void changeColOfEntry(DegreeOfFreedom a, DegreeOfFreedom b, DegreeOfFreedom c);
344

Thomas Witkowski's avatar
Thomas Witkowski committed
345
346
    /// Creates an entry with logical indices irow, icol if there is no entry
    /// yet. Than sign * entry is added to the value at this logical indices
347
348
349
    void addSparseDOFEntry(double sign,
			   int irow, int jcol, double entry,
			   bool add = true);
350

Thomas Witkowski's avatar
Thomas Witkowski committed
351
    void clearDirichletRows();
Thomas Witkowski's avatar
Thomas Witkowski committed
352

353
    /// Prints \ref matrix to stdout
354
355
    void print() const;

356
    /// Removes all matrix entries
357
358
359
360
    void clear()
    {
	set_to_zero(matrix);
    }
361

362
    /// Test whether \ref matrix is symmetric. Exits if not.
363
364
365
366
    void test();

    bool symmetric();

Thomas Witkowski's avatar
Thomas Witkowski committed
367
    inline vector<Operator*>& getOperators() 
Thomas Witkowski's avatar
Thomas Witkowski committed
368
    { 
369
      return operators; 
370
    }
371
    
Thomas Witkowski's avatar
Thomas Witkowski committed
372
    inline vector<double*>& getOperatorFactor() 
Thomas Witkowski's avatar
Thomas Witkowski committed
373
    { 
374
      return operatorFactor; 
375
    }
376

Thomas Witkowski's avatar
Thomas Witkowski committed
377
    inline vector<double*>& getOperatorEstFactor() 
Thomas Witkowski's avatar
Thomas Witkowski committed
378
    { 
379
      return operatorEstFactor; 
380
    }
381

Thomas Witkowski's avatar
Thomas Witkowski committed
382
383
    inline BoundaryManager* getBoundaryManager() const 
    { 
384
      return boundaryManager; 
385
    }
386

Thomas Witkowski's avatar
Thomas Witkowski committed
387
388
    inline void setBoundaryManager(BoundaryManager *bm) 
    {
389
      boundaryManager = bm;
390
    }
391

Thomas Witkowski's avatar
Thomas Witkowski committed
392
393
394
395
396
397
398
    /// Calculates the average of non zero entries per row in matrix.
    void calculateNnz()
    {
      nnzPerRow = 0;

      if (num_rows(matrix) != 0)
	nnzPerRow = int(double(matrix.nnz()) / num_rows(matrix) * 1.2); 
Thomas Witkowski's avatar
Thomas Witkowski committed
399
400
      if (nnzPerRow < 20) 
	nnzPerRow = 20;
Thomas Witkowski's avatar
Thomas Witkowski committed
401
402
403
404
405
406
407
408
    }

    /// Returns \ref nnzPerRow.
    int getNnz()
    {
      return nnzPerRow;
    }

409
    /// Writes the matrix to an output stream.
Thomas Witkowski's avatar
Thomas Witkowski committed
410
    void serialize(ostream &out);
411

412
    /// Reads a matrix from an input stream.
Thomas Witkowski's avatar
Thomas Witkowski committed
413
    void deserialize(istream &in);
414

415
    ///
416
417
418
    int memsize();

  protected:
Thomas Witkowski's avatar
Thomas Witkowski committed
419
420
    /// Pointer to a FiniteElemSpace with information about corresponding row DOFs
    /// and basis functions
421
    const FiniteElemSpace *rowFeSpace;
422

Thomas Witkowski's avatar
Thomas Witkowski committed
423
424
    /// Pointer to a FiniteElemSpace with information about corresponding 
    /// column DOFs and basis functions
425
    const FiniteElemSpace *colFeSpace;
426

427
    /// Name of the DOFMatrix
Thomas Witkowski's avatar
Thomas Witkowski committed
428
    string name;
429

Thomas Witkowski's avatar
Thomas Witkowski committed
430
431
    /// Sparse matrix, type is a template parameter by 
    /// default compressed2D<double>
432
    base_matrix_type matrix;
433

Thomas Witkowski's avatar
Thomas Witkowski committed
434
435
436
    /// Pointers to all operators of the equation systems. Are used in the
    /// assembling process.
    vector<Operator*> operators;
437
    
Thomas Witkowski's avatar
Thomas Witkowski committed
438
439
440
    /// Defines for each operator a factor which is used to scal the element
    /// matrix after the assembling process of the operator.
    vector<double*> operatorFactor;
441

442
    ///
Thomas Witkowski's avatar
Thomas Witkowski committed
443
    vector<double*> operatorEstFactor;
444

445
    ///
446
447
    BoundaryManager *boundaryManager;

Thomas Witkowski's avatar
Thomas Witkowski committed
448
449
    /// If false, the matrix is a diagonal matrix within a matrix of DOF matrices.
    /// Otherwise the value is true, and the matrix is an off-diagonal matrix.
450
451
    bool coupleMatrix;

452
    /// Temporary variable used in assemble()
453
454
455
456
457
458
459
460
461
    ElementMatrix elementMatrix;

    /// Number of basis functions in the row fe space.
    int nRow;

    /// Number of basis functions in the col fe space.
    int nCol;

    /// Maps local row indices of an element to global matrix indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
462
    vector<DegreeOfFreedom> rowIndices;
463
464

    /// Maps local col indices of an element to global matrix indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
465
    vector<DegreeOfFreedom> colIndices;
Thomas Witkowski's avatar
Thomas Witkowski committed
466

467
468
469
470
    /// A set of row indices. When assembling the DOF matrix, all rows, that
    /// correspond to a dof at a dirichlet boundary, are ignored and the row is
    /// left blank. After assembling, the diagonal element of the matrix must be
    /// set to 1. The indices of all rows, where this should be done, are stored
Thomas Witkowski's avatar
Thomas Witkowski committed
471
472
    /// in this set.
    std::set<DegreeOfFreedom> dirichletDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
473

474
475
476
477
    /// Number of non zero entries per row (average). For instationary problems this
    /// information may be used in the next timestep to accelerate insertion of
    /// elemnts during assembling.
    int nnzPerRow;
Thomas Witkowski's avatar
Thomas Witkowski committed
478

479
    /// Inserter object: implemented as pointer, allocated and deallocated as needed
480
    inserter_type *inserter;
481
      
482
483
484
485
486
487
488
489
490
491
    friend class DOFAdmin;
    friend class DOFVector<double>;
    friend class DOFVector<unsigned char>;
    friend class DOFVector<int>;
    friend class DOFVector<WorldVector<double> >;
  };

}

#endif  // AMDIS_DOFMATRIX_H