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 
36 #include <MultiRegions/ExpList.h>
38 
39 using namespace std;
40 
41 namespace Nektar
42 {
43 namespace SolverUtils
44 {
45 
46 std::string ForcingBody::classNameBody =
47  GetForcingFactory().RegisterCreatorFunction("Body", ForcingBody::create,
48  "Body Forcing");
49 std::string ForcingBody::classNameField =
50  GetForcingFactory().RegisterCreatorFunction("Field", ForcingBody::create,
51  "Field Forcing");
52 
53 ForcingBody::ForcingBody(const LibUtilities::SessionReaderSharedPtr &pSession,
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 
98  m_session->SubstituteExpressions(funcNameTime);
100  m_session->GetInterpreter(), funcNameTime);
101 
102  m_hasTimeFcnScaling = true;
103  }
104 
106  for (int i = 0; i < m_NumVariable; ++i)
107  {
108  tmp[i] = pFields[i]->GetPhys();
109  }
110 
111  std::map<std::string, int> varIndex;
112  for (int i = 0; i < m_NumVariable; ++i)
113  {
114  varIndex[m_session->GetVariable(i)] = i;
115  if (m_session->DefinesFunction(m_funcName, m_session->GetVariable(i)))
116  {
117  m_eqnEvars[i] = vector<int>();
118  }
119  }
120  m_hasEvars = false;
121  for (auto &it : m_eqnEvars)
122  {
123  string varStr = m_session->GetVariable(it.first);
125  m_session->GetFunctionType(m_funcName, varStr))
126  {
128  m_session->GetFunction(m_funcName, varStr);
129  string vlist = eqn->GetVlist();
130  if (!boost::iequals(vlist, "x y z t"))
131  {
132  // Coupled forcing
133  m_hasEvars = true;
134  std::vector<std::string> vars;
135  boost::split(vars, vlist, boost::is_any_of(", "));
136  for (size_t j = 4; j < vars.size(); ++j)
137  {
138  if (vars[j].size() != 0 &&
139  varIndex.find(vars[j]) == varIndex.end())
140  {
142  "Variable '" + vars[j] +
143  "' cannot be used as equation variable in "
144  "the body force '" +
145  m_funcName + "'.");
146  }
147  if (vars[j].size() &&
148  varIndex.find(vars[j]) != varIndex.end())
149  {
150  it.second.push_back(varIndex[vars[j]]);
151  }
152  }
153  }
154  }
155  }
156 
158  for (const auto &it : m_eqnEvars)
159  {
160  m_Forcing[it.first] =
161  Array<OneD, NekDouble>(pFields[0]->GetTotPoints(), 0.0);
162  }
163 
164  Update(pFields, tmp, 0.0);
165 }
166 
169  const Array<OneD, Array<OneD, NekDouble>> &inarray, const NekDouble &time)
170 {
171  std::vector<Array<OneD, const NekDouble>> fielddata;
172  int nq = pFields[0]->GetNpoints();
173  if (m_hasEvars)
174  {
175  Array<OneD, NekDouble> xc(nq), yc(nq), zc(nq), t(nq, time);
176  pFields[0]->GetCoords(xc, yc, zc);
177  fielddata.push_back(xc);
178  fielddata.push_back(yc);
179  fielddata.push_back(zc);
180  fielddata.push_back(t);
181  }
182 
183  for (const auto &it : m_eqnEvars)
184  {
185  int i = it.first;
186  if (it.second.size() > 0)
187  {
188  // Coupled forcing, reset fielddata for each equation
189  fielddata.resize(4);
190  for (int j : it.second)
191  {
192  if (m_homogeneous && pFields[i]->GetWaveSpace())
193  {
194  Array<OneD, NekDouble> tmp(nq);
195  pFields[i]->HomogeneousBwdTrans(nq, inarray[j], tmp);
196  fielddata.push_back(tmp);
197  }
198  else
199  {
200  fielddata.push_back(inarray[j]);
201  }
202  }
203  m_session->GetFunction(m_funcName, m_session->GetVariable(i))
204  ->Evaluate(fielddata, m_Forcing[i]);
205  }
206  else
207  {
208  GetFunction(pFields, m_session, m_funcName, true)
209  ->Evaluate(m_session->GetVariable(i), m_Forcing[i], time);
210  }
211 
212  // If homogeneous expansion is used, transform the forcing term to
213  // be in the Fourier space
214  if (m_homogeneous)
215  {
216  pFields[i]->HomogeneousFwdTrans(pFields[i]->GetTotPoints(),
217  m_Forcing[i], m_Forcing[i]);
218  }
219  }
220 }
221 
224  const Array<OneD, Array<OneD, NekDouble>> &inarray,
225  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
226 {
228  {
229  Array<OneD, NekDouble> TimeFcn(1);
230  EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
231 
232  for (const auto &it : m_eqnEvars)
233  {
234  int i = it.first;
235  Vmath::Svtvp(outarray[i].size(), TimeFcn[0], m_Forcing[i], 1,
236  outarray[i], 1, outarray[i], 1);
237  }
238  }
239  else
240  {
241  Update(fields, inarray, time);
242 
243  for (const auto &it : m_eqnEvars)
244  {
245  int i = it.first;
246  Vmath::Vadd(outarray[i].size(), outarray[i], 1, m_Forcing[i], 1,
247  outarray[i], 1);
248  }
249  }
250 }
251 
254  const Array<OneD, Array<OneD, NekDouble>> &inarray,
255  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
256 {
257  int ncoeff = outarray[m_NumVariable - 1].size();
258  Array<OneD, NekDouble> tmp(ncoeff, 0.0);
259 
261  {
262  Array<OneD, NekDouble> TimeFcn(1);
263  EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
264 
265  for (const auto &it : m_eqnEvars)
266  {
267  int i = it.first;
268  fields[i]->FwdTrans(m_Forcing[i], tmp);
269  Vmath::Svtvp(ncoeff, TimeFcn[0], tmp, 1, outarray[i], 1,
270  outarray[i], 1);
271  }
272  }
273  else
274  {
275  Update(fields, inarray, time);
276 
277  for (const auto &it : m_eqnEvars)
278  {
279  int i = it.first;
280  fields[i]->FwdTrans(m_Forcing[i], tmp);
281  Vmath::Vadd(ncoeff, outarray[i], 1, tmp, 1, outarray[i], 1);
282  }
283  }
284 }
285 
286 } // namespace SolverUtils
287 } // 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
LibUtilities::EquationSharedPtr m_timeFcnEqn
Definition: ForcingBody.h:93
void Update(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const Array< OneD, Array< OneD, NekDouble >> &inarray, const NekDouble &time)
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
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
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:122
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:120
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:166
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:190
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:116
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:622
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:359