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 
36 #include <MultiRegions/ExpList.h>
37 
38 using namespace std;
39 
40 namespace Nektar
41 {
42 namespace SolverUtils
43 {
44 
45  std::string ForcingBody::classNameBody = GetForcingFactory().
46  RegisterCreatorFunction("Body",
47  ForcingBody::create,
48  "Body Forcing");
49  std::string ForcingBody::classNameField = GetForcingFactory().
50  RegisterCreatorFunction("Field",
51  ForcingBody::create,
52  "Field Forcing");
53 
54  ForcingBody::ForcingBody(
56  const std::weak_ptr<EquationSystem> &pEquation)
57  : Forcing(pSession, pEquation),
58  m_hasTimeFcnScaling(false)
59  {
60  }
61 
64  const unsigned int& pNumForcingFields,
65  const TiXmlElement* pForce)
66  {
67  m_NumVariable = pNumForcingFields;
68 
69  const TiXmlElement* funcNameElmt = pForce->FirstChildElement("BODYFORCE");
70  if(!funcNameElmt)
71  {
72  funcNameElmt = pForce->FirstChildElement("FIELDFORCE");
73 
74  ASSERTL0(funcNameElmt, "Requires BODYFORCE or FIELDFORCE tag "
75  "specifying function name which prescribes body force.");
76  }
77 
78  m_funcName = funcNameElmt->GetText();
79  ASSERTL0(m_session->DefinesFunction(m_funcName),
80  "Function '" + m_funcName + "' not defined.");
81 
82  bool singleMode, halfMode;
83  m_session->MatchSolverInfo("ModeType","SingleMode",singleMode,false);
84  m_session->MatchSolverInfo("ModeType","HalfMode", halfMode, false);
85  bool homogeneous = pFields[0]->GetExpType() == MultiRegions::e3DH1D ||
86  pFields[0]->GetExpType() == MultiRegions::e3DH2D;
87  m_transform = (singleMode || halfMode || homogeneous);
88 
89  // Time function is optional
90  funcNameElmt = pForce->FirstChildElement("BODYFORCETIMEFCN");
91  if(!funcNameElmt)
92  {
93  funcNameElmt = pForce->FirstChildElement("FIELDFORCETIMEFCN");
94  }
95 
96  // Load time function if specified
97  if(funcNameElmt)
98  {
99  std::string funcNameTime = funcNameElmt->GetText();
100 
101  ASSERTL0(!funcNameTime.empty(),
102  "Expression must be given in BODYFORCETIMEFCN or "
103  "FIELDFORCETIMEFCN.");
104 
105  m_session->SubstituteExpressions(funcNameTime);
107  ::AllocateSharedPtr(m_session->GetInterpreter(),funcNameTime);
108 
109  m_hasTimeFcnScaling = true;
110  }
111 
113  for (int i = 0; i < m_NumVariable; ++i)
114  {
115  m_Forcing[i] = Array<OneD, NekDouble> (pFields[0]->GetTotPoints(), 0.0);
116  }
117 
118  Array<OneD, Array<OneD, NekDouble> > tmp(pFields.num_elements());
119  for (int i = 0; i < pFields.num_elements(); ++i)
120  {
121  tmp[i] = pFields[i]->GetPhys();
122  }
123 
124  Update(pFields, tmp, 0.0);
125  }
126 
127 
130  const Array<OneD, Array<OneD, NekDouble> > &inarray,
131  const NekDouble &time)
132  {
133  LibUtilities::EquationSharedPtr eqn = m_session->GetFunction(
134  m_funcName, m_session->GetVariable(0));
135 
136  if (!boost::iequals(eqn->GetVlist(), "x y z t"))
137  {
138  // Coupled forcing
139  int nq = pFields[0]->GetNpoints();
140  Array<OneD, NekDouble> xc(nq), yc(nq), zc(nq), t(nq, time);
141  std::string varstr = "x y z";
142  std::vector<Array<OneD, const NekDouble>> fielddata = {
143  xc, yc, zc, t};
144 
145  for (int i = 0; i < pFields.num_elements(); ++i)
146  {
147  varstr += " " + m_session->GetVariable(i);
148  fielddata.push_back(inarray[i]);
149  }
150 
151  // Evaluate function
152  for (int i = 0; i < m_NumVariable; ++i)
153  {
154  m_session->GetFunction(m_funcName, m_session->GetVariable(i))->
155  Evaluate(fielddata, m_Forcing[i]);
156  }
157 
158  return;
159  }
160 
161  for (int i = 0; i < m_NumVariable; ++i)
162  {
163  std::string s_FieldStr = m_session->GetVariable(i);
164  ASSERTL0(m_session->DefinesFunction(m_funcName, s_FieldStr),
165  "Variable '" + s_FieldStr + "' not defined.");
166  GetFunction(pFields, m_session, m_funcName, true)->Evaluate(
167  s_FieldStr, m_Forcing[i], time);
168  }
169 
170  // If singleMode or halfMode, transform the forcing term to be in
171  // physical space in the plane, but Fourier space in the homogeneous
172  // direction
173  if (m_transform)
174  {
175  for (int i = 0; i < m_NumVariable; ++i)
176  {
177  pFields[0]->HomogeneousFwdTrans(m_Forcing[i], m_Forcing[i]);
178  }
179  }
180  }
181 
182 
185  const Array<OneD, Array<OneD, NekDouble> > &inarray,
186  Array<OneD, Array<OneD, NekDouble> > &outarray,
187  const NekDouble &time)
188  {
190  {
191  Array<OneD, NekDouble> TimeFcn(1);
192 
193  for (int i = 0; i < m_NumVariable; i++)
194  {
195  EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
196 
197  Vmath::Svtvp(outarray[i].num_elements(), TimeFcn[0],
198  m_Forcing[i], 1,
199  outarray[i], 1,
200  outarray[i], 1);
201  }
202  }
203  else
204  {
205  Update(fields, inarray, time);
206 
207  for (int i = 0; i < m_NumVariable; i++)
208  {
209  Vmath::Vadd(outarray[i].num_elements(), outarray[i], 1,
210  m_Forcing[i], 1, outarray[i], 1);
211  }
212  }
213  }
214 
215 }
216 }
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:131
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:107
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
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:159
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:488
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:44
STL namespace.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
double NekDouble
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:103
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:131
int m_NumVariable
Number of variables.
Definition: Forcing.h:109
LibUtilities::EquationSharedPtr m_timeFcnEqn
Definition: ForcingBody.h:90
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)
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
Definition: ForcingBody.cpp:62
Defines a forcing term to be explicitly applied.
Definition: Forcing.h:72
std::shared_ptr< SessionReader > SessionReaderSharedPtr
void Update(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const Array< OneD, Array< OneD, NekDouble > > &inarray, const NekDouble &time)
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:302