Updates to the fieldValues function object
[OpenFOAM-1.6.x.git] / src / postProcessing / functionObjects / field / fieldValues / cellSource / cellSource.C
blob239aa4342d4c11c1dbe118387398b93eeba5c2a4
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 "cellSource.H"
28 #include "fvMesh.H"
29 #include "volFields.H"
30 #include "IOList.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace Foam
36     namespace fieldValues
37     {
38         defineTypeNameAndDebug(cellSource, 0);
39     }
41     template<>
42     const char* NamedEnum<fieldValues::cellSource::sourceType, 1>::
43         names[] = {"cellZone"};
45     const NamedEnum<fieldValues::cellSource::sourceType, 1>
46         fieldValues::cellSource::sourceTypeNames_;
48     template<>
49     const char* NamedEnum<fieldValues::cellSource::operationType, 5>::
50         names[] =
51         {
52             "none", "sum", "volAverage", "volIntegrate", "weightedAverage"
53         };
55     const NamedEnum<fieldValues::cellSource::operationType, 5>
56         fieldValues::cellSource::operationTypeNames_;
61 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
63 void Foam::fieldValues::cellSource::setCellZoneCells()
65     label zoneId = mesh().cellZones().findZoneID(sourceName_);
67     if (zoneId < 0)
68     {
69         FatalErrorIn("cellSource::cellSource::setCellZoneCells()")
70             << "Unknown cell zone name: " << sourceName_
71             << ". Valid cell zones are: " << mesh().cellZones().names()
72             << nl << exit(FatalError);
73     }
75     const cellZone& cZone = mesh().cellZones()[zoneId];
77     cellId_.setSize(cZone.size());
79     label count = 0;
80     forAll(cZone, i)
81     {
82         label cellI = cZone[i];
83         cellId_[count] = cellI;
84         count++;
85     }
87     cellId_.setSize(count);
89     if (debug)
90     {
91         Info<< "Original cell zone size = " << cZone.size()
92             << ", new size = " << count << endl;
93     }
97 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
99 void Foam::fieldValues::cellSource::initialise(const dictionary& dict)
101     switch (source_)
102     {
103         case stCellZone:
104         {
105             setCellZoneCells();
106             break;
107         }
108         default:
109         {
110             FatalErrorIn("cellSource::initialise()")
111                 << "Unknown source type. Valid source types are:"
112                 << sourceTypeNames_ << nl << exit(FatalError);
113         }
114     }
116     Info<< type() << " " << name_ << ":" << nl
117         << "    total cells  = " << cellId_.size() << nl
118         << "    total volume = " << sum(filterField(mesh().V()))
119         << nl << endl;
121     if (operation_ == opWeightedAverage)
122     {
123         dict.lookup("weightField") >> weightFieldName_;
124         if
125         (
126             obr().foundObject<volScalarField>(weightFieldName_)
127         )
128         {
129             Info<< "    weight field = " << weightFieldName_;
130         }
131         else
132         {
133             FatalErrorIn("cellSource::initialise()")
134                 << type() << " " << name_ << ": "
135                 << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
136                 << nl << "    Weight field " << weightFieldName_
137                 << " must be a " << volScalarField::typeName
138                 << nl << exit(FatalError);
139         }
140     }
142     Info<< nl << endl;
146 void Foam::fieldValues::cellSource::writeFileHeader()
148     if (outputFilePtr_.valid())
149     {
150         outputFilePtr_()
151             << "# Source : " << sourceTypeNames_[source_] << " "
152             << sourceName_ <<  nl << "# Cells  : " << cellId_.size() << nl
153             << "# Time" << tab << "sum(V)";
155         forAll(fields_, i)
156         {
157             outputFilePtr_()
158                 << tab << operationTypeNames_[operation_]
159                 << "(" << fields_[i] << ")";
160         }
162         outputFilePtr_() << endl;
163     }
167 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
169 Foam::fieldValues::cellSource::cellSource
171     const word& name,
172     const objectRegistry& obr,
173     const dictionary& dict,
174     const bool loadFromFiles
177     fieldValue(name, obr, dict, loadFromFiles),
178     source_(sourceTypeNames_.read(dict.lookup("source"))),
179     operation_(operationTypeNames_.read(dict.lookup("operation"))),
180     cellId_()
182     read(dict);
186 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
188 Foam::fieldValues::cellSource::~cellSource()
192 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
194 void Foam::fieldValues::cellSource::read(const dictionary& dict)
196     fieldValue::read(dict);
198     if (active_)
199     {
200         // no additional info to read
201         initialise(dict);
202     }
206 void Foam::fieldValues::cellSource::write()
208     fieldValue::write();
210     if (active_)
211     {
212         if (Pstream::master())
213         {
214             outputFilePtr_()
215                 << obr_.time().value() << tab
216                 << sum(filterField(mesh().V()));
217         }
219         forAll(fields_, i)
220         {
221             writeValues<scalar>(fields_[i]);
222             writeValues<vector>(fields_[i]);
223             writeValues<sphericalTensor>(fields_[i]);
224             writeValues<symmTensor>(fields_[i]);
225             writeValues<tensor>(fields_[i]);
226         }
228         if (Pstream::master())
229         {
230             outputFilePtr_()<< endl;
231         }
233         if (log_)
234         {
235             Info<< endl;
236         }
237     }
241 // ************************************************************************* //