fieldAverage.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) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 "fieldAverage.H"
27 #include "volFields.H"
28 #include "Time.H"
29 
30 #include "fieldAverageItem.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(fieldAverage, 0);
37 }
38 
39 
40 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
41 
43 {
44  forAll(faItems_, i)
45  {
46  if (faItems_[i].mean())
47  {
48  if (obr_.found(faItems_[i].meanFieldName()))
49  {
50  obr_.checkOut(*obr_[faItems_[i].meanFieldName()]);
51  }
52  }
53 
54  if (faItems_[i].prime2Mean())
55  {
56  if (obr_.found(faItems_[i].prime2MeanFieldName()))
57  {
58  obr_.checkOut(*obr_[faItems_[i].prime2MeanFieldName()]);
59  }
60  }
61  }
62 }
63 
64 
66 {
67  resetFields();
68 
69  if (log_) Info << type() << " " << name_ << ":" << nl;
70 
71 
72  // Add mean fields to the field lists
73  forAll(faItems_, fieldI)
74  {
75  addMeanField<scalar>(fieldI);
76  addMeanField<vector>(fieldI);
77  addMeanField<sphericalTensor>(fieldI);
78  addMeanField<symmTensor>(fieldI);
79  addMeanField<tensor>(fieldI);
80  }
81 
82  // Add prime-squared mean fields to the field lists
83  forAll(faItems_, fieldI)
84  {
85  addPrime2MeanField<scalar, scalar>(fieldI);
86  addPrime2MeanField<vector, symmTensor>(fieldI);
87  }
88 
89  forAll(faItems_, fieldI)
90  {
91  if (!faItems_[fieldI].active())
92  {
94  << "Field " << faItems_[fieldI].fieldName()
95  << " not found in database for averaging";
96  }
97  }
98 
99  // ensure first averaging works unconditionally
100  prevTimeIndex_ = -1;
101 
102  if (log_) Info << endl;
103 
104  initialised_ = true;
105 }
106 
107 
109 {
110  if (!initialised_)
111  {
112  initialize();
113  }
114 
115  const label currentTimeIndex =
116  static_cast<const fvMesh&>(obr_).time().timeIndex();
117 
118  if (prevTimeIndex_ == currentTimeIndex)
119  {
120  return;
121  }
122  else
123  {
124  prevTimeIndex_ = currentTimeIndex;
125  }
126 
127  if (log_) Info
128  << type() << " " << name_ << " output:" << nl
129  << " Calculating averages" << nl;
130 
131  addMeanSqrToPrime2Mean<scalar, scalar>();
132  addMeanSqrToPrime2Mean<vector, symmTensor>();
133 
134  calculateMeanFields<scalar>();
135  calculateMeanFields<vector>();
136  calculateMeanFields<sphericalTensor>();
137  calculateMeanFields<symmTensor>();
138  calculateMeanFields<tensor>();
139 
140  calculatePrime2MeanFields<scalar, scalar>();
141  calculatePrime2MeanFields<vector, symmTensor>();
142 
143  forAll(faItems_, fieldI)
144  {
145  totalIter_[fieldI]++;
146  totalTime_[fieldI] += obr_.time().deltaTValue();
147  }
148 }
149 
150 
152 {
153  if (log_) Info << " Writing average fields" << endl;
154 
155  writeFields<scalar>();
156  writeFields<vector>();
157  writeFields<sphericalTensor>();
158  writeFields<symmTensor>();
159  writeFields<tensor>();
160 }
161 
162 
164 {
165  forAll(faItems_, fieldI)
166  {
167  const word& fieldName = faItems_[fieldI].fieldName();
168 
170  propsDict.add("totalIter", totalIter_[fieldI]);
171  propsDict.add("totalTime", totalTime_[fieldI]);
172  setProperty(fieldName, propsDict);
173  }
174 }
175 
176 
178 {
179  totalIter_.clear();
180  totalIter_.setSize(faItems_.size(), 1);
181 
182  totalTime_.clear();
183  totalTime_.setSize(faItems_.size(), obr_.time().deltaTValue());
184 
185  if (log_ && (resetOnRestart_ || resetOnOutput_))
186  {
187  Info<< " Starting averaging at time " << obr_.time().timeName()
188  << nl;
189  }
190  else
191  {
192  if (log_) Info << " Restarting averaging for fields:" << nl;
193 
194  forAll(faItems_, fieldI)
195  {
196  const word& fieldName = faItems_[fieldI].fieldName();
197  if (foundProperty(fieldName))
198  {
199  dictionary fieldDict;
200  getProperty(fieldName, fieldDict);
201 
202  totalIter_[fieldI] = readLabel(fieldDict.lookup("totalIter"));
203  totalTime_[fieldI] = readScalar(fieldDict.lookup("totalTime"));
204 
205  if (log_) Info
206  << " " << fieldName
207  << " iters = " << totalIter_[fieldI]
208  << " time = " << totalTime_[fieldI] << nl;
209  }
210  else
211  {
212  if (log_) Info
213  << " " << fieldName
214  << ": starting averaging at time "
215  << obr_.time().timeName() << endl;
216  }
217  }
218  }
219 }
220 
221 
222 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
223 
225 (
226  const word& name,
227  const objectRegistry& obr,
228  const dictionary& dict,
229  const bool loadFromFiles
230 )
231 :
233  obr_(obr),
234  prevTimeIndex_(-1),
235  resetOnRestart_(false),
236  resetOnOutput_(false),
237  log_(true),
238  initialised_(false),
239  faItems_(),
240  totalIter_(),
241  totalTime_()
242 {
243  // Only active if a fvMesh is available
244  if (setActive<fvMesh>())
245  {
246  read(dict);
247  }
248 }
249 
250 
251 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
252 
254 {}
255 
256 
257 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
258 
260 {
261  if (active_)
262  {
263  initialised_ = false;
264 
265  log_.readIfPresent("log", dict);
266 
267  if (log_) Info << type() << " " << name_ << ":" << nl;
268 
269  dict.readIfPresent("resetOnRestart", resetOnRestart_);
270  dict.readIfPresent("resetOnOutput", resetOnOutput_);
271  dict.lookup("fields") >> faItems_;
272 
273  readAveragingProperties();
274 
275  if (log_) Info << endl;
276  }
277 }
278 
279 
281 {
282  if (active_)
283  {
284  calcAverages();
285  if (log_) Info << endl;
286  }
287 }
288 
289 
291 {}
292 
293 
295 {}
296 
297 
299 {
300  if (active_)
301  {
302  writeAverages();
303  writeAveragingProperties();
304 
305  if (resetOnOutput_)
306  {
307  if (log_) Info
308  << " Restarting averaging at time " << obr_.time().timeName()
309  << nl << endl;
310 
311  totalIter_.clear();
312  totalIter_.setSize(faItems_.size(), 1);
313 
314  totalTime_.clear();
315  totalTime_.setSize(faItems_.size(), obr_.time().deltaTValue());
316 
317  initialize();
318  }
319 
320  if (log_) Info << endl;
321  }
322 }
323 
324 
326 {
327  // Do nothing
328 }
329 
330 
332 {
333  // Do nothing
334 }
335 
336 
337 // ************************************************************************* //
Foam::fieldAverage::writeAverages
virtual void writeAverages() const
Write averages.
Definition: fieldAverage.C:151
volFields.H
Foam::fieldAverage::movePoints
virtual void movePoints(const polyMesh &)
Move points.
Definition: fieldAverage.C:331
Foam::fieldAverage::faItems_
List< fieldAverageItem > faItems_
List of field average items, describing what averages to be.
Definition: fieldAverage.H:200
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::functionObjectState
Base class for function objects, adding functionality to read/write state information (data required ...
Definition: functionObjectState.H:54
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::dictionary::readIfPresent
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
Definition: dictionaryTemplates.C:94
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::fieldAverage::writeAveragingProperties
void writeAveragingProperties()
Write averaging properties - steps and time.
Definition: fieldAverage.C:163
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::fieldAverage::timeSet
virtual void timeSet()
Called when time was set at the end of the Time::operator++.
Definition: fieldAverage.C:294
Foam::fieldAverage::readAveragingProperties
void readAveragingProperties()
Read averaging properties - steps and time.
Definition: fieldAverage.C:177
Foam::fieldAverage::updateMesh
virtual void updateMesh(const mapPolyMesh &)
Update mesh.
Definition: fieldAverage.C:325
fieldAverage.H
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::fieldAverage::fieldAverage
fieldAverage(const fieldAverage &)
Disallow default bitwise copy construct.
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::fieldAverage::end
virtual void end()
Execute the averaging at the final time-loop, currently does nothing.
Definition: fieldAverage.C:290
Foam::fieldAverage::execute
virtual void execute()
Execute the averaging.
Definition: fieldAverage.C:280
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
fieldAverageItem.H
propsDict
IOdictionary propsDict(IOobject("particleTrackProperties", runTime.constant(), mesh, IOobject::MUST_READ_IF_MODIFIED))
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::fieldAverage::calcAverages
virtual void calcAverages()
Main calculation routine.
Definition: fieldAverage.C:108
Foam::HashTable::found
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:109
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::fieldAverage::obr_
const objectRegistry & obr_
Reference to the database.
Definition: fieldAverage.H:181
Foam::fieldAverage::read
virtual void read(const dictionary &)
Read the field average data.
Definition: fieldAverage.C:259
Foam::readScalar
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
Foam::objectRegistry::checkOut
bool checkOut(regIOobject &) const
Remove an regIOobject from registry.
Definition: objectRegistry.C:221
Foam::fieldAverage::~fieldAverage
virtual ~fieldAverage()
Destructor.
Definition: fieldAverage.C:253
Foam::readLabel
label readLabel(Istream &is)
Definition: label.H:64
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::fieldAverage::write
virtual void write()
Calculate the field average data and write.
Definition: fieldAverage.C:298
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::fieldAverage::initialize
void initialize()
Reset lists (clear existing values) and initialize averaging.
Definition: fieldAverage.C:65
Foam::fieldAverage::resetFields
void resetFields()
Checkout fields (causes deletion) from the database.
Definition: fieldAverage.C:42
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729