BlockMTLMatrix.h 4.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
 

/** \file BlockMTLMatrix.h */

#ifndef AMDIS_BLOCK_MTL_MATRIX_H
#define AMDIS_BLOCK_MTL_MATRIX_H

#include "solver/SolverMatrix.h"
#include "solver/Mapper.h"
#include "MTL4Types.h"

31
namespace AMDiS {
32
33
34
35
  
  /// A wrapper for AMDiS::SolverMatrix to be used in MTL/ITL solvers
  struct BlockMTLMatrix
  {
36
    typedef mtl::Collection<MTLTypes::MTLMatrix>::size_type size_type;
37
38
39
40
41
42
43
44
    
    size_type n; // overall number of rows
    size_type m; // overall number of columns
    
    size_t n_rows; // number of row blocks
    size_t n_cols; // number of column blocks
    
    BlockMTLMatrix()
45
      : initialized(false), A(NULL) 
46
47
48
    { }
    
    BlockMTLMatrix(const SolverMatrix<Matrix<DOFMatrix*> >& A_)
49
      : initialized(false), A(&A_)
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    {
      init();
    }
    
    void setMatrix(const SolverMatrix<Matrix<DOFMatrix*> >& A_) 
    {
      A = &A_;
      init();
    }
    
    void init()
    {
      RectangularMapper mapper(*A);
      n = mapper.getNumRows();
      m = mapper.getNumCols();
      n_rows = mapper.getNumRowComponents();
      n_cols = mapper.getNumColComponents();
      
      r_rows.resize(n_rows);
      r_cols.resize(n_cols);
      int start = 0;
      mapper.setCol(0);
      for (size_t i = 0; i < n_rows; i++) {
	mapper.setRow(i+1);
	int finish = mapper.row(0);
	r_rows[i].set(start, finish);
	start = finish;
      }
      start = 0;
      mapper.setRow(0);
      for (size_t i = 0; i < n_cols; i++) {
	mapper.setCol(i+1);
	int finish = mapper.col(0);
	r_cols[i].set(start, finish);
	start = finish;
      }
      initialized = true;
    }
    
    inline const mtl::irange& getRowRange(size_t i) const
    {
      return r_rows[i];
    } 
    
    inline const mtl::irange& getColRange(size_t i) const
    {
      return r_cols[i];
    } 
    
    const DOFMatrix::base_matrix_type& getSubMatrix(size_t i, size_t j) const 
    {
      return A->getSubMatrix(i, j);
    }

    /// perform blockwise multiplication A*b -> x
    template <typename VectorIn, typename VectorOut, typename Assign>
    void mult(const VectorIn& b, VectorOut& x, Assign) const
    {
      TEST_EXIT_DBG(initialized)("Block matrix not initialized. Assign a SolverMatrix first!\n");
      
      for (size_t i = 0; i < n_rows; i++) {
	VectorOut x_i(x[r_rows[i]]);
	bool first = true;
	for (size_t j = 0; j < n_cols; j++) {
	  if ((*A->getOriginalMat())[i][j]) {
	    const VectorIn b_j(b[r_cols[j]]);
	    if (first) {
	      Assign::first_update(x_i, A->getSubMatrix(i, j) * b_j);
	      first = false;
	    } else {
	      Assign::update(x_i, A->getSubMatrix(i, j) * b_j);
	    }
	  }
	}
      }
    }

    template <typename VectorIn>
    mtl::vector::mat_cvec_multiplier<BlockMTLMatrix, VectorIn> operator*(const VectorIn& v) const
    {   return mtl::vector::mat_cvec_multiplier<BlockMTLMatrix, VectorIn>(*this, v);    }

  protected:
    bool initialized;
    
  private:
    const SolverMatrix<Matrix<DOFMatrix*> >* A;    
    std::vector<mtl::irange> r_rows, r_cols;
  };
  
  namespace dispatch
  {
    template< typename M >
    void initMatrix(BlockMTLMatrix& m, MapperBase<M>& mapper)
    { }
    
    template< typename M >
    void fillMatrix(BlockMTLMatrix& m, const SolverMatrix<Matrix<DOFMatrix*> >& source, MapperBase<M>& mapper)
    {
      m.setMatrix(source);
    }
    
  } // end namespace dispatch

} // end namespace AMDiS


inline AMDiS::BlockMTLMatrix::size_type size(const AMDiS::BlockMTLMatrix& A) { return (A.n) * (A.m); }
inline AMDiS::BlockMTLMatrix::size_type num_rows(const AMDiS::BlockMTLMatrix& A) { return A.n; }
inline AMDiS::BlockMTLMatrix::size_type num_cols(const AMDiS::BlockMTLMatrix& A) { return A.m; }

namespace mtl 
{
  template <>
  struct Collection<AMDiS::BlockMTLMatrix>
  {
    typedef double value_type;
166
    typedef AMDiS::BlockMTLMatrix::size_type size_type;
167
168
169
170
171
172
173
174
175
176
177
178
  };

  namespace ashape 
  {
    template <> struct ashape_aux<AMDiS::BlockMTLMatrix> 
    { 
      typedef nonscal type;
    };
  } // end namespace ashape
  
} // end namespace mtl

179
#endif // AMDIS_BLOCK_MTL_MATRIX_H