functionObjectList: Improved flexibility of field argument parsing

Additional flexibility for handling of field arguments has been extended
to dictionary lists of field settings, as well as word lists of field
names. This means that the following syntax is now supported:

    postProcess -func "fieldAverage(p, U { prime2Mean on; }, T)"

    postProcess -func "fieldAverage(fields=(p U { prime2Mean on; } T))"
This commit is contained in:
Will Bainbridge
2020-04-08 08:37:59 +01:00
parent b6f91de72c
commit 81188fefaa
5 changed files with 239 additions and 42 deletions

View File

@ -268,6 +268,7 @@ $(dll)/codedBase/codedBase.C
db/functionObjects/functionObject/functionObject.C db/functionObjects/functionObject/functionObject.C
db/functionObjects/functionObjectList/functionObjectList.C db/functionObjects/functionObjectList/functionObjectList.C
db/functionObjects/functionObjectList/wordAndDictionary.C
db/functionObjects/writeFile/writeFile.C db/functionObjects/writeFile/writeFile.C
db/functionObjects/logFiles/logFiles.C db/functionObjects/logFiles/logFiles.C
db/functionObjects/writeObjectsBase/writeObjectsBase.C db/functionObjects/writeObjectsBase/writeObjectsBase.C

View File

@ -33,6 +33,8 @@ License
#include "Tuple2.H" #include "Tuple2.H"
#include "etcFiles.H" #include "etcFiles.H"
#include "IOdictionary.H" #include "IOdictionary.H"
#include "wordAndDictionary.H"
/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
@ -334,15 +336,19 @@ bool Foam::functionObjectList::readFunctionObject
// Store the funcDict as read for error reporting context // Store the funcDict as read for error reporting context
const dictionary funcDict0(funcDict); const dictionary funcDict0(funcDict);
// Insert the 'field' and/or 'fields' entry corresponding to the optional // Insert the 'field' and/or 'fields' and 'objects' entries corresponding
// arguments or read the 'field' or 'fields' entry and add the required // to both the arguments and the named arguments
// fields to requiredFields DynamicList<wordAndDictionary> fieldArgs;
wordList fieldArgs(args); forAll(args, i)
{
fieldArgs.append(wordAndDictionary(args[i], dictionary::null));
}
forAll(namedArgs, i) forAll(namedArgs, i)
{ {
if (namedArgs[i].first() == "field") if (namedArgs[i].first() == "field")
{ {
fieldArgs.append(namedArgs[i].second()); IStringStream iss(namedArgs[i].second());
fieldArgs.append(wordAndDictionary(iss));
} }
if if
( (
@ -351,15 +357,16 @@ bool Foam::functionObjectList::readFunctionObject
) )
{ {
IStringStream iss(namedArgs[i].second()); IStringStream iss(namedArgs[i].second());
fieldArgs.append(wordList(iss)); fieldArgs.append(List<wordAndDictionary>(iss));
} }
} }
if (fieldArgs.size() == 1)
{
funcDict.set("field", fieldArgs[0].first());
funcDict.merge(fieldArgs[0].second());
}
if (fieldArgs.size() >= 1) if (fieldArgs.size() >= 1)
{ {
if (fieldArgs.size() == 1)
{
funcDict.set("field", fieldArgs[0]);
}
funcDict.set("fields", fieldArgs); funcDict.set("fields", fieldArgs);
funcDict.set("objects", fieldArgs); funcDict.set("objects", fieldArgs);
} }
@ -405,32 +412,38 @@ bool Foam::functionObjectList::readFunctionObject
); );
} }
// Check for anything in the configuration that has not been set
checkUnsetEntries(funcCall, funcArgsDict, funcDict0, context); checkUnsetEntries(funcCall, funcArgsDict, funcDict0, context);
// Lookup the field and fields entries from the now expanded funcDict // Lookup the field, fields and objects entries from the now expanded
// and insert into the requiredFields // funcDict and insert into the requiredFields
dictionary& expandedFuncDict = funcArgsDict.subDict(funcCallKeyword); dictionary& expandedFuncDict = funcArgsDict.subDict(funcCallKeyword);
if (functionObject::debug)
{
InfoInFunction
<< nl << incrIndent << indent
<< funcCall << expandedFuncDict
<< decrIndent << endl;
}
if (expandedFuncDict.found("field")) if (expandedFuncDict.found("field"))
{ {
requiredFields.insert requiredFields.insert(word(expandedFuncDict.lookup("field")));
(
word(expandedFuncDict.lookup("field"))
);
} }
if (expandedFuncDict.found("fields")) if (expandedFuncDict.found("fields"))
{ {
requiredFields.insert List<wordAndDictionary> fields(expandedFuncDict.lookup("fields"));
( forAll(fields, i)
wordOrDictNameList(expandedFuncDict.lookup("fields")) {
); requiredFields.insert(fields[i].first());
}
} }
if (expandedFuncDict.found("objects")) if (expandedFuncDict.found("objects"))
{ {
requiredFields.insert List<wordAndDictionary> objects(expandedFuncDict.lookup("objects"));
( forAll(objects, i)
wordOrDictNameList(expandedFuncDict.lookup("objects")) {
); requiredFields.insert(objects[i].first());
}
} }
// Merge this functionObject dictionary into functionsDict // Merge this functionObject dictionary into functionsDict

View File

@ -0,0 +1,79 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2020 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 "wordAndDictionary.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::wordAndDictionary::wordAndDictionary(Istream& is)
:
Tuple2<word, dictionary>()
{
is >> *this;
is.check("wordAndDictionary::wordAndDictionary(Istream& is)");
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::wordAndDictionary::~wordAndDictionary()
{}
// * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * * //
Foam::Istream& Foam::operator>>(Istream& is, wordAndDictionary& wd)
{
wd.first() = word(is);
wd.second().clear();
token t(is);
is.putBack(t);
if (t.isPunctuation() && t.pToken() == token::BEGIN_BLOCK)
{
dictionary d(is);
wd.second().transfer(d);
}
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const wordAndDictionary& wd)
{
os << wd.first();
if (!wd.second().empty())
{
os << token::SPACE << wd.second();
}
return os;
}
// ************************************************************************* //

View File

@ -0,0 +1,115 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2020 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::wordAndDictionary
Description
Tuple of a word and dictionary, used to read in per-field options for
function objects in the following syntax:
fields
(
p
{
option1 true;
option2 false;
}
U
T
{
option1 false;
}
);
IO is like the tuple, except that there are no enclosing parentheses, and
if the dictionary is empty then '{}' is omitted. The latter means that in
the absence of any options the syntax becomes that of a wordList, which
means it can be used for argument parsing for input of that form.
SourceFiles
wordAndDictionary.C
\*---------------------------------------------------------------------------*/
#ifndef wordAndDictionary_H
#define wordAndDictionary_H
#include "dictionary.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
class Istream;
class Ostream;
// Forward declaration of friend functions and operators
class wordAndDictionary;
Istream& operator>>(Istream&, wordAndDictionary&);
Ostream& operator<<(Ostream&, const wordAndDictionary&);
/*---------------------------------------------------------------------------*\
Class wordAndDictionary Declaration
\*---------------------------------------------------------------------------*/
class wordAndDictionary
:
public Tuple2<word, dictionary>
{
public:
// Constructors
//- Inherit tuple constructors
using Tuple2<word, dictionary>::Tuple2;
//- Construct from Istream
wordAndDictionary(Istream&);
//- Destructor
~wordAndDictionary();
// IOstream Operators
friend Istream& operator>>(Istream&, wordAndDictionary&);
friend Ostream& operator<<(Ostream&, const wordAndDictionary&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -25,6 +25,7 @@ License
#include "fieldAverageItem.H" #include "fieldAverageItem.H"
#include "fieldAverage.H" #include "fieldAverage.H"
#include "wordAndDictionary.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -46,25 +47,13 @@ Foam::functionObjects::fieldAverageItem::fieldAverageItem
"(Foam::Istream&, Foam::functionObjects::fieldAverageItem&)" "(Foam::Istream&, Foam::functionObjects::fieldAverageItem&)"
); );
token fieldNameToken(is); wordAndDictionary wd(is);
fieldName_ = fieldNameToken.wordToken();
token nextToken(is); fieldName_ = wd.first();
is.putBack(nextToken);
if (nextToken.isPunctuation() && nextToken.pToken() == token::BEGIN_BLOCK) mean_ = wd.second().lookupOrDefault<Switch>("mean", fa.mean_);
{ prime2Mean_ =
const dictionary entry(dictionary::null, is); wd.second().lookupOrDefault<Switch>("prime2Mean", fa.prime2Mean_);
mean_ = entry.lookupOrDefault<Switch>("mean", fa.mean_);
prime2Mean_ =
entry.lookupOrDefault<Switch>("prime2Mean", fa.prime2Mean_);
}
else
{
mean_ = fa.mean_;
prime2Mean_ = fa.prime2Mean_;
}
meanFieldName_ = IOobject::groupName meanFieldName_ = IOobject::groupName
( (