Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GlobalLinSysPETScStaticCond.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File GlobalLinSys.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: GlobalLinSys definition
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 
38 #include <petscsys.h>
39 #include <petscksp.h>
40 #include <petscmat.h>
41 
42 namespace Nektar
43 {
44  namespace MultiRegions
45  {
46  /**
47  * @class GlobalLinSysPETSc
48  *
49  * Solves a linear system using single- or multi-level static
50  * condensation.
51  */
52 
53  /**
54  * Registers the class with the Factory.
55  */
58  "PETScStaticCond",
60  "PETSc static condensation.");
61 
64  "PETScMultiLevelStaticCond",
66  "PETSc multi-level static condensation.");
67 
68  /**
69  * For a matrix system of the form @f[
70  * \left[ \begin{array}{cc}
71  * \boldsymbol{A} & \boldsymbol{B}\\
72  * \boldsymbol{C} & \boldsymbol{D}
73  * \end{array} \right]
74  * \left[ \begin{array}{c} \boldsymbol{x_1}\\ \boldsymbol{x_2}
75  * \end{array}\right]
76  * = \left[ \begin{array}{c} \boldsymbol{y_1}\\ \boldsymbol{y_2}
77  * \end{array}\right],
78  * @f]
79  * where @f$\boldsymbol{D}@f$ and
80  * @f$(\boldsymbol{A-BD^{-1}C})@f$ are invertible, store and assemble
81  * a static condensation system, according to a given local to global
82  * mapping. #m_linSys is constructed by AssembleSchurComplement().
83  * @param mKey Associated matrix key.
84  * @param pLocMatSys LocalMatrixSystem
85  * @param locToGloMap Local to global mapping.
86  */
88  const GlobalLinSysKey &pKey,
89  const boost::weak_ptr<ExpList> &pExpList,
90  const boost::shared_ptr<AssemblyMap> &pLocToGloMap)
91  : GlobalLinSys (pKey, pExpList, pLocToGloMap),
92  GlobalLinSysPETSc (pKey, pExpList, pLocToGloMap),
93  GlobalLinSysStaticCond(pKey, pExpList, pLocToGloMap)
94  {
97  "This constructor is only valid when using static "
98  "condensation");
100  == pLocToGloMap->GetGlobalSysSolnType(),
101  "The local to global map is not set up for the requested "
102  "solution type");
103  }
104 
105  /**
106  *
107  */
109  const GlobalLinSysKey &pKey,
110  const boost::weak_ptr<ExpList> &pExpList,
111  const DNekScalBlkMatSharedPtr pSchurCompl,
112  const DNekScalBlkMatSharedPtr pBinvD,
113  const DNekScalBlkMatSharedPtr pC,
114  const DNekScalBlkMatSharedPtr pInvD,
115  const boost::shared_ptr<AssemblyMap> &pLocToGloMap,
116  const PreconditionerSharedPtr pPrecon)
117  : GlobalLinSys (pKey, pExpList, pLocToGloMap),
118  GlobalLinSysPETSc (pKey, pExpList, pLocToGloMap),
119  GlobalLinSysStaticCond(pKey, pExpList, pLocToGloMap)
120  {
121  m_schurCompl = pSchurCompl;
122  m_BinvD = pBinvD;
123  m_C = pC;
124  m_invD = pInvD;
125  m_precon = pPrecon;
126  }
127 
128  /**
129  *
130  */
132  {
133 
134  }
135 
137  {
139 
140  // Allocate memory for top-level structure
142 
143  // Setup Block Matrix systems
144  int n, n_exp = m_expList.lock()->GetNumElmts();
145 
146  MatrixStorage blkmatStorage = eDIAGONAL;
147  const Array<OneD,const unsigned int>& nbdry_size
148  = m_locToGloMap->GetNumLocalBndCoeffsPerPatch();
149 
151  ::AllocateSharedPtr(nbdry_size, nbdry_size, blkmatStorage);
152 
153  // Preserve original matrix in m_S1Blk
154  for (n = 0; n < n_exp; ++n)
155  {
156  DNekScalMatSharedPtr mat = m_schurCompl->GetBlock(n, n);
157  m_S1Blk->SetBlock(n, n, mat);
158  }
159 
160  // Build preconditioner
161  m_precon->BuildPreconditioner();
162 
163  // Do transform of Schur complement matrix
164  for (n = 0; n < n_exp; ++n)
165  {
166  if (m_linSysKey.GetMatrixType() !=
168  {
169  DNekScalMatSharedPtr mat = m_S1Blk->GetBlock(n, n);
170  DNekScalMatSharedPtr t = m_precon->TransformedSchurCompl(
171  m_expList.lock()->GetOffset_Elmt_Id(n), mat);
172  m_schurCompl->SetBlock(n, n, t);
173  }
174  }
175 
176  // Construct this level
178  }
179 
180  /**
181  * Assemble the schur complement matrix from the block matrices stored
182  * in #m_blkMatrices and the given local to global mapping information.
183  * @param locToGloMap Local to global mapping information.
184  */
186  AssemblyMapSharedPtr pLocToGloMap)
187  {
188  int i, j, n, cnt, gid1, gid2, loc_lda;
189  NekDouble sign1, sign2, value;
190 
191  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
192 
197  DNekScalMatSharedPtr loc_mat;
198 
199  // Build precon again if we in multi-level static condensation (a
200  // bit of a hack)
203  {
205  m_precon->BuildPreconditioner();
206  }
207 
208  // CALCULATE REORDERING MAPPING
209  CalculateReordering(pLocToGloMap->GetGlobalToUniversalBndMap(),
210  pLocToGloMap->GetGlobalToUniversalBndMapUnique(),
211  pLocToGloMap);
212 
213  // SET UP VECTORS AND MATRIX
214  SetUpMatVec(pLocToGloMap->GetNumGlobalBndCoeffs(), nDirDofs);
215 
216  // SET UP SCATTER OBJECTS
217  SetUpScatter();
218 
219  // CONSTRUCT KSP OBJECT
220  SetUpSolver(pLocToGloMap->GetIterativeTolerance());
221 
222  // If we are using the matrix multiplication shell don't try to
223  // populate the matrix.
225  {
226  return;
227  }
228 
229  // POPULATE MATRIX
230  for(n = cnt = 0; n < m_schurCompl->GetNumberOfBlockRows(); ++n)
231  {
232  loc_mat = m_schurCompl->GetBlock(n,n);
233  loc_lda = loc_mat->GetRows();
234 
235  for(i = 0; i < loc_lda; ++i)
236  {
237  gid1 = pLocToGloMap->GetLocalToGlobalBndMap(cnt + i)-nDirDofs;
238  sign1 = pLocToGloMap->GetLocalToGlobalBndSign(cnt + i);
239  if(gid1 >= 0)
240  {
241  int gid1ro = m_reorderedMap[gid1];
242  for(j = 0; j < loc_lda; ++j)
243  {
244  gid2 = pLocToGloMap->GetLocalToGlobalBndMap(cnt + j)
245  - nDirDofs;
246  sign2 = pLocToGloMap->GetLocalToGlobalBndSign(cnt + j);
247  if(gid2 >= 0)
248  {
249  int gid2ro = m_reorderedMap[gid2];
250  value = sign1*sign2*(*loc_mat)(i,j);
251  MatSetValue(
252  m_matrix, gid1ro, gid2ro, value, ADD_VALUES);
253  }
254  }
255  }
256  }
257  cnt += loc_lda;
258  }
259 
260  // ASSEMBLE MATRIX
261  MatAssemblyBegin(m_matrix, MAT_FINAL_ASSEMBLY);
262  MatAssemblyEnd (m_matrix, MAT_FINAL_ASSEMBLY);
263  }
264 
266  v_GetStaticCondBlock(unsigned int n)
267  {
268  DNekScalBlkMatSharedPtr schurComplBlock;
269  int scLevel = m_locToGloMap->GetStaticCondLevel();
270  DNekScalBlkMatSharedPtr sc = scLevel == 0 ? m_S1Blk : m_schurCompl;
271  DNekScalMatSharedPtr localMat = sc->GetBlock(n,n);
272  unsigned int nbdry = localMat->GetRows();
273  unsigned int nblks = 1;
274  unsigned int esize[1] = {nbdry};
275 
276  schurComplBlock = MemoryManager<DNekScalBlkMat>
277  ::AllocateSharedPtr(nblks, nblks, esize, esize);
278  schurComplBlock->SetBlock(0, 0, localMat);
279 
280  return schurComplBlock;
281  }
282 
284  int scLevel,
285  NekVector<NekDouble> &F_GlobBnd)
286  {
287  if (scLevel == 0)
288  {
289  // When matrices are supplied to the constructor at the top
290  // level, the preconditioner is never set up.
291  if (!m_precon)
292  {
294  m_precon->BuildPreconditioner();
295  }
296 
297  return m_S1Blk;
298  }
299  else
300  {
301  return m_schurCompl;
302  }
303  }
304 
306  Array<OneD, NekDouble>& pInOut,
307  int offset)
308  {
309  m_precon->DoTransformToLowEnergy(pInOut, offset);
310  }
311 
313  Array<OneD, NekDouble>& pInOut)
314  {
315  m_precon->DoTransformFromLowEnergy(pInOut);
316  }
317 
318  /**
319  * @brief Apply matrix-vector multiplication using local approach and
320  * the assembly map.
321  *
322  * @param input Vector input.
323  * @param output Result of multiplication.
324  *
325  * @todo This can possibly be made faster by using the sparse
326  * block-matrix multiplication code from the iterative elastic
327  * systems.
328  */
330  const Array<OneD, const NekDouble> &input,
331  Array<OneD, NekDouble> &output)
332  {
333  int nLocBndDofs = m_locToGloMap->GetNumLocalBndCoeffs();
334  int nDirDofs = m_locToGloMap->GetNumGlobalDirBndCoeffs();
335 
336  NekVector<NekDouble> in(nLocBndDofs), out(nLocBndDofs);
337  m_locToGloMap->GlobalToLocalBnd(input, in.GetPtr(), nDirDofs);
338  out = (*m_schurCompl) * in;
339  m_locToGloMap->AssembleBnd(out.GetPtr(), output, nDirDofs);
340  }
341 
343  const GlobalLinSysKey &mkey,
344  const boost::weak_ptr<ExpList> &pExpList,
345  const DNekScalBlkMatSharedPtr pSchurCompl,
346  const DNekScalBlkMatSharedPtr pBinvD,
347  const DNekScalBlkMatSharedPtr pC,
348  const DNekScalBlkMatSharedPtr pInvD,
349  const boost::shared_ptr<AssemblyMap> &l2gMap)
350  {
352  GlobalLinSysPETScStaticCond>::AllocateSharedPtr(
353  mkey, pExpList, pSchurCompl, pBinvD, pC, pInvD, l2gMap,
354  m_precon);
355  sys->Initialise(l2gMap);
356  return sys;
357  }
358  }
359 }
GlobalSysSolnType GetGlobalSysSolnType() const
Return the associated solution type.
boost::shared_ptr< GlobalLinSysPETScStaticCond > GlobalLinSysPETScStaticCondSharedPtr
virtual void v_BasisInvTransform(Array< OneD, NekDouble > &pInOut)
virtual void v_BasisTransform(Array< OneD, NekDouble > &pInOut, int offset)
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
boost::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:53
DNekScalBlkMatSharedPtr m_schurCompl
Block Schur complement matrix.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
virtual DNekScalBlkMatSharedPtr v_PreSolve(int scLevel, NekVector< NekDouble > &F_GlobBnd)
DNekScalBlkMatSharedPtr m_C
Block matrix.
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.
GlobalLinSysPETScStaticCond(const GlobalLinSysKey &mkey, const boost::weak_ptr< ExpList > &pExpList, const boost::shared_ptr< AssemblyMap > &locToGloMap)
Constructor for full direct matrix solve.
boost::shared_ptr< Preconditioner > PreconditionerSharedPtr
Definition: GlobalLinSys.h:62
void SetUpSolver(NekDouble tolerance)
Set up KSP solver object.
void Initialise(const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Definition: GlobalLinSys.h:214
DNekScalBlkMatSharedPtr m_invD
Block matrix.
vector< int > m_reorderedMap
Reordering that takes universal IDs to a unique row in the PETSc matrix.
boost::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
virtual GlobalLinSysStaticCondSharedPtr v_Recurse(const GlobalLinSysKey &mkey, const boost::weak_ptr< ExpList > &pExpList, const DNekScalBlkMatSharedPtr pSchurCompl, const DNekScalBlkMatSharedPtr pBinvD, const DNekScalBlkMatSharedPtr pC, const DNekScalBlkMatSharedPtr pInvD, const boost::shared_ptr< AssemblyMap > &locToGloMap)
virtual DNekScalBlkMatSharedPtr v_GetStaticCondBlock(unsigned int n)
Retrieves a the static condensation block matrices from n-th expansion using the matrix key provided ...
boost::shared_ptr< DNekScalBlkMat > DNekScalBlkMatSharedPtr
Definition: NekTypeDefs.hpp:74
double NekDouble
void SetupTopLevel(const boost::shared_ptr< AssemblyMap > &locToGloMap)
Set up the storage for the Schur complement or the top level of the multi-level Schur complement...
Describe a linear system.
StdRegions::MatrixType GetMatrixType() const
Return the matrix type.
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
A global linear system.
Definition: GlobalLinSys.h:74
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.
virtual void v_AssembleSchurComplement(boost::shared_ptr< AssemblyMap > locToGloMap)
Assemble the Schur complement matrix.
void SetUpMatVec(int nGlobal, int nDir)
Construct PETSc matrix and vector handles.
boost::shared_ptr< AssemblyMap > m_locToGloMap
Local to global map.
GlobalLinSysFactory & GetGlobalLinSysFactory()
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:191
DNekScalBlkMatSharedPtr m_BinvD
Block matrix.
Array< OneD, DataType > & GetPtr()
Definition: NekVector.cpp:230
PETScMatMult m_matMult
Enumerator to select matrix multiplication type.
static GlobalLinSysSharedPtr create(const GlobalLinSysKey &pLinSysKey, const boost::weak_ptr< ExpList > &pExpList, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Creates an instance of this class.
PreconditionerSharedPtr CreatePrecon(AssemblyMapSharedPtr asmMap)
Create a preconditioner object from the parameters defined in the supplied assembly map...
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215
boost::shared_ptr< GlobalLinSysStaticCond > GlobalLinSysStaticCondSharedPtr
const boost::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:129