OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

fieldMinMax.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) 2008-2009 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 the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     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, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "fieldMinMax.H"
00028 #include "volFields.H"
00029 #include "dictionary.H"
00030 #include "Time.H"
00031 
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 namespace Foam
00036 {
00037     defineTypeNameAndDebug(fieldMinMax, 0);
00038 }
00039 
00040 
00041 template<>
00042 const char* Foam::NamedEnum<Foam::fieldMinMax::modeType, 2>::names[] =
00043 {
00044     "magnitude",
00045     "component"
00046 };
00047 
00048 
00049 const Foam::NamedEnum<Foam::fieldMinMax::modeType, 2>
00050 Foam::fieldMinMax::modeTypeNames_;
00051 
00052 
00053 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00054 
00055 Foam::fieldMinMax::fieldMinMax
00056 (
00057     const word& name,
00058     const objectRegistry& obr,
00059     const dictionary& dict,
00060     const bool loadFromFiles
00061 )
00062 :
00063     name_(name),
00064     obr_(obr),
00065     active_(true),
00066     log_(false),
00067     mode_(mdMag),
00068     fieldSet_(),
00069     fieldMinMaxFilePtr_(NULL)
00070 {
00071     // Check if the available mesh is an fvMesh otherise deactivate
00072     if (!isA<fvMesh>(obr_))
00073     {
00074         active_ = false;
00075         WarningIn
00076         (
00077             "fieldMinMax::fieldMinMax"
00078             "(const objectRegistry& obr, const dictionary& dict)"
00079         )   << "No fvMesh available, deactivating."
00080             << endl;
00081     }
00082 
00083     read(dict);
00084 }
00085 
00086 
00087 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00088 
00089 Foam::fieldMinMax::~fieldMinMax()
00090 {}
00091 
00092 
00093 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00094 
00095 void Foam::fieldMinMax::read(const dictionary& dict)
00096 {
00097     if (active_)
00098     {
00099         log_ = dict.lookupOrDefault<Switch>("log", false);
00100 
00101         mode_ = modeTypeNames_[dict.lookup("mode")];
00102         dict.lookup("fields") >> fieldSet_;
00103     }
00104 }
00105 
00106 
00107 void Foam::fieldMinMax::makeFile()
00108 {
00109     // Create the fieldMinMax file if not already created
00110     if (fieldMinMaxFilePtr_.empty())
00111     {
00112         if (debug)
00113         {
00114             Info<< "Creating fieldMinMax file." << endl;
00115         }
00116 
00117         // File update
00118         if (Pstream::master())
00119         {
00120             fileName fieldMinMaxDir;
00121             if (Pstream::parRun())
00122             {
00123                 // Put in undecomposed case (Note: gives problems for
00124                 // distributed data running)
00125                 fieldMinMaxDir =
00126                     obr_.time().path()/".."/name_/obr_.time().timeName();
00127             }
00128             else
00129             {
00130                 fieldMinMaxDir =
00131                     obr_.time().path()/name_/obr_.time().timeName();
00132             }
00133 
00134             // Create directory if does not exist.
00135             mkDir(fieldMinMaxDir);
00136 
00137             // Open new file at start up
00138             fieldMinMaxFilePtr_.reset
00139             (
00140                 new OFstream(fieldMinMaxDir/(type() + ".dat"))
00141             );
00142 
00143             // Add headers to output data
00144             writeFileHeader();
00145         }
00146     }
00147 }
00148 
00149 
00150 void Foam::fieldMinMax::writeFileHeader()
00151 {
00152     if (fieldMinMaxFilePtr_.valid())
00153     {
00154         fieldMinMaxFilePtr_()
00155             << "# Time" << tab << "field" << tab << "min" << tab << "max"
00156             << endl;
00157     }
00158 }
00159 
00160 
00161 void Foam::fieldMinMax::execute()
00162 {
00163     // Do nothing - only valid on write
00164 }
00165 
00166 
00167 void Foam::fieldMinMax::end()
00168 {
00169     // Do nothing - only valid on write
00170 }
00171 
00172 
00173 void Foam::fieldMinMax::write()
00174 {
00175     if (active_)
00176     {
00177         // Create the fieldMinMax file if not already created
00178         makeFile();
00179 
00180         forAll(fieldSet_, fieldI)
00181         {
00182             calcMinMaxFields<scalar>(fieldSet_[fieldI]);
00183             calcMinMaxFields<vector>(fieldSet_[fieldI]);
00184             calcMinMaxFields<sphericalTensor>(fieldSet_[fieldI]);
00185             calcMinMaxFields<symmTensor>(fieldSet_[fieldI]);
00186             calcMinMaxFields<tensor>(fieldSet_[fieldI]);
00187         }
00188     }
00189 }
00190 
00191 
00192 template<>
00193 void Foam::fieldMinMax::calcMinMaxFields<Foam::scalar>
00194 (
00195     const word& fieldName
00196 )
00197 {
00198     if (obr_.foundObject<volScalarField>(fieldName))
00199     {
00200         if (Pstream::master())
00201         {
00202             const volScalarField& field =
00203                 obr_.lookupObject<volScalarField>(fieldName);
00204             scalar minValue = min(field).value();
00205             scalar maxValue = max(field).value();
00206 
00207             fieldMinMaxFilePtr_() << obr_.time().value() << tab
00208                 << fieldName << tab << minValue << tab << maxValue << endl;
00209 
00210             if (log_)
00211             {
00212                 Info<< "fieldMinMax output:" << nl
00213                     << "    min(" << fieldName << ") = " << minValue << nl
00214                     << "    max(" << fieldName << ") = " << maxValue << nl
00215                     << endl;
00216             }
00217         }
00218     }
00219 }
00220 
00221 
00222 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd