Commit ea12bfdb authored by Andrew Heather's avatar Andrew Heather Committed by Mark OLESEN
Browse files

ENH: new multiFieldValue function object

    Computes a selected operation between multiple \c fieldValue function
    objects.

    The operation is applied to all results of each \c fieldValue object.

Note
    Each object must generate the same number and type of results.

Usage
    Minimal example by using \c system/controlDict.functions:

    multiFieldValue1
    {
        // Mandatory entries (unmodifiable)
        type            multiFieldValue;
        libs            (fieldFunctionObjects);

        // Mandatory entries (runtime modifiable)
        operation       subtract;

        // List of fieldValue function objects as dictionaries
        functions
        {
            region1
            {
                ...
            }
            region2
            {
                ...
            }

            ...

            regionN
            {
                ...
            }
        }

        // Optional (inherited) entries
        ...
    }

    where the entries mean:

      Property  ...
parent b6e87680
......@@ -6,7 +6,7 @@
\\/ M anipulation |
\*---------------------------------------------------------------------------*/
type fieldValueDelta;
type multiFieldValue;
libs ("libfieldFunctionObjects.so");
operation subtract;
......@@ -15,17 +15,20 @@ writeControl timeStep;
writeInterval 1;
log false;
region1
functions
{
#includeEtc "caseDicts/postProcessing/surfaceFieldValue/surfaceRegion.cfg"
operation areaAverage;
fields (p);
}
region2
{
#includeEtc "caseDicts/postProcessing/surfaceFieldValue/surfaceRegion.cfg"
operation areaAverage;
fields (p);
region1
{
#includeEtc "caseDicts/postProcessing/surfaceFieldValue/surfaceRegion.cfg"
operation areaAverage;
fields (p);
}
region2
{
#includeEtc "caseDicts/postProcessing/surfaceFieldValue/surfaceRegion.cfg"
operation areaAverage;
fields (p);
}
}
// ************************************************************************* //
......@@ -16,9 +16,9 @@ fieldMinMax/fieldMinMax.C
fieldValues/fieldValue/fieldValue.C
fieldValues/fieldValue/fieldValueNew.C
fieldValues/fieldValueDelta/fieldValueDelta.C
fieldValues/volFieldValue/volFieldValue.C
fieldValues/surfaceFieldValue/surfaceFieldValue.C
fieldValues/multiFieldValue/multiFieldValue.C
heatTransferCoeff/heatTransferCoeff.C
heatTransferCoeff/heatTransferCoeffModels/heatTransferCoeffModel/heatTransferCoeffModel.C
......
......@@ -37,7 +37,7 @@ namespace Foam
namespace functionObjects
{
defineTypeNameAndDebug(fieldValue, 0);
defineRunTimeSelectionTable(fieldValue, dictionary);
defineRunTimeSelectionTable(fieldValue, runTime);
}
}
......@@ -55,7 +55,7 @@ Foam::functionObjects::fieldValue::fieldValue
fvMeshFunctionObject(name, runTime, dict),
writeFile(obr_, name, valueType, dict),
writeFields_(false),
regionName_(word::null),
regionName_(),
scaleFactor_(1.0),
dict_(dict),
fields_()
......@@ -75,7 +75,7 @@ Foam::functionObjects::fieldValue::fieldValue
fvMeshFunctionObject(name, obr, dict),
writeFile(obr_, name, valueType, dict),
writeFields_(false),
regionName_(word::null),
regionName_(),
scaleFactor_(1.0),
dict_(dict),
fields_()
......@@ -88,19 +88,21 @@ Foam::functionObjects::fieldValue::fieldValue
bool Foam::functionObjects::fieldValue::read(const dictionary& dict)
{
if (dict != dict_)
if (fvMeshFunctionObject::read(dict) && writeFile::read(dict))
{
dict_ = dict;
}
if (dict != dict_)
{
dict_ = dict;
}
fvMeshFunctionObject::read(dict);
writeFile::read(dict);
dict.readEntry("writeFields", writeFields_);
scaleFactor_ = dict.getOrDefault<scalar>("scaleFactor", 1.0);
dict.readEntry("fields", fields_);
dict.readEntry("writeFields", writeFields_);
scaleFactor_ = dict.getOrDefault<scalar>("scaleFactor", 1.0);
dict.readEntry("fields", fields_);
return true;
}
return true;
return false;
}
......
......@@ -98,7 +98,6 @@ class fieldValue
public fvMeshFunctionObject,
public writeFile
{
protected:
// Protected Data
......@@ -141,16 +140,15 @@ public:
(
autoPtr,
fieldValue,
dictionary,
runTime,
(
const word& name,
const objectRegistry& obr,
const Time& runTime,
const dictionary& dict
),
(name, obr, dict)
(name, runTime, dict)
);
// Constructors
//- Construct from Time and dictionary
......@@ -175,7 +173,7 @@ public:
static autoPtr<fieldValue> New
(
const word& name,
const objectRegistry& obr,
const Time& runTime,
const dictionary& dict,
const bool output = true
);
......
......@@ -34,7 +34,7 @@ Foam::autoPtr<Foam::functionObjects::fieldValue>
Foam::functionObjects::fieldValue::New
(
const word& name,
const objectRegistry& obr,
const Time& runTime,
const dictionary& dict,
const bool output
)
......@@ -46,7 +46,7 @@ Foam::functionObjects::fieldValue::New
Info<< "Selecting " << typeName << ' ' << modelType << endl;
}
auto cstrIter = dictionaryConstructorTablePtr_->cfind(modelType);
auto cstrIter = runTimeConstructorTablePtr_->cfind(modelType);
if (!cstrIter.found())
{
......@@ -55,12 +55,13 @@ Foam::functionObjects::fieldValue::New
dict,
typeName,
modelType,
*dictionaryConstructorTablePtr_
*runTimeConstructorTablePtr_
) << exit(FatalIOError);
}
return autoPtr<fieldValue>(cstrIter()(name, obr, dict));
return autoPtr<fieldValue>(cstrIter()(name, runTime, dict));
}
// ************************************************************************* //
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2016 OpenFOAM Foundation
Copyright (C) 2015-2020 OpenCFD Ltd.
Copyright (C) 2015-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -26,7 +26,7 @@ License
\*---------------------------------------------------------------------------*/
#include "fieldValueDelta.H"
#include "multiFieldValue.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -37,8 +37,8 @@ namespace functionObjects
{
namespace fieldValues
{
defineTypeNameAndDebug(fieldValueDelta, 0);
addToRunTimeSelectionTable(functionObject, fieldValueDelta, dictionary);
defineTypeNameAndDebug(multiFieldValue, 0);
addToRunTimeSelectionTable(functionObject, multiFieldValue, dictionary);
}
}
}
......@@ -46,10 +46,11 @@ namespace fieldValues
const Foam::Enum
<
Foam::functionObjects::fieldValues::fieldValueDelta::operationType
Foam::functionObjects::fieldValues::multiFieldValue::operationType
>
Foam::functionObjects::fieldValues::fieldValueDelta::operationTypeNames_
Foam::functionObjects::fieldValues::multiFieldValue::operationTypeNames_
({
{ operationType::opSum, "sum" },
{ operationType::opAdd, "add" },
{ operationType::opSubtract, "subtract" },
{ operationType::opMin, "min" },
......@@ -60,32 +61,50 @@ Foam::functionObjects::fieldValues::fieldValueDelta::operationTypeNames_
// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
void Foam::functionObjects::fieldValues::fieldValueDelta::writeFileHeader
void Foam::functionObjects::fieldValues::multiFieldValue::writeFileHeader
(
Ostream& os
) const
{
const wordList& fields1 = region1Ptr_->fields();
const wordList& fields2 = region2Ptr_->fields();
const wordList& fields0 = functions_[0].fields();
DynamicList<word> commonFields(fields1.size());
forAll(fields1, fieldi)
DynamicList<word> commonFields(fields0.size());
for (const word& fieldName : fields0)
{
label index = fields2.find(fields1[fieldi]);
if (index != -1)
bool common = true;
for (label functioni=1; functioni < functions_.size(); ++functioni)
{
if (!functions_[functioni].fields().found(fieldName))
{
common = false;
break;
}
}
if (common)
{
commonFields.append(fields1[fieldi]);
commonFields.append(fieldName);
}
}
writeHeaderValue(os, "Source1", region1Ptr_->name());
writeHeaderValue(os, "Source2", region2Ptr_->name());
forAll(functions_, functioni)
{
writeHeaderValue
(
os,
"Source" + Foam::name(functioni),
functions_[functioni].name()
);
}
writeHeaderValue(os, "Operation", operationTypeNames_[operation_]);
writeCommented(os, "Time");
forAll(commonFields, i)
for (const word& fieldName : commonFields)
{
os << tab << commonFields[i];
os << tab << fieldName;
}
os << endl;
......@@ -94,123 +113,183 @@ void Foam::functionObjects::fieldValues::fieldValueDelta::writeFileHeader
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::functionObjects::fieldValues::fieldValueDelta::fieldValueDelta
Foam::functionObjects::fieldValues::multiFieldValue::multiFieldValue
(
const word& name,
const Time& runTime,
const dictionary& dict
)
:
fvMeshFunctionObject(name, runTime, dict),
writeFile(obr_, name, typeName, dict),
stateFunctionObject(name, runTime),
writeFile(runTime, name, typeName, dict),
operation_(opSubtract),
region1Ptr_(nullptr),
region2Ptr_(nullptr)
functions_()
{
read(dict);
writeFileHeader(file());
if (read(dict))
{
writeFileHeader(file());
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionObjects::fieldValues::fieldValueDelta::read
bool Foam::functionObjects::fieldValues::multiFieldValue::read
(
const dictionary& dict
)
{
fvMeshFunctionObject::read(dict);
writeFile::read(dict);
if (stateFunctionObject::read(dict) && writeFile::read(dict))
{
const dictionary& functionsDict = dict.subDict("functions");
functions_.resize(functionsDict.size());
region1Ptr_.reset
(
fieldValue::New
(
name() + ".region1",
obr_,
dict.subDict("region1"),
false
).ptr()
);
region2Ptr_.reset
(
fieldValue::New
(
name() + ".region2",
obr_,
dict.subDict("region2"),
false
).ptr()
);
if (functions_.empty())
{
WarningInFunction
<< "No functions specified"
<< endl;
return false;
}
operation_ = operationTypeNames_.get("operation", dict);
label functioni = 0;
for (const entry& dEntry : functionsDict)
{
if (!dEntry.isDict())
{
FatalIOErrorInFunction(dict)
<< "Functions must be specified in dictionary format"
<< exit(FatalIOError);
}
const dictionary& localDict = dEntry.dict();
functions_.set
(
functioni,
fieldValue::New
(
IOobject::scopedName(name(), localDict.dictName()),
time(),
localDict,
false
)
);
++functioni;
}
return true;
operation_ = operationTypeNames_.get("operation", dict);
return true;
}
return false;
}
bool Foam::functionObjects::fieldValues::fieldValueDelta::write()
bool Foam::functionObjects::fieldValues::multiFieldValue::write()
{
region1Ptr_->write();
region2Ptr_->write();
writeCurrentTime(file());
if (functions_.empty())
{
return false;
}
Log << type() << " " << name() << " write:" << endl;
const word& name1 = region1Ptr_->name();
const word& name2 = region2Ptr_->name();
const label nFunction = functions_.size();
wordList entries0;
label nEntries = -1;
const wordList entries1 = objectResultEntries(name1);
const wordList entries2 = objectResultEntries(name2);
wordList names(nFunction);
List<wordList> entries;
List<wordList> types;
if (entries1.size() != entries2.size())
forAll(functions_, functioni)
{
FatalErrorInFunction
<< name() << ": objects must generate the same number of results"
<< nl
<< " " << name1 << " objects: " << entries1 << nl
<< " " << name2 << " objects: " << entries2 << nl
<< exit(FatalError);
}
auto& f = functions_[functioni];
names[functioni] = f.name();
forAll(entries1, i)
{
const word& entry1(entries1[i]);
const word& entry2(entries2[i]);
const word type1 = objectResultType(name1, entry1);
const word type2 = objectResultType(name2, entry2);
// Note: results are not available until the call to write()
f.write();
const wordList e(objectResultEntries(f.name()));
if (functioni == 0)
{
entries0 = e;
nEntries = e.size();
entries.resize(nEntries);
types.resize(nEntries);
forAll(entries, entryi)
{
entries[entryi].resize(nFunction);
types[entryi].resize(nFunction);
}
}
if (type1 != type2)
if (e.size() != nEntries)
{
const word& f0Name = functions_[0].name();
FatalErrorInFunction
<< name()
<< ": input values for operation must be of the same type"
<< nl
<< " " << entry1 << ": " << type1 << nl
<< " " << entry2 << ": " << type2 << nl
<< exit(FatalError);
<< "Inconsistent number of result entries" << nl
<< " " << f0Name << " entries:" << entries0 << nl
<< " " << f.name() << " entries:" << e
<< abort(FatalError);
}
bool found = false;
forAll(e, entryi)
{
entries[entryi][functioni] = e[entryi];
types[entryi][functioni] = objectResultType(f.name(), e[entryi]);
}
}
writeCurrentTime(file());
applyOperation<scalar>(type1, name1, name2, entry1, entry2, found);
applyOperation<vector>(type1, name1, name2, entry1, entry2, found);
applyOperation<sphericalTensor>
(type1, name1, name2, entry1, entry2, found);
applyOperation<symmTensor>(type1, name1, name2, entry1, entry2, found);
applyOperation<tensor>(type1, name1, name2, entry1, entry2, found);
forAll(entries, entryi)
{
const wordList& entriesi = entries[entryi];
const word& t0 = types[entryi][0];
const wordList& typesi = types[entryi];
forAll(typesi, functioni)
{
const word& t = typesi[functioni];
if (t != t0)
{
FatalErrorInFunction
<< "Inconsistent function result types" << nl
<< " " << functions_[0].name()
<< " result type:" << t0 << nl
<< " " << functions_[functioni].name()
<< " result type:" << typesi[functioni]
<< abort(FatalError);
}
}
if (!found)
const bool ok
(
applyOperation<scalar>(t0, names, entriesi)
|| applyOperation<vector>(t0, names, entriesi)
|| applyOperation<sphericalTensor>(t0, names, entriesi)
|| applyOperation<symmTensor>(t0, names, entriesi)
|| applyOperation<tensor>(t0, names, entriesi)
);
if (!ok)
{
Log << "Operation between "
<< name1 << " with result " << entry1 << " and "
<< name2 << " with result " << entry2 << " not applied"
Log << "Operation not applied between functions:" << nl
<< flatOutput(names, FlatOutput::BareComma{}) << nl
<< "with result names:" << nl
<< flatOutput(entriesi, FlatOutput::BareComma{})
<< endl;
}
}
Log << (entries1.empty() ? " none" : "") << endl;
Log << (nEntries == 0 ? " none" : "") << endl;
file()<< endl;
......@@ -218,7 +297,7 @@ bool Foam::functionObjects::fieldValues::fieldValueDelta::write()
}
bool Foam::functionObjects::fieldValues::fieldValueDelta::execute()
bool Foam::functionObjects::fieldValues::multiFieldValue::execute()
{
return true;
}
......
......@@ -5,8 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2012-2016 OpenFOAM Foundation
Copyright (C) 2015-2020 OpenCFD Ltd.
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -25,35 +24,50 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::functionObjects::fieldValues::fieldValueDelta
Foam::functionObjects::fieldValues::multiFieldValue
Group
grpFieldFunctionObjects
Description
Computes a selected operation between two \c fieldValue function objects.
Computes a selected operation between multiple \c fieldValue function
objects.
The operation is applied to all results of each \c fieldValue object.
Accordingly, each object must generate the same number and type of results.
Note
Each object must generate the same number and type of results.
Usage
Minimal example by using \c system/controlDict.functions:
\verbatim
fieldValueDelta1
multiFieldValue1
{
// Mandatory entries (unmodifiable)
type fieldValueDelta;
libs (fieldFunctionObjects);
type multiFieldValue;
libs (fieldFunctionObjects);