runTimeControl.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "runTimeControl.H"
30 #include "dictionary.H"
31 #include "runTimeCondition.H"
32 #include "fvMesh.H"
33 #include "Time.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace functionObjects
41 {
42 namespace runTimeControls
43 {
45  addToRunTimeSelectionTable(functionObject, runTimeControl, dictionary);
46 }
47 }
48 }
49 
51 <
53 >
55 {
56  { satisfiedAction::END, "end"},
57  { satisfiedAction::SET_TRIGGER, "setTrigger"},
58 };
59 
60 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
61 
62 Foam::functionObjects::runTimeControls::runTimeControl::runTimeControl
63 (
64  const word& name,
65  const Time& runTime,
66  const dictionary& dict
67 )
68 :
70  conditions_(),
71  groupMap_(),
72  nWriteStep_(0),
73  writeStepI_(0),
74  satisfiedAction_(satisfiedAction::END),
75  triggerIndex_(labelMin),
76  active_(getObjectProperty(name, "active", true))
77 {
78  read(dict);
79 }
80 
81 
82 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
83 
85 (
86  const dictionary& dict
87 )
88 {
89  if (functionObject::postProcess)
90  {
91  Info<< "Deactivated " << name()
92  << " function object for post-processing"
93  << endl;
94 
95  return false;
96  }
97 
98 
100  {
101  const dictionary& conditionsDict = dict.subDict("conditions");
102  const wordList conditionNames(conditionsDict.toc());
103  conditions_.setSize(conditionNames.size());
104 
105  label uniqueGroupi = 0;
106  forAll(conditionNames, conditioni)
107  {
108  const word& conditionName = conditionNames[conditioni];
109  const dictionary& dict = conditionsDict.subDict(conditionName);
110 
111  conditions_.set
112  (
113  conditioni,
114  runTimeCondition::New(conditionName, obr_, dict, *this)
115  );
116 
117  label groupi = conditions_[conditioni].groupID();
118 
119  if (groupMap_.insert(groupi, uniqueGroupi))
120  {
121  ++uniqueGroupi;
122  }
123  }
124 
125  dict.readIfPresent("nWriteStep", nWriteStep_);
126 
127  // Check that some conditions are set
128  if (conditions_.empty())
129  {
130  Info<< type() << " " << name() << " output:" << nl
131  << " No conditions present" << nl
132  << endl;
133  }
134  else
135  {
136  // Check that at least one condition is active
137  bool check = false;
138  for (const auto& condition : conditions_)
139  {
140  if (condition.active())
141  {
142  check = true;
143  break;
144  }
145  }
146 
147  if (!check)
148  {
149  Info<< type() << " " << name() << " output:" << nl
150  << " All conditions are inactive" << nl
151  << endl;
152  }
153  }
154 
155  // Set the action to perform when all conditions are satisfied
156  // - set to end for backwards compatibility with v1806
157  satisfiedAction_ =
158  satisfiedActionNames.getOrDefault
159  (
160  "satisfiedAction",
161  dict,
162  satisfiedAction::END
163  );
164 
165  if (satisfiedAction_ == satisfiedAction::SET_TRIGGER)
166  {
167  triggerIndex_ = dict.get<label>("trigger");
168  }
169 
170  return true;
171  }
172 
173  return false;
174 }
175 
176 
178 {
179  if (!active_)
180  {
181  return true;
182  }
183 
184  Info<< type() << " " << name() << " output:" << nl;
185 
186  // IDs of satisfied conditions
187  DynamicList<label> IDs(conditions_.size());
188 
189  // Run stops only if all conditions within a group are satisfied
190  List<bool> groupSatisfied(groupMap_.size(), true);
191  List<bool> groupActive(groupMap_.size(), false);
192 
193  forAll(conditions_, conditioni)
194  {
195  runTimeCondition& condition = conditions_[conditioni];
196 
197  if (condition.active())
198  {
199  bool conditionSatisfied = condition.apply();
200 
201  label groupi = condition.groupID();
202 
203  auto conditionIter = groupMap_.cfind(groupi);
204 
205  if (!conditionIter.found())
206  {
208  << "group " << groupi << " not found in map"
209  << abort(FatalError);
210  }
211 
212  if (conditionSatisfied)
213  {
214  IDs.append(conditioni);
215 
216  groupActive[conditionIter()] = true;
217 
218  if (groupi == -1)
219  {
220  // Condition not part of a group - only requires this to be
221  // satisfied for completion flag to be set
222  groupSatisfied[conditionIter()] = true;
223  break;
224  }
225  }
226  else
227  {
228  groupSatisfied[conditionIter()] = false;
229  }
230  }
231  }
232 
233  bool done = false;
234  forAll(groupSatisfied, groupi)
235  {
236  if (groupSatisfied[groupi] && groupActive[groupi])
237  {
238  done = true;
239  break;
240  }
241  }
242 
243  if (done)
244  {
245  for (label conditioni : IDs)
246  {
247  Info<< " " << conditions_[conditioni].type() << ": "
248  << conditions_[conditioni].name()
249  << " condition satisfied" << nl;
250  }
251 
252  switch (satisfiedAction_)
253  {
255  {
256  // Set to write a data dump or finalise the calculation
257  Time& time = const_cast<Time&>(time_);
258 
259  if (writeStepI_ < nWriteStep_ - 1)
260  {
261  ++writeStepI_;
262  Info<< " Writing fields - step " << writeStepI_ << nl;
263  time.writeNow();
264  }
265  else
266  {
267  Info<< " Stopping calculation" << nl
268  << " Writing fields";
269 
270  if (nWriteStep_ != 0)
271  {
272  Info<< " - final step" << nl;
273  }
274  else
275  {
276  Info<< nl;
277  }
278 
279  Info<< endl;
280  active_ = false;
281 
282  // Write any registered objects and set the end-time
283  time.writeAndEnd();
284 
285  // Trigger any function objects
286  time.run();
287  }
288  break;
289  }
291  {
292  Info<< " Setting trigger " << triggerIndex_ << nl;
293  setTrigger(triggerIndex_);
294 
295  // Deactivate the model
296  active_ = false;
297  setProperty("active", active_);
298  break;
299  }
300  }
301  }
302  else
303  {
304  Info<< " Conditions not met" << nl;
305  }
306 
307  Info<< endl;
308 
309  return true;
310 }
311 
312 
314 {
315  for (auto& condition : conditions_)
316  {
317  condition.write();
318  }
319 
320  return true;
321 }
322 
323 
324 // ************************************************************************* //
Foam::functionObjects::runTimeControls::defineTypeNameAndDebug
defineTypeNameAndDebug(averageCondition, 0)
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Foam::functionObjects::runTimeControls::runTimeCondition
Base class for run time conditions.
Definition: runTimeCondition.H:61
Foam::functionObjects::runTimeControls::runTimeControl::satisfiedAction::END
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::runTimeControls::runTimeControl::satisfiedAction::SET_TRIGGER
Foam::DynamicList< label >
runTimeControl.H
Foam::functionObjects::timeFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: timeFunctionObject.H:65
Foam::functionObjects::runTimeControls::runTimeControl::satisfiedActionNames
static Enum< satisfiedAction > satisfiedActionNames
Definition: runTimeControl.H:82
Foam::functionObjects::runTimeControls::addToRunTimeSelectionTable
addToRunTimeSelectionTable(runTimeCondition, averageCondition, dictionary)
Foam::functionObjects::runTimeControls::runTimeControl::read
virtual bool read(const dictionary &)
Read the runTimeControl data.
Definition: runTimeControl.C:85
Foam::functionObjects::runTimeControls::runTimeControl::write
virtual bool write()
Calculate the runTimeControl and write.
Definition: runTimeControl.C:313
Foam::functionObjects::stateFunctionObject::setTrigger
bool setTrigger(const label triggeri)
Set the current trigger index.
Definition: stateFunctionObject.C:81
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:847
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
runTimeControl
Controls when the calculation is terminated based on satisfying user-specified conditions.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::Time::writeAndEnd
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:606
runTimeCondition.H
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:528
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::functionObjects::timeFunctionObject::time
const Time & time() const
Return time database.
Definition: timeFunctionObject.H:96
Foam::functionObject::name
const word & name() const
Return the name of this functionObject.
Definition: functionObject.C:131
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::List< word >
Foam::Time::writeNow
bool writeNow()
Definition: TimeIO.C:599
Foam::functionObjects::runTimeControls::runTimeControl::satisfiedAction
satisfiedAction
Definition: runTimeControl.H:76
dictionary.H
Foam::labelMin
constexpr label labelMin
Definition: label.H:60
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::functionObjects::runTimeControls::runTimeControl::execute
virtual bool execute()
Execute, currently does nothing.
Definition: runTimeControl.C:177
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:670
Foam::functionObjects::runTimeControls::runTimeCondition::groupID
virtual label groupID() const
Return the group index.
Definition: runTimeCondition.C:111
Foam::functionObjects::runTimeControls::runTimeCondition::active
virtual bool active() const
Return the active flag.
Definition: runTimeCondition.C:104
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::Time::run
virtual bool run() const
Return true if run should continue,.
Definition: Time.C:885
Foam::functionObjects::stateFunctionObject::setProperty
void setProperty(const word &entryName, const Type &value)
Add generic property.
Definition: stateFunctionObjectTemplates.C:59
Foam::functionObjects::runTimeControls::runTimeCondition::apply
virtual bool apply()=0
Apply the condition.