Forward compatibility: flex
[foam-extend-3.2.git] / src / mesh / cfMesh / utilities / workflowControls / workflowControls.C
blobb7ada30f543d14acfa8320be78f0ab5307826008
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.
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
19     for more details.
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/>.
24 Description
26 \*---------------------------------------------------------------------------*/
28 #include "workflowControls.H"
29 #include "polyMeshGen.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
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");
46     if
47     (
48         meshDict.found("workflowControls") &&
49         meshDict.isDict("workflowControls")
50     )
51     {
52         const dictionary& workflowControls =
53             meshDict.subDict("workflowControls");
55         if( workflowControls.found("restartFromLatestStep") )
56         {
57             const bool restart =
58                 readBool(workflowControls.lookup("restartFromLatestStep"));
60             return restart;
61         }
62     }
64     return false;
67 void workflowControls::setStepCompleted() const
69     if( mesh_.metaData().found("lastStep") )
70     {
71         mesh_.metaData().set("lastStep", currentStep_);
72     }
73     else
74     {
75         mesh_.metaData().add("lastStep", currentStep_);
76     }
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") )
85     {
86         mesh_.metaData().set("completedSteps", completedSteps);
87     }
88     else
89     {
90         mesh_.metaData().add("completedSteps", completedSteps);
91     }
94 bool workflowControls::isStepCompleted() const
96     const word latestStep = lastCompletedStep();
98     if( latestStep.empty() )
99         return false;
101     const label currVal = workflowSteps_.find(currentStep_)->second;
102     const label latestVal = workflowSteps_.find(latestStep)->second;
104     if( latestVal == currVal )
105         return true;
107     return false;
110 bool workflowControls::exitAfterCurrentStep() const
112     const dictionary& meshDict =
113         mesh_.returnTime().lookupObject<dictionary>("meshDict");
115     if
116     (
117         meshDict.found("workflowControls") &&
118         meshDict.isDict("workflowControls")
119     )
120     {
121         const dictionary& workflowControls =
122             meshDict.subDict("workflowControls");
124         if( workflowControls.found("stopAfter") )
125         {
126             const word exitStep(workflowControls.lookup("stopAfter"));
128             if( exitStep == currentStep_ )
129                 return true;
130         }
131     }
133     return false;
136 word workflowControls::lastCompletedStep() const
138     if( mesh_.metaData().found("lastStep") )
139     {
140         const word latestStep(mesh_.metaData().lookup("lastStep"));
142         return latestStep;
143     }
145     return word();
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
166     setStepCompleted();
168     if( exitAfterCurrentStep() )
169     {
170         bool writeSuccess(true);
172         try
173         {
174             Info << "Saving mesh generated after step " << currentStep_ << endl;
175             mesh_.write();
176         }
177         catch(...)
178         {
179             writeSuccess = false;
180         }
182         returnReduce(writeSuccess, minOp<bool>());
184         if( !writeSuccess )
185             FatalErrorIn
186             (
187                 "bool workflowControls::stopAfterCurrentStep() const"
188             ) << "Mesh was not written on disk" << exit(FatalError);
191         std::string message("Stopping after step ");
192         message += currentStep_;
194         throw message;
196         return true;
197     }
199     return false;
202 bool workflowControls::runAfterCurrentStep() const
204     if( currentStep_ == restartAfterStep_ )
205     {
206         try
207         {
208             Info << "Reading mesh generated after step "
209                  << currentStep_ << endl;
211             mesh_.read();
213             isRestarted_ = true;
215             return true;
216         }
217         catch(...)
218         {
219             FatalErrorIn
220             (
221                 "bool workflowControls::restartAfterCurrentStep() const"
222             ) << "Mesh cannot be loaded. Exitting..." << exit(FatalError);
223         }
224     }
226     return false;
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)
251     mesh_(mesh),
252     currentStep_("start"),
253     restartAfterStep_(),
254     completedStepsBeforeRestart_(),
255     isRestarted_(false)
257     if( restartRequested() )
258     {
259         restartAfterStep_ = lastCompletedStep();
260         completedStepsBeforeRestart_ = completedSteps();
261     }
262     else
263     {
264         clearCompletedSteps();
266     }
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 workflowControls::~workflowControls()
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
276 bool workflowControls::runCurrentStep(const word& stepName)
278     if
279     (
280         completedStepsBeforeRestart_.size() &&
281         completedStepsBeforeRestart_.contains(currentStep_) &&
282         restartRequested() &&
283         !isRestarted_
284     )
285     {
286         Info << "Step " << currentStep_ << " has already been executed" << endl;
288         const bool retVal = runAfterCurrentStep();
290         //- this step has already been executed
291         setStepCompleted();
292         currentStep_ = stepName;
294         return retVal;
295     }
296     else if( stopAfterCurrentStep() )
297     {
298         //- the process shall exit within the stopAfterCurrentStep function
299         return false;
300     }
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);
310         FatalErrorIn
311         (
312             "void workflowControls::setCurrentStep(const word&)"
313         ) << "Step " << stepName << " is not a valid name."
314           << " Valid step names are " << toc << exit(FatalError);
315     }
317     setStepCompleted();
318     currentStep_ = stepName;
320     return true;
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 // ************************************************************************* //