Nektar++
Loading...
Searching...
No Matches
NekNonlinSysIterNewton.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekNonlinSysIterNewton.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: NekNonlinSysIterNewton definition
33//
34///////////////////////////////////////////////////////////////////////////////
35
37
38using namespace std;
39
41{
42/**
43 * @class NekNonlinSysIterNewton
44 *
45 * Solves a nonlinear system using iterative methods.
46 */
50 "NekNonlinSysIterNewton solver.");
51
54 const LibUtilities::CommSharedPtr &vRowComm, const int nDimen,
55 const NekSysKey &pKey)
56 : NekNonlinSysIter(pSession, vRowComm, nDimen, pKey)
57{
58 int inexactNewtonForcing = 0;
59 pSession->LoadParameter("InexactNewtonForcing", inexactNewtonForcing, 0);
60 m_inexactNewtonForcing = (bool)inexactNewtonForcing;
61 pSession->LoadParameter("ForcingEtaInit", m_forcingEtaInit, 1.0e-2);
62 pSession->LoadParameter("ForcingEtaMin", m_forcingEtaMin,
64 pSession->LoadParameter("ForcingEtaMax", m_forcingEtaMax, 5.0e-2);
65 pSession->LoadParameter("ForcingGamma", m_forcingGamma, 0.9);
66 pSession->LoadParameter("ForcingAlpha", m_forcingAlpha, 1.5);
67 pSession->LoadParameter("NewtonScale", m_NewtonScale, 1.0);
68}
69
74
75/**
76 *
77 **/
79 const int nGlobal, const Array<OneD, const NekDouble> &pInput,
80 Array<OneD, NekDouble> &pOutput, [[maybe_unused]] const int nDir)
81{
82 ASSERTL0(nGlobal == m_SysDimen, "nGlobal != m_SysDimen");
83
84 m_SourceVec = pInput;
85 m_Solution = pOutput;
87 m_converged = false;
88
89 Vmath::Vcopy(nGlobal, pInput, 1, m_Solution, 1);
90
91 NekDouble resnormOld = 0.0;
92 int NttlNonlinIte = 0;
93 for (; NttlNonlinIte < m_NekNonlinSysMaxIterations; ++NttlNonlinIte)
94 {
96 {
98 }
99 else
100 {
101 m_haveUpdatedResidual = false;
102 }
103
104 ConvergenceCheck(NttlNonlinIte, m_Residual);
105 if (m_converged)
106 {
107 break;
108 }
109
110 NekDouble LinSysRelativeIteTol =
111 CalcInexactNewtonForcing(NttlNonlinIte, resnormOld, m_SysResNorm);
112 resnormOld = m_SysResNorm;
113 m_linsol->SetRhsMagnitude(m_SysResNorm);
114 m_linsol->SetNekLinSysTolerance(LinSysRelativeIteTol);
115 if (m_verbose)
116 {
117 cout << "Newton Non-It=" << NttlNonlinIte
118 << " m_SysResNorm0=" << m_SysResNorm0
119 << " m_SysResNorm=" << m_SysResNorm << " "
120 << "(RES=" << sqrt(m_SysResNorm)
121 << " Res/Res0= " << sqrt(m_SysResNorm / m_SysResNorm0)
122 << " Res/DtRHS= " << sqrt(m_SysResNorm / m_rhs_magnitude)
123 << " LinSysRelativeIteTol=" << LinSysRelativeIteTol << endl;
124 cout << "Will apply Newton correction with Newton Scale = "
125 << m_NewtonScale << endl;
126 }
127 int ntmpLinSysIts =
128 m_linsol->SolveSystem(nGlobal, m_Residual, m_DeltSltn, 0);
129 m_NtotLinSysIts += ntmpLinSysIts;
130
131 NekDouble oldResNorm = sqrt(m_SysResNorm);
132 bool accepted = v_ApplyNewtonUpdate(nGlobal, oldResNorm);
133 if (!accepted)
134 {
135 WARNINGL0(false, "Newton step rejected.");
136 break;
137 }
138 }
139
141 {
142 int nwidthcolm = 11;
143
145 " # Nonlinear solver not converge in DoImplicitSolve");
146 cout << right << scientific << setw(nwidthcolm)
147 << setprecision(nwidthcolm - 6)
148 << " * Newton-Its converged (RES=" << sqrt(m_SysResNorm)
149 << " Res/Res0= " << sqrt(m_SysResNorm / m_SysResNorm0)
150 << " Res/DtRHS= " << sqrt(m_SysResNorm / m_rhs_magnitude)
151 << " with " << setw(3) << NttlNonlinIte << " Non-Its)" << endl;
152 }
153
154 return NttlNonlinIte;
155}
156
158 const int ntotal, [[maybe_unused]] const NekDouble oldResNorm)
159{
160 Vmath::Svtvp(ntotal, -1.0 * m_NewtonScale, m_DeltSltn, 1, m_Solution, 1,
161 m_Solution, 1);
162 return true;
163}
164
166 const int &nIteration, const NekDouble &resnormOld,
167 const NekDouble &resnorm)
168{
170 {
172 }
173
174 if (nIteration == 0 || resnormOld <= 0.0)
175 {
176 return m_forcingEtaInit;
177 }
178
179 // resnorm and resnormOld are stored as squared norms in this class
180 NekDouble rk = sqrt(resnorm);
181 NekDouble rkm1 = sqrt(resnormOld);
182
183 NekDouble eta = m_forcingGamma * pow(rk / rkm1, m_forcingAlpha);
184
185 eta = std::max(m_forcingEtaMin, std::min(m_forcingEtaMax, eta));
186
187 return eta;
188}
189
190} // namespace Nektar::LibUtilities
#define ASSERTL0(condition, msg)
#define WARNINGL0(condition, msg)
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
void ConvergenceCheck(const int nIteration, const Array< OneD, const NekDouble > &Residual)
virtual bool v_ApplyNewtonUpdate(const int ntotal, const NekDouble oldResNorm)
int v_SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir) override
static NekNonlinSysIterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
NekNonlinSysIterNewton(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
NekDouble CalcInexactNewtonForcing(const int &nIteration, const NekDouble &resnormOld, const NekDouble &resnorm)
NekSysOperators m_operator
Definition NekSys.h:306
void DoNekSysResEval(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition NekSys.h:141
NekNonlinSysIterFactory & GetNekNonlinSysIterFactory()
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:55
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
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