Forward compatibility: flex
[foam-extend-3.2.git] / src / postProcessing / functionObjects / field / fieldMinMax / fieldMinMax.C
blob4cce3de6f42c519a7809a1476f74faed159964d1
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 "fieldMinMax.H"
27 #include "volFields.H"
28 #include "dictionary.H"
29 #include "foamTime.H"
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 namespace Foam
36     defineTypeNameAndDebug(fieldMinMax, 0);
40 template<>
41 const char* Foam::NamedEnum<Foam::fieldMinMax::modeType, 2>::names[] =
43     "magnitude",
44     "component"
48 const Foam::NamedEnum<Foam::fieldMinMax::modeType, 2>
49 Foam::fieldMinMax::modeTypeNames_;
52 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
54 Foam::fieldMinMax::fieldMinMax
56     const word& name,
57     const objectRegistry& obr,
58     const dictionary& dict,
59     const bool loadFromFiles
62     name_(name),
63     obr_(obr),
64     active_(true),
65     log_(false),
66     mode_(mdMag),
67     fieldSet_(),
68     fieldMinMaxFilePtr_(NULL)
70     // Check if the available mesh is an fvMesh otherise deactivate
71     if (!isA<fvMesh>(obr_))
72     {
73         active_ = false;
74         WarningIn
75         (
76             "fieldMinMax::fieldMinMax"
77             "(const objectRegistry& obr, const dictionary& dict)"
78         )   << "No fvMesh available, deactivating."
79             << endl;
80     }
82     read(dict);
86 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
88 Foam::fieldMinMax::~fieldMinMax()
92 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
94 void Foam::fieldMinMax::read(const dictionary& dict)
96     if (active_)
97     {
98         log_ = dict.lookupOrDefault<Switch>("log", false);
100         mode_ = modeTypeNames_[dict.lookup("mode")];
101         dict.lookup("fields") >> fieldSet_;
102     }
106 void Foam::fieldMinMax::makeFile()
108     // Create the fieldMinMax file if not already created
109     if (fieldMinMaxFilePtr_.empty())
110     {
111         if (debug)
112         {
113             Info<< "Creating fieldMinMax file." << endl;
114         }
116         // File update
117         if (Pstream::master())
118         {
119             fileName fieldMinMaxDir;
120             if (Pstream::parRun())
121             {
122                 // Put in undecomposed case (Note: gives problems for
123                 // distributed data running)
124                 fieldMinMaxDir =
125                     obr_.time().path()/".."/name_/obr_.time().timeName();
126             }
127             else
128             {
129                 fieldMinMaxDir =
130                     obr_.time().path()/name_/obr_.time().timeName();
131             }
133             // Create directory if does not exist.
134             mkDir(fieldMinMaxDir);
136             // Open new file at start up
137             fieldMinMaxFilePtr_.reset
138             (
139                 new OFstream(fieldMinMaxDir/(type() + ".dat"))
140             );
142             // Add headers to output data
143             writeFileHeader();
144         }
145     }
149 void Foam::fieldMinMax::writeFileHeader()
151     if (fieldMinMaxFilePtr_.valid())
152     {
153         fieldMinMaxFilePtr_()
154             << "# Time" << tab << "field" << tab << "min" << tab << "max"
155             << endl;
156     }
160 void Foam::fieldMinMax::execute()
162     // Do nothing - only valid on write
166 void Foam::fieldMinMax::end()
168     // Do nothing - only valid on write
172 void Foam::fieldMinMax::write()
174     if (active_)
175     {
176         // Create the fieldMinMax file if not already created
177         makeFile();
179         forAll(fieldSet_, fieldI)
180         {
181             calcMinMaxFields<scalar>(fieldSet_[fieldI]);
182             calcMinMaxFields<vector>(fieldSet_[fieldI]);
183             calcMinMaxFields<sphericalTensor>(fieldSet_[fieldI]);
184             calcMinMaxFields<symmTensor>(fieldSet_[fieldI]);
185             calcMinMaxFields<tensor>(fieldSet_[fieldI]);
186         }
187     }
191 template<>
192 void Foam::fieldMinMax::calcMinMaxFields<Foam::scalar>
194     const word& fieldName
197     if (obr_.foundObject<volScalarField>(fieldName))
198     {
199         const volScalarField& field =
200             obr_.lookupObject<volScalarField>(fieldName);
201         scalar minValue = min(field).value();
202         scalar maxValue = max(field).value();
204         if (Pstream::master())
205         {
206             fieldMinMaxFilePtr_() << obr_.time().value() << tab
207                 << fieldName << tab << minValue << tab << maxValue << endl;
209             if (log_)
210             {
211                 Info<< "fieldMinMax output:" << nl
212                     << "    min(" << fieldName << ") = " << minValue << nl
213                     << "    max(" << fieldName << ") = " << maxValue << nl
214                     << endl;
215             }
216         }
217     }
221 // ************************************************************************* //