Nektar++
ForcingBody.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ForcingBody.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// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Body forcing
32//
33///////////////////////////////////////////////////////////////////////////////
34#include <boost/algorithm/string.hpp>
35
38
39using namespace std;
40
41namespace Nektar::SolverUtils
42{
43
46 "Body Forcing");
49 "Field Forcing");
50
52 const std::weak_ptr<EquationSystem> &pEquation)
53 : Forcing(pSession, pEquation), m_hasTimeFcnScaling(false)
54{
55}
56
59 const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
60{
61 m_NumVariable = pNumForcingFields;
62
63 const TiXmlElement *funcNameElmt = pForce->FirstChildElement("BODYFORCE");
64 if (!funcNameElmt)
65 {
66 funcNameElmt = pForce->FirstChildElement("FIELDFORCE");
67
68 ASSERTL0(funcNameElmt,
69 "Requires BODYFORCE or FIELDFORCE tag "
70 "specifying function name which prescribes body force.");
71 }
72
73 m_funcName = funcNameElmt->GetText();
74 ASSERTL0(m_session->DefinesFunction(m_funcName),
75 "Function '" + m_funcName + "' not defined.");
76
77 m_homogeneous = pFields[0]->GetExpType() == MultiRegions::e3DH1D ||
78 pFields[0]->GetExpType() == MultiRegions::e3DH2D;
79
80 // Time function is optional
81 funcNameElmt = pForce->FirstChildElement("BODYFORCETIMEFCN");
82 if (!funcNameElmt)
83 {
84 funcNameElmt = pForce->FirstChildElement("FIELDFORCETIMEFCN");
85 }
86
87 // Load time function if specified
88 if (funcNameElmt)
89 {
90 std::string funcNameTime = funcNameElmt->GetText();
91
92 ASSERTL0(!funcNameTime.empty(),
93 "Expression must be given in BODYFORCETIMEFCN or "
94 "FIELDFORCETIMEFCN.");
95
97 m_session->GetInterpreter(), funcNameTime);
98
100 }
101
103 for (int i = 0; i < m_NumVariable; ++i)
104 {
105 tmp[i] = pFields[i]->GetPhys();
106 }
107
108 std::map<std::string, int> varIndex;
109 for (int i = 0; i < m_NumVariable; ++i)
110 {
111 varIndex[m_session->GetVariable(i)] = i;
112 if (m_session->DefinesFunction(m_funcName, m_session->GetVariable(i)))
113 {
114 m_eqnEvars[i] = vector<int>();
115 }
116 }
117 m_hasEvars = false;
118 for (auto &it : m_eqnEvars)
119 {
120 string varStr = m_session->GetVariable(it.first);
122 m_session->GetFunctionType(m_funcName, varStr))
123 {
125 m_session->GetFunction(m_funcName, varStr);
126 string vlist = eqn->GetVlist();
127 if (!boost::iequals(vlist, "x y z t"))
128 {
129 // Coupled forcing
130 m_hasEvars = true;
131 std::vector<std::string> vars;
132 boost::split(vars, vlist, boost::is_any_of(", "));
133 for (size_t j = 4; j < vars.size(); ++j)
134 {
135 if (vars[j].size() != 0 &&
136 varIndex.find(vars[j]) == varIndex.end())
137 {
139 "Variable '" + vars[j] +
140 "' cannot be used as equation variable in "
141 "the body force '" +
142 m_funcName + "'.");
143 }
144 if (vars[j].size() &&
145 varIndex.find(vars[j]) != varIndex.end())
146 {
147 it.second.push_back(varIndex[vars[j]]);
148 }
149 }
150 }
151 }
152 }
153
155 for (const auto &it : m_eqnEvars)
156 {
157 m_Forcing[it.first] =
158 Array<OneD, NekDouble>(pFields[0]->GetTotPoints(), 0.0);
159 }
160
161 Update(pFields, tmp, 0.0);
162}
163
166 const Array<OneD, Array<OneD, NekDouble>> &inarray, const NekDouble &time)
167{
168 std::vector<Array<OneD, const NekDouble>> fielddata;
169 int nq = pFields[0]->GetNpoints();
170 if (m_hasEvars)
171 {
172 Array<OneD, NekDouble> xc(nq), yc(nq), zc(nq), t(nq, time);
173 pFields[0]->GetCoords(xc, yc, zc);
174 fielddata.push_back(xc);
175 fielddata.push_back(yc);
176 fielddata.push_back(zc);
177 fielddata.push_back(t);
178 }
179
180 for (const auto &it : m_eqnEvars)
181 {
182 int i = it.first;
183 if (it.second.size() > 0)
184 {
185 // Coupled forcing, reset fielddata for each equation
186 fielddata.resize(4);
187 for (int j : it.second)
188 {
189 if (m_homogeneous && pFields[i]->GetWaveSpace())
190 {
192 pFields[i]->HomogeneousBwdTrans(nq, inarray[j], tmp);
193 fielddata.push_back(tmp);
194 }
195 else
196 {
197 fielddata.push_back(inarray[j]);
198 }
199 }
200 m_session->GetFunction(m_funcName, m_session->GetVariable(i))
201 ->Evaluate(fielddata, m_Forcing[i]);
202 }
203 else
204 {
205 GetFunction(pFields, m_session, m_funcName, true)
206 ->Evaluate(m_session->GetVariable(i), m_Forcing[i], time);
207 }
208
209 // If homogeneous expansion is used, transform the forcing term to
210 // be in the Fourier space
211 if (m_homogeneous)
212 {
213 pFields[i]->HomogeneousFwdTrans(pFields[i]->GetTotPoints(),
214 m_Forcing[i], m_Forcing[i]);
215 }
216 }
217}
218
221 const Array<OneD, Array<OneD, NekDouble>> &inarray,
222 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
223{
225 {
226 Array<OneD, NekDouble> TimeFcn(1);
227 EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
228
229 for (const auto &it : m_eqnEvars)
230 {
231 int i = it.first;
232 Vmath::Svtvp(outarray[i].size(), TimeFcn[0], m_Forcing[i], 1,
233 outarray[i], 1, outarray[i], 1);
234 }
235 }
236 else
237 {
238 Update(fields, inarray, time);
239
240 for (const auto &it : m_eqnEvars)
241 {
242 int i = it.first;
243 Vmath::Vadd(outarray[i].size(), outarray[i], 1, m_Forcing[i], 1,
244 outarray[i], 1);
245 }
246 }
247}
248
251 const Array<OneD, Array<OneD, NekDouble>> &inarray,
252 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
253{
254 int ncoeff = outarray[m_NumVariable - 1].size();
255 Array<OneD, NekDouble> tmp(ncoeff, 0.0);
256
258 {
259 Array<OneD, NekDouble> TimeFcn(1);
260 EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
261
262 for (const auto &it : m_eqnEvars)
263 {
264 int i = it.first;
265 fields[i]->FwdTrans(m_Forcing[i], tmp);
266 Vmath::Svtvp(ncoeff, TimeFcn[0], tmp, 1, outarray[i], 1,
267 outarray[i], 1);
268 }
269 }
270 else
271 {
272 Update(fields, inarray, time);
273
274 for (const auto &it : m_eqnEvars)
275 {
276 int i = it.first;
277 fields[i]->FwdTrans(m_Forcing[i], tmp);
278 Vmath::Vadd(ncoeff, outarray[i], 1, tmp, 1, outarray[i], 1);
279 }
280 }
281}
282
283} // namespace Nektar::SolverUtils
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
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.
SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce) override
Definition: ForcingBody.cpp:57
static std::string classNameBody
Name of the class.
Definition: ForcingBody.h:67
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
void Update(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const Array< OneD, Array< OneD, NekDouble > > &inarray, const NekDouble &time)
LibUtilities::EquationSharedPtr m_timeFcnEqn
Definition: ForcingBody.h:91
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
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.
Definition: ForcingBody.h:54
static std::string classNameField
Definition: ForcingBody.h:68
ForcingBody(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
Definition: ForcingBody.cpp:51
std::map< int, std::vector< int > > m_eqnEvars
Definition: ForcingBody.h:94
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 void EvaluateTimeFunction(LibUtilities::SessionReaderSharedPtr pSession, std::string pFieldName, Array< OneD, NekDouble > &pArray, const std::string &pFunctionName, NekDouble pTime=NekDouble(0))
Definition: Forcing.cpp:168
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
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
double NekDouble
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 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