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

This commit is contained in:
Mark Olesen
2018-02-09 19:24:31 +01:00
parent 3e3c97397e
commit 88f4b6ca8d
19 changed files with 790 additions and 44 deletions

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. 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 * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::systemCall::systemCall Foam::functionObjects::systemCall::systemCall
@ -58,27 +88,27 @@ Foam::functionObjects::systemCall::systemCall
functionObject(name), functionObject(name),
executeCalls_(), executeCalls_(),
endCalls_(), endCalls_(),
writeCalls_() writeCalls_(),
masterOnly_(false)
{ {
read(dict); read(dict);
} }
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::functionObjects::systemCall::~systemCall()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::systemCall::read(const dictionary& dict) bool Foam::functionObjects::systemCall::read(const dictionary& dict)
{ {
functionObject::read(dict); functionObject::read(dict);
executeCalls_.clear();
writeCalls_.clear();
endCalls_.clear();
dict.readIfPresent("executeCalls", executeCalls_); dict.readIfPresent("executeCalls", executeCalls_);
dict.readIfPresent("endCalls", endCalls_);
dict.readIfPresent("writeCalls", writeCalls_); dict.readIfPresent("writeCalls", writeCalls_);
dict.readIfPresent("endCalls", endCalls_);
masterOnly_ = dict.lookupOrDefault("master", false);
if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty()) if (executeCalls_.empty() && endCalls_.empty() && writeCalls_.empty())
{ {
@ -89,8 +119,8 @@ bool Foam::functionObjects::systemCall::read(const dictionary& dict)
else if (!dynamicCode::allowSystemOperations) else if (!dynamicCode::allowSystemOperations)
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Executing user-supplied system calls is not enabled by " << "Executing user-supplied system calls may not be enabled by "
<< "default because of " << nl << "default due to potential " << nl
<< "security issues. If you trust the case you can enable this " << "security issues. If you trust the case you can enable this "
<< "facility by " << nl << "facility by " << nl
<< "adding to the InfoSwitches setting in the system controlDict:" << "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() bool Foam::functionObjects::systemCall::execute()
{ {
forAll(executeCalls_, calli) dispatch(executeCalls_);
{
Foam::system(executeCalls_[calli]);
}
return true;
}
bool Foam::functionObjects::systemCall::end()
{
forAll(endCalls_, calli)
{
Foam::system(endCalls_[calli]);
}
return true; return true;
} }
bool Foam::functionObjects::systemCall::write() bool Foam::functionObjects::systemCall::write()
{ {
forAll(writeCalls_, calli) dispatch(writeCalls_);
{ return true;
Foam::system(writeCalls_[calli]); }
}
bool Foam::functionObjects::systemCall::end()
{
dispatch(endCalls_);
return true; return true;
} }

View File

@ -28,7 +28,7 @@ Group
grpUtilitiesFunctionObjects grpUtilitiesFunctionObjects
Description 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: Calls can be made at the following points in the calculation:
- every time step - every time step
@ -36,7 +36,7 @@ Description
- end of the calculation - end of the calculation
Usage Usage
Example of function object specification: Example of the function object specification:
\verbatim \verbatim
systemCall1 systemCall1
{ {
@ -49,11 +49,11 @@ Usage
); );
writeCalls writeCalls
( (
"echo \*\*\* writing data \*\*\*" "echo === writing data ==="
); );
endCalls endCalls
( (
"echo \*\*\* writing .bashrc \*\*\*" "echo === echoing .bashrc ==="
"cat ~/.bashrc" "cat ~/.bashrc"
"echo \*\*\* done \*\*\*" "echo \*\*\* done \*\*\*"
); );
@ -67,6 +67,7 @@ Usage
executeCalls | list of calls on execute | yes | executeCalls | list of calls on execute | yes |
writeCalls | list of calls on write | yes | writeCalls | list of calls on write | yes |
endCalls | list of calls on end | yes | endCalls | list of calls on end | yes |
master | execute on master only | no | false
\endtable \endtable
Note Note
@ -75,6 +76,9 @@ Note
\c allowSystemOperations must be set to '1'; otherwise, system calls will \c allowSystemOperations must be set to '1'; otherwise, system calls will
not be allowed. not be allowed.
Additionally, since the system commands are normally sent via the shell,
special shell character may require backslash escaping.
See also See also
Foam::functionObject Foam::functionObject
Foam::functionObjects::timeControl Foam::functionObjects::timeControl
@ -118,16 +122,20 @@ protected:
//- List of calls to execute - write steps //- List of calls to execute - write steps
stringList writeCalls_; stringList writeCalls_;
//- Perform system calls on the master only
bool masterOnly_;
private:
// Private member functions // Protected Member Functions
//- Dispatch specified calls
label dispatch(const stringList& calls);
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
systemCall(const systemCall&); systemCall(const systemCall&) = delete;
//- Disallow default bitwise assignment //- Disallow default bitwise assignment
void operator=(const systemCall&); void operator=(const systemCall&) = delete;
public: public:
@ -148,22 +156,22 @@ public:
//- Destructor //- Destructor
virtual ~systemCall(); virtual ~systemCall() = default;
// Member Functions // Member Functions
//- Read the system calls //- Read the system calls
virtual bool read(const dictionary&); virtual bool read(const dictionary& dict);
//- Execute the "executeCalls" at each time-step //- Execute the "executeCalls" at each time-step
virtual bool execute(); virtual bool execute();
//- Execute the "endCalls" at the final time-loop
virtual bool end();
//- Write, execute the "writeCalls" //- Write, execute the "writeCalls"
virtual bool write(); virtual bool write();
//- Execute the "endCalls" at the final time-loop
virtual bool end();
}; };

View File

@ -0,0 +1,52 @@
/*--------------------------------*- 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"
}
// ************************************************************************* //

View File

@ -0,0 +1,15 @@
/*--------------------------------*- 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;
}
// ************************************************************************* //

View File

@ -0,0 +1,19 @@
/*--------------------------------*- 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;
}
// ************************************************************************* //

View File

@ -0,0 +1,14 @@
/*--------------------------------*- 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;
// ************************************************************************* //

View File

@ -0,0 +1,53 @@
/*--------------------------------*- 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"
}
// ************************************************************************* //

View File

@ -0,0 +1,64 @@
/*--------------------------------*- 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;
}
}
// ************************************************************************* //

View File

@ -0,0 +1,52 @@
/*--------------------------------*- 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"
}
// ************************************************************************* //

View File

@ -0,0 +1,51 @@
/*--------------------------------*- 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"
}
// ************************************************************************* //

13
tutorials/IO/systemCall/Allrun Executable file
View File

@ -0,0 +1,13 @@
#!/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)
#------------------------------------------------------------------------------

View File

@ -0,0 +1,21 @@
/*--------------------------------*- 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;
// ************************************************************************* //

View File

@ -0,0 +1,28 @@
/*--------------------------------*- 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;
}
// ************************************************************************* //

View File

@ -0,0 +1,86 @@
/*--------------------------------*- 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 blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
scale 1;
vertices
(
(-5 -4 0)
(15 -4 0)
(15 4 0)
(-5 4 0)
(-5 -4 8)
(15 -4 8)
(15 4 8)
(-5 4 8)
);
blocks
(
hex (0 1 2 3 4 5 6 7) (20 8 8) simpleGrading (1 1 1)
);
edges
(
);
boundary
(
frontAndBack
{
type patch;
faces
(
(3 7 6 2)
(1 5 4 0)
);
}
inlet
{
type patch;
faces
(
(0 4 7 3)
);
}
outlet
{
type patch;
faces
(
(2 6 5 1)
);
}
lowerWall
{
type wall;
faces
(
(0 3 2 1)
);
}
upperWall
{
type patch;
faces
(
(4 5 6 7)
);
}
);
// ************************************************************************* //

View File

@ -0,0 +1,53 @@
/*--------------------------------*- 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 controlDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
application simpleFoam;
startFrom latestTime;
startTime 0;
stopAt endTime;
endTime 4;
deltaT 1;
writeControl timeStep;
writeInterval 100;
purgeWrite 0;
writeFormat binary;
writePrecision 6;
writeCompression off;
timeFormat general;
timePrecision 6;
runTimeModifiable true;
functions
{
#include "systemCall"
}
// ************************************************************************* //

View File

@ -0,0 +1,26 @@
/*--------------------------------*- 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 decomposeParDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
numberOfSubdomains 6;
method hierarchical;
coeffs
{
n (3 2 1);
}
// ************************************************************************* //

View File

@ -0,0 +1,58 @@
/*--------------------------------*- 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 fvSchemes;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
ddtSchemes
{
default steadyState;
}
gradSchemes
{
default Gauss linear;
grad(U) cellLimited Gauss linear 1;
}
divSchemes
{
default none;
div(phi,U) bounded Gauss linearUpwindV grad(U);
div(phi,k) bounded Gauss upwind;
div(phi,omega) bounded Gauss upwind;
div((nuEff*dev2(T(grad(U))))) Gauss linear;
}
laplacianSchemes
{
default Gauss linear corrected;
}
interpolationSchemes
{
default linear;
}
snGradSchemes
{
default corrected;
}
wallDist
{
method meshWave;
}
// ************************************************************************* //

View File

@ -0,0 +1,86 @@
/*--------------------------------*- 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 fvSolution;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
solvers
{
p
{
solver GAMG;
smoother GaussSeidel;
tolerance 1e-7;
relTol 0.01;
}
Phi
{
$p;
}
U
{
solver smoothSolver;
smoother GaussSeidel;
tolerance 1e-8;
relTol 0.1;
nSweeps 1;
}
k
{
solver smoothSolver;
smoother GaussSeidel;
tolerance 1e-8;
relTol 0.1;
nSweeps 1;
}
omega
{
solver smoothSolver;
smoother GaussSeidel;
tolerance 1e-8;
relTol 0.1;
nSweeps 1;
}
}
SIMPLE
{
nNonOrthogonalCorrectors 0;
consistent yes;
}
potentialFlow
{
nNonOrthogonalCorrectors 10;
}
relaxationFactors
{
equations
{
U 0.9;
k 0.7;
omega 0.7;
}
}
cache
{
grad(U);
}
// ************************************************************************* //

View File

@ -0,0 +1,29 @@
// -*- C++ -*-
// An example of using systemCall
system
{
type systemCall;
libs ("libutilityFunctionObjects.so");
// Execute on the master process only
master true;
executeCalls
(
"echo execute: shell $$"
);
writeCalls
(
"echo \*\*\* writing data \*\*\*"
);
endCalls
(
"echo \*\*\* end of run \*\*\*"
"grep application system/controlDict"
"echo \*\*\* done \*\*\*"
);
}
// ************************************************************************* //