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 {
62 
63  void operator=(const addwriteNowSignalToOpt&) = delete;
64 
65  explicit addwriteNowSignalToOpt(const char* name)
66  :
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 
84 addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal");
85 
86 } // End namespace Foam
87 
88 
89 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
90 
91 void 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 
142 void 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 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::resetHandler
static void resetHandler(const char *what, int sigNum)
Definition: signalMacros.C:46
Foam::addwriteNowSignalToOpt::~addwriteNowSignalToOpt
virtual ~addwriteNowSignalToOpt()=default
Foam::addwriteNowSignalToOpt::addwriteNowSignalToOpt
addwriteNowSignalToOpt(const addwriteNowSignalToOpt &)=delete
sigWriteNow.H
Foam::sigWriteNow::sigWriteNow
sigWriteNow()
Construct null.
Definition: sigWriteNow.C:104
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Time::writeOnce
void writeOnce()
Write the objects once (one shot) and continue the run.
Definition: TimeIO.C:612
Foam::sigWriteNow::set
static void set(bool verbose=false)
Set/reset signal handler.
Definition: sigWriteNow.C:142
Foam::sigWriteNow::~sigWriteNow
~sigWriteNow()
Destructor.
Definition: sigWriteNow.C:117
Foam::addwriteNowSignalToOpt::addwriteNowSignalToOpt
addwriteNowSignalToOpt(const char *name)
Definition: sigWriteNow.C:65
Foam::sigWriteNow::active
static bool active()
Is active?
Definition: sigWriteNow.C:130
error.H
Foam::addwriteNowSignalToOpt
Definition: sigWriteNow.C:57
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
signalMacros.C
File-local code for setting/resetting signal handlers.
Foam::debug::optimisationSwitch
int optimisationSwitch(const char *name, const int deflt=0)
Lookup optimisation switch or add default value.
Definition: debug.C:237
Foam::sigWriteNow::signalNumber
static int signalNumber()
The signal number being used.
Definition: sigWriteNow.C:136
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::addwriteNowSignalToOpt::operator=
void operator=(const addwriteNowSignalToOpt &)=delete
Time.H
runTimePtr_
static Foam::Time * runTimePtr_
Definition: sigWriteNow.C:48
Foam::setHandler
static void setHandler(const char *what, int sigNum, void(*handler)(int))
Definition: signalMacros.C:61
Foam::addwriteNowSignalToOpt::readData
virtual void readData(Foam::Istream &is)
Read.
Definition: sigWriteNow.C:72
Foam::nl
constexpr char nl
Definition: Ostream.H:404
JobInfo.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::addwriteNowSignalToOpt::writeData
virtual void writeData(Foam::Ostream &os) const
Write.
Definition: sigWriteNow.C:78
Foam::debug::addOptimisationObject
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:262
Foam::simpleRegIOobject
Abstract base class for registered object with I/O. Used in debug symbol registration.
Definition: simpleRegIOobject.H:52
Foam::addwriteNowSignalToOpt_
addwriteNowSignalToOpt addwriteNowSignalToOpt_("writeNowSignal")