FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

cellSourceTemplates.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) 2009-2011 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "cellSource.H"
00027 #include <finiteVolume/volFields.H>
00028 #include <OpenFOAM/IOList.H>
00029 
00030 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
00031 
00032 template<class Type>
00033 bool Foam::fieldValues::cellSource::validField(const word& fieldName) const
00034 {
00035     typedef GeometricField<Type, fvPatchField, volMesh> vf;
00036 
00037     if (obr_.foundObject<vf>(fieldName))
00038     {
00039         return true;
00040     }
00041 
00042     return false;
00043 }
00044 
00045 
00046 template<class Type>
00047 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::cellSource::setFieldValues
00048 (
00049     const word& fieldName
00050 ) const
00051 {
00052     typedef GeometricField<Type, fvPatchField, volMesh> vf;
00053 
00054     if (obr_.foundObject<vf>(fieldName))
00055     {
00056         return filterField(obr_.lookupObject<vf>(fieldName));
00057     }
00058 
00059     return tmp<Field<Type> >(new Field<Type>(0.0));
00060 }
00061 
00062 
00063 template<class Type>
00064 Type Foam::fieldValues::cellSource::processValues
00065 (
00066     const Field<Type>& values,
00067     const scalarField& V,
00068     const scalarField& weightField
00069 ) const
00070 {
00071     Type result = pTraits<Type>::zero;
00072     switch (operation_)
00073     {
00074         case opSum:
00075         {
00076             result = sum(values);
00077             break;
00078         }
00079         case opVolAverage:
00080         {
00081             result = sum(values*V)/sum(V);
00082             break;
00083         }
00084         case opVolIntegrate:
00085         {
00086             result = sum(values*V);
00087             break;
00088         }
00089         case opWeightedAverage:
00090         {
00091             result = sum(values*weightField)/sum(weightField);
00092             break;
00093         }
00094         case opMin:
00095         {
00096             result = min(values);
00097             break;
00098         }
00099         case opMax:
00100         {
00101             result = max(values);
00102             break;
00103         }
00104         default:
00105         {
00106             // Do nothing
00107         }
00108     }
00109 
00110     return result;
00111 }
00112 
00113 
00114 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00115 
00116 template<class Type>
00117 bool Foam::fieldValues::cellSource::writeValues(const word& fieldName)
00118 {
00119     const bool ok = validField<Type>(fieldName);
00120 
00121     if (ok)
00122     {
00123         Field<Type> values = combineFields(setFieldValues<Type>(fieldName)());
00124 
00125         scalarField V = combineFields(filterField(mesh().V())());
00126 
00127         scalarField weightField =
00128             combineFields(setFieldValues<scalar>(weightFieldName_)());
00129 
00130         if (Pstream::master())
00131         {
00132             Type result = processValues(values, V, weightField);
00133 
00134             if (valueOutput_)
00135             {
00136                 IOList<Type>
00137                 (
00138                     IOobject
00139                     (
00140                         fieldName + "_" + sourceTypeNames_[source_] + "-"
00141                             + sourceName_,
00142                         obr_.time().timeName(),
00143                         obr_,
00144                         IOobject::NO_READ,
00145                         IOobject::NO_WRITE
00146                     ),
00147                     values
00148                 ).write();
00149             }
00150 
00151 
00152             outputFilePtr_()<< tab << result;
00153 
00154             if (log_)
00155             {
00156                 Info<< "    " << operationTypeNames_[operation_]
00157                     << "(" << sourceName_ << ") for " << fieldName
00158                     <<  " = " << result << endl;
00159             }
00160         }
00161     }
00162 
00163     return ok;
00164 }
00165 
00166 
00167 template<class Type>
00168 Foam::tmp<Foam::Field<Type> > Foam::fieldValues::cellSource::filterField
00169 (
00170     const Field<Type>& field
00171 ) const
00172 {
00173     return tmp<Field<Type> >(new Field<Type>(field, cellId_));
00174 }
00175 
00176 
00177 // ************************ vim: set sw=4 sts=4 et: ************************ //
00178 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines