Nektar++
PreconditionerLinear.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File Preconditioner.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: Preconditioner definition
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
41 
42 #ifdef NEKTAR_USING_PETSC
44 #endif
45 
46 #include <LocalRegions/MatrixKey.h>
47 #include <math.h>
48 
49 using namespace std;
50 
51 namespace Nektar
52 {
53  namespace MultiRegions
54  {
55  /**
56  * Registers the class with the Factory.
57  */
58 
59  string PreconditionerLinear::className1
61  "FullLinearSpace",
62  PreconditionerLinear::create,
63  "Full Linear space inverse Preconditioning");
64 
65  std::string PreconditionerLinear::solveType =
66  LibUtilities::SessionReader::RegisterDefaultSolverInfo(
67  "LinearPreconSolver",
68  "Xxt");
69  std::string PreconditionerLinear::solveTypeIds[] = {
70  LibUtilities::SessionReader::RegisterEnumValue(
71  "LinearPreconSolver",
72  "PETSc",
74  LibUtilities::SessionReader::RegisterEnumValue(
75  "LinearPreconSolver",
76  "Xxt",
78  };
79 
80  /**
81  * @class PreconditionerLinear
82  *
83  * This class implements preconditioning for the conjugate
84  * gradient matrix solver.
85  */
86 
87  PreconditionerLinear::PreconditionerLinear(
88  const std::shared_ptr<GlobalLinSys> &plinsys,
89  const AssemblyMapSharedPtr &pLocToGloMap)
90  : Preconditioner(plinsys, pLocToGloMap)
91  {
92  }
93 
95  {
96  }
97 
99  {
100  GlobalSysSolnType sType = m_locToGloMap.lock()->GetGlobalSysSolnType();
101  ASSERTL0(sType == eIterativeStaticCond || sType == ePETScStaticCond,
102  "This type of preconditioning is not implemented "
103  "for this solver");
104 
105  std::shared_ptr<MultiRegions::ExpList>
106  expList=((m_linsys.lock())->GetLocMat()).lock();
107 
109  expList->GetSession()->GetSolverInfoAsEnum<LinearPreconSolver>(
110  "LinearPreconSolver");
111 
112  GlobalSysSolnType linSolveType;
113 
114  switch(solveType)
115  {
116  case eLinearPreconPETSc:
117  {
118  linSolveType = ePETScFullMatrix;
119 #ifndef NEKTAR_USING_PETSC
121  "Nektar++ has not been compiled with "
122  "PETSc support.");
123 #endif
124  break;
125  }
126  case eLinearPreconXxt:
127  default:
128  {
129  linSolveType = eXxtFullMatrix;
130  break;
131  }
132  }
133 
134  m_vertLocToGloMap = m_locToGloMap.lock()->LinearSpaceMap(*expList, linSolveType);
135 
136  // Generate linear solve system.
137  StdRegions::MatrixType mType =
138  m_linsys.lock()->GetKey().GetMatrixType() == StdRegions::eMass ?
141 
142  GlobalLinSysKey preconKey(mType, m_vertLocToGloMap, (m_linsys.lock())->GetKey().GetConstFactors());
143 
144  switch(solveType)
145  {
146  case eLinearPreconXxt:
147  {
149  AllocateSharedPtr(preconKey,expList,m_vertLocToGloMap);
150  break;
151  }
152  case eLinearPreconPETSc:
153  {
154 #ifdef NEKTAR_USING_PETSC
156  AllocateSharedPtr(preconKey,expList,m_vertLocToGloMap);
157 #else
158  ASSERTL0(false, "Nektar++ has not been compiled with "
159  "PETSc support.");
160 #endif
161  }
162  }
163  }
164 
165  /**
166  *
167  */
169  const Array<OneD, NekDouble>& pInput,
170  Array<OneD, NekDouble>& pOutput)
171  {
174  }
175 
176  /**
177  *
178  */
180  const Array<OneD, NekDouble>& pInput,
181  Array<OneD, NekDouble>& pOutput,
182  const Array<OneD, NekDouble>& pNonVertOutput,
183  Array<OneD, NekDouble>& pVertForce)
184  {
185  GlobalSysSolnType solvertype=m_locToGloMap.lock()->GetGlobalSysSolnType();
186  switch(solvertype)
187  {
190  {
191  int i,val;
192  int nloc = m_vertLocToGloMap->GetNumLocalCoeffs();
193  int nglo = m_vertLocToGloMap->GetNumGlobalCoeffs();
194  // mapping from full space to vertices
195  Array<OneD, int> LocToGloBnd = m_vertLocToGloMap->GetLocalToGlobalBndMap();
196 
197  // Global to local for linear solver (different from above)
198  Array<OneD, int> LocToGlo = m_vertLocToGloMap->GetLocalToGlobalMap();
199 
200  // number of Dir coeffs in from full problem
201  int nDirFull = m_locToGloMap.lock()->GetNumGlobalDirBndCoeffs();
202 
203  Array<OneD,NekDouble> In(nglo,0.0);
204  Array<OneD,NekDouble> Out(nglo,0.0);
205 
206  // Gather rhs
207  for(i = 0; i < nloc; ++i)
208  {
209  val = LocToGloBnd[i];
210  if(val >= nDirFull)
211  {
212  In[LocToGlo[i]] = pInput[val-nDirFull];
213  }
214  }
215 
216  // Do solve without enforcing any boundary conditions.
217  m_vertLinsys->SolveLinearSystem(
218  m_vertLocToGloMap->GetNumGlobalCoeffs(),
219  In,Out,m_vertLocToGloMap,
220  m_vertLocToGloMap->GetNumGlobalDirBndCoeffs());
221 
222 
223  if(pNonVertOutput != NullNekDouble1DArray)
224  {
225  ASSERTL1(pNonVertOutput.num_elements() >= pOutput.num_elements(),"Non Vert output is not of sufficient length");
226  Vmath::Vcopy(pOutput.num_elements(),pNonVertOutput,1,pOutput,1);
227  }
228  else
229  {
230  //Copy input to output as a unit preconditioner on
231  //any other value
232  Vmath::Vcopy(pInput.num_elements(),pInput,1,pOutput,1);
233  }
234 
235  if(pVertForce != NullNekDouble1DArray)
236  {
237  Vmath::Zero(pVertForce.num_elements(),pVertForce,1);
238  // Scatter back soln from linear solve
239  for(i = 0; i < nloc; ++i)
240  {
241  val = LocToGloBnd[i];
242  if(val >= nDirFull)
243  {
244  pOutput[val-nDirFull] = Out[LocToGlo[i]];
245  // copy vertex forcing into this vector
246  pVertForce[val-nDirFull] = In[LocToGlo[i]];
247  }
248  }
249  }
250  else
251  {
252  // Scatter back soln from linear solve
253  for(i = 0; i < nloc; ++i)
254  {
255  val = LocToGloBnd[i];
256  if(val >= nDirFull)
257  {
258  pOutput[val-nDirFull] = Out[LocToGlo[i]];
259  }
260  }
261  }
262  }
263  break;
264  default:
265  ASSERTL0(0,"Unsupported solver type");
266  break;
267  }
268  }
269  }
270 }
271 
272 
273 
274 
275 
276 
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
virtual void v_DoPreconditioner(const Array< OneD, NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)
Apply a preconditioner to the conjugate gradient method.
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
static Array< OneD, NekDouble > NullNekDouble1DArray
const std::weak_ptr< GlobalLinSys > m_linsys
virtual void v_DoPreconditionerWithNonVertOutput(const Array< OneD, NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const Array< OneD, NekDouble > &pNonVertOutput, Array< OneD, NekDouble > &pVertForce)
Apply a preconditioner to the conjugate gradient method with an output for non-vertex degrees of free...
PreconFactory & GetPreconFactory()
STL namespace.
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:52
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
Describe a linear system.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:376
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:250
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1064
std::weak_ptr< AssemblyMap > m_locToGloMap
std::shared_ptr< AssemblyMap > m_vertLocToGloMap