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-2014 OpenFOAM Foundation
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 
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  return
51  enabled_
52  && time_.value() >= timeStart_
53  && time_.value() <= timeEnd_;
54 }
55 
56 
57 template<class OutputFilter>
59 {
60  if (dictName_.size())
61  {
62  ptr_.reset
63  (
64  new IOOutputFilter<OutputFilter>
65  (
66  name(),
67  time_.lookupObject<objectRegistry>(regionName_),
68  dictName_
69  )
70  );
71  }
72  else
73  {
74  ptr_.reset
75  (
76  new OutputFilter
77  (
78  name(),
79  time_.lookupObject<objectRegistry>(regionName_),
80  dict_
81  )
82  );
83  }
84 }
85 
86 
87 template<class OutputFilter>
89 {
90  ptr_.reset();
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
95 
96 template<class OutputFilter>
98 (
99  const word& name,
100  const Time& t,
101  const dictionary& dict
102 )
103 :
104  functionObject(name),
105  time_(t),
106  dict_(dict),
107  regionName_(polyMesh::defaultRegion),
108  dictName_(),
109  enabled_(true),
110  storeFilter_(true),
111  timeStart_(-VGREAT),
112  timeEnd_(VGREAT),
113  nStepsToStartTimeChange_
114  (
115  dict.lookupOrDefault("nStepsToStartTimeChange", 3)
116  ),
117  outputControl_(t, dict, "output"),
118  evaluateControl_(t, dict, "evaluate")
119 {
120  readDict();
121 }
122 
123 
124 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
125 
126 template<class OutputFilter>
128 {
129  enabled_ = true;
130 }
131 
132 
133 template<class OutputFilter>
135 {
136  enabled_ = false;
137 }
138 
139 
140 template<class OutputFilter>
142 {
143  readDict();
144 
145  if (enabled_ && storeFilter_)
146  {
147  allocateFilter();
148  }
149 
150  return true;
151 }
152 
153 
154 template<class OutputFilter>
156 (
157  const bool forceWrite
158 )
159 {
160  if (active())
161  {
162  if (!storeFilter_)
163  {
164  allocateFilter();
165  }
166 
167  if (evaluateControl_.output())
168  {
169  ptr_->execute();
170  }
171 
172  if (forceWrite || outputControl_.output())
173  {
174  ptr_->write();
175  }
176 
177  if (!storeFilter_)
178  {
179  destroyFilter();
180  }
181  }
182 
183  return true;
184 }
185 
186 
187 template<class OutputFilter>
189 {
190  if (enabled_)
191  {
192  if (!storeFilter_)
193  {
194  allocateFilter();
195  }
196 
197  ptr_->end();
198 
199  if (outputControl_.output())
200  {
201  ptr_->write();
202  }
203 
204  if (!storeFilter_)
205  {
206  destroyFilter();
207  }
208  }
209 
210  return true;
211 }
212 
213 
214 template<class OutputFilter>
216 {
217  if (active())
218  {
219  ptr_->timeSet();
220  }
221 
222  return true;
223 }
224 
225 
226 template<class OutputFilter>
228 {
229  if
230  (
231  active()
232  && outputControl_.outputControl()
233  == outputFilterOutputControl::ocAdjustableTime
234  )
235  {
236  const label outputTimeIndex = outputControl_.outputTimeLastDump();
237  const scalar writeInterval = outputControl_.writeInterval();
238 
239  scalar timeToNextWrite = max
240  (
241  0.0,
242  (outputTimeIndex + 1)*writeInterval
243  - (time_.value() - time_.startTime().value())
244  );
245 
246  scalar deltaT = time_.deltaTValue();
247 
248  scalar nSteps = timeToNextWrite/deltaT - SMALL;
249 
250  // function objects modify deltaT inside nStepsToStartTimeChange range
251  // NOTE: Potential problem if two function objects dump inside the same
252  // interval
253  if (nSteps < nStepsToStartTimeChange_)
254  {
255  label nStepsToNextWrite = label(nSteps) + 1;
256 
257  scalar newDeltaT = timeToNextWrite/nStepsToNextWrite;
258 
259  // Adjust time step
260  if (newDeltaT < deltaT)
261  {
262  deltaT = max(newDeltaT, 0.2*deltaT);
263  const_cast<Time&>(time_).setDeltaT(deltaT, false);
264  }
265  }
266  }
267 
268  return true;
269 }
270 
271 
272 template<class OutputFilter>
274 (
275  const dictionary& dict
276 )
277 {
278  if (dict != dict_)
279  {
280  dict_ = dict;
281  outputControl_.read(dict);
282 
283  return start();
284  }
285  else
286  {
287  return false;
288  }
289 }
290 
291 
292 template<class OutputFilter>
294 (
295  const mapPolyMesh& mpm
296 )
297 {
298  if (active() && mpm.mesh().name() == regionName_)
299  {
300  ptr_->updateMesh(mpm);
301  }
302 }
303 
304 
305 template<class OutputFilter>
307 (
308  const polyMesh& mesh
309 )
310 {
311  if (active() && mesh.name() == regionName_)
312  {
313  ptr_->movePoints(mesh);
314  }
315 }
316 
317 
318 // ************************************************************************* //
const polyMesh & mesh() const
Return polyMesh.
Definition: mapPolyMesh.H:359
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Abstract base-class for Time/database function objects.
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
A class for handling words, derived from string.
Definition: word.H:59
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
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
virtual bool execute(const bool forceWrite)
Called at each ++ or += of the time-loop.
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
dictionary dict
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
A functionObject wrapper around OutputFilter to allow them to be created via the functions entry with...
virtual bool timeSet()
Called when time was set at the end of the Time::operator++.
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
Macros for easy insertion into run-time selection tables.
virtual bool start()
Called at the start of the time-loop.
const word & name() const
Return name.
Definition: IOobject.H:260
virtual void on()
Switch the function object on.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
virtual void off()
Switch the function object off.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
virtual bool end()
Called when Time::run() determines that the time-loop exits.