Nektar++
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  m_hasRefFlow = true;
105  for (int i = 0; i < m_NumVariable; ++i)
106  {
107  std::string s_FieldStr = m_session->GetVariable(i);
108  ASSERTL0(m_session->DefinesFunction(m_funcNameTime, s_FieldStr),
109  "Variable '" + s_FieldStr + "' not defined.");
110  m_Refflow[i] = Array<OneD, NekDouble> (npts, 0.0);
111  }
112  }
113  }
114 
117  &pFields,
118  const TiXmlElement *pForce)
119  {
120  const TiXmlElement *funcNameElmt = pForce->FirstChildElement("COEFF");
121  ASSERTL0(funcNameElmt,
122  "Requires COEFF tag, specifying function "
123  "name which prescribes absorption layer coefficient.");
124  string funcName = funcNameElmt->GetText();
125  ASSERTL0(m_session->DefinesFunction(funcName),
126  "Function '" + funcName + "' not defined.");
127 
128  int npts = pFields[0]->GetTotPoints();
129 
131  for (int i = 0; i < m_NumVariable; ++i)
132  {
133  m_Absorption[i] = Array<OneD, NekDouble>(npts, 0.0);
134  }
135 
136  funcNameElmt = pForce->FirstChildElement("BOUNDARYREGIONS");
137  if (funcNameElmt)
138  {
139  ASSERTL0(ParseUtils::GenerateVector(funcNameElmt->GetText(),
140  m_bRegions),
141  "Unable to process list of BOUNDARYREGIONS in Absorption "
142  "Forcing: " +
143  std::string(funcNameElmt->GetText()));
144 
145  // alter m_bRegions so that it contains the boundaryRegions of this rank
146  std::vector<unsigned int> localBRegions;
147  SpatialDomains::BoundaryConditions bcs(m_session, pFields[0]->GetGraph());
149  SpatialDomains::BoundaryRegionCollection::iterator it1;
150  int n = 0;
151  for (it1 = regions.begin(); it1 != regions.end(); ++it1)
152  {
153  if (std::find(m_bRegions.begin(), m_bRegions.end(), it1->first) != m_bRegions.end())
154  {
155  localBRegions.push_back(n);
156  }
157  n++;
158  }
159  m_bRegions = localBRegions;
160 
161  if (m_bRegions.size() == 0)
162  {
163  return;
164  }
165 
166  std::vector<Array<OneD, const NekDouble> > points;
167 
169  for (int i = 0; i < 3; i++)
170  {
171  x[i] = Array<OneD, NekDouble>(npts, 0.0);
172  }
173  pFields[0]->GetCoords(x[0], x[1], x[2]);
174  for (int i = 0; i < 3; i++)
175  {
176  points.push_back(x[i]);
177  }
178 
179  Array<OneD, NekDouble> t(npts, 0.0);
180  points.push_back(t);
181 
182  Array<OneD, NekDouble> r(npts, 0.0);
183  std::vector<unsigned int>::iterator it;
184  std::vector<BPointPair> inPoints;
186  for (it = m_bRegions.begin(); it != m_bRegions.end(); ++it)
187  {
188  int bpts = pFields[0]->GetBndCondExpansions()[*it]->GetNpoints();
189  for (int i = 0; i < 3; i++)
190  {
191  b[i] = Array<OneD, NekDouble>(bpts, 0.0);
192  }
193  pFields[0]->GetBndCondExpansions()[*it]->GetCoords(
194  b[0], b[1], b[2]);
195  for (int i = 0;
196  i < pFields[0]->GetBndCondExpansions()[*it]->GetNpoints();
197  ++i)
198  {
199  inPoints.push_back(
200  BPointPair(BPoint(b[0][i], b[1][i], b[2][i]), i));
201  }
202  }
204  m_rtree->insert(inPoints.begin(), inPoints.end());
205 
206  for (int i = 0; i < npts; ++i)
207  {
208  std::vector<BPointPair> result;
209  BPoint sPoint(x[0][i], x[1][i], x[2][i]);
210  m_rtree->query(bgi::nearest(sPoint, 1), std::back_inserter(result));
211  r[i] = bg::distance(sPoint, result[0].first);
212  }
213  points.push_back(r);
214 
215  std::string s_FieldStr;
216  for (int i = 0; i < m_NumVariable; ++i)
217  {
218  s_FieldStr = m_session->GetVariable(i);
219  ASSERTL0(m_session->DefinesFunction(funcName, s_FieldStr),
220  "Variable '" + s_FieldStr + "' not defined.");
221 
223  m_session->GetFunction(funcName, s_FieldStr);
224  ASSERTL0(ffunc->GetVlist() == "x y z t r",
225  "EVARS of " + funcName + " must be 'r'");
226 
227  ffunc->Evaluate(points, m_Absorption[i]);
228  }
229  }
230  else
231  {
232  for (int i = 0; i < m_NumVariable; ++i)
233  {
234  std::string s_FieldStr = m_session->GetVariable(i);
235  GetFunction(pFields, m_session, funcName)->Evaluate(s_FieldStr, m_Absorption[i]);
236  }
237  }
238  }
239 
242  const Array<OneD, Array<OneD, NekDouble> > &inarray,
243  Array<OneD, Array<OneD, NekDouble> > &outarray,
244  const NekDouble &time)
245  {
246  int nq = m_Forcing[0].size();
247  CalculateForcing(fields, inarray, time);
248  for (int i = 0; i < m_NumVariable; ++i)
249  {
250  Vmath::Vadd(nq, m_Forcing[i], 1,
251  outarray[i], 1, outarray[i], 1);
252  }
253  }
254 
257  const Array<OneD, Array<OneD, NekDouble> > &inarray,
258  Array<OneD, Array<OneD, NekDouble> > &outarray,
259  const NekDouble &time)
260  {
261  int ncoeff = outarray[m_NumVariable - 1].size();
262  Array<OneD, NekDouble> tmp(ncoeff, 0.0);
263  CalculateForcing(fields, inarray, time);
264 
265  for (int i = 0; i < m_NumVariable; ++i)
266  {
267  fields[i]->FwdTrans(m_Forcing[i], tmp);
268  Vmath::Vadd(ncoeff, tmp, 1,
269  outarray[i], 1, outarray[i], 1);
270  }
271  }
272 
275  const Array<OneD, Array<OneD, NekDouble> > &inarray,
276  const NekDouble &time)
277  {
278  boost::ignore_unused(fields);
279  int nq = m_Forcing[0].size();
280 
281  std::string s_FieldStr;
282  Array<OneD, NekDouble> TimeScale(1);
284 
285  if (m_hasRefFlow)
286  {
287  for (int i = 0; i < m_NumVariable; i++)
288  {
289  RefflowScaled[i] = Array<OneD, NekDouble> (nq);
290  if (m_hasRefFlowTime)
291  {
292  s_FieldStr = m_session->GetVariable(i);
293 
294  std::string s_FieldStr = m_session->GetVariable(i);
296  Evaluate(s_FieldStr, m_Refflow[i], time);
297  Vmath::Vcopy(nq, m_Refflow[i], 1, RefflowScaled[i], 1);
298  }
299  else
300  {
301  Vmath::Vcopy(nq, m_Refflow[i],1, RefflowScaled[i],1);
302  }
303 
304 
305  Vmath::Vsub(nq, inarray[i], 1,
306  RefflowScaled[i], 1, m_Forcing[i], 1);
307  Vmath::Vmul(nq, m_Absorption[i], 1,
308  m_Forcing[i], 1, m_Forcing[i], 1);
309  }
310  }
311  else
312  {
313  for (int i = 0; i < m_NumVariable; i++)
314  {
315  Vmath::Vmul(nq, m_Absorption[i], 1,
316  inarray[i], 1, m_Forcing[i], 1);
317  }
318  }
319  }
320 
321 }
322 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
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
bg::model::point< NekDouble, 3, bg::cs::cartesian > BPoint
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)
Array< OneD, Array< OneD, NekDouble > > m_Refflow
std::pair< BPoint, unsigned int > BPointPair
void CalcAbsorption(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const TiXmlElement *pForce)
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)
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
void CalculateForcing(const Array< OneD, MultiRegions::ExpListSharedPtr > &fields, const Array< OneD, Array< OneD, NekDouble > > &inarray, const NekDouble &time)
std::vector< unsigned int > m_bRegions
Array< OneD, Array< OneD, NekDouble > > m_Absorption
Defines a forcing term to be explicitly applied.
Definition: Forcing.h:73
int m_NumVariable
Number of variables.
Definition: Forcing.h:124
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:122
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
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:118
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition: Conditions.h:238
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::map< int, BoundaryRegionShPtr > BoundaryRegionCollection
Definition: Conditions.h:217
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:362
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
double NekDouble
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:192
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:322
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1199
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:372