PetscSolverGlobalMatrix.h 4.73 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// 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.



/** \file PetscSolverGlobalMatrix.h */

#ifndef AMDIS_PETSC_SOLVER_GLOBAL_MATRIX_H
#define AMDIS_PETSC_SOLVER_GLOBAL_MATRIX_H

26
#include <boost/tuple/tuple.hpp>
Thomas Witkowski's avatar
Thomas Witkowski committed
27
28
29
30
31
32
33
#include "AMDiS_fwd.h"
#include "parallel/PetscSolver.h"

namespace AMDiS {

  using namespace std;

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
  struct NnzStructure {
    NnzStructure()
      : dnnz(NULL),
	onnz(NULL)
    {}
	
    void clear()
    {
      if (dnnz) {
	delete [] dnnz;
	dnnz = NULL;
      }

      if (onnz) {
	delete [] onnz;
	onnz = NULL;
      }
    }

    void create(int nRows0, int nRows1 = -1)
    {
      if (nRows0 == 0)
	return;

      TEST_EXIT_DBG(nRows0 > 0)("Should not happen!\n");

      dnnz = new int[nRows0];
      for (int i = 0; i < nRows0; i++)
	dnnz[i] = 0;

      if (nRows1 > 0) {
	onnz = new int[nRows1];
	for (int i = 0; i < nRows1; i++)
	  onnz[i] = 0;	
      }
    }

    int *dnnz;

    int *onnz;
  };



Thomas Witkowski's avatar
Thomas Witkowski committed
78
79
80
81
82
  class PetscSolverGlobalMatrix : public PetscSolver
  {
  public:
    PetscSolverGlobalMatrix()
      : PetscSolver(),
Thomas Witkowski's avatar
Thomas Witkowski committed
83
	petscSolVec(PETSC_NULL),
Thomas Witkowski's avatar
Thomas Witkowski committed
84
	lastMeshNnz(0),
85
	zeroStartVector(false),
86
	alwaysCreateNnzStructure(false)
Thomas Witkowski's avatar
Thomas Witkowski committed
87
    {
88
      Parameters::get("parallel->use zero start vector", zeroStartVector);
89
90
      Parameters::get("parallel->always create nnz structure", 
		      alwaysCreateNnzStructure);
Thomas Witkowski's avatar
Thomas Witkowski committed
91
92
    }

93
94
    void fillPetscMatrix(Matrix<DOFMatrix*> *mat);

95
96
97
98
    /// This function is just a small wrapper that creates a 1x1 matrix that
    /// contains exactly one DOFMatrix and than calls \ref fillPetscMatrix
    void fillPetscMatrix(DOFMatrix *mat);

99
100
    void fillPetscMatrixWithCoarseSpace(Matrix<DOFMatrix*> *mat);

101
    void fillPetscRhs(SystemVector *vec);
Thomas Witkowski's avatar
Thomas Witkowski committed
102
103
104

    void solvePetscMatrix(SystemVector &vec, AdaptInfo *adaptInfo);    

105
106
    void solveGlobal(Vec &rhs, Vec &sol);

107
    void destroyMatrixData();
108

109
110
    void destroyVectorData();

111
  protected:
112
113
    bool checkMeshChange(Matrix<DOFMatrix*> *mat, bool localMatrix = false);

114
115
    void createFieldSplit(PC pc);

116
117
118
119
    virtual void initPreconditioner(PC pc);

    virtual void exitPreconditioner(PC pc);

Thomas Witkowski's avatar
Thomas Witkowski committed
120
121
    /// Creates a new non zero pattern structure for the PETSc matrix. 
    void createPetscNnzStructure(Matrix<DOFMatrix*> *mat);
122
123

    void createPetscNnzStructureWithCoarseSpace(Matrix<DOFMatrix*> *mat,
124
125
126
						ParallelDofMapping &rowDofMap,
						ParallelDofMapping &colDofMap,
						NnzStructure &nnz,
127
						bool localMatrix = false);
Thomas Witkowski's avatar
Thomas Witkowski committed
128
129
    
    /// Takes a DOF matrix and sends the values to the global PETSc matrix.
130
    void setDofMatrix(DOFMatrix* mat, int nRowMat = 0, int nColMat = 0);
Thomas Witkowski's avatar
Thomas Witkowski committed
131
132

    /// Takes a DOF vector and sends its values to a given PETSc vector.
Thomas Witkowski's avatar
Thomas Witkowski committed
133
134
135
    void setDofVector(Vec vecInterior,
		      Vec vecCoarse,
		      DOFVector<double>* vec, 
136
		      int nRowVec, bool rankOnly = false);
Thomas Witkowski's avatar
Thomas Witkowski committed
137

138
139
140
141
    void removeDirichletBcDofs(Matrix<DOFMatrix*> *mat);

    void removeDirichletBcDofs(SystemVector *vec);

Thomas Witkowski's avatar
Thomas Witkowski committed
142
143
144
145
146
147
148
149
    inline void setDofVector(Vec vecInterior,
			     DOFVector<double>* vec, 
			     int nRowVec, bool rankOnly = false)
    {
      setDofVector(vecInterior, PETSC_NULL, vec, nRowVec, rankOnly);
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
150
  protected:
151
152
153
    NnzStructure nnzInterior;

    NnzStructure nnzCoarse;
154

155
    NnzStructure nnzIntCoarse, nnzCoarseInt;
Thomas Witkowski's avatar
Thomas Witkowski committed
156

157
158
    Vec petscSolVec;

159
160
161
162
    /// Stores the mesh change index of the mesh the nnz structure was created for.
    /// Therefore, if the mesh change index is higher than this value, we have to create
    /// a new nnz structure for PETSc matrices, because the mesh has been changed and
    /// therefore also the assembled matrix structure.
Thomas Witkowski's avatar
Thomas Witkowski committed
163
164
165
    int lastMeshNnz;

    bool zeroStartVector;
166
167
168
169
170
171
172
173

    /// If this variable is set to true, the non-zero matrix structure is
    /// created each time from scratch by calling \ref createPetscNnzStrcuture.
    /// This can be necessary if the number of non-zeros in the matrix varies
    /// though the mesh does not change. This may happen if there are many
    /// operators using DOFVectors from old timestep containing many zeros due to
    /// some phase fields.
    bool alwaysCreateNnzStructure;
Thomas Witkowski's avatar
Thomas Witkowski committed
174
175
176
177
178
179
180
  };


}

#endif