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-2015 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
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  {
57  }
58  virtual void writeData(Foam::Ostream& os) const
59  {
61  }
62 };
63 addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_
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 
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 controlDict ("
149  << findEtcFile("controlDict", false) << ")."
150  << exit(FatalError);
151  }
152 
153 
154  struct sigaction newAction;
155  newAction.sa_handler = sigHandler;
156  newAction.sa_flags = SA_NODEFER;
157  sigemptyset(&newAction.sa_mask);
158  if (sigaction(signal_, &newAction, &oldAction_) < 0)
159  {
161  << "Cannot set " << signal_ << " trapping"
162  << abort(FatalError);
163  }
164 
165  if (verbose)
166  {
167  Info<< "sigStopAtWriteNow :"
168  << " Enabling writing and stopping upon signal " << signal_
169  << endl;
170  }
171  }
172 }
173 
174 
176 {
177  return signal_ > 0;
178 }
179 
180 
181 // ************************************************************************* //
Foam::sigWriteNow::signal_
static int signal_
Number of signal to use.
Definition: sigWriteNow.H:57
Foam::findEtcFile
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: POSIX.C:404
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::addstopAtWriteNowSignalToOpt_
addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_("stopAtWriteNowSignal")
Foam::sigStopAtWriteNow::~sigStopAtWriteNow
~sigStopAtWriteNow()
Destructor.
Definition: sigStopAtWriteNow.C:121
Foam::addstopAtWriteNowSignalToOpt::~addstopAtWriteNowSignalToOpt
virtual ~addstopAtWriteNowSignalToOpt()
Definition: sigStopAtWriteNow.C:51
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::sigStopAtWriteNow::signal_
static int signal_
Number of signal to use.
Definition: sigStopAtWriteNow.H:58
Foam::debug::optimisationSwitch
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:182
Foam::addstopAtWriteNowSignalToOpt::readData
virtual void readData(Foam::Istream &is)
Read.
Definition: sigStopAtWriteNow.C:53
error.H
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
runTimePtr_
static Foam::Time const * runTimePtr_
Definition: sigStopAtWriteNow.C:70
Foam::FatalError
error FatalError
Foam::sigStopAtWriteNow::sigStopAtWriteNow
sigStopAtWriteNow()
Construct null.
Definition: sigStopAtWriteNow.C:103
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
sigStopAtWriteNow.H
Foam::addstopAtWriteNowSignalToOpt::writeData
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigStopAtWriteNow.C:58
Foam::sigStopAtWriteNow::sigHandler
static void sigHandler(int)
Definition: sigStopAtWriteNow.C:78
Foam::sigStopAtWriteNow::set
static void set(const bool verbose)
(re)set signal catcher
Definition: sigStopAtWriteNow.C:138
Foam::Time::stopAt
virtual bool stopAt(const stopAtControls) const
Adjust the current stopAtControl. Note that this value.
Definition: Time.C:946
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:35
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::addstopAtWriteNowSignalToOpt::addstopAtWriteNowSignalToOpt
addstopAtWriteNowSignalToOpt(const char *name)
Definition: sigStopAtWriteNow.C:47
Foam::readLabel
label readLabel(Istream &is)
Definition: label.H:64
JobInfo.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::JobInfo::signalEnd
void signalEnd() const
Definition: JobInfo.C:176
Foam::sigStopAtWriteNow::active
bool active() const
Is active?
Definition: sigStopAtWriteNow.C:175
Foam::debug::addOptimisationObject
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:234
Foam::simpleRegIOobject
Abstract base class for registered object with I/O. Used in debug symbol registration.
Definition: simpleRegIOobject.H:50
Foam::addstopAtWriteNowSignalToOpt
Definition: sigStopAtWriteNow.C:42
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47