sigWriteNow.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-------------------------------------------------------------------------------
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 "error.H"
31#include "JobInfo.H"
32#include "IOstreams.H"
33#include "Time.H"
34
35// File-local functions
36#include "signalMacros.C"
37
38
39// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40
41// Signal number to catch
42int Foam::sigWriteNow::signal_
43(
44 Foam::debug::optimisationSwitch("writeNowSignal", -1)
45);
46
47// Pointer to Time (file-local variable)
48static Foam::Time* runTimePtr_ = nullptr;
49
50
51// * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * //
52
53namespace Foam
54{
55
56// Register re-reader
58:
60{
62
63 void operator=(const addwriteNowSignalToOpt&) = delete;
64
65 explicit addwriteNowSignalToOpt(const char* name)
66 :
67 ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
68 {}
69
70 virtual ~addwriteNowSignalToOpt() = default;
71
72 virtual void readData(Foam::Istream& is)
73 {
74 is >> sigWriteNow::signal_;
75 sigWriteNow::set(true);
76 }
77
78 virtual void writeData(Foam::Ostream& os) const
79 {
80 os << sigWriteNow::signal_;
81 }
82};
83
85
86} // End namespace Foam
87
88
89// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
90
91void Foam::sigWriteNow::sigHandler(int)
92{
93 if (runTimePtr_)
94 {
95 Info<< "sigWriteNow :"
96 << " setting up write at end of the next iteration" << nl << endl;
98 }
99}
100
101
102// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
103
105{}
106
107
109{
110 runTimePtr_ = &runTime; // Store runTime
111 set(verbose);
112}
113
114
115// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
116
118{
119 if (!active())
120 {
121 return;
122 }
123
124 resetHandler("writeNow", signal_);
125}
126
127
128// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129
131{
132 return signal_ > 0;
133}
134
135
137{
138 return signal_;
139}
140
141
142void Foam::sigWriteNow::set(bool verbose)
143{
144 if (!active())
145 {
146 return;
147 }
148
149 if (verbose)
150 {
151 Info<< "sigWriteNow :"
152 << " Enabling writing upon signal " << signal_ << nl;
153 }
154
155 setHandler("writeNow", signal_, sigHandler);
156}
157
158
159// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static Foam::Time const * runTimePtr_
static Foam::Time * runTimePtr_
Definition: sigWriteNow.C:48
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
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
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:612
static bool active()
Is active?
Definition: sigWriteNow.C:130
~sigWriteNow()
Destructor.
Definition: sigWriteNow.C:117
static int signalNumber()
The signal number being used.
Definition: sigWriteNow.C:136
sigWriteNow()
Construct null.
Definition: sigWriteNow.C:104
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
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.
addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal")
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
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:46
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
virtual void readData(Foam::Istream &is)
Read.
Definition: sigWriteNow.C:72
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigWriteNow.C:78
virtual ~addwriteNowSignalToOpt()=default
addwriteNowSignalToOpt(const addwriteNowSignalToOpt &)=delete
addwriteNowSignalToOpt(const char *name)
Definition: sigWriteNow.C:65
void operator=(const addwriteNowSignalToOpt &)=delete