MatrixStreams.h 9.71 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
/******************************************************************************
 *
 * 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 MatrixStreams.h */

#ifndef AMDIS_MATRIXSTREAMS_H
#define AMDIS_MATRIXSTREAMS_H

#include "DOFMatrix.h"
#include "solver/SolverMatrix.h"
#include "SystemVector.h"
#include "Collection.h"

#ifdef HAVE_PETSC
#include "solver/PetscTypes.h"
#include <petsc.h>
#include <petscmat.h> 
#include <petscvec.h>
#endif

using namespace mtl::operations;
namespace AMDiS {
  
  template< typename VecT, typename CurMap>
  struct VecMap {
    VecT& vec;
    CurMap& mapper;
    VecMap(VecT& vec, CurMap& mapper):
      vec(vec),mapper(mapper) {}
  };

  template< typename MatT, typename CurMap>
  struct MatMap {
    MatT& mat;
    CurMap& mapper;
    MatMap(MatT& mat, CurMap& m):
      mat(mat), mapper(m) {}
  };

  template< typename MTLMatrix, typename Mapper >
  void operator<<(MTLMatrix& matrix, MatMap<const SolverMatrix<Matrix<DOFMatrix* > >, Mapper >& Asolver)
  {
    const Matrix< DOFMatrix* >& A = *(Asolver.mat.getOriginalMat());
    int ns = A.getSize();

    Mapper& mapper(Asolver.mapper);
    set_to_zero(matrix);

    int nnz = 0;
    for (int rb = 0; rb < ns; ++rb)
      for (int cb = 0; cb < ns; ++cb)
        if (A[rb][cb])
	  nnz += A[rb][cb]->getBaseMatrix().nnz();	  
      
    {
      typedef mtl::matrix::mapped_inserter< typename Collection< MTLMatrix >::Inserter, Mapper > Inserter;
      Inserter ins(matrix, mapper, int(1.2 * nnz / matrix.dim1()));
      for (int rb = 0; rb < ns; ++rb) {
        mapper.setRow(rb);
        for (int cb = 0; cb < ns; ++cb) {
	  mapper.setCol(cb);
          if (A[rb][cb])
	    ins << A[rb][cb]->getBaseMatrix();
        }
      }
    }
  }

  template< typename Vector, typename CurMap >
  void operator<<(Vector& dest, VecMap<DOFVector<double >, CurMap >& rhs)
  {
    DOFVector<double>::Iterator it_x(&rhs.vec, USED_DOFS);
    int counter(0);
    typedef typename Collection< Vector >::Inserter Inserter ;

    Inserter swapIns(dest);
    mtl::vector::mapped_inserter< Inserter, CurMap > ins(swapIns, rhs.mapper);

    for (it_x.reset(); !it_x.end(); ++it_x) {
      ins[counter] << *it_x;
      counter++;
    }
  }

  template< typename Vector, typename CurMap >
  inline void operator>>(const Vector& dest, VecMap<DOFVector< double >, CurMap >& rhs)
  {
    DOFVector<double>::Iterator it_x(&rhs.vec, USED_DOFS);
    int counter(0);
    {
      mtl::vector::extracter< Vector > extracter(dest);
      for (it_x.reset(); !it_x.end(); ++it_x) {
	extracter[rhs.mapper.row(counter)] >> *it_x ;
	counter++;
      }
    }
  }

  template< typename Vector, typename CurMap >
  void operator<<(Vector& dest, VecMap<SystemVector, CurMap>& rhs)
  {
    int ns = rhs.vec.getSize();  // Number of systems.

    // Copy vectors
    typedef typename Collection< Vector >::Inserter Inserter;
    Inserter swapInserter(dest);
    mtl::vector::mapped_inserter< Inserter, CurMap > ins(swapInserter, rhs.mapper);
    for (int i = 0; i < ns; i++) {
      DOFVector<double>::Iterator it_source(rhs.vec.getDOFVector(i), USED_DOFS);
      int counter(0);
      rhs.mapper.setRow(i);
      for (it_source.reset(); !it_source.end(); ++it_source) {
        ins[counter] << *it_source;
        counter++;
      }
    }
  }

  template< typename Vector, typename CurMap >
  void operator>>(const Vector& dest, VecMap<SystemVector, CurMap>& rhs) 
  {
    int ns = rhs.vec.getSize();  // Number of systems.

    // Copy vectors
    for (int i = 0; i < ns; i++) {
      DOFVector< double >& dofvec(*(rhs.vec.getDOFVector(i)));
      rhs.mapper.setRow(i);
      VecMap< DOFVector< double >, CurMap > swap(dofvec, rhs.mapper);
      dest >> swap;
    }
  }
  
#ifdef HAVE_PETSC
  
  /// fill PETSc matrix from DOFMatrix
  inline void operator<<(Mat& mat, const DOFMatrix& rhs)
  {
    bool initMatrix = false;
    if (mat == PETSC_NULL) {
      std::vector<PetscInt> nnz(rhs.getSize());
159
      for (size_t k = 0; k < static_cast<size_t>(rhs.getSize()); k++)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	nnz[k] = rhs.getBaseMatrix().nnz_local(k);	  
      
      MatCreateSeqAIJ(PETSC_COMM_SELF, rhs.getSize(), rhs.getSize(), 0, &(nnz[0]), &mat);
      initMatrix = true;
    }
    
    std::vector<PetscInt> indices;
    for (size_t i = 0; i < rhs.getBaseMatrix().ref_minor().size(); i++)
      indices.push_back(rhs.getBaseMatrix().ref_minor()[i]);

    for (PetscInt i = 0; i < static_cast<PetscInt>(num_rows(rhs.getBaseMatrix())); i++) {
      MatSetValues(mat, 1, &i, rhs.getBaseMatrix().nnz_local(i), 
		   &(indices[rhs.getBaseMatrix().ref_major()[i]]), 
		   &(rhs.getBaseMatrix().value_from_offset(rhs.getBaseMatrix().ref_major()[i])), ADD_VALUES);	
    }
    
    if (initMatrix) {      
	MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);
	MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);
    }
  }
    

  /// create and fill \ref PetscMatrix
  inline void operator<<(PetscMatrix& mat, const SolverMatrix<Matrix<DOFMatrix*> >& Asolver)
  {
    const Matrix< DOFMatrix* >& A = *(Asolver.getOriginalMat());
    
    std::vector<PetscInt> nnz;      
    mat.nestMat.resize(A.getNumRows() * A.getNumCols());
    
191
192
    for (size_t i = 0; i < static_cast<size_t>(A.getNumRows()); i++) {
      for (size_t j = 0; j < static_cast<size_t>(A.getNumCols()); j++) {
193
194
195
196
197
198
199
200
201
202
203
204
205
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
233
234
235
236
237
238
239
240
241
242
243
244
245
	size_t idx = i * A.getNumCols() + j;
	
	if (A[i][j] == NULL 
	    || num_rows(A[i][j]->getBaseMatrix()) == 0 
	    || num_cols(A[i][j]->getBaseMatrix()) == 0
	    || A[i][j]->getBaseMatrix().nnz() == 0) {
	  mat.nestMat[idx] = PETSC_NULL;
	  continue;
	}
	
	mat.nestMat[idx] << *(A[i][j]);
      }
    }
      
    // create nested matrix from a vector of block matrices
    MatCreateNest(PETSC_COMM_SELF, A.getNumRows(), PETSC_NULL, A.getNumCols(), PETSC_NULL, &(mat.nestMat[0]), &mat.matrix);
  }
  
  
  /// fill PETSc vector from DOFVector
  inline void operator<<(Vec& petscVec, /* const */DOFVector<double>& vec)
  {
    // Traverse all used DOFs in the dof vector.
    DOFVector<double>::Iterator dofIt(&vec, USED_DOFS);
    PetscInt index = 0;
    for (dofIt.reset(); !dofIt.end(); ++dofIt, ++index) {
      double value = *dofIt;
      VecSetValue(petscVec, index, value, ADD_VALUES);      
    }
  }
    
    
  /// DOFVector from PETSc vector
  inline void operator>>(const Vec& petscVec, DOFVector<double>& vec)
  {
    // Traverse all used DOFs in the dof vector.
    DOFVector<double>::Iterator dofIt(&vec, USED_DOFS);
    PetscInt index = 0;
    for (dofIt.reset(); !dofIt.end(); ++dofIt, ++index) {
      double value = 0.0;
      VecGetValues(petscVec, 1, &index, &value);
      *dofIt = value;
    }
  }
  
  
  /// fill PETSc vector from DOFVector
  inline void operator<<(Vec& petscVec, /* const */SystemVector& vec)
  {
    VecType vecType; // TODO: type changed from petsc 3.2 to petsc 3.3
    VecGetType(petscVec, &vecType);
    
    if (strcmp(vecType, VECNEST) == 0) {
246
      for (size_t i = 0; i < static_cast<size_t>(vec.getSize()); i++) {  
247
248
249
250
251
252
	Vec v;
	VecNestGetSubVec(petscVec, i, &v);
	v << *(vec.getDOFVector(i));
      }
    } else {
      PetscInt index = 0;
253
      for (size_t i = 0; i < static_cast<size_t>(vec.getSize()); i++) {
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	DOFVector<double>::Iterator dofIt(vec.getDOFVector(i), USED_DOFS);
	for (dofIt.reset(); !dofIt.end(); ++dofIt, ++index) {
	  double value = *dofIt;
	  VecSetValue(petscVec, index, value, ADD_VALUES);      
	}
      }
    }
  }
    
    
  /// fill SystemVector from PETSc vector
  inline void operator>>(const Vec& petscVec, SystemVector& vec)
  {
    VecType vecType;
    VecGetType(petscVec, &vecType);
    
    if (strcmp(vecType, VECNEST) == 0) {
271
      for (size_t i = 0; i < static_cast<size_t>(vec.getSize()); i++) {  
272
273
274
275
276
	Vec v;
	VecNestGetSubVec(petscVec, i, &v);
	v >> *(vec.getDOFVector(i));
      }
    } else {
277
278
      PetscInt n = 0;
      for (size_t i = 0; i < static_cast<size_t>(vec.getSize()); i++)
279
280
281
282
283
284
285
	n += vec.getDOFVector(i)->getUsedSize();
      
      PetscInt N = 0;
      VecGetSize(petscVec, &N);      
      assert(n == N);
      
      PetscInt index = 0;
286
      for (size_t i = 0; i < static_cast<size_t>(vec.getSize()); i++) {
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
	DOFVector<double>::Iterator dofIt(vec.getDOFVector(i), USED_DOFS);
	for (dofIt.reset(); !dofIt.end(); ++dofIt, ++index) {
	  double value = 0.0;
	  VecGetValues(petscVec, 1, &index, &value);
	  *dofIt = value;
	}
      }
    }
  }
  

  /// create and fill \ref PetscVector
  inline void operator<<(PetscVector& petscVec, /* const */SystemVector& rhs)
  {
    size_t nComponents = rhs.getSize();
    
    // only create nested vector if requested, i.e. flag createNestedVector is set to true
    if (petscVec.createNestedVector) {
      petscVec.nestVec.resize(nComponents);

      for (size_t i = 0; i < nComponents; i++) {
	VecCreateSeq(PETSC_COMM_SELF,rhs.getDOFVector(i)->getUsedSize(), &(petscVec.nestVec[i]));

	petscVec.nestVec[i] << *(rhs.getDOFVector(i));
	
	VecAssemblyBegin(petscVec.nestVec[i]);
	VecAssemblyEnd(petscVec.nestVec[i]);
      }

      // create nested vector from vector of block vectors
      VecCreateNest(PETSC_COMM_SELF, nComponents, PETSC_NULL, &(petscVec.nestVec[0]), &(petscVec.vector));
    } else {
319
      PetscInt n = 0;
320
321
322
323
324
325
326
327
328
329
330
      for (size_t i = 0; i < nComponents; i++)
	n += rhs.getDOFVector(i)->getUsedSize();
      
      VecCreateSeq(PETSC_COMM_SELF, n, &(petscVec.vector));
      petscVec.vector << rhs;
    }
  }
  
#endif
}
#endif // AMDIS_MATRIXSTREAMS_H