Nektar++
ForcingAbsorption.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ForcingAbsorption.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2016 Kilian Lackhove
10 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
11 // Department of Aeronautics, Imperial College London (UK), and Scientific
12 // Computing and Imaging Institute, University of Utah (USA).
13 //
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: Absorption layer forcing
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #include <boost/core/ignore_unused.hpp>
37 
39 
42 
43 using namespace std;
44 
45 namespace Nektar
46 {
47 namespace SolverUtils
48 {
49 
50  std::string ForcingAbsorption::className = GetForcingFactory().
51  RegisterCreatorFunction("Absorption",
52  ForcingAbsorption::create,
53  "Forcing Absorption");
54 
55  ForcingAbsorption::ForcingAbsorption(
57  const std::weak_ptr<EquationSystem> &pEquation)
58  : Forcing(pSession, pEquation),
59  m_hasRefFlow(false),
60  m_hasRefFlowTime(false)
61  {
62  }
63 
66  const unsigned int& pNumForcingFields,
67  const TiXmlElement* pForce)
68  {
69  m_NumVariable = pNumForcingFields;
70  int npts = pFields[0]->GetTotPoints();
71 
72  CalcAbsorption(pFields, pForce);
73 
75  for (int i = 0; i < m_NumVariable; ++i)
76  {
77  m_Forcing[i] = Array<OneD, NekDouble>(npts, 0.0);
78  }
79 
80  const TiXmlElement* funcNameElmt = pForce->FirstChildElement("REFFLOW");
81  if (funcNameElmt)
82  {
83  string funcName = funcNameElmt->GetText();
84  ASSERTL0(m_session->DefinesFunction(funcName),
85  "Function '" + funcName + "' not defined.");
87  for (int i = 0; i < m_NumVariable; ++i)
88  {
89  std::string s_FieldStr = m_session->GetVariable(i);
90  ASSERTL0(m_session->DefinesFunction(funcName, s_FieldStr),
91  "Variable '" + s_FieldStr + "' not defined.");
92  m_Refflow[i] = Array<OneD, NekDouble> (npts, 0.0);
93  GetFunction(pFields, m_session, funcName)->Evaluate(s_FieldStr, m_Refflow[i]);
94  }
95  m_hasRefFlow = true;
96  }
97 
98  funcNameElmt = pForce->FirstChildElement("REFFLOWTIME");
99  if (funcNameElmt)
100  {
101  m_funcNameTime = funcNameElmt->GetText();
102  m_hasRefFlowTime = true;
103  }
104  }
105 
108  &pFields,
109  const TiXmlElement *pForce)
110  {
111  const TiXmlElement *funcNameElmt = pForce->FirstChildElement("COEFF");
112  ASSERTL0(funcNameElmt,
113  "Requires COEFF tag, specifying function "
114  "name which prescribes absorption layer coefficient.");
115  string funcName = funcNameElmt->GetText();
116  ASSERTL0(m_session->DefinesFunction(funcName),
117  "Function '" + funcName + "' not defined.");
118 
119  int npts = pFields[0]->GetTotPoints();
120 
122  for (int i = 0; i < m_NumVariable; ++i)
123  {
124  m_Absorption[i] = Array<OneD, NekDouble>(npts, 0.0);
125  }
126 
127  funcNameElmt = pForce->FirstChildElement("BOUNDARYREGIONS");
128  if (funcNameElmt)
129  {
130  ASSERTL0(ParseUtils::GenerateVector(funcNameElmt->GetText(),
131  m_bRegions),
132  "Unable to process list of BOUNDARYREGIONS in Absorption "
133  "Forcing: " +
134  std::string(funcNameElmt->GetText()));
135 
136  // alter m_bRegions so that it contains the boundaryRegions of this rank
137  std::vector<unsigned int> localBRegions;
138  SpatialDomains::BoundaryConditions bcs(m_session, pFields[0]->GetGraph());
140  SpatialDomains::BoundaryRegionCollection::iterator it1;
141  int n = 0;
142  for (it1 = regions.begin(); it1 != regions.end(); ++it1)
143  {
144  if (std::find(m_bRegions.begin(), m_bRegions.end(), it1->first) != m_bRegions.end())
145  {
146  localBRegions.push_back(n);
147  }
148  n++;
149  }
150  m_bRegions = localBRegions;
151 
152  if (m_bRegions.size() == 0)
153  {
154  return;
155  }
156 
157  std::vector<Array<OneD, const NekDouble> > points;
158 
160  for (int i = 0; i < 3; i++)
161  {
162  x[i] = Array<OneD, NekDouble>(npts, 0.0);
163  }
164  pFields[0]->GetCoords(x[0], x[1], x[2]);
165  for (int i = 0; i < 3; i++)
166  {
167  points.push_back(x[i]);
168  }
169 
170  Array<OneD, NekDouble> t(npts, 0.0);
171  points.push_back(t);
172 
173  Array<OneD, NekDouble> r(npts, 0.0);
174  std::vector<unsigned int>::iterator it;
175  std::vector<BPointPair> inPoints;
177  for (it = m_bRegions.begin(); it != m_bRegions.end(); ++it)
178  {
179  int bpts = pFields[0]->GetBndCondExpansions()[*it]->GetNpoints();
180  for (int i = 0; i < 3; i++)
181  {
182  b[i] = Array<OneD, NekDouble>(bpts, 0.0);
183  }
184  pFields[0]->GetBndCondExpansions()[*it]->GetCoords(
185  b[0], b[1], b[2]);
186  for (int i = 0;
187  i < pFields[0]->GetBndCondExpansions()[*it]->GetNpoints();
188  ++i)
189  {
190  inPoints.push_back(
191  BPointPair(BPoint(b[0][i], b[1][i], b[2][i]), i));
192  }
193  }
195  m_rtree->insert(inPoints.begin(), inPoints.end());
196 
197  for (int i = 0; i < npts; ++i)
198  {
199  std::vector<BPointPair> result;
200  BPoint sPoint(x[0][i], x[1][i], x[2][i]);
201  m_rtree->query(bgi::nearest(sPoint, 1), std::back_inserter(result));
202  r[i] = bg::distance(sPoint, result[0].first);
203  }
204  points.push_back(r);
205 
206  std::string s_FieldStr;
207  for (int i = 0; i < m_NumVariable; ++i)
208  {
209  s_FieldStr = m_session->GetVariable(i);
210  ASSERTL0(m_session->DefinesFunction(funcName, s_FieldStr),
211  "Variable '" + s_FieldStr + "' not defined.");
212 
214  m_session->GetFunction(funcName, s_FieldStr);
215  ASSERTL0(ffunc->GetVlist() == "x y z t r",
216  "EVARS of " + funcName + " must be 'r'");
217 
218  ffunc->Evaluate(points, m_Absorption[i]);
219  }
220  }
221  else
222  {
223  for (int i = 0; i < m_NumVariable; ++i)
224  {
225  std::string s_FieldStr = m_session->GetVariable(i);
226  GetFunction(pFields, m_session, funcName)->Evaluate(s_FieldStr, m_Absorption[i]);
227  }
228  }
229  }
230 
233  const Array<OneD, Array<OneD, NekDouble> > &inarray,
234  Array<OneD, Array<OneD, NekDouble> > &outarray,
235  const NekDouble &time)
236  {
237  boost::ignore_unused(fields);
238 
239  int nq = m_Forcing[0].num_elements();
240 
241  std::string s_FieldStr;
242  Array<OneD, NekDouble> TimeScale(1);
244 
245  if (m_hasRefFlow)
246  {
247  for (int i = 0; i < m_NumVariable; i++)
248  {
249  RefflowScaled[i] = Array<OneD, NekDouble> (nq);
250  if (m_hasRefFlowTime)
251  {
252  s_FieldStr = m_session->GetVariable(i);
253  EvaluateTimeFunction(m_session, s_FieldStr, TimeScale, m_funcNameTime, time);
254  Vmath::Smul(nq, TimeScale[0], m_Refflow[i],1,RefflowScaled[i],1);
255  }
256  else
257  {
258  Vmath::Vcopy(nq, m_Refflow[i],1, RefflowScaled[i],1);
259  }
260 
261 
262  Vmath::Vsub(nq, inarray[i], 1,
263  RefflowScaled[i], 1, m_Forcing[i], 1);
264  Vmath::Vmul(nq, m_Absorption[i], 1,
265  m_Forcing[i], 1, m_Forcing[i], 1);
266  Vmath::Vadd(nq, m_Forcing[i], 1,
267  outarray[i], 1, outarray[i], 1);
268  }
269  }
270  else
271  {
272  for (int i = 0; i < m_NumVariable; i++)
273  {
274  Vmath::Vmul(nq, m_Absorption[i], 1,
275  inarray[i], 1, m_Forcing[i], 1);
276  Vmath::Vadd(nq, m_Forcing[i], 1,
277  outarray[i], 1, outarray[i], 1);
278  }
279  }
280  }
281 
282 }
283 }
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
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)
void CalcAbsorption(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const TiXmlElement *pForce)
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
bg::model::point< NekDouble, 3, bg::cs::cartesian > BPoint
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:44
STL namespace.
Array< OneD, Array< OneD, NekDouble > > m_Absorption
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*y.
Definition: Vmath.cpp:216
Array< OneD, Array< OneD, NekDouble > > m_Refflow
std::map< int, BoundaryRegionShPtr > BoundaryRegionCollection
Definition: Conditions.h:217
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
double NekDouble
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:135
std::vector< unsigned int > m_bRegions
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:103
std::pair< BPoint, unsigned int > BPointPair
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.cpp:346
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:131
int m_NumVariable
Number of variables.
Definition: Forcing.h:109
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:358
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
Defines a forcing term to be explicitly applied.
Definition: Forcing.h:72
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1064
std::shared_ptr< SessionReader > SessionReaderSharedPtr
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition: Conditions.h:238
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
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition: Vmath.cpp:186