Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // 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: GlobalLinSysPETScFull definition
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 #include <MultiRegions/ExpList.h>
38 
39 #include "petscao.h"
40 #include "petscis.h"
41 
42 namespace Nektar
43 {
44  namespace MultiRegions
45  {
46  /**
47  * @class GlobalLinSysPETScFull
48  */
49 
50  /**
51  * Registers the class with the Factory.
52  */
55  "PETScFull",
57  "PETSc Full Matrix.");
58 
59 
60  /// Constructor for full direct matrix solve.
62  const GlobalLinSysKey &pLinSysKey,
63  const boost::weak_ptr<ExpList> &pExp,
64  const boost::shared_ptr<AssemblyMap> &pLocToGloMap)
65  : GlobalLinSys (pLinSysKey, pExp, pLocToGloMap),
66  GlobalLinSysPETSc(pLinSysKey, pExp, pLocToGloMap)
67  {
68  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
69 
70  int i, j, n, cnt, gid1, gid2, loc_lda;
71  NekDouble sign1, sign2, value;
72  DNekScalMatSharedPtr loc_mat;
73 
74  // CALCULATE REORDERING MAPPING
75  CalculateReordering(pLocToGloMap->GetGlobalToUniversalMap(),
76  pLocToGloMap->GetGlobalToUniversalMapUnique(),
77  pLocToGloMap);
78 
79  // SET UP VECTORS AND MATRIX
80  SetUpMatVec(pLocToGloMap->GetNumGlobalCoeffs(), nDirDofs);
81 
82  // SET UP SCATTER OBJECTS
83  SetUpScatter();
84 
85  // CONSTRUCT KSP OBJECT
86  SetUpSolver(pLocToGloMap->GetIterativeTolerance());
87 
88  // POPULATE MATRIX
89  for(n = cnt = 0; n < m_expList.lock()->GetNumElmts(); ++n)
90  {
91  loc_mat = GetBlock(m_expList.lock()->GetOffset_Elmt_Id(n));
92  loc_lda = loc_mat->GetRows();
93 
94  for(i = 0; i < loc_lda; ++i)
95  {
96  gid1 = pLocToGloMap->GetLocalToGlobalMap(cnt+i) - nDirDofs;
97  sign1 = pLocToGloMap->GetLocalToGlobalSign(cnt + i);
98  if(gid1 >= 0)
99  {
100  int gid1ro = m_reorderedMap[gid1];
101  for(j = 0; j < loc_lda; ++j)
102  {
103  gid2 = pLocToGloMap->GetLocalToGlobalMap(cnt + j)
104  - nDirDofs;
105  sign2 = pLocToGloMap->GetLocalToGlobalSign(cnt + j);
106  if(gid2 >= 0)
107  {
108  int gid2ro = m_reorderedMap[gid2];
109  value = sign1*sign2*(*loc_mat)(i,j);
110  MatSetValue(
111  m_matrix, gid1ro, gid2ro, value, ADD_VALUES);
112  }
113  }
114  }
115  }
116  cnt += loc_lda;
117  }
118 
119  // ASSEMBLE MATRIX
120  MatAssemblyBegin(m_matrix, MAT_FINAL_ASSEMBLY);
121  MatAssemblyEnd (m_matrix, MAT_FINAL_ASSEMBLY);
122  }
123 
124 
126  {
127 
128  }
129 
130 
131  /**
132  * Solve the linear system using a full global matrix system.
133  */
135  const Array<OneD, const NekDouble> &pInput,
136  Array<OneD, NekDouble> &pOutput,
137  const AssemblyMapSharedPtr &pLocToGloMap,
138  const Array<OneD, const NekDouble> &pDirForcing)
139  {
140  bool dirForcCalculated = (bool) pDirForcing.num_elements();
141  int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
142  int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
143  Array<OneD, NekDouble> tmp(nGlobDofs), tmp2;
144 
145  int nDirTotal = nDirDofs;
146  m_expList.lock()->GetComm()->GetRowComm()->AllReduce(
147  nDirTotal, LibUtilities::ReduceSum);
148 
149  if(nDirTotal)
150  {
151  // calculate the dirichlet forcing
152  if(dirForcCalculated)
153  {
154  Vmath::Vsub(nGlobDofs,
155  pInput.get(), 1,
156  pDirForcing.get(), 1,
157  tmp.get(), 1);
158  }
159  else
160  {
161  // Calculate the dirichlet forcing and substract it
162  // from the rhs
163  m_expList.lock()->GeneralMatrixOp(
164  m_linSysKey, pOutput, tmp, eGlobal);
165 
166  Vmath::Vsub(nGlobDofs,
167  pInput.get(), 1,
168  tmp.get(), 1,
169  tmp.get(), 1);
170  }
171 
172  Array<OneD, NekDouble> out(nGlobDofs,0.0);
173  SolveLinearSystem(nGlobDofs, tmp, out, pLocToGloMap, nDirDofs);
174  Vmath::Vadd(nGlobDofs-nDirDofs, &out [nDirDofs], 1,
175  &pOutput[nDirDofs], 1, &pOutput[nDirDofs], 1);
176  }
177  else
178  {
179  SolveLinearSystem(nDirDofs, pInput, pOutput, pLocToGloMap);
180  }
181  }
182 
183  /**
184  * @brief Apply matrix-vector multiplication using local approach and
185  * the assembly map.
186  *
187  * @param input Vector input.
188  * @param output Result of multiplication.
189  */
191  const Array<OneD, const NekDouble> &input,
192  Array<OneD, NekDouble> &output)
193  {
194  boost::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
195 
196  // Perform matrix-vector operation A*d_i
197  expList->GeneralMatrixOp(
198  m_linSysKey, input, output, eGlobal);
199  }
200  }
201 }
static std::string className
Name of class.
boost::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:53
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:199
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.
GlobalLinSysPETScFull(const GlobalLinSysKey &pLinSysKey, const boost::weak_ptr< ExpList > &pExpList, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
void SetUpSolver(NekDouble tolerance)
Set up KSP solver object.
vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
boost::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
Global coefficients.
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...
DNekScalMatSharedPtr GetBlock(unsigned int n)
Definition: GlobalLinSys.h:220
double NekDouble
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...
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:127
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:329
A global linear system.
Definition: GlobalLinSys.h:74
void SetUpMatVec(int nGlobal, int nDir)
Construct PETSc matrix and vector handles.
GlobalLinSysFactory & GetGlobalLinSysFactory()
static GlobalLinSysSharedPtr create(const GlobalLinSysKey &pLinSysKey, const boost::weak_ptr< ExpList > &pExpList, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Creates an instance of this class.
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:285
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.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215
const boost::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:129