sigStopAtWriteNow.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-2012 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 "sigStopAtWriteNow.H"
27 #include "error.H"
28 #include "JobInfo.H"
29 #include "IOstreams.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 // Signal number to catch
37 int sigStopAtWriteNow::signal_
38 (
39  debug::optimisationSwitch("stopAtWriteNowSignal", -1)
40 );
41 // Register re-reader
43 :
45 {
46 public:
48  :
50  {}
52  {}
53  virtual void readData(Foam::Istream& is)
54  {
55  sigStopAtWriteNow::signal_ = readLabel(is);
57  }
58  virtual void writeData(Foam::Ostream& os) const
59  {
60  os << sigStopAtWriteNow::signal_;
61  }
62 };
64 (
65  "stopAtWriteNowSignal"
66 );
67 }
68 
69 
70 static Foam::Time const* runTimePtr_ = NULL;
71 
72 
73 struct sigaction Foam::sigStopAtWriteNow::oldAction_;
74 
75 
76 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
77 
78 void Foam::sigStopAtWriteNow::sigHandler(int)
79 {
80  // Reset old handling
81  if (sigaction(signal_, &oldAction_, NULL) < 0)
82  {
84  (
85  "Foam::sigStopAtWriteNow::sigHandler(int)"
86  ) << "Cannot reset " << signal_ << " trapping"
87  << abort(FatalError);
88  }
89 
90  // Update jobInfo file
92 
93  Info<< "sigStopAtWriteNow :"
94  << " setting up write and stop at end of the next iteration"
95  << nl << endl;
96  runTimePtr_->stopAt(Time::saWriteNow);
97 
99  //raise(signal_);
100 }
101 
102 
103 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
104 
106 
107 
109 (
110  const bool verbose,
111  const Time& runTime
112 )
113 {
114  // Store runTime
115  runTimePtr_ = &runTime;
116 
117  set(verbose);
118 }
119 
120 
121 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
122 
124 {
125  // Reset old handling
126  if (signal_ > 0)
127  {
128  if (sigaction(signal_, &oldAction_, NULL) < 0)
129  {
131  (
132  "Foam::sigStopAtWriteNow::~sigStopAtWriteNow()"
133  ) << "Cannot reset " << signal_ << " trapping"
134  << abort(FatalError);
135  }
136  }
137 }
138 
139 
140 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
141 
142 void Foam::sigStopAtWriteNow::set(const bool verbose)
143 {
144  if (signal_ > 0)
145  {
146  // Check that the signal is different from the writeNowSignal
147  if (sigWriteNow::signal_ == signal_)
148  {
150  (
151  "Foam::sigStopAtWriteNow::sigStopAtWriteNow"
152  "(const bool, const Time&)"
153  ) << "stopAtWriteNowSignal : " << signal_
154  << " cannot be the same as the writeNowSignal."
155  << " Please change this in the controlDict ("
156  << findEtcFile("controlDict", false) << ")."
157  << exit(FatalError);
158  }
159 
160 
161  struct sigaction newAction;
162  newAction.sa_handler = sigHandler;
163  newAction.sa_flags = SA_NODEFER;
164  sigemptyset(&newAction.sa_mask);
165  if (sigaction(signal_, &newAction, &oldAction_) < 0)
166  {
168  (
169  "Foam::sigStopAtWriteNow::sigStopAtWriteNow"
170  "(const bool, const Time&)"
171  ) << "Cannot set " << signal_ << " trapping"
172  << abort(FatalError);
173  }
174 
175  if (verbose)
176  {
177  Info<< "sigStopAtWriteNow :"
178  << " Enabling writing and stopping upon signal " << signal_
179  << endl;
180  }
181  }
182 }
183 
184 
186 {
187  return signal_ > 0;
188 }
189 
190 
191 // ************************************************************************* //
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
virtual void writeData(Foam::Ostream &os) const
Write.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual void readData(Foam::Istream &is)
Read.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
messageStream Info
sigStopAtWriteNow()
Construct null.
void signalEnd() const
Definition: JobInfo.C:176
addstopAtWriteNowSignalToOpt(const char *name)
Namespace for OpenFOAM.
bool active() const
Is active?
label readLabel(Istream &is)
Definition: label.H:64
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
static const char nl
Definition: Ostream.H:260
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: POSIX.C:405
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:235
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Abstract base class for registered object with I/O. Used in debug symbol registration.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
JobInfo jobInfo
Definition: JobInfo.C:35
~sigStopAtWriteNow()
Destructor.
error FatalError
addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_("stopAtWriteNowSignal")
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:183
virtual bool stopAt(const stopAtControls) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:943
static void set(const bool verbose)
(re)set signal catcher