File size: 9,432 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
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     | Website:  https://openfoam.org
    \\  /    A nd           | Copyright (C) 2016-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 "plenumPressureFvPatchScalarField.H"
#include "addToRunTimeSelectionTable.H"
#include "fieldMapper.H"
#include "volFields.H"
#include "surfaceFields.H"

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

Foam::plenumPressureFvPatchScalarField::plenumPressureFvPatchScalarField
(
    const fvPatch& p,
    const DimensionedField<scalar, fvMesh>& iF,
    const dictionary& dict
)
:
    fixedValueFvPatchScalarField(p, iF, dict),
    gamma_(dict.lookup<scalar>("gamma", dimless)),
    R_(dict.lookup<scalar>("R", dimensions::gasConstant)),
    supplyMassFlowRate_
    (
        dict.lookup<scalar>("supplyMassFlowRate", dimensions::massFlux)
    ),
    supplyTotalTemperature_
    (
        dict.lookup<scalar>("supplyTotalTemperature", dimensions::temperature)
    ),
    plenumVolume_(dict.lookup<scalar>("plenumVolume", dimensions::volume)),
    plenumDensity_(dict.lookup<scalar>("plenumDensity", dimensions::density)),
    plenumTemperature_
    (
        dict.lookup<scalar>("plenumTemperature", dimensions::temperature)
    ),
    rho_(1.0),
    hasRho_(false),
    inletAreaRatio_(dict.lookup<scalar>("inletAreaRatio", units::fraction)),
    inletDischargeCoefficient_
    (
        dict.lookup<scalar>("inletDischargeCoefficient", units::fraction)
    ),
    timeScale_
    (
        dict.lookupOrDefault<scalar>("timeScale", dimensions::time, 0.0)
    ),
    phiName_(dict.lookupOrDefault<word>("phi", "phi")),
    UName_(dict.lookupOrDefault<word>("U", "U"))
{
    if (dict.found("rho"))
    {
        rho_ = dict.lookup<scalar>("rho", dimensions::density);
        hasRho_ = true;
    }
}


Foam::plenumPressureFvPatchScalarField::plenumPressureFvPatchScalarField
(
    const plenumPressureFvPatchScalarField& ptf,
    const fvPatch& p,
    const DimensionedField<scalar, fvMesh>& iF,
    const fieldMapper& mapper
)
:
    fixedValueFvPatchScalarField(ptf, p, iF, mapper),
    gamma_(ptf.gamma_),
    R_(ptf.R_),
    supplyMassFlowRate_(ptf.supplyMassFlowRate_),
    supplyTotalTemperature_(ptf.supplyTotalTemperature_),
    plenumVolume_(ptf.plenumVolume_),
    plenumDensity_(ptf.plenumDensity_),
    plenumTemperature_(ptf.plenumTemperature_),
    rho_(ptf.rho_),
    hasRho_(ptf.hasRho_),
    inletAreaRatio_(ptf.inletAreaRatio_),
    inletDischargeCoefficient_(ptf.inletDischargeCoefficient_),
    timeScale_(ptf.timeScale_),
    phiName_(ptf.phiName_),
    UName_(ptf.UName_)
{}


Foam::plenumPressureFvPatchScalarField::plenumPressureFvPatchScalarField
(
    const plenumPressureFvPatchScalarField& tppsf,
    const DimensionedField<scalar, fvMesh>& iF
)
:
    fixedValueFvPatchScalarField(tppsf, iF),
    gamma_(tppsf.gamma_),
    R_(tppsf.R_),
    supplyMassFlowRate_(tppsf.supplyMassFlowRate_),
    supplyTotalTemperature_(tppsf.supplyTotalTemperature_),
    plenumVolume_(tppsf.plenumVolume_),
    plenumDensity_(tppsf.plenumDensity_),
    plenumTemperature_(tppsf.plenumTemperature_),
    rho_(tppsf.rho_),
    hasRho_(tppsf.hasRho_),
    inletAreaRatio_(tppsf.inletAreaRatio_),
    inletDischargeCoefficient_(tppsf.inletDischargeCoefficient_),
    timeScale_(tppsf.timeScale_),
    phiName_(tppsf.phiName_),
    UName_(tppsf.UName_)
{}


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

void Foam::plenumPressureFvPatchScalarField::updateCoeffs()
{
    if (updated())
    {
        return;
    }

    // Patch properties
    const fvPatchField<scalar>& p = *this;
    const fvPatchField<scalar>& p_old =
        db().lookupObject<volScalarField>
        (
            internalField().name()
        ).oldTime().boundaryField()[patch().index()];
    const fvPatchField<vector>& U =
        patch().lookupPatchField<volVectorField, vector>(UName_);
    const fvsPatchField<scalar>& phi =
        patch().lookupPatchField<surfaceScalarField, scalar>(phiName_);

    // Get the timestep
    const scalar dt = time().deltaTValue();

    // Check if operating at a new time index and update the old-time properties
    // if so
    if (timeIndex_ != time().timeIndex())
    {
        timeIndex_ = time().timeIndex();
        plenumDensityOld_ = plenumDensity_;
        plenumTemperatureOld_ = plenumTemperature_;
    }

    // Calculate the current mass flow rate
    scalar massFlowRate(1.0);
    if (phi.internalField().dimensions() == dimensions::volumetricFlux)
    {
        if (hasRho_)
        {
            massFlowRate = - gSum(rho_*phi);
        }
        else
        {
            FatalErrorInFunction
                << "The density must be specified when using a volumetric flux."
                << exit(FatalError);
        }
    }
    else if
    (
        phi.internalField().dimensions()
     == dimensions::massFlux
    )
    {
        if (hasRho_)
        {
            FatalErrorInFunction
                << "The density must be not specified when using a mass flux."
                << exit(FatalError);
        }
        else
        {
            massFlowRate = -gSum(phi.field());
        }
    }
    else
    {
        FatalErrorInFunction
            << "dimensions of phi are not correct"
            << "\n    on patch " << patch().name()
            << " of field " << internalField().name()
            << " in file " << internalField().objectPath() << nl
            << exit(FatalError);
    }

    // Calcaulate the specific heats
    const scalar cv = R_/(gamma_ - 1), cp = R_*gamma_/(gamma_ - 1);

    // Calculate the new plenum properties
    plenumDensity_ =
        plenumDensityOld_
      + (dt/plenumVolume_)*(supplyMassFlowRate_ - massFlowRate);
    plenumTemperature_ =
        plenumTemperatureOld_
      + (dt/(plenumDensity_*cv*plenumVolume_))
      * (
            supplyMassFlowRate_
           *(cp*supplyTotalTemperature_ - cv*plenumTemperature_)
          - massFlowRate*R_*plenumTemperature_
        );
    const scalar plenumPressure = plenumDensity_*R_*plenumTemperature_;

    // Squared velocity magnitude at exit of channels
    const scalarField U_e(magSqr(U/inletAreaRatio_));

    // Exit temperature to plenum temperature ratio
    const scalarField r
    (
        1.0 - (gamma_ - 1.0)*U_e/(2.0*gamma_*R_*plenumTemperature_)
    );

    // Quadratic coefficient (others not needed as b = +1.0 and c = -1.0)
    const scalarField a
    (
        (1.0 - r)/(r*r*inletDischargeCoefficient_*inletDischargeCoefficient_)
    );

    // Isentropic exit temperature to plenum temperature ratio
    const scalarField s(2.0/(1.0 + sqrt(1.0 + 4.0*a)));

    // Exit pressure to plenum pressure ratio
    const scalarField t(pow(s, gamma_/(gamma_ - 1.0)));

    // Limit to prevent outflow
    const scalarField p_new
    (
        neg(phi)*t*plenumPressure + pos0(phi)*max(p, plenumPressure)
    );

    // Relaxation fraction
    const scalar oneByFraction = timeScale_/dt;
    const scalar fraction = oneByFraction < 1.0 ? 1.0 : 1.0/oneByFraction;

    // Set the new value
    operator==((1.0 - fraction)*p_old + fraction*p_new);
    fixedValueFvPatchScalarField::updateCoeffs();
}


void Foam::plenumPressureFvPatchScalarField::write(Ostream& os) const
{
    fvPatchScalarField::write(os);
    writeEntry(os, "gamma", gamma_);
    writeEntry(os, "R", R_);
    writeEntry(os, "supplyMassFlowRate", supplyMassFlowRate_);
    writeEntry(os, "supplyTotalTemperature", supplyTotalTemperature_);
    writeEntry(os, "plenumVolume", plenumVolume_);
    writeEntry(os, "plenumDensity", plenumDensity_);
    writeEntry(os, "plenumTemperature", plenumTemperature_);
    if (hasRho_)
    {
        writeEntry(os, "rho", rho_);
    }
    writeEntry(os, "inletAreaRatio", inletAreaRatio_);
    writeEntry
    (
        os,
        "inletDischargeCoefficient",
        inletDischargeCoefficient_
    );
    writeEntryIfDifferent<scalar>(os, "timeScale", 0.0, timeScale_);
    writeEntryIfDifferent<word>(os, "phi", "phi", phiName_);
    writeEntryIfDifferent<word>(os, "U", "U", UName_);
    writeEntry(os, "value", *this);
}


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

namespace Foam
{
    makePatchTypeField
    (
        fvPatchScalarField,
        plenumPressureFvPatchScalarField
    );
}

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