/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2026 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 "volFieldValue.H"
#include "volFields.H"
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
template
bool Foam::functionObjects::fieldValues::volFieldValue::validField
(
const word& fieldName
) const
{
if
(
obr_.foundObject>(fieldName)
|| obr_.foundObject>(fieldName)
)
{
return true;
}
return false;
}
template
Foam::tmp>
Foam::functionObjects::fieldValues::volFieldValue::getFieldValues
(
const word& fieldName
) const
{
if (obr_.foundObject>(fieldName))
{
return filterField(obr_.lookupObject>(fieldName));
}
if (obr_.foundObject>(fieldName))
{
return filterField
(
obr_.lookupObject>(fieldName)
);
}
FatalErrorInFunction
<< "Field " << fieldName << " not found in database"
<< abort(FatalError);
return tmp>(nullptr);
}
template
void Foam::functionObjects::fieldValues::volFieldValue::compareScalars
(
const scalarField& values,
const scalar emptyVal,
Result& result,
const Op& op
) const
{
// Check if the zone on this processor is non-zero sized
if (values.size())
{
label i = 0;
forAll(values, j)
{
if (op(values[j], values[i]))
{
i = j;
}
}
result.value = values[i];
result.celli = zone_.celli(i);
result.cc = fieldValue::mesh_.C()[result.celli];
}
else
{
result.value = emptyVal;
}
result.proci = Pstream::parRun() ? Pstream::myProcNo() : -1;
reduce
(
result,
[&op](const Result& a, const Result& b)
{
return op(a.value, b.value) ? a : b;
}
);
}
template
bool Foam::functionObjects::fieldValues::volFieldValue::processValues
(
const Field& values,
const scalarField& weights,
const scalarField& V,
Result& result
) const
{
return false;
}
template
bool Foam::functionObjects::fieldValues::volFieldValue::processValues
(
const Field& values,
const scalarField& weights,
const scalarField& V,
Result& result
) const
{
return processValuesTypeType(values, weights, V, result);
}
template
bool Foam::functionObjects::fieldValues::volFieldValue::processValues
(
const Field& values,
const scalarField& weights,
const scalarField& V,
Result& result
) const
{
switch (operation_)
{
case operationType::minMag:
{
compareScalars(mag(values), vGreat, result, lessOp());
return true;
}
case operationType::maxMag:
{
compareScalars(mag(values), -vGreat, result, greaterOp());
return true;
}
default:
{
return false;
}
}
}
template
bool Foam::functionObjects::fieldValues::volFieldValue::processValuesTypeType
(
const Field& values,
const scalarField& weights,
const scalarField& V,
Result& result
) const
{
switch (operation_)
{
case operationType::sum:
{
result.value = gSum(weights*values);
return true;
}
case operationType::sumMag:
{
result.value = gSum(cmptMag(values));
return true;
}
case operationType::average:
{
result.value = gSum(weights*values)/max(gSum(weights), vSmall);
return true;
}
case operationType::volAverage:
{
result.value = gSum(weights*V*values)/max(gSum(weights*V), vSmall);
return true;
}
case operationType::volIntegrate:
{
result.value = gSum(weights*V*values);
return true;
}
case operationType::min:
{
result.value = gMin(values);
return true;
}
case operationType::max:
{
result.value = gMax(values);
return true;
}
case operationType::CoV:
{
const scalar sumWeightsV = max(gSum(weights*V), vSmall);
const Type meanValue = gSum(weights*V*values)/sumWeightsV;
const label nComp = pTraits::nComponents;
for (direction d=0; d::nComponents;
for (direction d=0; d
bool Foam::functionObjects::fieldValues::volFieldValue::writeValues
(
const word& fieldName,
const scalarField& weights,
const scalarField& V
)
{
const bool ok = validField(fieldName);
if (ok)
{
// Get the values
Field values(getFieldValues(fieldName));
// Write raw values if specified
if (writeFields_)
{
IOField
(
IOobject
(
fieldName + '_' + zone_.name(),
time_.name(),
obr_,
IOobject::NO_READ,
IOobject::NO_WRITE
),
weights*values
).write();
}
// Do the operation
if (operation_ != operationType::none)
{
// Apply scale factor
values *= scaleFactor_;
bool ok = false;
#define writeValuesFieldType(fieldType, none) \
ok = \
ok \
|| writeValues \
( \
fieldName, \
values, \
weights, \
V \
);
FOR_ALL_FIELD_TYPES(writeValuesFieldType);
#undef writeValuesFieldType
if (!ok)
{
FatalErrorInFunction
<< "Operation " << operationTypeNames_[operation_]
<< " not available for values of type "
<< pTraits::typeName
<< exit(FatalError);
}
}
}
return ok;
}
template
bool Foam::functionObjects::fieldValues::volFieldValue::writeValues
(
const word& fieldName,
const Field& values,
const scalarField& weights,
const scalarField& V
)
{
Result result({Zero, -1, -1, point::uniform(NaN)});
if (processValues(values, weights, V, result))
{
// Add to result dictionary, over-writing any previous entry
resultDict_.add(fieldName, result.value, true);
if (Pstream::master())
{
file() << tab << result.value;
Log << " " << operationTypeNames_[operation_]
<< "(" << zone_.name() << ") of " << fieldName
<< " = " << result.value;
if (result.celli != -1)
{
Log << " at location " << result.cc;
if (writeLocation_) file() << tab << result.cc;
}
if (result.celli != -1)
{
Log << " in cell " << result.celli;
if (writeLocation_) file() << tab << result.celli;
}
if (result.proci != -1)
{
Log << " on processor " << result.proci;
if (writeLocation_) file() << tab << result.proci;
}
Log << endl;
}
return true;
}
return false;
}
template
Foam::tmp>
Foam::functionObjects::fieldValues::volFieldValue::filterField
(
const Field& field
) const
{
if (zone_.all())
{
return field;
}
else
{
return tmp>(new Field(field, zone_.zone()));
}
}
// ************************************************************************* //