diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 32cc2797af..4dff17e071 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -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 diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C index d139b1d302..51f2db7893 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C @@ -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); diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C index 3f7d101236..ce570bd6a0 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C @@ -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 diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H index 714b3f141d..a50b1f8448 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H @@ -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 diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcIncludeEntry/calcIncludeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcIncludeEntry/calcIncludeEntry.C new file mode 100644 index 0000000000..3a457402ef --- /dev/null +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcIncludeEntry/calcIncludeEntry.C @@ -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 . + +\*---------------------------------------------------------------------------*/ + +#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::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); + } +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcIncludeEntry/calcIncludeEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/calcIncludeEntry/calcIncludeEntry.H new file mode 100644 index 0000000000..19a63184e6 --- /dev/null +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcIncludeEntry/calcIncludeEntry.H @@ -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 . + +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 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 + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C index a94e3781f6..982712f1ba 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/codeStream/codeStream.C @@ -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); diff --git a/tutorials/fluid/aerofoilNACA0012Steady/0/U b/tutorials/fluid/aerofoilNACA0012Steady/0/U index b216fafa31..2ec2b92aee 100644 --- a/tutorials/fluid/aerofoilNACA0012Steady/0/U +++ b/tutorials/fluid/aerofoilNACA0012Steady/0/U @@ -16,10 +16,11 @@ FoamFile speed 250; angleOfAttack 0; // degs -angle #calc "degToRad($angleOfAttack)"; +angle #calc "-degToRad($angleOfAttack)"; -liftDir #calc "vector(-sin($angle), 0, cos($angle))"; -dragDir #calc "vector(cos($angle), 0, sin($angle))"; +#calcInclude "transform.H" +liftDir #calc "transform(Ry($angle), vector(0, 0, 1))"; +dragDir #calc "transform(Ry($angle), vector(1, 0, 0))"; Uinlet #calc "$speed*$dragDir";