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
57class addwriteNowSignalToOpt
58:
60{
61public:
62
64
65 void operator=(const addwriteNowSignalToOpt&) = delete;
66
67 explicit addwriteNowSignalToOpt(const char* name)
68 :
69 ::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
70 {}
71
72 virtual ~addwriteNowSignalToOpt() = default;
73
74 virtual void readData(Foam::Istream& is)
75 {
76 is >> sigWriteNow::signal_;
77 sigWriteNow::set(true);
78 }
79
80 virtual void writeData(Foam::Ostream& os) const
81 {
82 os << sigWriteNow::signal_;
83 }
84};
85
86addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal");
87
88} // End namespace Foam
89
90
91// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
92
93void Foam::sigWriteNow::sigHandler(int)
94{
95 if (runTimePtr_)
96 {
97 Info<< "sigWriteNow :"
98 << " setting up write at end of the next iteration" << nl << endl;
100 }
101}
102
103
104// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
105
107{}
108
109
110Foam::sigWriteNow::sigWriteNow(Time& runTime, bool verbose)
111{
112 runTimePtr_ = &runTime; // Store runTime
113 set(verbose);
114}
115
116
117// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
118
120{
121 if (!active())
122 {
123 return;
124 }
125
126 resetHandler("writeNow", signal_);
127}
128
129
130// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
131
133{
134 return signal_ > 0;
135}
136
137
139{
140 return signal_;
141}
142
143
144void Foam::sigWriteNow::set(bool verbose)
145{
146 if (!active())
147 {
148 return;
149 }
150
151 if (verbose)
152 {
153 Info<< "sigWriteNow :"
154 << " Enabling writing upon signal " << signal_ << nl;
155 }
156
157 setHandler("writeNow", signal_, sigHandler);
158}
159
160
161// ************************************************************************* //
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)
void set(List< bool > &bools, const labelUList &locations)
Set the listed locations (assign 'true').
Definition: BitOps.C:38
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:74
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigWriteNow.C:80
virtual ~addwriteNowSignalToOpt()=default
addwriteNowSignalToOpt(const addwriteNowSignalToOpt &)=delete
addwriteNowSignalToOpt(const char *name)
Definition: sigWriteNow.C:67
void operator=(const addwriteNowSignalToOpt &)=delete