Nektar++
Loading...
Searching...
No Matches
PreconditionerLOR.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: PreconditionerLOR.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: PreconditionerLOR definition
32//
33///////////////////////////////////////////////////////////////////////////////
34
42
43#ifdef NEKTAR_USE_MPI
45#endif
46
47#ifdef NEKTAR_USING_PETSC
49#endif
50
51#include <cmath>
52
54{
55/**
56 * Registers the class with the Factory.
57 */
60 "LOR Preconditioning");
61
62/**
63 * @class PreconditionerLOR
64 *
65 * This class implements LOR preconditioning for the conjugate gradient matrix
66 * solver.
67 */
69 const std::shared_ptr<GlobalLinSys> &plinsys,
70 const AssemblyMapSharedPtr &pLocToGloMap)
71 : Preconditioner(plinsys, pLocToGloMap), m_verboseIter(false)
72{
73}
74
78
80{
81 GlobalSysSolnType solvertype = m_locToGloMap.lock()->GetGlobalSysSolnType();
82
83 ASSERTL0(solvertype == eIterativeFull, "Only works for full iterative");
84
85 // Grab explist
86 auto expList = ((m_linsys.lock())->GetLocMat()).lock();
87
88 std::shared_ptr<LibUtilities::SessionReader> session =
89 expList->GetSession();
90
91 m_nsplit = expList->GetExp(0)->GetBasisNumModes(0) - 1;
92
93 std::string var = m_locToGloMap.lock()->GetVariable();
94
95 m_equiSpaced = false;
96 if (session->DefinesGlobalSysSolnInfo(var, "LORPointDistribution"))
97 {
98 if (boost::iequals("GLL", session->GetGlobalSysSolnInfo(
99 var, "LORPointDistribution")))
100 {
101 m_equiSpaced = false;
102 }
103 else if (boost::iequals("Equispaced", session->GetGlobalSysSolnInfo(
104 var, "LORPointDistribution")))
105 {
106 m_equiSpaced = true;
107 }
108 }
109
110 m_useSimplex = false;
111 if (session->DefinesGlobalSysSolnInfo(var, "LORSimplex"))
112 {
113 if (boost::iequals("True",
114 session->GetGlobalSysSolnInfo(var, "LORSimplex")))
115 {
116 m_useSimplex = true;
117 }
118 }
119
120 // Setup session !m_equispaced since true means GLL distribution
121 std::map<int, std::pair<int, std::vector<int>>> coeffmap;
122 SpatialDomains::LinearMeshGraph linear(expList->GetGraph());
124 m_lor_graph = linear.GetLinearGraph();
125
126 std::string preconType;
127
128 if (session->DefinesGlobalSysSolnInfo(var, "LORSolverType"))
129 {
130 m_slvType = session->GetGlobalSysSolnInfo(var, "LORSolverType");
131 }
132 else
133 {
134 NEKERROR(
136 "Need to define LORSolverType "
137 "(DirectFull, IterativeFull, Preconditioner, PETScFull, XxtFull)"
138 "with LOR Preconditioner");
139 }
140
141 if (session->GetComm()->GetRank() == 0)
142 {
143 std::cout << "LOR Solve Type: " << m_slvType << std::endl;
144 }
145
146 std::string LinSysIterSolver;
147 if (session->DefinesGlobalSysSolnInfo(var, "LORLinSysIterSolver"))
148 {
150 session->GetGlobalSysSolnInfo(var, "LORLinSysIterSolver");
151 }
152 else
153 {
154 LinSysIterSolver = "ConjugateGradient";
155 }
156
157 if (session->DefinesGlobalSysSolnInfo(var, "LORPreconditioner"))
158 {
159 preconType = session->GetGlobalSysSolnInfo(var, "LORPreconditioner");
160 }
161 else
162 {
163 preconType = "Diagonal";
164 }
165
166 if (boost::iequals(m_slvType, "Preconditioner"))
167 {
168 session->SetGlobalSysSolnInfo(var, "GlobalSysSoln", "IterativeFull");
169 }
170 else
171 {
172 session->SetGlobalSysSolnInfo(var, "GlobalSysSoln", m_slvType.c_str());
173
174 if (boost::iequals(m_slvType, "IterativeFull"))
175 {
177 expList->GetSession()->DefinesCmdLineArgument("verbose");
178 }
179 }
180
181 session->SetGlobalSysSolnInfo(var, "Preconditioner", preconType.c_str());
182
183 // setup Fixed iteration if difined in GlobalSysSoln section
184 std::string prevFixedIter;
185 if (session->DefinesGlobalSysSolnInfo(var, "LORFixedIterations"))
186 {
187
188 if (session->DefinesGlobalSysSolnInfo(var, "NekLinSysFixedIterations"))
189 {
190 prevFixedIter =
191 session->GetGlobalSysSolnInfo(var, "NekLinSysFixedIterations");
192 }
193
194 session->SetGlobalSysSolnInfo(
195 var, "NekLinSysFixedIterations",
196 session->GetGlobalSysSolnInfo(var, "LORFixedIterations"));
197 }
198
199 std::string prevIterTol;
200 if (session->DefinesGlobalSysSolnInfo(var, "LORIterativeTolerance"))
201 {
202
203 if (session->DefinesGlobalSysSolnInfo(var, "IterativeSolverTolerance"))
204 {
205 prevIterTol =
206 session->GetGlobalSysSolnInfo(var, "IterativeSolverTolerance");
207 }
208
209 session->SetGlobalSysSolnInfo(
210 var, "IterativeSolverTolerance",
211 session->GetGlobalSysSolnInfo(var, "LORIterativeTolerance"));
212 }
213
214 session->SetGlobalSysSolnInfo(var, "LinSysIterSolver",
215 LinSysIterSolver.c_str());
216
217 // Create contfield
219 session, m_lor_graph, var);
220
221 // Set up high-order to LOR map
222 m_ho2lor = Array<OneD, int>(m_lor_field->GetNcoeffs());
223 std::map<int, int> offset;
224
225 // check if the coeffmap has been setup correctly in the LOR space
226 for (int e = 0; e < expList->GetNumElmts(); ++e)
227 {
228 offset[expList->GetExp(e)->GetGeom()->GetGlobalID()] =
229 expList->GetCoeff_Offset(e);
230 }
231
232 int cnt = 0;
233 for (int e = 0; e < m_lor_field->GetNumElmts(); ++e)
234 {
235 int gid = m_lor_field->GetExp(e)->GetGeom()->GetGlobalID();
236 int id = coeffmap[gid].first;
237
238 for (int i = 0; i < coeffmap[gid].second.size(); ++i)
239 {
240 m_ho2lor[cnt++] = offset[id] + coeffmap[gid].second[i];
241 }
242 }
243 ASSERTL1(cnt == m_lor_field->GetNcoeffs(),
244 "Error in setting up high order to lower order map");
245
246 // for preconditioner have a homogeneous system so need to set boundary
247 // conditions to homogeneous too
248 m_lor_field->SetBCsToHomogeneous();
249
250 GlobalLinSysKey preconKey(m_linsys.lock()->GetKey().GetMatrixType(),
251 m_lor_field->GetLocalToGlobalMap(),
252 m_linsys.lock()->GetKey().GetConstFactors());
253
254 if (boost::iequals(m_slvType, "DirectFull"))
255 {
257 preconKey, m_lor_field, m_lor_field->GetLocalToGlobalMap());
258 }
259 else if (boost::iequals(m_slvType, "XxtFull"))
260 {
261#ifdef NEKTAR_USE_MPI
263 preconKey, m_lor_field, m_lor_field->GetLocalToGlobalMap());
264#else
265 ASSERTL0(false, "Nektar++ has not been compiled with MPI support.");
266#endif
267 }
268 else if (boost::iequals(m_slvType, "IterativeFull"))
269 {
272 preconKey, m_lor_field, m_lor_field->GetLocalToGlobalMap());
273 }
274 else if (boost::iequals(m_slvType, "PETScFull"))
275 {
276#ifdef NEKTAR_USING_PETSC
278 preconKey, m_lor_field, m_lor_field->GetLocalToGlobalMap());
279#else
280 ASSERTL0(false, "Nektar++ has not been compiled with PETSc support.");
281#endif
282 }
283 else if (boost::iequals(m_slvType, "Preconditioner"))
284 {
285 AssemblyMapSharedPtr l2gmap = m_lor_field->GetLocalToGlobalMap();
286
289 preconKey, m_lor_field, l2gmap);
290
291 std::dynamic_pointer_cast<GlobalLinSysIterativeFull>(m_LORLinSys)
292 ->Initialise(l2gmap->GetNumGlobalCoeffs(), l2gmap,
293 l2gmap->GetNumGlobalDirBndCoeffs());
294 }
295 else
296 {
298 "LORSlvType must be specified as one of "
299 "DirectFull, IterativeFull, Preconditioner, PETScFull");
300 }
301
302 // reset GlobalSysSolnType
303 session->SetGlobalSysSolnInfo(var, "GlobalSysSoln",
304 GlobalSysSolnTypeMap[solvertype]);
305
306 // set fixed iterations if required;
307 if (prevFixedIter.size())
308 {
309 session->SetGlobalSysSolnInfo(var, "NekLinSysFixedIterations",
310 prevFixedIter);
311 }
312
313 // reset iterative tolerance
314 if (prevIterTol.size())
315 {
316 session->SetGlobalSysSolnInfo(var, "IterativeSolverTolerance",
317 prevIterTol);
318 }
319
321
322 // store ndir for easy access in DoPreconditioner
323 m_nDir = m_locToGloMap.lock()->GetNumGlobalDirBndCoeffs();
324}
325
326/**
327 *
328 */
330 Array<OneD, NekDouble> &pOutput,
331 const bool &isLocal)
332{
333 auto expList = ((m_linsys.lock())->GetLocMat()).lock();
334
335 int nLocal = expList->GetNcoeffs();
336 int lor_nLocal = m_lor_field->GetNcoeffs();
337
338 Array<OneD, NekDouble> tmp, solve(std::max(nLocal, lor_nLocal), 0.0);
339 Array<OneD, NekDouble> Local(nLocal);
340
341 if (isLocal)
342 {
343 // Input array is in local (non-assembled format) and to reproduce the
344 // globally assembled problem the LOR preconditioner works best if we
345 // take the averaged value so assemble and then divide by the
346 // multiplicity.
347 //
348 // Note: Not entirely clear to me why we cannot take the input value
349 m_locToGloMap.lock()->Assemble(pInput, solve);
351 m_invMultiplicity + m_nDir, 1, solve + m_nDir, 1,
352 tmp = solve + m_nDir, 1);
353 Vmath::Zero(m_nDir, solve, 1);
354 expList->GlobalToLocal(solve, Local);
355 }
356 else // recover local forcing coefficients
357 {
359 m_invMultiplicity + m_nDir, 1, pInput, 1,
360 tmp = solve + m_nDir, 1);
361 Vmath::Zero(m_nDir, solve, 1);
362
363 expList->GlobalToLocal(solve, Local);
364 }
365
369 GlobalMatrixKey key(mtype, m_locToGloMap.lock(),
370 m_linsys.lock()->GetKey().GetConstFactors());
371
372 // Transform inner product to equispaced polynomial with transpose multiply
373 expList->MultiplyByBlockMatrix(key, Local, solve, true);
374
375 // Get force for LOR system
376 Array<OneD, NekDouble> fce(lor_nLocal);
377 // makes inner system symmetric by dividing by linear mesh elemental
378 // multiplicity
379 Vmath::Vmul(nLocal, m_invLinMeshMultiplicity, 1, solve, 1, solve, 1);
380 Vmath::Gathr(lor_nLocal, solve, m_ho2lor, fce);
381
382 // solve linear sys solve for low order system
383 AssemblyMapSharedPtr l2gmap = m_lor_field->GetLocalToGlobalMap();
384 Vmath::Zero(lor_nLocal, solve, 1);
385
386 if (boost::iequals(m_slvType, "DirectFull") ||
387 boost::iequals(m_slvType, "IterativeFull") ||
388 boost::iequals(m_slvType, "XxtFull") ||
389 boost::iequals(m_slvType, "PETScFull"))
390 {
391 m_LORLinSys->SolveLinearSystem(l2gmap->GetNumGlobalCoeffs(), fce, solve,
392 l2gmap,
393 l2gmap->GetNumGlobalDirBndCoeffs());
394 }
395 else
396 {
397 std::dynamic_pointer_cast<GlobalLinSysIterativeFull>(m_LORLinSys)
398 ->DoPreconditionerFlag(fce, solve, true);
399 }
400
401 // Project solution back to p-expansion and in Lagrange space
402 Vmath::Scatr(lor_nLocal, solve, m_ho2lor, Local);
403
404 if (isLocal)
405 {
406 // Transform residual to polynomial
407
408 expList->MultiplyByBlockMatrix(key, Local, pOutput);
409
410 // enforce zero Bcs - should be done with a mask on local variables.
411 expList->LocalToGlobal(pOutput, solve);
412 Vmath::Zero(m_nDir, solve, 1);
413 expList->GlobalToLocal(solve, pOutput);
414 }
415 else
416 {
417 // Transform residual to polynomial
418 expList->MultiplyByBlockMatrix(key, Local, solve);
419 expList->LocalToGlobal(solve, Local);
420 Vmath::Vcopy(pOutput.size(), Local + m_nDir, 1, pOutput, 1);
421 }
422}
423
424/**
425 * Create inv multiplicity
426 */
428{
429 auto asmMap = m_locToGloMap.lock();
430 unsigned nGlobal = asmMap->GetNumGlobalCoeffs();
431 unsigned nLocal = asmMap->GetNumLocalCoeffs();
432
433 // Construct a mask array
435
436 const Array<OneD, const int> &map = asmMap->GetLocalToGlobalMap();
437
438 for (unsigned i = 0; i < nLocal; ++i)
439 {
440 m_invMultiplicity[map[i]] += 1.0;
441 }
442
443 asmMap->UniversalAssemble(m_invMultiplicity);
444 Vmath::Sdiv(nGlobal, 1.0, m_invMultiplicity, 1, m_invMultiplicity, 1);
445
446 auto lor_nLocal = m_ho2lor.size();
448
449 for (unsigned i = 0; i < lor_nLocal; ++i)
450 {
452 }
453 for (unsigned i = 0; i < nLocal; ++i)
454 {
456 }
457}
458
459} // namespace Nektar::MultiRegions
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
Describes a matrix with ordering defined by a local to global map.
const std::weak_ptr< GlobalLinSys > m_linsys
std::weak_ptr< AssemblyMap > m_locToGloMap
Array< OneD, int > m_ho2lor
Map from high-order node numbers to LOR representation.
MultiRegions::ContFieldSharedPtr m_lor_field
Field object for the LOR system.
PreconditionerLOR(const std::shared_ptr< GlobalLinSys > &plinsys, const AssemblyMapSharedPtr &pLocToGloMap)
Constructor.
bool m_useSimplex
Use a simplex distribution in the LOR system so that e.g. quadrilaterals are split into triangles.
GlobalLinSysSharedPtr m_LORLinSys
Linear solver for LOR system.
Array< OneD, NekDouble > m_invLinMeshMultiplicity
Inverse of multiplicity of each global DOF in the LOR mesh.
std::string m_slvType
Solution type for LOR space, DirectFull, IterativeFull, PETScFull.
static PreconditionerSharedPtr create(const std::shared_ptr< GlobalLinSys > &plinsys, const std::shared_ptr< AssemblyMap > &pLocToGloMap)
Creates an instance of this class.
void v_DoPreconditioner(const Array< OneD, NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const bool &isLocal=false) override
Apply a preconditioner to the conjugate gradient method.
static std::string className
Name of class.
unsigned int m_nDir
Number of Dirichlet values on original mesh.
Array< OneD, NekDouble > m_invMultiplicity
Inverse of multiplicity of each global DOF in the high-order mesh.
SpatialDomains::MeshGraphSharedPtr m_lor_graph
MeshGraph for the LOR system.
bool m_equiSpaced
Flag that determines whether to use equispaced point distributions or GLL.
SpatialDomains::MeshGraphSharedPtr GetLinearGraph()
SpatialDomains::MeshGraphSharedPtr CreateLinearGraph(int nsplit, std::map< int, std::pair< int, std::vector< int > > > &LinCoeffMap, bool UseGLL, bool useSimplex)
const char *const GlobalSysSolnTypeMap[]
PreconFactory & GetPreconFactory()
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition AssemblyMap.h:50
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition Vmath.hpp:72
void Gathr(I n, const T *x, const I *y, T *z)
Gather vector z[i] = x[y[i]].
Definition Vmath.hpp:507
void Scatr(int n, const T *x, const int *y, T *z)
Scatter vector z[y[i]] = x[i].
Definition Vmath.hpp:539
void Sdiv(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha/x.
Definition Vmath.hpp:154
void Zero(int n, T *x, const int incx)
Zero vector.
Definition Vmath.hpp:273
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:825