Nektar++
NekSys.h
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekSys.h
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: NekSys header
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_NekSys_H
37#define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_NekSys_H
38
42#include <iomanip>
43
45{
46
47// =====================================================================
48// ==== DEFINITION OF THE CLASS NekSysOperators
49// ==== Defines operators needed by iterative solver of nonlinear or
50// ==== linear system
51// =====================================================================
53{
54public:
57
58 typedef std::function<void(InArrayType &, OutArrayType &, const bool &)>
60 typedef std::function<void(InArrayType &, InArrayType &, OutArrayType &,
61 const bool &)>
65 static const int nfunctor1 = 4;
66 static const int nfunctor2 = 1;
67
69 {
70 }
71
74 {
75 for (int i = 0; i < nfunctor1; ++i)
76 {
77 m_functors1[i] = in.m_functors1[i];
78 }
79 for (int i = 0; i < nfunctor2; ++i)
80 {
81 m_functors2[i] = in.m_functors2[i];
82 }
83 }
84
86 {
87 for (int i = 0; i < nfunctor1; ++i)
88 {
89 m_functors1[i] = in.m_functors1[i];
90 }
91 for (int i = 0; i < nfunctor2; ++i)
92 {
93 m_functors2[i] = in.m_functors2[i];
94 }
95
96 return *this;
97 }
98
99 template <typename FuncPointerT, typename ObjectPointerT>
100 void DefineNekSysResEval(FuncPointerT func, ObjectPointerT obj)
101 {
102 m_functors1[0] =
103 std::bind(func, obj, std::placeholders::_1, std::placeholders::_2,
104 std::placeholders::_3);
105 }
106 template <typename FuncPointerT, typename ObjectPointerT>
107 void DefineNekSysLhsEval(FuncPointerT func, ObjectPointerT obj)
108 {
109 m_functors1[1] =
110 std::bind(func, obj, std::placeholders::_1, std::placeholders::_2,
111 std::placeholders::_3);
112 }
113 template <typename FuncPointerT, typename ObjectPointerT>
114 void DefineNekSysPrecon(FuncPointerT func, ObjectPointerT obj)
115 {
116 m_functors1[2] =
117 std::bind(func, obj, std::placeholders::_1, std::placeholders::_2,
118 std::placeholders::_3);
119 }
120 template <typename FuncPointerT, typename ObjectPointerT>
121 void DefineAssembleLoc(FuncPointerT func, ObjectPointerT obj)
122 {
123 m_functors1[3] =
124 std::bind(func, obj, std::placeholders::_1, std::placeholders::_2,
125 std::placeholders::_3);
126 }
127 template <typename FuncPointerT, typename ObjectPointerT>
128 void DefineNekSysFixPointIte(FuncPointerT func, ObjectPointerT obj)
129 {
130 m_functors2[0] =
131 std::bind(func, obj, std::placeholders::_1, std::placeholders::_2,
132 std::placeholders::_3, std::placeholders::_4);
133 }
134
135 inline void DoNekSysResEval(InArrayType &inarray, OutArrayType &outarray,
136 const bool &flag = false) const
137 {
138 ASSERTL1(m_functors1[0], "DoNekSysResEval should be defined");
139 m_functors1[0](inarray, outarray, flag);
140 }
141
142 inline void DoNekSysLhsEval(InArrayType &inarray, OutArrayType &outarray,
143 const bool &flag = false) const
144 {
145 ASSERTL1(m_functors1[1], "DoNekSysLhsEval should be defined");
146 m_functors1[1](inarray, outarray, flag);
147 }
148
149 inline void DoNekSysPrecon(InArrayType &inarray, OutArrayType &outarray,
150 const bool &flag = false) const
151 {
152 if (m_functors1[2])
153 {
154 m_functors1[2](inarray, outarray, flag);
155 }
156 else
157 {
158 Vmath::Vcopy(outarray.size(), inarray, 1, outarray, 1);
159 }
160 }
161
163 const bool &flag = false) const
164 {
165 ASSERTL1(m_functors1[3], "DoAssembleLoc should be defined");
166 m_functors1[3](xn, xn1, flag);
167 }
168
170 OutArrayType &xn1,
171 const bool &flag = false) const
172 {
173 ASSERTL1(m_functors2[0], "DoNekSysFixPointIte should be defined");
174 m_functors2[0](rhs, xn, xn1, flag);
175 }
176
177protected:
178 /* Defines operators
179 DoNekSysResEval :
180 evaluations the residual of the Nonlinear/Linear system
181 ie. the residual b-Ax and N(x) for linear and
182 nonlinear systems, respectively
183 May not be used for linear system.
184 DoNekSysLhsEval :
185 evaluations the LHS of the Nonlinear/Linear system (Ax),
186 where A is the matrix and x is solution vector.
187 For linear system A is the coefficient matrix;
188 For nonlinear system A is the coefficient matrix in
189 each nonlinear iterations, for example A is the
190 Jacobian matrix for Newton method;
191 DoNekSysPrecon :
192 Preconditioning operator of the system.
193 DoAssembleLoc :
194 Operator to assemble array and scater it back to the lcoal storage
195 if flag is true will zero Dirichlet conditions
196 DoNekSysFixPointIte :
197 */
199
200 /* Defines one operator
201 DoNekSysFixPointIte :
202 Operator to calculate RHS of fixed point iterations
203 (x^{n+1}=M^{-1}(b-N*x^{n}), with M+N=A).
204 */
206};
207
209{
210public:
211 NekSysKey() = default;
212 ~NekSysKey() = default;
213
214 // NekLinSysIter
217
218 // NekNonlinSysIter
222 std::string m_LinSysIterSolverTypeInNonlin = "GMRES";
223
224 // GMRES
230};
231
232class NekSys;
233
234typedef std::shared_ptr<NekSys> NekSysSharedPtr;
235
236class NekSys : public std::enable_shared_from_this<NekSys>
237{
238public:
239 /// Support creation through MemoryManager.
240 friend class MemoryManager<NekSys>;
241
244 const LibUtilities::CommSharedPtr &vRowComm, const int nDimen,
245 const NekSysKey &pKey)
246 {
248 pSession, vRowComm, nDimen, pKey);
249 return p;
250 }
251
254 const LibUtilities::CommSharedPtr &vRowComm, const int nDimen,
255 const NekSysKey &pKey);
256 LIB_UTILITIES_EXPORT virtual ~NekSys() = default;
257
259 {
260 v_InitObject();
261 }
262
264 const int nGlobal, const Array<OneD, const NekDouble> &pInput,
265 Array<OneD, NekDouble> &pOutput, const int nDir = 0)
266 {
267 return v_SolveSystem(nGlobal, pInput, pOutput, nDir);
268 }
269
271 {
272 return m_operator;
273 }
274
276 {
278 }
279
281 {
282 m_FlagWarnings = in;
283 }
284
286 {
287 m_rhs_magnitude = mag;
288 }
289
290protected:
293 bool m_root;
297
299
301
302 virtual void v_InitObject()
303 {
304 }
305
306 virtual void v_SetSysOperators(const NekSysOperators &in)
307 {
308 m_operator = in;
309 }
310
311 virtual int v_SolveSystem(
312 [[maybe_unused]] const int nGlobal,
313 [[maybe_unused]] const Array<OneD, const NekDouble> &pInput,
314 [[maybe_unused]] Array<OneD, NekDouble> &pOutput,
315 [[maybe_unused]] const int nDir)
316 {
317 ASSERTL0(false, "LinSysIterSolver NOT CORRECT.");
318 return 0;
319 }
320};
321
322} // namespace Nektar::LibUtilities
323#endif
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:242
#define LIB_UTILITIES_EXPORT
void SetFlagWarnings(bool in)
Definition: NekSys.h:280
void SetRhsMagnitude(const NekDouble mag)
Definition: NekSys.h:285
const NekSysOperators & GetSysOperators()
Definition: NekSys.h:270
static NekSysSharedPtr CreateInstance(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
Definition: NekSys.h:242
void SetSysOperators(const NekSysOperators &in)
Definition: NekSys.h:275
LibUtilities::CommSharedPtr m_rowComm
Definition: NekSys.h:291
virtual void v_InitObject()
Definition: NekSys.h:302
int SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir=0)
Definition: NekSys.h:263
NekSysOperators m_operator
Definition: NekSys.h:298
virtual ~NekSys()=default
NekSys(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
Definition: NekSys.cpp:46
virtual int v_SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir)
Definition: NekSys.h:311
virtual void v_SetSysOperators(const NekSysOperators &in)
Definition: NekSys.h:306
NekDouble m_NonlinIterTolRelativeL2
Definition: NekSys.h:221
std::string m_LinSysIterSolverTypeInNonlin
Definition: NekSys.h:222
void DefineAssembleLoc(FuncPointerT func, ObjectPointerT obj)
Definition: NekSys.h:121
void DefineNekSysFixPointIte(FuncPointerT func, ObjectPointerT obj)
Definition: NekSys.h:128
Array< OneD, FunctorType2 > FunctorType2Array
Definition: NekSys.h:64
void DoNekSysResEval(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition: NekSys.h:135
void DefineNekSysResEval(FuncPointerT func, ObjectPointerT obj)
Definition: NekSys.h:100
void DefineNekSysLhsEval(FuncPointerT func, ObjectPointerT obj)
Definition: NekSys.h:107
const Array< OneD, const NekDouble > InArrayType
Definition: NekSys.h:55
NekSysOperators & operator=(const NekSysOperators &in)
Definition: NekSys.h:85
std::function< void(InArrayType &, InArrayType &, OutArrayType &, const bool &)> FunctorType2
Definition: NekSys.h:62
Array< OneD, NekDouble > OutArrayType
Definition: NekSys.h:56
std::function< void(InArrayType &, OutArrayType &, const bool &)> FunctorType1
Definition: NekSys.h:59
NekSysOperators(const NekSysOperators &in)
Definition: NekSys.h:72
void DoNekSysFixPointIte(InArrayType &rhs, InArrayType &xn, OutArrayType &xn1, const bool &flag=false) const
Definition: NekSys.h:169
void DoAssembleLoc(InArrayType &xn, OutArrayType &xn1, const bool &flag=false) const
Definition: NekSys.h:162
void DoNekSysPrecon(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition: NekSys.h:149
Array< OneD, FunctorType1 > FunctorType1Array
Definition: NekSys.h:63
void DefineNekSysPrecon(FuncPointerT func, ObjectPointerT obj)
Definition: NekSys.h:114
void DoNekSysLhsEval(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition: NekSys.h:142
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< NekSys > NekSysSharedPtr
Definition: NekSys.h:234
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
static const NekDouble kNekIterativeTol
static const NekDouble kNekUnsetDouble
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825