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-2021 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
58 :
60 {
62 
63  void operator=(const addstopAtWriteNowSignalToOpt&) = delete;
64 
65  explicit addstopAtWriteNowSignalToOpt(const char* name)
66  :
68  {}
69 
70  virtual ~addstopAtWriteNowSignalToOpt() = default;
71 
72  virtual void readData(Foam::Istream& is)
73  {
74  is >> sigStopAtWriteNow::signal_;
76  }
77 
78  virtual void writeData(Foam::Ostream& os) const
79  {
80  os << sigStopAtWriteNow::signal_;
81  }
82 };
83 
84 addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_
85 (
86  "stopAtWriteNowSignal"
87 );
88 
89 } // End namespace Foam
90 
91 
92 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
93 
94 void Foam::sigStopAtWriteNow::sigHandler(int)
95 {
96  resetHandler("stopAtWriteNow", signal_);
97 
98  JobInfo::shutdown(); // From running -> finished
99 
100  if (runTimePtr_)
101  {
102  Info<< "sigStopAtWriteNow :"
103  << " setting up write and stop at end of the next iteration"
104  << nl << endl;
106  }
107 }
108 
109 
110 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
111 
113 {}
114 
115 
117 {
118  runTimePtr_ = &runTime; // Store runTime
119  set(verbose);
120 }
121 
122 
123 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
124 
126 {
127  if (!active())
128  {
129  return;
130  }
131 
132  resetHandler("stopAtWriteNow", signal_);
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
139 {
140  return signal_ > 0;
141 }
142 
143 
145 {
146  return signal_;
147 }
148 
149 
151 {
152  if (!active())
153  {
154  return;
155  }
156 
157 
158  // Check that the signal is different from the writeNowSignal
159  if (sigWriteNow::signalNumber() == signal_)
160  {
162  << "stopAtWriteNowSignal : " << signal_
163  << " cannot be the same as the writeNowSignal."
164  << " Please change this in the etc/controlDict."
165  << exit(FatalError);
166  }
167 
168  if (verbose)
169  {
170  Info<< "sigStopAtWriteNow :"
171  << " Enabling writing and stopping upon signal " << signal_
172  << endl;
173  }
174 
175  setHandler("stopAtWriteNow", signal_, sigHandler);
176 }
177 
178 
179 // ************************************************************************* //
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
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
sigWriteNow.H
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:369
Foam::addstopAtWriteNowSignalToOpt
Definition: sigStopAtWriteNow.C:57
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 (stdout output on master, null elsewhere)
runTimePtr_
static Foam::Time const * runTimePtr_
Definition: sigStopAtWriteNow.C:49
signalMacros.C
File-local code for setting/resetting signal handlers.
Foam::debug::optimisationSwitch
int optimisationSwitch(const char *name, const int deflt=0)
Lookup optimisation switch or add default value.
Definition: debug.C:237
Foam::JobInfo::shutdown
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:98
Foam::sigWriteNow::signalNumber
static int signalNumber()
The signal number being used.
Definition: sigWriteNow.C:136
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam::sigStopAtWriteNow::sigStopAtWriteNow
sigStopAtWriteNow()
Construct null.
Definition: sigStopAtWriteNow.C:112
Foam::addstopAtWriteNowSignalToOpt::readData
virtual void readData(Foam::Istream &is)
Read.
Definition: sigStopAtWriteNow.C:72
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::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:453
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:404
Foam::addstopAtWriteNowSignalToOpt::addstopAtWriteNowSignalToOpt
addstopAtWriteNowSignalToOpt(const char *name)
Definition: sigStopAtWriteNow.C:65
Foam::addstopAtWriteNowSignalToOpt::addstopAtWriteNowSignalToOpt
addstopAtWriteNowSignalToOpt(const addstopAtWriteNowSignalToOpt &)=delete
sigStopAtWriteNow.H
JobInfo.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::addstopAtWriteNowSignalToOpt::writeData
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigStopAtWriteNow.C:78
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
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