ForcingNoise.cpp 5.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
///////////////////////////////////////////////////////////////////////////////
//
// File: ForcingNoise.cpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: white noise forcing
//
///////////////////////////////////////////////////////////////////////////////

#include <SolverUtils/Forcing/ForcingNoise.h>
#include <MultiRegions/ExpList.h>

39
40
using namespace std;

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace Nektar
{
namespace SolverUtils
{

    std::string ForcingNoise::className = GetForcingFactory().
                                RegisterCreatorFunction("Noise",
                                                        ForcingNoise::create,
                                                        "White Noise Forcing");

    ForcingNoise::ForcingNoise(
            const LibUtilities::SessionReaderSharedPtr& pSession)
        : Forcing(pSession)
    {
    }

    void ForcingNoise::v_InitObject(
            const Array<OneD, MultiRegions::ExpListSharedPtr>& pFields,
            const unsigned int& pNumForcingFields,
            const TiXmlElement* pForce)
    {
        m_NumVariable = pNumForcingFields;
        int nq         = pFields[0]->GetTotPoints();

65
66
        const TiXmlElement* noiseElmt = pForce->FirstChildElement("WHITENOISE");
        ASSERTL0(noiseElmt, "Requires WHITENOISE tag specifying "
Douglas Serson's avatar
Douglas Serson committed
67
                               "magnitude of white noise force.");
68
        
69
        string noiseValue = noiseElmt->GetText();
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        m_noise = boost::lexical_cast<NekDouble>(noiseValue);

        // Load optional parameters
        const TiXmlElement* freqElmt = pForce->FirstChildElement("UPDATEFREQ");
        if (freqElmt)
        {
            string freqValue = freqElmt->GetText();
            m_updateFreq = boost::lexical_cast<int>(freqValue);
        }
        else
        {
            // Default is 0 (never update forcing)
            m_updateFreq = 0;
        }

        const TiXmlElement* stepsElmt = pForce->FirstChildElement("NSTEPS");
        if (stepsElmt)
        {
            string stepsValue = stepsElmt->GetText();
            m_numSteps = boost::lexical_cast<int>(stepsValue);
        }
        else
        {
            // Default is 0 (use noise in the entire simulation)
            m_numSteps = 0;
        }
97
98
99

        m_Forcing = Array<OneD, Array<OneD, NekDouble> > (m_NumVariable);

100
101
102
        // Fill forcing: use i and rank in seed to avoid repeated results
        int rank = m_session->GetComm()->GetRank();
        int seed = - (rank * m_NumVariable);
103
104
105
        for (int i = 0; i < m_NumVariable; ++i)
        {
            m_Forcing[i] = Array<OneD, NekDouble> (nq, 0.0);
106
107
            Vmath::FillWhiteNoise(nq,m_noise,&m_Forcing[i][0],1,seed);
            --seed;
108
        }
109
110

        m_index = 0;
111
112
113
114
115
    }

    void ForcingNoise::v_Apply(
            const Array<OneD, MultiRegions::ExpListSharedPtr> &fields,
            const Array<OneD, Array<OneD, NekDouble> > &inarray,
116
117
            Array<OneD, Array<OneD, NekDouble> > &outarray,
            const NekDouble &time)
118
    {
119
120
121
122
123
124
125
126
127
        // Do not apply forcing if exceeded m_numSteps
        if( m_numSteps && (m_index >= m_numSteps) )
        {
            return;
        }

        // Update forcing (change seed to avoid getting same result)
        if(m_updateFreq && m_index && !((m_index)%m_updateFreq))
        {
128
129
130
            int rank   = m_session->GetComm()->GetRank();
            int nProcs = m_session->GetComm()->GetSize();
            int seed   = - (rank* m_NumVariable + m_index*m_NumVariable*nProcs);
131
132
133
            for (int i = 0; i < m_NumVariable; ++i)
            {
                Vmath::FillWhiteNoise(outarray[i].num_elements(),
Douglas Serson's avatar
Douglas Serson committed
134
                                      m_noise,&m_Forcing[i][0],1,
135
136
                                      seed);
                --seed;
137
138
139
140
            }
        }

        // Apply forcing
141
142
143
144
145
        for (int i = 0; i < m_NumVariable; i++)
        {
            Vmath::Vadd(outarray[i].num_elements(), outarray[i], 1,
                        m_Forcing[i], 1, outarray[i], 1);
        }
146
147

        ++m_index;
148
149
150
151
    }

}
}