sigStopAtWriteNow.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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 
37 // Signal number to catch
38 int sigStopAtWriteNow::signal_
39 (
40  debug::optimisationSwitch("stopAtWriteNowSignal", -1)
41 );
42 
43 // Register re-reader
45 :
47 {
48 public:
49 
51  :
52  ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
53  {}
54 
56  {}
57 
58  virtual void readData(Foam::Istream& is)
59  {
60  sigStopAtWriteNow::signal_ = readLabel(is);
62  }
63 
64  virtual void writeData(Foam::Ostream& os) const
65  {
66  os << sigStopAtWriteNow::signal_;
67  }
68 };
69 
71 (
72  "stopAtWriteNowSignal"
73 );
74 
75 } // End namespace Foam
76 
77 
78 Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr;
79 struct sigaction Foam::sigStopAtWriteNow::oldAction_;
80 
81 
82 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
83 
84 void Foam::sigStopAtWriteNow::sigHandler(int)
85 {
86  // Reset old handling
87  if (sigaction(signal_, &oldAction_, nullptr) < 0)
88  {
90  << "Cannot reset " << signal_ << " trapping"
91  << abort(FatalError);
92  }
93 
94  // Update jobInfo file
96 
97  Info<< "sigStopAtWriteNow :"
98  << " setting up write and stop at end of the next iteration"
99  << nl << endl;
100  runTimePtr_->stopAt(Time::stopAtControl::writeNow);
101 
103  // raise(signal_);
104 }
105 
106 
107 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
108 
110 
111 
113 (
114  const bool verbose,
115  const Time& runTime
116 )
117 {
118  // Store runTime
119  runTimePtr_ = &runTime;
120 
121  set(verbose);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
126 
128 {
129  // Reset old handling
130  if (signal_ > 0)
131  {
132  if (sigaction(signal_, &oldAction_, nullptr) < 0)
133  {
135  << "Cannot reset " << signal_ << " trapping"
136  << abort(FatalError);
137  }
138  }
139 }
140 
141 
142 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
143 
144 void Foam::sigStopAtWriteNow::set(const bool verbose)
145 {
146  if (signal_ > 0)
147  {
148  // Check that the signal is different from the writeNowSignal
149  if (sigWriteNow::signal_ == signal_)
150  {
152  << "stopAtWriteNowSignal : " << signal_
153  << " cannot be the same as the writeNowSignal."
154  << " Please change this in the etc/controlDict."
155  << exit(FatalError);
156  }
157 
158 
159  struct sigaction newAction;
160  newAction.sa_handler = sigHandler;
161  newAction.sa_flags = SA_NODEFER;
162  sigemptyset(&newAction.sa_mask);
163  if (sigaction(signal_, &newAction, &oldAction_) < 0)
164  {
166  << "Cannot set " << signal_ << " trapping"
167  << abort(FatalError);
168  }
169 
170  if (verbose)
171  {
172  Info<< "sigStopAtWriteNow :"
173  << " Enabling writing and stopping upon signal " << signal_
174  << endl;
175  }
176  }
177 }
178 
179 
181 {
182  return signal_ > 0;
183 }
184 
185 
186 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
virtual void readData(Foam::Istream &is)
Read.
~sigStopAtWriteNow()
Destructor.
virtual void writeData(Foam::Ostream &os) const
Write.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
bool writeInfoHeader
set endTime to stop immediately w/ writing
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void signalEnd() const
Definition: jobInfo.C:210
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:196
bool active() const
Is active?
jobInfo jobInfo_
Definition: jobInfo.C:44
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...
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:54
static const char nl
Definition: Ostream.H:260
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
messageStream Info
sigStopAtWriteNow()
Construct null.
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:261