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 -------------------------------------------------------------------------------
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 "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
42 int Foam::sigWriteNow::signal_
43 (
44  Foam::debug::optimisationSwitch("writeNowSignal", -1)
45 );
46 
47 // Pointer to Time (file-local variable)
48 static Foam::Time* runTimePtr_ = nullptr;
49 
50 
51 // * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Register re-reader
58 :
60 {
61 public:
62 
64 
65  void operator=(const addwriteNowSignalToOpt&) = delete;
66 
67  explicit addwriteNowSignalToOpt(const char* name)
68  :
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 
86 addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal");
87 
88 } // End namespace Foam
89 
90 
91 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
92 
93 void 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 
104 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
105 
107 {}
108 
109 
111 {
112  runTimePtr_ = &runTime; // Store runTime
113  set(verbose);
114 }
115 
116 
117 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
118 
120 {
121  if (!active())
122  {
123  return;
124  }
126  resetHandler("writeNow", signal_);
127 }
128 
129 
130 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 {
134  return signal_ > 0;
135 }
136 
139 {
140  return signal_;
141 }
142 
143 
144 void 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 // ************************************************************************* //
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigWriteNow.C:73
Abstract base class for registered object with I/O. Used in debug symbol registration.
static void setHandler(const char *what, int sigNum, void(*handler)(int))
Definition: signalMacros.C:54
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
~sigWriteNow()
Destructor.
Definition: sigWriteNow.C:112
addwriteNowSignalToOpt(const addwriteNowSignalToOpt &)=delete
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:42
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
virtual ~addwriteNowSignalToOpt()=default
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
File-local code for setting/resetting signal handlers.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
int optimisationSwitch(const char *name, const int deflt=0)
Lookup optimisation switch or add default value.
Definition: debug.C:234
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
void operator=(const addwriteNowSignalToOpt &)=delete
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:614
messageStream Info
Information stream (stdout output on master, null elsewhere)
addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal")
static bool active()
Is active?
Definition: sigWriteNow.C:125
static int signalNumber()
The signal number being used.
Definition: sigWriteNow.C:131
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:259
static Foam::Time * runTimePtr_
Definition: sigWriteNow.C:41
sigWriteNow()
Construct null.
Definition: sigWriteNow.C:99
virtual void readData(Foam::Istream &is)
Read.
Definition: sigWriteNow.C:67
Namespace for OpenFOAM.
static void set(bool verbose=false)
Set/reset signal handler.
Definition: sigWriteNow.C:137