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 | Copyright (C) 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 "runTimeControl.H"
27 #include "dictionary.H"
28 #include "runTimeCondition.H"
29 #include "fvMesh.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(runTimeControl, 0);
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 (
44  const word& name,
45  const objectRegistry& obr,
46  const dictionary& dict,
47  const bool loadFromFiles
48 )
49 :
51  obr_(obr),
52  conditions_(),
53  groupMap_(),
54  nWriteStep_(0),
55  writeStepI_(0)
56 {
57  // Check if the available mesh is an fvMesh, otherwise deactivate
58  if (setActive<fvMesh>())
59  {
60  read(dict);
61 
62  // Check that some conditions are set
63  if (conditions_.empty())
64  {
65  Info<< type() << " " << name_ << " output:" << nl
66  << " No conditions present - deactivating" << nl
67  << endl;
68 
69  active_ = false;
70  }
71  else
72  {
73  // Check that at least one condition is active
74  active_ = false;
75  forAll(conditions_, conditionI)
76  {
77  if (conditions_[conditionI].active())
78  {
79  active_ = true;
80  break;
81  }
82  }
83 
84  if (!active_)
85  {
86  Info<< type() << " " << name_ << " output:" << nl
87  << " All conditions inactive - deactivating" << nl
88  << endl;
89  }
90  }
91  }
92 }
93 
94 
95 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
96 
98 {}
99 
100 
101 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
102 
104 {
105  if (active_)
106  {
107  const dictionary& conditionsDict = dict.subDict("conditions");
108  const wordList conditionNames(conditionsDict.toc());
109  conditions_.setSize(conditionNames.size());
110 
111  label uniqueGroupI = 0;
112  forAll(conditionNames, conditionI)
113  {
114  const word& conditionName = conditionNames[conditionI];
115  const dictionary& dict = conditionsDict.subDict(conditionName);
116 
117  conditions_.set
118  (
119  conditionI,
120  runTimeCondition::New(conditionName, obr_, dict, *this)
121  );
122 
123  label groupI = conditions_[conditionI].groupID();
124 
125  if (groupMap_.insert(groupI, uniqueGroupI))
126  {
127  uniqueGroupI++;
128  }
129  }
130 
131  dict.readIfPresent("nWriteStep", nWriteStep_);
132  }
133 }
134 
135 
137 {
138  if (!active_)
139  {
140  return;
141  }
142 
143  Info<< type() << " " << name_ << " output:" << nl;
144 
145  // IDs of satisfied conditions
146  DynamicList<label> IDs(conditions_.size());
147 
148  // Run stops only if all conditions within a group are satisfied
149  List<bool> groupSatisfied(groupMap_.size(), true);
150  List<bool> groupActive(groupMap_.size(), false);
151 
152  forAll(conditions_, conditionI)
153  {
154  runTimeCondition& condition = conditions_[conditionI];
155 
156  if (condition.active())
157  {
158  bool conditionSatisfied = condition.apply();
159 
160  label groupI = condition.groupID();
161 
162  Map<label>::const_iterator conditionIter = groupMap_.find(groupI);
163 
164  if (conditionIter == groupMap_.end())
165  {
167  << "group " << groupI << " not found in map"
168  << abort(FatalError);
169  }
170 
171  if (conditionSatisfied)
172  {
173  IDs.append(conditionI);
174 
175  groupActive[conditionIter()] = true;
176 
177  if (groupI == -1)
178  {
179  // Condition not part of a group - only requires this to be
180  // satisfied for completion flag to be set
181  groupSatisfied[conditionIter()] = true;
182  break;
183  }
184  }
185  else
186  {
187  groupSatisfied[conditionIter()] = false;
188  }
189  }
190  }
191 
192  bool done = false;
193  forAll(groupSatisfied, groupI)
194  {
195  if (groupSatisfied[groupI] && groupActive[groupI])
196  {
197  done = true;
198  break;
199  }
200  }
201 
202  if (done)
203  {
204  forAll(IDs, conditionI)
205  {
206  Info<< " " << conditions_[conditionI].type() << ": "
207  << conditions_[conditionI].name()
208  << " condition satisfied" << nl;
209  }
210 
211 
212  // Set to write a data dump or finalise the calculation
213  Time& time = const_cast<Time&>(obr_.time());
214 
215  if (writeStepI_ < nWriteStep_ - 1)
216  {
217  writeStepI_++;
218  Info<< " Writing fields - step " << writeStepI_ << nl;
219  time.writeNow();
220  }
221  else
222  {
223  Info<< " Stopping calculation" << nl
224  << " Writing fields - final step" << nl;
225  time.writeAndEnd();
226  }
227  }
228  else
229  {
230  Info<< " Conditions not met - calculations proceeding" << nl;
231  }
232 
233  Info<< endl;
234 }
235 
236 
238 {
239  // Do nothing
240 }
241 
242 
244 {
245  // Do nothing
246 }
247 
248 
250 {
251  if (active_)
252  {
253  forAll(conditions_, conditionI)
254  {
255  conditions_[conditionI].write();
256  }
257  }
258 }
259 
260 
261 // ************************************************************************* //
Foam::runTimeCondition
Base class for run time conditions.
Definition: runTimeCondition.H:54
Foam::runTimeCondition::New
static autoPtr< runTimeCondition > New(const word &conditionName, const objectRegistry &obr, const dictionary &dict, functionObjectState &state)
Selector.
Definition: runTimeConditionNew.C:31
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
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::DynamicList< label >
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
runTimeControl.H
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: PrimitivePatchTemplate.H:68
Foam::runTimeControl::timeSet
virtual void timeSet()
Called when time was set at the end of the Time::operator++.
Definition: runTimeControl.C:243
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::runTimeControl::end
virtual void end()
Execute at the final time-loop, currently does nothing.
Definition: runTimeControl.C:237
Foam::runTimeControl::execute
virtual void execute()
Execute, currently does nothing.
Definition: runTimeControl.C:136
Foam::runTimeControl::write
virtual void write()
Calculate the runTimeControl and write.
Definition: runTimeControl.C:249
Foam::runTimeControl::~runTimeControl
virtual ~runTimeControl()
Destructor.
Definition: runTimeControl.C:97
Foam::Time::writeAndEnd
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:579
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
runTimeCondition.H
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::runTimeControl::read
virtual void read(const dictionary &)
Read the runTimeControl data.
Definition: runTimeControl.C:103
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::DynamicList::append
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::runTimeControl::runTimeControl
runTimeControl(const runTimeControl &)
Disallow default bitwise copy construct.
Foam::runTimeCondition::active
virtual bool active() const
Return the active flag.
Definition: runTimeCondition.C:98
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::Time::writeNow
bool writeNow()
Write the objects now (not at end of iteration) and continue.
Definition: TimeIO.C:571
dictionary.H
Foam::runTimeCondition::apply
virtual bool apply()=0
Apply the condition.
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:697
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::type
fileName::Type type(const fileName &)
Return the file type: DIRECTORY or FILE.
Definition: POSIX.C:588
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::runTimeCondition::groupID
virtual label groupID() const
Return the group index.
Definition: runTimeCondition.C:104
Foam::dictionary::set
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:856