Added new fieldValues function object
[OpenFOAM-1.6.x.git] / src / postProcessing / functionObjects / field / fieldValues / faceSource / faceSourceTemplates.C
blobd7609573a72cb18d4b7cbeb6c0cee65e67459654
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2009-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 "faceSource.H"
28 #include "surfaceFields.H"
29 #include "volFields.H"
30 #include "IOList.H"
31 #include "ListListOps.H"
33 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
35 template<class Type>
36 bool Foam::fieldValues::faceSource::setFieldValues
38     const word& fieldName,
39     List<Type>& values
40 ) const
42     values.setSize(faceId_.size(), pTraits<Type>::zero);
44     typedef GeometricField<Type, fvsPatchField, surfaceMesh> sf;
45     typedef GeometricField<Type, fvPatchField, volMesh> vf;
47     if (obr_.foundObject<sf>(fieldName))
48     {
49         const sf& field = obr_.lookupObject<sf>(fieldName);
51         forAll(values, i)
52         {
53             label faceI = faceId_[i];
54             label patchI = facePatchId_[i];
55             if (patchI >= 0)
56             {
57                 values[i] = field.boundaryField()[patchI][faceI];
58             }
59             else
60             {
61                 values[i] = field[faceI];
62             }
64             values[i] *= flipMap_[i];
65         }
67         return true;
68     }
69     else if (obr_.foundObject<vf>(fieldName))
70     {
71         const vf& field = obr_.lookupObject<vf>(fieldName);
73         forAll(values, i)
74         {
75             label faceI = faceId_[i];
76             label patchI = facePatchId_[i];
77             if (patchI >= 0)
78             {
79                 values[i] = field.boundaryField()[patchI][faceI];
80             }
81             else
82             {
83                 FatalErrorIn
84                 (
85                     "fieldValues::faceSource::setFieldValues"
86                     "("
87                         "const word&, "
88                         "List<Type>&"
89                     ") const"
90                 )   << type() << " " << name_ << ": "
91                     << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
92                     << nl
93                     << "    Unable to process internal faces for volume field "
94                     << fieldName << nl << abort(FatalError);
95             }
97             values[i] *= flipMap_[i];
98         }
100         return true;
101     }
103     return false;
107 template<class Type>
108 Type Foam::fieldValues::faceSource::processValues
110     const List<Type>& values
111 ) const
113     Type result = pTraits<Type>::zero;
114     switch (operation_)
115     {
116         case opSum:
117         {
118             result = sum(values);
119             break;
120         }
121         case opAreaAverage:
122         {
123             tmp<scalarField> magSf = filterField(mesh().magSf());
124             result = sum(values*magSf())/sum(magSf());
125             break;
126         }
127         case opAreaIntegrate:
128         {
129             result = sum(values*filterField(mesh().magSf()));
130             break;
131         }
132         case opWeightedAverage:
133         {
134             if (mesh().foundObject<volScalarField>(weightFieldName_))
135             {
136                 tmp<scalarField> wField =
137                     filterField
138                     (
139                         mesh().lookupObject<volScalarField>(weightFieldName_)
140                     );
141                result = sum(values*wField())/sum(wField());
142             }
143             else if (mesh().foundObject<surfaceScalarField>(weightFieldName_))
144             {
145                 tmp<scalarField> wField =
146                     filterField
147                     (
148                         mesh().lookupObject<surfaceScalarField>
149                         (
150                             weightFieldName_
151                         )
152                     );
153                result = sum(values*wField())/sum(wField());
154             }
155             else
156             {
157                 FatalErrorIn
158                 (
159                     "fieldValues::faceSource::processValues"
160                     "("
161                         "List<Type>&"
162                     ") const"
163                 )   << type() << " " << name_ << ": "
164                     << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
165                     << nl
166                     << "    Weight field " << weightFieldName_
167                     << " must be either a " << volScalarField::typeName
168                     << " or " << surfaceScalarField::typeName << nl
169                     << abort(FatalError);
170             }
171             break;
172         }
173         default:
174         {
175             // Do nothing
176         }
177     }
179     return result;
183 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
185 template<class Type>
186 bool Foam::fieldValues::faceSource::writeValues(const word& fieldName)
188     List<List<Type> > allValues(Pstream::nProcs());
190     bool validField =
191         setFieldValues<Type>(fieldName, allValues[Pstream::myProcNo()]);
193     if (validField)
194     {
195         Pstream::gatherList(allValues);
197         if (Pstream::master())
198         {
199             List<Type> values =
200                 ListListOps::combine<List<Type> >
201                 (
202                     allValues,
203                     accessOp<List<Type> >()
204                 );
206             Type result = processValues(values);
208             if (valueOutput_)
209             {
210                 IOList<Type>
211                 (
212                     IOobject
213                     (
214                         fieldName + "_" + sourceTypeNames_[source_] + "-"
215                             + sourceName_,
216                         obr_.time().timeName(),
217                         obr_,
218                         IOobject::NO_READ,
219                         IOobject::NO_WRITE
220                     ),
221                     values
222                 ).write();
223             }
226             outputFilePtr_()<< tab << result;
228             if (log_)
229             {
230                 Info<< "    " << operationTypeNames_[operation_]
231                     << "(" << sourceName_ << ") for " << fieldName
232                     <<  " = " << result << endl;
233             }
234         }
235     }
237     return validField;
241 template<class Type>
242 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::faceSource::filterField
244     const GeometricField<Type, fvPatchField, volMesh>& field
245 ) const
247     tmp<Field<Type> > tvalues(new Field<Type>(faceId_.size()));
248     Field<Type>& values = tvalues();
250     forAll(values, i)
251     {
252         label faceI = faceId_[i];
253         label patchI = facePatchId_[i];
254         if (patchI >= 0)
255         {
256             values[i] = field.boundaryField()[patchI][faceI];
257         }
258         else
259         {
260             FatalErrorIn
261             (
262                 "fieldValues::faceSource::filterField"
263                 "("
264                     "const GeometricField<Type, fvPatchField, volMesh>&"
265                 ") const"
266             )   << type() << " " << name_ << ": "
267                 << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
268                 << nl
269                 << "    Unable to process internal faces for volume field "
270                 << field.name() << nl << abort(FatalError);
271         }
273         values[i] *= flipMap_[i];
274     }
276     return tvalues;
280 template<class Type>
281 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::faceSource::filterField
283     const GeometricField<Type, fvsPatchField, surfaceMesh>& field
284 ) const
286     tmp<Field<Type> > tvalues(new Field<Type>(faceId_.size()));
287     Field<Type>& values = tvalues();
289     forAll(values, i)
290     {
291         label faceI = faceId_[i];
292         label patchI = facePatchId_[i];
293         if (patchI >= 0)
294         {
295             values[i] = field.boundaryField()[patchI][faceI];
296         }
297         else
298         {
299             values[i] = field[faceI];
300         }
302         values[i] *= flipMap_[i];
303     }
305     return tvalues;
309 // ************************************************************************* //