Nektar++
GlobalLinSysIterativeFull.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 // 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: GlobalLinSys definition
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include <map>
38 
39 using namespace std;
40 
41 namespace Nektar
42 {
43  namespace MultiRegions
44  {
45  /**
46  * @class GlobalLinSysIterativeCG
47  *
48  * This class implements a conjugate gradient matrix solver.
49  * Preconditioning is implemented using a Jacobi (diagonal)
50  * preconditioner.
51  */
52 
53  /**
54  * Registers the class with the Factory.
55  */
56  string GlobalLinSysIterativeFull::className
58  "IterativeFull",
59  GlobalLinSysIterativeFull::create,
60  "Iterative solver for full matrix system.");
61 
62 
63  /**
64  * Constructor for full direct matrix solve.
65  * @param pKey Key specifying matrix to solve.
66  * @param pExp Shared pointer to expansion list for applying
67  * matrix evaluations.
68  * @param pLocToGloMap Local to global mapping.
69  */
70  GlobalLinSysIterativeFull::GlobalLinSysIterativeFull(
71  const GlobalLinSysKey &pKey,
72  const std::weak_ptr<ExpList> &pExp,
73  const std::shared_ptr<AssemblyMap> &pLocToGloMap)
74  : GlobalLinSys (pKey, pExp, pLocToGloMap),
75  GlobalLinSysIterative(pKey, pExp, pLocToGloMap)
76  {
78  "This routine should only be used when using an Iterative "
79  "conjugate gradient matrix solve.");
80  }
81 
82 
83  /**
84  *
85  */
87  {
88 
89  }
90 
91 
92  /**
93  * Solve a global linear system with Dirichlet forcing using a
94  * conjugate gradient method. This routine performs handling of the
95  * Dirichlet forcing terms and wraps the underlying iterative solver
96  * used for the remaining degrees of freedom.
97  *
98  * Consider solving for \f$x\f$, the matrix system \f$Ax=b\f$, where
99  * \f$b\f$ is known. To enforce the Dirichlet terms we instead solve
100  * \f[A(x-x_0) = b - Ax_0 \f]
101  * where \f$x_0\f$ is the Dirichlet forcing.
102  *
103  * @param pInput RHS of linear system, \f$b\f$.
104  * @param pOutput On input, values of dirichlet degrees
105  * of freedom with initial guess on other values.
106  * On output, the solution \f$x\f$.
107  * @param pLocToGloMap Local to global mapping.
108  * @param pDirForcing Precalculated Dirichlet forcing.
109  */
111  const Array<OneD, const NekDouble> &pLocInput,
112  Array<OneD, NekDouble> &pLocOutput,
113  const AssemblyMapSharedPtr &pLocToGloMap,
114  const Array<OneD, const NekDouble> &pDirForcing)
115  {
116  std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
117  bool vCG = false;
118  m_locToGloMap = pLocToGloMap;
119 
120  if (std::dynamic_pointer_cast<AssemblyMapCG>(pLocToGloMap))
121  {
122  vCG = true;
123  }
124  else if (std::dynamic_pointer_cast<AssemblyMapDG>(pLocToGloMap))
125  {
126  vCG = false;
127  }
128  else
129  {
130  NEKERROR(ErrorUtil::efatal, "Unknown map type");
131  }
132 
133  bool dirForcCalculated = (bool) pDirForcing.size();
134  int nDirDofs = pLocToGloMap->GetNumGlobalDirBndCoeffs();
135  int nGlobDofs = pLocToGloMap->GetNumGlobalCoeffs();
136  int nLocDofs = pLocToGloMap->GetNumLocalCoeffs();
137 
138  int nDirTotal = nDirDofs;
139  expList->GetComm()->GetRowComm()
140  ->AllReduce(nDirTotal, LibUtilities::ReduceSum);
141 
142  Array<OneD, NekDouble> tmp (nLocDofs);
143  Array<OneD, NekDouble> tmp1(nLocDofs);
144 
145  Array<OneD, NekDouble> global(nGlobDofs,0.0);
146 
147  if(nDirTotal)
148  {
149  // calculate the Dirichlet forcing
150  if(dirForcCalculated)
151  {
152  Vmath::Vsub(nLocDofs, pLocInput, 1,
153  pDirForcing, 1, tmp1, 1);
154  }
155  else
156  {
157  // Calculate the dirichlet forcing B_b (== X_b) and
158  // substract it from the rhs
159  expList->GeneralMatrixOp(m_linSysKey, pLocOutput, tmp);
160 
161  // Iterate over all the elements computing Robin BCs where
162  // necessary
163  for(auto &r : m_robinBCInfo) // add robin mass matrix
164  {
166  Array<OneD, NekDouble> tmploc;
167 
168  int n = r.first;
169  int offset = expList->GetCoeff_Offset(n);
170 
171  LocalRegions::ExpansionSharedPtr vExp = expList->GetExp(n);
172  // add local matrix contribution
173  for(rBC = r.second;rBC; rBC = rBC->next)
174  {
175  vExp->AddRobinTraceContribution(rBC->m_robinID,
176  rBC->m_robinPrimitiveCoeffs,
177  pLocOutput + offset,
178  tmploc = tmp + offset);
179  }
180  }
181 
182  Vmath::Vsub(nLocDofs, pLocInput, 1, tmp, 1, tmp1, 1);
183 
184  }
185  if (vCG)
186  {
187  pLocToGloMap->Assemble(tmp1,tmp);
188 
189  // solve for perturbation from initial guess in pOutput
191  nGlobDofs, tmp, global, pLocToGloMap, nDirDofs);
192 
193  pLocToGloMap->GlobalToLocal(global,tmp);
194 
195  // Add back initial condition
196  Vmath::Vadd(nLocDofs, tmp, 1, pLocOutput, 1, pLocOutput, 1);
197  }
198  else
199  {
200  ASSERTL0(false, "Need DG solve if using Dir BCs");
201  }
202  }
203  else
204  {
205  pLocToGloMap->Assemble(pLocInput,tmp);
206  SolveLinearSystem(nGlobDofs, tmp, global, pLocToGloMap,nDirDofs);
207  pLocToGloMap->GlobalToLocal(global,pLocOutput);
208  }
209  }
210 
211 
212  /**
213  *
214  */
216  const Array<OneD, NekDouble>& pInput,
217  Array<OneD, NekDouble>& pOutput)
218  {
219  std::shared_ptr<MultiRegions::ExpList> expList = m_expList.lock();
220 
221  AssemblyMapSharedPtr asmMap = m_locToGloMap.lock();
222 
223  int ncoeffs = expList->GetNcoeffs();
224 
225  Array<OneD,NekDouble> InputLoc(ncoeffs);
226  Array<OneD,NekDouble> OutputLoc(ncoeffs);
227  asmMap->GlobalToLocal(pInput, InputLoc);
228 
229  // Perform matrix-vector operation A*d_i
230  expList->GeneralMatrixOp(m_linSysKey,
231  InputLoc, OutputLoc);
232 
233 
234  // Apply robin boundary conditions to the solution.
235  for(auto &r : m_robinBCInfo) // add robin mass matrix
236  {
239 
240  int n = r.first;
241 
242  int offset = expList->GetCoeff_Offset(n);
243  LocalRegions::ExpansionSharedPtr vExp = expList->GetExp(n);
244 
245  // add local matrix contribution
246  for(rBC = r.second;rBC; rBC = rBC->next)
247  {
248  vExp->AddRobinTraceContribution(rBC->m_robinID,
249  rBC->m_robinPrimitiveCoeffs,
250  InputLoc + offset,
251  tmp = OutputLoc + offset);
252  }
253  }
254 
255  // put back in global coeffs
256  asmMap->Assemble(OutputLoc, pOutput);
257  }
258 
259  /**
260  *
261  */
263  {
264  m_map = m_locToGloMap.lock()->GetGlobalToUniversalMapUnique();
265  }
266 
267  }
268 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:250
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
A global linear system.
Definition: GlobalLinSys.h:73
const std::weak_ptr< ExpList > m_expList
Local Matrix System.
Definition: GlobalLinSys.h:127
const std::map< int, RobinBCInfoSharedPtr > m_robinBCInfo
Robin boundary info.
Definition: GlobalLinSys.h:129
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:201
const GlobalLinSysKey m_linSysKey
Key associated with this linear system.
Definition: GlobalLinSys.h:125
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.
virtual void v_DoMatrixMultiply(const Array< OneD, NekDouble > &pInput, Array< OneD, NekDouble > &pOutput)
Array< OneD, int > m_map
Global to universal unique map.
GlobalSysSolnType GetGlobalSysSolnType() const
Return the associated solution type.
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:68
std::shared_ptr< RobinBCInfo > RobinBCInfoSharedPtr
GlobalLinSysFactory & GetGlobalLinSysFactory()
std::shared_ptr< AssemblyMap > AssemblyMapSharedPtr
Definition: AssemblyMap.h:52
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
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:322
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:372