Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GlobalLinSysPETScFull.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File GlobalLinSysPETScFull.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: GlobalLinSysPETScFull definition
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 #include <MultiRegions/ExpList.h>
38 
39 #include "petscao.h"
40 #include "petscis.h"
41 
42 namespace Nektar
43 {
44  namespace MultiRegions
45  {
46  /**
47  * @class GlobalLinSysPETScFull
48  */
49 
50  /**
51  * Registers the class with the Factory.
52  */
55  "PETScFull",
57  "PETSc Full Matrix.");
58 
59 
60  /// Constructor for full direct matrix solve.
62  const GlobalLinSysKey &pLinSysKey,
63  const boost::weak_ptr<ExpList> &pExp,
64  const boost::shared_ptr<AssemblyMap> &pLocToGloMap)
65  : GlobalLinSys (pLinSysKey, pExp, pLocToGloMap),
66  GlobalLinSysPETSc(pLinSysKey, pExp, pLocToGloMap)
67  {
68  const int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
69 
70  int i, j, n, cnt, gid1, gid2, loc_lda;
71  NekDouble sign1, sign2, value;
72  DNekScalMatSharedPtr loc_mat;
73 
74  // CALCULATE REORDERING MAPPING
75  CalculateReordering(pLocToGloMap->GetGlobalToUniversalMap(),
76  pLocToGloMap->GetGlobalToUniversalMapUnique(),
77  pLocToGloMap);
78 
79  // SET UP VECTORS AND MATRIX
80  SetUpMatVec();
81 
82  // POPULATE MATRIX
83  for(n = cnt = 0; n < m_expList.lock()->GetNumElmts(); ++n)
84  {
85  loc_mat = GetBlock(m_expList.lock()->GetOffset_Elmt_Id(n));
86  loc_lda = loc_mat->GetRows();
87 
88  for(i = 0; i < loc_lda; ++i)
89  {
90  gid1 = pLocToGloMap->GetLocalToGlobalMap(cnt+i) - nDirDofs;
91  sign1 = pLocToGloMap->GetLocalToGlobalSign(cnt + i);
92  if(gid1 >= 0)
93  {
94  int gid1ro = m_reorderedMap[gid1];
95  for(j = 0; j < loc_lda; ++j)
96  {
97  gid2 = pLocToGloMap->GetLocalToGlobalMap(cnt + j)
98  - nDirDofs;
99  sign2 = pLocToGloMap->GetLocalToGlobalSign(cnt + j);
100  if(gid2 >= 0)
101  {
102  int gid2ro = m_reorderedMap[gid2];
103  value = sign1*sign2*(*loc_mat)(i,j);
104  MatSetValue(
105  m_matrix, gid1ro, gid2ro, value, ADD_VALUES);
106  }
107  }
108  }
109  }
110  cnt += loc_lda;
111  }
112 
113  // ASSEMBLE MATRIX
114  MatAssemblyBegin(m_matrix, MAT_FINAL_ASSEMBLY);
115  MatAssemblyEnd (m_matrix, MAT_FINAL_ASSEMBLY);
116 
117  // SET UP SCATTER OBJECTS
118  SetUpScatter();
119 
120  // CONSTRUCT KSP OBJECT
121  SetUpSolver(pLocToGloMap->GetIterativeTolerance());
122  }
123 
124 
126  {
127 
128  }
129 
130 
131  /**
132  * Solve the linear system using a full global matrix system.
133  */
135  const Array<OneD, const NekDouble> &pInput,
136  Array<OneD, NekDouble> &pOutput,
137  const AssemblyMapSharedPtr &pLocToGloMap,
138  const Array<OneD, const NekDouble> &pDirForcing)
139  {
140  bool dirForcCalculated = (bool) pDirForcing.num_elements();
141  int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
142  int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
143  Array<OneD, NekDouble> tmp(nGlobDofs), tmp2;
144 
145  int nDirTotal = nDirDofs;
146  m_expList.lock()->GetComm()->GetRowComm()->AllReduce(
147  nDirTotal, LibUtilities::ReduceSum);
148 
149  if(nDirTotal)
150  {
151  // calculate the dirichlet forcing
152  if(dirForcCalculated)
153  {
154  Vmath::Vsub(nGlobDofs,
155  pInput.get(), 1,
156  pDirForcing.get(), 1,
157  tmp.get(), 1);
158  }
159  else
160  {
161  // Calculate the dirichlet forcing and substract it
162  // from the rhs
163  m_expList.lock()->GeneralMatrixOp(
164  m_linSysKey, pOutput, tmp, eGlobal);
165 
166  Vmath::Vsub(nGlobDofs,
167  pInput.get(), 1,
168  tmp.get(), 1,
169  tmp.get(), 1);
170  }
171 
172  Array<OneD, NekDouble> out(nGlobDofs,0.0);
173  SolveLinearSystem(nGlobDofs, tmp, out, pLocToGloMap, nDirDofs);
174  Vmath::Vadd(nGlobDofs-nDirDofs, &out [nDirDofs], 1,
175  &pOutput[nDirDofs], 1, &pOutput[nDirDofs], 1);
176  }
177  else
178  {
179  SolveLinearSystem(nDirDofs, pInput, pOutput, pLocToGloMap);
180  }
181  }
182  }
183 }