Nektar++
Loading...
Searching...
No Matches
NekLinSysIterCG.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekLinSysIterCG.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: NekLinSysIterCG definition
33//
34///////////////////////////////////////////////////////////////////////////////
35
37
38using namespace std;
39
41{
42/**
43 * @class NekLinSysIterCG
44 *
45 * Solves a linear system using iterative methods.
46 */
49 "ConjugateGradient", NekLinSysIterCG::create,
50 "NekLinSysIterCG solver.");
51
54 const LibUtilities::CommSharedPtr &vRowComm, const int nDimen,
55 const NekSysKey &pKey)
56 : NekLinSysIter(pSession, vRowComm, nDimen, pKey)
57{
58 m_flexible = pSession->DefinesParameter("FlexibleConjugateGradient")
59 ? pSession->GetParameter("FlexibleConjugateGradient")
60 : false;
61}
62
67
68/**
69 *
70 */
71int NekLinSysIterCG::v_SolveSystem(const int nGlobal,
72 const Array<OneD, const NekDouble> &pInput,
74 const int nDir)
75{
76 DoConjugateGradient(nGlobal, pInput, pOutput, nDir);
77
78 return m_totalIterations;
79}
80
81void NekLinSysIterCG::v_DoIterate(const int nGlobal,
82 const Array<OneD, NekDouble> &rhs,
83 Array<OneD, NekDouble> &x, const int nDir,
84 NekDouble &err, int &iter)
85{
86 DoConjugateGradient(nGlobal, rhs, x, nDir);
87 iter = m_totalIterations;
88 err = m_finalError;
89}
90
91/**  
92 * Solve a global linear system using the conjugate gradient method.  
93 * We solve only for the non-Dirichlet modes. The operator is evaluated  
94 * using an auxiliary function m_operator.DoNekSysLhsEval defined by the  
95 * specific solver. Distributed math routines are used to support  
96 * parallel execution of the solver.  
97 *  
98 * The implemented algorithm uses a reduced-communication reordering of  
99 * the standard PCG method (Demmel, Heath and Vorst, 1993)  
100 *  
101 * @param pInput Input residual of all DOFs.  
102 * @param pOutput Solution vector of all DOFs.  
103 */
105 const int nGlobal, const Array<OneD, const NekDouble> &pInput,
106 Array<OneD, NekDouble> &pOutput, const int nDir)
107{
108 // Get vector sizes
109 int nNonDir = nGlobal - nDir;
110
111 // Allocate array storage
112 Array<OneD, NekDouble> w_A(nGlobal, 0.0);
113 Array<OneD, NekDouble> s_A(nGlobal, 0.0);
114 Array<OneD, NekDouble> p_A(nNonDir, 0.0);
115 Array<OneD, NekDouble> r_A(nNonDir, 0.0);
116 Array<OneD, NekDouble> q_A(nNonDir, 0.0);
118
119 NekDouble alpha;
121 NekDouble rho;
122 NekDouble rho_new;
123 NekDouble rho_star;
124 NekDouble mu;
125 NekDouble eps;
126 Array<OneD, NekDouble> vExchange(4, 0.0);
127
128 // Copy initial residual from input
129 Vmath::Vcopy(nNonDir, pInput + nDir, 1, r_A, 1);
130
131 // Zero homogeneous out array ready for solution updates
132 // Should not be earlier in case input vector is same as
133 // output and above copy has been peformed
134 Vmath::Zero(nNonDir, tmp = pOutput + nDir, 1);
135
136 // Evaluate initial residual error for exit check
137 vExchange[2] = Vmath::Dot2(nNonDir, r_A, r_A, m_map + nDir);
138
139 m_rowComm->AllReduce(vExchange[2], Nektar::LibUtilities::ReduceSum);
140
141 eps = vExchange[2];
142
144 {
145 Set_Rhs_Magnitude(pInput);
146 }
147
149
150 // If input residual is less than tolerance skip solve.
152 {
154 if (m_verbose && m_root)
155 {
156 cout << "CG iterations made = " << m_totalIterations
157 << " using tolerance of " << m_NekLinSysTolerance
158 << " (error = " << m_finalError
159 << ", rhs_mag = " << sqrt(m_rhs_magnitude) << ")" << endl;
160 }
161 return;
162 }
163
164 m_operator.DoNekSysPrecon(r_A, tmp = w_A + nDir);
165 m_operator.DoNekSysLhsEval(w_A, s_A);
166
167 vExchange[0] = Vmath::Dot2(nNonDir, r_A, w_A + nDir, m_map + nDir);
168 vExchange[1] = Vmath::Dot2(nNonDir, s_A + nDir, w_A + nDir, m_map + nDir);
169
170 m_rowComm->AllReduce(vExchange, Nektar::LibUtilities::ReduceSum);
171
172 rho_star = 0.0;
173 rho = vExchange[0];
174 mu = vExchange[1];
175 beta = 0.0;
176 alpha = rho / mu;
178
179 // Continue until convergence
180 while (true)
181 {
183 {
185 if (m_root)
186 {
187 cout << "CG iterations made = " << m_totalIterations
188 << " using tolerance of " << m_NekLinSysTolerance
189 << " (error = " << m_finalError
190 << ", rhs_mag = " << sqrt(m_rhs_magnitude) << ")"
191 << " WARNING: Exceeded maxIt" << endl;
192 }
193 break;
194 }
195
196 // Compute new search direction p_k, q_k
197 Vmath::Svtvp(nNonDir, beta, &p_A[0], 1, &w_A[nDir], 1, &p_A[0], 1);
198 Vmath::Svtvp(nNonDir, beta, &q_A[0], 1, &s_A[nDir], 1, &q_A[0], 1);
199
200 // Update solution x_{k+1}
201 Vmath::Svtvp(nNonDir, alpha, &p_A[0], 1, &pOutput[nDir], 1,
202 &pOutput[nDir], 1);
203
204 // Update residual vector r_{k+1}
205 Vmath::Svtvp(nNonDir, -alpha, &q_A[0], 1, &r_A[0], 1, &r_A[0], 1);
206
207 if (m_flexible)
208 {
209 if (m_mapIsOnes)
210 {
211 // <r_{k+1}, w_{k}>
212 vExchange[3] = Vmath::Dot(nNonDir, r_A, w_A + nDir);
213 }
214 else
215 {
216 // <r_{k+1}, w_{k}>
217 vExchange[3] =
218 Vmath::Dot2(nNonDir, r_A, w_A + nDir, m_map + nDir);
219 }
220 }
221
222 // Apply preconditioner
223 m_operator.DoNekSysPrecon(r_A, tmp = w_A + nDir);
224
225 // Perform the method-specific matrix-vector multiply operation.
226 m_operator.DoNekSysLhsEval(w_A, s_A);
227
228 if (m_mapIsOnes)
229 {
230 // <r_{k+1}, w_{k+1}>
231 vExchange[0] = Vmath::Dot(nNonDir, r_A, w_A + nDir);
232
233 // <s_{k+1}, w_{k+1}>
234 vExchange[1] = Vmath::Dot(nNonDir, s_A + nDir, w_A + nDir);
235
237 {
238 // <r_{k+1}, r_{k+1}>
239 vExchange[2] = Vmath::Dot(nNonDir, r_A, r_A);
240 }
241 }
242 else
243 {
244 // <r_{k+1}, w_{k+1}>
245 vExchange[0] = Vmath::Dot2(nNonDir, r_A, w_A + nDir, m_map + nDir);
246
247 // <s_{k+1}, w_{k+1}>
248 vExchange[1] =
249 Vmath::Dot2(nNonDir, s_A + nDir, w_A + nDir, m_map + nDir);
250
252 {
253 // <r_{k+1}, r_{k+1}>
254 vExchange[2] = Vmath::Dot2(nNonDir, r_A, r_A, m_map + nDir);
255 }
256 }
257 // Perform inner-product exchanges
258 m_rowComm->AllReduce(vExchange, Nektar::LibUtilities::ReduceSum);
259
260 rho_new = vExchange[0];
261 mu = vExchange[1];
262 eps = vExchange[2];
263 if (m_flexible)
264 {
265 rho_star = vExchange[3];
266 }
267
269
270 // Test if norm is within tolerance
272 {
274 if (m_verbose && m_root)
275 {
276 cout << "CG iterations made = " << m_totalIterations
277 << " using tolerance of " << m_NekLinSysTolerance
278 << " (error = " << m_finalError
279 << ", rhs_mag = " << sqrt(m_rhs_magnitude) << ")" << endl;
280 }
281 break;
282 }
283
284 // Compute search direction and solution coefficients
285 beta = (rho_new - rho_star) / rho;
286 alpha = rho_new / (mu - rho_new * beta / alpha);
287 rho = rho_new;
288 }
289}
290} // namespace Nektar::LibUtilities
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
void DoConjugateGradient(const int pNumRows, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int pNumDir)
Actual iterative solve.
void v_DoIterate(const int nGlobal, const Array< OneD, NekDouble > &rhs, Array< OneD, NekDouble > &x, const int nDir, NekDouble &err, int &iter) override
int v_SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir) override
NekLinSysIterCG(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
Constructor for full direct matrix solve.
static NekLinSysIterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
void Set_Rhs_Magnitude(const Array< OneD, NekDouble > &pIn)
Array< OneD, int > m_map
Global to universal unique map.
LibUtilities::CommSharedPtr m_rowComm
Definition NekSys.h:299
NekSysOperators m_operator
Definition NekSys.h:306
void DoNekSysPrecon(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition NekSys.h:155
void DoNekSysLhsEval(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition NekSys.h:148
std::shared_ptr< SessionReader > SessionReaderSharedPtr
NekLinSysIterFactory & GetNekLinSysIterFactory()
@ beta
Gauss Radau pinned at x=-1,.
Definition PointsType.h:59
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:55
static const NekDouble kNekUnsetDouble
void Svtvp(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Svtvp (scalar times vector plus vector): z = alpha*x + y.
Definition Vmath.hpp:396
T Dot2(int n, const T *w, const T *x, const int *y)
dot product
Definition Vmath.hpp:790
T Dot(int n, const T *w, const T *x)
dot product
Definition Vmath.hpp:761
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
STL namespace.
scalarT< T > sqrt(scalarT< T > in)
Definition scalar.hpp:290