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