Nektar++
Loading...
Searching...
No Matches
NekLinSysIterCGLoc.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekLinSysIterCGLoc.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: NekLinSysIterCGLoc definition
33//
34///////////////////////////////////////////////////////////////////////////////
35
37
38using namespace std;
39
41{
42/**
43 * @class NekLinSysIterCGLoc
44 *
45 * Solves a linear system using iterative methods.
46 */
49 "ConjugateGradientLoc", NekLinSysIterCGLoc::create,
50 "NekLinSysIterCG solver in Local Space.");
51
54 const LibUtilities::CommSharedPtr &vRowComm, const int nDimen,
55 const NekSysKey &pKey)
56 : NekLinSysIter(pSession, vRowComm, nDimen, pKey)
57{
58 m_isLocal = true;
59 m_flexible = pSession->DefinesParameter("FlexibleConjugateGradient")
60 ? pSession->GetParameter("FlexibleConjugateGradient")
61 : false;
62}
63
68
69/**
70 *
71 */
73 const int nLocal, const Array<OneD, const NekDouble> &pInput,
74 Array<OneD, NekDouble> &pOutput, [[maybe_unused]] const int nDir)
75{
76 DoConjugateGradient(nLocal, pInput, pOutput);
77
78 return m_totalIterations;
79}
80
81void NekLinSysIterCGLoc::v_DoIterate(const int nGlobal,
82 const Array<OneD, NekDouble> &rhs,
84 [[maybe_unused]] const int nDir,
85 NekDouble &err, int &iter)
86{
87 DoConjugateGradient(nGlobal, rhs, x);
88 iter = m_totalIterations;
89 err = m_finalError;
90}
91
92/**  
93 * Solve a global linear system using the conjugate gradient method.  
94 * We solve only for the non-Dirichlet modes. The operator is evaluated  
95 * using an auxiliary function m_operator.DoNekSysLhsEval defined by the  
96 * specific solver. Distributed math routines are used to support  
97 * parallel execution of the solver.  
98 *  
99 * The implemented algorithm uses a reduced-communication reordering of  
100 * the standard PCG method (Demmel, Heath and Vorst, 1993)  
101 *  
102 * @param pInput Input residual of all DOFs.  
103 * @param pOutput Solution vector of all DOFs.  
104 */
106 const int nLocal, const Array<OneD, const NekDouble> &pInput,
107 Array<OneD, NekDouble> &pOutput)
108{
109 // Allocate array storage
110 Array<OneD, NekDouble> w_A(nLocal, 0.0);
111 Array<OneD, NekDouble> s_A(nLocal, 0.0);
112 Array<OneD, NekDouble> p_A(nLocal, 0.0);
113 Array<OneD, NekDouble> r_A(nLocal, 0.0);
114 Array<OneD, NekDouble> q_A(nLocal, 0.0);
115 Array<OneD, NekDouble> wk(nLocal, 0.0);
116
117 NekDouble alpha;
119 NekDouble rho;
120 NekDouble rho_new;
121 NekDouble rho_star;
122 NekDouble mu;
123 NekDouble eps;
124 Array<OneD, NekDouble> vExchange(4, 0.0);
125
126 // Copy initial residual from input
127 Vmath::Vcopy(nLocal, pInput, 1, r_A, 1);
128
129 // zero homogeneous out array ready for solution updates
130 // Should not be earlier in case input vector is same as
131 // output and above copy has been peformed
132 Vmath::Zero(nLocal, pOutput, 1);
133
134 // evaluate initial residual error for exit check
135 m_operator.DoAssembleLoc(r_A, wk, true);
136 vExchange[2] = Vmath::Dot(nLocal, wk, r_A);
137
138 m_rowComm->AllReduce(vExchange, Nektar::LibUtilities::ReduceSum);
139
140 eps = vExchange[2];
141
143 {
144 Set_Rhs_Magnitude(pInput);
145 }
146
148
149 // If input residual is less than tolerance skip solve.
151 {
153 if (m_verbose && m_root)
154 {
155 cout << "CG iterations made = " << m_totalIterations
156 << " using tolerance of " << m_NekLinSysTolerance
157 << " (error = " << m_finalError
158 << ", rhs_mag = " << sqrt(m_rhs_magnitude) << ")" << endl;
159 }
160 return;
161 }
162
163 m_operator.DoNekSysPrecon(r_A, w_A, true);
164 m_operator.DoNekSysLhsEval(w_A, s_A);
165
166 vExchange[0] = Vmath::Dot(nLocal, r_A, w_A);
167 vExchange[1] = Vmath::Dot(nLocal, s_A, w_A);
168
169 m_rowComm->AllReduce(vExchange, Nektar::LibUtilities::ReduceSum);
170
171 rho_star = 0.0;
172 rho = vExchange[0];
173 mu = vExchange[1];
174 beta = 0.0;
175 alpha = rho / mu;
177
178 // Continue until convergence
179 while (true)
180 {
182 {
184 if (m_root)
185 {
186 cout << "CG iterations made = " << m_totalIterations
187 << " using tolerance of " << m_NekLinSysTolerance
188 << " (error = " << m_finalError
189 << ", rhs_mag = " << sqrt(m_rhs_magnitude) << ")"
190 << " WARNING: Exceeded maxIt" << endl;
191 }
192 break;
193 }
194
195 // Compute new search direction p_k, q_k
196 Vmath::Svtvp(nLocal, beta, p_A, 1, w_A, 1, p_A, 1);
197 Vmath::Svtvp(nLocal, beta, q_A, 1, s_A, 1, q_A, 1);
198
199 // Update solution x_{k+1}
200 Vmath::Svtvp(nLocal, alpha, p_A, 1, pOutput, 1, pOutput, 1);
201
202 // Update residual vector r_{k+1}
203 Vmath::Svtvp(nLocal, -alpha, q_A, 1, r_A, 1, r_A, 1);
204
205 if (m_flexible)
206 {
207 // <r_{k+1}, w_{k}>
208 vExchange[3] = Vmath::Dot(nLocal, r_A, w_A);
209 }
210
211 // Apply preconditioner
212 m_operator.DoNekSysPrecon(r_A, w_A, true);
213
214 // Perform the method-specific matrix-vector multiply operation.
215 m_operator.DoNekSysLhsEval(w_A, s_A);
216
217 // <r_{k+1}, w_{k+1}>
218 vExchange[0] = Vmath::Dot(nLocal, r_A, w_A);
219
220 // <s_{k+1}, w_{k+1}>
221 vExchange[1] = Vmath::Dot(nLocal, s_A, w_A);
222
224 {
225 // <r_{k+1}, r_{k+1}>
226 m_operator.DoAssembleLoc(r_A, wk, true);
227 vExchange[2] = Vmath::Dot(nLocal, wk, r_A);
228 }
229 // Perform inner-product exchanges
230 m_rowComm->AllReduce(vExchange, Nektar::LibUtilities::ReduceSum);
231
232 rho_new = vExchange[0];
233 mu = vExchange[1];
234 eps = vExchange[2];
235 if (m_flexible)
236 {
237 rho_star = vExchange[3];
238 }
239
241
242 // Test if norm is within tolerance
244 {
246 if (m_verbose && m_root)
247 {
248 cout << "CG iterations made = " << m_totalIterations
249 << " using tolerance of " << m_NekLinSysTolerance
250 << " (error = " << m_finalError
251 << ", rhs_mag = " << sqrt(m_rhs_magnitude) << ")" << endl;
252 }
253 break;
254 }
255
256 // Compute search direction and solution coefficients
257 beta = (rho_new - rho_star) / rho;
258 alpha = rho_new / (mu - rho_new * beta / alpha);
259 rho = rho_new;
260 }
261}
262} // 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)
Actual iterative solve.
static NekLinSysIterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
NekLinSysIterCGLoc(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
Constructor for full direct matrix solve.
int v_SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir) override
void v_DoIterate(const int nGlobal, const Array< OneD, NekDouble > &rhs, Array< OneD, NekDouble > &x, const int nDir, NekDouble &err, int &iter) override
void Set_Rhs_Magnitude(const Array< OneD, NekDouble > &pIn)
LibUtilities::CommSharedPtr m_rowComm
Definition NekSys.h:299
NekSysOperators m_operator
Definition NekSys.h:306
void DoAssembleLoc(InArrayType &xn, OutArrayType &xn1, const bool &flag=false) const
Definition NekSys.h:168
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 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