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 
61 
62 
63 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
64 
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 
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 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::abortCalculation::read
virtual void read(const dictionary &)
Read the dictionary settings.
Definition: abortCalculation.C:109
Foam::dictionary::readIfPresent
bool readIfPresent(const word &, T &, bool recursive=false, bool patternMatch=true) const
Find an entry if present, and assign to T.
Definition: dictionaryTemplates.C:94
Foam::rm
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:954
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::abortCalculation::abortCalculation
abortCalculation(const abortCalculation &)
Disallow default bitwise copy construct.
Foam::Time::saNextWrite
@ saNextWrite
Definition: Time.H:106
Foam::abortCalculation::~abortCalculation
virtual ~abortCalculation()
Destructor.
Definition: abortCalculation.C:103
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::abortCalculation::execute
virtual void execute()
Execute, check existence of abort file and take action.
Definition: abortCalculation.C:127
abortCalculation.H
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::reduce
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
Definition: PstreamReduceOps.H:43
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
error.H
Foam::orOp
Definition: ops.H:178
Foam::Info
messageStream Info
Foam::abortCalculation::write
virtual void write()
Execute, check existence of abort file and take action.
Definition: abortCalculation.C:188
Foam::Time::saWriteNow
@ saWriteNow
Definition: Time.H:105
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::abortCalculation::actionTypeNames_
static const NamedEnum< actionType, 3 > actionTypeNames_
Action type names.
Definition: abortCalculation.H:91
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::isFile
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:622
PstreamReduceOps.H
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
Foam::abortCalculation::removeFile
void removeFile() const
Remove abort file.
Definition: abortCalculation.C:65
Foam::Time::saNoWriteNow
@ saNoWriteNow
Definition: Time.H:104
Foam::abortCalculation::timeSet
virtual void timeSet()
Called when time was set at the end of the Time::operator++.
Definition: abortCalculation.C:182
dictionary.H
Foam::abortCalculation::end
virtual void end()
Execute at the final time-loop, used for cleanup.
Definition: abortCalculation.C:176
Foam::abortCalculation::actionType
actionType
Enumeration defining the type of action.
Definition: abortCalculation.H:71
Foam::abortCalculation::abortFile_
fileName abortFile_
The fully-qualified name of the abort file.
Definition: abortCalculation.H:88
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::NamedEnum
Initialise the NamedEnum HashTable from the static list of names.
Definition: NamedEnum.H:52