PetscSolver.h 5.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * 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.
 * 
 ******************************************************************************/


/** \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 39 40 41 42 43 44
#include "Timer.h"
#include <vector>
#include <iostream>
#include <boost/mpl/bool.hpp>

#include <petsc.h>
#include <petscksp.h>
#include <petscmat.h> 
#include <petscvec.h>
#include <petscsys.h>
#include <petscao.h>

namespace AMDiS {
45 46 47 48 49 50 51
    
  /**
   * \ingroup Solver
   * 
   * \brief Common base class for wrappers to use Petsc preconditioners in AMDiS.
   */
  template< class MatrixType, class VectorType >
52
  struct PetscPreconditionerInterface : public PreconditionerInterface
53
  {
54 55 56 57
    PetscPreconditionerInterface(std::string prefix, std::string name) 
      : prefix(prefix), name(name) {}
      
    virtual ~PetscPreconditionerInterface() {}
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    
    virtual void init(PC pc, const SolverMatrix<Matrix<DOFMatrix*> >& A, const MatrixType& fullMatrix) 
    {
      PetscOptionsInsertString(("-" + prefix + "pc_type " + name).c_str());
      PCSetFromOptions(pc);
    }
    
    virtual void exit() {}
    
  protected:
    std::string prefix;
    std::string name;
  };

  
73 74 75 76 77
  /// PETSc preconditioners using MATSEQAIJ and VECSEQ data structures
  typedef PetscPreconditionerInterface<PetscMatrix, PetscVector> PetscPreconditioner;
  
  /// 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 89 90 91 92 93 94 95
    
    /// Destructor, deletes preconditioner object \ref preconditioner
    ~PetscRunner() 
    {
      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 114 115
    KSP getSolver() 
    { 
      return ksp; 
    }

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

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 133
    {
      return preconditioner;
    }
    
134 135 136 137 138 139 140 141
    
    static void createSubSolver(KSP &ksp_, Mat m, std::string kspPrefix_);
    
    static void setSolver(KSP ksp_, std::string kspPrefix_,
			  KSPType kspType, PCType pcType, 
			  PetscReal rtol = PETSC_DEFAULT,
			  PetscReal atol = PETSC_DEFAULT,
			  PetscInt maxIt = PETSC_DEFAULT);
142 143 144 145 146 147 148
			  
    
  protected:    
    /// Creates a preconditioner object \ref preconditioner
    void setPrecon();
    
  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 163
    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
    
164 165
    /// Preconditioner object, that sets parameters to \ref pc
    PetscPreconditionerInterface<MatrixType, VectorType>* preconditioner;
166 167 168 169 170 171 172 173 174 175 176 177 178
  };

  
  /**
   * \ingroup Solver
   * 
   * \brief
   * Wrapper for the external PETSc solver:
   *   http://www.mcs.anl.gov/petsc/
   *
   * This is a suite of data structures and routines for the 
   * 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