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-2018 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 
49 public:
50 
52  :
53  ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
54  {}
55 
57  {}
58 
59  virtual void readData(Foam::Istream& is)
60  {
61  sigStopAtWriteNow::signal_ = readLabel(is);
63  }
64 
65  virtual void writeData(Foam::Ostream& os) const
66  {
67  os << sigStopAtWriteNow::signal_;
68  }
69 };
70 
72 (
73  "stopAtWriteNowSignal"
74 );
75 
76 }
77 
78 
79 Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr;
80 
81 
82 struct sigaction Foam::sigStopAtWriteNow::oldAction_;
83 
84 
85 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
86 
87 void Foam::sigStopAtWriteNow::sigHandler(int)
88 {
89  // Reset old handling
90  if (sigaction(signal_, &oldAction_, nullptr) < 0)
91  {
93  << "Cannot reset " << signal_ << " trapping"
94  << abort(FatalError);
95  }
96 
97  // Update jobInfo file
99 
100  Info<< "sigStopAtWriteNow :"
101  << " setting up write and stop at end of the next iteration"
102  << nl << endl;
103  runTimePtr_->stopAt(Time::saWriteNow);
104 
106  // raise(signal_);
107 }
108 
109 
110 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
111 
113 
114 
116 (
117  const bool verbose,
118  const Time& runTime
119 )
120 {
121  // Store runTime
122  runTimePtr_ = &runTime;
123 
124  set(verbose);
125 }
126 
127 
128 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
129 
131 {
132  // Reset old handling
133  if (signal_ > 0)
134  {
135  if (sigaction(signal_, &oldAction_, nullptr) < 0)
136  {
138  << "Cannot reset " << signal_ << " trapping"
139  << abort(FatalError);
140  }
141  }
142 }
143 
144 
145 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
146 
147 void Foam::sigStopAtWriteNow::set(const bool verbose)
148 {
149  if (signal_ > 0)
150  {
151  // Check that the signal is different from the writeNowSignal
152  if (sigWriteNow::signal_ == signal_)
153  {
155  << "stopAtWriteNowSignal : " << signal_
156  << " cannot be the same as the writeNowSignal."
157  << " Please change this in the etc/controlDict."
158  << exit(FatalError);
159  }
160 
161 
162  struct sigaction newAction;
163  newAction.sa_handler = sigHandler;
164  newAction.sa_flags = SA_NODEFER;
165  sigemptyset(&newAction.sa_mask);
166  if (sigaction(signal_, &newAction, &oldAction_) < 0)
167  {
169  << "Cannot set " << signal_ << " trapping"
170  << abort(FatalError);
171  }
172 
173  if (verbose)
174  {
175  Info<< "sigStopAtWriteNow :"
176  << " Enabling writing and stopping upon signal " << signal_
177  << endl;
178  }
179  }
180 }
181 
182 
184 {
185  return signal_ > 0;
186 }
187 
188 
189 // ************************************************************************* //
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
set endTime to stop immediately w/ writing
Definition: Time.H:102
#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
void signalEnd() const
Definition: jobInfo.C:210
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
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:53
static const char nl
Definition: Ostream.H:265
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