Nektar++
GlobalLinSysPETSc.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File GlobalLinSys.h
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: GlobalLinSysPETSc header
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 #ifndef NEKTAR_LIB_MULTIREGIONS_GLOBALLINSYSPETSC_H
35 #define NEKTAR_LIB_MULTIREGIONS_GLOBALLINSYSPETSC_H
36 
39 
40 #include <petscmat.h>
41 #include <petscksp.h>
42 
43 namespace Nektar
44 {
45  namespace MultiRegions
46  {
47  // Forward declarations
48  class ExpList;
49 
50  /// Enumerator
52  {
55  };
56 
57  /// A PETSc global linear system.
58  class GlobalLinSysPETSc : virtual public GlobalLinSys
59  {
60  public:
61  /// Constructor for full direct matrix solve.
63  const GlobalLinSysKey &pKey,
64  const std::weak_ptr<ExpList> &pExp,
65  const std::shared_ptr<AssemblyMap> &pLocToGloMap);
66 
68 
69  virtual void v_SolveLinearSystem(
70  const int pNumRows,
71  const Array<OneD,const NekDouble> &pInput,
72  Array<OneD, NekDouble> &pOutput,
73  const AssemblyMapSharedPtr &locToGloMap,
74  const int pNumDir);
75 
76  protected:
77  /// PETSc matrix object.
78  Mat m_matrix;
79  /// PETSc vector objects used for local storage.
80  Vec m_x, m_b, m_locVec;
81  /// KSP object that represents solver system.
82  KSP m_ksp;
83  /// PCShell for preconditioner.
84  PC m_pc;
85  /// Enumerator to select matrix multiplication type.
87  /// Reordering that takes universal IDs to a unique row in the PETSc
88  /// matrix. @see GlobalLinSysPETSc::CalculateReordering
89  std::vector<int> m_reorderedMap;
90  /// PETSc scatter context that takes us between Nektar++ global
91  /// ordering and PETSc vector ordering.
92  VecScatter m_ctx;
93  /// Number of unique degrees of freedom on this process.
94  int m_nLocal;
95 
97 
98  /**
99  * @brief Internal struct for MatShell and PCShell calls to store
100  * current context for callback.
101  *
102  * To use the MatShell/PCShell representation inside PETSc KSP and
103  * PC objects (so that we can use the local spectral element
104  * approach) requires the use of a callback function, which must be
105  * static. This is a lightweight wrapper allowing us to call a
106  * virtual function so that we can handle the static
107  * condensation/full variants of the global system.
108  *
109  * @see GlobalLinSysPETSc::DoMatrixMultiply
110  */
111  struct ShellCtx
112  {
113  /// Number of global degrees of freedom.
114  int nGlobal;
115  /// Number of Dirichlet degrees of freedom.
116  int nDir;
117  /// Pointer to the original calling object.
118  GlobalLinSysPETSc *linSys;
119  };
120 
121  void SetUpScatter();
122  void SetUpMatVec(int nGlobal, int nDir);
123  void SetUpSolver(NekDouble tolerance);
124  void CalculateReordering(
125  const Array<OneD, const int> &glo2uniMap,
126  const Array<OneD, const int> &glo2unique,
127  const AssemblyMapSharedPtr &pLocToGloMap);
128 
129  virtual void v_DoMatrixMultiply(
130  const Array<OneD, const NekDouble>& pInput,
131  Array<OneD, NekDouble>& pOutput) = 0;
132  private:
133  static std::string matMult;
134  static std::string matMultIds[];
135 
136  static PetscErrorCode DoMatrixMultiply(Mat M, Vec in, Vec out);
137  static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out);
138  static void DoNekppOperation(
139  Vec &in, Vec &out, ShellCtx *ctx, bool precon);
140  static PetscErrorCode DoDestroyMatCtx(Mat M);
141  static PetscErrorCode DoDestroyPCCtx (PC pc);
142  };
143  }
144 }
145 
146 #endif
VecScatter m_ctx
PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering...
GlobalLinSysPETSc * linSys
Pointer to the original calling object.
void CalculateReordering(const Array< OneD, const int > &glo2uniMap, const Array< OneD, const int > &glo2unique, const AssemblyMapSharedPtr &pLocToGloMap)
Calculate a reordering of universal IDs for PETSc.
PC m_pc
PCShell for preconditioner.
#define MULTI_REGIONS_EXPORT
void SetUpSolver(NekDouble tolerance)
Set up KSP solver object.
Internal struct for MatShell and PCShell calls to store current context for callback.
Vec m_x
PETSc vector objects used for local storage.
static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out)
Apply preconditioning using Nektar++ routines.
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:52
int nGlobal
Number of global degrees of freedom.
int m_nLocal
Number of unique degrees of freedom on this process.
KSP m_ksp
KSP object that represents solver system.
double NekDouble
virtual void v_DoMatrixMultiply(const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)=0
virtual ~GlobalLinSysPETSc()
Clean up PETSc objects.
static PetscErrorCode DoMatrixMultiply(Mat M, Vec in, Vec out)
Perform matrix multiplication using Nektar++ routines.
Describe a linear system.
A PETSc global linear system.
void SetUpScatter()
Set up PETSc local (equivalent to Nektar++ global) and global (equivalent to universal) scatter maps...
A global linear system.
Definition: GlobalLinSys.h:72
int nDir
Number of Dirichlet degrees of freedom.
virtual void v_SolveLinearSystem(const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir)
Solve linear system using PETSc.
static PetscErrorCode DoDestroyMatCtx(Mat M)
Destroy matrix shell context object.
void SetUpMatVec(int nGlobal, int nDir)
Construct PETSc matrix and vector handles.
std::shared_ptr< Preconditioner > PreconditionerSharedPtr
Definition: GlobalLinSys.h:60
static void DoNekppOperation(Vec &in, Vec &out, ShellCtx *ctx, bool precon)
Perform either matrix multiplication or preconditioning using Nektar++ routines.
std::vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
GlobalLinSysPETSc(const GlobalLinSysKey &pKey, const std::weak_ptr< ExpList > &pExp, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
static PetscErrorCode DoDestroyPCCtx(PC pc)
Destroy preconditioner context object.
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.