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 
35 #include <MultiRegions/ExpList.h>
37 
38 #include "petscao.h"
39 #include "petscis.h"
40 
41 using namespace std;
42 
43 namespace Nektar
44 {
45 namespace MultiRegions
46 {
47 /**
48  * @class GlobalLinSysPETScFull
49  */
50 
51 /**
52  * Registers the class with the Factory.
53  */
54 string GlobalLinSysPETScFull::className =
56  "PETScFull", GlobalLinSysPETScFull::create, "PETSc Full Matrix.");
57 
58 /// Constructor for full direct matrix solve.
59 GlobalLinSysPETScFull::GlobalLinSysPETScFull(
60  const GlobalLinSysKey &pLinSysKey, const std::weak_ptr<ExpList> &pExp,
61  const std::shared_ptr<AssemblyMap> &pLocToGloMap)
62  : GlobalLinSys(pLinSysKey, pExp, pLocToGloMap),
63  GlobalLinSysPETSc(pLinSysKey, pExp, pLocToGloMap)
64 {
65  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
66 
67  int i, j, n, cnt, gid1, gid2, loc_lda;
68  NekDouble sign1, sign2, value;
69  DNekScalMatSharedPtr loc_mat;
70 
71  // CALCULATE REORDERING MAPPING
72  CalculateReordering(pLocToGloMap->GetGlobalToUniversalMap(),
73  pLocToGloMap->GetGlobalToUniversalMapUnique(),
74  pLocToGloMap);
75 
76  // SET UP VECTORS AND MATRIX
77  SetUpMatVec(pLocToGloMap->GetNumGlobalCoeffs(), nDirDofs);
78 
79  // SET UP SCATTER OBJECTS
80  SetUpScatter();
81 
82  // CONSTRUCT KSP OBJECT
83  SetUpSolver(pLocToGloMap->GetIterativeTolerance());
84 
85  // POPULATE MATRIX
86  for (n = cnt = 0; n < m_expList.lock()->GetNumElmts(); ++n)
87  {
88  loc_mat = GetBlock(n);
89  loc_lda = loc_mat->GetRows();
90 
91  for (i = 0; i < loc_lda; ++i)
92  {
93  gid1 = pLocToGloMap->GetLocalToGlobalMap(cnt + i) - nDirDofs;
94  sign1 = pLocToGloMap->GetLocalToGlobalSign(cnt + i);
95  if (gid1 >= 0)
96  {
97  int gid1ro = m_reorderedMap[gid1];
98  for (j = 0; j < loc_lda; ++j)
99  {
100  gid2 =
101  pLocToGloMap->GetLocalToGlobalMap(cnt + j) - nDirDofs;
102  sign2 = pLocToGloMap->GetLocalToGlobalSign(cnt + j);
103  if (gid2 >= 0)
104  {
105  int gid2ro = m_reorderedMap[gid2];
106  value = sign1 * sign2 * (*loc_mat)(i, j);
107  MatSetValue(m_matrix, gid1ro, gid2ro, value,
108  ADD_VALUES);
109  }
110  }
111  }
112  }
113  cnt += loc_lda;
114  }
115 
116  // ASSEMBLE MATRIX
117  MatAssemblyBegin(m_matrix, MAT_FINAL_ASSEMBLY);
118  MatAssemblyEnd(m_matrix, MAT_FINAL_ASSEMBLY);
119 }
120 
122 {
123 }
124 
125 /**
126  * Solve the linear system using a full global matrix system.
127  */
129  const Array<OneD, const NekDouble> &pLocInput,
130  Array<OneD, NekDouble> &pLocOutput,
131  const AssemblyMapSharedPtr &pLocToGloMap,
132  const Array<OneD, const NekDouble> &pDirForcing)
133 {
134  std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
135  bool dirForcCalculated = (bool)pDirForcing.size();
136  int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
137  int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
138  int nLocDofs = pLocToGloMap->GetNumLocalCoeffs();
139 
140  m_locToGloMap = pLocToGloMap; // required for DoMatrixMultiply
141 
142  Array<OneD, NekDouble> tmp(nLocDofs);
143  Array<OneD, NekDouble> tmp1(nLocDofs);
144  Array<OneD, NekDouble> global(nGlobDofs, 0.0);
145 
146  expList->GetComm()->GetRowComm()->AllReduce(nDirDofs,
148 
149  if (nDirDofs)
150  {
151  // calculate the dirichlet forcing
152  if (dirForcCalculated)
153  {
154  // assume pDirForcing is in local space
155  ASSERTL0(
156  pDirForcing.size() >= nLocDofs,
157  "DirForcing is not of sufficient size. Is it in local space?");
158  Vmath::Vsub(nLocDofs, pLocInput, 1, pDirForcing, 1, tmp1, 1);
159  }
160  else
161  {
162  // Calculate the dirichlet forcing and substract it
163  // from the rhs
164  expList->GeneralMatrixOp(m_linSysKey, pLocOutput, tmp);
165 
166  // Apply robin boundary conditions to the solution.
167  for (auto &r : m_robinBCInfo) // add robin mass matrix
168  {
170  Array<OneD, NekDouble> tmploc;
171 
172  int n = r.first;
173 
174  int offset = expList->GetCoeff_Offset(n);
175  LocalRegions::ExpansionSharedPtr vExp = expList->GetExp(n);
176 
177  // add local matrix contribution
178  for (rBC = r.second; rBC; rBC = rBC->next)
179  {
180  vExp->AddRobinTraceContribution(
181  rBC->m_robinID, rBC->m_robinPrimitiveCoeffs,
182  pLocOutput + offset, tmploc = tmp + offset);
183  }
184  }
185 
186  Vmath::Vsub(nLocDofs, pLocInput, 1, tmp, 1, tmp1, 1);
187  }
188 
189  pLocToGloMap->Assemble(tmp1, tmp);
190 
191  SolveLinearSystem(nGlobDofs, tmp, global, pLocToGloMap, nDirDofs);
192 
193  pLocToGloMap->GlobalToLocal(global, tmp);
194 
195  // Add back initial and boundary condition
196  Vmath::Vadd(nLocDofs, tmp, 1, pLocOutput, 1, pLocOutput, 1);
197  }
198  else
199  {
200  pLocToGloMap->Assemble(pLocInput, tmp);
201  SolveLinearSystem(nGlobDofs, tmp, global, pLocToGloMap);
202  pLocToGloMap->GlobalToLocal(global, pLocOutput);
203  }
204 }
205 
206 /**
207  * @brief Apply matrix-vector multiplication using local approach and
208  * the assembly map.
209  *
210  * @param input Vector input.
211  * @param output Result of multiplication.
212  */
215 {
216  std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
217 
218  int nLocDofs = m_locToGloMap->GetNumLocalCoeffs();
219 
220  Array<OneD, NekDouble> tmp(nLocDofs);
221  Array<OneD, NekDouble> tmp1(nLocDofs);
222 
223  m_locToGloMap->GlobalToLocal(input, tmp);
224 
225  // Perform matrix-vector operation A*d_i
226  expList->GeneralMatrixOp(m_linSysKey, tmp, tmp1);
227 
228  m_locToGloMap->Assemble(tmp1, output);
229 }
230 
231 } // namespace MultiRegions
232 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
A global linear system.
Definition: GlobalLinSys.h:72
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:123
const std::map< int, RobinBCInfoSharedPtr > m_robinBCInfo
Robin boundary info.
Definition: GlobalLinSys.h:125
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:121
DNekScalMatSharedPtr GetBlock(unsigned int n)
Definition: GlobalLinSys.h:209
virtual void v_Solve(const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out, const AssemblyMapSharedPtr &locToGloMap, const Array< OneD, const NekDouble > &dirForcing=NullNekDouble1DArray)
Solve the linear system for given input and output vectors using a specified local to global map.
std::shared_ptr< AssemblyMap > m_locToGloMap
virtual void v_DoMatrixMultiply(const Array< OneD, const NekDouble > &input, Array< OneD, NekDouble > &output)
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:68
std::shared_ptr< RobinBCInfo > RobinBCInfoSharedPtr
GlobalLinSysFactory & GetGlobalLinSysFactory()
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:51
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
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.cpp:359
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.cpp:419