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