Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: GlobalLinSysPETSc header
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 #ifndef NEKTAR_LIB_MULTIREGIONS_GLOBALLINSYSPETSC_H
36 #define NEKTAR_LIB_MULTIREGIONS_GLOBALLINSYSPETSC_H
37 
40 
41 #include <petscmat.h>
42 #include <petscksp.h>
43 
44 namespace Nektar
45 {
46  namespace MultiRegions
47  {
48  // Forward declarations
49  class ExpList;
50 
51  /// Enumerator
53  {
56  };
57 
58  /// A PETSc global linear system.
59  class GlobalLinSysPETSc : virtual public GlobalLinSys
60  {
61  public:
62  /// Constructor for full direct matrix solve.
64  const GlobalLinSysKey &pKey,
65  const boost::weak_ptr<ExpList> &pExp,
66  const boost::shared_ptr<AssemblyMap> &pLocToGloMap);
67 
69 
70  virtual void v_SolveLinearSystem(
71  const int pNumRows,
72  const Array<OneD,const NekDouble> &pInput,
73  Array<OneD, NekDouble> &pOutput,
74  const AssemblyMapSharedPtr &locToGloMap,
75  const int pNumDir);
76 
77  protected:
78  /// PETSc matrix object.
79  Mat m_matrix;
80  /// PETSc vector objects used for local storage.
81  Vec m_x, m_b, m_locVec;
82  /// KSP object that represents solver system.
83  KSP m_ksp;
84  /// PCShell for preconditioner.
85  PC m_pc;
86  /// Enumerator to select matrix multiplication type.
88  /// Reordering that takes universal IDs to a unique row in the PETSc
89  /// matrix. @see GlobalLinSysPETSc::CalculateReordering
90  std::vector<int> m_reorderedMap;
91  /// PETSc scatter context that takes us between Nektar++ global
92  /// ordering and PETSc vector ordering.
93  VecScatter m_ctx;
94  /// Number of unique degrees of freedom on this process.
95  int m_nLocal;
96 
98 
99  /**
100  * @brief Internal struct for MatShell and PCShell calls to store
101  * current context for callback.
102  *
103  * To use the MatShell/PCShell representation inside PETSc KSP and
104  * PC objects (so that we can use the local spectral element
105  * approach) requires the use of a callback function, which must be
106  * static. This is a lightweight wrapper allowing us to call a
107  * virtual function so that we can handle the static
108  * condensation/full variants of the global system.
109  *
110  * @see GlobalLinSysPETSc::DoMatrixMultiply
111  */
112  struct ShellCtx
113  {
114  /// Number of global degrees of freedom.
115  int nGlobal;
116  /// Number of Dirichlet degrees of freedom.
117  int nDir;
118  /// Pointer to the original calling object.
119  GlobalLinSysPETSc *linSys;
120  };
121 
122  void SetUpScatter();
123  void SetUpMatVec(int nGlobal, int nDir);
124  void SetUpSolver(NekDouble tolerance);
125  void CalculateReordering(
126  const Array<OneD, const int> &glo2uniMap,
127  const Array<OneD, const int> &glo2unique,
128  const AssemblyMapSharedPtr &pLocToGloMap);
129 
130  virtual void v_DoMatrixMultiply(
131  const Array<OneD, const NekDouble>& pInput,
132  Array<OneD, NekDouble>& pOutput) = 0;
133  private:
134  static std::string matMult;
135  static std::string matMultIds[];
136 
137  static PetscErrorCode DoMatrixMultiply(Mat M, Vec in, Vec out);
138  static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out);
139  static void DoNekppOperation(
140  Vec &in, Vec &out, ShellCtx *ctx, bool precon);
141  static PetscErrorCode DoDestroyMatCtx(Mat M);
142  static PetscErrorCode DoDestroyPCCtx (PC pc);
143  };
144  }
145 }
146 
147 #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.
boost::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:53
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
boost::shared_ptr< Preconditioner > PreconditionerSharedPtr
Definition: GlobalLinSys.h:62
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.
int nGlobal
Number of global degrees of freedom.
GlobalLinSysPETSc(const GlobalLinSysKey &pKey, const boost::weak_ptr< ExpList > &pExp, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
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:74
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.
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.
static PetscErrorCode DoDestroyPCCtx(PC pc)
Destroy preconditioner context object.
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.