functionEntries::calcIncludeEntry: New dictionary function entry to provide additional include files for #calc

Description
    Specify an include file for #calc, expects a single string to follow.

    For example if functions from transform.H are used in the #calc expression
    \verbatim
    angleOfAttack   5; // degs

    angle           #calc "-degToRad($angleOfAttack)";

    #calcInclude    "transform.H"
    liftDir         #calc "transform(Ry($angle), vector(0, 0, 1))";
    dragDir         #calc "transform(Ry($angle), vector(1, 0, 0))";
    \endverbatim

    The usual expansion of environment variables and other constructs
    (eg, the \c ~OpenFOAM/ expansion) is retained.

See also:

Class
    Foam::functionEntries::calcEntry

Description
    Uses dynamic compilation to provide calculating functionality
    for entering dictionary entries.

    E.g.

    \verbatim
    a 1.0;
    b 3;
    c #calc "$a*$b";
    \endverbatim

    Note the explicit trailing 0 ('1.0') to force a to be read (and written)
    as a floating point number.

    Special care is required for calc entries that include a division since
    "/" is also used as the scoping operator to identify keywords in
    sub-dictionaries. For example, "$a/b" expects a keyword "b" within a
    sub-dictionary named "a". A division can be correctly executed by using a
    space between a variables and "/", e.g.

    \verbatim
    c #calc "$a / $b";
    \endverbatim

    or "()" scoping around the variable, e.g.

    \verbatim
    c #calc "($a)/$b";
    \endverbatim

    Additional include files for the #calc code compilation can be specified
    using the #calcInclude entry, e.g. if functions from transform.H are used
    \verbatim
    angleOfAttack   5; // degs

    angle           #calc "-degToRad($angleOfAttack)";

    #calcInclude    "transform.H"
    liftDir         #calc "transform(Ry($angle), vector(0, 0, 1))";
    dragDir         #calc "transform(Ry($angle), vector(1, 0, 0))";
    \endverbatim

    Note:
        Internally this is just a wrapper around codeStream functionality - the
        #calc string is used to construct a dictionary for codeStream.
This commit is contained in:
Henry Weller
2023-06-24 16:56:59 +01:00
parent 755eae4aa9
commit 0de23df8bb
8 changed files with 273 additions and 13 deletions

View File

@ -227,6 +227,7 @@ $(dictionaryListEntry)/dictionaryListEntryIO.C
functionEntries = $(dictionary)/functionEntries
$(functionEntries)/negEntry/negEntry.C
$(functionEntries)/calcIncludeEntry/calcIncludeEntry.C
$(functionEntries)/calcEntry/calcEntry.C
$(functionEntries)/codeStream/codeStream.C
$(functionEntries)/functionEntry/functionEntry.C

View File

@ -27,6 +27,7 @@ License
#include "IOobject.H"
#include "inputSyntaxEntry.H"
#include "inputModeEntry.H"
#include "calcIncludeEntry.H"
#include "stringOps.H"
#include "etcFiles.H"
#include "wordAndDictionary.H"
@ -64,6 +65,9 @@ Foam::dictionary::dictionary(Istream& is, const bool keepHeader)
// Reset input mode as this is a "top-level" dictionary
functionEntries::inputModeEntry::clear();
// Clear the cache of #calc include files
functionEntries::calcIncludeEntry::clear();
read(is, keepHeader);
}
@ -191,6 +195,9 @@ Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict)
// Reset input mode assuming this is a "top-level" dictionary
functionEntries::inputModeEntry::clear();
// Clear the cache of #calc include files
functionEntries::calcIncludeEntry::clear();
dict.clear();
dict.name() = is.name();
dict.read(is);

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-2020 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -24,10 +24,11 @@ License
\*---------------------------------------------------------------------------*/
#include "calcEntry.H"
#include "addToMemberFunctionSelectionTable.H"
#include "calcIncludeEntry.H"
#include "dictionary.H"
#include "dynamicCode.H"
#include "codeStream.H"
#include "addToMemberFunctionSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -78,8 +79,25 @@ Foam::string Foam::functionEntries::calcEntry::calc
string s(is);
// Construct codeDict for codeStream
// Must reference parent dictionary for stringOps::expand to work
dictionary codeDict(dict.parent(), dict);
// with dict as parent dictionary for string expansion
// and variable substitution
dictionary codeDict(dict, dictionary());
// if (dict.found("codeInclude", true))
// {
// codeDict.add(dict.lookupEntry("codeInclude", true, false), true);
// }
// codeDict.add
// (
// "codeInclude",
// verbatimString
// (
// "#include \"scalar.H\"\n"
// "#include \"transform.H\""
// )
// );
calcIncludeEntry::codeInclude(codeDict);
codeDict.add("code", "os << (" + s + ");");
codeStream::streamingFunctionType function = codeStream::getFunction

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-2022 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -55,9 +55,24 @@ Description
c #calc "($a)/$b";
\endverbatim
Additional include files for the #calc code compilation can be specified
using the #calcInclude entry, e.g. if functions from transform.H are used
\verbatim
angleOfAttack 5; // degs
angle #calc "-degToRad($angleOfAttack)";
#calcInclude "transform.H"
liftDir #calc "transform(Ry($angle), vector(0, 0, 1))";
dragDir #calc "transform(Ry($angle), vector(1, 0, 0))";
\endverbatim
Note:
Internally this is just a wrapper around codeStream functionality - the
#calc string gets used to construct a dictionary for codeStream.
#calc string is used to construct a dictionary for codeStream.
See also
Foam::functionEntries::calcIncludeEntry
SourceFiles
calcEntry.C

View File

@ -0,0 +1,99 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2023 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "calcIncludeEntry.H"
#include "dictionary.H"
#include "IFstream.H"
#include "addToMemberFunctionSelectionTable.H"
#include "stringOps.H"
#include "IOobject.H"
#include "fileOperation.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
defineTypeNameAndDebug(calcIncludeEntry, 0);
addToMemberFunctionSelectionTable
(
functionEntry,
calcIncludeEntry,
execute,
dictionaryIstream
);
}
}
// Construct the static include file name cache
Foam::DynamicList<Foam::fileName>
Foam::functionEntries::calcIncludeEntry::includeFiles_;
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionEntries::calcIncludeEntry::execute
(
dictionary& parentDict,
Istream& is
)
{
// Read the include file name
fileName fName(is);
// Substitute dictionary and environment variables. Allow empty
// substitutions.
stringOps::inplaceExpandEntry(fName, parentDict, true, true);
// Add the file name to the cache
includeFiles_.append(fName);
return true;
}
void Foam::functionEntries::calcIncludeEntry::clear()
{
includeFiles_.clear();
}
void Foam::functionEntries::calcIncludeEntry::codeInclude(dictionary& codeDict)
{
if (includeFiles_.size())
{
verbatimString codeInclude;
forAll(includeFiles_, i)
{
codeInclude += "#include \"" + includeFiles_[i] + '"' + '\n';
}
codeDict.add("codeInclude", codeInclude);
}
}
// ************************************************************************* //

View File

@ -0,0 +1,119 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2023 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::functionEntries::calcIncludeEntry
Description
Specify an include file for #calc, expects a single string to follow.
For example if functions from transform.H are used in the #calc expression
\verbatim
angleOfAttack 5; // degs
angle #calc "-degToRad($angleOfAttack)";
#calcInclude "transform.H"
liftDir #calc "transform(Ry($angle), vector(0, 0, 1))";
dragDir #calc "transform(Ry($angle), vector(1, 0, 0))";
\endverbatim
The usual expansion of environment variables and other constructs
(eg, the \c ~OpenFOAM/ expansion) is retained.
See also
Foam::functionEntries::calcEntry
SourceFiles
calcIncludeEntry.C
\*---------------------------------------------------------------------------*/
#ifndef calcIncludeEntry_H
#define calcIncludeEntry_H
#include "functionEntry.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
/*---------------------------------------------------------------------------*\
Class calcIncludeEntry Declaration
\*---------------------------------------------------------------------------*/
class calcIncludeEntry
:
public functionEntry
{
// Private static data
// Static include file name cache
static DynamicList<fileName> includeFiles_;
public:
//- Runtime type information
ClassName("calcInclude");
// Constructors
//- Disallow default bitwise copy construction
calcIncludeEntry(const calcIncludeEntry&) = delete;
// Member Functions
//- Execute the functionEntry in a sub-dict context
static bool execute(dictionary& parentDict, Istream&);
//- Reset the cache of #calc include file names
static void clear();
//- Add the cached include file names to the codeInclude entry
// in codeDict
static void codeInclude(dictionary& codeDict);
// Member Operators
//- Disallow default bitwise assignment
void operator=(const calcIncludeEntry&) = delete;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace functionEntries
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

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-2022 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -333,9 +333,9 @@ Foam::string Foam::functionEntries::codeStream::run
parentDict
);
// Get code dictionary
// must reference parent for stringOps::expand to work nicely
const dictionary codeDict("#codeStream", parentDict, is);
// Construct codeDict for codeStream
// Parent dictionary provided for string expansion and variable substitution
const dictionary codeDict("#codeStream", parentDict, is);
const streamingFunctionType function = getFunction(parentDict, codeDict);