sigStopAtWriteNow.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-2021 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 "sigStopAtWriteNow.H"
31 #include "error.H"
32 #include "JobInfo.H"
33 #include "IOstreams.H"
34 #include "Time.H"
35 
36 // File-local functions
37 #include "signalMacros.C"
38 
39 
40 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
41 
42 // Signal number to catch
43 int Foam::sigStopAtWriteNow::signal_
44 (
45  Foam::debug::optimisationSwitch("stopAtWriteNowSignal", -1)
46 );
47 
48 // Pointer to Time (file-local variable)
49 static Foam::Time const* runTimePtr_ = nullptr;
50 
51 
52 // * * * * * * * * * * * * * * * Local Classes * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56 // Register re-reader
58 :
60 {
61 public:
62 
64 
65  void operator=(const addstopAtWriteNowSignalToOpt&) = delete;
66 
67  explicit addstopAtWriteNowSignalToOpt(const char* name)
68  :
70  {}
71 
72  virtual ~addstopAtWriteNowSignalToOpt() = default;
73 
74  virtual void readData(Foam::Istream& is)
75  {
76  is >> sigStopAtWriteNow::signal_;
78  }
79 
80  virtual void writeData(Foam::Ostream& os) const
81  {
82  os << sigStopAtWriteNow::signal_;
83  }
84 };
85 
86 addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_
87 (
88  "stopAtWriteNowSignal"
89 );
90 
91 } // End namespace Foam
92 
93 
94 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
95 
96 void Foam::sigStopAtWriteNow::sigHandler(int)
97 {
98  resetHandler("stopAtWriteNow", signal_);
99 
100  JobInfo::shutdown(); // From running -> finished
101 
102  if (runTimePtr_)
103  {
104  Info<< "sigStopAtWriteNow :"
105  << " setting up write and stop at end of the next iteration"
106  << nl << endl;
108  }
109 }
110 
111 
112 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
113 
114 Foam::sigStopAtWriteNow::sigStopAtWriteNow(const Time& runTime, bool verbose)
115 {
116  runTimePtr_ = &runTime; // Store runTime
117  set(verbose);
118 }
119 
120 
121 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
122 
124 {
125  if (!active())
126  {
127  return;
128  }
130  resetHandler("stopAtWriteNow", signal_);
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 
136 void Foam::sigStopAtWriteNow::set(bool verbose)
137 {
138  if (!active())
139  {
140  return;
141  }
142 
143  // Check that the signal is different from the writeNowSignal
144  if (sigWriteNow::signalNumber() == signal_)
145  {
147  << "stopAtWriteNowSignal : " << signal_
148  << " cannot be the same as the writeNowSignal."
149  << " Please change this in the etc/controlDict."
150  << exit(FatalError);
151  }
152 
153  if (verbose)
154  {
155  Info<< "sigStopAtWriteNow :"
156  << " Enabling writing and stopping upon signal " << signal_
157  << endl;
158  }
159 
160  setHandler("stopAtWriteNow", signal_, sigHandler);
161 }
162 
163 
164 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
virtual void readData(Foam::Istream &is)
Read.
~sigStopAtWriteNow()
Destructor.
virtual void writeData(Foam::Ostream &os) const
Write.
static void setHandler(const char *what, int sigNum, void(*handler)(int))
Definition: signalMacros.C:54
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
adjust endTime to stop immediately w/ writing
Definition: Time.H:101
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
virtual stopAtControls stopAt() const
Return the stop control information.
Definition: Time.C:801
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
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:94
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
addstopAtWriteNowSignalToOpt(const addstopAtWriteNowSignalToOpt &)=delete
virtual ~addstopAtWriteNowSignalToOpt()=default
void operator=(const addstopAtWriteNowSignalToOpt &)=delete
static int signalNumber() noexcept
The signal number being used.
Definition: sigWriteNow.H:104
addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_("stopAtWriteNowSignal")
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...
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.
static void set(bool verbose=false)
Set/reset signal handler.
static Foam::Time const * runTimePtr_
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
sigStopAtWriteNow() noexcept=default
Default construct.
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
messageStream Info
Information stream (stdout output on master, null elsewhere)
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:259
Namespace for OpenFOAM.