FreeFOAM The Cross-Platform CFD Toolkit
fieldMinMax.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2008-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "fieldMinMax.H"
27 #include <finiteVolume/volFields.H>
28 #include <OpenFOAM/dictionary.H>
29 #include <OpenFOAM/Time.H>
30 
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(fieldMinMax, 0);
37 }
38 
39 
40 template<>
42 {
43  "magnitude",
44  "component"
45 };
46 
47 
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
55 (
56  const word& name,
57  const objectRegistry& obr,
58  const dictionary& dict,
59  const bool loadFromFiles
60 )
61 :
62  name_(name),
63  obr_(obr),
64  active_(true),
65  log_(false),
66  mode_(mdMag),
67  fieldSet_(),
68  fieldMinMaxFilePtr_(NULL)
69 {
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  }
81 
82  read(dict);
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
87 
89 {}
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
95 {
96  if (active_)
97  {
98  log_ = dict.lookupOrDefault<Switch>("log", false);
99 
100  mode_ = modeTypeNames_[dict.lookup("mode")];
101  dict.lookup("fields") >> fieldSet_;
102  }
103 }
104 
105 
107 {
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  }
115 
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  }
132 
133  // Create directory if does not exist.
134  mkDir(fieldMinMaxDir);
135 
136  // Open new file at start up
137  fieldMinMaxFilePtr_.reset
138  (
139  new OFstream(fieldMinMaxDir/(type() + ".dat"))
140  );
141 
142  // Add headers to output data
143  writeFileHeader();
144  }
145  }
146 }
147 
148 
150 {
151  if (fieldMinMaxFilePtr_.valid())
152  {
153  fieldMinMaxFilePtr_()
154  << "# Time" << tab << "field" << tab << "min" << tab << "max"
155  << endl;
156  }
157 }
158 
159 
161 {
162  // Do nothing - only valid on write
163 }
164 
165 
167 {
168  // Do nothing - only valid on write
169 }
170 
171 
173 {
174  if (active_)
175  {
176  // Create the fieldMinMax file if not already created
177  makeFile();
178 
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  }
188 }
189 
190 
191 template<>
192 void Foam::fieldMinMax::calcMinMaxFields<Foam::scalar>
193 (
194  const word& fieldName
195 )
196 {
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();
203 
204  if (Pstream::master())
205  {
206  fieldMinMaxFilePtr_() << obr_.time().value() << tab
207  << fieldName << tab << minValue << tab << maxValue << endl;
208 
209  if (log_)
210  {
211  Info<< "fieldMinMax output:" << nl
212  << " min(" << fieldName << ") = " << minValue << nl
213  << " max(" << fieldName << ") = " << maxValue << nl
214  << endl;
215  }
216  }
217  }
218 }
219 
220 
221 // ************************ vim: set sw=4 sts=4 et: ************************ //