Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / postProcessing / functionObjects / field / fieldValues / cellSource / cellSourceTemplates.C
blobe9be76e52744e6b70536de87145fa221c22529b2
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2009-2011 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
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 "cellSource.H"
27 #include "volFields.H"
29 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
31 template<class Type>
32 bool Foam::fieldValues::cellSource::validField(const word& fieldName) const
34     typedef GeometricField<Type, fvPatchField, volMesh> vf;
36     if (obr_.foundObject<vf>(fieldName))
37     {
38         return true;
39     }
41     return false;
45 template<class Type>
46 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::cellSource::setFieldValues
48     const word& fieldName
49 ) const
51     typedef GeometricField<Type, fvPatchField, volMesh> vf;
53     if (obr_.foundObject<vf>(fieldName))
54     {
55         return filterField(obr_.lookupObject<vf>(fieldName));
56     }
58     return tmp<Field<Type> >(new Field<Type>(0.0));
62 template<class Type>
63 Type Foam::fieldValues::cellSource::processValues
65     const Field<Type>& values,
66     const scalarField& V,
67     const scalarField& weightField
68 ) const
70     Type result = pTraits<Type>::zero;
71     switch (operation_)
72     {
73         case opSum:
74         {
75             result = sum(values);
76             break;
77         }
78         case opVolAverage:
79         {
80             result = sum(values*V)/sum(V);
81             break;
82         }
83         case opVolIntegrate:
84         {
85             result = sum(values*V);
86             break;
87         }
88         case opWeightedAverage:
89         {
90             result = sum(values*weightField)/sum(weightField);
91             break;
92         }
93         case opMin:
94         {
95             result = min(values);
96             break;
97         }
98         case opMax:
99         {
100             result = max(values);
101             break;
102         }
103         default:
104         {
105             // Do nothing
106         }
107     }
109     return result;
113 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
115 template<class Type>
116 bool Foam::fieldValues::cellSource::writeValues(const word& fieldName)
118     const bool ok = validField<Type>(fieldName);
120     if (ok)
121     {
122         Field<Type> values(setFieldValues<Type>(fieldName));
123         combineFields(values);
125         scalarField V(filterField(mesh().V()));
126         combineFields(V);
128         scalarField weightField(setFieldValues<scalar>(weightFieldName_));
129         combineFields(weightField);
131         if (Pstream::master())
132         {
133             Type result = processValues(values, V, weightField);
135             if (valueOutput_)
136             {
137                 IOField<Type>
138                 (
139                     IOobject
140                     (
141                         fieldName + "_" + sourceTypeNames_[source_] + "-"
142                             + sourceName_,
143                         obr_.time().timeName(),
144                         obr_,
145                         IOobject::NO_READ,
146                         IOobject::NO_WRITE
147                     ),
148                     values
149                 ).write();
150             }
153             outputFilePtr_()<< tab << result;
155             if (log_)
156             {
157                 Info<< "    " << operationTypeNames_[operation_]
158                     << "(" << sourceName_ << ") for " << fieldName
159                     <<  " = " << result << endl;
160             }
161         }
162     }
164     return ok;
168 template<class Type>
169 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::cellSource::filterField
171     const Field<Type>& field
172 ) const
174     return tmp<Field<Type> >(new Field<Type>(field, cellId_));
178 // ************************************************************************* //