Forward compatibility: flex
[foam-extend-3.2.git] / src / postProcessing / functionObjects / field / fieldValues / cellSource / cellSource.C
blob32ab94e7a3d10a6716eaaa46b924fe5b9f5df30f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "cellSource.H"
27 #include "fvMesh.H"
28 #include "volFields.H"
29 #include "IOList.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 namespace Foam
35     namespace fieldValues
36     {
37         defineTypeNameAndDebug(cellSource, 0);
38     }
40     template<>
41     const char* NamedEnum<fieldValues::cellSource::sourceType, 1>::
42         names[] = {"cellZone"};
44     const NamedEnum<fieldValues::cellSource::sourceType, 1>
45         fieldValues::cellSource::sourceTypeNames_;
47     template<>
48     const char* NamedEnum<fieldValues::cellSource::operationType, 7>::
49         names[] =
50         {
51             "none", "sum", "volAverage",
52             "volIntegrate", "weightedAverage", "min", "max"
53         };
55     const NamedEnum<fieldValues::cellSource::operationType, 7>
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);
88     nCells_ = returnReduce(cellId_.size(), sumOp<label>());
90     if (debug)
91     {
92         Pout<< "Original cell zone size = " << cZone.size()
93             << ", new size = " << count << endl;
94     }
98 // * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //
100 void Foam::fieldValues::cellSource::initialise(const dictionary& dict)
102     switch (source_)
103     {
104         case stCellZone:
105         {
106             setCellZoneCells();
107             break;
108         }
109         default:
110         {
111             FatalErrorIn("cellSource::initialise()")
112                 << "Unknown source type. Valid source types are:"
113                 << sourceTypeNames_ << nl << exit(FatalError);
114         }
115     }
117     Info<< type() << " " << name_ << ":" << nl
118         << "    total cells  = " << nCells_ << nl
119         << "    total volume = " << gSum(filterField(mesh().V()))
120         << nl << endl;
122     if (operation_ == opWeightedAverage)
123     {
124         dict.lookup("weightField") >> weightFieldName_;
125         if
126         (
127             obr().foundObject<volScalarField>(weightFieldName_)
128         )
129         {
130             Info<< "    weight field = " << weightFieldName_;
131         }
132         else
133         {
134             FatalErrorIn("cellSource::initialise()")
135                 << type() << " " << name_ << ": "
136                 << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
137                 << nl << "    Weight field " << weightFieldName_
138                 << " must be a " << volScalarField::typeName
139                 << nl << exit(FatalError);
140         }
141     }
143     Info<< nl << endl;
147 void Foam::fieldValues::cellSource::writeFileHeader()
149     if (outputFilePtr_.valid())
150     {
151         outputFilePtr_()
152             << "# Source : " << sourceTypeNames_[source_] << " "
153             << sourceName_ <<  nl << "# Cells  : " << nCells_ << nl
154             << "# Time" << tab << "sum(V)";
156         forAll(fields_, i)
157         {
158             outputFilePtr_()
159                 << tab << operationTypeNames_[operation_]
160                 << "(" << fields_[i] << ")";
161         }
163         outputFilePtr_() << endl;
164     }
168 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
170 Foam::fieldValues::cellSource::cellSource
172     const word& name,
173     const objectRegistry& obr,
174     const dictionary& dict,
175     const bool loadFromFiles
178     fieldValue(name, obr, dict, loadFromFiles),
179     source_(sourceTypeNames_.read(dict.lookup("source"))),
180     operation_(operationTypeNames_.read(dict.lookup("operation"))),
181     nCells_(0),
182     cellId_()
184     read(dict);
188 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
190 Foam::fieldValues::cellSource::~cellSource()
194 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
196 void Foam::fieldValues::cellSource::read(const dictionary& dict)
198     fieldValue::read(dict);
200     if (active_)
201     {
202         // no additional info to read
203         initialise(dict);
204     }
208 void Foam::fieldValues::cellSource::write()
210     fieldValue::write();
212     if (active_)
213     {
214         if (Pstream::master())
215         {
216             outputFilePtr_()
217                 << obr_.time().value() << tab
218                 << sum(filterField(mesh().V()));
219         }
221         forAll(fields_, i)
222         {
223             writeValues<scalar>(fields_[i]);
224             writeValues<vector>(fields_[i]);
225             writeValues<sphericalTensor>(fields_[i]);
226             writeValues<symmTensor>(fields_[i]);
227             writeValues<tensor>(fields_[i]);
228         }
230         if (Pstream::master())
231         {
232             outputFilePtr_()<< endl;
233         }
235         if (log_)
236         {
237             Info<< endl;
238         }
239     }
243 // ************************************************************************* //