Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // 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: white noise forcing
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
37 #include <MultiRegions/ExpList.h>
38 
39 using namespace std;
40 
41 namespace Nektar
42 {
43 namespace SolverUtils
44 {
45 
46  std::string ForcingNoise::className = GetForcingFactory().
47  RegisterCreatorFunction("Noise",
48  ForcingNoise::create,
49  "White Noise Forcing");
50 
51  ForcingNoise::ForcingNoise(
53  : Forcing(pSession)
54  {
55  }
56 
59  const unsigned int& pNumForcingFields,
60  const TiXmlElement* pForce)
61  {
62  m_NumVariable = pNumForcingFields;
63  int nq = pFields[0]->GetTotPoints();
64 
65  const TiXmlElement* noiseElmt = pForce->FirstChildElement("WHITENOISE");
66  ASSERTL0(noiseElmt, "Requires WHITENOISE tag specifying "
67  "magnitude of white noise force.");
68 
69  string noiseValue = noiseElmt->GetText();
70 
71  m_noise = boost::lexical_cast<NekDouble>(noiseValue);
72 
73  // Load optional parameters
74  const TiXmlElement* freqElmt = pForce->FirstChildElement("UPDATEFREQ");
75  if (freqElmt)
76  {
77  string freqValue = freqElmt->GetText();
78  m_updateFreq = boost::lexical_cast<int>(freqValue);
79  }
80  else
81  {
82  // Default is 0 (never update forcing)
83  m_updateFreq = 0;
84  }
85 
86  const TiXmlElement* stepsElmt = pForce->FirstChildElement("NSTEPS");
87  if (stepsElmt)
88  {
89  string stepsValue = stepsElmt->GetText();
90  m_numSteps = boost::lexical_cast<int>(stepsValue);
91  }
92  else
93  {
94  // Default is 0 (use noise in the entire simulation)
95  m_numSteps = 0;
96  }
97 
99 
100  // Fill forcing: use rank in seed to avoid repeated results
101  int seed = - m_session->GetComm()->GetRank();
102  m_Forcing[0] = Array<OneD, NekDouble> (nq, 0.0);
104  for (int i = 1; i < m_NumVariable; ++i)
105  {
106  m_Forcing[i] = Array<OneD, NekDouble> (nq, 0.0);
108  }
109 
110  m_index = 0;
111  }
112 
115  const Array<OneD, Array<OneD, NekDouble> > &inarray,
116  Array<OneD, Array<OneD, NekDouble> > &outarray,
117  const NekDouble &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)
127  {
128  for (int i = 0; i < m_NumVariable; ++i)
129  {
130  Vmath::FillWhiteNoise(outarray[i].num_elements(),
131  m_noise,m_Forcing[i],1);
132  }
133  }
134 
135  // Apply forcing
136  for (int i = 0; i < m_NumVariable; i++)
137  {
138  Vmath::Vadd(outarray[i].num_elements(), outarray[i], 1,
139  m_Forcing[i], 1, outarray[i], 1);
140  }
141 
142  ++m_index;
143  }
144 
145 }
146 }
virtual SOLVER_UTILS_EXPORT void v_InitObject(const Array< OneD, MultiRegions::ExpListSharedPtr > &pFields, const unsigned int &pNumForcingFields, const TiXmlElement *pForce)
Array< OneD, Array< OneD, NekDouble > > m_Forcing
Evaluated forcing function.
Definition: Forcing.h:102
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
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)
ForcingFactory & GetForcingFactory()
Declaration of the forcing factory singleton.
Definition: Forcing.cpp:44
STL namespace.
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
double NekDouble
LibUtilities::SessionReaderSharedPtr m_session
Session reader.
Definition: Forcing.h:100
int m_NumVariable
Number of variables.
Definition: Forcing.h:104
void FillWhiteNoise(int n, const T eps, T *x, const int incx, int outseed)
Fills a vector with white noise.
Definition: Vmath.cpp:138
Defines a forcing term to be explicitly applied.
Definition: Forcing.h:70
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:299