From 81188fefaa9cc628b9a7ea848b587ac8bb892b34 Mon Sep 17 00:00:00 2001 From: Will Bainbridge Date: Wed, 8 Apr 2020 08:37:59 +0100 Subject: [PATCH] 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))" --- src/OpenFOAM/Make/files | 1 + .../functionObjectList/functionObjectList.C | 63 ++++++---- .../functionObjectList/wordAndDictionary.C | 79 ++++++++++++ .../functionObjectList/wordAndDictionary.H | 115 ++++++++++++++++++ .../fieldAverageItem/fieldAverageItemIO.C | 23 +--- 5 files changed, 239 insertions(+), 42 deletions(-) create mode 100644 src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.C create mode 100644 src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.H diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index a4acd62ded..955dae636e 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -268,6 +268,7 @@ $(dll)/codedBase/codedBase.C db/functionObjects/functionObject/functionObject.C db/functionObjects/functionObjectList/functionObjectList.C +db/functionObjects/functionObjectList/wordAndDictionary.C db/functionObjects/writeFile/writeFile.C db/functionObjects/logFiles/logFiles.C db/functionObjects/writeObjectsBase/writeObjectsBase.C diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index 6a01e21e87..663fcfdc4a 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -33,6 +33,8 @@ License #include "Tuple2.H" #include "etcFiles.H" #include "IOdictionary.H" +#include "wordAndDictionary.H" + /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ @@ -334,15 +336,19 @@ bool Foam::functionObjectList::readFunctionObject // Store the funcDict as read for error reporting context const dictionary funcDict0(funcDict); - // Insert the 'field' and/or 'fields' entry corresponding to the optional - // arguments or read the 'field' or 'fields' entry and add the required - // fields to requiredFields - wordList fieldArgs(args); + // Insert the 'field' and/or 'fields' and 'objects' entries corresponding + // to both the arguments and the named arguments + DynamicList fieldArgs; + forAll(args, i) + { + fieldArgs.append(wordAndDictionary(args[i], dictionary::null)); + } forAll(namedArgs, i) { if (namedArgs[i].first() == "field") { - fieldArgs.append(namedArgs[i].second()); + IStringStream iss(namedArgs[i].second()); + fieldArgs.append(wordAndDictionary(iss)); } if ( @@ -351,15 +357,16 @@ bool Foam::functionObjectList::readFunctionObject ) { IStringStream iss(namedArgs[i].second()); - fieldArgs.append(wordList(iss)); + fieldArgs.append(List(iss)); } } + if (fieldArgs.size() == 1) + { + funcDict.set("field", fieldArgs[0].first()); + funcDict.merge(fieldArgs[0].second()); + } if (fieldArgs.size() >= 1) { - if (fieldArgs.size() == 1) - { - funcDict.set("field", fieldArgs[0]); - } funcDict.set("fields", 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); - // Lookup the field and fields entries from the now expanded funcDict - // and insert into the requiredFields + // Lookup the field, fields and objects entries from the now expanded + // funcDict and insert into the requiredFields dictionary& expandedFuncDict = funcArgsDict.subDict(funcCallKeyword); - + if (functionObject::debug) + { + InfoInFunction + << nl << incrIndent << indent + << funcCall << expandedFuncDict + << decrIndent << endl; + } if (expandedFuncDict.found("field")) { - requiredFields.insert - ( - word(expandedFuncDict.lookup("field")) - ); + requiredFields.insert(word(expandedFuncDict.lookup("field"))); } if (expandedFuncDict.found("fields")) { - requiredFields.insert - ( - wordOrDictNameList(expandedFuncDict.lookup("fields")) - ); + List fields(expandedFuncDict.lookup("fields")); + forAll(fields, i) + { + requiredFields.insert(fields[i].first()); + } } if (expandedFuncDict.found("objects")) { - requiredFields.insert - ( - wordOrDictNameList(expandedFuncDict.lookup("objects")) - ); + List objects(expandedFuncDict.lookup("objects")); + forAll(objects, i) + { + requiredFields.insert(objects[i].first()); + } } // Merge this functionObject dictionary into functionsDict diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.C b/src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.C new file mode 100644 index 0000000000..964a274b63 --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.C @@ -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 . + +\*---------------------------------------------------------------------------*/ + +#include "wordAndDictionary.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::wordAndDictionary::wordAndDictionary(Istream& is) +: + Tuple2() +{ + 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; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.H b/src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.H new file mode 100644 index 0000000000..c7a1ca3b20 --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/wordAndDictionary.H @@ -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 . + +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 +{ +public: + + // Constructors + + //- Inherit tuple constructors + using Tuple2::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 + +// ************************************************************************* // diff --git a/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItemIO.C b/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItemIO.C index dd81ad86b4..d076c026d2 100644 --- a/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItemIO.C +++ b/src/functionObjects/field/fieldAverage/fieldAverageItem/fieldAverageItemIO.C @@ -25,6 +25,7 @@ License #include "fieldAverageItem.H" #include "fieldAverage.H" +#include "wordAndDictionary.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -46,25 +47,13 @@ Foam::functionObjects::fieldAverageItem::fieldAverageItem "(Foam::Istream&, Foam::functionObjects::fieldAverageItem&)" ); - token fieldNameToken(is); - fieldName_ = fieldNameToken.wordToken(); + wordAndDictionary wd(is); - token nextToken(is); - is.putBack(nextToken); + fieldName_ = wd.first(); - if (nextToken.isPunctuation() && nextToken.pToken() == token::BEGIN_BLOCK) - { - const dictionary entry(dictionary::null, is); - - mean_ = entry.lookupOrDefault("mean", fa.mean_); - prime2Mean_ = - entry.lookupOrDefault("prime2Mean", fa.prime2Mean_); - } - else - { - mean_ = fa.mean_; - prime2Mean_ = fa.prime2Mean_; - } + mean_ = wd.second().lookupOrDefault("mean", fa.mean_); + prime2Mean_ = + wd.second().lookupOrDefault("prime2Mean", fa.prime2Mean_); meanFieldName_ = IOobject::groupName (