1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | cfMesh: A library for mesh generation
5 \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6 \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of cfMesh.
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.
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
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/>.
26 \*---------------------------------------------------------------------------*/
28 #include "workflowControls.H"
29 #include "polyMeshGen.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 const std::map<word, label> workflowControls::workflowSteps_ =
37 populateWorkflowSteps();
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 bool workflowControls::restartRequested() const
43 const dictionary& meshDict =
44 mesh_.returnTime().lookupObject<dictionary>("meshDict");
48 meshDict.found("workflowControls") &&
49 meshDict.isDict("workflowControls")
52 const dictionary& workflowControls =
53 meshDict.subDict("workflowControls");
55 if( workflowControls.found("restartFromLatestStep") )
58 readBool(workflowControls.lookup("restartFromLatestStep"));
67 void workflowControls::setStepCompleted() const
69 if( mesh_.metaData().found("lastStep") )
71 mesh_.metaData().set("lastStep", currentStep_);
75 mesh_.metaData().add("lastStep", currentStep_);
78 DynList<word> completedSteps;
79 if( mesh_.metaData().found("completedSteps") )
80 completedSteps = wordList(mesh_.metaData().lookup("completedSteps"));
82 completedSteps.append(currentStep_);
84 if( mesh_.metaData().found("completedSteps") )
86 mesh_.metaData().set("completedSteps", completedSteps);
90 mesh_.metaData().add("completedSteps", completedSteps);
94 bool workflowControls::isStepCompleted() const
96 const word latestStep = lastCompletedStep();
98 if( latestStep.empty() )
101 const label currVal = workflowSteps_.find(currentStep_)->second;
102 const label latestVal = workflowSteps_.find(latestStep)->second;
104 if( latestVal == currVal )
110 bool workflowControls::exitAfterCurrentStep() const
112 const dictionary& meshDict =
113 mesh_.returnTime().lookupObject<dictionary>("meshDict");
117 meshDict.found("workflowControls") &&
118 meshDict.isDict("workflowControls")
121 const dictionary& workflowControls =
122 meshDict.subDict("workflowControls");
124 if( workflowControls.found("stopAfter") )
126 const word exitStep(workflowControls.lookup("stopAfter"));
128 if( exitStep == currentStep_ )
136 word workflowControls::lastCompletedStep() const
138 if( mesh_.metaData().found("lastStep") )
140 const word latestStep(mesh_.metaData().lookup("lastStep"));
148 DynList<word> workflowControls::completedSteps() const
150 DynList<word> completedSteps;
152 if( mesh_.metaData().found("completedSteps") )
153 completedSteps = wordList(mesh_.metaData().lookup("completedSteps"));
155 return completedSteps;
158 void workflowControls::clearCompletedSteps()
160 mesh_.metaData().remove("completedSteps");
161 mesh_.metaData().remove("lastStep");
164 bool workflowControls::stopAfterCurrentStep() const
168 if( exitAfterCurrentStep() )
170 bool writeSuccess(true);
174 Info << "Saving mesh generated after step " << currentStep_ << endl;
179 writeSuccess = false;
182 returnReduce(writeSuccess, minOp<bool>());
187 "bool workflowControls::stopAfterCurrentStep() const"
188 ) << "Mesh was not written on disk" << exit(FatalError);
191 std::string message("Stopping after step ");
192 message += currentStep_;
202 bool workflowControls::runAfterCurrentStep() const
204 if( currentStep_ == restartAfterStep_ )
208 Info << "Reading mesh generated after step "
209 << currentStep_ << endl;
221 "bool workflowControls::restartAfterCurrentStep() const"
222 ) << "Mesh cannot be loaded. Exitting..." << exit(FatalError);
229 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
231 std::map<word, label> workflowControls::populateWorkflowSteps()
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));
244 return workflowSteps;
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 workflowControls::workflowControls(polyMeshGen& mesh)
252 currentStep_("start"),
254 completedStepsBeforeRestart_(),
257 if( restartRequested() )
259 restartAfterStep_ = lastCompletedStep();
260 completedStepsBeforeRestart_ = completedSteps();
264 clearCompletedSteps();
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 workflowControls::~workflowControls()
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
276 bool workflowControls::runCurrentStep(const word& stepName)
280 completedStepsBeforeRestart_.size() &&
281 completedStepsBeforeRestart_.contains(currentStep_) &&
282 restartRequested() &&
286 Info << "Step " << currentStep_ << " has already been executed" << endl;
288 const bool retVal = runAfterCurrentStep();
290 //- this step has already been executed
292 currentStep_ = stepName;
296 else if( stopAfterCurrentStep() )
298 //- the process shall exit within the stopAfterCurrentStep function
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() )
307 for(it=workflowSteps_.begin();it!=workflowSteps_.end();++it)
308 toc.append(it->first);
312 "void workflowControls::setCurrentStep(const word&)"
313 ) << "Step " << stepName << " is not a valid name."
314 << " Valid step names are " << toc << exit(FatalError);
318 currentStep_ = stepName;
323 void workflowControls::workflowCompleted()
325 if( mesh_.metaData().found("lastStep") )
326 mesh_.metaData().remove("lastStep");
328 if( mesh_.metaData().found("completedSteps") )
329 mesh_.metaData().remove("completedSteps");
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
334 } // End namespace Foam
336 // ************************************************************************* //