abortCalculation.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-2013 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 "abortCalculation.H"
27 #include "dictionary.H"
28 #include "error.H"
29 #include "Time.H"
30 #include "OSspecific.H"
31 #include "PstreamReduceOps.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 defineTypeNameAndDebug(abortCalculation, 0);
38 }
39 
40 
41 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45  template<>
46  const char* Foam::NamedEnum
47  <
49  3
50  >::names[] =
51  {
52  "noWriteNow",
53  "writeNow",
54  "nextWrite"
55  };
56 }
57 
58 
60  Foam::abortCalculation::actionTypeNames_;
61 
62 
63 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
64 
65 void Foam::abortCalculation::removeFile() const
66 {
67  bool hasAbort = isFile(abortFile_);
68  reduce(hasAbort, orOp<bool>());
69 
70  if (hasAbort && Pstream::master())
71  {
72  // cleanup ABORT file (on master only)
73  rm(abortFile_);
74  }
75 }
76 
77 
78 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
79 
80 Foam::abortCalculation::abortCalculation
81 (
82  const word& name,
83  const objectRegistry& obr,
84  const dictionary& dict,
85  const bool loadFromFiles
86 )
87 :
88  name_(name),
89  obr_(obr),
90  abortFile_("$FOAM_CASE/" + name),
91  action_(nextWrite)
92 {
93  abortFile_.expand();
94  read(dict);
95 
96  // remove any old files from previous runs
97  removeFile();
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
102 
104 {}
105 
106 
107 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
108 
110 {
111  if (dict.found("action"))
112  {
113  action_ = actionTypeNames_.read(dict.lookup("action"));
114  }
115  else
116  {
117  action_ = nextWrite;
118  }
119 
120  if (dict.readIfPresent("fileName", abortFile_))
121  {
122  abortFile_.expand();
123  }
124 }
125 
126 
128 {
129  bool hasAbort = isFile(abortFile_);
130  reduce(hasAbort, orOp<bool>());
131 
132  if (hasAbort)
133  {
134  switch (action_)
135  {
136  case noWriteNow :
137  {
138  if (obr_.time().stopAt(Time::saNoWriteNow))
139  {
140  Info<< "USER REQUESTED ABORT (timeIndex="
141  << obr_.time().timeIndex()
142  << "): stop without writing data"
143  << endl;
144  }
145  break;
146  }
147 
148  case writeNow :
149  {
150  if (obr_.time().stopAt(Time::saWriteNow))
151  {
152  Info<< "USER REQUESTED ABORT (timeIndex="
153  << obr_.time().timeIndex()
154  << "): stop+write data"
155  << endl;
156  }
157  break;
158  }
159 
160  case nextWrite :
161  {
162  if (obr_.time().stopAt(Time::saNextWrite))
163  {
164  Info<< "USER REQUESTED ABORT (timeIndex="
165  << obr_.time().timeIndex()
166  << "): stop after next data write"
167  << endl;
168  }
169  break;
170  }
171  }
172  }
173 }
174 
175 
177 {
178  removeFile();
179 }
180 
181 
183 {
184  // Do nothing - only valid on execute
185 }
186 
187 
189 {
190  // Do nothing - only valid on execute
191 }
192 
193 
194 // ************************************************************************* //
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:306
virtual ~abortCalculation()
Destructor.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A class for handling words, derived from string.
Definition: word.H:59
messageStream Info
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:955
virtual void end()
Execute at the final time-loop, used for cleanup.
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52
virtual void read(const dictionary &)
Read the dictionary settings.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:452
Registry of regIOobjects.
actionType
Enumeration defining the type of action.
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:398
bool read(const char *, int32_t &)
Definition: int32IO.C:87
virtual void execute()
Execute, check existence of abort file and take action.
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:623
virtual void timeSet()
Called when time was set at the end of the Time::operator++.
defineTypeNameAndDebug(combustionModel, 0)
virtual void write()
Execute, check existence of abort file and take action.