OutputFilterFunctionObject.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 
27 #include "IOOutputFilter.H"
28 #include "polyMesh.H"
29 #include "mapPolyMesh.H"
31 
32 // * * * * * * * * * * * * * * * Private Members * * * * * * * * * * * * * * //
33 
34 template<class OutputFilter>
36 {
37  dict_.readIfPresent("region", regionName_);
38  dict_.readIfPresent("dictionary", dictName_);
39  dict_.readIfPresent("enabled", enabled_);
40  dict_.readIfPresent("storeFilter", storeFilter_);
41  dict_.readIfPresent("timeStart", timeStart_);
42  dict_.readIfPresent("timeEnd", timeEnd_);
43  dict_.readIfPresent("nStepsToStartTimeChange", nStepsToStartTimeChange_);
44 }
45 
46 
47 template<class OutputFilter>
49 {
50  // The logic here mimics that of Time itself and uses 0.5*deltaT
51  // as the tolerance to account for numerical precision errors
52  // (see e.g. Time::run()) since the current time might be e.g.
53  // 0.3000000000001 instead of exactly 0.3.
54  return
55  enabled_
56  && time_.value() >= (timeStart_ - 0.5*time_.deltaTValue())
57  && time_.value() <= (timeEnd_ + 0.5*time_.deltaTValue());
58 }
59 
60 
61 template<class OutputFilter>
63 {
64  if (dictName_.size())
65  {
66  ptr_.reset
67  (
69  (
70  name(),
71  time_.lookupObject<objectRegistry>(regionName_),
72  dictName_
73  )
74  );
75  }
76  else
77  {
78  ptr_.reset
79  (
80  new OutputFilter
81  (
82  name(),
83  time_.lookupObject<objectRegistry>(regionName_),
84  dict_
85  )
86  );
87  }
88 }
89 
90 
91 template<class OutputFilter>
93 {
94  ptr_.reset();
95 }
96 
97 
98 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
99 
100 template<class OutputFilter>
102 (
103  const word& name,
104  const Time& t,
105  const dictionary& dict
106 )
107 :
109  time_(t),
110  dict_(dict),
111  regionName_(polyMesh::defaultRegion),
112  dictName_(),
113  enabled_(true),
114  storeFilter_(true),
115  timeStart_(-VGREAT),
116  timeEnd_(VGREAT),
117  nStepsToStartTimeChange_
118  (
119  dict.lookupOrDefault("nStepsToStartTimeChange", 3)
120  ),
121  outputControl_(t, dict, "output"),
122  evaluateControl_(t, dict, "evaluate")
123 {
124  readDict();
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
130 template<class OutputFilter>
132 {
133  enabled_ = true;
134 }
135 
136 
137 template<class OutputFilter>
139 {
140  enabled_ = false;
141 }
142 
143 
144 template<class OutputFilter>
146 {
147  readDict();
148 
149  if (enabled_ && storeFilter_)
150  {
151  allocateFilter();
152  }
153 
154  return true;
155 }
156 
157 
158 template<class OutputFilter>
160 (
161  const bool forceWrite
162 )
163 {
164  if (active())
165  {
166  if (!storeFilter_)
167  {
168  allocateFilter();
169  }
170 
171  if (evaluateControl_.output())
172  {
173  ptr_->execute();
174  }
175 
176  if (forceWrite || outputControl_.output())
177  {
178  ptr_->write();
179  }
180 
181  if (!storeFilter_)
182  {
183  destroyFilter();
184  }
185  }
186  else if (enabled_ && time_.value() > timeEnd_)
187  {
188  // End early if the time is controlled by the user timeEnd entry
189  end();
190 
191  enabled_ = false;
192  }
193 
194  return true;
195 }
196 
197 
198 template<class OutputFilter>
200 {
201  if (enabled_)
202  {
203  if (!storeFilter_)
204  {
205  allocateFilter();
206  }
207 
208  ptr_->end();
209 
210  if (!storeFilter_)
211  {
212  destroyFilter();
213  }
214  }
215 
216  return true;
217 }
218 
219 
220 template<class OutputFilter>
222 {
223  if (active())
224  {
225  ptr_->timeSet();
226  }
227 
228  return true;
229 }
230 
231 
232 template<class OutputFilter>
234 {
235  if
236  (
237  active()
238  && outputControl_.outputControl()
239  == outputFilterOutputControl::ocAdjustableTime
240  )
241  {
242  const label outputTimeIndex = outputControl_.outputTimeLastDump();
243  const scalar writeInterval = outputControl_.writeInterval();
244 
245  // Logic mimics that of Time::adjustDeltaT() except we only allow
246  // making the time step lower.
247 
248  scalar timeToNextWrite = max
249  (
250  0.0,
251  (outputTimeIndex + 1)*writeInterval
252  - (time_.value() - time_.startTime().value())
253  );
254 
255  scalar deltaT = time_.deltaTValue();
256 
257  scalar nSteps = timeToNextWrite/deltaT - SMALL;
258 
259  // function objects modify deltaT inside nStepsToStartTimeChange range
260  // NOTE: Potential problem if two function objects dump inside the same
261  // interval
262  if (nSteps < nStepsToStartTimeChange_)
263  {
264  label nStepsToNextWrite = label(nSteps) + 1;
265 
266  scalar newDeltaT = timeToNextWrite/nStepsToNextWrite;
267 
268  // Adjust time step
269  if (newDeltaT < deltaT)
270  {
271  deltaT = max(newDeltaT, 0.2*deltaT);
272  const_cast<Time&>(time_).setDeltaT(deltaT, false);
273  }
274  }
275  }
276 
277  return true;
278 }
279 
280 
281 template<class OutputFilter>
283 (
284  const dictionary& dict
285 )
286 {
287  if (dict != dict_)
288  {
289  dict_ = dict;
290  outputControl_.read(dict);
291 
292  return start();
293  }
294  else
295  {
296  return false;
297  }
298 }
299 
300 
301 template<class OutputFilter>
303 (
304  const mapPolyMesh& mpm
305 )
306 {
307  if (active() && mpm.mesh().name() == regionName_)
308  {
309  ptr_->updateMesh(mpm);
310  }
311 }
312 
313 
314 template<class OutputFilter>
316 (
317  const polyMesh& mesh
318 )
319 {
320  if (active() && mesh.name() == regionName_)
321  {
322  ptr_->movePoints(mesh);
323  }
324 }
325 
326 
327 // ************************************************************************* //
OutputFilterFunctionObject.H
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::OutputFilterFunctionObject::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
Definition: OutputFilterFunctionObject.C:303
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::OutputFilterFunctionObject::execute
virtual bool execute(const bool forceWrite)
Called at each ++ or += of the time-loop.
Definition: OutputFilterFunctionObject.C:160
mapPolyMesh.H
IOOutputFilter.H
polyMesh.H
Foam::OutputFilterFunctionObject::readDict
void readDict()
Read relevant dictionary entries.
Definition: OutputFilterFunctionObject.C:35
Foam::OutputFilterFunctionObject::start
virtual bool start()
Called at the start of the time-loop.
Definition: OutputFilterFunctionObject.C:145
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:58
Foam::OutputFilterFunctionObject::timeSet
virtual bool timeSet()
Called when time was set at the end of the Time::operator++.
Definition: OutputFilterFunctionObject.C:221
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::OutputFilterFunctionObject::end
virtual bool end()
Called when Time::run() determines that the time-loop exits.
Definition: OutputFilterFunctionObject.C:199
Foam::IOOutputFilter
IOdictionary wrapper around OutputFilter to allow them to read from their associated dictionaries.
Definition: IOOutputFilter.H:59
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobject.H:273
Foam::OutputFilterFunctionObject::off
virtual void off()
Switch the function object off.
Definition: OutputFilterFunctionObject.C:138
Foam::OutputFilterFunctionObject::destroyFilter
void destroyFilter()
Destroys most of the data associated with this object.
Definition: OutputFilterFunctionObject.C:92
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
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::OutputFilterFunctionObject::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
Definition: OutputFilterFunctionObject.C:316
Foam::OutputFilterFunctionObject::active
bool active() const
Returns true if active (enabled and within time bounds)
Definition: OutputFilterFunctionObject.C:48
Foam::OutputFilterFunctionObject::adjustTimeStep
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Definition: OutputFilterFunctionObject.C:233
Foam::max
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::OutputFilterFunctionObject::read
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
Definition: OutputFilterFunctionObject.C:283
Foam::OutputFilterFunctionObject::OutputFilterFunctionObject
OutputFilterFunctionObject(const OutputFilterFunctionObject &)
Disallow default bitwise copy construct.
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Foam::mapPolyMesh::mesh
const polyMesh & mesh() const
Return polyMesh.
Definition: mapPolyMesh.H:359
Foam::OutputFilterFunctionObject::on
virtual void on()
Switch the function object on.
Definition: OutputFilterFunctionObject.C:131
Foam::OutputFilterFunctionObject::allocateFilter
void allocateFilter()
Creates most of the data associated with this object.
Definition: OutputFilterFunctionObject.C:62
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47