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(
52  const std::weak_ptr<EquationSystem> &pEquation)
53  : m_session(pSession), m_equ(pEquation)
54  {
55 
56  }
57 
60  const unsigned int& pNumForcingFields,
61  const TiXmlElement* pForce)
62  {
63  v_InitObject(pFields, pNumForcingFields, pForce);
64  }
65 
66 
67  /**
68  * @param fields Expansion lists corresponding to input arrays
69  * @param inarray u^n from previous timestep
70  * @param outarray output array to append forcing to
71  */
74  const Array<OneD, Array<OneD, NekDouble> >& inarray,
75  Array<OneD, Array<OneD, NekDouble> >& outarray,
76  const NekDouble& time)
77  {
78  v_Apply(fields, inarray, outarray, time);
79  }
80 
83  const Array<OneD, Array<OneD, NekDouble> >& inarray,
84  Array<OneD, Array<OneD, NekDouble> >& outarray,
85  const NekDouble& time)
86  {
87  v_PreApply(fields, inarray, outarray, time);
88  }
89 
92  const Array<OneD, Array<OneD, NekDouble> >& inarray,
93  Array<OneD, Array<OneD, NekDouble> >& outarray,
94  const NekDouble& time)
95  {
96  boost::ignore_unused(fields, time);
97  if (&inarray != &outarray)
98  {
99  int nvar = std::min(inarray.size(), outarray.size());
100  for (int i=0; i < nvar; ++i)
101  {
102  if (&inarray[i] != &outarray[i])
103  {
104  int np = std::min(inarray[i].size(), outarray[i].size());
105  Vmath::Vcopy(np, inarray[i], 1, outarray[i], 1);
106  }
107  }
108  }
109  }
110 
111  /**
112  * @param fields Expansion lists corresponding to input arrays
113  * @param inarray u^n from previous timestep
114  * @param outarray output array to append forcing to
115  */
118  const Array<OneD, Array<OneD, NekDouble> > &inarray,
119  Array<OneD, Array<OneD, NekDouble> > &outarray,
120  const NekDouble& time)
121  {
122  v_ApplyCoeff(fields, inarray, outarray, time);
123  }
124 
125  /**
126  *
127  */
128  vector<ForcingSharedPtr> Forcing::Load(
129  const LibUtilities::SessionReaderSharedPtr &pSession,
130  const std::weak_ptr<EquationSystem> &pEquation,
132  const unsigned int& pNumForcingFields)
133  {
134  vector<ForcingSharedPtr> vForceList;
135 
136  if (!pSession->DefinesElement("Nektar/Forcing"))
137  {
138  return vForceList;
139  }
140 
141  TiXmlElement* vForcing = pSession->GetElement("Nektar/Forcing");
142  if (vForcing)
143  {
144  unsigned int vNumForcingFields = pNumForcingFields;
145  if (!pNumForcingFields)
146  {
147  vNumForcingFields = pFields.size();
148  }
149 
150  TiXmlElement* vForce = vForcing->FirstChildElement("FORCE");
151  while (vForce)
152  {
153  string vType = vForce->Attribute("TYPE");
154 
155  vForceList.push_back(GetForcingFactory().CreateInstance(
156  vType, pSession, pEquation, pFields,
157  vNumForcingFields, vForce));
158  vForce = vForce->NextSiblingElement("FORCE");
159  }
160  }
161  return vForceList;
162  }
163 
165  {
166  return m_Forcing;
167  }
168 
170  {
171  return m_Forcing;
172  }
173 
176  std::string pFieldName,
177  Array<OneD, NekDouble>& pArray,
178  const std::string& pFunctionName,
179  NekDouble pTime)
180  {
181  ASSERTL0(pSession->DefinesFunction(pFunctionName),
182  "Function '" + pFunctionName + "' does not exist.");
183 
185  pSession->GetFunction(pFunctionName, pFieldName);
186 
187  EvaluateTimeFunction(pTime,ffunc,pArray);
188  }
189 
190 
192  const NekDouble pTime,
194  Array<OneD, NekDouble>& pArray)
195  {
196  // dummy array of zero pts.
197  Array<OneD, NekDouble> x0(pArray.size(),0.0);
198 
199  pEqn->Evaluate(x0, x0, x0, pTime, pArray);
200  }
201 
204  const LibUtilities::SessionReaderSharedPtr &pSession,
205  std::string pName,
206  bool pCache)
207  {
208  if (pCache)
209  {
210  if ((m_sessionFunctions.find(pName) == m_sessionFunctions.end())
211  || (m_sessionFunctions[pName]->GetSession() != pSession)
212  || (m_sessionFunctions[pName]->GetExpansion() != pFields[0])
213  )
214  {
215  m_sessionFunctions[pName] =
217  pSession, pFields[0], pName, pCache);
218  }
219 
220  return m_sessionFunctions[pName];
221  }
222  else
223  {
224  return SessionFunctionSharedPtr(new SessionFunction(pSession, pFields[0], pName, pCache));
225  }
226  }
227 
230  const Array<OneD, Array<OneD, NekDouble> > &inarray,
231  Array<OneD, Array<OneD, NekDouble> > &outarray,
232  const NekDouble &time)
233  {
234  boost::ignore_unused(fields, inarray, outarray, time);
235  ASSERTL0(false, "v_ApplyCoeff not defined");
236  }
237  }
238 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
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
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:90
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:122
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)=0
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:72
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:128
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:58
SOLVER_UTILS_EXPORT Array< OneD, Array< OneD, NekDouble > > & UpdateForces()
Definition: Forcing.cpp:164
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:116
SOLVER_UTILS_EXPORT const Array< OneD, const Array< OneD, NekDouble > > & GetForces()
Definition: Forcing.cpp:169
std::map< std::string, SolverUtils::SessionFunctionSharedPtr > m_sessionFunctions
Map of known SessionFunctions.
Definition: Forcing.h:126
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:174
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:81
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:202
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:228
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:131
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:1
double NekDouble
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1199