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