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-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 "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  << "Cannot reset " << signal_ << " trapping"
85  << abort(FatalError);
86  }
87 
88  // Update jobInfo file
90 
91  Info<< "sigStopAtWriteNow :"
92  << " setting up write and stop at end of the next iteration"
93  << nl << endl;
94  runTimePtr_->stopAt(Time::saWriteNow);
95 
97  //raise(signal_);
98 }
99 
100 
101 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
102 
104 
105 
107 (
108  const bool verbose,
109  const Time& runTime
110 )
111 {
112  // Store runTime
113  runTimePtr_ = &runTime;
114 
115  set(verbose);
116 }
117 
118 
119 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
120 
122 {
123  // Reset old handling
124  if (signal_ > 0)
125  {
126  if (sigaction(signal_, &oldAction_, NULL) < 0)
127  {
129  << "Cannot reset " << signal_ << " trapping"
130  << abort(FatalError);
131  }
132  }
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
138 void Foam::sigStopAtWriteNow::set(const bool verbose)
139 {
140  if (signal_ > 0)
141  {
142  // Check that the signal is different from the writeNowSignal
143  if (sigWriteNow::signal_ == signal_)
144  {
146  << "stopAtWriteNowSignal : " << signal_
147  << " cannot be the same as the writeNowSignal."
148  << " Please change this in the etc/controlDict."
149  << exit(FatalError);
150  }
151 
152 
153  struct sigaction newAction;
154  newAction.sa_handler = sigHandler;
155  newAction.sa_flags = SA_NODEFER;
156  sigemptyset(&newAction.sa_mask);
157  if (sigaction(signal_, &newAction, &oldAction_) < 0)
158  {
160  << "Cannot set " << signal_ << " trapping"
161  << abort(FatalError);
162  }
163 
164  if (verbose)
165  {
166  Info<< "sigStopAtWriteNow :"
167  << " Enabling writing and stopping upon signal " << signal_
168  << endl;
169  }
170  }
171 }
172 
173 
175 {
176  return signal_ > 0;
177 }
178 
179 
180 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
virtual void readData(Foam::Istream &is)
Read.
~sigStopAtWriteNow()
Destructor.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
set endTime to stop immediately w/ writing
Definition: Time.H:105
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:184
addstopAtWriteNowSignalToOpt(const char *name)
addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_("stopAtWriteNowSignal")
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
static void set(const bool verbose)
(re)set signal catcher
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
bool active() const
Is active?
errorManip< error > abort(error &err)
Definition: errorManip.H:131
label readLabel(Istream &is)
Definition: label.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:262
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void signalEnd() const
Definition: JobInfo.C:175
JobInfo jobInfo
Definition: JobInfo.C:35
messageStream Info
sigStopAtWriteNow()
Construct null.
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:236
virtual bool stopAt(const stopAtControls) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:906
Namespace for OpenFOAM.
virtual void writeData(Foam::Ostream &os) const
Write.