workflowControls.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the
13  Free Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "workflowControls.H"
29 #include "polyMeshGen.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 const std::map<word, label> workflowControls::workflowSteps_ =
37  populateWorkflowSteps();
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 
42 {
43  const dictionary& meshDict =
44  mesh_.returnTime().lookupObject<dictionary>("meshDict");
45 
46  if
47  (
48  meshDict.found("workflowControls") &&
49  meshDict.isDict("workflowControls")
50  )
51  {
53  meshDict.subDict("workflowControls");
54 
55  if( workflowControls.found("restartFromLatestStep") )
56  {
57  const bool restart =
58  readBool(workflowControls.lookup("restartFromLatestStep"));
59 
60  return restart;
61  }
62  }
63 
64  return false;
65 }
66 
68 {
69  if( mesh_.metaData().found("lastStep") )
70  {
71  mesh_.metaData().set("lastStep", currentStep_);
72  }
73  else
74  {
75  mesh_.metaData().add("lastStep", currentStep_);
76  }
77 
79  if( mesh_.metaData().found("completedSteps") )
80  completedSteps = wordList(mesh_.metaData().lookup("completedSteps"));
81 
83 
84  if( mesh_.metaData().found("completedSteps") )
85  {
86  mesh_.metaData().set("completedSteps", completedSteps);
87  }
88  else
89  {
90  mesh_.metaData().add("completedSteps", completedSteps);
91  }
92 }
93 
95 {
96  const word latestStep = lastCompletedStep();
97 
98  if( latestStep.empty() )
99  return false;
100 
101  const label currVal = workflowSteps_.find(currentStep_)->second;
102  const label latestVal = workflowSteps_.find(latestStep)->second;
103 
104  if( latestVal == currVal )
105  return true;
106 
107  return false;
108 }
109 
111 {
112  const dictionary& meshDict =
113  mesh_.returnTime().lookupObject<dictionary>("meshDict");
114 
115  if
116  (
117  meshDict.found("workflowControls") &&
118  meshDict.isDict("workflowControls")
119  )
120  {
122  meshDict.subDict("workflowControls");
123 
124  if( workflowControls.found("stopAfter") )
125  {
126  const word exitStep(workflowControls.lookup("stopAfter"));
127 
128  if( exitStep == currentStep_ )
129  return true;
130  }
131  }
132 
133  return false;
134 }
135 
137 {
138  if( mesh_.metaData().found("lastStep") )
139  {
140  const word latestStep(mesh_.metaData().lookup("lastStep"));
141 
142  return latestStep;
143  }
144 
145  return word();
146 }
147 
149 {
151 
152  if( mesh_.metaData().found("completedSteps") )
153  completedSteps = wordList(mesh_.metaData().lookup("completedSteps"));
154 
155  return completedSteps;
156 }
157 
159 {
160  mesh_.metaData().remove("completedSteps");
161  mesh_.metaData().remove("lastStep");
162 }
163 
165 {
167 
168  if( exitAfterCurrentStep() )
169  {
170  bool writeSuccess(true);
171 
172  try
173  {
174  Info << "Saving mesh generated after step " << currentStep_ << endl;
175  mesh_.write();
176  }
177  catch(...)
178  {
179  writeSuccess = false;
180  }
181 
182  returnReduce(writeSuccess, minOp<bool>());
183 
184  if( !writeSuccess )
186  (
187  "bool workflowControls::stopAfterCurrentStep() const"
188  ) << "Mesh was not written on disk" << exit(FatalError);
189 
190 
191  std::string message("Stopping after step ");
192  message += currentStep_;
193 
194  throw message;
195 
196  return true;
197  }
198 
199  return false;
200 }
201 
203 {
205  {
206  try
207  {
208  Info << "Reading mesh generated after step "
209  << currentStep_ << endl;
210 
211  mesh_.read();
212 
213  isRestarted_ = true;
214 
215  return true;
216  }
217  catch(...)
218  {
220  (
221  "bool workflowControls::restartAfterCurrentStep() const"
222  ) << "Mesh cannot be loaded. Exitting..." << exit(FatalError);
223  }
224  }
225 
226  return false;
227 }
228 
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
230 
232 {
233  std::map<word, label> workflowSteps;
234  workflowSteps.insert(std::make_pair(word("start"), 0));
235  workflowSteps.insert(std::make_pair(word("templateGeneration"), 1));
236  workflowSteps.insert(std::make_pair(word("surfaceTopology"), 2));
237  workflowSteps.insert(std::make_pair(word("surfaceProjection"), 4));
238  workflowSteps.insert(std::make_pair(word("patchAssignment"), 8));
239  workflowSteps.insert(std::make_pair(word("edgeExtraction"), 16));
240  workflowSteps.insert(std::make_pair(word("meshOptimisation"), 32));
241  workflowSteps.insert(std::make_pair(word("boundaryLayerGeneration"), 64));
242  workflowSteps.insert(std::make_pair(word("boundaryLayerRefinement"), 128));
243 
244  return workflowSteps;
245 }
246 
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 
250 :
251  mesh_(mesh),
252  currentStep_("start"),
253  restartAfterStep_(),
254  completedStepsBeforeRestart_(),
255  isRestarted_(false)
256 {
257  if( restartRequested() )
258  {
261  }
262  else
263  {
265 
266  }
267 }
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
272 {}
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
277 {
278  if
279  (
282  restartRequested() &&
283  !isRestarted_
284  )
285  {
286  Info << "Step " << currentStep_ << " has already been executed" << endl;
287 
288  const bool retVal = runAfterCurrentStep();
289 
290  //- this step has already been executed
292  currentStep_ = stepName;
293 
294  return retVal;
295  }
296  else if( stopAfterCurrentStep() )
297  {
298  //- the process shall exit within the stopAfterCurrentStep function
299  return false;
300  }
301 
302  //- check if the requested step exists in the database of steps
303  std::map<word, label>::const_iterator it = workflowSteps_.find(stepName);
304  if( it == workflowSteps_.end() )
305  {
306  DynList<word> toc;
307  for(it=workflowSteps_.begin();it!=workflowSteps_.end();++it)
308  toc.append(it->first);
309 
311  (
312  "void workflowControls::setCurrentStep(const word&)"
313  ) << "Step " << stepName << " is not a valid name."
314  << " Valid step names are " << toc << exit(FatalError);
315  }
316 
318  currentStep_ = stepName;
319 
320  return true;
321 }
322 
324 {
325  if( mesh_.metaData().found("lastStep") )
326  mesh_.metaData().remove("lastStep");
327 
328  if( mesh_.metaData().found("completedSteps") )
329  mesh_.metaData().remove("completedSteps");
330 }
331 
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 
334 } // End namespace Foam
335 
336 // ************************************************************************* //
Foam::workflowControls::runCurrentStep
bool runCurrentStep(const word &)
shall the process run the current step
Definition: workflowControls.C:276
Foam::workflowControls::lastCompletedStep
word lastCompletedStep() const
return the latest completed step
Definition: workflowControls.C:136
Foam::workflowControls::clearCompletedSteps
void clearCompletedSteps()
remove completedStep from the dictionary
Definition: workflowControls.C:158
Foam::workflowControls::restartRequested
bool restartRequested() const
check if restart is requested
Definition: workflowControls.C:41
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::returnReduce
T returnReduce(const T &Value, const BinaryOp &bop, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Definition: PstreamReduceOps.H:86
Foam::minOp
Definition: ops.H:173
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::dictionary::remove
bool remove(const word &)
Remove an entry specified by keyword.
Definition: dictionary.C:881
Foam::workflowControls::populateWorkflowSteps
static std::map< word, label > populateWorkflowSteps()
populate workflowSteps with values
Definition: workflowControls.C:231
Foam::polyMeshGen
Definition: polyMeshGen.H:46
Foam::workflowControls::mesh_
polyMeshGen & mesh_
reference to the mesh
Definition: workflowControls.H:59
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::workflowControls::completedStepsBeforeRestart_
DynList< word > completedStepsBeforeRestart_
completed step before restart
Definition: workflowControls.H:68
Foam::polyMeshGen::read
void read()
Definition: polyMeshGen.C:121
Foam::dictionary::isDict
bool isDict(const word &) const
Check if entry is a sub-dictionary.
Definition: dictionary.C:600
Foam::wordList
List< word > wordList
A List of words.
Definition: fileName.H:54
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::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::workflowControls::setStepCompleted
void setStepCompleted() const
sets the current step to completed
Definition: workflowControls.C:67
Foam::Info
messageStream Info
polyMeshGen.H
Foam::workflowControls::restartAfterStep_
word restartAfterStep_
step for restarting the workflow
Definition: workflowControls.H:65
Foam::workflowControls::completedSteps
DynList< word > completedSteps() const
return the names of completed steps
Definition: workflowControls.C:148
Foam::workflowControls::workflowCompleted
void workflowCompleted()
set the workflow completed flag
Definition: workflowControls.C:323
Foam::polyMeshGen::metaData
const dictionary & metaData() const
return a constant reference to metaDict
Definition: polyMeshGen.H:86
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
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::workflowControls::workflowControls
workflowControls(polyMeshGen &mesh)
Construct from IOdictionary.
Definition: workflowControls.C:249
Foam::polyMeshGenPoints::returnTime
const Time & returnTime() const
access to Time
Definition: polyMeshGenPointsI.H:39
Foam::DynList
Definition: DynList.H:53
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::workflowControls::isRestarted_
bool isRestarted_
holds information whether the workflow has been restarted
Definition: workflowControls.H:71
Foam::workflowControls::isStepCompleted
bool isStepCompleted() const
is the current step already completed
Definition: workflowControls.C:94
Foam::workflowControls::~workflowControls
~workflowControls()
Definition: workflowControls.C:271
Foam::polyMeshGen::write
void write() const
Definition: polyMeshGen.C:126
Foam::workflowControls::runAfterCurrentStep
bool runAfterCurrentStep() const
shall the workflow run after the current step
Definition: workflowControls.C:202
Foam::workflowControls::workflowSteps_
static const std::map< word, label > workflowSteps_
Definition: workflowControls.H:74
workflowControls.H
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:313
Foam::readBool
bool readBool(Istream &)
Definition: boolIO.C:60
Foam::objectRegistry::lookupObject
const Type & lookupObject(const word &name) const
Lookup and return the object of the given Type.
Definition: objectRegistryTemplates.C:165
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::workflowControls::currentStep_
word currentStep_
current step in the workflow
Definition: workflowControls.H:62
Foam::workflowControls
Definition: workflowControls.H:55
Foam::workflowControls::stopAfterCurrentStep
bool stopAfterCurrentStep() const
shall the workflow stop after the current step
Definition: workflowControls.C:164
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729
Foam::DynList::append
void append(const T &e)
Append an element at the end of the list.
Definition: DynListI.H:304
Foam::workflowControls::exitAfterCurrentStep
bool exitAfterCurrentStep() const
shall the procedure stop after the current step
Definition: workflowControls.C:110
Foam::dictionary::set
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:856