Nektar++
GlobalLinSysDirectFull.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 #include <MultiRegions/ExpList.h>
38 
39 namespace Nektar
40 {
41  namespace MultiRegions
42  {
43  /**
44  * @class GlobalLinSysDirect
45  *
46  * Consider a linear system
47  * \f$\boldsymbol{M\hat{u}}_g=\boldsymbol{\hat{f}}\f$
48  * to be solved, where \f$\boldsymbol{M}\f$ is a matrix of type
49  * specified by \a mkey. This function assembles the global system
50  * matrix \f$\boldsymbol{M}\f$ out of the elemental submatrices
51  * \f$\boldsymbol{M}^e\f$. This is equivalent to:
52  * \f[ \boldsymbol{M}=\boldsymbol{\mathcal{A}}^T
53  * \underline{\boldsymbol{M}}^e\boldsymbol{\mathcal{A}}.\f]
54  * where the matrix \f$\boldsymbol{\mathcal{A}}\f$ is a sparse
55  * permutation matrix of size \f$N_{\mathrm{eof}}\times
56  * N_{\mathrm{dof}}\f$. However, due to the size and sparsity of the
57  * matrix \f$\boldsymbol{\mathcal{A}}\f$, it is more efficient to
58  * assemble the global matrix using the mapping array \a
59  * map\f$[e][i]\f$ contained in the input argument \a locToGloMap.
60  * The global assembly is then evaluated as:
61  * \f[ \boldsymbol{M}\left[\mathrm{\texttt{map}}[e][i]\right]
62  * \left[\mathrm{\texttt{map}}[e][j]\right]
63  * =\mathrm{\texttt{sign}}[e][i]\cdot
64  * \mathrm{\texttt{sign}}[e][j] \cdot\boldsymbol{M}^e[i][j]\f]
65  * where the values \a sign\f$[e][i]\f$ ensure the correct connectivity.
66  */
67 
68  /**
69  * Registers the class with the Factory.
70  */
73  "DirectFull",
75  "Direct Full.");
76 
77 
78  /// Constructor for full direct matrix solve.
80  const GlobalLinSysKey &pLinSysKey,
81  const boost::weak_ptr<ExpList> &pExp,
82  const boost::shared_ptr<AssemblyMap>
83  &pLocToGloMap)
84  : GlobalLinSys(pLinSysKey, pExp, pLocToGloMap),
85  GlobalLinSysDirect(pLinSysKey, pExp, pLocToGloMap)
86  {
87 
89  "This routine should only be used when using a Full Direct"
90  " matrix solve");
91  ASSERTL1(pExp.lock()->GetComm()->GetSize() == 1,
92  "Direct full matrix solve can only be used in serial.");
93 
94  AssembleFullMatrix(pLocToGloMap);
95  }
96 
97 
99  {
100 
101  }
102 
103 
104  /**
105  * Solve the linear system using a full global matrix system.
106  */
108  const Array<OneD, const NekDouble> &pInput,
109  Array<OneD, NekDouble> &pOutput,
110  const AssemblyMapSharedPtr &pLocToGloMap,
111  const Array<OneD, const NekDouble> &pDirForcing)
112  {
113  bool dirForcCalculated = (bool) pDirForcing.num_elements();
114  int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
115  int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
116  Array<OneD, NekDouble> tmp(nGlobDofs);
117 
118  if(nDirDofs)
119  {
120  // calculate the dirichlet forcing
121  if(dirForcCalculated)
122  {
123  Vmath::Vsub(nGlobDofs,
124  pInput.get(), 1,
125  pDirForcing.get(), 1,
126  tmp.get(), 1);
127  }
128  else
129  {
130  // Calculate Dirichlet forcing and subtract it from the rhs
131  m_expList.lock()->GeneralMatrixOp(
132  m_linSysKey, pOutput, tmp, eGlobal);
133 
134  Vmath::Vsub(nGlobDofs,
135  pInput.get(), 1,
136  tmp.get(), 1,
137  tmp.get(), 1);
138  }
139 
140  Array<OneD, NekDouble> out(nGlobDofs,0.0);
141  SolveLinearSystem(nGlobDofs, tmp, out, pLocToGloMap, nDirDofs);
142  Vmath::Vadd(nGlobDofs-nDirDofs, &out [nDirDofs], 1,
143  &pOutput[nDirDofs], 1, &pOutput[nDirDofs], 1);
144  }
145  else
146  {
147  SolveLinearSystem(nDirDofs, pInput, pOutput, pLocToGloMap);
148  }
149  }
150 
151 
152  /**
153  * Assemble a full matrix from the block matrix stored in
154  * #m_blkMatrices and the given local to global mapping information.
155  * @param locToGloMap Local to global mapping information.
156  */
158  const AssemblyMapSharedPtr& pLocToGloMap)
159  {
160  int i,j,n,cnt,gid1,gid2;
161  NekDouble sign1,sign2,value;
162  int totDofs = pLocToGloMap->GetNumGlobalCoeffs();
163  int NumDirBCs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
164 
165  unsigned int rows = totDofs - NumDirBCs;
166  unsigned int cols = totDofs - NumDirBCs;
167  NekDouble zero = 0.0;
168 
169  DNekMatSharedPtr Gmat;
170  int bwidth = pLocToGloMap->GetFullSystemBandWidth();
171  MatrixStorage matStorage;
172 
173  switch(m_linSysKey.GetMatrixType())
174  {
175  // case for all symmetric matices
176  case StdRegions::eMass:
180  {
181  if( (2*(bwidth+1)) < rows)
182  {
185  ::AllocateSharedPtr(rows, cols, zero,
186  matStorage,
187  bwidth, bwidth);
188  }
189  else
190  {
191  matStorage = ePOSITIVE_DEFINITE_SYMMETRIC;
193  ::AllocateSharedPtr(rows, cols, zero,
194  matStorage);
195  }
196  break;
197  }
200  {
201  matStorage = eFULL;
203  ::AllocateSharedPtr(rows, cols, zero,
204  matStorage);
205  break;
206  }
207  default:
208  {
209  NEKERROR(ErrorUtil::efatal, "Add MatrixType to switch "
210  "statement");
211  }
212  }
213 
214  // fill global matrix
215  DNekScalMatSharedPtr loc_mat;
216 
217  int loc_lda;
218  for(n = cnt = 0; n < m_expList.lock()->GetNumElmts(); ++n)
219  {
220  loc_mat = GetBlock(m_expList.lock()->GetOffset_Elmt_Id(n));
221  loc_lda = loc_mat->GetRows();
222 
223  for(i = 0; i < loc_lda; ++i)
224  {
225  gid1 = pLocToGloMap->GetLocalToGlobalMap(cnt + i)-NumDirBCs;
226  sign1 = pLocToGloMap->GetLocalToGlobalSign(cnt + i);
227  if(gid1 >= 0)
228  {
229  for(j = 0; j < loc_lda; ++j)
230  {
231  gid2 = pLocToGloMap->GetLocalToGlobalMap(cnt + j)
232  - NumDirBCs;
233  sign2 = pLocToGloMap->GetLocalToGlobalSign(cnt + j);
234  if(gid2 >= 0)
235  {
236  // When global matrix is symmetric,
237  // only add the value for the upper
238  // triangular part in order to avoid
239  // entries to be entered twice
240  if((matStorage == eFULL)||(gid2 >= gid1))
241  {
242  value = Gmat->GetValue(gid1,gid2)
243  + sign1*sign2*(*loc_mat)(i,j);
244  Gmat->SetValue(gid1,gid2,value);
245  }
246  }
247  }
248  }
249  }
250  cnt += loc_lda;
251  }
252 
253  if(rows)
254  {
257  }
258  }
259  }
260 }
GlobalSysSolnType GetGlobalSysSolnType() const
Return the associated solution type.
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:158
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
boost::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:53
DNekLinSysSharedPtr m_linSys
Basic linear system object.
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:193
static std::string className
Name of class.
static GlobalLinSysSharedPtr create(const GlobalLinSysKey &pLinSysKey, const boost::weak_ptr< ExpList > &pExpList, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Creates an instance of this class.
boost::shared_ptr< DNekMat > DNekMatSharedPtr
Definition: NekTypeDefs.hpp:70
boost::shared_ptr< DNekScalMat > DNekScalMatSharedPtr
Global coefficients.
GlobalLinSysDirectFull(const GlobalLinSysKey &pLinSysKey, const boost::weak_ptr< ExpList > &pExpList, const boost::shared_ptr< AssemblyMap > &pLocToGloMap)
Constructor for full direct matrix solve.
DNekScalMatSharedPtr GetBlock(unsigned int n)
Definition: GlobalLinSys.h:214
double NekDouble
Describe a linear system.
PointerWrapper
Specifies if the pointer passed to a NekMatrix or NekVector is copied into an internal representation...
StdRegions::MatrixType GetMatrixType() const
Return the matrix type.
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:123
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:70
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...
void AssembleFullMatrix(const boost::shared_ptr< AssemblyMap > &locToGloMap)
GlobalLinSysFactory & GetGlobalLinSysFactory()
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:191
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
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:125