sigStopAtWriteNow.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "sigStopAtWriteNow.H"
27 #include "error.H"
28 #include "jobInfo.H"
29 #include "IOstreams.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 // Signal number to catch
38 int sigStopAtWriteNow::signal_
39 (
40  debug::optimisationSwitch("stopAtWriteNowSignal", -1)
41 );
42 
43 } // End namespace Foam
44 
45 
46 Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr;
47 struct sigaction Foam::sigStopAtWriteNow::oldAction_;
48 
49 
50 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
51 
52 void Foam::sigStopAtWriteNow::sigHandler(int)
53 {
54  // Reset old handling
55  if (sigaction(signal_, &oldAction_, nullptr) < 0)
56  {
58  << "Cannot reset " << signal_ << " trapping"
59  << abort(FatalError);
60  }
61 
62  // Update jobInfo file
64 
65  Info<< "sigStopAtWriteNow :"
66  << " setting up write and stop at end of the next iteration"
67  << nl << endl;
68  runTimePtr_->stopAt(Time::stopAtControl::writeNow);
69 
71  // raise(signal_);
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
76 
78 
79 
81 (
82  const bool verbose,
83  const Time& runTime
84 )
85 {
86  // Store runTime
87  runTimePtr_ = &runTime;
88 
89  set(verbose);
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
94 
96 {
97  // Reset old handling
98  if (signal_ > 0)
99  {
100  if (sigaction(signal_, &oldAction_, nullptr) < 0)
101  {
103  << "Cannot reset " << signal_ << " trapping"
104  << abort(FatalError);
105  }
106  }
107 }
108 
109 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 
112 void Foam::sigStopAtWriteNow::set(const bool verbose)
113 {
114  if (signal_ > 0)
115  {
116  // Check that the signal is different from the writeNowSignal
117  if (sigWriteNow::signal_ == signal_)
118  {
120  << "stopAtWriteNowSignal : " << signal_
121  << " cannot be the same as the writeNowSignal."
122  << " Please change this in the etc/controlDict."
123  << exit(FatalError);
124  }
125 
126 
127  struct sigaction newAction;
128  newAction.sa_handler = sigHandler;
129  newAction.sa_flags = SA_NODEFER;
130  sigemptyset(&newAction.sa_mask);
131  if (sigaction(signal_, &newAction, &oldAction_) < 0)
132  {
134  << "Cannot set " << signal_ << " trapping"
135  << abort(FatalError);
136  }
137 
138  if (verbose)
139  {
140  Info<< "sigStopAtWriteNow :"
141  << " Enabling writing and stopping upon signal " << signal_
142  << endl;
143  }
144  }
145 }
146 
147 
149 {
150  return signal_ > 0;
151 }
152 
153 
154 // ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
void signalEnd() const
Definition: jobInfo.C:210
sigStopAtWriteNow()
Construct null.
bool active() const
Is active?
static void set(const bool verbose)
(re)set signal catcher
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:255
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
jobInfo jobInfo_
Definition: jobInfo.C:44
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
error FatalError
static const char nl
Definition: Ostream.H:260