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-2022 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 }
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 sub-ranks. Also acts as an MPI barrier
222  int intAction(action);
223  Pstream::broadcast(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 
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 // ************************************************************************* //
static word validate(const std::string &s, const bool prefix=false)
Construct validated word (no invalid characters).
Definition: word.C:39
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
Inter-processor communication reduction functions.
virtual bool read(const dictionary &dict)
Read the dictionary settings.
Definition: abort.C:154
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
adjust endTime to stop immediately w/ writing
Definition: Time.H:101
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual bool end()
Remove the trigger file after the final time-loop.
Definition: abort.C:241
static const Enum< stopAtControls > stopAtControlNames
Names for stopAtControls.
Definition: Time.H:114
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool warnOnly=false) const
Find the key in the dictionary and return the corresponding enumeration element based on its name...
Definition: Enum.C:173
Macros for easy insertion into run-time selection tables.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all communicator ranks. Does nothing in non-paral...
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual bool execute()
Check existence of the file and take action.
Definition: abort.C:194
A class for handling words, derived from Foam::string.
Definition: word.H:63
static std::string longDescription(const Time::stopAtControls ctrl)
Definition: abort.C:88
errorManip< error > abort(error &err)
Definition: errorManip.H:139
static enum Time::stopAtControls getStopAction(const std::string &filename)
Definition: abort.C:61
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
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: POSIX.C:877
Adjust endTime to stop immediately w/o writing.
Definition: Time.H:100
Stop when Time reaches prescribed endTime.
Definition: Time.H:99
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
messageStream Info
Information stream (stdout output on master, null elsewhere)
stop at the next data write interval
Definition: Time.H:102
stopAtControls
Stop-run control options, which are primarily used when altering the stopAt condition.
Definition: Time.H:97
virtual bool write()
No-op.
Definition: abort.C:235
Virtual base class for function objects with a reference to Time.
Namespace for OpenFOAM.
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition: Enum.C:85
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: POSIX.C:1404