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-------------------------------------------------------------------------------
11License
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
43int Foam::sigStopAtWriteNow::signal_
44(
45 Foam::debug::optimisationSwitch("stopAtWriteNowSignal", -1)
46);
47
48// Pointer to Time (file-local variable)
49static Foam::Time const* runTimePtr_ = nullptr;
50
51
52// * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * //
53
54namespace Foam
55{
56// Register re-reader
58:
60{
62
64
65 explicit addstopAtWriteNowSignalToOpt(const char* name)
66 :
67 ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
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
85(
86 "stopAtWriteNowSignal"
87);
88
89} // End namespace Foam
90
91
92// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
93
94void 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// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static Foam::Time const * runTimePtr_
File-local code for setting/resetting signal handlers.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:98
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
@ saWriteNow
adjust endTime to stop immediately w/ writing
Definition: Time.H:101
virtual stopAtControls stopAt() const
Return the stop control information.
Definition: Time.C:873
static bool active()
Is active?
sigStopAtWriteNow()
Construct null.
static int signalNumber()
Signal number being used.
static int signalNumber()
The signal number being used.
Definition: sigWriteNow.C:136
Abstract base class for registered object with I/O. Used in debug symbol registration.
bool set() const
Are all the vector set.
Definition: triadI.H:76
engineTime & runTime
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
int optimisationSwitch(const char *name, const int deflt=0)
Lookup optimisation switch or add default value.
Definition: debug.C:237
Namespace for OpenFOAM.
static void setHandler(const char *what, int sigNum, void(*handler)(int))
Definition: signalMacros.C:61
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_("stopAtWriteNowSignal")
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:46
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
void operator=(const addstopAtWriteNowSignalToOpt &)=delete
addstopAtWriteNowSignalToOpt(const char *name)
virtual void readData(Foam::Istream &is)
Read.
virtual void writeData(Foam::Ostream &os) const
Write.
virtual ~addstopAtWriteNowSignalToOpt()=default
addstopAtWriteNowSignalToOpt(const addstopAtWriteNowSignalToOpt &)=delete