pressure.C 7.25 KB
Newer Older
1
2
3
4
5
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2012-2016 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2016-2017 OpenCFD Ltd.
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
-------------------------------------------------------------------------------
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 "pressure.H"
#include "volFields.H"
#include "addToRunTimeSelectionTable.H"

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

namespace Foam
{
namespace functionObjects
{
    defineTypeNameAndDebug(pressure, 0);
    addToRunTimeSelectionTable(functionObject, pressure, dictionary);
}
}


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

Foam::word Foam::functionObjects::pressure::resultName() const
{
    word rName;

    if (calcTotal_)
    {
        rName = "total(" + fieldName_ + ")";
    }
    else
    {
        rName = "static(" + fieldName_ + ")";
    }

    if (calcCoeff_)
    {
        rName += "_coeff";
    }

    return rName;
}


Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::rhoScale
(
    const volScalarField& p
) const
{
    if (p.dimensions() == dimPressure)
    {
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        return tmp<volScalarField>
        (
            new volScalarField
            (
                IOobject
                (
                    "rhoScale",
                    p.mesh().time().timeName(),
                    p.mesh(),
                    IOobject::NO_READ,
                    IOobject::NO_WRITE,
                    false
                ),
                p,
                fvPatchField<scalar>::calculatedType()
            )
        );
90
91
92
    }
    else
    {
93
94
95
        if (!rhoInfInitialised_)
        {
            FatalErrorInFunction
96
                << type() << " " << name() << ": "
97
                << "pressure identified as incompressible, but reference "
98
99
                << "density is not set.  Please set 'rho' to 'rhoInf', and "
                << "set an appropriate value for 'rhoInf'"
100
101
102
                << exit(FatalError);
        }

103
104
105
106
107
108
109
110
111
112
113
114
115
        return dimensionedScalar("rhoInf", dimDensity, rhoInf_)*p;
    }
}


Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::rhoScale
(
    const volScalarField& p,
    const tmp<volScalarField>& tsf
) const
{
    if (p.dimensions() == dimPressure)
    {
116
        return lookupObject<volScalarField>(rhoName_)*tsf;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    }
    else
    {
        return dimensionedScalar("rhoInf", dimDensity, rhoInf_)*tsf;
    }
}


Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::pRef
(
    const tmp<volScalarField>& tp
) const
{
    if (calcTotal_)
    {
        return tp + dimensionedScalar("pRef", dimPressure, pRef_);
    }
    else
    {
136
        return std::move(tp);
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    }
}


Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::pDyn
(
    const volScalarField& p,
    const tmp<volScalarField>& tp
) const
{
    if (calcTotal_)
    {
        return
            tp
151
          + rhoScale(p, 0.5*magSqr(lookupObject<volVectorField>(UName_)));
152
153
154
    }
    else
    {
155
        return std::move(tp);
156
157
158
159
    }
}


160
Foam::tmp<Foam::volScalarField> Foam::functionObjects::pressure::coeff
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(
    const tmp<volScalarField>& tp
) const
{
    if (calcCoeff_)
    {
        tmp<volScalarField> tpCoeff(tp.ptr());
        volScalarField& pCoeff = tpCoeff.ref();

        pCoeff -= dimensionedScalar("pInf", dimPressure, pInf_);

        const dimensionedScalar pSmall("pSmall", dimPressure, SMALL);
        const dimensionedVector U("U", dimVelocity, UInf_);
        const dimensionedScalar rho("rho", dimDensity, rhoInf_);

        pCoeff /= 0.5*rho*magSqr(U) + pSmall;

        return tpCoeff;
    }
    else
    {
182
        return std::move(tp);
183
184
185
186
    }
}


187
188
189
190
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

bool Foam::functionObjects::pressure::calc()
{
191
    if (foundObject<volScalarField>(fieldName_))
192
    {
193
        const volScalarField& p = lookupObject<volScalarField>(fieldName_);
194

195
        tmp<volScalarField> tp
196
        (
197
198
            new volScalarField
            (
199
200
201
202
203
204
205
206
                IOobject
                (
                    resultName_,
                    p.mesh().time().timeName(),
                    p.mesh(),
                    IOobject::NO_READ,
                    IOobject::NO_WRITE
                ),
207
                coeff(pRef(pDyn(p, rhoScale(p))))
208
            )
209
        );
210
211

        return store(resultName_, tp);
212
213
214
215
216
217
218
219
    }
    else
    {
        return false;
    }
}


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::functionObjects::pressure::pressure
(
    const word& name,
    const Time& runTime,
    const dictionary& dict
)
:
    fieldExpression(name, runTime, dict, "p"),
    UName_("U"),
    rhoName_("rho"),
    calcTotal_(false),
    pRef_(0),
    calcCoeff_(false),
    pInf_(0),
    UInf_(Zero),
237
238
    rhoInf_(1),
    rhoInfInitialised_(false)
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
{
    read(dict);

    dimensionSet pDims(dimPressure);

    if (calcCoeff_)
    {
        pDims /= dimPressure;
    }
}


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

Foam::functionObjects::pressure::~pressure()
{}


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

bool Foam::functionObjects::pressure::read(const dictionary& dict)
{
261
262
    fieldExpression::read(dict);

263
264
    UName_   = dict.lookupOrDefault<word>("U", "U");
    rhoName_ = dict.lookupOrDefault<word>("rho", "rho");
265
266
267
268

    if (rhoName_ == "rhoInf")
    {
        dict.lookup("rhoInf") >> rhoInf_;
269
        rhoInfInitialised_ = true;
270
271
272
273
274
    }

    dict.lookup("calcTotal") >> calcTotal_;
    if (calcTotal_)
    {
275
        pRef_ = dict.lookupOrDefault<scalar>("pRef", 0.0);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    }

    dict.lookup("calcCoeff") >> calcCoeff_;
    if (calcCoeff_)
    {
        dict.lookup("pInf") >> pInf_;
        dict.lookup("UInf") >> UInf_;
        dict.lookup("rhoInf") >> rhoInf_;

        scalar zeroCheck = 0.5*rhoInf_*magSqr(UInf_) + pInf_;

        if (mag(zeroCheck) < ROOTVSMALL)
        {
            WarningInFunction
                << type() << " " << name() << ": "
                << "Coefficient calculation requested, but reference "
                << "pressure level is zero.  Please check the supplied "
                << "values of pInf, UInf and rhoInf" << endl;
        }
295
296

        rhoInfInitialised_ = true;
297
298
299
300
301
302
303
304
305
    }

    resultName_ = dict.lookupOrDefault<word>("result", resultName());

    return true;
}


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