ENH: provide dictionary access for coded BCs etc (#1922)

- in some cases, additional dictionary inputs are useful for extending
  the input parameters or functionality of dynamic coded conditions.

  Typically this can be used to provide a simple set of dictionary
  inputs that are used to drive specific code, but allows changing the
  inputs without causing a recompilation.

  Accessed with this type of code:
  ```
  const dictionary& dict = this->codeContext();
  ```

boundary conditions and function objects:

* specify an additional codeContext dictionary entry:
  ```
  codeContext
  {
      ...
  }
  ```

PatchFunction1:

* The code context dictionary is simply the dictionary used to specify
  the PatchFunction1 coefficients.

  To replicated persistant data, use local member static data.
  Eg,
  ```
  code
  #{
      // Persistent (Member) Data
      static autoPtr<Function1<scalar>> baseVel;
      static autoPtr<Function1<vector>> baseDir;
      ...
  #}
  ```

fvOptions:

* currently not applicable
This commit is contained in:
Mark Olesen
2021-06-01 11:27:45 +02:00
parent b0891824fa
commit b59ae32d68
18 changed files with 233 additions and 19 deletions

View File

@ -102,6 +102,7 @@ SourceFiles
#define codedFvOptionTemplate_H
#include "cellSetOption.H"
#include "dictionaryContent.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -116,7 +117,8 @@ namespace fv
class ${typeName}FvOption${SourceType}
:
public fv::cellSetOption
public fv::cellSetOption,
public dictionaryContent
{
// Private Member Functions
@ -153,6 +155,12 @@ public:
// Member Functions
//- Code context as a dictionary
const dictionary& codeContext() const
{
return dictionaryContent::dict();
}
//- Correct field
virtual void correct
(

View File

@ -107,16 +107,6 @@ ${typeName}PatchFunction1${FieldType}
}
${typeName}PatchFunction1${FieldType}::
${typeName}PatchFunction1${FieldType}
(
const ${typeName}PatchFunction1${FieldType}& rhs
)
:
parent_bctype(rhs)
{}
${typeName}PatchFunction1${FieldType}::
${typeName}PatchFunction1${FieldType}
(

View File

@ -35,6 +35,7 @@ SourceFiles
#define codedPatchFunction1Template${FieldType}_H
#include "PatchFunction1.H"
#include "dictionaryContent.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -49,12 +50,12 @@ namespace PatchFunction1Types
class ${typeName}PatchFunction1${FieldType}
:
public PatchFunction1<${TemplateType}>
public PatchFunction1<${TemplateType}>,
public dictionaryContent
{
//- The parent PatchFunction1 type
typedef PatchFunction1<${TemplateType}> parent_bctype;
// Private Member Functions
//- Report a message with the SHA1sum
@ -88,7 +89,7 @@ public:
${typeName}PatchFunction1${FieldType}
(
const ${typeName}PatchFunction1${FieldType}& rhs
);
) = default;
//- Copy construct, resetting patch
${typeName}PatchFunction1${FieldType}

View File

@ -36,6 +36,7 @@ SourceFiles
#define codedPoints0MotionSolverTemplate_H
#include "points0MotionSolver.H"
#include "dictionaryContent.H"
//{{{ begin codeInclude
${codeInclude}
@ -52,7 +53,8 @@ namespace Foam
class ${typeName}Points0MotionSolver
:
public points0MotionSolver
public points0MotionSolver,
public dictionaryContent
{
// Private Member Functions
@ -100,6 +102,12 @@ public:
// Member Functions
//- Code context as a dictionary
const dictionary& codeContext() const
{
return dictionaryContent::dict();
}
//- Provide current points for motion. Uses current motion field
virtual tmp<pointField> curPoints() const;

View File

@ -38,6 +38,7 @@ SourceFiles
#define fixedValueFvPatchTemplate${FieldType}_H
#include "fixedValueFvPatchFields.H"
#include "dictionaryContent.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -50,7 +51,8 @@ namespace Foam
class ${typeName}FixedValueFvPatch${FieldType}
:
public fixedValueFvPatchField<${TemplateType}>
public fixedValueFvPatchField<${TemplateType}>,
public dictionaryContent
{
//- The parent boundary condition type
typedef fixedValueFvPatchField<${TemplateType}> parent_bctype;
@ -140,6 +142,12 @@ public:
// Member Functions
//- Code context as a dictionary
const dictionary& codeContext() const
{
return dictionaryContent::dict();
}
//- Update the coefficients associated with the patch field
virtual void updateCoeffs();
};

View File

@ -38,6 +38,7 @@ SourceFiles
#define coded_fixedValuePointPatchTemplate${FieldType}_H
#include "fixedValuePointPatchFields.H"
#include "dictionaryContent.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -50,7 +51,8 @@ namespace Foam
class ${typeName}FixedValuePointPatch${FieldType}
:
public fixedValuePointPatchField<${TemplateType}>
public fixedValuePointPatchField<${TemplateType}>,
public dictionaryContent
{
//- The parent boundary condition type
typedef fixedValuePointPatchField<${TemplateType}> parent_bctype;
@ -141,6 +143,12 @@ public:
// Member Functions
//- Code context as a dictionary
const dictionary& codeContext() const
{
return dictionaryContent::dict();
}
//- Update the coefficients associated with the patch field
virtual void updateCoeffs();
};

View File

@ -36,6 +36,7 @@ SourceFiles
#define functionObjectTemplate_H
#include "regionFunctionObject.H"
#include "dictionaryContent.H"
//{{{ begin codeInclude
${codeInclude}
@ -55,7 +56,8 @@ class fvMesh;
class ${typeName}FunctionObject
:
public functionObjects::regionFunctionObject
public functionObjects::regionFunctionObject,
public dictionaryContent
{
// Private Data
@ -113,6 +115,12 @@ public:
// Member Functions
//- Code context as a dictionary
const dictionary& codeContext() const
{
return dictionaryContent::dict();
}
//- Read optional controls
virtual bool read(const dictionary& dict);

View File

@ -38,6 +38,7 @@ SourceFiles
#define coded_mixedFvPatchTemplate${FieldType}_H
#include "mixedFvPatchFields.H"
#include "dictionaryContent.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -50,7 +51,8 @@ namespace Foam
class ${typeName}MixedValueFvPatch${FieldType}
:
public mixedFvPatchField<${TemplateType}>
public mixedFvPatchField<${TemplateType}>,
public dictionaryContent
{
//- The parent boundary condition type
typedef mixedFvPatchField<${TemplateType}> parent_bctype;
@ -140,6 +142,12 @@ public:
// Member Functions
//- Code context as a dictionary
const dictionary& codeContext() const
{
return dictionaryContent::dict();
}
//- Update the coefficients associated with the patch field
virtual void updateCoeffs();
};

View File

@ -60,6 +60,15 @@ void Foam::codedFixedValuePointPatchField<Type>::clearRedirect() const
}
template<class Type>
const Foam::dictionary&
Foam::codedFixedValuePointPatchField<Type>::codeContext() const
{
const dictionary* ptr = dict_.findDict("codeContext", keyType::LITERAL);
return (ptr ? *ptr : dictionary::null);
}
template<class Type>
const Foam::dictionary&
Foam::codedFixedValuePointPatchField<Type>::codeDict() const
@ -235,6 +244,22 @@ Foam::codedFixedValuePointPatchField<Type>::redirectPatchField() const
constructDict
).ptr()
);
// Forward copy of codeContext to the code template
auto* contentPtr =
dynamic_cast<dictionaryContent*>(redirectPatchFieldPtr_.get());
if (contentPtr)
{
contentPtr->dict(this->codeContext());
}
else
{
WarningInFunction
<< name_ << " Did not derive from dictionaryContent"
<< nl << nl;
}
}
return *redirectPatchFieldPtr_;
}

View File

@ -38,6 +38,7 @@ Description
codeLibs | linker line: added to LIB_LIBS (Make/options)
localCode | c++; local static functions
code | c++; patch value assignment
codeContext | additional dictionary context for the code
\endplaintable
Example:
@ -56,6 +57,11 @@ Description
);
#};
codeContext
{
...
}
//codeInclude
//#{
// #include "fvCFD.H"
@ -82,6 +88,10 @@ Description
}
\endverbatim
Note
The code context dictionary can be supplied separately as the
\c codeContext entry.
See also
codedFixedValueFvPatchField
@ -136,6 +146,9 @@ class codedFixedValuePointPatchField
//- Clear redirected object(s)
virtual void clearRedirect() const;
//- Additional 'codeContext' dictionary to pass through
virtual const dictionary& codeContext() const;
//- The code dictionary. Inline "code" or from system/codeDict
virtual const dictionary& codeDict() const;

View File

@ -60,6 +60,15 @@ void Foam::codedFixedValueFvPatchField<Type>::clearRedirect() const
}
template<class Type>
const Foam::dictionary&
Foam::codedFixedValueFvPatchField<Type>::codeContext() const
{
const dictionary* ptr = dict_.findDict("codeContext", keyType::LITERAL);
return (ptr ? *ptr : dictionary::null);
}
template<class Type>
const Foam::dictionary&
Foam::codedFixedValueFvPatchField<Type>::codeDict() const
@ -234,6 +243,22 @@ Foam::codedFixedValueFvPatchField<Type>::redirectPatchField() const
constructDict
).ptr()
);
// Forward copy of codeContext to the code template
auto* contentPtr =
dynamic_cast<dictionaryContent*>(redirectPatchFieldPtr_.get());
if (contentPtr)
{
contentPtr->dict(this->codeContext());
}
else
{
WarningInFunction
<< name_ << " Did not derive from dictionaryContent"
<< nl << nl;
}
}
return *redirectPatchFieldPtr_;
}

View File

@ -41,6 +41,7 @@ Description
codeLibs | linker line: added to LIB_LIBS (Make/options)
localCode | c++; local static functions
code | c++; patch value assignment
codeContext | additional dictionary context for the code
\endplaintable
Usage
@ -52,6 +53,11 @@ Usage
value uniform 0;
name rampedFixedValue; // name of generated BC
codeContext
{
...
}
code
#{
operator==(min(10, 0.1*this->db().time().value()));
@ -83,6 +89,10 @@ Usage
}
\endverbatim
Note
The code context dictionary can be supplied separately as the
\c codeContext entry.
See also
Foam::dynamicCode
Foam::functionEntries::codeStream
@ -138,6 +148,9 @@ class codedFixedValueFvPatchField
//- Clear redirected object(s)
virtual void clearRedirect() const;
//- Additional 'codeContext' dictionary to pass through
virtual const dictionary& codeContext() const;
//- The code dictionary. Inline "code" or from system/codeDict
virtual const dictionary& codeDict() const;

View File

@ -60,6 +60,15 @@ void Foam::codedMixedFvPatchField<Type>::clearRedirect() const
}
template<class Type>
const Foam::dictionary&
Foam::codedMixedFvPatchField<Type>::codeContext() const
{
const dictionary* ptr = dict_.findDict("codeContext", keyType::LITERAL);
return (ptr ? *ptr : dictionary::null);
}
template<class Type>
const Foam::dictionary&
Foam::codedMixedFvPatchField<Type>::codeDict() const
@ -240,6 +249,21 @@ Foam::codedMixedFvPatchField<Type>::redirectPatchField() const
).ptr()
)
);
// Forward copy of dictionary content to the code template
auto* contentPtr =
dynamic_cast<dictionaryContent*>(redirectPatchFieldPtr_.get());
if (contentPtr)
{
contentPtr->dict(this->codeContext());
}
else
{
WarningInFunction
<< name_ << " Did not derive from dictionaryContent"
<< nl << nl;
}
}
return *redirectPatchFieldPtr_;
}

View File

@ -41,6 +41,7 @@ Description
codeLibs | linker line: added to LIB_LIBS (Make/options)
localCode | c++; local static functions;
code | c++; patch value assignment
codeContext | additional dictionary context for the code
\endplaintable
Usage
@ -93,6 +94,10 @@ Usage
}
\endverbatim
Note
The code context dictionary can be supplied separately as the
\c codeContext entry.
See also
Foam::dynamicCode
Foam::functionEntries::codeStream
@ -150,6 +155,9 @@ protected:
//- Clear redirected object(s)
virtual void clearRedirect() const;
//- Additional 'codeContext' dictionary to pass through
virtual const dictionary& codeContext() const;
//- The code dictionary. Inline "code" or from system/codeDict
virtual const dictionary& codeDict() const;

View File

@ -72,6 +72,14 @@ void Foam::functionObjects::codedFunctionObject::clearRedirect() const
}
const Foam::dictionary&
Foam::functionObjects::codedFunctionObject::codeContext() const
{
const dictionary* ptr = dict_.findDict("codeContext", keyType::LITERAL);
return (ptr ? *ptr : dictionary::null);
}
const Foam::dictionary&
Foam::functionObjects::codedFunctionObject::codeDict() const
{
@ -157,6 +165,22 @@ Foam::functionObjects::codedFunctionObject::redirectFunctionObject() const
time_,
constructDict
);
// Forward copy of codeContext to the code template
auto* contentPtr =
dynamic_cast<dictionaryContent*>(redirectFunctionObjectPtr_.get());
if (contentPtr)
{
contentPtr->dict(this->codeContext());
}
else
{
WarningInFunction
<< name_ << " Did not derive from dictionaryContent"
<< nl << nl;
}
}
return *redirectFunctionObjectPtr_;
}

View File

@ -44,6 +44,7 @@ Description
codeExecute | c++; upon functionObject::execute()
codeWrite | c++; upon functionObject::write()
codeEnd | c++; upon functionObject::end()
codeContext | additional dictionary context for the code
\endplaintable
Usage
@ -67,6 +68,10 @@ Usage
}
\endverbatim
Note
The code context dictionary can be supplied separately as the
\c codeContext entry.
See also
Foam::functionObject
Foam::codedBase
@ -128,6 +133,9 @@ protected:
//- Clear redirected object(s)
virtual void clearRedirect() const;
//- Additional 'codeContext' dictionary to pass through
virtual const dictionary& codeContext() const;
//- The code dictionary
virtual const dictionary& codeDict() const;

View File

@ -54,6 +54,15 @@ void Foam::PatchFunction1Types::CodedField<Type>::clearRedirect() const
}
template<class Type>
const Foam::dictionary&
Foam::PatchFunction1Types::CodedField<Type>::codeContext() const
{
// What else would make sense?
return dict_;
}
template<class Type>
const Foam::dictionary&
Foam::PatchFunction1Types::CodedField<Type>::codeDict
@ -148,6 +157,10 @@ Foam::PatchFunction1Types::CodedField<Type>::CodedField
dict_(dict),
name_(dict.getOrDefault<word>("name", entryName))
{
this->codedBase::setCodeContext(dict_);
// No additional code chunks...
updateLibrary(name_);
}
@ -202,6 +215,21 @@ Foam::PatchFunction1Types::CodedField<Type>::redirectFunction() const
this->faceValues()
)
);
// Forward copy of codeContext to the code template
auto* contentPtr =
dynamic_cast<dictionaryContent*>(redirectFunctionPtr_.get());
if (contentPtr)
{
contentPtr->dict(this->codeContext());
}
else
{
WarningInFunction
<< name_ << " Did not derive from dictionaryContent"
<< nl << nl;
}
}
return *redirectFunctionPtr_;
}

View File

@ -70,6 +70,10 @@ Usage
}
\endverbatim
Note
The code context dictionary is simply the dictionary used to specify
the PatchFunction1 coefficients.
See also
Foam::dynamicCode
Foam::codedFixedValue
@ -132,6 +136,9 @@ protected:
//- Clear redirected object(s)
virtual void clearRedirect() const;
//- Additional 'codeContext' dictionary to pass through
virtual const dictionary& codeContext() const;
// Get the code (sub)dictionary
virtual const dictionary& codeDict(const dictionary& dict) const;