OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

pressureGradientExplicitSource.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "pressureGradientExplicitSource.H"
00028 #include "volFields.H"
00029 #include "IFstream.H"
00030 
00031 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
00032 
00033 void Foam::pressureGradientExplicitSource::writeGradP() const
00034 {
00035     // Only write on output time
00036     if (mesh_.time().outputTime())
00037     {
00038         IOdictionary propsDict
00039         (
00040             IOobject
00041             (
00042                 sourceName_ + "Properties",
00043                 mesh_.time().timeName(),
00044                 "uniform",
00045                 mesh_,
00046                 IOobject::NO_READ,
00047                 IOobject::NO_WRITE
00048             )
00049         );
00050         propsDict.add("gradient", gradP_);
00051         propsDict.regIOobject::write();
00052     }
00053 }
00054 
00055 
00056 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00057 
00058 Foam::pressureGradientExplicitSource::pressureGradientExplicitSource
00059 (
00060     const word& sourceName,
00061     volVectorField& U
00062 )
00063 :
00064     sourceName_(sourceName),
00065     mesh_(U.mesh()),
00066     U_(U),
00067     dict_
00068     (
00069         IOobject
00070         (
00071             sourceName + "Properties",
00072             mesh_.time().constant(),
00073             mesh_,
00074             IOobject::MUST_READ,
00075             IOobject::NO_WRITE
00076         )
00077     ),
00078     Ubar_(dict_.lookup("Ubar")),
00079     gradPini_(dict_.lookup("gradPini")),
00080     gradP_(gradPini_),
00081     flowDir_(Ubar_/mag(Ubar_)),
00082     cellSource_(dict_.lookup("cellSource")),
00083     cellSelector_
00084     (
00085         topoSetSource::New
00086         (
00087             cellSource_,
00088             mesh_,
00089             dict_.subDict(cellSource_ + "Coeffs")
00090         )
00091     ),
00092     selectedCellSet_
00093     (
00094         mesh_,
00095         sourceName_ + "CellSet",
00096         mesh_.nCells()/10 + 1  // Reasonable size estimate.
00097     )
00098 {
00099     // Create the cell set
00100     cellSelector_->applyToSet
00101     (
00102         topoSetSource::NEW,
00103         selectedCellSet_
00104     );
00105 
00106     // Give some feedback
00107     Info<< "    Selected "
00108         << returnReduce(selectedCellSet_.size(), sumOp<label>())
00109         << " cells" << endl;
00110 
00111     // Read the initial pressure gradient from file if it exists
00112     IFstream propsFile
00113     (
00114         mesh_.time().timeName()/"uniform"/(sourceName_ + "Properties")
00115     );
00116 
00117     if (propsFile.good())
00118     {
00119         Info<< "    Reading pressure gradient from file" << endl;
00120         dictionary propsDict(dictionary::null, propsFile);
00121         propsDict.lookup("gradient") >> gradP_;
00122     }
00123 
00124     Info<< "    Initial pressure gradient = " << gradP_ << nl << endl;
00125 }
00126 
00127 
00128 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00129 
00130 Foam::tmp<Foam::DimensionedField<Foam::vector, Foam::volMesh> >
00131 Foam::pressureGradientExplicitSource::Su() const
00132 {
00133     tmp<DimensionedField<vector, volMesh> > tSource
00134     (
00135         new DimensionedField<vector, volMesh>
00136         (
00137             IOobject
00138             (
00139                 sourceName_,
00140                 mesh_.time().timeName(),
00141                 mesh_,
00142                 IOobject::NO_READ,
00143                 IOobject::NO_WRITE
00144             ),
00145             mesh_,
00146             dimensionedVector("zero", gradP_.dimensions(), vector::zero)
00147         )
00148     );
00149 
00150     DimensionedField<vector, volMesh>& sourceField = tSource();
00151 
00152     forAllConstIter(cellSet, selectedCellSet_, iter)
00153     {
00154         label cellI = iter.key();
00155 
00156         sourceField[cellI] = flowDir_*gradP_.value();
00157     }
00158 
00159     return tSource;
00160 }
00161 
00162 
00163 void Foam::pressureGradientExplicitSource::update()
00164 {
00165     const volScalarField& rUA =
00166         mesh_.lookupObject<volScalarField>("(1|A(" + U_.name() + "))");
00167 
00168     // Integrate flow variables over cell set
00169     scalar volTot = 0.0;
00170     scalar magUbarAve = 0.0;
00171     scalar rUAave = 0.0;
00172     forAllConstIter(cellSet, selectedCellSet_, iter)
00173     {
00174         label cellI = iter.key();
00175 
00176         scalar volCell = mesh_.V()[cellI];
00177         volTot += volCell;
00178 
00179         magUbarAve += (flowDir_ & U_[cellI])*volCell;
00180         rUAave += rUA[cellI]*volCell;
00181     }
00182 
00183     // Collect across all processors
00184     reduce(volTot, sumOp<scalar>());
00185     reduce(magUbarAve, sumOp<scalar>());
00186     reduce(rUAave, sumOp<scalar>());
00187 
00188     // Volume averages
00189     magUbarAve /= volTot;
00190     rUAave /= volTot;
00191 
00192     // Calculate the pressure gradient increment needed to adjust the average
00193     // flow-rate to the desired value
00194     scalar gradPplus = (mag(Ubar_) - magUbarAve)/rUAave;
00195 
00196     // Apply correction to velocity field
00197     forAllConstIter(cellSet, selectedCellSet_, iter)
00198     {
00199         label cellI = iter.key();
00200         U_[cellI] += flowDir_*rUA[cellI]*gradPplus;
00201     }
00202 
00203     // Update pressure gradient
00204     gradP_.value() += gradPplus;
00205 
00206     Info<< "Uncorrected Ubar = " << magUbarAve << tab
00207         << "Pressure gradient = " << gradP_.value() << endl;
00208 
00209     writeGradP();
00210 }
00211 
00212 
00213 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd