FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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