PetscSolver.h 5.08 KB
Newer Older
1
2
3
4
5
6
7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9
10
11
12
13
14
15
16
17
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
18
 *
19
20
21
22
23
24
25
26
 ******************************************************************************/


/** \file PetscSolver.h */

#ifndef AMDIS_PETSC_SOLVER_SEQ_H
#define AMDIS_PETSC_SOLVER_SEQ_H

27
#ifdef HAVE_SEQ_PETSC
28

29
#include "solver/LinearSolver.h"
30
#include "solver/PetscTypes.h"
31
#include "solver/MatrixStreams.h"
32
33
34
35
36
37
38
#include "Timer.h"
#include <vector>
#include <iostream>
#include <boost/mpl/bool.hpp>

#include <petsc.h>
#include <petscksp.h>
39
#include <petscmat.h>
40
41
42
43
44
#include <petscvec.h>
#include <petscsys.h>
#include <petscao.h>

namespace AMDiS {
45

46
47
  /**
   * \ingroup Solver
48
   *
49
50
51
   * \brief Common base class for wrappers to use Petsc preconditioners in AMDiS.
   */
  template< class MatrixType, class VectorType >
52
  struct PetscPreconditionerInterface : public PreconditionerInterface
53
  {
54
    PetscPreconditionerInterface(std::string prefix, std::string name)
55
      : prefix(prefix), name(name) {}
56

57
    virtual ~PetscPreconditionerInterface() {}
58
59

    virtual void init(PC pc, const SolverMatrix<Matrix<DOFMatrix*> >& A, const MatrixType& fullMatrix)
60
    {
61
      petsc_options_insert_string(("-" + prefix + "pc_type " + name).c_str());
62
63
      PCSetFromOptions(pc);
    }
64

65
    virtual void exit() {}
66

67
68
69
70
71
  protected:
    std::string prefix;
    std::string name;
  };

72

73
74
  /// PETSc preconditioners using MATSEQAIJ and VECSEQ data structures
  typedef PetscPreconditionerInterface<PetscMatrix, PetscVector> PetscPreconditioner;
75

76
77
  /// PETSc preconditioner using MATNEST and VECNEST data structures
  typedef PetscPreconditionerInterface<PetscMatrixNested, PetscVectorNested> PetscPreconditionerNested;
78
79


80
  /// Runner that can be passed to LinearSolver to redirect the solution to PETSc
81
  template<typename MatrixType, typename VectorType>
82
  class PetscRunner : public RunnerInterface
83
84
85
  {
  public:
    /// Constructor of standard PETSc runner. Reads ksp and pc parameters from initfile.
86
    PetscRunner(LinearSolverInterface* oem);
87

88
    /// Destructor, deletes preconditioner object \ref preconditioner
89
    ~PetscRunner()
90
91
92
93
94
95
    {
      if (preconditioner) {
	delete preconditioner;
	preconditioner = NULL;
      }
    }
96

97
98
    /// Initialize the solver \ref ksp and preconditioner \ref pc
    void init(const SolverMatrix<Matrix<DOFMatrix*> >& A, const MatrixType& fullMatrix);
99

100
101
    /// Solve the linear equation \f$ A\cdot x = b \f$ by applying the PETSc solver \ref ksp
    int solve(const MatrixType& A, VectorType& x, const VectorType& b);
102

103
    /// Destroy solver \ref ksp and preconditioner \ref pc
104
105
    virtual void exit()
    {
106
      preconditioner->exit();
107
108
109
      KSPDestroy(&ksp);
    }

110
    /// Get the PETSc solver \ref ksp
111
112
113
    KSP getSolver()
    {
      return ksp;
114
115
    }

116
    /// Get the PETSc preconditioner \ref pc
117
118
119
    PC getPc()
    {
      return pc;
120
121
    }

122
    /// Returns preconditioner object \ref preconditioner
123
    PreconditionerInterface* getLeftPrecon()
124
125
126
127
128
    {
      return preconditioner;
    }

    /// Returns preconditioner object \ref preconditioner
129
    PreconditionerInterface* getRightPrecon()
130
131
132
    {
      return preconditioner;
    }
133
134


135
    static void createSubSolver(KSP &ksp_, Mat m, std::string kspPrefix_);
136

137
    static void setSolver(KSP ksp_, std::string kspPrefix_,
138
			  KSPType kspType, PCType pcType,
139
140
141
			  PetscReal rtol = PETSC_DEFAULT,
			  PetscReal atol = PETSC_DEFAULT,
			  PetscInt maxIt = PETSC_DEFAULT);
142
143
144


  protected:
145
146
    /// Creates a preconditioner object \ref preconditioner
    void setPrecon();
147

148
  protected:
149
    LinearSolverInterface& oem;
150

151
152
153
154
155
156
157
    /// PETSc solver object
    KSP ksp;

    /// PETSc preconditioner object
    PC pc;

    /// KSP database prefix
158
    std::string kspPrefix;
159

160
161
162
    bool zeroStartVector;	///< initialize startsolution for iterative solver with 0
    bool initialized;		///< true when \ref init() was called
    bool matSolverPackage;	///< true when using a direct solver
163

164
165
    /// Preconditioner object, that sets parameters to \ref pc
    PetscPreconditionerInterface<MatrixType, VectorType>* preconditioner;
166
167
  };

168

169
170
  /**
   * \ingroup Solver
171
   *
172
173
174
175
   * \brief
   * Wrapper for the external PETSc solver:
   *   http://www.mcs.anl.gov/petsc/
   *
176
   * This is a suite of data structures and routines for the
177
178
   * scalable (parallel) solution of scientific applications.
   */
179
180
  typedef LinearSolver< PetscMatrix, PetscVector, PetscRunner<PetscMatrix, PetscVector> > PetscSolver;
  typedef LinearSolver< PetscMatrixNested, PetscVectorNested, PetscRunner<PetscMatrixNested, PetscVectorNested> > PetscSolverNested;
181

182
183
} // end namespace AMDiS

184
#endif // HAVE_SEQ_PETSC
185

186
187
#include "solver/PetscSolver.hh"

188
#endif // AMDIS_PETSC_SOLVER_SEQ_H