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
42{
43namespace SolverUtils
44{
45
48 "Body Forcing");
51 "Field Forcing");
52
54 const std::weak_ptr<EquationSystem> &pEquation)
55 : Forcing(pSession, pEquation), m_hasTimeFcnScaling(false)
56{
57}
58
61 const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
62{
63 m_NumVariable = pNumForcingFields;
64
65 const TiXmlElement *funcNameElmt = pForce->FirstChildElement("BODYFORCE");
66 if (!funcNameElmt)
67 {
68 funcNameElmt = pForce->FirstChildElement("FIELDFORCE");
69
70 ASSERTL0(funcNameElmt,
71 "Requires BODYFORCE or FIELDFORCE tag "
72 "specifying function name which prescribes body force.");
73 }
74
75 m_funcName = funcNameElmt->GetText();
76 ASSERTL0(m_session->DefinesFunction(m_funcName),
77 "Function '" + m_funcName + "' not defined.");
78
79 m_homogeneous = pFields[0]->GetExpType() == MultiRegions::e3DH1D ||
80 pFields[0]->GetExpType() == MultiRegions::e3DH2D;
81
82 // Time function is optional
83 funcNameElmt = pForce->FirstChildElement("BODYFORCETIMEFCN");
84 if (!funcNameElmt)
85 {
86 funcNameElmt = pForce->FirstChildElement("FIELDFORCETIMEFCN");
87 }
88
89 // Load time function if specified
90 if (funcNameElmt)
91 {
92 std::string funcNameTime = funcNameElmt->GetText();
93
94 ASSERTL0(!funcNameTime.empty(),
95 "Expression must be given in BODYFORCETIMEFCN or "
96 "FIELDFORCETIMEFCN.");
97
99 m_session->GetInterpreter(), funcNameTime);
100
101 m_hasTimeFcnScaling = true;
102 }
103
105 for (int i = 0; i < m_NumVariable; ++i)
106 {
107 tmp[i] = pFields[i]->GetPhys();
108 }
109
110 std::map<std::string, int> varIndex;
111 for (int i = 0; i < m_NumVariable; ++i)
112 {
113 varIndex[m_session->GetVariable(i)] = i;
114 if (m_session->DefinesFunction(m_funcName, m_session->GetVariable(i)))
115 {
116 m_eqnEvars[i] = vector<int>();
117 }
118 }
119 m_hasEvars = false;
120 for (auto &it : m_eqnEvars)
121 {
122 string varStr = m_session->GetVariable(it.first);
124 m_session->GetFunctionType(m_funcName, varStr))
125 {
127 m_session->GetFunction(m_funcName, varStr);
128 string vlist = eqn->GetVlist();
129 if (!boost::iequals(vlist, "x y z t"))
130 {
131 // Coupled forcing
132 m_hasEvars = true;
133 std::vector<std::string> vars;
134 boost::split(vars, vlist, boost::is_any_of(", "));
135 for (size_t j = 4; j < vars.size(); ++j)
136 {
137 if (vars[j].size() != 0 &&
138 varIndex.find(vars[j]) == varIndex.end())
139 {
141 "Variable '" + vars[j] +
142 "' cannot be used as equation variable in "
143 "the body force '" +
144 m_funcName + "'.");
145 }
146 if (vars[j].size() &&
147 varIndex.find(vars[j]) != varIndex.end())
148 {
149 it.second.push_back(varIndex[vars[j]]);
150 }
151 }
152 }
153 }
154 }
155
157 for (const auto &it : m_eqnEvars)
158 {
159 m_Forcing[it.first] =
160 Array<OneD, NekDouble>(pFields[0]->GetTotPoints(), 0.0);
161 }
162
163 Update(pFields, tmp, 0.0);
164}
165
168 const Array<OneD, Array<OneD, NekDouble>> &inarray, const NekDouble &time)
169{
170 std::vector<Array<OneD, const NekDouble>> fielddata;
171 int nq = pFields[0]->GetNpoints();
172 if (m_hasEvars)
173 {
174 Array<OneD, NekDouble> xc(nq), yc(nq), zc(nq), t(nq, time);
175 pFields[0]->GetCoords(xc, yc, zc);
176 fielddata.push_back(xc);
177 fielddata.push_back(yc);
178 fielddata.push_back(zc);
179 fielddata.push_back(t);
180 }
181
182 for (const auto &it : m_eqnEvars)
183 {
184 int i = it.first;
185 if (it.second.size() > 0)
186 {
187 // Coupled forcing, reset fielddata for each equation
188 fielddata.resize(4);
189 for (int j : it.second)
190 {
191 if (m_homogeneous && pFields[i]->GetWaveSpace())
192 {
194 pFields[i]->HomogeneousBwdTrans(nq, inarray[j], tmp);
195 fielddata.push_back(tmp);
196 }
197 else
198 {
199 fielddata.push_back(inarray[j]);
200 }
201 }
202 m_session->GetFunction(m_funcName, m_session->GetVariable(i))
203 ->Evaluate(fielddata, m_Forcing[i]);
204 }
205 else
206 {
207 GetFunction(pFields, m_session, m_funcName, true)
208 ->Evaluate(m_session->GetVariable(i), m_Forcing[i], time);
209 }
210
211 // If homogeneous expansion is used, transform the forcing term to
212 // be in the Fourier space
213 if (m_homogeneous)
214 {
215 pFields[i]->HomogeneousFwdTrans(pFields[i]->GetTotPoints(),
216 m_Forcing[i], m_Forcing[i]);
217 }
218 }
219}
220
223 const Array<OneD, Array<OneD, NekDouble>> &inarray,
224 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
225{
227 {
228 Array<OneD, NekDouble> TimeFcn(1);
229 EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
230
231 for (const auto &it : m_eqnEvars)
232 {
233 int i = it.first;
234 Vmath::Svtvp(outarray[i].size(), TimeFcn[0], m_Forcing[i], 1,
235 outarray[i], 1, outarray[i], 1);
236 }
237 }
238 else
239 {
240 Update(fields, inarray, time);
241
242 for (const auto &it : m_eqnEvars)
243 {
244 int i = it.first;
245 Vmath::Vadd(outarray[i].size(), outarray[i], 1, m_Forcing[i], 1,
246 outarray[i], 1);
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 ncoeff = outarray[m_NumVariable - 1].size();
257 Array<OneD, NekDouble> tmp(ncoeff, 0.0);
258
260 {
261 Array<OneD, NekDouble> TimeFcn(1);
262 EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
263
264 for (const auto &it : m_eqnEvars)
265 {
266 int i = it.first;
267 fields[i]->FwdTrans(m_Forcing[i], tmp);
268 Vmath::Svtvp(ncoeff, TimeFcn[0], tmp, 1, outarray[i], 1,
269 outarray[i], 1);
270 }
271 }
272 else
273 {
274 Update(fields, inarray, time);
275
276 for (const auto &it : m_eqnEvars)
277 {
278 int i = it.first;
279 fields[i]->FwdTrans(m_Forcing[i], tmp);
280 Vmath::Vadd(ncoeff, outarray[i], 1, tmp, 1, outarray[i], 1);
281 }
282 }
283}
284
285} // namespace SolverUtils
286} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce) override
Definition: ForcingBody.cpp:59
static std::string classNameBody
Name of the class.
Definition: ForcingBody.h:69
virtual 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:93
virtual 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:56
static std::string classNameField
Definition: ForcingBody.h:70
ForcingBody(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
Definition: ForcingBody.cpp:53
std::map< int, std::vector< int > > m_eqnEvars
Definition: ForcingBody.h:96
Defines a forcing term to be explicitly applied.
Definition: Forcing.h:73
int m_NumVariable
Number of variables.
Definition: Forcing.h:123
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:121
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:170
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:194
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:117
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:129
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:44
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
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.cpp:617
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.cpp:354