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

patchAverage.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 Application
00026     patchAverage
00027 
00028 Description
00029     Calculates the average of the specified field over the specified patch.
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "fvCFD.H"
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 // Main program:
00037 
00038 int main(int argc, char *argv[])
00039 {
00040     timeSelector::addOptions();
00041     argList::validArgs.append("fieldName");
00042     argList::validArgs.append("patchName");
00043 #   include "setRootCase.H"
00044 #   include "createTime.H"
00045     instantList timeDirs = timeSelector::select0(runTime, args);
00046 #   include "createMesh.H"
00047 
00048     word fieldName(args.additionalArgs()[0]);
00049     word patchName(args.additionalArgs()[1]);
00050 
00051     forAll(timeDirs, timeI)
00052     {
00053         runTime.setTime(timeDirs[timeI], timeI);
00054         Info<< "Time = " << runTime.timeName() << endl;
00055 
00056         IOobject fieldHeader
00057         (
00058             fieldName,
00059             runTime.timeName(),
00060             mesh,
00061             IOobject::MUST_READ
00062         );
00063 
00064         // Check field exists
00065         if (fieldHeader.headerOk())
00066         {
00067             mesh.readUpdate();
00068 
00069             label patchi = mesh.boundaryMesh().findPatchID(patchName);
00070             if (patchi < 0)
00071             {
00072                 FatalError
00073                     << "Unable to find patch " << patchName << nl
00074                     << exit(FatalError);
00075             }
00076 
00077             if (fieldHeader.headerClassName() == "volScalarField")
00078             {
00079                 Info<< "    Reading volScalarField " << fieldName << endl;
00080                 volScalarField field(fieldHeader, mesh);
00081 
00082                 scalar area = gSum(mesh.magSf().boundaryField()[patchi]);
00083                 scalar sumField = 0;
00084 
00085                 if (area > 0)
00086                 {
00087                     sumField = gSum
00088                     (
00089                         mesh.magSf().boundaryField()[patchi]
00090                       * field.boundaryField()[patchi]
00091                     ) / area;
00092                 }
00093 
00094                 Info<< "    Average of " << fieldName << " over patch "
00095                     << patchName << '[' << patchi << ']' << " = "
00096                     << sumField << endl;
00097             }
00098             else
00099             {
00100                 FatalError
00101                     << "Only possible to average volScalarFields "
00102                     << nl << exit(FatalError);
00103             }
00104         }
00105         else
00106         {
00107             Info<< "    No field " << fieldName << endl;
00108         }
00109 
00110         Info<< endl;
00111     }
00112 
00113     Info<< "End\n" << endl;
00114 
00115     return 0;
00116 }
00117 
00118 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd