Nektar++
Loading...
Searching...
No Matches
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 &, OutArrayType &, OutArrayType &,
61 const bool &)>
65 static const int nfunctor1 = 4;
66 static const int nfunctor2 = 1;
67
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 // check if this operator is defined
137 {
138 return m_functors2[0] ? true : false;
139 }
140
141 inline void DoNekSysResEval(InArrayType &inarray, OutArrayType &outarray,
142 const bool &flag = false) const
143 {
144 ASSERTL1(m_functors1[0], "DoNekSysResEval should be defined");
145 m_functors1[0](inarray, outarray, flag);
146 }
147
148 inline void DoNekSysLhsEval(InArrayType &inarray, OutArrayType &outarray,
149 const bool &flag = false) const
150 {
151 ASSERTL1(m_functors1[1], "DoNekSysLhsEval should be defined");
152 m_functors1[1](inarray, outarray, flag);
153 }
154
155 inline void DoNekSysPrecon(InArrayType &inarray, OutArrayType &outarray,
156 const bool &flag = false) const
157 {
158 if (m_functors1[2])
159 {
160 m_functors1[2](inarray, outarray, flag);
161 }
162 else
163 {
164 Vmath::Vcopy(outarray.size(), inarray, 1, outarray, 1);
165 }
166 }
167
169 const bool &flag = false) const
170 {
171 ASSERTL1(m_functors1[3], "DoAssembleLoc should be defined");
172 m_functors1[3](xn, xn1, flag);
173 }
174
175 // xn contains with initial guess and overwritten by solution on exit
176 // xn1 is filled by final residual for convergence check
178 OutArrayType &xn1,
179 const bool &flag = false) const
180 {
181 ASSERTL1(m_functors2[0], "DoNekSysFixPointIte should be defined");
182 m_functors2[0](rhs, xn, xn1, flag);
183 }
184
185protected:
186 /* Defines operators
187 DoNekSysResEval :
188 evaluations the residual of the Nonlinear/Linear system
189 ie. the residual b-Ax and N(x) for linear and
190 nonlinear systems, respectively
191 May not be used for linear system.
192 DoNekSysLhsEval :
193 evaluations the LHS of the Nonlinear/Linear system (Ax),
194 where A is the matrix and x is solution vector.
195 For linear system A is the coefficient matrix;
196 For nonlinear system A is the coefficient matrix in
197 each nonlinear iterations, for example A is the
198 Jacobian matrix for Newton method;
199 DoNekSysPrecon :
200 Preconditioning operator of the system.
201 DoAssembleLoc :
202 Operator to assemble array and scater it back to the lcoal storage
203 if flag is true will zero Dirichlet conditions
204 DoNekSysFixPointIte :
205 */
207
208 /* Defines one operator
209 DoNekSysFixPointIte :
210 Operator to calculate RHS of fixed point iterations
211 (x^{n+1}=M^{-1}(b-N*x^{n}), with M+N=A).
212 */
214};
215
239
240class NekSys;
241
242typedef std::shared_ptr<NekSys> NekSysSharedPtr;
243
244class NekSys : public std::enable_shared_from_this<NekSys>
245{
246public:
247 /// Support creation through MemoryManager.
248 friend class MemoryManager<NekSys>;
249
252 const LibUtilities::CommSharedPtr &vRowComm, const int nDimen,
253 const NekSysKey &pKey)
254 {
256 pSession, vRowComm, nDimen, pKey);
257 return p;
258 }
259
262 const LibUtilities::CommSharedPtr &vRowComm, const int nDimen,
263 const NekSysKey &pKey);
264 LIB_UTILITIES_EXPORT virtual ~NekSys() = default;
265
270
272 const int nGlobal, const Array<OneD, const NekDouble> &pInput,
273 Array<OneD, NekDouble> &pOutput, const int nDir = 0)
274 {
275 return v_SolveSystem(nGlobal, pInput, pOutput, nDir);
276 }
277
279 {
280 return m_operator;
281 }
282
284 {
286 }
287
289 {
290 m_FlagWarnings = in;
291 }
292
294 {
295 m_rhs_magnitude = mag;
296 }
297
298protected:
301 bool m_root;
305
307
309
310 virtual void v_InitObject()
311 {
312 }
313
314 virtual void v_SetSysOperators(const NekSysOperators &in)
315 {
316 m_operator = in;
317 }
318
319 virtual int v_SolveSystem(
320 [[maybe_unused]] const int nGlobal,
321 [[maybe_unused]] const Array<OneD, const NekDouble> &pInput,
322 [[maybe_unused]] Array<OneD, NekDouble> &pOutput,
323 [[maybe_unused]] const int nDir)
324 {
325 ASSERTL0(false, "LinSysIterSolver NOT CORRECT.");
326 return 0;
327 }
328};
329
330} // namespace Nektar::LibUtilities
331#endif
#define ASSERTL0(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
#define LIB_UTILITIES_EXPORT
void SetFlagWarnings(bool in)
Definition NekSys.h:288
void SetRhsMagnitude(const NekDouble mag)
Definition NekSys.h:293
const NekSysOperators & GetSysOperators()
Definition NekSys.h:278
static NekSysSharedPtr CreateInstance(const LibUtilities::SessionReaderSharedPtr &pSession, const LibUtilities::CommSharedPtr &vRowComm, const int nDimen, const NekSysKey &pKey)
Definition NekSys.h:250
void SetSysOperators(const NekSysOperators &in)
Definition NekSys.h:283
LibUtilities::CommSharedPtr m_rowComm
Definition NekSys.h:299
virtual void v_InitObject()
Definition NekSys.h:310
int SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir=0)
Definition NekSys.h:271
NekSysOperators m_operator
Definition NekSys.h:306
virtual ~NekSys()=default
virtual int v_SolveSystem(const int nGlobal, const Array< OneD, const NekDouble > &pInput, Array< OneD, NekDouble > &pOutput, const int nDir)
Definition NekSys.h:319
virtual void v_SetSysOperators(const NekSysOperators &in)
Definition NekSys.h:314
std::string m_LinSysIterSolverTypeInNonlin
Definition NekSys.h:230
void DefineAssembleLoc(FuncPointerT func, ObjectPointerT obj)
Definition NekSys.h:121
void DefineNekSysFixPointIte(FuncPointerT func, ObjectPointerT obj)
Definition NekSys.h:128
std::function< void(InArrayType &, OutArrayType &, OutArrayType &, const bool &)> FunctorType2
Definition NekSys.h:62
Array< OneD, FunctorType2 > FunctorType2Array
Definition NekSys.h:64
void DoNekSysResEval(InArrayType &inarray, OutArrayType &outarray, const bool &flag=false) const
Definition NekSys.h:141
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
Array< OneD, NekDouble > OutArrayType
Definition NekSys.h:56
std::function< void(InArrayType &, OutArrayType &, const bool &)> FunctorType1
Definition NekSys.h:59
void DoNekSysFixPointIte(InArrayType &rhs, OutArrayType &xn, OutArrayType &xn1, const bool &flag=false) const
Definition NekSys.h:177
NekSysOperators(const NekSysOperators &in)
Definition NekSys.h:72
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
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:148
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:242
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition Comm.h:55
static const NekDouble kNekIterativeTol
static const NekDouble kNekUnsetDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition Vmath.hpp:825