FreeFOAM The Cross-Platform CFD Toolkit
patchAverage.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Application
25  patchAverage
26 
27 Description
28  Calculates the average of the specified field over the specified patch.
29 
30 Usage
31 
32  - patchAverage [OPTIONS] <fieldName> <patchName>
33 
34  @param <fieldName> \n
35  @todo Detailed description of argument.
36 
37  @param <patchName> \n
38  @todo Detailed description of argument.
39 
40  @param -noZero \n
41  Ignore timestep 0.
42 
43  @param -constant \n
44  Include the constant directory.
45 
46  @param -time <time>\n
47  Apply only to specific time.
48 
49  @param -latestTime \n
50  Only apply to latest time step.
51 
52  @param -case <dir>\n
53  Case directory.
54 
55  @param -parallel \n
56  Run in parallel.
57 
58  @param -help \n
59  Display help message.
60 
61  @param -doc \n
62  Display Doxygen API documentation page for this application.
63 
64  @param -srcDoc \n
65  Display Doxygen source documentation page for this application.
66 
67 \*---------------------------------------------------------------------------*/
68 
69 #include <finiteVolume/fvCFD.H>
70 
71 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
72 // Main program:
73 
74 int main(int argc, char *argv[])
75 {
76  timeSelector::addOptions();
77  argList::validArgs.append("fieldName");
78  argList::validArgs.append("patchName");
79 # include <OpenFOAM/setRootCase.H>
80 # include <OpenFOAM/createTime.H>
81  instantList timeDirs = timeSelector::select0(runTime, args);
82 # include <OpenFOAM/createMesh.H>
83 
84  word fieldName(args.additionalArgs()[0]);
85  word patchName(args.additionalArgs()[1]);
86 
87  forAll(timeDirs, timeI)
88  {
89  runTime.setTime(timeDirs[timeI], timeI);
90  Info<< "Time = " << runTime.timeName() << endl;
91 
92  IOobject fieldHeader
93  (
94  fieldName,
95  runTime.timeName(),
96  mesh,
97  IOobject::MUST_READ
98  );
99 
100  // Check field exists
101  if (fieldHeader.headerOk())
102  {
103  mesh.readUpdate();
104 
105  label patchi = mesh.boundaryMesh().findPatchID(patchName);
106  if (patchi < 0)
107  {
108  FatalError
109  << "Unable to find patch " << patchName << nl
110  << exit(FatalError);
111  }
112 
113  if (fieldHeader.headerClassName() == "volScalarField")
114  {
115  Info<< " Reading volScalarField " << fieldName << endl;
116  volScalarField field(fieldHeader, mesh);
117 
118  scalar area = gSum(mesh.magSf().boundaryField()[patchi]);
119  scalar sumField = 0;
120 
121  if (area > 0)
122  {
123  sumField = gSum
124  (
126  * field.boundaryField()[patchi]
127  ) / area;
128  }
129 
130  Info<< " Average of " << fieldName << " over patch "
131  << patchName << '[' << patchi << ']' << " = "
132  << sumField << endl;
133  }
134  else
135  {
136  FatalError
137  << "Only possible to average volScalarFields "
138  << nl << exit(FatalError);
139  }
140  }
141  else
142  {
143  Info<< " No field " << fieldName << endl;
144  }
145 
146  Info<< endl;
147  }
148 
149  Info<< "End\n" << endl;
150 
151  return 0;
152 }
153 
154 // ************************ vim: set sw=4 sts=4 et: ************************ //