File size: 8,714 Bytes
5a600cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2023-2026 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "irregular.H"
#include "mathematicalConstants.H"
#include "OSspecific.H"
#include "randomGenerator.H"
#include "rawSetWriter.H"
#include "writeFile.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
namespace waveModels
{
    defineTypeNameAndDebug(irregular, 0);
    addToRunTimeSelectionTable(waveModel, irregular, dictionary);
}
}


// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

Foam::waveModels::AiryCoeffs
Foam::waveModels::irregular::coeffs(const label i) const
{
    return AiryCoeffs(depth_, amplitudes_[i], lengths_[i], g());
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::waveModels::irregular::irregular(const irregular& wave)
:
    waveModel(wave),
    depth_(wave.depth_),
    spectrum_(wave.spectrum_, false),
    n_(wave.n_),
    span_(wave.span_),
    formatter_(wave.formatter_, false),
    amplitudes_(wave.amplitudes_),
    lengths_(wave.lengths_),
    phases_(wave.phases_)
{}


Foam::waveModels::irregular::irregular
(
    const dictionary& dict,
    const scalar g,
    const word& modelName
)
:
    waveModel(dict, g),
    depth_(dict.lookupOrDefault<scalar>("depth", great)),
    spectrum_(waveSpectrum::New(dict, g)),
    n_(dict.lookup<label>("n")),
    span_(dict.lookupOrDefault("span", Pair<scalar>(0.01, 0.99))),
    formatter_
    (
        dict.found("setFormat")
      ? setWriter::New(dict.lookup("setFormat"), dict).ptr()
      : nullptr
    ),
    amplitudes_(n_),
    lengths_(n_),
    phases_(n_)
{
    // Get the bounds of the frequency space
    const Pair<scalar> f01
    (
        spectrum_->fFraction(span_.first()),
        spectrum_->fFraction(span_.second())
    );

    // Create a discretisation in frequency space
    const scalarField x(scalarField(scalarList(identityMap(n_ + 1)))/n_);
    const scalarField f((1 - x)*f01.first() + x*f01.second());

    // Evaluate the integrals of the distribution
    const scalarField integralS(spectrum_->integralS(f));
    const scalarField integralFS(spectrum_->integralFS(f));

    // Set the wave parameters using means constructed from the integrals over
    // the frequency intervals
    scalarList periods(n_);
    forAll(amplitudes_, i)
    {
        const label j0 = n_ - 1 - i, j1 = n_ - i;

        // Integrating the of power spectral density gives the square of the
        // root-mean-squared value. So, a factor of two is needed to convert to
        // peak amplitude.
        amplitudes_[i] = sqrt(2*(integralS[j1] - integralS[j0]));

        // Frequency is the integrated average, Integral(f*S)/Integral(S), for
        // this interval. Period is the reciprocal of this value.
        periods[i] =
            (integralS[j1] - integralS[j0])/(integralFS[j1] - integralFS[j0]);

        // Let the Airy calculation engine convert the above to wavelength
        lengths_[i] =
            AiryCoeffs
            (
                depth_,
                amplitudes_[i],
                periods[i],
                g,
                AiryCoeffs::celerity
            ).length;
    }

    // Set random phases
    randomGenerator rndGen(0, true);
    forAll(phases_, i)
    {
        phases_[i] = rndGen.scalarAB(0, constant::mathematical::twoPi);
    }

    // Report
    const scalar avgAmplitude =
        sqrt(2*(integralS.last() - integralS.first())/(f.last() - f.first()));
    const scalar avgPeroid =
        (integralS.last() - integralS.first())
       /(integralFS.last() - integralFS.first());
    const scalar avgLength =
        AiryCoeffs(depth_, avgAmplitude, avgPeroid, g, AiryCoeffs::celerity)
       .length;
    const string::size_type pad =
        waveModel::typeName.length() + modelName.length() + 2;
    Info<< waveModel::typeName << ": " << modelName
        << ": min/average/max period = "
        << periods.first() << '/' << avgPeroid << '/' << periods.last()
        << endl << string(pad, ' ').c_str()
        << "  min/average/max length = "
        << lengths_.first() << '/' << avgLength << '/' << lengths_.last()
        << endl;

    // Write out the continuous distribution
    if (Pstream::master() && formatter_.valid())
    {
        static const scalar t = 0.1;

        const Pair<scalar> g01
        (
            max(rootVSmall, (1 + t)*(f01.first() - t*f01.second())),
            - t*f01.first() + (1 + t)*f01.second()
        );

        const label nn = 1024;
        const scalarField xx(linearSequence01(nn + 1));
        const scalarField ff((1 - xx)*g01.first() + xx*g01.second());

        formatter_->write
        (
            cwd()
           /functionObjects::writeFile::outputPrefix
           /waveModel::typeName + "s"
           /modelName + spectrum_->type().capitalise(),
            "continuous",
            coordSet(true, "f", ff),
            "S",
            spectrum_->S(ff)()
        );
    }

    // Write out the sampled distribution
    if (Pstream::master() && formatter_.valid())
    {
        scalarField ff(3*n_ + 1), SS(3*n_ + 1);

        forAll(f, i)
        {
            if (i != 0)
            {
                ff[3*i - 1] = f[i];
                SS[3*i - 1] = sqr(amplitudes_[n_ - i])/2/(f[i] - f[i - 1]);
            }

            ff[3*i] = f[i];
            SS[3*i] = 0;

            if (i != n_)
            {
                ff[3*i + 1] = f[i];
                SS[3*i + 1] = sqr(amplitudes_[n_ - 1 - i])/2/(f[i + 1] - f[i]);
            }
        }

        formatter_->write
        (
            cwd()
           /functionObjects::writeFile::outputPrefix
           /waveModel::typeName + "s"
           /modelName + spectrum_->type().capitalise(),
            "sampled",
            coordSet(true, "f", ff),
            "S",
            SS
        );
    }
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

Foam::waveModels::irregular::~irregular()
{}


// * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //

Foam::scalar Foam::waveModels::irregular::celerity() const
{
    return coeffs(amplitudes_.size() - 1).celerity();
}


Foam::tmp<Foam::scalarField> Foam::waveModels::irregular::elevation
(
    const scalar t,
    const scalarField& x
) const
{
    tmp<scalarField> tResult(new scalarField(x.size(), scalar(0)));
    scalarField& result = tResult.ref();

    forAll(amplitudes_, i)
    {
        result += coeffs(i).elevation(phases_[i], t, x);
    }

    return tResult;
}


Foam::tmp<Foam::vector2DField> Foam::waveModels::irregular::velocity
(
    const scalar t,
    const vector2DField& xz
) const
{
    tmp<vector2DField> tResult(new vector2DField(xz.size(), vector2D::zero));
    vector2DField& result = tResult.ref();

    forAll(amplitudes_, i)
    {
        result += coeffs(i).velocity(phases_[i], t, xz);
    }

    return tResult;
}


void Foam::waveModels::irregular::write(Ostream& os) const
{
    waveModel::write(os);

    if (!coeffs(amplitudes_.size() - 1).deep())
    {
        writeEntry(os, "depth", depth_);
    }

    writeEntry(os, "spectrum", spectrum_->type());

    os  << indent << spectrum_->type() << nl
        << indent << token::BEGIN_BLOCK << nl << incrIndent;
    spectrum_->write(os);
    os  << decrIndent
        << indent << token::END_BLOCK << nl;

    writeEntry(os, "n", n_);
    writeEntryIfDifferent(os, "span", Pair<scalar>(0.01, 0.99), span_);
}


// ************************************************************************* //