Nektar++
GlobalLinSysXxtFull.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: GlobalLinSysXxtFull.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: GlobalLinSysXxtFull definition
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
36 #include <MultiRegions/ExpList.h>
38 
39 using namespace std;
40 
41 namespace Nektar
42 {
43 namespace MultiRegions
44 {
45 /**
46  * @class GlobalLinSysXxtFull
47  */
48 
49 /**
50  * Registers the class with the Factory.
51  */
52 string GlobalLinSysXxtFull::className =
54  "XxtFull", GlobalLinSysXxtFull::create, "Xxt Full Matrix.");
55 
56 /// Constructor for full direct matrix solve.
57 GlobalLinSysXxtFull::GlobalLinSysXxtFull(
58  const GlobalLinSysKey &pLinSysKey, const std::weak_ptr<ExpList> &pExp,
59  const std::shared_ptr<AssemblyMap> &pLocToGloMap)
60  : GlobalLinSys(pLinSysKey, pExp, pLocToGloMap),
61  GlobalLinSysXxt(pLinSysKey, pExp, pLocToGloMap)
62 {
63 
65  "This routine should only be used when using a Full XXT"
66  " matrix solve");
67 
68  CreateMap(pLocToGloMap);
69  AssembleMatrixArrays(pLocToGloMap);
70 }
71 
73 {
74 }
75 
76 /**
77  * Solve the linear system using a full global matrix system.
78  */
80  const Array<OneD, const NekDouble> &pLocInput,
81  Array<OneD, NekDouble> &pLocOutput,
82  const AssemblyMapSharedPtr &pLocToGloMap,
83  const Array<OneD, const NekDouble> &pDirForcing)
84 {
85  bool dirForcCalculated = (bool)pDirForcing.size();
86  int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
87  int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
88  int nLocDofs = pLocToGloMap->GetNumLocalCoeffs();
89 
90  Array<OneD, NekDouble> tmp(nLocDofs);
91  Array<OneD, NekDouble> tmp1(nLocDofs);
92  Array<OneD, NekDouble> global(nGlobDofs, 0.0);
93 
94  if (nDirDofs)
95  {
96  // calculate the dirichlet forcing
97  if (dirForcCalculated)
98  {
99  // assume pDirForcing is in local space
100  ASSERTL0(
101  pDirForcing.size() >= nLocDofs,
102  "DirForcing is not of sufficient size. Is it in local space?");
103  Vmath::Vsub(nLocDofs, pLocInput, 1, pDirForcing, 1, tmp1, 1);
104  }
105  else
106  {
107  // Calculate the dirichlet forcing and substract it
108  // from the rhs
109  m_expList.lock()->GeneralMatrixOp(m_linSysKey, pLocOutput, tmp);
110 
111  Vmath::Vsub(nLocDofs, pLocInput, 1, tmp, 1, tmp1, 1);
112  }
113 
114  pLocToGloMap->Assemble(tmp1, tmp);
115 
116  SolveLinearSystem(pLocToGloMap->GetNumLocalCoeffs(), tmp, global,
117  pLocToGloMap);
118  pLocToGloMap->GlobalToLocal(global, tmp);
119 
120  // Add back initial and boundary condition
121  Vmath::Vadd(nLocDofs, tmp, 1, pLocOutput, 1, pLocOutput, 1);
122  }
123  else
124  {
125  pLocToGloMap->Assemble(pLocInput, tmp);
126  SolveLinearSystem(pLocToGloMap->GetNumLocalCoeffs(), tmp, global,
127  pLocToGloMap);
128  pLocToGloMap->GlobalToLocal(global, pLocOutput);
129  }
130 }
131 
132 /**
133  * Create the inverse multiplicity map.
134  * @param locToGloMap Local to global mapping information.
135  */
137  const std::shared_ptr<AssemblyMap> &pLocToGloMap)
138 {
139  const Array<OneD, const int> &vMap = pLocToGloMap->GetLocalToGlobalMap();
140  unsigned int nGlo = pLocToGloMap->GetNumGlobalCoeffs();
141  unsigned int nEntries = pLocToGloMap->GetNumLocalCoeffs();
142  unsigned int i;
143 
144  // Count the multiplicity of each global DOF on this process
145  Array<OneD, NekDouble> vCounts(nGlo, 0.0);
146  for (i = 0; i < nEntries; ++i)
147  {
148  vCounts[vMap[i]] += 1.0;
149  }
150 
151  // Get universal multiplicity by globally assembling counts
152  pLocToGloMap->UniversalAssemble(vCounts);
153 
154  // Construct a map of 1/multiplicity for use in XXT solve
156  for (i = 0; i < nEntries; ++i)
157  {
158  m_locToGloSignMult[i] = 1.0 / vCounts[vMap[i]];
159  }
160 
161  m_map = pLocToGloMap->GetLocalToGlobalMap();
162 }
163 
164 /**
165  * Construct the local matrix row index, column index and value index
166  * arrays and initialize the XXT data structure with this information.
167  * @param locToGloMap Local to global mapping information.
168  */
170  const std::shared_ptr<AssemblyMap> &pLocToGloMap)
171 {
172  ExpListSharedPtr vExp = m_expList.lock();
173  unsigned int nElmt = vExp->GetNumElmts();
174  DNekScalMatSharedPtr loc_mat;
175  unsigned int iCount = 0;
176  unsigned int rCount = 0;
177  unsigned int nRows = 0;
178  unsigned int nEntries = 0;
179  unsigned int numDirBnd = pLocToGloMap->GetNumGlobalDirBndCoeffs();
180  unsigned int nLocal = pLocToGloMap->GetNumLocalCoeffs();
181  const Array<OneD, NekDouble> &vMapSign =
182  pLocToGloMap->GetLocalToGlobalSign();
183  bool doSign = pLocToGloMap->GetSignChange();
184  unsigned int i = 0, j = 0, k = 0, n = 0;
185  int gid1;
186  Array<OneD, unsigned int> vSizes(nElmt);
187 
188  // First construct a map of the number of local DOFs in each block
189  // and the number of matrix entries for each block
190 
191  // Dimension of matrix is just the linear vertex space
194  {
195  for (n = 0; n < nElmt; ++n)
196  {
197  vSizes[n] = vExp->GetExp(n)->GetNverts();
198  nEntries += vSizes[n] * vSizes[n];
199  }
200  }
201  else
202  {
203  for (n = 0; n < nElmt; ++n)
204  {
205  vSizes[n] = vExp->GetExp(n)->GetNcoeffs();
206  nEntries += vSizes[n] * vSizes[n];
207  }
208  }
209 
210  // Set up i-index, j-index and value arrays
211  m_Ai = Array<OneD, unsigned int>(nEntries);
212  m_Aj = Array<OneD, unsigned int>(nEntries);
213  m_Ar = Array<OneD, double>(nEntries, 0.0);
214 
215  // Set up the universal ID array for XXT
216  Array<OneD, unsigned long> vId(nLocal);
217 
218  // Loop over each elemental block, extract matrix indices and value
219  // and set the universal ID array
220  for (n = iCount = 0; n < nElmt; ++n)
221  {
222  loc_mat = GetBlock(n);
223  nRows = loc_mat->GetRows();
224 
225  for (i = 0; i < nRows; ++i)
226  {
227  gid1 = pLocToGloMap->GetLocalToGlobalMap(iCount + i);
228  for (j = 0; j < nRows; ++j)
229  {
230  k = rCount + i * vSizes[n] + j;
231  m_Ai[k] = iCount + i;
232  m_Aj[k] = iCount + j;
233  m_Ar[k] = (*loc_mat)(i, j);
234  if (doSign)
235  {
236  m_Ar[k] *= vMapSign[iCount + i] * vMapSign[iCount + j];
237  }
238  }
239 
240  // Dirichlet DOFs are not included in the solve, so we set
241  // these to the special XXT id=0.
242  if (gid1 < numDirBnd)
243  {
244  vId[iCount + i] = 0;
245  }
246  else
247  {
248  vId[iCount + i] = pLocToGloMap->GetGlobalToUniversalMap(gid1);
249  }
250  }
251  iCount += vSizes[n];
252  rCount += vSizes[n] * vSizes[n];
253  }
254 
255  // Set up XXT and output some stats
256  LibUtilities::CommSharedPtr vComm = pLocToGloMap->GetComm();
257  m_crsData = Xxt::Init(nLocal, vId, m_Ai, m_Aj, m_Ar, vComm);
258  if (m_verbose)
259  {
261  }
262 }
263 } // namespace MultiRegions
264 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249
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:124
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:192
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:122
DNekScalMatSharedPtr GetBlock(unsigned int n)
Definition: GlobalLinSys.h:211
GlobalSysSolnType GetGlobalSysSolnType() const
Return the associated solution type.
virtual void v_Solve(const Array< OneD, const NekDouble > &in, Array< OneD, NekDouble > &out, const AssemblyMapSharedPtr &locToGloMap, const Array< OneD, const NekDouble > &dirForcing=NullNekDouble1DArray) override
Solve the linear system for given input and output vectors using a specified local to global map.
void CreateMap(const std::shared_ptr< AssemblyMap > &pLocToGloMap)
void AssembleMatrixArrays(const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Array< OneD, NekDouble > m_locToGloSignMult
Array< OneD, unsigned int > m_Aj
Array< OneD, unsigned int > m_Ai
StdRegions::MatrixType GetMatrixType() const
Return the matrix type.
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:54
GlobalLinSysFactory & GetGlobalLinSysFactory()
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:51
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
std::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
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
void nektar_crs_stats(struct crs_data *data)
static struct crs_data * Init(unsigned int pRank, const Nektar::Array< OneD, unsigned long > pId, const Nektar::Array< OneD, unsigned int > pAi, const Nektar::Array< OneD, unsigned int > pAj, const Nektar::Array< OneD, NekDouble > pAr, const LibUtilities::CommSharedPtr &pComm)
Initialise the matrix-solve.
Definition: Xxt.hpp:161