Commit a85c55bb authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: ensure that content changes in coded objects are noticed (#1293)

- for codedFunctionObject and CodedSource the main code snippets
  were not included in the SHA1 calculation, which meant that many
  changes would not be noticed and no new library would be compiled.

  As a workaround, a dummy 'code' entry could be used solely for the
  purposes of generating a SHA1, but this is easily forgotten.

  We now allow tracking of the dynamicCodeContext for the coded
  objects and append to the SHA1 hasher with specific entries.
  This should solve the previous misbehaviour.

  We additionally add information about the ordering of the code
  sections. Suppose we have a coded function object (all code
  segments are optional) with the following:

      codeExecute "";
      codeWrite   #{ Info<< "Called\n"; #};

  which we subsequently change to this:

      codeExecute #{ Info<< "Called\n"; #};
      codeWrite   "";

  If the code strings are simply concatenated together, the SHA1 hashes
  will be identical. We thus 'salt' with their semantic locations,
  choosing tags that are unlikely to occur within the code strings
  themselves.

- simplify the coded templates with constexpr for the SHA1sum
  information.

- Correct the CodedSource to use 'codeConstrain' instead of
  'codeSetValue' for consistency with the underlying functions.
parent 23e5d43e
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -51,18 +51,11 @@ ${localCode}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
extern "C"
extern "C" void ${typeName}(Ostream& os, const dictionary& dict)
{
void ${typeName}
(
Ostream& os,
const dictionary& dict
)
{
//{{{ begin code
${code}
${code}
//}}} end code
}
}
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -42,7 +42,6 @@ ${codeInclude}
namespace Foam
{
namespace fv
{
......@@ -55,33 +54,26 @@ ${localCode}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
extern "C"
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
extern "C" void ${typeName}_${SHA1sum}(bool load)
{
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
void ${typeName}_${SHA1sum}(bool load)
if (load)
{
// Code that can be explicitly executed after loading
}
else
{
if (load)
{
// code that can be explicitly executed after loading
}
else
{
// code that can be explicitly executed before unloading
}
// Code that can be explicitly executed before unloading
}
}
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
//makeRemovablePatchTypeField
//(
// fvPatch${FieldType},
// ${typeName}FvOption${SourceType}
//);
defineTypeNameAndDebug(${typeName}FvOption${SourceType}, 0);
addRemovableToRunTimeSelectionTable
(
......@@ -91,10 +83,6 @@ addRemovableToRunTimeSelectionTable
);
const char* const ${typeName}FvOption${SourceType}::SHA1sum =
"${SHA1sum}";
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
${typeName}FvOption${SourceType}::
......@@ -110,8 +98,7 @@ ${typeName}FvOption${SourceType}
{
if (${verbose:-false})
{
Info<<"construct ${typeName} sha1: ${SHA1sum}"
" from components\n";
printMessage("Construct ${typeName} from components");
}
}
......@@ -123,7 +110,7 @@ ${typeName}FvOption${SourceType}::
{
if (${verbose:-false})
{
Info<<"destroy ${typeName} sha1: ${SHA1sum}\n";
printMessage("Destroy ${typeName}");
}
}
......@@ -193,14 +180,14 @@ void ${typeName}FvOption${SourceType}::constrain
}
//{{{ begin code
${codeSetValue}
${codeConstrain}
//}}} end code
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace fv
} // End namespace Foam
} // End namespace fv
// ************************************************************************* //
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -27,6 +27,7 @@ Description
Template for use with dynamic code generation of a source.
The hook functions take the following arguments:
\verbatim
codeCorrect
(
GeometricField<Type, fvPatchField, volMesh>& fld
......@@ -38,16 +39,18 @@ Description
const label fieldi
)
constrain
codeConstrain
(
fvMatrix<Type>& eqn,
const label fieldi
)
\endverbatim
where :
fieldi is the index in the fields entry
eqn is the fvMatrix
\verbatim
energySource
{
type scalarCodedSource;
......@@ -62,7 +65,6 @@ Description
codeInclude
#{
#};
codeCorrect
......@@ -78,18 +80,9 @@ Description
heSource -= 0.1*sqr(time.value())*V;
#};
codeSetValue
#{
Pout<< "**codeSetValue**" << endl;
#};
// Dummy entry. Make dependent on above to trigger recompilation
code
codeConstrain
#{
$codeInclude
$codeCorrect
$codeAddSup
$codeSetValue
Pout<< "**codeConstrain**" << endl;
#};
}
......@@ -98,6 +91,7 @@ Description
// Dummy entry
}
}
\endverbatim
SourceFiles
codedFvOptionTemplate.C
......@@ -113,7 +107,6 @@ SourceFiles
namespace Foam
{
namespace fv
{
......@@ -125,10 +118,18 @@ class ${typeName}FvOption${SourceType}
:
public cellSetOption
{
// Private Member Functions
//- Report a message with the SHA1sum
inline static void printMessage(const char* message)
{
Info<< message << " sha1: " << SHA1sum << '\n';
}
public:
//- Information about the SHA1 of the code itself
static const char* const SHA1sum;
//- SHA1 representation of the code content
static constexpr const char* const SHA1sum = "${SHA1sum}";
//- Runtime type information
TypeName("${typeName}");
......@@ -149,44 +150,42 @@ public:
virtual ~${typeName}FvOption${SourceType}();
// Member functions
//- Correct field
virtual void correct
(
GeometricField<${TemplateType}, fvPatchField, volMesh>&
);
//- Explicit and implicit matrix contributions
virtual void addSup
(
fvMatrix<${TemplateType}>& eqn,
const label fieldi
);
//- Explicit and implicit matrix contributions for compressible
// equations
virtual void addSup
(
const volScalarField& rho,
fvMatrix<${TemplateType}>& eqn,
const label fieldi
);
//- Set value
virtual void constrain
(
fvMatrix<${TemplateType}>& eqn,
const label fieldi
);
// Member Functions
//- Correct field
virtual void correct
(
GeometricField<${TemplateType}, fvPatchField, volMesh>& fld
);
//- Explicit/implicit matrix contributions
virtual void addSup
(
fvMatrix<${TemplateType}>& eqn,
const label fieldi
);
//- Explicit/implicit matrix contributions for compressible equations
virtual void addSup
(
const volScalarField& rho,
fvMatrix<${TemplateType}>& eqn,
const label fieldi
);
//- Set value
virtual void constrain
(
fvMatrix<${TemplateType}>& eqn,
const label fieldi
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
} // End namespace fv
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -49,23 +49,20 @@ addRemovableToRunTimeSelectionTable
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
extern "C"
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
extern "C" void ${typeName}_${SHA1sum}(bool load)
{
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
void ${typeName}_${SHA1sum}(bool load)
if (load)
{
if (load)
{
// code that can be explicitly executed after loading
}
else
{
// code that can be explicitly executed before unloading
}
// Code that can be explicitly executed after loading
}
else
{
// Code that can be explicitly executed before unloading
}
}
......@@ -101,7 +98,7 @@ tmp<pointField> ${typeName}Points0MotionSolver::curPoints() const
{
if (${verbose:-false})
{
Info<<"curPoints ${typeName} sha1: ${SHA1sum}\n";
printMessage("curPoints ${typeName}");
}
//{{{ begin code
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -56,6 +56,12 @@ class ${typeName}Points0MotionSolver
{
// Private Member Functions
//- Report a message with the SHA1sum
inline static void printMessage(const char* message)
{
Info<< message << " sha1: " << SHA1sum << '\n';
}
//- No copy construct
${typeName}Points0MotionSolver
(
......@@ -68,6 +74,9 @@ class ${typeName}Points0MotionSolver
public:
//- SHA1 representation of the code content
static constexpr const char* const SHA1sum = "${SHA1sum}";
//- Runtime type information
TypeName("${typeName}");
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -31,6 +31,7 @@ License
#include "volFields.H"
#include "surfaceFields.H"
#include "unitConversion.H"
//{{{ begin codeInclude
${codeInclude}
//}}} end codeInclude
......@@ -50,23 +51,20 @@ ${localCode}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
extern "C"
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
extern "C" void ${typeName}_${SHA1sum}(bool load)
{
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
void ${typeName}_${SHA1sum}(bool load)
if (load)
{
// Code that can be explicitly executed after loading
}
else
{
if (load)
{
// code that can be explicitly executed after loading
}
else
{
// code that can be explicitly executed before unloading
}
// Code that can be explicitly executed before unloading
}
}
......@@ -79,10 +77,6 @@ makeRemovablePatchTypeField
);
const char* const ${typeName}FixedValueFvPatch${FieldType}::SHA1sum =
"${SHA1sum}";
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
${typeName}FixedValueFvPatch${FieldType}::
......@@ -96,8 +90,7 @@ ${typeName}FixedValueFvPatch${FieldType}
{
if (${verbose:-false})
{
Info<<"construct ${typeName} sha1: ${SHA1sum}"
" from patch/DimensionedField\n";
printMessage("Construct ${typeName} : patch/DimensionedField");
}
}
......@@ -115,8 +108,7 @@ ${typeName}FixedValueFvPatch${FieldType}
{
if (${verbose:-false})
{
Info<<"construct ${typeName} sha1: ${SHA1sum}"
" from patch/DimensionedField/mapper\n";
printMessage("Construct ${typeName} : patch/DimensionedField/mapper");
}
}
......@@ -133,8 +125,7 @@ ${typeName}FixedValueFvPatch${FieldType}
{
if (${verbose:-false})
{
Info<<"construct ${typeName} sha1: ${SHA1sum}"
" from patch/dictionary\n";
printMessage("Construct ${typeName} : patch/dictionary");
}
}
......@@ -149,8 +140,7 @@ ${typeName}FixedValueFvPatch${FieldType}
{
if (${verbose:-false})
{
Info<<"construct ${typeName} sha1: ${SHA1sum}"
" as copy\n";
printMessage("Copy construct ${typeName}");
}
}
......@@ -166,8 +156,7 @@ ${typeName}FixedValueFvPatch${FieldType}
{
if (${verbose:-false})
{
Info<<"construct ${typeName} sha1: ${SHA1sum} "
"as copy/DimensionedField\n";
printMessage("Construct ${typeName} : copy/DimensionedField");
}
}
......@@ -179,7 +168,7 @@ ${typeName}FixedValueFvPatch${FieldType}::
{
if (${verbose:-false})
{
Info<<"destroy ${typeName} sha1: ${SHA1sum}\n";
printMessage("Destroy ${typeName}");
}
}
......@@ -195,7 +184,7 @@ void ${typeName}FixedValueFvPatch${FieldType}::updateCoeffs()
if (${verbose:-false})
{
Info<<"updateCoeffs ${typeName} sha1: ${SHA1sum}\n";
printMessage("updateCoeffs ${typeName}");
}
//{{{ begin code
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -52,10 +52,18 @@ class ${typeName}FixedValueFvPatch${FieldType}
:
public fixedValueFvPatchField<${TemplateType}>
{
// Private Member Functions
//- Report a message with the SHA1sum
inline static void printMessage(const char* message)
{
Info<< message << " sha1: " << SHA1sum << '\n';
}
public:
//- Information about the SHA1 of the code itself
static const char* const SHA1sum;
//- SHA1 representation of the code content
static constexpr const char* const SHA1sum = "${SHA1sum}";
//- Runtime type information
TypeName("${typeName}");
......@@ -126,7 +134,7 @@ public:
virtual ~${typeName}FixedValueFvPatch${FieldType}();
// Member functions
// Member Functions
//- Update the coefficients associated with the patch field
virtual void updateCoeffs();
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) YEAR AUTHOR,AFFILIATION
......@@ -30,6 +30,7 @@ License
#include "pointPatchFieldMapper.H"
#include "pointFields.H"
#include "unitConversion.H"
//{{{ begin codeInclude
${codeInclude}
//}}} end codeInclude
......@@ -49,23 +50,20 @@ ${localCode}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
extern "C"
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
extern "C" void ${typeName}_${SHA1sum}(bool load)
{
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
void ${typeName}_${SHA1sum}(bool load)
if (load)
{
// Code that can be explicitly executed after loading
}
else
{
if (load)
{
// code that can be explicitly executed after loading
}
else
{
// code that can be explicitly executed before unloading
}