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 
38 #include <MultiRegions/ExpList.h>
39 
40 using namespace std;
41 
42 namespace Nektar
43 {
44 namespace SolverUtils
45 {
46 
47  std::string ForcingNoise::className = GetForcingFactory().
48  RegisterCreatorFunction("Noise",
49  ForcingNoise::create,
50  "White Noise Forcing");
51 
52  ForcingNoise::ForcingNoise(
54  const std::weak_ptr<EquationSystem> &pEquation)
55  : Forcing(pSession, pEquation)
56  {
57  }
58 
61  const unsigned int& pNumForcingFields,
62  const TiXmlElement* pForce)
63  {
64  m_NumVariable = pNumForcingFields;
65  int nq = pFields[0]->GetTotPoints();
66 
67  const TiXmlElement* noiseElmt = pForce->FirstChildElement("WHITENOISE");
68  ASSERTL0(noiseElmt, "Requires WHITENOISE tag specifying "
69  "magnitude of white noise force.");
70 
71  string noiseValue = noiseElmt->GetText();
72 
73  m_noise = boost::lexical_cast<NekDouble>(noiseValue);
74 
75  // Load optional parameters
76  const TiXmlElement* freqElmt = pForce->FirstChildElement("UPDATEFREQ");
77  if (freqElmt)
78  {
79  string freqValue = freqElmt->GetText();
80  m_updateFreq = boost::lexical_cast<int>(freqValue);
81  }
82  else
83  {
84  // Default is 0 (never update forcing)
85  m_updateFreq = 0;
86  }
87 
88  const TiXmlElement* stepsElmt = pForce->FirstChildElement("NSTEPS");
89  if (stepsElmt)
90  {
91  string stepsValue = stepsElmt->GetText();
92  m_numSteps = boost::lexical_cast<int>(stepsValue);
93  }
94  else
95  {
96  // Default is 0 (use noise in the entire simulation)
97  m_numSteps = 0;
98  }
99 
101 
102  // Fill forcing: use rank in seed to avoid repeated results
103  int seed = - m_session->GetComm()->GetRank();
104  m_Forcing[0] = Array<OneD, NekDouble> (nq, 0.0);
106  for (int i = 1; i < m_NumVariable; ++i)
107  {
108  m_Forcing[i] = Array<OneD, NekDouble> (nq, 0.0);
110  }
111 
112  m_index = 0;
113  }
114 
117  const Array<OneD, Array<OneD, NekDouble> > &inarray,
118  Array<OneD, Array<OneD, NekDouble> > &outarray,
119  const NekDouble &time)
120  {
121  boost::ignore_unused(fields, inarray, time);
122 
123  // Do not apply forcing if exceeded m_numSteps
124  if( m_numSteps && (m_index >= m_numSteps) )
125  {
126  return;
127  }
128 
129  // Update forcing (change seed to avoid getting same result)
131  {
132  for (int i = 0; i < m_NumVariable; ++i)
133  {
134  Vmath::FillWhiteNoise(outarray[i].size(),
135  m_noise,m_Forcing[i],1);
136  }
137  }
138 
139  // Apply forcing
140  for (int i = 0; i < m_NumVariable; i++)
141  {
142  Vmath::Vadd(outarray[i].size(), outarray[i], 1,
143  m_Forcing[i], 1, outarray[i], 1);
144  }
145 
146  ++m_index;
147  }
148 
149 }
150 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
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
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:118
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
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)
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:1
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:322
void FillWhiteNoise(int n, const T eps, T *x, const int incx, int outseed)
Fills a vector with white noise.
Definition: Vmath.cpp:142