Nektar++
GlobalLinSysIterativeStaticCond.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: GlobalLinSysIterativeStaticCond.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: Implementation to linear solver using single-
32 // or multi-level static condensation
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
41 
42 using namespace std;
43 
44 namespace Nektar
45 {
46  namespace MultiRegions
47  {
48  /**
49  * @class GlobalLinSysIterativeStaticCond
50  *
51  * Solves a linear system iteratively using single- or multi-level
52  * static condensation.
53  */
54 
55  /**
56  * Registers the class with the Factory.
57  */
58  string GlobalLinSysIterativeStaticCond::className
60  "IterativeStaticCond",
61  GlobalLinSysIterativeStaticCond::create,
62  "Iterative static condensation.");
63 
64  string GlobalLinSysIterativeStaticCond::className2
66  "IterativeMultiLevelStaticCond",
67  GlobalLinSysIterativeStaticCond::create,
68  "Iterative multi-level static condensation.");
69 
70 
71  std::string GlobalLinSysIterativeStaticCond::storagedef =
72  LibUtilities::SessionReader::RegisterDefaultSolverInfo(
73  "LocalMatrixStorageStrategy",
74  "Sparse");
75  std::string GlobalLinSysIterativeStaticCond::storagelookupIds[3] = {
76  LibUtilities::SessionReader::RegisterEnumValue(
77  "LocalMatrixStorageStrategy",
78  "Contiguous",
80  LibUtilities::SessionReader::RegisterEnumValue(
81  "LocalMatrixStorageStrategy",
82  "Non-contiguous",
84  LibUtilities::SessionReader::RegisterEnumValue(
85  "LocalMatrixStorageStrategy",
86  "Sparse",
88  };
89 
90  /**
91  * For a matrix system of the form @f[
92  * \left[ \begin{array}{cc}
93  * \boldsymbol{A} & \boldsymbol{B}\\
94  * \boldsymbol{C} & \boldsymbol{D}
95  * \end{array} \right]
96  * \left[ \begin{array}{c} \boldsymbol{x_1}\\ \boldsymbol{x_2}
97  * \end{array}\right]
98  * = \left[ \begin{array}{c} \boldsymbol{y_1}\\ \boldsymbol{y_2}
99  * \end{array}\right],
100  * @f]
101  * where @f$\boldsymbol{D}@f$ and
102  * @f$(\boldsymbol{A-BD^{-1}C})@f$ are invertible, store and assemble
103  * a static condensation system, according to a given local to global
104  * mapping. #m_linSys is constructed by AssembleSchurComplement().
105  * @param mKey Associated matrix key.
106  * @param pLocMatSys LocalMatrixSystem
107  * @param locToGloMap Local to global mapping.
108  */
109  GlobalLinSysIterativeStaticCond::GlobalLinSysIterativeStaticCond(
110  const GlobalLinSysKey &pKey,
111  const std::weak_ptr<ExpList> &pExpList,
112  const std::shared_ptr<AssemblyMap> &pLocToGloMap)
113  : GlobalLinSys (pKey, pExpList, pLocToGloMap),
114  GlobalLinSysIterative (pKey, pExpList, pLocToGloMap),
115  GlobalLinSysStaticCond(pKey, pExpList, pLocToGloMap)
116  {
119  "This constructor is only valid when using static "
120  "condensation");
122  == pLocToGloMap->GetGlobalSysSolnType(),
123  "The local to global map is not set up for the requested "
124  "solution type");
125  }
126 
127 
128  /**
129  *
130  */
132  const GlobalLinSysKey &pKey,
133  const std::weak_ptr<ExpList> &pExpList,
134  const DNekScalBlkMatSharedPtr pSchurCompl,
135  const DNekScalBlkMatSharedPtr pBinvD,
136  const DNekScalBlkMatSharedPtr pC,
137  const DNekScalBlkMatSharedPtr pInvD,
138  const std::shared_ptr<AssemblyMap> &pLocToGloMap,
139  const PreconditionerSharedPtr pPrecon)
140  : GlobalLinSys (pKey, pExpList, pLocToGloMap),
141  GlobalLinSysIterative (pKey, pExpList, pLocToGloMap),
142  GlobalLinSysStaticCond(pKey, pExpList, pLocToGloMap)
143  {
144  m_schurCompl = pSchurCompl;
145  m_BinvD = pBinvD;
146  m_C = pC;
147  m_invD = pInvD;
148  m_precon = pPrecon;
149  }
150 
151 
153  {
154  auto asmMap = m_locToGloMap.lock();
155 
156  m_precon = CreatePrecon(asmMap);
157 
158  // Allocate memory for top-level structure
159  SetupTopLevel(asmMap);
160 
161  // Setup Block Matrix systems
162  int n, n_exp = m_expList.lock()->GetNumElmts();
163 
164  // Build preconditioner
165  m_precon->BuildPreconditioner();
166 
167  // Do transform of Schur complement matrix
168  int cnt = 0;
169  for (n = 0; n < n_exp; ++n)
170  {
171  if (m_linSysKey.GetMatrixType() !=
173  {
174  DNekScalMatSharedPtr mat = m_schurCompl->GetBlock(n, n);
175  DNekScalMatSharedPtr t = m_precon->TransformedSchurCompl(
176  n, cnt, mat);
177  m_schurCompl->SetBlock(n, n, t);
178  cnt += mat->GetRows();
179  }
180  }
181 
182  // Construct this level
183  Initialise(asmMap);
184  }
185 
186  /**
187  *
188  */
190  {
191 
192  }
193 
195  v_GetStaticCondBlock(unsigned int n)
196  {
197  DNekScalBlkMatSharedPtr schurComplBlock;
198  DNekScalMatSharedPtr localMat = m_schurCompl->GetBlock(n,n);
199  unsigned int nbdry = localMat->GetRows();
200  unsigned int nblks = 1;
201  unsigned int esize[1] = {nbdry};
202 
203  schurComplBlock = MemoryManager<DNekScalBlkMat>
204  ::AllocateSharedPtr(nblks, nblks, esize, esize);
205  schurComplBlock->SetBlock(0, 0, localMat);
206 
207  return schurComplBlock;
208  }
209 
210  /**
211  * Assemble the schur complement matrix from the block matrices stored
212  * in #m_blkMatrices and the given local to global mapping information.
213  * @param locToGloMap Local to global mapping information.
214  */
216  const AssemblyMapSharedPtr pLocToGloMap)
217  {
218  boost::ignore_unused(pLocToGloMap);
219  // Set up unique map
220  v_UniqueMap();
221 
222  // Build precon again if we in multi-level static condensation (a
223  // bit of a hack)
226  {
228  m_precon->BuildPreconditioner();
229  }
230 
232  }
233 
234 
235  /**
236  * Populates sparse block-diagonal schur complement matrix from
237  * the block matrices stored in #m_blkMatrices.
238  */
240  {
241  LocalMatrixStorageStrategy storageStrategy =
242  m_expList.lock()->GetSession()->
243  GetSolverInfoAsEnum<LocalMatrixStorageStrategy>(
244  "LocalMatrixStorageStrategy");
245 
246  switch(storageStrategy)
247  {
250  {
251  size_t storageSize = 0;
252  int nBlk = m_schurCompl->GetNumberOfBlockRows();
253 
254  m_scale = Array<OneD, NekDouble> (nBlk, 1.0);
255  m_rows = Array<OneD, unsigned int> (nBlk, 0U);
256 
257  // Determine storage requirements for dense blocks.
258  for (int i = 0; i < nBlk; ++i)
259  {
260  m_rows[i] = m_schurCompl->GetBlock(i,i)->GetRows();
261  m_scale[i] = m_schurCompl->GetBlock(i,i)->Scale();
262  storageSize += m_rows[i] * m_rows[i];
263  }
264 
265  // Assemble dense storage blocks.
266  DNekScalMatSharedPtr loc_mat;
267  m_denseBlocks.resize(nBlk);
268  double *ptr = 0;
269 
270  if (MultiRegions::eContiguous == storageStrategy)
271  {
272  m_storage.resize (storageSize);
273  ptr = &m_storage[0];
274  }
275 
276  for (unsigned int n = 0; n < nBlk; ++n)
277  {
278  loc_mat = m_schurCompl->GetBlock(n,n);
279 
280  if (MultiRegions::eContiguous == storageStrategy)
281  {
282  int loc_lda = loc_mat->GetRows();
283  int blockSize = loc_lda * loc_lda;
284  m_denseBlocks[n] = ptr;
285  for(int i = 0; i < loc_lda; ++i)
286  {
287  for(int j = 0; j < loc_lda; ++j)
288  {
289  ptr[j*loc_lda+i] = (*loc_mat)(i,j);
290  }
291  }
292  ptr += blockSize;
294  }
295  else
296  {
297  m_denseBlocks[n] = loc_mat->GetRawPtr();
298  }
299  }
300  break;
301  }
303  {
304  DNekScalMatSharedPtr loc_mat;
305  int loc_lda;
306  int blockSize = 0;
307 
308  // First run through to split the set of local matrices into
309  // partitions of fixed block size, and count number of local
310  // matrices that belong to each partition.
311  std::vector<std::pair<int,int> > partitions;
312  for(int n = 0; n < m_schurCompl->GetNumberOfBlockRows(); ++n)
313  {
314  loc_mat = m_schurCompl->GetBlock(n,n);
315  loc_lda = loc_mat->GetRows();
316 
317  ASSERTL1(loc_lda >= 0,
318  boost::lexical_cast<std::string>(n) + "-th "
319  "matrix block in Schur complement has "
320  "rank 0!");
321 
322  if (blockSize == loc_lda)
323  {
324  partitions[partitions.size()-1].first++;
325  }
326  else
327  {
328  blockSize = loc_lda;
329  partitions.push_back(make_pair(1,loc_lda));
330  }
331  }
332 
333  MatrixStorage matStorage = eFULL;
334 
335  // Create a vector of sparse storage holders
337  sparseStorage (partitions.size());
338 
339  for (int part = 0, n = 0; part < partitions.size(); ++part)
340  {
341  BCOMatType partMat;
342 
343  for(int k = 0; k < partitions[part].first; ++k, ++n)
344  {
345  loc_mat = m_schurCompl->GetBlock(n,n);
346  loc_lda = loc_mat->GetRows();
347 
348  ASSERTL1(loc_lda == partitions[part].second,
349  boost::lexical_cast<std::string>(n) + "-th"
350  " matrix block in Schur complement has "
351  "unexpected rank");
352 
353  NekDouble scale = loc_mat->Scale();
354  if(fabs(scale-1.0) > NekConstants::kNekZeroTol)
355  {
356  Array<OneD, NekDouble> matarray(loc_lda*loc_lda);
357  Vmath::Smul(loc_lda*loc_lda,scale,
358  loc_mat->GetRawPtr(),1,&matarray[0],1);
359  partMat[make_pair(k,k)] = BCOEntryType(matarray);
360  }
361  else // scale factor is 1.0
362  {
363  partMat[make_pair(k,k)] = BCOEntryType(
364  loc_lda*loc_lda, loc_mat->GetRawPtr());
365  }
366 
368  }
369 
370  sparseStorage[part] =
373  partitions[part].first, partitions[part].first,
374  partitions[part].second, partMat, matStorage );
375  }
376 
377  // Create block diagonal matrix
379  AllocateSharedPtr(sparseStorage);
380 
381  break;
382  }
383  default:
384  ErrorUtil::NekError("Solver info property \
385  LocalMatrixStorageStrategy takes values \
386  Contiguous, Non-contiguous and Sparse");
387  }
388  }
389 
390  /**
391  *
392  */
394  const Array<OneD, NekDouble>& pInput,
395  Array<OneD, NekDouble>& pOutput)
396  {
397  int nLocal = m_locToGloMap.lock()->GetNumLocalBndCoeffs();
398  AssemblyMapSharedPtr asmMap = m_locToGloMap.lock();
399 
400  if (m_sparseSchurCompl)
401  {
402  // Do matrix multiply locally using block-diagonal sparse matrix
403  Array<OneD, NekDouble> tmp = m_wsp + nLocal;
404 
405  asmMap->GlobalToLocalBnd(pInput, m_wsp);
406  m_sparseSchurCompl->Multiply(m_wsp,tmp);
407  asmMap->AssembleBnd(tmp, pOutput);
408  }
409  else
410  {
411  // Do matrix multiply locally, using direct BLAS calls
412  asmMap->GlobalToLocalBnd(pInput, m_wsp);
413  int i, cnt;
414  Array<OneD, NekDouble> tmpout = m_wsp + nLocal;
415  for (i = cnt = 0; i < m_denseBlocks.size(); cnt += m_rows[i], ++i)
416  {
417  const int rows = m_rows[i];
418  Blas::Dgemv('N', rows, rows,
419  m_scale[i], m_denseBlocks[i], rows,
420  m_wsp.get()+cnt, 1,
421  0.0, tmpout.get()+cnt, 1);
422  }
423  asmMap->AssembleBnd(tmpout, pOutput);
424  }
425  }
426 
428  {
429  m_map = m_locToGloMap.lock()->GetGlobalToUniversalBndMapUnique();
430  }
431 
433  int scLevel,
434  Array<OneD, NekDouble> &F_bnd)
435  {
436  if (scLevel == 0)
437  {
438  // When matrices are supplied to the constructor at the top
439  // level, the preconditioner is never set up.
440  if (!m_precon)
441  {
443  m_precon->BuildPreconditioner();
444  }
445 
446 #if 1 // to be consistent with original
447 
448  int nGloBndDofs = m_locToGloMap.lock()->GetNumGlobalBndCoeffs();
449  Array<OneD, NekDouble> F_gloBnd(nGloBndDofs);
450  NekVector<NekDouble> F_GloBnd(nGloBndDofs,F_gloBnd,eWrapper);
451 
452  m_locToGloMap.lock()->AssembleBnd(F_bnd,F_gloBnd);
453  Set_Rhs_Magnitude(F_GloBnd);
454 
455 #else
456  int nLocBndDofs = m_locToGloMap.lock()->GetNumLocalBndCoeffs();
457 
458  //Set_Rhs_Magnitude - version using local array
459 
460  Array<OneD, NekDouble> vExchange(1, 0.0);
461 
462  vExchange[0] += Blas::Ddot(nLocBndDofs, F_bnd,1,F_bnd,1);
463  m_expList.lock()->GetComm()->GetRowComm()->AllReduce(
465 
466  // To ensure that very different rhs values are not being
467  // used in subsequent solvers such as the velocit solve in
468  // INC NS. If this works we then need to work out a better
469  // way to control this.
470  NekDouble new_rhs_mag = (vExchange[0] > 1e-6)? vExchange[0] : 1.0;
471 
473  {
474  m_rhs_magnitude = new_rhs_mag;
475  }
476  else
477  {
479  (1.0-m_rhs_mag_sm)*new_rhs_mag);
480  }
481 #endif
482  }
483  else
484  {
485  // for multilevel iterative solver always use rhs
486  // vector value with no weighting
488  }
489  }
490 
492  Array<OneD, NekDouble>& pInOut)
493  {
494  m_precon->DoTransformBasisToLowEnergy(pInOut);
495  }
496 
498  Array<OneD, NekDouble>& pInOut)
499  {
500  m_precon->DoTransformCoeffsFromLowEnergy(pInOut);
501  }
502 
504  const Array<OneD, NekDouble>& pInput,
505  Array<OneD, NekDouble>& pOutput)
506  {
507  m_precon->DoTransformCoeffsToLowEnergy(pInput,pOutput);
508  }
509 
511  const GlobalLinSysKey &mkey,
512  const std::weak_ptr<ExpList> &pExpList,
513  const DNekScalBlkMatSharedPtr pSchurCompl,
514  const DNekScalBlkMatSharedPtr pBinvD,
515  const DNekScalBlkMatSharedPtr pC,
516  const DNekScalBlkMatSharedPtr pInvD,
517  const std::shared_ptr<AssemblyMap> &l2gMap)
518  {
520  GlobalLinSysIterativeStaticCond>::AllocateSharedPtr(
521  mkey, pExpList, pSchurCompl, pBinvD, pC, pInvD, l2gMap,
522  m_precon);
523  sys->Initialise(l2gMap);
524  return sys;
525  }
526  }
527 }
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:250
Nektar::ErrorUtil::NekError NekError
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
A global linear system.
Definition: GlobalLinSys.h:73
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:127
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:125
PreconditionerSharedPtr CreatePrecon(AssemblyMapSharedPtr asmMap)
Create a preconditioner object from the parameters defined in the supplied assembly map.
virtual void v_DropStaticCondBlock(unsigned int n)
Releases the static condensation block matrices from NekManager of n-th expansion using the matrix ke...
void Initialise(const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Definition: GlobalLinSys.h:216
Array< OneD, int > m_map
Global to universal unique map.
NekDouble m_rhs_magnitude
dot product of rhs to normalise stopping criterion
void Set_Rhs_Magnitude(const NekVector< NekDouble > &pIn)
NekDouble m_rhs_mag_sm
cnt to how many times rhs_magnitude is called
void v_PreSolve(int scLevel, Array< OneD, NekDouble > &F_bnd)
std::vector< const double * > m_denseBlocks
Vector of pointers to local matrix data.
virtual void v_CoeffsBwdTransform(Array< OneD, NekDouble > &pInOut)
virtual GlobalLinSysStaticCondSharedPtr v_Recurse(const GlobalLinSysKey &mkey, const std::weak_ptr< ExpList > &pExpList, const DNekScalBlkMatSharedPtr pSchurCompl, const DNekScalBlkMatSharedPtr pBinvD, const DNekScalBlkMatSharedPtr pC, const DNekScalBlkMatSharedPtr pInvD, const std::shared_ptr< AssemblyMap > &locToGloMap)
virtual void v_DoMatrixMultiply(const Array< OneD, NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)
Perform a Shur-complement matrix multiply operation.
std::vector< double > m_storage
Dense storage for block Schur complement matrix.
GlobalLinSysIterativeStaticCond(const GlobalLinSysKey &mkey, const std::weak_ptr< ExpList > &pExpList, const std::shared_ptr< AssemblyMap > &locToGloMap)
Constructor for full direct matrix solve.
void PrepareLocalSchurComplement()
Prepares local representation of Schur complement stored as a sparse block-diagonal matrix.
virtual void v_BasisFwdTransform(Array< OneD, NekDouble > &pInOut)
Array< OneD, unsigned int > m_rows
Ranks of local matrices.
virtual DNekScalBlkMatSharedPtr v_GetStaticCondBlock(unsigned int n)
Retrieves a the static condensation block matrices from n-th expansion using the matrix key provided ...
void v_AssembleSchurComplement(const std::shared_ptr< AssemblyMap > locToGloMap)
Assemble the Schur complement matrix.
Array< OneD, NekDouble > m_scale
Scaling factors for local matrices.
DNekSmvBsrDiagBlkMatSharedPtr m_sparseSchurCompl
Sparse representation of Schur complement matrix at this level.
virtual void v_CoeffsFwdTransform(const Array< OneD, NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)
GlobalSysSolnType GetGlobalSysSolnType() const
Return the associated solution type.
DNekScalBlkMatSharedPtr m_schurCompl
Block Schur complement matrix.
std::weak_ptr< AssemblyMap > m_locToGloMap
Local to global map.
void SetupTopLevel(const std::shared_ptr< AssemblyMap > &locToGloMap)
Set up the storage for the Schur complement or the top level of the multi-level Schur complement.
Array< OneD, NekDouble > m_wsp
Workspace array for matrix multiplication.
DNekScalBlkMatSharedPtr m_BinvD
Block matrix.
DNekScalBlkMatSharedPtr m_C
Block matrix.
DNekScalBlkMatSharedPtr m_invD
Block matrix.
StdRegions::MatrixType GetMatrixType() const
Return the matrix type.
static void Dgemv(const char &trans, const int &m, const int &n, const double &alpha, const double *a, const int &lda, const double *x, const int &incx, const double &beta, double *y, const int &incy)
BLAS level 2: Matrix vector multiply y = A x where A[m x n].
Definition: Blas.hpp:265
static double Ddot(const int &n, const double *x, const int &incx, const double *y, const int &incy)
BLAS level 1: output = .
Definition: Blas.hpp:197
std::shared_ptr< GlobalLinSysStaticCond > GlobalLinSysStaticCondSharedPtr
GlobalLinSysFactory & GetGlobalLinSysFactory()
std::shared_ptr< GlobalLinSysIterativeStaticCond > GlobalLinSysIterativeStaticCondSharedPtr
std::shared_ptr< Preconditioner > PreconditionerSharedPtr
Definition: GlobalLinSys.h:60
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:52
static const NekDouble kNekUnsetDouble
static const NekDouble kNekZeroTol
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
std::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
Array< OneD, NekDouble > BCOEntryType
std::shared_ptr< DNekScalBlkMat > DNekScalBlkMatSharedPtr
Definition: NekTypeDefs.hpp:73
std::map< CoordType, BCOEntryType > BCOMatType
double NekDouble
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition: Vmath.cpp:225