PetscSolver.h 4.12 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
35
#include "DOFMatrix.h"
#include "parallel/MeshDistributor.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
  class PetscSolver
  {
48
  public:
49
    PetscSolver();
50

51
    virtual ~PetscSolver() {}
52

53
54
55
    void setMeshDistributor(MeshDistributor *m)
    {
      meshDistributor = m;
56
      mpiRank = meshDistributor->getMpiRank();
57
      mpiComm = &(meshDistributor->getMpiComm());
58
59
    }

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
81
82
83
84
85
    virtual Flag getBoundaryDofRequirement()
    {
      return 0;
    }

86
87
88
89
    KSP getSolver() 
    { 
      return solver; 
    }
90

91
92
93
94
    PC getPc() 
    { 
      return pc; 
    }
95

96
97
98
99
100
    void setKspPrefix(std::string s)
    {
      kspPrefix = s;
    }

101
102
103
104
105
  protected:
    void printSolutionInfo(AdaptInfo* adaptInfo,
			   bool iterationCounter = true,
			   bool residual = true);

106
107
108
109
110
111
112
113
114
115
116
117
118
119
    /** \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);

120
121
122
123
124
125
126
127
128
129
    /// Checks if all given FE spaces are handled by the mesh distributor.
    void checkFeSpaces(vector<const FiniteElemSpace*>& feSpaces);

    /// Returns a vector with the FE spaces of each row in the matrix. Thus, the
    /// resulting vector may contain the same FE space multiple times.
    vector<const FiniteElemSpace*> getFeSpaces(Matrix<DOFMatrix*> *mat);

    /// Returns a vector with the FE spaces of all components of a system vector.
    vector<const FiniteElemSpace*> getFeSpaces(SystemVector *vec);

130
  protected:
131
    MeshDistributor *meshDistributor;
132
133
    
    int mpiRank;
134

135
136
    MPI::Intracomm* mpiComm;

137
138
139
    /// Petsc's matrix structure.
    Mat petscMatrix;

140
141
    /// PETSc's vector structures for the rhs vector, the solution vector and a
    /// temporary vector for calculating the final residuum.
142
143
    Vec petscRhsVec, petscSolVec, petscTmpVec;

144
    /// PETSc solver object
145
146
    KSP solver;

147
    /// PETSc preconditioner object
148
    PC pc;
149
150
151

    /// KSP database prefix
    string kspPrefix;
152
153
  };

154

155
} // namespace AMDiS
156
157

#endif