Nektar++
ForcingAbsorption.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ForcingAbsorption.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2016 Kilian Lackhove
10// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
11// Department of Aeronautics, Imperial College London (UK), and Scientific
12// Computing and Imaging Institute, University of Utah (USA).
13//
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: Absorption layer forcing
33//
34///////////////////////////////////////////////////////////////////////////////
35
37
39
40using namespace std;
41
42namespace Nektar::SolverUtils
43{
44
47 "Absorption", ForcingAbsorption::create, "Forcing Absorption");
48
51 const std::weak_ptr<EquationSystem> &pEquation)
52 : Forcing(pSession, pEquation), m_hasRefFlow(false), m_hasRefFlowTime(false)
53{
54}
55
58 const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
59{
60 m_NumVariable = pNumForcingFields;
61 int npts = pFields[0]->GetTotPoints();
62
63 // Check for homogeneous expansion
64 // for later transformation of evaluated Absorption (SessionFunction)
65 m_homogeneous = pFields[0]->GetExpType() == MultiRegions::e3DH1D ||
66 pFields[0]->GetExpType() == MultiRegions::e3DH2D;
67
68 CalcAbsorption(pFields, pForce);
69
71 for (int i = 0; i < m_NumVariable; ++i)
72 {
73 m_Forcing[i] = Array<OneD, NekDouble>(npts, 0.0);
74 }
75
76 const TiXmlElement *funcNameElmt = pForce->FirstChildElement("REFFLOW");
77 if (funcNameElmt)
78 {
79 string funcName = funcNameElmt->GetText();
80 ASSERTL0(m_session->DefinesFunction(funcName),
81 "Function '" + funcName + "' not defined.");
83 for (int i = 0; i < m_NumVariable; ++i)
84 {
85 std::string s_FieldStr = m_session->GetVariable(i);
86 ASSERTL0(m_session->DefinesFunction(funcName, s_FieldStr),
87 "Variable '" + s_FieldStr + "' not defined.");
88 m_Refflow[i] = Array<OneD, NekDouble>(npts, 0.0);
89 GetFunction(pFields, m_session, funcName)
90 ->Evaluate(s_FieldStr, m_Refflow[i]);
91 }
92 m_hasRefFlow = true;
93 }
94
95 funcNameElmt = pForce->FirstChildElement("REFFLOWTIME");
96 if (funcNameElmt)
97 {
98 m_funcNameTime = funcNameElmt->GetText();
99 m_hasRefFlowTime = true;
100 m_hasRefFlow = true;
102 for (int i = 0; i < m_NumVariable; ++i)
103 {
104 std::string s_FieldStr = m_session->GetVariable(i);
105 ASSERTL0(m_session->DefinesFunction(m_funcNameTime, s_FieldStr),
106 "Variable '" + s_FieldStr + "' not defined.");
107 m_Refflow[i] = Array<OneD, NekDouble>(npts, 0.0);
108 }
109 }
110}
111
114 &pFields,
115 const TiXmlElement *pForce)
116{
117 const TiXmlElement *funcNameElmt = pForce->FirstChildElement("COEFF");
118 ASSERTL0(funcNameElmt,
119 "Requires COEFF tag, specifying function "
120 "name which prescribes absorption layer coefficient.");
121 string funcName = funcNameElmt->GetText();
122 ASSERTL0(m_session->DefinesFunction(funcName),
123 "Function '" + funcName + "' not defined.");
124
125 int npts = pFields[0]->GetTotPoints();
126
128 for (int i = 0; i < m_NumVariable; ++i)
129 {
130 m_Absorption[i] = Array<OneD, NekDouble>(npts, 0.0);
131 }
132
133 funcNameElmt = pForce->FirstChildElement("BOUNDARYREGIONS");
134 if (funcNameElmt)
135 {
136 ASSERTL0(
137 ParseUtils::GenerateVector(funcNameElmt->GetText(), m_bRegions),
138 "Unable to process list of BOUNDARYREGIONS in Absorption "
139 "Forcing: " +
140 std::string(funcNameElmt->GetText()));
141
142 // alter m_bRegions so that it contains the boundaryRegions of this rank
143 std::vector<unsigned int> localBRegions;
145 pFields[0]->GetGraph());
147 bcs.GetBoundaryRegions();
148 SpatialDomains::BoundaryRegionCollection::iterator it1;
149 int n = 0;
150 for (it1 = regions.begin(); it1 != regions.end(); ++it1)
151 {
152 if (std::find(m_bRegions.begin(), m_bRegions.end(), it1->first) !=
153 m_bRegions.end())
154 {
155 localBRegions.push_back(n);
156 }
157 n++;
158 }
159 m_bRegions = localBRegions;
160
161 if (m_bRegions.size() == 0)
162 {
163 return;
164 }
165
166 std::vector<Array<OneD, const NekDouble>> points;
167
169 for (int i = 0; i < 3; i++)
170 {
171 x[i] = Array<OneD, NekDouble>(npts, 0.0);
172 }
173 pFields[0]->GetCoords(x[0], x[1], x[2]);
174 for (int i = 0; i < 3; i++)
175 {
176 points.push_back(x[i]);
177 }
178
179 Array<OneD, NekDouble> t(npts, 0.0);
180 points.push_back(t);
181
182 Array<OneD, NekDouble> r(npts, 0.0);
183 std::vector<unsigned int>::iterator it;
184 std::vector<BPointPair> inPoints;
186 for (it = m_bRegions.begin(); it != m_bRegions.end(); ++it)
187 {
188 int bpts = pFields[0]->GetBndCondExpansions()[*it]->GetNpoints();
189 for (int i = 0; i < 3; i++)
190 {
191 b[i] = Array<OneD, NekDouble>(bpts, 0.0);
192 }
193 pFields[0]->GetBndCondExpansions()[*it]->GetCoords(b[0], b[1],
194 b[2]);
195 for (int i = 0;
196 i < pFields[0]->GetBndCondExpansions()[*it]->GetNpoints(); ++i)
197 {
198 inPoints.push_back(
199 BPointPair(BPoint(b[0][i], b[1][i], b[2][i]), i));
200 }
201 }
203 m_rtree->insert(inPoints.begin(), inPoints.end());
204
205 for (int i = 0; i < npts; ++i)
206 {
207 std::vector<BPointPair> result;
208 BPoint sPoint(x[0][i], x[1][i], x[2][i]);
209 m_rtree->query(bgi::nearest(sPoint, 1), std::back_inserter(result));
210 r[i] = bg::distance(sPoint, result[0].first);
211 }
212 points.push_back(r);
213
214 std::string s_FieldStr;
215 for (int i = 0; i < m_NumVariable; ++i)
216 {
217 s_FieldStr = m_session->GetVariable(i);
218 ASSERTL0(m_session->DefinesFunction(funcName, s_FieldStr),
219 "Variable '" + s_FieldStr + "' not defined.");
220
222 m_session->GetFunction(funcName, s_FieldStr);
223 ASSERTL0(ffunc->GetVlist() == "x y z t r",
224 "EVARS of " + funcName + " must be 'r'");
225
226 ffunc->Evaluate(points, m_Absorption[i]);
227 }
228 }
229 else
230 {
231 for (int i = 0; i < m_NumVariable; ++i)
232 {
233 std::string s_FieldStr = m_session->GetVariable(i);
234 GetFunction(pFields, m_session, funcName)
235 ->Evaluate(s_FieldStr, m_Absorption[i]);
236 }
237 }
238
239 // If homogeneous expansion is used, transform the forcing term to
240 // be in the Fourier space
241 if (m_homogeneous)
242 {
243 for (int i = 0; i < m_NumVariable; ++i)
244 {
245 pFields[i]->HomogeneousFwdTrans(pFields[i]->GetTotPoints(),
247 }
248 }
249}
250
253 const Array<OneD, Array<OneD, NekDouble>> &inarray,
254 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
255{
256 int nq = m_Forcing[0].size();
257 CalculateForcing(fields, inarray, time);
258 for (int i = 0; i < m_NumVariable; ++i)
259 {
260 Vmath::Vadd(nq, m_Forcing[i], 1, outarray[i], 1, outarray[i], 1);
261 }
262}
263
266 const Array<OneD, Array<OneD, NekDouble>> &inarray,
267 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
268{
269 int ncoeff = outarray[m_NumVariable - 1].size();
270 Array<OneD, NekDouble> tmp(ncoeff, 0.0);
271 CalculateForcing(fields, inarray, time);
272
273 for (int i = 0; i < m_NumVariable; ++i)
274 {
275 fields[i]->FwdTrans(m_Forcing[i], tmp);
276 Vmath::Vadd(ncoeff, tmp, 1, outarray[i], 1, outarray[i], 1);
277 }
278}
279
281 [[maybe_unused]] const Array<OneD, MultiRegions::ExpListSharedPtr> &fields,
282 const Array<OneD, Array<OneD, NekDouble>> &inarray, const NekDouble &time)
283{
284 int nq = m_Forcing[0].size();
285
286 std::string s_FieldStr;
287 Array<OneD, NekDouble> TimeScale(1);
289
290 if (m_hasRefFlow)
291 {
292 for (int i = 0; i < m_NumVariable; i++)
293 {
294 RefflowScaled[i] = Array<OneD, NekDouble>(nq);
296 {
297 s_FieldStr = m_session->GetVariable(i);
298
299 std::string s_FieldStr = m_session->GetVariable(i);
301 ->Evaluate(s_FieldStr, m_Refflow[i], time);
302 Vmath::Vcopy(nq, m_Refflow[i], 1, RefflowScaled[i], 1);
303 }
304 else
305 {
306 Vmath::Vcopy(nq, m_Refflow[i], 1, RefflowScaled[i], 1);
307 }
308
309 Vmath::Vsub(nq, inarray[i], 1, RefflowScaled[i], 1, m_Forcing[i],
310 1);
311 Vmath::Vmul(nq, m_Absorption[i], 1, m_Forcing[i], 1, m_Forcing[i],
312 1);
313 }
314 }
315 else
316 {
317 for (int i = 0; i < m_NumVariable; i++)
318 {
319 Vmath::Vmul(nq, m_Absorption[i], 1, inarray[i], 1, m_Forcing[i], 1);
320 }
321 }
322}
323
324} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:197
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:130
bg::model::point< NekDouble, 3, bg::cs::cartesian > BPoint
SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce) override
SOLVER_UTILS_EXPORT void v_ApplyCoeff(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time) override
ForcingAbsorption(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
static SOLVER_UTILS_EXPORT ForcingSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
Creates an instance of this class.
SOLVER_UTILS_EXPORT void v_Apply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble &time) override
Array< OneD, Array< OneD, NekDouble > > m_Refflow
std::pair< BPoint, unsigned int > BPointPair
void CalcAbsorption(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const TiXmlElement *pForce)
void CalculateForcing(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, const NekDouble &time)
static std::string className
Name of the class.
std::vector< unsigned int > m_bRegions
Array< OneD, Array< OneD, NekDouble > > m_Absorption
Defines a forcing term to be explicitly applied.
Definition: Forcing.h:71
int m_NumVariable
Number of variables.
Definition: Forcing.h:121
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:119
SOLVER_UTILS_EXPORT SessionFunctionSharedPtr GetFunction(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const LibUtilities::SessionReaderSharedPtr &pSession, std::string pName, bool pCache=false)
Get a SessionFunction by name.
Definition: Forcing.cpp:192
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:115
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition: Conditions.h:234
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:125
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:42
std::map< int, BoundaryRegionShPtr > BoundaryRegionCollection
Definition: Conditions.h:210
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:447
double NekDouble
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition: Vmath.hpp:72
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.hpp:180
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