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-2023 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 "Time.H"
32 #include "IOstreams.H"
33 
34 // File-local functions
35 #include "signalMacros.C"
36 
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 // Signal number to catch
41 int Foam::sigWriteNow::signal_
42 (
43  Foam::debug::optimisationSwitch("writeNowSignal", -1)
44 );
45 
46 // Pointer to Time (file-local variable)
47 static Foam::Time* runTimePtr_ = nullptr;
48 
49 
50 // * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 // Register re-reader
57 :
59 {
60 public:
61 
63 
64  void operator=(const addwriteNowSignalToOpt&) = delete;
65 
66  explicit addwriteNowSignalToOpt(const char* name)
67  :
69  {}
70 
71  virtual ~addwriteNowSignalToOpt() = default;
72 
73  virtual void readData(Foam::Istream& is)
74  {
75  is >> sigWriteNow::signal_;
76  sigWriteNow::set(true);
77  }
78 
79  virtual void writeData(Foam::Ostream& os) const
80  {
81  os << sigWriteNow::signal_;
82  }
83 };
84 
85 addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal");
86 
87 } // End namespace Foam
88 
89 
90 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
91 
92 void Foam::sigWriteNow::sigHandler(int)
93 {
94  if (runTimePtr_)
95  {
96  Info<< "sigWriteNow :"
97  << " setting up write at end of the next iteration" << nl << endl;
99  }
100 }
101 
102 
103 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
104 
105 Foam::sigWriteNow::sigWriteNow(Time& runTime, bool verbose)
106 {
107  runTimePtr_ = &runTime; // Store Time reference
108  set(verbose);
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
113 
115 {
116  if (!active())
117  {
118  return;
119  }
121  resetHandler("writeNow", signal_);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
127 void Foam::sigWriteNow::set(bool verbose)
128 {
129  if (!active())
130  {
131  return;
132  }
133 
134  if (verbose)
135  {
136  Info<< "sigWriteNow :"
137  << " Enabling writing upon signal " << signal_ << nl;
138  }
139 
140  setHandler("writeNow", signal_, sigHandler);
141 }
142 
143 
144 // ************************************************************************* //
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigWriteNow.C:72
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:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
~sigWriteNow()
Destructor.
Definition: sigWriteNow.C:107
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
sigWriteNow() noexcept=default
Default construct.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
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:56
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:598
messageStream Info
Information stream (stdout output on master, null elsewhere)
addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal")
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:259
static Foam::Time * runTimePtr_
Definition: sigWriteNow.C:40
virtual void readData(Foam::Istream &is)
Read.
Definition: sigWriteNow.C:66
Namespace for OpenFOAM.
static void set(bool verbose=false)
Set/reset signal handler.
Definition: sigWriteNow.C:120