Commit 88f4b6ca authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: optionally limit systemCall function-object to master only (closes #729)

parent 3e3c9739
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -46,6 +46,36 @@ namespace functionObjects
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
Foam::label Foam::functionObjects::systemCall::dispatch(const stringList& calls)
{
if (calls.empty())
{
return 0;
}
label nCalls = 0;
if (!masterOnly_ || Pstream::master())
{
for (const string& call : calls)
{
Foam::system(call); // Handles empty command as a successful no-op.
++nCalls;
}
}
// MPI barrier
if (masterOnly_)
{
Pstream::scatter(nCalls);
}
return nCalls;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::systemCall::systemCall
......@@ -58,27 +88,27 @@ Foam::functionObjects::systemCall::systemCall
functionObject(name),
executeCalls_(),
endCalls_(),
writeCalls_()
writeCalls_(),
masterOnly_(false)
{
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::systemCall::~systemCall()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::systemCall::read(const dictionary& dict)
{
functionObject::read(dict);
executeCalls_.clear();
writeCalls_.clear();
endCalls_.clear();
dict.readIfPresent("executeCalls", executeCalls_);
dict.readIfPresent("endCalls", endCalls_);
dict.readIfPresent("writeCalls", writeCalls_);
dict.readIfPresent("endCalls", endCalls_);
masterOnly_ = dict.lookupOrDefault("master", false);
if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty())
{
......@@ -89,8 +119,8 @@ bool Foam::functionObjects::systemCall::read(const dictionary& dict)
else if (!dynamicCode::allowSystemOperations)
{
FatalErrorInFunction
<< "Executing user-supplied system calls is not enabled by "
<< "default because of " << nl
<< "Executing user-supplied system calls may not be enabled by "
<< "default due to potential " << nl
<< "security issues. If you trust the case you can enable this "
<< "facility by " << nl
<< "adding to the InfoSwitches setting in the system controlDict:"
......@@ -109,33 +139,21 @@ bool Foam::functionObjects::systemCall::read(const dictionary& dict)
bool Foam::functionObjects::systemCall::execute()
{
forAll(executeCalls_, calli)
{
Foam::system(executeCalls_[calli]);
}
dispatch(executeCalls_);
return true;
}
bool Foam::functionObjects::systemCall::end()
bool Foam::functionObjects::systemCall::write()
{
forAll(endCalls_, calli)
{
Foam::system(endCalls_[calli]);
}
dispatch(writeCalls_);
return true;
}
bool Foam::functionObjects::systemCall::write()
bool Foam::functionObjects::systemCall::end()
{
forAll(writeCalls_, calli)
{
Foam::system(writeCalls_[calli]);
}
dispatch(endCalls_);
return true;
}
......
......@@ -28,7 +28,7 @@ Group
grpUtilitiesFunctionObjects
Description
Executes system calls, entered in the form of a string lists.
Executes system calls, entered in the form of string lists.
Calls can be made at the following points in the calculation:
- every time step
......@@ -36,7 +36,7 @@ Description
- end of the calculation
Usage
Example of function object specification:
Example of the function object specification:
\verbatim
systemCall1
{
......@@ -49,11 +49,11 @@ Usage
);
writeCalls
(
"echo \*\*\* writing data \*\*\*"
"echo === writing data ==="
);
endCalls
(
"echo \*\*\* writing .bashrc \*\*\*"
"echo === echoing .bashrc ==="
"cat ~/.bashrc"
"echo \*\*\* done \*\*\*"
);
......@@ -67,6 +67,7 @@ Usage
executeCalls | list of calls on execute | yes |
writeCalls | list of calls on write | yes |
endCalls | list of calls on end | yes |
master | execute on master only | no | false
\endtable
Note
......@@ -75,6 +76,9 @@ Note
\c allowSystemOperations must be set to '1'; otherwise, system calls will
not be allowed.
Additionally, since the system commands are normally sent via the shell,
special shell character may require backslash escaping.
See also
Foam::functionObject
Foam::functionObjects::timeControl
......@@ -118,16 +122,20 @@ protected:
//- List of calls to execute - write steps
stringList writeCalls_;
//- Perform system calls on the master only
bool masterOnly_;
private:
// Protected Member Functions
// Private member functions
//- Dispatch specified calls
label dispatch(const stringList& calls);
//- Disallow default bitwise copy construct
systemCall(const systemCall&);
systemCall(const systemCall&) = delete;
//- Disallow default bitwise assignment
void operator=(const systemCall&);
void operator=(const systemCall&) = delete;
public:
......@@ -148,22 +156,22 @@ public:
//- Destructor
virtual ~systemCall();
virtual ~systemCall() = default;
// Member Functions
//- Read the system calls
virtual bool read(const dictionary&);
virtual bool read(const dictionary& dict);
//- Execute the "executeCalls" at each time-step
virtual bool execute();
//- Execute the "endCalls" at the final time-loop
virtual bool end();
//- Write, execute the "writeCalls"
virtual bool write();
//- Execute the "endCalls" at the final time-loop
virtual bool end();
};
......
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class volVectorField;
location "0";
object U;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "include/initialConditions"
dimensions [0 1 -1 0 0 0 0];
internalField uniform $flowVelocity;
boundaryField
{
#includeEtc "caseDicts/setConstraintTypes"
#include "include/fixedInlet"
outlet
{
type inletOutlet;
inletValue uniform (0 0 0);
value $internalField;
}
lowerWall
{
type fixedValue;
value $internalField;
}
motorBikeGroup
{
type noSlip;
}
#include "include/frontBackUpperPatches"
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
inlet
{
type fixedValue;
value $internalField;
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
upperWall
{
type slip;
}
frontAndBack
{
type slip;
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
flowVelocity (20 0 0);
pressure 0;
turbulentKE 0.24;
turbulentOmega 1.78;
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class volScalarField;
object k;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "include/initialConditions"
dimensions [0 2 -2 0 0 0 0];
internalField uniform $turbulentKE;
boundaryField
{
#includeEtc "caseDicts/setConstraintTypes"
//- Define inlet conditions
#include "include/fixedInlet"
outlet
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
lowerWall
{
type kqRWallFunction;
value $internalField;
}
motorBikeGroup
{
type kqRWallFunction;
value $internalField;
}
#include "include/frontBackUpperPatches"
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class volScalarField;
location "0";
object nut;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 2 -1 0 0 0 0];
internalField uniform 0;
boundaryField
{
#includeEtc "caseDicts/setConstraintTypes"
frontAndBack
{
type calculated;
value uniform 0;
}
inlet
{
type calculated;
value uniform 0;
}
outlet
{
type calculated;
value uniform 0;
}
lowerWall
{
type nutkWallFunction;
value uniform 0;
}
upperWall
{
type calculated;
value uniform 0;
}
motorBikeGroup
{
type nutkWallFunction;
value uniform 0;
}
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class volScalarField;
object omega;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "include/initialConditions"
dimensions [0 0 -1 0 0 0 0];
internalField uniform $turbulentOmega;
boundaryField
{
#includeEtc "caseDicts/setConstraintTypes"
#include "include/fixedInlet"
outlet
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
lowerWall
{
type omegaWallFunction;
value $internalField;
}
motorBikeGroup
{
type omegaWallFunction;
value $internalField;
}
#include "include/frontBackUpperPatches"
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class volScalarField;
object p;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "include/initialConditions"
dimensions [0 2 -2 0 0 0 0];
internalField uniform $pressure;
boundaryField
{
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type zeroGradient;
}
outlet
{
type fixedValue;
value $internalField;
}
lowerWall
{
type zeroGradient;
}
motorBikeGroup
{
type zeroGradient;
}
#include "include/frontBackUpperPatches"
}
// ************************************************************************* //
#!/bin/sh
cd ${0%/*} || exit 1 # Run from this directory
. $WM_PROJECT_DIR/bin/tools/RunFunctions # Tutorial run functions
runApplication blockMesh
runApplication decomposePar
#- For parallel running: set the initial fields
restore0Dir -processor
runParallel $(getApplication)
#------------------------------------------------------------------------------
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object transportProperties;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
transportModel Newtonian;
nu 1.5e-05;
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object turbulenceProperties;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
simulationType RAS;
RAS
{
RASModel kOmegaSST;
turbulence on;
printCoeffs on;
}
// ************************************************************************* //
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: plus |