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 
35 #include <MultiRegions/ExpList.h>
37 
38 using namespace std;
39 
40 namespace Nektar
41 {
42 namespace SolverUtils
43 {
44 
45 std::string ForcingBody::classNameBody =
46  GetForcingFactory().RegisterCreatorFunction("Body", ForcingBody::create,
47  "Body Forcing");
48 std::string ForcingBody::classNameField =
49  GetForcingFactory().RegisterCreatorFunction("Field", ForcingBody::create,
50  "Field Forcing");
51 
52 ForcingBody::ForcingBody(const LibUtilities::SessionReaderSharedPtr &pSession,
53  const std::weak_ptr<EquationSystem> &pEquation)
54  : Forcing(pSession, pEquation), m_hasTimeFcnScaling(false)
55 {
56 }
57 
60  const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
61 {
62  m_NumVariable = pNumForcingFields;
63 
64  const TiXmlElement *funcNameElmt = pForce->FirstChildElement("BODYFORCE");
65  if (!funcNameElmt)
66  {
67  funcNameElmt = pForce->FirstChildElement("FIELDFORCE");
68 
69  ASSERTL0(funcNameElmt,
70  "Requires BODYFORCE or FIELDFORCE tag "
71  "specifying function name which prescribes body force.");
72  }
73 
74  m_funcName = funcNameElmt->GetText();
75  ASSERTL0(m_session->DefinesFunction(m_funcName),
76  "Function '" + m_funcName + "' not defined.");
77 
78  bool singleMode, halfMode;
79  m_session->MatchSolverInfo("ModeType", "SingleMode", singleMode, false);
80  m_session->MatchSolverInfo("ModeType", "HalfMode", halfMode, false);
81  bool homogeneous = pFields[0]->GetExpType() == MultiRegions::e3DH1D ||
82  pFields[0]->GetExpType() == MultiRegions::e3DH2D;
83  m_transform = (singleMode || halfMode || homogeneous);
84 
85  // Time function is optional
86  funcNameElmt = pForce->FirstChildElement("BODYFORCETIMEFCN");
87  if (!funcNameElmt)
88  {
89  funcNameElmt = pForce->FirstChildElement("FIELDFORCETIMEFCN");
90  }
91 
92  // Load time function if specified
93  if (funcNameElmt)
94  {
95  std::string funcNameTime = funcNameElmt->GetText();
96 
97  ASSERTL0(!funcNameTime.empty(),
98  "Expression must be given in BODYFORCETIMEFCN or "
99  "FIELDFORCETIMEFCN.");
100 
101  m_session->SubstituteExpressions(funcNameTime);
103  m_session->GetInterpreter(), funcNameTime);
104 
105  m_hasTimeFcnScaling = true;
106  }
107 
109  for (int i = 0; i < m_NumVariable; ++i)
110  {
111  m_Forcing[i] = Array<OneD, NekDouble>(pFields[0]->GetTotPoints(), 0.0);
112  }
113 
114  Array<OneD, Array<OneD, NekDouble>> tmp(pFields.size());
115  for (int i = 0; i < m_NumVariable; ++i)
116  {
117  tmp[i] = pFields[i]->GetPhys();
118  }
119 
120  Update(pFields, tmp, 0.0);
121 }
122 
125  const Array<OneD, Array<OneD, NekDouble>> &inarray, const NekDouble &time)
126 {
127  for (int i = 0; i < m_NumVariable; ++i)
128  {
130  m_session->GetFunctionType(m_funcName, m_session->GetVariable(i)))
131  {
133  m_session->GetFunction(m_funcName, m_session->GetVariable(i));
134  if (!boost::iequals(eqn->GetVlist(), "x y z t"))
135  {
136  // Coupled forcing
137  int nq = pFields[0]->GetNpoints();
138  Array<OneD, NekDouble> xc(nq), yc(nq), zc(nq), t(nq, time);
139  std::string varstr = "x y z";
140  std::vector<Array<OneD, const NekDouble>> fielddata = {xc, yc,
141  zc, t};
142 
143  for (int j = 0; j < m_NumVariable; ++j)
144  {
145  varstr += " " + m_session->GetVariable(j);
146  fielddata.push_back(inarray[j]);
147  }
148 
149  // Evaluate function
150  m_session->GetFunction(m_funcName, m_session->GetVariable(i))
151  ->Evaluate(fielddata, m_Forcing[i]);
152  continue;
153  }
154  }
155  std::string s_FieldStr = m_session->GetVariable(i);
156  ASSERTL0(m_session->DefinesFunction(m_funcName, s_FieldStr),
157  "Variable '" + s_FieldStr + "' not defined.");
158  GetFunction(pFields, m_session, m_funcName, true)
159  ->Evaluate(s_FieldStr, m_Forcing[i], time);
160  }
161 
162  // If singleMode or halfMode, transform the forcing term to be in
163  // physical space in the plane, but Fourier space in the homogeneous
164  // direction
165  if (m_transform)
166  {
167  for (int i = 0; i < m_NumVariable; ++i)
168  {
169  pFields[0]->HomogeneousFwdTrans(m_Forcing[i], m_Forcing[i]);
170  }
171  }
172 }
173 
176  const Array<OneD, Array<OneD, NekDouble>> &inarray,
177  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
178 {
180  {
181  Array<OneD, NekDouble> TimeFcn(1);
182 
183  for (int i = 0; i < m_NumVariable; i++)
184  {
185  EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
186 
187  Vmath::Svtvp(outarray[i].size(), TimeFcn[0], m_Forcing[i], 1,
188  outarray[i], 1, outarray[i], 1);
189  }
190  }
191  else
192  {
193  Update(fields, inarray, time);
194 
195  for (int i = 0; i < m_NumVariable; i++)
196  {
197  Vmath::Vadd(outarray[i].size(), outarray[i], 1, m_Forcing[i], 1,
198  outarray[i], 1);
199  }
200  }
201 }
202 
205  const Array<OneD, Array<OneD, NekDouble>> &inarray,
206  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
207 {
208  int ncoeff = outarray[m_NumVariable - 1].size();
209  Array<OneD, NekDouble> tmp(ncoeff, 0.0);
210 
212  {
213  Array<OneD, NekDouble> TimeFcn(1);
214 
215  for (int i = 0; i < m_NumVariable; ++i)
216  {
217  EvaluateTimeFunction(time, m_timeFcnEqn, TimeFcn);
218 
219  fields[i]->FwdTrans(m_Forcing[i], tmp);
220 
221  Vmath::Svtvp(ncoeff, TimeFcn[0], tmp, 1, outarray[i], 1,
222  outarray[i], 1);
223  }
224  }
225  else
226  {
227  Update(fields, inarray, time);
228 
229  for (int i = 0; i < m_NumVariable; ++i)
230  {
231  fields[i]->FwdTrans(m_Forcing[i], tmp);
232 
233  Vmath::Vadd(ncoeff, outarray[i], 1, tmp, 1, outarray[i], 1);
234  }
235  }
236 }
237 
238 } // namespace SolverUtils
239 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
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_Apply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble >> &inarray, Array< OneD, Array< OneD, NekDouble >> &outarray, const NekDouble &time)
LibUtilities::EquationSharedPtr m_timeFcnEqn
Definition: ForcingBody.h:90
void Update(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const Array< OneD, Array< OneD, NekDouble >> &inarray, 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:58
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)
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:130
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:1
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