PetscSolver.h 6 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
#include <set>
#include <map>
#include <mpi.h>
29 30 31 32
#include <petsc.h>
#include <petscsys.h>
#include <petscao.h>
#include <petscksp.h>
33

34 35
#include "AMDiS_fwd.h"
#include "Global.h"
36
#include "Initfile.h"
37 38
#include "DOFMatrix.h"
#include "parallel/MeshDistributor.h"
39
#include "parallel/ParallelCoarseSpaceMatVec.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(string name);
54

55
    virtual ~PetscSolver() {}
56

Thomas Witkowski's avatar
bo eh  
Thomas Witkowski committed
57 58 59
    void init(vector<const FiniteElemSpace*> &componentSpaces,
	      vector<const FiniteElemSpace*> &feSpaces,
	      bool createGlobalMapping = true);
60

61
    /** \brief
62 63
     * 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.
64 65
     *
     * \param[in] mat
66 67 68
     */
    virtual void fillPetscMatrix(Matrix<DOFMatrix*> *mat) = 0;

69 70
    /// This function is just a small wrapper that creates a 1x1 matrix that
    /// contains exactly one DOFMatrix and than calls \ref fillPetscMatrix
71 72
    void fillPetscMatrix(DOFMatrix* mat);

73 74 75
    /** \brief
     * Create a PETSc vector and fills it with the rhs values of the system.
     *
76 77
     * \param[in] vec
     */
78
    virtual void fillPetscRhs(SystemVector *vec) = 0;
79

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

83 84 85 86
    virtual void solve(Vec &rhs, Vec &sol);

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

87 88 89
    /// Destroys all matrix data structures.
    virtual void destroyMatrixData() = 0;

90 91 92
    /// Detroys all vector data structures.
    virtual void destroyVectorData() = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
93 94 95 96 97
    virtual Flag getBoundaryDofRequirement()
    {
      return 0;
    }

98 99
    KSP getSolver() 
    { 
100
      return kspInterior; 
101
    }
102

103 104
    PC getPc() 
    { 
105
      return pcInterior; 
106
    }
107

108 109 110 111 112
    void setKspPrefix(std::string s)
    {
      kspPrefix = s;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
113
    void setRemoveRhsNullspace(bool b)
114
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
115
      removeRhsNullspace = b;
116 117
    }

118 119 120 121 122
    void setSymmetric(bool b)
    {
      isSymmetric = b;
    }

123 124 125 126 127 128
    /// Adds a new vector to the basis of the operator's nullspace.
    void addNullspaceVector(SystemVector *vec)
    {
      nullspace.push_back(vec);
    }

129 130 131 132 133 134 135 136 137 138 139 140 141
    /// 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);
    }

142 143 144 145 146 147
    /// Informs the solver whether is has to handle dirichlet rows or not.
    void setHandleDirichletRows(bool b)
    {
      handleDirichletRows = b;
    }

148 149 150 151 152
    ParallelDofMapping& getDofMap()
    {
      return dofMap;
    }

153 154 155 156 157
    vector<const FiniteElemSpace*>& getComponentSpaces()
    {
      return componentSpaces;
    }

158
  protected:
159 160 161 162 163 164 165 166 167 168 169 170 171 172
    /** \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);

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
173 174
    /// Run test, if matrix is symmetric.
    bool testMatrixSymmetric(Mat mat, bool advancedTest = false);
175
  protected:
176 177 178
    /// Prefix string for parameters in init file.
    string initFileStr;

179 180 181 182 183 184 185 186 187 188
    /// FE spaces of all components for the stationary problem the specific
    /// solver object is registered to.
    vector<const FiniteElemSpace*> componentSpaces;

    /// Set of unique FE spaces in \ref componentSpaces.
    vector<const FiniteElemSpace*> feSpaces;

    ///
    ParallelDofMapping dofMap, dofMapSd;

189
    /// PETSc solver object
190
    KSP kspInterior;
191

192
    /// PETSc preconditioner object
193
    PC pcInterior;
194

195 196 197
    /// A set of vectors that span the null space of the operator.
    vector<SystemVector*> nullspace;

198 199
    /// KSP database prefix
    string kspPrefix;
200 201 202

    /// 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
203 204 205
    bool removeRhsNullspace;

    bool hasConstantNullspace;
206

207 208
    bool isSymmetric;

209 210 211 212 213 214
    /// If true, dirichlet rows are handled by the solver correspondently. To 
    /// set this value to false makes only sense, of this solver is just used
    /// as a subsolver and the main solver above alread handles dirichlet rows
    /// in some way.
    bool handleDirichletRows;

215
    vector<int> constNullspaceComponent;
216 217
  };

218

219
} // namespace AMDiS
220 221

#endif