systemCall.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-2023 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 "systemCall.H"
27 #include "Time.H"
28 #include "dynamicCode.H"
29 #include "OSspecific.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
39 
41  (
43  systemCall,
45  );
46 }
47 }
48 
49 
50 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
51 
53 (
54  const word& name,
55  const Time& time,
56  const dictionary& dict
57 )
58 :
60  parallel_(false),
61  executeCalls_(),
62  endCalls_(),
63  writeCalls_()
64 {
65  read(dict);
66 }
67 
68 
69 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
70 
72 {}
73 
74 
75 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
76 
78 {
79  parallel_ = dict.lookupOrDefault("parallel", false);
80 
81  dict.readIfPresent("executeCalls", executeCalls_);
82  dict.readIfPresent("endCalls", endCalls_);
83  dict.readIfPresent("writeCalls", writeCalls_);
84 
85  if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty())
86  {
88  << "no executeCalls, endCalls or writeCalls defined."
89  << endl;
90  }
92  {
94  << "Executing user-supplied system calls is not enabled by "
95  << "default because of " << nl
96  << "security issues. If you trust the case you can enable this "
97  << "facility by " << nl
98  << "adding to the InfoSwitches setting in the system controlDict:"
99  << nl << nl
100  << " allowSystemOperations 1" << nl << nl
101  << "The system controlDict is either" << nl << nl
102  << " ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" << nl << nl
103  << "or" << nl << nl
104  << " $WM_PROJECT_DIR/etc/controlDict" << nl << nl
105  << exit(FatalError);
106  }
107 
108  return true;
109 }
110 
111 
113 {
114  if (Pstream::master() || parallel_)
115  {
116  forAll(executeCalls_, callI)
117  {
118  Foam::system(executeCalls_[callI]);
119  }
120  }
121 
122  return true;
123 }
124 
125 
127 {
128  if (Pstream::master() || parallel_)
129  {
130  forAll(endCalls_, callI)
131  {
132  Foam::system(endCalls_[callI]);
133  }
134  }
135 
136  return true;
137 }
138 
139 
141 {
142  if (Pstream::master() || parallel_)
143  {
144  forAll(writeCalls_, callI)
145  {
146  Foam::system(writeCalls_[callI]);
147  }
148  }
149 
150  return true;
151 }
152 
153 
154 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
Macros for easy insertion into run-time selection tables.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
static int allowSystemOperations
Flag if system operations are allowed.
Definition: dynamicCode.H:156
Abstract base-class for Time/database functionObjects.
Executes system calls, entered in the form of a string lists.
Definition: systemCall.H:136
virtual ~systemCall()
Destructor.
Definition: systemCall.C:71
systemCall(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: systemCall.C:53
virtual bool execute()
Execute the "executeCalls" at each time-step.
Definition: systemCall.C:112
virtual bool write()
Write, execute the "writeCalls".
Definition: systemCall.C:140
virtual bool end()
Execute the "endCalls" at the final time-loop.
Definition: systemCall.C:126
virtual bool read(const dictionary &)
Read the system calls.
Definition: systemCall.C:77
Writes run time, CPU time and clock time and optionally the CPU and clock times per time step.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:334
#define WarningInFunction
Report a warning using Foam::Warning.
defineTypeNameAndDebug(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1230
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
error FatalError
static const char nl
Definition: Ostream.H:266
dictionary dict