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

Thomas Witkowski's avatar
Thomas Witkowski committed
23
24
#ifndef AMDIS_PETSCSOLVER_H
#define AMDIS_PETSCSOLVER_H
25
26
27

#include "AMDiS_fwd.h"
#include "Global.h"
28
#include "MeshDistributor.h"
29
30
#include "ProblemVec.h"
#include "ProblemInstat.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
31
#include "ParallelProblemStatBase.h"
32
33
34
35
36
37

#include "petsc.h"
#include "petscsys.h"
#include "petscao.h"

namespace AMDiS {
38
  
Thomas Witkowski's avatar
Thomas Witkowski committed
39
  class PetscSolver : public ParallelProblemStatBase
40
41
  {
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
42
43
44
    PetscSolver(std::string nameStr,
		ProblemIterationInterface *problemIteration = NULL)
      : ParallelProblemStatBase(nameStr, problemIteration),
45
	d_nnz(NULL),
46
47
	o_nnz(NULL),
	lastMeshNnz(0)
48
49
    {}

Thomas Witkowski's avatar
Thomas Witkowski committed
50
    ~PetscSolver()
51
52
    {}

53
    void solve(AdaptInfo *adaptInfo, bool fixedMatrix = false);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

  protected:
    /// Creates a new non zero pattern structure for the Petsc matrix. 
    void createPetscNnzStructure(Matrix<DOFMatrix*> *mat);
    
    /** \brief
     * Create a PETSc matrix and PETSc vectors. The given DOF matrices are used to
     * create the nnz structure of the PETSc matrix and the values are transfered to it.
     * The given DOF vectors are used to the the values of the PETSc rhs vector.
     *
     * \param[in] mat
     * \param[in] vec
     */
    void fillPetscMatrix(Matrix<DOFMatrix*> *mat, SystemVector *vec);

    /// Takes a dof matrix and sends the values to the global petsc matrix.
    void setDofMatrix(DOFMatrix* mat, int dispMult = 1, 
		      int dispAddRow = 0, int dispAddCol = 0);

    /// Takes a dof vector and sends its values to a given petsc vector.
    void setDofVector(Vec& petscVec, DOFVector<double>* vec, 
		      int disMult = 1, int dispAdd = 0);

77
    /// Use PETSc to solve the linear system of equations
78
    void solvePetscMatrix(SystemVector &vec, AdaptInfo *adaptInfo);
79

80
  private:
81
82
83
84
    /// Is used to create a PETSc solver and preconditioner object. This function may
    /// be overridden to create some special PETSc solver.
    virtual void providePetscSolver(KSP &solver, PC &pc);

85
86
87
88
89
90
91
92
93
94
95
96
  protected:
    /// Petsc's matrix structure.
    Mat petscMatrix;

    /** \brief
     * Petsc's vector structures for the rhs vector, the solution vector and a
     * temporary vector for calculating the final residuum.
     */
    Vec petscRhsVec, petscSolVec, petscTmpVec;

    /// Arrays definig the non zero pattern of Petsc's matrix.
    int *d_nnz, *o_nnz;
97
98
99
100
101
102
103
104

    /** \brief
     * 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.
     */
    int lastMeshNnz;
105
106
  };

Thomas Witkowski's avatar
Thomas Witkowski committed
107
  typedef PetscSolver ParallelProblemStat;
108

109
110
111
} //namespace AMDiS

#endif