timeControlFunctionObject.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) 2016 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 "polyMesh.H"
28 #include "mapPolyMesh.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
37  defineTypeNameAndDebug(timeControl, 0);
38 }
39 }
40 
41 
42 // * * * * * * * * * * * * * * * Private Members * * * * * * * * * * * * * * //
43 
44 void Foam::functionObjects::timeControl::readControls()
45 {
46  dict_.readIfPresent("timeStart", timeStart_);
47  dict_.readIfPresent("timeEnd", timeEnd_);
48  dict_.readIfPresent("nStepsToStartTimeChange", nStepsToStartTimeChange_);
49 }
50 
51 
52 bool Foam::functionObjects::timeControl::active() const
53 {
54  return
55  time_.value() >= timeStart_
56  && time_.value() <= timeEnd_;
57 }
58 
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
62 Foam::functionObjects::timeControl::timeControl
63 (
64  const word& name,
65  const Time& t,
66  const dictionary& dict
67 )
68 :
69  functionObject(name),
70  time_(t),
71  dict_(dict),
72  timeStart_(-VGREAT),
73  timeEnd_(VGREAT),
74  nStepsToStartTimeChange_
75  (
76  dict.lookupOrDefault("nStepsToStartTimeChange", 3)
77  ),
78  executeControl_(t, dict, "execute"),
79  writeControl_(t, dict, "write"),
80  foPtr_(functionObject::New(name, t, dict_))
81 {
82  readControls();
83 }
84 
85 
86 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
87 
89 {
90  if (active() && (postProcess || executeControl_.execute()))
91  {
92  foPtr_->execute();
93  }
94 
95  return true;
96 }
97 
98 
100 {
101  if (active() && (postProcess || writeControl_.execute()))
102  {
103  foPtr_->write();
104  }
105 
106  return true;
107 }
108 
109 
111 {
112  if (active() && (executeControl_.execute() || writeControl_.execute()))
113  {
114  foPtr_->end();
115  }
116 
117  return true;
118 }
119 
120 
121 
123 {
124  if
125  (
126  active()
127  && writeControl_.control()
129  )
130  {
131  const label writeTimeIndex = writeControl_.executionIndex();
132  const scalar writeInterval = writeControl_.interval();
133 
134  scalar timeToNextWrite = max
135  (
136  0.0,
137  (writeTimeIndex + 1)*writeInterval
138  - (time_.value() - time_.startTime().value())
139  );
140 
141  scalar deltaT = time_.deltaTValue();
142 
143  scalar nSteps = timeToNextWrite/deltaT - SMALL;
144 
145  // functionObjects modify deltaT within nStepsToStartTimeChange
146  // NOTE: Potential problems arise if two function objects dump within
147  // the same interval
148  if (nSteps < nStepsToStartTimeChange_)
149  {
150  label nStepsToNextWrite = label(nSteps) + 1;
151 
152  scalar newDeltaT = timeToNextWrite/nStepsToNextWrite;
153 
154  // Adjust time step
155  if (newDeltaT < deltaT)
156  {
157  deltaT = max(newDeltaT, 0.2*deltaT);
158  const_cast<Time&>(time_).setDeltaT(deltaT, false);
159  }
160  }
161  }
162 
163  return true;
164 }
165 
166 
168 (
169  const dictionary& dict
170 )
171 {
172  if (dict != dict_)
173  {
174  dict_ = dict;
175 
176  writeControl_.read(dict);
177  executeControl_.read(dict);
178  readControls();
179 
180  return true;
181  }
182  else
183  {
184  return false;
185  }
186 }
187 
188 
190 (
191  const mapPolyMesh& mpm
192 )
193 {
194  if (active())
195  {
196  foPtr_->updateMesh(mpm);
197  }
198 }
199 
200 
202 (
203  const polyMesh& mesh
204 )
205 {
206  if (active())
207  {
208  foPtr_->movePoints(mesh);
209  }
210 }
211 
212 
213 // ************************************************************************* //
dictionary dict
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
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
virtual bool execute()
Called at each ++ or += of the time-loop.
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Macros for easy insertion into run-time selection tables.
virtual bool write()
Called at each ++ or += of the time-loop.
A class for handling words, derived from string.
Definition: word.H:59
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh.
virtual void movePoints(const polyMesh &mesh)
Update for changes of mesh.
static autoPtr< functionObject > New(const word &name, const Time &, const dictionary &)
Select from dictionary, based on its "type" entry.
Adjust time step for execution.
Definition: timeControl.H:59
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
virtual bool read(const dictionary &)
Read and set the function object if its data have changed.
virtual bool end()
Called when Time::run() determines that the time-loop exits.
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Namespace for OpenFOAM.