Nektar++
GlobalLinSysPETSc.h
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: GlobalLinSysPETSc.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 <petscksp.h>
41#include <petscmat.h>
42
43namespace Nektar
44{
45namespace MultiRegions
46{
47// Forward declarations
48class ExpList;
49
50/// Enumerator
52{
55};
56
57/// A PETSc global linear system.
58class GlobalLinSysPETSc : virtual public GlobalLinSys
59{
60public:
61 /// Constructor for full direct matrix solve.
63 const GlobalLinSysKey &pKey, const std::weak_ptr<ExpList> &pExp,
64 const std::shared_ptr<AssemblyMap> &pLocToGloMap);
65
67
68protected:
69 /// PETSc matrix object.
71 /// PETSc vector objects used for local storage.
73 /// KSP object that represents solver system.
74 KSP m_ksp;
75 /// PCShell for preconditioner.
76 PC m_pc;
77 /// Enumerator to select matrix multiplication type.
79 /// Reordering that takes universal IDs to a unique row in the PETSc
80 /// matrix. @see GlobalLinSysPETSc::CalculateReordering
81 std::vector<int> m_reorderedMap;
82 /// PETSc scatter context that takes us between Nektar++ global
83 /// ordering and PETSc vector ordering.
84 VecScatter m_ctx;
85 /// Number of unique degrees of freedom on this process.
87
89
90 /**
91 * @brief Internal struct for MatShell and PCShell calls to store
92 * current context for callback.
93 *
94 * To use the MatShell/PCShell representation inside PETSc KSP and
95 * PC objects (so that we can use the local spectral element
96 * approach) requires the use of a callback function, which must be
97 * static. This is a lightweight wrapper allowing us to call a
98 * virtual function so that we can handle the static
99 * condensation/full variants of the global system.
100 *
101 * @see GlobalLinSysPETSc::DoMatrixMultiply
102 */
103 struct ShellCtx
104 {
105 /// Number of global degrees of freedom.
107 /// Number of Dirichlet degrees of freedom.
108 int nDir;
109 /// Pointer to the original calling object.
111 };
112
113 void SetUpScatter();
114 void SetUpMatVec(int nGlobal, int nDir);
115 void SetUpSolver(NekDouble tolerance);
116 void CalculateReordering(const Array<OneD, const int> &glo2uniMap,
117 const Array<OneD, const int> &glo2unique,
118 const AssemblyMapSharedPtr &pLocToGloMap);
119
120 virtual void v_SolveLinearSystem(const int pNumRows,
121 const Array<OneD, const NekDouble> &pInput,
122 Array<OneD, NekDouble> &pOutput,
123 const AssemblyMapSharedPtr &locToGloMap,
124 const int pNumDir) override;
125
127 Array<OneD, NekDouble> &pOutput) = 0;
128
129private:
130 static std::string matMult;
131 static std::string matMultIds[];
132
133 static PetscErrorCode DoMatrixMultiply(Mat M, Vec in, Vec out);
134 static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out);
135 static void DoNekppOperation(Vec &in, Vec &out, ShellCtx *ctx, bool precon);
136 static PetscErrorCode DoDestroyMatCtx(Mat M);
137 static PetscErrorCode DoDestroyPCCtx(PC pc);
138};
139} // namespace MultiRegions
140} // namespace Nektar
141
142#endif
#define MULTI_REGIONS_EXPORT
A global linear system.
Definition: GlobalLinSys.h:72
A PETSc global linear system.
Vec m_x
PETSc vector objects used for local storage.
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.
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.
void SetUpScatter()
Set up PETSc local (equivalent to Nektar++ global) and global (equivalent to universal) scatter maps.
virtual void v_DoMatrixMultiply(const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)=0
static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out)
Apply preconditioning using Nektar++ routines.
void SetUpSolver(NekDouble tolerance)
Set up KSP solver object.
GlobalLinSysPETSc(const GlobalLinSysKey &pKey, const std::weak_ptr< ExpList > &pExp, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
static PetscErrorCode DoMatrixMultiply(Mat M, Vec in, Vec out)
Perform matrix multiplication using Nektar++ routines.
void SetUpMatVec(int nGlobal, int nDir)
Construct PETSc matrix and vector handles.
static PetscErrorCode DoDestroyMatCtx(Mat M)
Destroy matrix shell context object.
VecScatter m_ctx
PETSc scatter context that takes us between Nektar++ global ordering and PETSc vector ordering.
virtual ~GlobalLinSysPETSc()
Clean up PETSc objects.
PC m_pc
PCShell for preconditioner.
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.
virtual void v_SolveLinearSystem(const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir) override
Solve linear system using PETSc.
KSP m_ksp
KSP object that represents solver system.
static PetscErrorCode DoDestroyPCCtx(PC pc)
Destroy preconditioner context object.
int m_nLocal
Number of unique degrees of freedom on this process.
std::shared_ptr< Preconditioner > PreconditionerSharedPtr
Definition: GlobalLinSys.h:60
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:52
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
Internal struct for MatShell and PCShell calls to store current context for callback.
int nDir
Number of Dirichlet degrees of freedom.
int nGlobal
Number of global degrees of freedom.
GlobalLinSysPETSc * linSys
Pointer to the original calling object.