initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / finiteVolume / cfdTools / general / adjustPhi / adjustPhi.C
blob9977a3470582fafe5c4cd66b563742e400ed1959
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "adjustPhi.H"
28 #include "volFields.H"
29 #include "surfaceFields.H"
30 #include "processorFvsPatchFields.H"
31 #include "inletOutletFvPatchFields.H"
33 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
35 bool Foam::adjustPhi
37     surfaceScalarField& phi,
38     const volVectorField& U,
39     volScalarField& p
42     if (p.needReference())
43     {
44         p.boundaryField().updateCoeffs();
46         scalar massIn = 0.0;
47         scalar fixedMassOut = 0.0;
48         scalar adjustableMassOut = 0.0;
50         forAll (phi.boundaryField(), patchi)
51         {
52             const fvPatchVectorField& Up = U.boundaryField()[patchi];
53             const fvsPatchScalarField& phip = phi.boundaryField()[patchi];
55             if (!isType<processorFvsPatchScalarField>(phip))
56             {
57                 if
58                 (
59                     Up.fixesValue()
60                  && !isA<inletOutletFvPatchVectorField>(Up)
61                 )
62                 {
63                     forAll(phip, i)
64                     {
65                         if (phip[i] < 0.0)
66                         {
67                             massIn -= phip[i];
68                         }
69                         else
70                         {
71                             fixedMassOut += phip[i];
72                         }
73                     }
74                 }
75                 else
76                 {
77                     forAll(phip, i)
78                     {
79                         if (phip[i] < 0.0)
80                         {
81                             massIn -= phip[i];
82                         }
83                         else
84                         {
85                             adjustableMassOut += phip[i];
86                         }
87                     }
88                 }
89             }
90         }
92         // Calculate the total flux in the domain, used for normalisation
93         scalar totalFlux = VSMALL + sum(mag(phi)).value();
95         reduce(massIn, sumOp<scalar>());
96         reduce(fixedMassOut, sumOp<scalar>());
97         reduce(adjustableMassOut, sumOp<scalar>());
99         scalar massCorr = 1.0;
100         scalar magAdjustableMassOut = mag(adjustableMassOut);
102         if
103         (
104             magAdjustableMassOut > VSMALL
105          && magAdjustableMassOut/totalFlux > SMALL
106         )
107         {
108             massCorr = (massIn - fixedMassOut)/adjustableMassOut;
109         }
110         else if(mag(fixedMassOut - massIn)/totalFlux > 1e-10)
111         {
112             FatalErrorIn
113             (
114                 "adjustPhi(surfaceScalarField& phi, const volVectorField& U,"
115                 "const volScalarField& p"
116             )   << "Continuity error cannot be removed by adjusting the"
117                    " outflow.\nPlease check the velocity boundary conditions"
118                    " and/or run potentialFoam to initialise the outflow." << nl
119                 << "Total flux              : " << totalFlux << nl
120                 << "Specified mass inflow   : " << massIn << nl
121                 << "Specified mass outflow  : " << fixedMassOut << nl
122                 << "Adjustable mass outflow : " << adjustableMassOut << nl
123                 << exit(FatalError);
124         }
126         forAll (phi.boundaryField(), patchi)
127         {
128             const fvPatchVectorField& Up = U.boundaryField()[patchi];
129             fvsPatchScalarField& phip = phi.boundaryField()[patchi];
131             if (!isType<processorFvsPatchScalarField>(phip))
132             {
133                 if
134                 (
135                     !Up.fixesValue()
136                  || isA<inletOutletFvPatchVectorField>(Up)
137                 )
138                 {
139                     forAll(phip, i)
140                     {
141                         if (phip[i] > 0.0)
142                         {
143                             phip[i] *= massCorr;
144                         }
145                     }
146                 }
147             }
148         }
150         return mag(massIn)/totalFlux < SMALL
151             && mag(fixedMassOut)/totalFlux < SMALL
152             && mag(adjustableMassOut)/totalFlux < SMALL;
153     }
154     else
155     {
156         return false;
157     }
161 // ************************************************************************* //