From efd50eae81ea986a15c29d9255b06ef29d8e32bf Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Sat, 18 Jul 2020 13:24:46 +0100 Subject: [PATCH] dictionary::functionEntries::calcEntry: Change code dictionary construction so variable lookup works as expected For example thermo:rho.air1 { explicit 3e-07; implicit 0; } f1.air1.bubbles { value 3.5; explicit #calc "$value*$../thermo:rho.air1/explicit"; implicit 0; } now works, whereas previously an extra level of '../' was required: explicit #calc "$value*$../../thermo:rho.air1/explicit"; because #calc created its own sub-dictionary. The '$value' would have also needed a '../' except that the 'value' entry is in the direct parent and could be looked-up automatically by the parent search. --- .../functionEntries/calcEntry/calcEntry.C | 29 +++++++++---------- .../functionEntries/calcEntry/calcEntry.H | 8 ++--- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C index 51d0a309df..3f7d101236 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-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -61,39 +61,36 @@ namespace functionEntries Foam::string Foam::functionEntries::calcEntry::calc ( - const dictionary& parentDict, + const dictionary& dict, Istream& is ) { Info<< "Using #calcEntry at line " << is.lineNumber() - << " in file " << parentDict.name() << endl; + << " in file " << dict.name() << endl; dynamicCode::checkSecurity ( "functionEntries::calcEntry::execute(..)", - parentDict + dict ); // Read string string s(is); - // Make sure we stop this entry - // is.putBack(token(token::END_STATEMENT, is.lineNumber())); // Construct codeDict for codeStream - // must reference parent for stringOps::expand to work nicely. - dictionary codeSubDict; - codeSubDict.add("code", "os << (" + s + ");"); - dictionary codeDict(parentDict, codeSubDict); + // Must reference parent dictionary for stringOps::expand to work + dictionary codeDict(dict.parent(), dict); + codeDict.add("code", "os << (" + s + ");"); codeStream::streamingFunctionType function = codeStream::getFunction ( - parentDict, + dict, codeDict ); // Use function to write stream OStringStream os(is.format()); - (*function)(os, parentDict); + (*function)(os, dict); // Return the string containing the results of the calculation return os.str(); @@ -104,22 +101,22 @@ Foam::string Foam::functionEntries::calcEntry::calc bool Foam::functionEntries::calcEntry::execute ( - dictionary& parentDict, + dictionary& dict, Istream& is ) { - return insert(parentDict, calc(parentDict, is)); + return insert(dict, calc(dict, is)); } bool Foam::functionEntries::calcEntry::execute ( - const dictionary& parentDict, + const dictionary& dict, primitiveEntry& thisEntry, Istream& is ) { - return insert(parentDict, thisEntry, calc(parentDict, is)); + return insert(dict, thisEntry, calc(dict, is)); } diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H index 38767c2ead..7444062c9e 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-2019 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -76,7 +76,7 @@ class calcEntry //- Perform the calculation and return the resulting string static string calc ( - const dictionary& parentDict, + const dictionary& dict, Istream& is ); @@ -96,12 +96,12 @@ public: // Member Functions //- Execute the functionEntry in a sub-dict context - static bool execute(dictionary& parentDict, Istream&); + static bool execute(dictionary& dict, Istream&); //- Execute the functionEntry in a primitiveEntry context static bool execute ( - const dictionary& parentDict, + const dictionary& dict, primitiveEntry&, Istream& );