PetscSolver.h 4.46 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

Thomas Witkowski's avatar
Thomas Witkowski committed
21
/** \file PetscSolver.h */
22

23
24
#ifndef AMDIS_PETSC_SOLVER_H
#define AMDIS_PETSC_SOLVER_H
25

26
27
28
29
#include <set>
#include <map>
#include <mpi.h>

30
31
#include "AMDiS_fwd.h"
#include "Global.h"
32
#include "Initfile.h"
33
34
#include "DOFMatrix.h"
#include "parallel/MeshDistributor.h"
35
#include "parallel/ParallelCoarseSpaceMatVec.h"
36
37
38
#include <petsc.h>
#include <petscsys.h>
#include <petscao.h>
39
#include <petscksp.h>
40
41
42

namespace AMDiS {

43
44
  using namespace std;

45
46
47
48
49
50
  /**
   * Create an abstract interface to an arbitrary PETSc solver. This class is
   * based on \ref ParallelCoarseSpaceMatVec to support for solvers which make
   * use of a coarse grid problem.
   */
  class PetscSolver : public ParallelCoarseSpaceMatVec
51
  {
52
  public:
53
    PetscSolver();
54

55
    virtual ~PetscSolver() {}
56

57
    /** \brief
58
59
     * Create a PETSc matrix. The given DOF matrices are used to create the nnz 
     * structure of the PETSc matrix and the values are transfered to it.
60
61
     *
     * \param[in] mat
62
63
64
65
66
67
     */
    virtual void fillPetscMatrix(Matrix<DOFMatrix*> *mat) = 0;

    /** \brief
     * Create a PETSc vector and fills it with the rhs values of the system.
     *
68
69
     * \param[in] vec
     */
70
    virtual void fillPetscRhs(SystemVector *vec) = 0;
71

72
73
    /// Use PETSc to solve the linear system of equations
    virtual void solvePetscMatrix(SystemVector &vec, AdaptInfo *adaptInfo) = 0;
74

75
76
77
78
    virtual void solve(Vec &rhs, Vec &sol);

    virtual void solveGlobal(Vec &rhs, Vec &sol);

79
80
81
    /// Destroys all matrix data structures.
    virtual void destroyMatrixData() = 0;

82
83
84
    /// Detroys all vector data structures.
    virtual void destroyVectorData() = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
85
86
87
88
89
    virtual Flag getBoundaryDofRequirement()
    {
      return 0;
    }

90
91
    KSP getSolver() 
    { 
92
      return kspInterior; 
93
    }
94

95
96
    PC getPc() 
    { 
97
      return pcInterior; 
98
    }
99

100
101
102
103
104
    void setKspPrefix(std::string s)
    {
      kspPrefix = s;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
105
    void setRemoveRhsNullspace(bool b)
106
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
107
      removeRhsNullspace = b;
108
109
    }

110
111
112
113
114
115
    /// Adds a new vector to the basis of the operator's nullspace.
    void addNullspaceVector(SystemVector *vec)
    {
      nullspace.push_back(vec);
    }

116
117
118
119
120
121
122
123
124
125
126
127
128
    /// Sets the nullspace to be constant for some specific components.
    void setConstantNullspace(vector<int> &components)
    {
      constNullspaceComponent = components;
    }

    /// Sets the nullspace to be constant for a specific component.
    void setConstantNullspace(int component)
    {
      constNullspaceComponent.clear();
      constNullspaceComponent.push_back(component);
    }

129
  protected:
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    /** \brief
     * Copies between to PETSc vectors by using different index sets for the
     * origin and the destination vectors.
     *
     * \param[in]   originVec    The PETSc vector from which we copy from.
     * \param[out]  destVec      The PETSc vector we copy too.
     * \param[in]   originIndex  Set of global indices referring to the 
     *                           origin vector.
     * \param[in]   destIndex    Set of global indices referring to the
     *                           destination vector.
     */
    void copyVec(Vec& originVec, Vec& destVec, 
		 vector<int>& originIndex, vector<int>& destIndex);

144
  protected:
145
    /// PETSc solver object
146
    KSP kspInterior;
147

148
    /// PETSc preconditioner object
149
    PC pcInterior;
150

151
152
153
    /// A set of vectors that span the null space of the operator.
    vector<SystemVector*> nullspace;

154
155
    /// KSP database prefix
    string kspPrefix;
156
157
158

    /// If true, the constant null space is projected out of the RHS vector. It
    /// depends on the specific PETSc solver if it considers this value.
Thomas Witkowski's avatar
Thomas Witkowski committed
159
160
161
    bool removeRhsNullspace;

    bool hasConstantNullspace;
162
163

    vector<int> constNullspaceComponent;
164
165
  };

166

167
} // namespace AMDiS
168
169

#endif