coded: Permit both untyped and typed substitutions

Coded functionality now supports basic un-typed substitutions from the
surrounding dictionary. For example:

    value 1.2345;

    #codeExecute
    {
        scalar s = $value;
        ...
    };

It also now supports the more functional typed substitutions, such as:

    direction (1 0 0);

    #codeExecute
    {
        vector v = $<vector>direction;
        ...
    };

These substitutions are now possible in all code blocks. Blocks with
access to the dictionary (e.g., #codeRead) will do a lookup which will
not require re-compilation if the value is changed. Blocks without
access to the dictionary will have the value directly substituted, and
will require recompilation when the value is changed.
This commit is contained in:
Will Bainbridge
2024-01-10 15:26:01 +00:00
parent 97f88d4991
commit ddeaa566ef
28 changed files with 141 additions and 57 deletions

View File

@ -28,7 +28,9 @@ License
#include "fieldMapper.H"
#include "volFields.H"
#include "surfaceFields.H"
#include "read.H"
#include "unitConversion.H"
//{{{ begin codeInclude
${codeInclude}
//}}} end codeInclude

View File

@ -27,7 +27,9 @@ License
#include "addToRunTimeSelectionTable.H"
#include "fieldMapper.H"
#include "pointFields.H"
#include "read.H"
#include "unitConversion.H"
//{{{ begin codeInclude
${codeInclude}
//}}} end codeInclude

View File

@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "codedFunction1Template.H"
#include "read.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

View File

@ -24,6 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "codedFunction2Template.H"
#include "read.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

View File

@ -25,6 +25,7 @@ License
#include "codedFunctionObjectTemplate.H"
#include "volFields.H"
#include "read.H"
#include "unitConversion.H"
#include "addToRunTimeSelectionTable.H"

View File

@ -27,6 +27,7 @@ License
#include "addToRunTimeSelectionTable.H"
#include "volFields.H"
#include "surfaceFields.H"
#include "read.H"
#include "unitConversion.H"
#include "fvMatrix.H"

View File

@ -28,7 +28,9 @@ License
#include "fieldMapper.H"
#include "volFields.H"
#include "surfaceFields.H"
#include "read.H"
#include "unitConversion.H"
//{{{ begin codeInclude
${codeInclude}
//}}} end codeInclude

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -92,7 +92,8 @@ Foam::functionEntries::codeStream::getFunction
const dynamicCodeContext context
(
codeDict,
{"code", "codeInclude", "localCode"}
{"code", "codeInclude", "localCode"},
{"dict", word::null, word::null}
);
// codeName: codeStream + _<sha1>

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -322,7 +322,7 @@ void Foam::codedBase::updateLibrary() const
dict
);
dynamicCodeContext context(dict, codeKeys());
dynamicCodeContext context(dict, codeKeys(), codeDictVars());
// codeName: name + _<sha1>
// codeDir : name

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -112,6 +112,9 @@ protected:
//- Get the keywords associated with source code
virtual wordList codeKeys() const = 0;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const = 0;
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const = 0;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2021-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -73,6 +73,12 @@ class compileTemplate
return wordList::null();
}
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const
{
return wordList::null();
}
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -45,7 +45,8 @@ void Foam::dynamicCodeContext::addLineDirective
Foam::dynamicCodeContext::dynamicCodeContext
(
const dictionary& dict,
const wordList& codeKeys
const wordList& codeKeys,
const wordList& codeDictVars
)
:
dict_(dict),
@ -64,7 +65,7 @@ Foam::dynamicCodeContext::dynamicCodeContext
if (codePtrs[i])
{
string s(stringOps::trim(verbatimString(codePtrs[i]->stream())));
stringOps::inplaceExpandCodeString(s, dict);
stringOps::inplaceExpandCodeString(s, dict, codeDictVars[i]);
code_.insert(key, s);
}
else
@ -78,7 +79,7 @@ Foam::dynamicCodeContext::dynamicCodeContext
if (optionsPtr)
{
options_ = stringOps::trim(verbatimString(optionsPtr->stream()));
stringOps::inplaceExpandCodeString(options_, dict);
stringOps::inplaceExpandCodeString(options_, dict, word::null);
}
// Libs
@ -86,7 +87,7 @@ Foam::dynamicCodeContext::dynamicCodeContext
if (libsPtr)
{
libs_ = stringOps::trim(verbatimString(libsPtr->stream()));
stringOps::inplaceExpandCodeString(libs_, dict);
stringOps::inplaceExpandCodeString(libs_, dict, word::null);
}
// Calculate SHA1 digest from all entries

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -89,7 +89,8 @@ public:
dynamicCodeContext
(
const dictionary& dict,
const wordList& codeKeys
const wordList& codeKeys,
const wordList& codeDictVars
);

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2012-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2012-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -35,12 +35,14 @@ License
template<class Type>
Foam::wordList Foam::codedFixedValuePointPatchField<Type>::codeKeys() const
{
return
return {"code", "codeInclude", "localCode"};
}
template<class Type>
Foam::wordList Foam::codedFixedValuePointPatchField<Type>::codeDictVars() const
{
"code",
"codeInclude",
"localCode"
};
return {word::null, word::null, word::null};
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2012-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2012-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -110,6 +110,9 @@ class codedFixedValuePointPatchField
//- Get the keywords associated with source code
virtual wordList codeKeys() const;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const;
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2020-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2020-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -32,11 +32,14 @@ License
template<class Type>
Foam::wordList Foam::Function1s::Coded<Type>::codeKeys() const
{
return
return {"code", "codeInclude"};
}
template<class Type>
Foam::wordList Foam::Function1s::Coded<Type>::codeDictVars() const
{
"code",
"codeInclude"
};
return {word::null, word::null};
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2020-2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2020-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -98,6 +98,9 @@ class Coded
//- Get the keywords associated with source code
virtual wordList codeKeys() const;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const;
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2020-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2020-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -32,11 +32,14 @@ License
template<class Type>
Foam::wordList Foam::Function2s::Coded<Type>::codeKeys() const
{
return
return {"code", "codeInclude"};
}
template<class Type>
Foam::wordList Foam::Function2s::Coded<Type>::codeDictVars() const
{
"code",
"codeInclude"
};
return {word::null, word::null};
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2020-2021 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2020-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -83,6 +83,9 @@ class Coded
//- Get the keywords associated with source code
virtual wordList codeKeys() const;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const;
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -587,7 +587,7 @@ Foam::string& Foam::stringOps::inplaceExpandCodeString
// If the dictionary is not accessible but the
// type is known, then read the substituted value
// from a string
buf << "read<" << varType << ">(\"";
buf << "Foam::read<" << varType << ">(\"";
}
// If the dictionary is not accessible and/or the type

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -35,12 +35,14 @@ License
template<class Type>
Foam::wordList Foam::codedFixedValueFvPatchField<Type>::codeKeys() const
{
return
return {"code", "codeInclude", "localCode"};
}
template<class Type>
Foam::wordList Foam::codedFixedValueFvPatchField<Type>::codeDictVars() const
{
"code",
"codeInclude",
"localCode"
};
return {word::null, word::null, word::null};
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -108,6 +108,9 @@ class codedFixedValueFvPatchField
//- Get the keywords associated with source code
virtual wordList codeKeys() const;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const;
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -35,12 +35,14 @@ License
template<class Type>
Foam::wordList Foam::codedMixedFvPatchField<Type>::codeKeys() const
{
return
return {"code", "codeInclude", "localCode"};
}
template<class Type>
Foam::wordList Foam::codedMixedFvPatchField<Type>::codeDictVars() const
{
"code",
"codeInclude",
"localCode"
};
return {word::null, word::null, word::null};
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -118,6 +118,9 @@ class codedMixedFvPatchField
//- Get the keywords associated with source code
virtual wordList codeKeys() const;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const;
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -66,6 +66,22 @@ Foam::wordList Foam::codedFunctionObject::codeKeys() const
}
Foam::wordList Foam::codedFunctionObject::codeDictVars() const
{
return
{
word::null,
word::null,
word::null,
word::null,
"dict",
word::null,
word::null,
word::null,
};
}
void Foam::codedFunctionObject::prepare
(
dynamicCode& dynCode,

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -99,6 +99,9 @@ class codedFunctionObject
//- Get the keywords associated with source code
virtual wordList codeKeys() const;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const;
//- Adapt the context for the current object
virtual void prepare(dynamicCode&, const dynamicCodeContext&) const;

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2012-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2012-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -144,6 +144,19 @@ Foam::wordList Foam::fv::codedFvModel::codeKeys() const
}
Foam::wordList Foam::fv::codedFvModel::codeDictVars() const
{
return
{
word::null,
word::null,
word::null,
word::null,
word::null
};
}
Foam::fvModel& Foam::fv::codedFvModel::redirectFvModel() const
{
if (!redirectFvModelPtr_.valid())

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2012-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2012-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -126,6 +126,9 @@ class codedFvModel
//- Get the keywords associated with source code
virtual wordList codeKeys() const;
//- Get the name of the dictionary variables in the source code
virtual wordList codeDictVars() const;
//- Dynamically compiled fvModel
fvModel& redirectFvModel() const;