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 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 }