BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / postProcessing / functionObjects / field / fieldValues / faceSource / faceSourceTemplates.C
blob47fb3a61e10e460ef461b85b487435a3dddb45fa
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
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
19     for more details.
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 "faceSource.H"
27 #include "surfaceFields.H"
28 #include "volFields.H"
29 #include "sampledSurface.H"
31 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
33 template<class Type>
34 bool Foam::fieldValues::faceSource::validField(const word& fieldName) const
36     typedef GeometricField<Type, fvsPatchField, surfaceMesh> sf;
37     typedef GeometricField<Type, fvPatchField, volMesh> vf;
39     if (source_ != stSampledSurface && obr_.foundObject<sf>(fieldName))
40     {
41         return true;
42     }
43     else if (obr_.foundObject<vf>(fieldName))
44     {
45         return true;
46     }
48     return false;
52 template<class Type>
53 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::faceSource::getFieldValues
55     const word& fieldName,
56     const bool mustGet
57 ) const
59     typedef GeometricField<Type, fvsPatchField, surfaceMesh> sf;
60     typedef GeometricField<Type, fvPatchField, volMesh> vf;
62     if (source_ != stSampledSurface && obr_.foundObject<sf>(fieldName))
63     {
64         return filterField(obr_.lookupObject<sf>(fieldName), true);
65     }
66     else if (obr_.foundObject<vf>(fieldName))
67     {
68         if (surfacePtr_.valid())
69         {
70             return surfacePtr_().sample(obr_.lookupObject<vf>(fieldName));
71         }
72         else
73         {
74             return filterField(obr_.lookupObject<vf>(fieldName), true);
75         }
76     }
78     if (mustGet)
79     {
80         FatalErrorIn
81         (
82             "Foam::tmp<Foam::Field<Type> > "
83             "Foam::fieldValues::faceSource::getFieldValues"
84             "("
85                 "const word&, "
86                 "const bool"
87             ") const"
88         )   << "Field " << fieldName << " not found in database"
89             << abort(FatalError);
90     }
92     return tmp<Field<Type> >(new Field<Type>(0));
96 template<class Type>
97 Type Foam::fieldValues::faceSource::processValues
99     const Field<Type>& values,
100     const scalarField& magSf,
101     const scalarField& weightField
102 ) const
104     Type result = pTraits<Type>::zero;
105     switch (operation_)
106     {
107         case opSum:
108         {
109             result = sum(values);
110             break;
111         }
112         case opAreaAverage:
113         {
114             result = sum(values*magSf)/sum(magSf);
115             break;
116         }
117         case opAreaIntegrate:
118         {
119             result = sum(values*magSf);
120             break;
121         }
122         case opWeightedAverage:
123         {
124             result = sum(values*weightField)/sum(weightField);
125             break;
126         }
127         case opMin:
128         {
129             result = min(values);
130             break;
131         }
132         case opMax:
133         {
134             result = max(values);
135             break;
136         }
137         default:
138         {
139             // Do nothing
140         }
141     }
143     return result;
147 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
149 template<class Type>
150 bool Foam::fieldValues::faceSource::writeValues(const word& fieldName)
152     const bool ok = validField<Type>(fieldName);
154     if (ok)
155     {
156         Field<Type> values(getFieldValues<Type>(fieldName));
157         scalarField weightField;
159         if (operation_ == opWeightedAverage)
160         {
161             weightField = getFieldValues<scalar>(weightFieldName_, true);
162         }
164         scalarField magSf;
166         if (surfacePtr_.valid())
167         {
168             // Get unoriented magSf
169             magSf = surfacePtr_().magSf();
170         }
171         else
172         {
173             // Get unoriented magSf
174             magSf = filterField(mesh().magSf(), false);
175         }
177         // Combine onto master
178         combineFields(values);
179         combineFields(magSf);
180         combineFields(weightField);
183         if (Pstream::master())
184         {
185             Type result = processValues(values, magSf, weightField);
187             if (valueOutput_)
188             {
189                 IOField<Type>
190                 (
191                     IOobject
192                     (
193                         fieldName + "_" + sourceTypeNames_[source_] + "-"
194                             + sourceName_,
195                         obr_.time().timeName(),
196                         obr_,
197                         IOobject::NO_READ,
198                         IOobject::NO_WRITE
199                     ),
200                     values
201                 ).write();
202             }
204             outputFilePtr_()<< tab << result;
206             if (log_)
207             {
208                 Info<< "    " << operationTypeNames_[operation_]
209                     << "(" << sourceName_ << ") for " << fieldName
210                     <<  " = " << result << endl;
211             }
212         }
213     }
215     return ok;
219 template<class Type>
220 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::faceSource::filterField
222     const GeometricField<Type, fvPatchField, volMesh>& field,
223     const bool applyOrientation
224 ) const
226     tmp<Field<Type> > tvalues(new Field<Type>(faceId_.size()));
227     Field<Type>& values = tvalues();
229     forAll(values, i)
230     {
231         label faceI = faceId_[i];
232         label patchI = facePatchId_[i];
233         if (patchI >= 0)
234         {
235             values[i] = field.boundaryField()[patchI][faceI];
236         }
237         else
238         {
239             FatalErrorIn
240             (
241                 "fieldValues::faceSource::filterField"
242                 "("
243                     "const GeometricField<Type, fvPatchField, volMesh>&"
244                 ") const"
245             )   << type() << " " << name_ << ": "
246                 << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
247                 << nl
248                 << "    Unable to process internal faces for volume field "
249                 << field.name() << nl << abort(FatalError);
250         }
251     }
253     if (applyOrientation)
254     {
255         forAll(values, i)
256         {
257             values[i] *= faceSign_[i];
258         }
259     }
261     return tvalues;
265 template<class Type>
266 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::faceSource::filterField
268     const GeometricField<Type, fvsPatchField, surfaceMesh>& field,
269     const bool applyOrientation
270 ) const
272     tmp<Field<Type> > tvalues(new Field<Type>(faceId_.size()));
273     Field<Type>& values = tvalues();
275     forAll(values, i)
276     {
277         label faceI = faceId_[i];
278         label patchI = facePatchId_[i];
279         if (patchI >= 0)
280         {
281             values[i] = field.boundaryField()[patchI][faceI];
282         }
283         else
284         {
285             values[i] = field[faceI];
286         }
287     }
289     if (applyOrientation)
290     {
291         forAll(values, i)
292         {
293             values[i] *= faceSign_[i];
294         }
295     }
297     return tvalues;
301 // ************************************************************************* //