Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Abstract base class for forcing terms.
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 
38 namespace Nektar
39 {
40  namespace SolverUtils
41  {
43  {
44  typedef Loki::SingletonHolder<ForcingFactory,
45  Loki::CreateUsingNew,
46  Loki::NoDestroy,
47  Loki::SingleThreaded> Type;
48  return Type::Instance();
49  }
50 
52  : m_session(pSession)
53  {
54 
55  }
56 
59  const unsigned int& pNumForcingFields,
60  const TiXmlElement* pForce)
61  {
62  v_InitObject(pFields, pNumForcingFields, pForce);
63  }
64 
65 
66  /**
67  * @param fields Expansion lists corresponding to input arrays
68  * @param inarray u^n from previous timestep
69  * @param outarray output array to append forcing to
70  */
73  const Array<OneD, Array<OneD, NekDouble> >& inarray,
74  Array<OneD, Array<OneD, NekDouble> >& outarray,
75  const NekDouble& time)
76  {
77  v_Apply(fields, inarray, outarray, time);
78  }
79 
80 
81  /**
82  *
83  */
84  vector<ForcingSharedPtr> Forcing::Load(
87  const unsigned int& pNumForcingFields)
88  {
89  vector<ForcingSharedPtr> vForceList;
90 
91  if (!pSession->DefinesElement("Nektar/Forcing"))
92  {
93  return vForceList;
94  }
95 
96  TiXmlElement* vForcing = pSession->GetElement("Nektar/Forcing");
97  if (vForcing)
98  {
99  unsigned int vNumForcingFields = pNumForcingFields;
100  if (!pNumForcingFields)
101  {
102  vNumForcingFields = pFields.num_elements();
103  }
104 
105  TiXmlElement* vForce = vForcing->FirstChildElement("FORCE");
106  while (vForce)
107  {
108  string vType = vForce->Attribute("TYPE");
109 
110  vForceList.push_back(GetForcingFactory().CreateInstance(
111  vType, pSession, pFields,
112  vNumForcingFields, vForce));
113  vForce = vForce->NextSiblingElement("FORCE");
114  }
115  }
116  return vForceList;
117  }
118 
121  std::string pFieldName,
122  Array<OneD, NekDouble>& pArray,
123  const std::string& pFunctionName,
124  NekDouble pTime)
125  {
126  ASSERTL0(pSession->DefinesFunction(pFunctionName),
127  "Function '" + pFunctionName + "' does not exist.");
128 
130  pSession->GetFunction(pFunctionName, pFieldName);
131 
132  EvaluateTimeFunction(pTime,ffunc,pArray);
133  }
134 
135 
137  const NekDouble pTime,
139  Array<OneD, NekDouble>& pArray)
140  {
141  // dummy array of zero pts.
142  Array<OneD, NekDouble> x0(pArray.num_elements(),0.0);
143 
144  pEqn->Evaluate(x0, x0, x0, pTime, pArray);
145  }
146 
147 
148 
152  std::string pFieldName,
153  Array<OneD, NekDouble>& pArray,
154  const std::string& pFunctionName,
155  NekDouble pTime)
156  {
157  ASSERTL0(pSession->DefinesFunction(pFunctionName),
158  "Function '" + pFunctionName + "' does not exist.");
159 
160  unsigned int nq = pFields[0]->GetNpoints();
161  if (pArray.num_elements() != nq)
162  {
163  pArray = Array<OneD, NekDouble> (nq);
164  }
165 
167  vType = pSession->GetFunctionType(pFunctionName, pFieldName);
169  {
170  Array<OneD, NekDouble> x0(nq);
171  Array<OneD, NekDouble> x1(nq);
172  Array<OneD, NekDouble> x2(nq);
173 
174  pFields[0]->GetCoords(x0, x1, x2);
176  pSession->GetFunction(pFunctionName, pFieldName);
177 
178  ffunc->Evaluate(x0, x1, x2, pTime, pArray);
179  }
180  else if (vType == LibUtilities::eFunctionTypeFile)
181  {
182  std::string filename = pSession->GetFunctionFilename(
183  pFunctionName,
184  pFieldName);
185 
186  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
187  std::vector<std::vector<NekDouble> > FieldData;
188  Array<OneD, NekDouble> vCoeffs(pFields[0]->GetNcoeffs());
189  Vmath::Zero(vCoeffs.num_elements(), vCoeffs, 1);
190 
193  fld->Import(filename, FieldDef, FieldData);
194 
195  int idx = -1;
196  for (int i = 0; i < FieldDef.size(); ++i)
197  {
198  for (int j = 0; j < FieldDef[i]->m_fields.size(); ++j)
199  {
200  if (FieldDef[i]->m_fields[j] == pFieldName)
201  {
202  idx = j;
203  }
204  }
205 
206  if (idx >= 0)
207  {
208  pFields[0]->ExtractDataToCoeffs(
209  FieldDef[i],
210  FieldData[i],
211  FieldDef[i]->m_fields[idx],
212  vCoeffs);
213  }
214  else
215  {
216  cout << "Field " + pFieldName + " not found." << endl;
217  }
218  }
219  pFields[0]->BwdTrans_IterPerExp(vCoeffs, pArray);
220  }
221  }
222 
223  }
224 }
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:119
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
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:57
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
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:71
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:42
SOLVER_UTILS_EXPORT void EvaluateFunction(Array< OneD, MultiRegions::ExpListSharedPtr > pFields, LibUtilities::SessionReaderSharedPtr pSession, std::string pFieldName, Array< OneD, NekDouble > &pArray, const std::string &pFunctionName, NekDouble pTime=NekDouble(0))
Definition: Forcing.cpp:149
static SOLVER_UTILS_EXPORT std::vector< ForcingSharedPtr > Load(const LibUtilities::SessionReaderSharedPtr &pSession, const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields=0)
Definition: Forcing.cpp:84
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
LibUtilities::NekFactory< std::string, Forcing, const LibUtilities::SessionReaderSharedPtr &, const Array< OneD, MultiRegions::ExpListSharedPtr > &, const unsigned int &, const TiXmlElement * > ForcingFactory
Declaration of the forcing factory.
Definition: Forcing.h:61
boost::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:225
double NekDouble
SOLVER_UTILS_EXPORT Forcing(const LibUtilities::SessionReaderSharedPtr &)
Constructor.
Definition: Forcing.cpp:51
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:95
boost::shared_ptr< Equation > EquationSharedPtr
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)=0
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:359
Provides a generic Factory class.
Definition: NekFactory.hpp:116
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