Nektar++
ForcingNoise.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ForcingNoise.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: white noise forcing
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
35 #include <boost/core/ignore_unused.hpp>
36 
37 #include <MultiRegions/ExpList.h>
39 
40 using namespace std;
41 
42 namespace Nektar
43 {
44 namespace SolverUtils
45 {
46 
47 std::string ForcingNoise::className =
48  GetForcingFactory().RegisterCreatorFunction("Noise", ForcingNoise::create,
49  "White Noise Forcing");
50 
51 ForcingNoise::ForcingNoise(const LibUtilities::SessionReaderSharedPtr &pSession,
52  const std::weak_ptr<EquationSystem> &pEquation)
53  : Forcing(pSession, pEquation)
54 {
55 }
56 
59  const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
60 {
61  m_NumVariable = pNumForcingFields;
62  int nq = pFields[0]->GetTotPoints();
63 
64  const TiXmlElement *noiseElmt = pForce->FirstChildElement("WHITENOISE");
65  ASSERTL0(noiseElmt, "Requires WHITENOISE tag specifying "
66  "magnitude of white noise force.");
67 
68  string noiseValue = noiseElmt->GetText();
69 
70  m_noise = boost::lexical_cast<NekDouble>(noiseValue);
71 
72  // Load optional parameters
73  const TiXmlElement *freqElmt = pForce->FirstChildElement("UPDATEFREQ");
74  if (freqElmt)
75  {
76  string freqValue = freqElmt->GetText();
77  m_updateFreq = boost::lexical_cast<int>(freqValue);
78  }
79  else
80  {
81  // Default is 0 (never update forcing)
82  m_updateFreq = 0;
83  }
84 
85  const TiXmlElement *stepsElmt = pForce->FirstChildElement("NSTEPS");
86  if (stepsElmt)
87  {
88  string stepsValue = stepsElmt->GetText();
89  m_numSteps = boost::lexical_cast<int>(stepsValue);
90  }
91  else
92  {
93  // Default is 0 (use noise in the entire simulation)
94  m_numSteps = 0;
95  }
96 
98 
99  // Fill forcing: use rank in seed to avoid repeated results
100  int seed = -m_session->GetComm()->GetRank();
101  m_Forcing[0] = Array<OneD, NekDouble>(nq, 0.0);
102  Vmath::FillWhiteNoise(nq, m_noise, m_Forcing[0], 1, seed);
103  for (int i = 1; i < m_NumVariable; ++i)
104  {
105  m_Forcing[i] = Array<OneD, NekDouble>(nq, 0.0);
107  }
108 
109  m_index = 0;
110 }
111 
114  const Array<OneD, Array<OneD, NekDouble>> &inarray,
115  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble &time)
116 {
117  boost::ignore_unused(fields, inarray, time);
118 
119  // Do not apply forcing if exceeded m_numSteps
120  if (m_numSteps && (m_index >= m_numSteps))
121  {
122  return;
123  }
124 
125  // Update forcing (change seed to avoid getting same result)
126  if (m_updateFreq && m_index && !((m_index) % m_updateFreq))
127  {
128  for (int i = 0; i < m_NumVariable; ++i)
129  {
130  Vmath::FillWhiteNoise(outarray[i].size(), m_noise, m_Forcing[i], 1);
131  }
132  }
133 
134  // Apply forcing
135  for (int i = 0; i < m_NumVariable; i++)
136  {
137  Vmath::Vadd(outarray[i].size(), outarray[i], 1, m_Forcing[i], 1,
138  outarray[i], 1);
139  }
140 
141  ++m_index;
142 }
143 
144 } // namespace SolverUtils
145 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
Defines a forcing term to be explicitly applied.
Definition: Forcing.h:73
int m_NumVariable
Number of variables.
Definition: Forcing.h:122
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:120
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h: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) override
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce) override
std::shared_ptr< SessionReader > SessionReaderSharedPtr
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:44
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
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:359
void FillWhiteNoise(int n, const T eps, T *x, const int incx, int outseed)
Fills a vector with white noise.
Definition: Vmath.cpp:155