Nektar++
Loading...
Searching...
No Matches
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
44{
45// Forward declarations
46class ExpList;
47
48/// Enumerator
54
55/// A PETSc global linear system.
56class GlobalLinSysPETSc : virtual public GlobalLinSys
57{
58public:
59 /// Constructor for full direct matrix solve.
61 const GlobalLinSysKey &pKey, const std::weak_ptr<ExpList> &pExp,
62 const std::shared_ptr<AssemblyMap> &pLocToGloMap);
63
65
66protected:
67 /// PETSc matrix object.
69 /// PETSc vector objects used for local storage.
71 /// KSP object that represents solver system.
72 KSP m_ksp;
73 /// PCShell for preconditioner.
74 PC m_pc;
75 /// Enumerator to select matrix multiplication type.
77 /// Reordering that takes universal IDs to a unique row in the PETSc
78 /// matrix. @see GlobalLinSysPETSc::CalculateReordering
79 std::vector<int> m_reorderedMap;
80 /// PETSc scatter context that takes us between Nektar++ global
81 /// ordering and PETSc vector ordering.
82 VecScatter m_ctx;
83 /// Number of unique degrees of freedom on this process.
85
87
88 /**
89 * @brief Internal struct for MatShell and PCShell calls to store
90 * current context for callback.
91 *
92 * To use the MatShell/PCShell representation inside PETSc KSP and
93 * PC objects (so that we can use the local spectral element
94 * approach) requires the use of a callback function, which must be
95 * static. This is a lightweight wrapper allowing us to call a
96 * virtual function so that we can handle the static
97 * condensation/full variants of the global system.
98 *
99 * @see GlobalLinSysPETSc::DoMatrixMultiply
100 */
101 struct ShellCtx
102 {
103 /// Number of global degrees of freedom.
105 /// Number of Dirichlet degrees of freedom.
106 int nDir;
107 /// Pointer to the original calling object.
109 };
110
111 void SetUpScatter();
112 void SetUpMatVec(int nGlobal, int nDir);
113 void SetUpSolver(NekDouble tolerance);
114 void CalculateReordering(const Array<OneD, const int> &glo2uniMap,
115 const Array<OneD, const int> &glo2unique,
116 const AssemblyMapSharedPtr &pLocToGloMap);
117
118 void v_SolveLinearSystem(const int pNumRows,
119 const Array<OneD, const NekDouble> &pInput,
120 Array<OneD, NekDouble> &pOutput,
121 const AssemblyMapSharedPtr &locToGloMap,
122 const int pNumDir) override;
123
125 Array<OneD, NekDouble> &pOutput) = 0;
126
127private:
128 static std::string matMult;
129 static std::string matMultIds[];
130
131 static PetscErrorCode DoMatrixMultiply(Mat M, Vec in, Vec out);
132 static PetscErrorCode DoPreconditioner(PC pc, Vec in, Vec out);
133 static void DoNekppOperation(Vec &in, Vec &out, ShellCtx *ctx, bool precon);
134 static PetscErrorCode DoDestroyMatCtx(Mat M);
135 static PetscErrorCode DoDestroyPCCtx(PC pc);
136};
137} // namespace Nektar::MultiRegions
138
139#endif
#define MULTI_REGIONS_EXPORT
A 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() override
Clean up PETSc objects.
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.
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.
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
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition AssemblyMap.h:50
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.