Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / src / postProcessing / functionObjects / field / fieldValues / cellSource / cellSource.C
blob31b96f7e98fd7bd6abcd2eb747ee2789b8555642
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 "fvMesh.H"
28 #include "volFields.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::fieldValues::cellSource, 0);
34 namespace Foam
37     template<>
38     const char* Foam::NamedEnum
39     <
40         Foam::fieldValues::cellSource::sourceType,
41         2
42     >::names[] =
43     {
44         "cellZone",
45         "all"
46     };
49     template<>
50     const char* Foam::NamedEnum
51     <
52         Foam::fieldValues::cellSource::operationType,
53         7
54     >::names[] =
55     {
56         "none",
57         "sum",
58         "volAverage",
59         "volIntegrate",
60         "weightedAverage",
61         "min",
62         "max"
63     };
67 const Foam::NamedEnum<Foam::fieldValues::cellSource::sourceType, 2>
68     Foam::fieldValues::cellSource::sourceTypeNames_;
70 const Foam::NamedEnum<Foam::fieldValues::cellSource::operationType, 7>
71     Foam::fieldValues::cellSource::operationTypeNames_;
74 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
76 void Foam::fieldValues::cellSource::setCellZoneCells()
78     switch (source_)
79     {
80         case stCellZone:
81         {
82             label zoneId = mesh().cellZones().findZoneID(sourceName_);
84             if (zoneId < 0)
85             {
86                 FatalErrorIn("cellSource::cellSource::setCellZoneCells()")
87                     << "Unknown cell zone name: " << sourceName_
88                     << ". Valid cell zones are: " << mesh().cellZones().names()
89                     << nl << exit(FatalError);
90             }
92             cellId_ = mesh().cellZones()[zoneId];
93             nCells_ = returnReduce(cellId_.size(), sumOp<label>());
94             break;
95         }
97         case stAll:
98         {
99             cellId_ = identity(mesh().nCells());
100             nCells_ = returnReduce(cellId_.size(), sumOp<label>());
101             break;
102         }
104         default:
105         {
106             FatalErrorIn("cellSource::setCellZoneCells()")
107                << "Unknown source type. Valid source types are:"
108                 << sourceTypeNames_ << nl << exit(FatalError);
109         }
110     }
112     if (debug)
113     {
114         Pout<< "Selected source size = " << cellId_.size() << endl;
115     }
119 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
121 void Foam::fieldValues::cellSource::initialise(const dictionary& dict)
123     setCellZoneCells();
125     if (nCells_ == 0)
126     {
127         WarningIn
128         (
129             "Foam::fieldValues::cellSource::initialise(const dictionary&)"
130         )
131             << type() << " " << name_ << ": "
132             << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
133             << "    Source has no cells - deactivating" << endl;
135         active_ = false;
136         return;
137     }
139     Info<< type() << " " << name_ << ":"
140         << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
141         << "    total cells  = " << nCells_ << nl
142         << "    total volume = " << gSum(filterField(mesh().V()))
143         << nl << endl;
145     if (operation_ == opWeightedAverage)
146     {
147         dict.lookup("weightField") >> weightFieldName_;
148         if
149         (
150             obr().foundObject<volScalarField>(weightFieldName_)
151         )
152         {
153             Info<< "    weight field = " << weightFieldName_;
154         }
155         else
156         {
157             FatalErrorIn("cellSource::initialise()")
158                 << type() << " " << name_ << ": "
159                 << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
160                 << nl << "    Weight field " << weightFieldName_
161                 << " must be a " << volScalarField::typeName
162                 << nl << exit(FatalError);
163         }
164     }
166     Info<< nl << endl;
170 void Foam::fieldValues::cellSource::writeFileHeader()
172     if (outputFilePtr_.valid())
173     {
174         outputFilePtr_()
175             << "# Source : " << sourceTypeNames_[source_] << " "
176             << sourceName_ <<  nl << "# Cells  : " << nCells_ << nl
177             << "# Time" << tab << "sum(V)";
179         forAll(fields_, i)
180         {
181             outputFilePtr_()
182                 << tab << operationTypeNames_[operation_]
183                 << "(" << fields_[i] << ")";
184         }
186         outputFilePtr_() << endl;
187     }
191 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
193 Foam::fieldValues::cellSource::cellSource
195     const word& name,
196     const objectRegistry& obr,
197     const dictionary& dict,
198     const bool loadFromFiles
201     fieldValue(name, obr, dict, loadFromFiles),
202     source_(sourceTypeNames_.read(dict.lookup("source"))),
203     operation_(operationTypeNames_.read(dict.lookup("operation"))),
204     nCells_(0),
205     cellId_()
207     read(dict);
211 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
213 Foam::fieldValues::cellSource::~cellSource()
217 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
219 void Foam::fieldValues::cellSource::read(const dictionary& dict)
221     fieldValue::read(dict);
223     if (active_)
224     {
225         // no additional info to read
226         initialise(dict);
227     }
231 void Foam::fieldValues::cellSource::write()
233     fieldValue::write();
235     if (active_)
236     {
237         scalar totalVolume = gSum(filterField(mesh().V()));
238         if (Pstream::master())
239         {
240             outputFilePtr_() << obr_.time().value() << tab << totalVolume;
241         }
243         forAll(fields_, i)
244         {
245             writeValues<scalar>(fields_[i]);
246             writeValues<vector>(fields_[i]);
247             writeValues<sphericalTensor>(fields_[i]);
248             writeValues<symmTensor>(fields_[i]);
249             writeValues<tensor>(fields_[i]);
250         }
252         if (Pstream::master())
253         {
254             outputFilePtr_()<< endl;
255         }
257         if (log_)
258         {
259             Info<< endl;
260         }
261     }
265 // ************************************************************************* //