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 | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM 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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "abort.H"
27 #include "dictionary.H"
28 #include "error.H"
29 #include "Time.H"
30 #include "OSspecific.H"
31 #include "PstreamReduceOps.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
41 
43  (
44  functionObject,
45  abort,
46  dictionary
47  );
48 }
49 }
50 
51 template<>
52 const char* Foam::NamedEnum
53 <
55  3
56 >::names[] =
57 {
58  "noWriteNow",
59  "writeNow",
60  "nextWrite"
61 };
62 
63 const Foam::NamedEnum
64 <
66  3
67 > Foam::functionObjects::abort::actionTypeNames_;
68 
69 
70 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
71 
72 void Foam::functionObjects::abort::removeFile() const
73 {
74  bool hasAbort = isFile(abortFile_);
75  reduce(hasAbort, orOp<bool>());
76 
77  if (hasAbort && Pstream::master())
78  {
79  // Cleanup ABORT file (on master only)
80  rm(abortFile_);
81  }
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86 
87 Foam::functionObjects::abort::abort
88 (
89  const word& name,
90  const Time& runTime,
91  const dictionary& dict
92 )
93 :
94  functionObject(name),
95  time_(runTime),
96  abortFile_("$FOAM_CASE/" + name),
97  action_(nextWrite)
98 {
99  abortFile_.expand();
100  read(dict);
101 
102  // Remove any old files from previous runs
103  removeFile();
104 }
105 
106 
107 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
108 
110 {}
111 
112 
113 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
114 
116 {
117  if (dict.found("action"))
118  {
119  action_ = actionTypeNames_.read(dict.lookup("action"));
120  }
121  else
122  {
123  action_ = nextWrite;
124  }
125 
126  if (dict.readIfPresent("fileName", abortFile_))
127  {
128  abortFile_.expand();
129  }
130 
131  return true;
132 }
133 
134 
136 {
137  bool hasAbort = isFile(abortFile_);
138  reduce(hasAbort, orOp<bool>());
139 
140  if (hasAbort)
141  {
142  switch (action_)
143  {
144  case noWriteNow :
145  {
146  if (time_.stopAt(Time::saNoWriteNow))
147  {
148  Info<< "USER REQUESTED ABORT (timeIndex="
149  << time_.timeIndex()
150  << "): stop without writing data"
151  << endl;
152  }
153  break;
154  }
155 
156  case writeNow :
157  {
158  if (time_.stopAt(Time::saWriteNow))
159  {
160  Info<< "USER REQUESTED ABORT (timeIndex="
161  << time_.timeIndex()
162  << "): stop+write data"
163  << endl;
164  }
165  break;
166  }
167 
168  case nextWrite :
169  {
170  if (time_.stopAt(Time::saNextWrite))
171  {
172  Info<< "USER REQUESTED ABORT (timeIndex="
173  << time_.timeIndex()
174  << "): stop after next data write"
175  << endl;
176  }
177  break;
178  }
179  }
180  }
181 
182  return true;
183 }
184 
185 
187 {
188  return true;
189 }
190 
191 
193 {
194  removeFile();
195  return true;
196 }
197 
198 
199 // ************************************************************************* //
Inter-processor communication reduction functions.
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
set endTime to stop immediately w/ writing
Definition: Time.H:105
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:492
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:411
virtual bool end()
Execute at the final time-loop, used for cleanup.
Definition: abort.C:192
Abstract base-class for Time/database function objects.
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Macros for easy insertion into run-time selection tables.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool read(const char *, int32_t &)
Definition: int32IO.C:85
virtual bool execute()
Execute, check existence of abort file and take action.
Definition: abort.C:135
A class for handling words, derived from string.
Definition: word.H:59
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
virtual bool read(const dictionary &)
Read the dictionary settings.
Definition: abort.C:115
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:306
errorManip< error > abort(error &err)
Definition: errorManip.H:131
virtual ~abort()
Destructor.
Definition: abort.C:109
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
set endTime to stop immediately w/o writing
Definition: Time.H:104
addToRunTimeSelectionTable(functionObject, blendingFactor, dictionary)
actionType
Enumeration defining the type of action.
Definition: abort.H:69
messageStream Info
stop the next time data are written
Definition: Time.H:106
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:819
virtual bool write()
Execute, check existence of abort file and take action.
Definition: abort.C:186
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:451