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-------------------------------------------------------------------------------
11License
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
40namespace Foam
41{
42namespace functionObjects
43{
45
47 (
49 abort,
51 );
52}
53}
54
55
56// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
57
58namespace 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//
68static 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,
87 );
88 }
89
91}
92
93
94// Long description for the action name
95static 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
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
187 (
188 "action",
189 dict,
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 {
207
208 if (Pstream::master() && Foam::isFile(file_))
209 {
210 action = getStopAction(file_);
211
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// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Inter-processor communication reduction functions.
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
EnumType getOrDefault(const word &key, const dictionary &dict, const EnumType deflt, const bool failsafe=false) const
Definition: Enum.C:179
EnumType lookup(const word &enumName, const EnumType deflt) const
The enumeration corresponding to the given name.
Definition: Enum.C:92
static void scatter(const List< commsStruct > &comms, T &value, const int tag, const label comm)
Broadcast data: Distribute without modification.
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static const Enum< stopAtControls > stopAtControlNames
Names for stopAtControls.
Definition: Time.H:119
stopAtControls
Definition: Time.H:98
@ saNoWriteNow
Adjust endTime to stop immediately w/o writing.
Definition: Time.H:100
@ saWriteNow
adjust endTime to stop immediately w/ writing
Definition: Time.H:101
@ saUnknown
Dummy no-op. Do not change current value.
Definition: Time.H:103
@ saEndTime
Stop when Time reaches prescribed endTime.
Definition: Time.H:99
@ saNextWrite
stop at the next data write interval
Definition: Time.H:102
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Abstract base-class for Time/database function objects.
Watches for presence of the named trigger file in the case directory and signals a simulation stop (o...
Definition: abort.H:128
virtual bool read(const dictionary &dict)
Read the dictionary settings.
Definition: abort.C:161
virtual bool execute()
Check existence of the file and take action.
Definition: abort.C:201
virtual bool write()
No-op.
Definition: abort.C:242
virtual bool end()
Remove the trigger file after the final time-loop.
Definition: abort.C:248
Virtual base class for function objects with a reference to Time.
virtual void validate()
Validate the turbulence fields after construction.
Definition: kkLOmega.C:597
splitCell * master() const
Definition: splitCell.H:113
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
engineTime & runTime
Namespace for OpenFOAM.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1012
static enum Time::stopAtControls getStopAction(const std::string &filename)
Definition: abort.C:68
messageStream Info
Information stream (stdout output on master, null elsewhere)
static std::string longDescription(const Time::stopAtControls ctrl)
Definition: abort.C:95
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
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:666
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict