Nektar++
Forcing.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Forcing.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: Abstract base class for forcing terms.
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
37 
38 using namespace std;
39 
40 namespace Nektar
41 {
42 namespace SolverUtils
43 {
45 {
46  static ForcingFactory instance;
47  return instance;
48 }
49 
50 Forcing::Forcing(const LibUtilities::SessionReaderSharedPtr &pSession,
51  const std::weak_ptr<EquationSystem> &pEquation)
52  : m_session(pSession), m_equ(pEquation)
53 {
54 }
55 
58  const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
59 {
60  v_InitObject(pFields, pNumForcingFields, pForce);
61 }
62 
63 /**
64  * @param fields Expansion lists corresponding to input arrays
65  * @param inarray u^n from previous timestep
66  * @param outarray output array to append forcing to
67  */
69  const Array<OneD, Array<OneD, NekDouble>> &inarray,
70  Array<OneD, Array<OneD, NekDouble>> &outarray,
71  const NekDouble &time)
72 {
73  v_Apply(fields, inarray, outarray, time);
74 }
75 
78  const Array<OneD, Array<OneD, NekDouble>> &inarray,
79  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
80 {
81  v_PreApply(fields, inarray, outarray, time);
82 }
83 
86  const Array<OneD, Array<OneD, NekDouble>> &inarray,
87  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
88 {
89  boost::ignore_unused(fields, time);
90  if (&inarray != &outarray)
91  {
92  int nvar = std::min(inarray.size(), outarray.size());
93  for (int i = 0; i < nvar; ++i)
94  {
95  if (&inarray[i] != &outarray[i])
96  {
97  int np = std::min(inarray[i].size(), outarray[i].size());
98  Vmath::Vcopy(np, inarray[i], 1, outarray[i], 1);
99  }
100  }
101  }
102 }
103 
104 /**
105  * @param fields Expansion lists corresponding to input arrays
106  * @param inarray u^n from previous timestep
107  * @param outarray output array to append forcing to
108  */
111  const Array<OneD, Array<OneD, NekDouble>> &inarray,
112  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
113 {
114  v_ApplyCoeff(fields, inarray, outarray, time);
115 }
116 
117 /**
118  *
119  */
120 vector<ForcingSharedPtr> Forcing::Load(
121  const LibUtilities::SessionReaderSharedPtr &pSession,
122  const std::weak_ptr<EquationSystem> &pEquation,
124  const unsigned int &pNumForcingFields)
125 {
126  vector<ForcingSharedPtr> vForceList;
127 
128  if (!pSession->DefinesElement("Nektar/Forcing"))
129  {
130  return vForceList;
131  }
132 
133  TiXmlElement *vForcing = pSession->GetElement("Nektar/Forcing");
134  if (vForcing)
135  {
136  unsigned int vNumForcingFields = pNumForcingFields;
137  if (!pNumForcingFields)
138  {
139  vNumForcingFields = pFields.size();
140  }
141 
142  TiXmlElement *vForce = vForcing->FirstChildElement("FORCE");
143  while (vForce)
144  {
145  string vType = vForce->Attribute("TYPE");
146 
147  vForceList.push_back(GetForcingFactory().CreateInstance(
148  vType, pSession, pEquation, pFields, vNumForcingFields,
149  vForce));
150  vForce = vForce->NextSiblingElement("FORCE");
151  }
152  }
153  return vForceList;
154 }
155 
157 {
158  return m_Forcing;
159 }
160 
162 {
163  return m_Forcing;
164 }
165 
167  LibUtilities::SessionReaderSharedPtr pSession, std::string pFieldName,
168  Array<OneD, NekDouble> &pArray, const std::string &pFunctionName,
169  NekDouble pTime)
170 {
171  ASSERTL0(pSession->DefinesFunction(pFunctionName),
172  "Function '" + pFunctionName + "' does not exist.");
173 
175  pSession->GetFunction(pFunctionName, pFieldName);
176 
177  EvaluateTimeFunction(pTime, ffunc, pArray);
178 }
179 
182  Array<OneD, NekDouble> &pArray)
183 {
184  // dummy array of zero pts.
185  Array<OneD, NekDouble> x0(pArray.size(), 0.0);
186 
187  pEqn->Evaluate(x0, x0, x0, pTime, pArray);
188 }
189 
192  const LibUtilities::SessionReaderSharedPtr &pSession, std::string pName,
193  bool pCache)
194 {
195  if (pCache)
196  {
197  if ((m_sessionFunctions.find(pName) == m_sessionFunctions.end()) ||
198  (m_sessionFunctions[pName]->GetSession() != pSession) ||
199  (m_sessionFunctions[pName]->GetExpansion() != pFields[0]))
200  {
201  m_sessionFunctions[pName] =
203  pSession, pFields[0], pName, pCache);
204  }
205 
206  return m_sessionFunctions[pName];
207  }
208  else
209  {
211  new SessionFunction(pSession, pFields[0], pName, pCache));
212  }
213 }
214 
217  const Array<OneD, Array<OneD, NekDouble>> &inarray,
218  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
219 {
220  boost::ignore_unused(fields, inarray, outarray, time);
221  ASSERTL0(false, "v_ApplyCoeff not defined");
222 }
223 } // namespace SolverUtils
224 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
Provides a generic Factory class.
Definition: NekFactory.hpp:105
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)=0
SOLVER_UTILS_EXPORT void PreApply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble >> &inarray, Array< OneD, Array< OneD, NekDouble >> &outarray, const NekDouble &time)
Change the advection velocity before applying the forcing. For example, subtracting the frame velocit...
Definition: Forcing.cpp:76
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:120
virtual SOLVER_UTILS_EXPORT void v_PreApply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble >> &inarray, Array< OneD, Array< OneD, NekDouble >> &outarray, const NekDouble &time)
Definition: Forcing.cpp:84
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)=0
static SOLVER_UTILS_EXPORT std::vector< ForcingSharedPtr > Load(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields=0)
Definition: Forcing.cpp:120
SOLVER_UTILS_EXPORT void InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
Initialise the forcing object.
Definition: Forcing.cpp:56
SOLVER_UTILS_EXPORT Array< OneD, Array< OneD, NekDouble > > & UpdateForces()
Definition: Forcing.cpp:156
SOLVER_UTILS_EXPORT const Array< OneD, const Array< OneD, NekDouble > > & GetForces()
Definition: Forcing.cpp:161
std::map< std::string, SolverUtils::SessionFunctionSharedPtr > m_sessionFunctions
Map of known SessionFunctions.
Definition: Forcing.h:125
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 void Apply(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble >> &inarray, Array< OneD, Array< OneD, NekDouble >> &outarray, const NekDouble &time)
Apply the forcing.
Definition: Forcing.cpp:68
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)
Definition: Forcing.cpp:215
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
SOLVER_UTILS_EXPORT void ApplyCoeff(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble >> &inarray, Array< OneD, Array< OneD, NekDouble >> &outarray, const NekDouble &time)
Apply the forcing.
Definition: Forcing.cpp:109
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
std::shared_ptr< SessionFunction > SessionFunctionSharedPtr
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255