Nektar++
GlobalLinSysPETScFull.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: GlobalLinSysPETScFull.cpp
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: GlobalLinSysPETScFull definition
32//
33///////////////////////////////////////////////////////////////////////////////
34
37
38#include "petscao.h"
39#include "petscis.h"
40
41using namespace std;
42
44{
45/**
46 * @class GlobalLinSysPETScFull
47 */
48
49/**
50 * Registers the class with the Factory.
51 */
54 "PETScFull", GlobalLinSysPETScFull::create, "PETSc Full Matrix.");
55
56/// Constructor for full direct matrix solve.
58 const GlobalLinSysKey &pLinSysKey, const std::weak_ptr<ExpList> &pExp,
59 const std::shared_ptr<AssemblyMap> &pLocToGloMap)
60 : GlobalLinSys(pLinSysKey, pExp, pLocToGloMap),
61 GlobalLinSysPETSc(pLinSysKey, pExp, pLocToGloMap)
62{
63 const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
64
65 int i, j, n, cnt, gid1, gid2, loc_lda;
66 NekDouble sign1, sign2, value;
68
69 // CALCULATE REORDERING MAPPING
70 CalculateReordering(pLocToGloMap->GetGlobalToUniversalMap(),
71 pLocToGloMap->GetGlobalToUniversalMapUnique(),
72 pLocToGloMap);
73
74 // SET UP VECTORS AND MATRIX
75 SetUpMatVec(pLocToGloMap->GetNumGlobalCoeffs(), nDirDofs);
76
77 // SET UP SCATTER OBJECTS
79
80 // CONSTRUCT KSP OBJECT
81 SetUpSolver(pLocToGloMap->GetIterativeTolerance());
82
83 // POPULATE MATRIX
84 for (n = cnt = 0; n < m_expList.lock()->GetNumElmts(); ++n)
85 {
86 loc_mat = GetBlock(n);
87 loc_lda = loc_mat->GetRows();
88
89 for (i = 0; i < loc_lda; ++i)
90 {
91 gid1 = pLocToGloMap->GetLocalToGlobalMap(cnt + i) - nDirDofs;
92 sign1 = pLocToGloMap->GetLocalToGlobalSign(cnt + i);
93 if (gid1 >= 0)
94 {
95 int gid1ro = m_reorderedMap[gid1];
96 for (j = 0; j < loc_lda; ++j)
97 {
98 gid2 =
99 pLocToGloMap->GetLocalToGlobalMap(cnt + j) - nDirDofs;
100 sign2 = pLocToGloMap->GetLocalToGlobalSign(cnt + j);
101 if (gid2 >= 0)
102 {
103 int gid2ro = m_reorderedMap[gid2];
104 value = sign1 * sign2 * (*loc_mat)(i, j);
105 MatSetValue(m_matrix, gid1ro, gid2ro, value,
106 ADD_VALUES);
107 }
108 }
109 }
110 }
111 cnt += loc_lda;
112 }
113
114 // ASSEMBLE MATRIX
115 MatAssemblyBegin(m_matrix, MAT_FINAL_ASSEMBLY);
116 MatAssemblyEnd(m_matrix, MAT_FINAL_ASSEMBLY);
117}
118
119/**
120 * Solve the linear system using a full global matrix system.
121 */
123 const Array<OneD, const NekDouble> &pLocInput,
124 Array<OneD, NekDouble> &pLocOutput,
125 const AssemblyMapSharedPtr &pLocToGloMap,
126 const Array<OneD, const NekDouble> &pDirForcing)
127{
128 m_locToGloMap = pLocToGloMap;
129
130 bool dirForcCalculated = (bool)pDirForcing.size();
131 int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
132 int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
133 int nLocDofs = pLocToGloMap->GetNumLocalCoeffs();
134
135 int nDirTotal = nDirDofs;
136 std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
137 expList->GetComm()->GetRowComm()->AllReduce(nDirTotal,
139
140 if (nDirTotal)
141 {
142 Array<OneD, NekDouble> rhs(nLocDofs);
143
144 // Calculate the Dirichlet forcing
145 if (dirForcCalculated)
146 {
147 // Assume pDirForcing is in local space
148 ASSERTL0(
149 pDirForcing.size() >= nLocDofs,
150 "DirForcing is not of sufficient size. Is it in local space?");
151 Vmath::Vsub(nLocDofs, pLocInput, 1, pDirForcing, 1, rhs, 1);
152 }
153 else
154 {
155 // Calculate initial condition and Dirichlet forcing and subtract it
156 // from the rhs
157 expList->GeneralMatrixOp(m_linSysKey, pLocOutput, rhs);
158
159 // Iterate over all the elements computing Robin BCs where
160 // necessary
161 for (auto &r : m_robinBCInfo) // add robin mass matrix
162 {
165
166 int n = r.first;
167 int offset = expList->GetCoeff_Offset(n);
168
169 LocalRegions::ExpansionSharedPtr vExp = expList->GetExp(n);
170 // Add local matrix contribution
171 for (rBC = r.second; rBC; rBC = rBC->next)
172 {
173 vExp->AddRobinTraceContribution(
174 rBC->m_robinID, rBC->m_robinPrimitiveCoeffs,
175 pLocOutput + offset, rhsloc = rhs + offset);
176 }
177 }
178 Vmath::Vsub(nLocDofs, pLocInput, 1, rhs, 1, rhs, 1);
179 }
180
181 Array<OneD, NekDouble> diff(nLocDofs);
182
183 // Solve for perturbation from initial guess in pOutput
184 SolveLinearSystem(nGlobDofs, rhs, diff, pLocToGloMap, nDirDofs);
185
186 // Add back initial and boundary condition
187 Vmath::Vadd(nLocDofs, diff, 1, pLocOutput, 1, pLocOutput, 1);
188 }
189 else
190 {
191 SolveLinearSystem(nGlobDofs, pLocInput, pLocOutput, pLocToGloMap,
192 nDirDofs);
193 }
194}
195
196/**
197 * @brief Apply matrix-vector multiplication using local approach and
198 * the assembly map.
199 *
200 * @param input Vector input.
201 * @param output Result of multiplication.
202 */
205{
206 std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
207
208 int nLocDofs = m_locToGloMap->GetNumLocalCoeffs();
209
210 Array<OneD, NekDouble> tmp(nLocDofs);
211 Array<OneD, NekDouble> tmp1(nLocDofs);
212
213 m_locToGloMap->GlobalToLocal(input, tmp);
214
215 // Perform matrix-vector operation A*d_i
216 expList->GeneralMatrixOp(m_linSysKey, tmp, tmp1);
217
218 m_locToGloMap->Assemble(tmp1, output);
219}
220
221} // namespace Nektar::MultiRegions
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:197
A global linear system.
Definition: GlobalLinSys.h:70
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:122
const std::map< int, RobinBCInfoSharedPtr > m_robinBCInfo
Robin boundary info.
Definition: GlobalLinSys.h:124
void SolveLinearSystem(const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const AssemblyMapSharedPtr &locToGloMap, const int pNumDir=0)
Solve the linear system for given input and output vectors.
Definition: GlobalLinSys.h:190
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:120
DNekScalMatSharedPtr GetBlock(unsigned int n)
Definition: GlobalLinSys.h:209
GlobalLinSysPETScFull(const GlobalLinSysKey &pLinSysKey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
static std::string className
Name of class.
static GlobalLinSysSharedPtr create(const GlobalLinSysKey &pLinSysKey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Creates an instance of this class.
void v_Solve(const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out, const AssemblyMapSharedPtr &locToGloMap, const Array< OneD, const NekDouble > &dirForcing=NullNekDouble1DArray) override
Solve the linear system for given input and output vectors using a specified local to global map.
std::shared_ptr< AssemblyMap > m_locToGloMap
void v_DoMatrixMultiply(const Array< OneD, const NekDouble > &input, Array< OneD, NekDouble > &output) override
Apply matrix-vector multiplication using local approach and the assembly map.
A PETSc global linear system.
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.
void SetUpSolver(NekDouble tolerance)
Set up KSP solver object.
void SetUpMatVec(int nGlobal, int nDir)
Construct PETSc matrix and vector handles.
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.
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:66
std::shared_ptr< RobinBCInfo > RobinBCInfoSharedPtr
GlobalLinSysFactory & GetGlobalLinSysFactory()
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:50
std::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
double NekDouble
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.hpp:180
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.hpp:220