Nektar++
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 nscale,
55 const NekSysKey &pKey)
56 : NekNonlinSysIter(pSession, vRowComm, nscale, pKey)
57{
58}
59
61{
63}
64
65/**
66 *
67 **/
69 const int nGlobal, const Array<OneD, const NekDouble> &pInput,
70 Array<OneD, NekDouble> &pOutput, [[maybe_unused]] const int nDir)
71{
72 ASSERTL0(nGlobal == m_SysDimen, "nGlobal != m_SysDimen");
73
74 m_SourceVec = pInput;
75 m_Solution = pOutput;
77 m_converged = false;
78
79 Vmath::Vcopy(nGlobal, pInput, 1, m_Solution, 1);
80
81 NekDouble resnormOld = 0.0;
82 int NttlNonlinIte = 0;
83 for (; NttlNonlinIte < m_NekNonlinSysMaxIterations; ++NttlNonlinIte)
84 {
86
87 ConvergenceCheck(NttlNonlinIte, m_Residual);
88 if (m_converged)
89 {
90 break;
91 }
92
93 NekDouble LinSysRelativeIteTol =
94 CalcInexactNewtonForcing(NttlNonlinIte, resnormOld, m_SysResNorm);
95 resnormOld = m_SysResNorm;
96 m_linsol->SetRhsMagnitude(m_SysResNorm);
97 m_linsol->SetNekLinSysTolerance(LinSysRelativeIteTol);
98 int ntmpLinSysIts =
99 m_linsol->SolveSystem(nGlobal, m_Residual, m_DeltSltn, 0);
100 m_NtotLinSysIts += ntmpLinSysIts;
101
102 Vmath::Vsub(nGlobal, m_Solution, 1, m_DeltSltn, 1, m_Solution, 1);
103 }
104
106 {
107 int nwidthcolm = 11;
108
110 " # Nonlinear solver not converge in DoImplicitSolve");
111 cout << right << scientific << setw(nwidthcolm)
112 << setprecision(nwidthcolm - 6)
113 << " * Newton-Its converged (RES=" << sqrt(m_SysResNorm)
114 << " Res/Res0= " << sqrt(m_SysResNorm / m_SysResNorm0)
115 << " Res/DtRHS= " << sqrt(m_SysResNorm / m_rhs_magnitude)
116 << " with " << setw(3) << NttlNonlinIte << " Non-Its)" << endl;
117 }
118
119 return NttlNonlinIte;
120}
121
123 const int &nIteration, const NekDouble &resnormOld,
124 const NekDouble &resnorm)
125{
126 if (nIteration == 0 || !m_InexactNewtonForcing)
127 {
129 }
130 else
131 {
132 static const NekDouble forcingGamma = 1.0;
133 static const NekDouble forcingAlpha = 0.5 * (1.0 + sqrt(5.0));
134 NekDouble tmpForc =
135 forcingGamma * pow((resnorm / resnormOld), forcingAlpha);
136 return max(min(m_NekLinSysTolerance, tmpForc), 1.0E-6);
137 }
138}
139
140} // namespace Nektar::LibUtilities
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define WARNINGL0(condition, msg)
Definition: ErrorUtil.hpp:215
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)
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)
NekDouble CalcInexactNewtonForcing(const int &nIteration, const NekDouble &resnormOld, const NekDouble &resnorm)
NekNonlinSysIterNewton(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nscale, const NekSysKey &pKey)
NekSysOperators m_operator
Definition: NekSys.h:298
void DoNekSysResEval(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition: NekSys.h:135
NekNonlinSysIterFactory & GetNekNonlinSysIterFactory()
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825
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.hpp:220
STL namespace.
scalarT< T > sqrt(scalarT< T > in)
Definition: scalar.hpp:294