abort.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2021 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 "abort.H"
30 #include "dictionary.H"
31 #include "error.H"
32 #include "Time.H"
33 #include "OSspecific.H"
34 #include "PstreamReduceOps.H"
36 #include <fstream>
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42 namespace functionObjects
43 {
45 
47  (
48  functionObject,
49  abort,
50  dictionary
51  );
52 }
53 }
54 
55 
56 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 // Read file contents and return a stop control as follows:
62 //
63 // - action=writeNow, action=nextWrite action=noWriteNow :
64 // The signalled action. Report as corresponding <action>.
65 //
66 // Anything else (empty file, no action=, etc) is reported as <unknown>.
67 //
68 static enum Time::stopAtControls getStopAction(const std::string& filename)
69 {
70  // Slurp entire input file (must exist) as a single string
71  std::string fileContent;
72 
73  std::ifstream is(filename);
74  std::getline(is, fileContent, '\0');
75 
76  const auto equals = fileContent.find('=');
77 
78  if (equals != std::string::npos)
79  {
80  const word actionName(word::validate(fileContent.substr(equals+1)));
81 
82  return
84  (
85  actionName,
86  Time::stopAtControls::saUnknown
87  );
88  }
89 
90  return Time::stopAtControls::saUnknown;
91 }
92 
93 
94 // Long description for the action name
95 static std::string longDescription(const Time::stopAtControls ctrl)
96 {
97  switch (ctrl)
98  {
99  case Time::saEndTime :
100  {
101  return "continue simulation to the endTime";
102  break;
103  }
104 
105  case Time::saNoWriteNow :
106  {
107  return "stop without writing data";
108  break;
109  }
110 
111  case Time::saWriteNow :
112  {
113  return "stop and write data";
114  break;
115  }
116 
117  case Time::saNextWrite :
118  {
119  return "stop after next data write";
120  break;
121  }
122 
123  default:
124  {
125  // Invalid choices already filtered out by Enum
126  return "unknown action";
127  break;
128  }
129  }
130 }
131 
132 } // End namespace Foam
133 
134 
135 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
136 
137 Foam::functionObjects::abort::abort
138 (
139  const word& name,
140  const Time& runTime,
141  const dictionary& dict
142 )
143 :
145  file_(),
146  defaultAction_(Time::stopAtControls::saUnknown),
147  triggered_(false)
148 {
149  read(dict);
150 
151  // Cleanup old files from previous runs
152  if (Pstream::master())
153  {
154  Foam::rm(file_);
155  }
156 }
157 
158 
159 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
160 
162 {
164 
165  file_.clear();
166 
167  if (dict.readIfPresent("file", file_))
168  {
169  file_.expand();
170  if (!file_.empty() && !file_.isAbsolute())
171  {
172  file_ = time_.globalPath()/file_;
173  file_.clean(); // Remove unneeded ".."
174  }
175  }
176 
177  // Ensure we always have a reasonable default file
178  if (file_.empty())
179  {
180  file_ = time_.globalPath()/name();
181  file_.clean(); // Remove unneeded ".."
182  }
183 
184  triggered_ = false;
185 
186  defaultAction_ = Time::stopAtControlNames.getOrDefault
187  (
188  "action",
189  dict,
190  Time::stopAtControls::saNextWrite
191  );
192 
193  Info<< type() << " activated ("
194  << longDescription(defaultAction_).c_str() <<")" << nl
195  << " File: " << file_ << endl;
196 
197  return true;
198 }
199 
200 
202 {
203  // If it has been triggered (eg, nextWrite) don't need to check it again
204  if (!triggered_)
205  {
206  auto action = Time::stopAtControls::saUnknown;
207 
208  if (Pstream::master() && Foam::isFile(file_))
209  {
210  action = getStopAction(file_);
211 
212  if (Time::stopAtControls::saUnknown == action)
213  {
214  // An unknown action means an empty file or bad content.
215  // Treat as a request for the default action.
216 
217  action = defaultAction_;
218  }
219  }
220 
221  // Send to slaves. Also acts as an MPI barrier
222  label intAction(action);
223  Pstream::scatter(intAction);
224 
225  action = Time::stopAtControls(intAction);
226 
227  // Call stopAt() on all processes
228  triggered_ = time_.stopAt(action);
229 
230  if (triggered_)
231  {
232  Info<< "USER REQUESTED ABORT (timeIndex="
233  << time_.timeIndex() << "): "
234  << longDescription(action).c_str() << endl;
235  }
236  }
237 
238  return true;
239 }
240 
241 
243 {
244  return true;
245 }
246 
247 
249 {
250  // Cleanup trigger file
251  if (Pstream::master())
252  {
253  Foam::rm(file_);
254  }
255 
256  return true;
257 }
258 
259 
260 // ************************************************************************* //
runTime
engineTime & runTime
Definition: createEngineTime.H:13
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
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:65
Foam::Time::saNextWrite
stop at the next data write interval
Definition: Time.H:102
Foam::functionObjects::timeFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: timeFunctionObject.H:65
Foam::functionObjects::abort::execute
virtual bool execute()
Check existence of the file and take action.
Definition: abort.C:201
Foam::functionObjects::timeFunctionObject
Virtual base class for function objects with a reference to Time.
Definition: timeFunctionObject.H:56
Foam::word::validate
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:45
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
Foam::isFile
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: MSwindows.C:658
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1004
Foam::longDescription
static std::string longDescription(const Time::stopAtControls ctrl)
Definition: abort.C:95
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Time::saEndTime
Stop when Time reaches prescribed endTime.
Definition: Time.H:99
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::Time::stopAtControlNames
static const Enum< stopAtControls > stopAtControlNames
Names for stopAtControls.
Definition: Time.H:119
Foam::getStopAction
static enum Time::stopAtControls getStopAction(const std::string &filename)
Definition: abort.C:68
error.H
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::Enum::getOrDefault
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Definition: Enum.C:179
Foam::Time::saNoWriteNow
Adjust endTime to stop immediately w/o writing.
Definition: Time.H:100
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
Foam::Enum::lookup
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition: Enum.C:92
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
PstreamReduceOps.H
Inter-processor communication reduction functions.
Time.H
Foam::functionObject::name
const word & name() const noexcept
Return the name of this functionObject.
Definition: functionObject.C:143
Foam::functionObjects::abort::end
virtual bool end()
Remove the trigger file after the final time-loop.
Definition: abort.C:248
Foam::Time::saWriteNow
adjust endTime to stop immediately w/ writing
Definition: Time.H:101
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::string::expand
string & expand(const bool allowEmpty=false)
Definition: string.C:173
dictionary.H
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::TimePaths::globalPath
fileName globalPath() const
Return global path for the case.
Definition: TimePathsI.H:80
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::fileName::clean
static bool clean(std::string &str)
Definition: fileName.C:199
Foam::functionObjects::abort::write
virtual bool write()
No-op.
Definition: abort.C:242
Foam::Time::stopAtControls
stopAtControls
Definition: Time.H:97
Foam::functionObjects::abort::read
virtual bool read(const dictionary &dict)
Read the dictionary settings.
Definition: abort.C:161
abort.H
Foam::fileName::isAbsolute
static bool isAbsolute(const std::string &str)
Definition: fileNameI.H:136