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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "sigWriteNow.H"
30 #include "sigStopAtWriteNow.H"
31 #include "error.H"
32 #include "JobInfo.H"
33 #include "IOstreams.H"
34 #include "Time.H"
35 
36 // File-local functions
37 #include "signalMacros.C"
38 
39 
40 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
41 
42 // Signal number to catch
43 int Foam::sigStopAtWriteNow::signal_
44 (
45  Foam::debug::optimisationSwitch("stopAtWriteNowSignal", -1)
46 );
47 
48 // Pointer to Time (file-local variable)
49 static Foam::Time const* runTimePtr_ = nullptr;
50 
51 
52 // * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 // Register re-reader
57 class addstopAtWriteNowSignalToOpt
58 :
60 {
61 public:
62 
64 
65  void operator=(const addstopAtWriteNowSignalToOpt&) = delete;
66 
67  explicit addstopAtWriteNowSignalToOpt(const char* name)
68  :
70  {}
71 
72  virtual ~addstopAtWriteNowSignalToOpt() = default;
73 
74  virtual void readData(Foam::Istream& is)
75  {
76  is >> sigStopAtWriteNow::signal_;
78  }
79 
80  virtual void writeData(Foam::Ostream& os) const
81  {
82  os << sigStopAtWriteNow::signal_;
83  }
84 };
85 
86 addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_
87 (
88  "stopAtWriteNowSignal"
89 );
90 
91 } // End namespace Foam
92 
93 
94 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
95 
96 void Foam::sigStopAtWriteNow::sigHandler(int)
97 {
98  resetHandler("stopAtWriteNow", signal_);
99 
100  jobInfo.signalEnd(); // Update jobInfo file
101 
102  if (runTimePtr_)
103  {
104  Info<< "sigStopAtWriteNow :"
105  << " setting up write and stop at end of the next iteration"
106  << nl << endl;
108  }
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
113 
115 {}
116 
117 
118 Foam::sigStopAtWriteNow::sigStopAtWriteNow(const Time& runTime, bool verbose)
119 {
120  runTimePtr_ = &runTime; // Store runTime
121  set(verbose);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
126 
128 {
129  if (!active())
130  {
131  return;
132  }
133 
134  resetHandler("stopAtWriteNow", signal_);
135 }
136 
137 
138 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
139 
141 {
142  return signal_ > 0;
143 }
144 
145 
147 {
148  return signal_;
149 }
150 
151 
152 void Foam::sigStopAtWriteNow::set(bool verbose)
153 {
154  if (!active())
155  {
156  return;
157  }
158 
159  // Check that the signal is different from the writeNowSignal
160  if (sigWriteNow::signalNumber() == signal_)
161  {
163  << "stopAtWriteNowSignal : " << signal_
164  << " cannot be the same as the writeNowSignal."
165  << " Please change this in the etc/controlDict."
166  << exit(FatalError);
167  }
168 
169  if (verbose)
170  {
171  Info<< "sigStopAtWriteNow :"
172  << " Enabling writing and stopping upon signal " << signal_
173  << endl;
174  }
175 
176  setHandler("stopAtWriteNow", signal_, sigHandler);
177 }
178 
179 
180 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::sigStopAtWriteNow::set
static void set(bool verbose=false)
Set/reset signal handler.
Definition: sigStopAtWriteNow.C:150
runTime
engineTime & runTime
Definition: createEngineTime.H:13
sigWriteNow.H
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
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:73
Foam::sigStopAtWriteNow::active
static bool active()
Is active?
Definition: sigStopAtWriteNow.C:138
Foam::resetHandler
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:46
Foam::addstopAtWriteNowSignalToOpt_
addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_("stopAtWriteNowSignal")
Foam::sigStopAtWriteNow::~sigStopAtWriteNow
~sigStopAtWriteNow()
Destructor.
Definition: sigStopAtWriteNow.C:125
Foam::addstopAtWriteNowSignalToOpt::~addstopAtWriteNowSignalToOpt
virtual ~addstopAtWriteNowSignalToOpt()=default
Foam::Time::stopAt
virtual stopAtControls stopAt() const
Return the stop control information.
Definition: Time.C:879
Foam::addstopAtWriteNowSignalToOpt::operator=
void operator=(const addstopAtWriteNowSignalToOpt &)=delete
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
error.H
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
signalMacros.C
File-local code for setting/resetting signal handlers.
sigStopAtWriteNow.H
Foam::debug::optimisationSwitch
int optimisationSwitch(const char *name, const int deflt=0)
Lookup optimisation switch or add default value.
Definition: debug.C:237
Foam::sigWriteNow::signalNumber
static int signalNumber()
The signal number being used.
Definition: sigWriteNow.C:136
Foam::FatalError
error FatalError
Foam::sigStopAtWriteNow::sigStopAtWriteNow
sigStopAtWriteNow()
Construct null.
Definition: sigStopAtWriteNow.C:112
Foam::addstopAtWriteNowSignalToOpt::readData
virtual void readData(Foam::Istream &is)
Read.
Definition: sigStopAtWriteNow.C:74
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:49
Foam::sigStopAtWriteNow::signalNumber
static int signalNumber()
Signal number being used.
Definition: sigStopAtWriteNow.C:144
Time.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
Foam::setHandler
static void setHandler(const char *what, int sigNum, void(*handler)(int))
Definition: signalMacros.C:61
Foam::Time::saWriteNow
adjust endTime to stop immediately w/ writing
Definition: Time.H:101
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::addstopAtWriteNowSignalToOpt::addstopAtWriteNowSignalToOpt
addstopAtWriteNowSignalToOpt(const char *name)
Definition: sigStopAtWriteNow.C:67
Foam::addstopAtWriteNowSignalToOpt::addstopAtWriteNowSignalToOpt
addstopAtWriteNowSignalToOpt(const addstopAtWriteNowSignalToOpt &)=delete
JobInfo.H
Foam::addstopAtWriteNowSignalToOpt::writeData
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigStopAtWriteNow.C:80
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
runTimePtr_
static Foam::Time const * runTimePtr_
Definition: sigStopAtWriteNow.C:49
Foam::JobInfo::signalEnd
void signalEnd() const
Update job info and relocate the file from running to finished.
Definition: JobInfo.C:184
Foam::debug::addOptimisationObject
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:262
Foam::simpleRegIOobject
Abstract base class for registered object with I/O. Used in debug symbol registration.
Definition: simpleRegIOobject.H:52