PetscSolver.h 4.93 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 32 33 34 35
#include "AMDiS_fwd.h"
#include "Global.h"
#include "Parameters.h"
#include "DOFMatrix.h"
#include "parallel/MeshDistributor.h"

36 37 38
#include <petsc.h>
#include <petscsys.h>
#include <petscao.h>
39 40 41

namespace AMDiS {

42 43 44 45
  using namespace std;

  class PetscSolver
  {
46
  public:
47 48
    PetscSolver()
      : meshDistributor(NULL)
49 50
    {}

51
    virtual ~PetscSolver() {}
52

53 54 55 56 57
    void setMeshDistributor(MeshDistributor *m)
    {
      meshDistributor = m;
    }

58 59 60 61 62 63 64 65 66
    /** \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
     */
    virtual void fillPetscMatrix(Matrix<DOFMatrix*> *mat, SystemVector *vec) = 0;
67

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

71
  protected:
72
    MeshDistributor *meshDistributor;
73 74
  };

75

76 77 78
  class PetscSolverGlobalMatrix : public PetscSolver
  {
  public:
79 80
    PetscSolverGlobalMatrix()
      : PetscSolver(),
81 82 83 84 85 86 87 88 89 90 91
	d_nnz(NULL),
	o_nnz(NULL),
	lastMeshNnz(0),
	zeroStartVector(false)
    {
      GET_PARAMETER(0, "parallel->use zero start vector", "%d", &zeroStartVector);
    }

    void fillPetscMatrix(Matrix<DOFMatrix*> *mat, SystemVector *vec);

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

93
  protected:
94
    /// Creates a new non zero pattern structure for the PETSc matrix. 
95 96
    void createPetscNnzStructure(Matrix<DOFMatrix*> *mat);
    
97
    /// Takes a DOF matrix and sends the values to the global PETSc matrix.
98 99 100
    void setDofMatrix(DOFMatrix* mat, int dispMult = 1, 
		      int dispAddRow = 0, int dispAddCol = 0);

101
    /// Takes a DOF vector and sends its values to a given PETSc vector.
102 103 104 105 106 107 108 109
    void setDofVector(Vec& petscVec, DOFVector<double>* vec, 
		      int disMult = 1, int dispAdd = 0, bool rankOnly = false);

  protected:
    /// Petsc's matrix structure.
    Mat petscMatrix;

    /** \brief
110
     * PETSc's vector structures for the rhs vector, the solution vector and a
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
     * 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;

    /** \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;

    bool zeroStartVector;
127 128
  };

129

Thomas Witkowski's avatar
Thomas Witkowski committed
130
#ifdef HAVE_PETSC_DEV
131 132 133
  class PetscSolverSchur : public PetscSolver
  {
  public:
134 135
    PetscSolverSchur() 
      : PetscSolver()
136 137
    {}

138 139 140 141 142
    void fillPetscMatrix(Matrix<DOFMatrix*> *mat, SystemVector *vec);

    void solvePetscMatrix(SystemVector &vec, AdaptInfo *adaptInfo);
    
  protected:
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    void updateDofData();

    /// 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, bool rankOnly = false);

  protected:
    int nBoundaryDofs;

    int rStartBoundaryDofs;

    int nOverallBoundaryDofs;

    std::set<DegreeOfFreedom> boundaryDofs;

    map<DegreeOfFreedom, DegreeOfFreedom> mapGlobalBoundaryDof;

    int nInteriorDofs;

    int rStartInteriorDofs;

    int nOverallInteriorDofs;

    std::set<DegreeOfFreedom> interiorDofs;    

    map<DegreeOfFreedom, DegreeOfFreedom> mapGlobalInteriorDof;

    Mat matA11, matA12, matA21, matA22;

    Mat petscMatrix;

    Vec petscRhsVec1, petscRhsVec2;

    Vec petscSolVec1, petscSolVec2;
181

182
    Vec petscRhsVec, petscSolVec;
183
  };
Thomas Witkowski's avatar
Thomas Witkowski committed
184
#endif
185 186

} // namespace AMDiS
187 188

#endif