File size: 7,417 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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2017-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 "forcing.H"
#include "fvMatrix.H"
#include "fviGrad.H"
#include "fviDomainIntegrate.H"

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

namespace Foam
{
namespace fv
{
    defineTypeNameAndDebug(forcing, 0);
}
}


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

void Foam::fv::forcing::readLambda(const dictionary& dict)
{
    lambda_ =
        dimensionedScalar
        (
            lambda_.name(),
            lambda_.dimensions(),
            dict.lookup(lambda_.name())
        );

    lambdaBoundary_ =
        dimensionedScalar
        (
            lambdaBoundary_.name(),
            lambdaBoundary_.dimensions(),
            dict.lookupOrDefault(lambdaBoundary_.name(), 0.0)
        );
}


void Foam::fv::forcing::readCoeffs(const dictionary& dict)
{
    writeForceFields_ = dict.lookupOrDefault("writeForceFields", false);

    const bool foundScale = dict.found("scale");
    const bool foundOgn = dict.found("origin");
    const bool foundDir = dict.found("direction");
    const bool foundOgns = dict.found("origins");
    const bool foundDirs = dict.found("directions");
    const bool foundAll =
        foundScale
     && (
            (foundOgn && foundDir && !foundOgns && !foundDirs)
         || (!foundOgn && !foundDir && foundOgns && foundDirs)
        );
     const bool foundAny =
        foundScale || foundOgn || foundDir || foundOgns || foundDirs;

    if (!foundAll)
    {
        scale_ = autoPtr<DimensionedFunction1<scalar>>();
        origins_.clear();
        directions_.clear();
    }

    if (foundAll)
    {
        scale_ =
            DimensionedFunction1<scalar>::New
            (
                "scale",
                dimensions::length,
                dimless,
                dict
            );
        if (foundOgn)
        {
            origins_.setSize(1);
            directions_.setSize(1);
            dict.lookup("origin") >> origins_.last();
            dict.lookup("direction") >> directions_.last();
        }
        else
        {
            dict.lookup("origins") >> origins_;
            dict.lookup("directions") >> directions_;

            if
            (
                origins_.size() == 0
             || directions_.size() == 0
             || origins_.size() != directions_.size()
            )
            {
                FatalErrorInFunction
                    << "The same, non-zero number of origins and "
                    << "directions must be provided" << exit(FatalError);
            }
        }
        forAll(directions_, i)
        {
            directions_[i] /= mag(directions_[i]);
        }
    }

    if (!foundAll && foundAny)
    {
        WarningInFunction
            << "The scaling specification is incomplete. \"scale\", "
            << "\"origin\" and \"direction\" (or \"origins\" and "
            << "\"directions\"), must all be specified in order to scale "
            << "the forcing. The forcing will be applied uniformly across "
            << "the cell set." << endl << endl;
    }
}


Foam::dimensionedScalar Foam::fv::forcing::regionLength() const
{
    dimensionedScalar vs("vs", dimensions::volume, 0);
    dimensionedScalar vgrads("vs", dimensions::area, 0);

    forAll(origins_, i)
    {
        const volScalarField x
        (
            (mesh().C() - dimensionedVector(dimensions::length, origins_[i]))
          & directions_[i]
        );

        const volScalarField scale(scale_->value(x));

        vs += fvi::domainIntegrate(scale);
        vgrads += fvi::domainIntegrate(directions_[i] & fvi::grad(scale));
    }

    return vs/vgrads;
}



Foam::tmp<Foam::volInternalScalarField> Foam::fv::forcing::scale() const
{
    tmp<volInternalScalarField> tscale
    (
        volInternalScalarField::New
        (
            typedName("scale"),
            mesh(),
            dimensionedScalar(dimless, 0)
        )
    );

    volInternalScalarField& scale = tscale.ref();

    forAll(origins_, i)
    {
        const dimensionedVector o(dimLength, origins_[i]);
        const dimensionedVector d(dimless, directions_[i]);
        scale = max(scale, scale_->value(eval((mesh().C()() - o) & d)));
    }

    return tscale;
}


Foam::tmp<Foam::volInternalScalarField> Foam::fv::forcing::forceCoeff() const
{
    tmp<volInternalScalarField> tscale(this->scale());
    const volInternalScalarField& scale = tscale();

    tmp<volInternalScalarField> tforceCoeff
    (
        volInternalScalarField::New(typedName("forceCoeff"), lambda_*scale)
    );

    // Damp the cells adjacent to the boundary with lambdaBoundary if specified
    if (lambdaBoundary_.value() > 0)
    {
        const fvBoundaryMesh& bm = mesh().boundary();

        forAll(bm, patchi)
        {
            if (!bm[patchi].coupled())
            {
                UIndirectList<scalar>(tforceCoeff.ref(), bm[patchi].faceCells())
              = eval
                (
                    lambdaBoundary_.value()
                   *Field<scalar>(scale, bm[patchi].faceCells())
                );
            }
        }
    }

    return tforceCoeff;
}


void Foam::fv::forcing::writeForceFields() const
{
    if (writeForceFields_)
    {
        Info<< "    Writing forcing fields: forcing:scale, forcing:forceCoeff"
            << endl;

        scale()().write();
        forceCoeff()().write();
    }
}


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

Foam::fv::forcing::forcing
(
    const word& name,
    const word& modelType,
    const fvMesh& mesh,
    const dictionary& dict
)
:
    fvModel(name, modelType, mesh, dict),
    writeForceFields_(false),
    lambda_("lambda", dimensions::rate, NaN),
    lambdaBoundary_("lambdaBoundary", dimensions::rate, 0.0),
    scale_(nullptr),
    origins_(),
    directions_()
{
    readCoeffs(coeffs(dict));
}


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

bool Foam::fv::forcing::read(const dictionary& dict)
{
    if (fvModel::read(dict))
    {
        readCoeffs(coeffs(dict));
        return true;
    }
    else
    {
        return false;
    }
}


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