1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "pressureGradientExplicitSource.H"
27 #include "volFields.H"
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 void Foam::pressureGradientExplicitSource::writeGradP() const
34 // Only write on output time
35 if (mesh_.time().outputTime())
37 IOdictionary propsDict
41 sourceName_ + "Properties",
42 mesh_.time().timeName(),
49 propsDict.add("gradient", gradP_);
50 propsDict.regIOobject::write();
55 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
57 Foam::pressureGradientExplicitSource::pressureGradientExplicitSource
59 const word& sourceName,
63 sourceName_(sourceName),
70 sourceName + "Properties",
71 mesh_.time().constant(),
73 IOobject::MUST_READ_IF_MODIFIED,
77 Ubar_(dict_.lookup("Ubar")),
78 gradPini_(dict_.lookup("gradPini")),
80 flowDir_(Ubar_/mag(Ubar_)),
81 cellSource_(dict_.lookup("cellSource")),
88 dict_.subDict(cellSource_ + "Coeffs")
94 sourceName_ + "CellSet",
95 mesh_.nCells()/10 + 1 // Reasonable size estimate.
98 // Create the cell set
99 cellSelector_->applyToSet
105 // Give some feedback
107 << returnReduce(selectedCellSet_.size(), sumOp<label>())
110 // Read the initial pressure gradient from file if it exists
113 mesh_.time().timeName()/"uniform"/(sourceName_ + "Properties")
116 if (propsFile.good())
118 Info<< " Reading pressure gradient from file" << endl;
119 dictionary propsDict(dictionary::null, propsFile);
120 propsDict.lookup("gradient") >> gradP_;
123 Info<< " Initial pressure gradient = " << gradP_ << nl << endl;
127 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 Foam::tmp<Foam::DimensionedField<Foam::vector, Foam::volMesh> >
130 Foam::pressureGradientExplicitSource::Su() const
132 tmp<DimensionedField<vector, volMesh> > tSource
134 new DimensionedField<vector, volMesh>
139 mesh_.time().timeName(),
145 dimensionedVector("zero", gradP_.dimensions(), vector::zero)
149 DimensionedField<vector, volMesh>& sourceField = tSource();
151 forAllConstIter(cellSet, selectedCellSet_, iter)
153 label cellI = iter.key();
155 sourceField[cellI] = flowDir_*gradP_.value();
162 void Foam::pressureGradientExplicitSource::update()
164 const volScalarField& rAU =
165 mesh_.lookupObject<volScalarField>("(1|A(" + U_.name() + "))");
167 // Integrate flow variables over cell set
169 scalar magUbarAve = 0.0;
171 forAllConstIter(cellSet, selectedCellSet_, iter)
173 label cellI = iter.key();
175 scalar volCell = mesh_.V()[cellI];
178 magUbarAve += (flowDir_ & U_[cellI])*volCell;
179 rAUave += rAU[cellI]*volCell;
182 // Collect across all processors
183 reduce(volTot, sumOp<scalar>());
184 reduce(magUbarAve, sumOp<scalar>());
185 reduce(rAUave, sumOp<scalar>());
188 magUbarAve /= volTot;
191 // Calculate the pressure gradient increment needed to adjust the average
192 // flow-rate to the desired value
193 scalar gradPplus = (mag(Ubar_) - magUbarAve)/rAUave;
195 // Apply correction to velocity field
196 forAllConstIter(cellSet, selectedCellSet_, iter)
198 label cellI = iter.key();
199 U_[cellI] += flowDir_*rAU[cellI]*gradPplus;
202 // Update pressure gradient
203 gradP_.value() += gradPplus;
205 Info<< "Uncorrected Ubar = " << magUbarAve << tab
206 << "Pressure gradient = " << gradP_.value() << endl;
212 // ************************************************************************* //