functionObject: Improved handling of #includeFunc arguments

adding support for argument substitution into sub-dictionaries for
e.g. pressureDifferencePatch, white space before, in and after the argument list
and continuation lines, for example:

functions
{
    #includeFunc flowRatePatch(name=inlet)
    #includeFunc flowRatePatch ( name = outlet )

    #includeFunc pressureDifferencePatch \
    (                   \
        patch1 = inlet, \
        patch2 = outlet \
    )

    #includeFunc yPlus
    #includeFunc  residuals
}
This commit is contained in:
Henry Weller
2019-08-09 12:40:00 +01:00
parent f65f1d6883
commit 61f9131389
8 changed files with 64 additions and 13 deletions

View File

@ -131,6 +131,10 @@ public:
//- Raw, low-level getline into a string function. //- Raw, low-level getline into a string function.
inline ISstream& getLine(string&); inline ISstream& getLine(string&);
//- Get multiple lines using the '\' continuation character
// into a string
inline ISstream& getMultiLines(string&);
//- Raw, low-level putback character function. //- Raw, low-level putback character function.
inline ISstream& putback(const char&); inline ISstream& putback(const char&);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -80,6 +80,32 @@ inline Foam::ISstream& Foam::ISstream::getLine(string& s)
setState(is_.rdstate()); setState(is_.rdstate());
lineNumber_++; lineNumber_++;
while (s.back() == '\\')
{
string contLine;
getline(is_, contLine);
setState(is_.rdstate());
lineNumber_++;
s.pop_back();
s += contLine;
}
return *this;
}
inline Foam::ISstream& Foam::ISstream::getMultiLines(string& s)
{
getLine(s);
while (s.back() == '\\')
{
s.pop_back();
string contLine;
getLine(contLine);
s += contLine;
}
return *this; return *this;
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -153,7 +153,6 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
{ {
if (keyword[0] == '#') // ... Function entry if (keyword[0] == '#') // ... Function entry
{ {
word functionName = keyword(1, keyword.size()-1);
if (disableFunctionEntries) if (disableFunctionEntries)
{ {
return parentDict.add return parentDict.add
@ -169,6 +168,7 @@ bool Foam::entry::New(dictionary& parentDict, Istream& is)
} }
else else
{ {
const word functionName = keyword(1, keyword.size() - 1);
return functionEntry::execute(functionName, parentDict, is); return functionEntry::execute(functionName, parentDict, is);
} }
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -49,12 +49,12 @@ namespace Foam
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
Foam::token Foam::functionEntry::readLine(const word& key, Istream& is) Foam::string Foam::functionEntry::readLine(const word& key, Istream& is)
{ {
string s; string s;
dynamic_cast<ISstream&>(is).getLine(s); dynamic_cast<ISstream&>(is).getLine(s);
return token(string(key+s), is.lineNumber()); return key + s;
} }
@ -69,8 +69,8 @@ Foam::functionEntry::functionEntry
: :
primitiveEntry primitiveEntry
( (
word(key+dict.name()+Foam::name(is.lineNumber())), word(key + dict.name() + Foam::name(is.lineNumber())),
readLine(key, is) readLine(key, is).c_str()
) )
{} {}

View File

@ -68,7 +68,7 @@ class functionEntry
// Private Member Functions // Private Member Functions
//- Read line as string token //- Read line as string token
static token readLine(const word& key, Istream& is); static string readLine(const word& key, Istream& is);
public: public:

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2016-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2016-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -25,6 +25,7 @@ License
#include "includeFuncEntry.H" #include "includeFuncEntry.H"
#include "functionObjectList.H" #include "functionObjectList.H"
#include "stringOps.H"
#include "addToMemberFunctionSelectionTable.H" #include "addToMemberFunctionSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -54,7 +55,10 @@ bool Foam::functionEntries::includeFuncEntry::execute
Istream& is Istream& is
) )
{ {
const word fNameArgs(is); // Read line containing the function name and all the arguments
string fNameArgs;
dynamic_cast<ISstream&>(is).getMultiLines(fNameArgs);
HashSet<word> selectedFields; HashSet<word> selectedFields;
return functionObjectList::readFunctionObject return functionObjectList::readFunctionObject

View File

@ -232,7 +232,7 @@ Foam::primitiveEntry::primitiveEntry(const keyType& key, Istream& is)
void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
{ {
if (!contentsOnly) if (!contentsOnly && keyword().size() && keyword()[0] != '#')
{ {
os.writeKeyword(keyword()); os.writeKeyword(keyword());
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -270,6 +270,9 @@ bool Foam::functionObjectList::readFunctionObject
++i; ++i;
} }
// Strip whitespace from the function name
string::stripInvalid<word>(funcName);
// Search for the functionObject dictionary // Search for the functionObject dictionary
fileName path = findDict(funcName, region); fileName path = findDict(funcName, region);
@ -285,7 +288,12 @@ bool Foam::functionObjectList::readFunctionObject
autoPtr<ISstream> fileStreamPtr(fileHandler().NewIFstream(path)); autoPtr<ISstream> fileStreamPtr(fileHandler().NewIFstream(path));
ISstream& fileStream = fileStreamPtr(); ISstream& fileStream = fileStreamPtr();
// Delay processing the functionEntries
// until after the function argument entries have been added
entry::disableFunctionEntries = true;
dictionary funcsDict(fileStream); dictionary funcsDict(fileStream);
entry::disableFunctionEntries = false;
dictionary* funcDictPtr = &funcsDict; dictionary* funcDictPtr = &funcsDict;
if (funcsDict.found(funcName) && funcsDict.isDict(funcName)) if (funcsDict.found(funcName) && funcsDict.isDict(funcName))
@ -338,6 +346,15 @@ bool Foam::functionObjectList::readFunctionObject
const word funcNameArgsWord = string::validate<word>(funcNameArgs); const word funcNameArgsWord = string::validate<word>(funcNameArgs);
dictionary funcArgsDict; dictionary funcArgsDict;
funcArgsDict.add(funcNameArgsWord, funcDict); funcArgsDict.add(funcNameArgsWord, funcDict);
// Re-parse the funcDict to execute the functionEntries
// now that the function argument entries have been added
{
OStringStream os;
funcArgsDict.write(os);
funcArgsDict = dictionary(IStringStream(os.str())());
}
functionsDict.merge(funcArgsDict); functionsDict.merge(funcArgsDict);
functionsDict.subDict(funcNameArgsWord).name() = funcDict.name(); functionsDict.subDict(funcNameArgsWord).name() = funcDict.name();