ENH: Added binary operations to fieldValueDelta function object

This commit is contained in:
andy
2012-11-21 10:25:12 +00:00
parent dd1c6c4167
commit a959387b56
3 changed files with 106 additions and 6 deletions

View File

@ -29,8 +29,25 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(Foam::fieldValues::fieldValueDelta, 0);
template<>
const char*
NamedEnum<fieldValues::fieldValueDelta::operationType, 4>::names[] =
{
"add",
"subtract",
"min",
"max"
};
const Foam::NamedEnum<Foam::fieldValues::fieldValueDelta::operationType, 4>
Foam::fieldValues::fieldValueDelta::operationTypeNames_;
}
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::fieldValues::fieldValueDelta::updateMesh(const mapPolyMesh&)
@ -60,6 +77,7 @@ Foam::fieldValues::fieldValueDelta::fieldValueDelta
obr_(obr),
loadFromFiles_(loadFromFiles),
log_(false),
operation_(opSubtract),
source1Ptr_(NULL),
source2Ptr_(NULL)
{
@ -82,14 +100,20 @@ void Foam::fieldValues::fieldValueDelta::writeFileHeader(const label i)
}
}
file() << "# Time";
Ostream& os = file();
os << "# Source1 : " << source1Ptr_->name() << nl
<< "# Source2 : " << source2Ptr_->name() << nl
<< "# Operation : " << operationTypeNames_[operation_] << nl;
os << "# Time";
forAll(commonFields, i)
{
file()<< tab << commonFields[i];
os << tab << commonFields[i];
}
file() << endl;
os << endl;
}
@ -126,6 +150,8 @@ void Foam::fieldValues::fieldValueDelta::read(const dictionary& dict)
false
).ptr()
);
operation_ = operationTypeNames_.read(dict.lookup("operation"));
}

View File

@ -85,6 +85,19 @@ class fieldValueDelta
:
public functionObjectFile
{
public:
//- Operation type enumeration
enum operationType
{
opAdd,
opSubtract,
opMin,
opMax
};
//- Operation type names
static const NamedEnum<operationType, 4> operationTypeNames_;
private:
@ -102,6 +115,9 @@ private:
//- Switch to send output to Info as well as to file
Switch log_;
//- Operation to apply to values
operationType operation_;
//- Field value source object 1
autoPtr<fieldValue> source1Ptr_;
@ -115,6 +131,10 @@ private:
template<class Type>
void processFields(bool& found);
//- Templated function to apply the operation
template<class Type>
Type applyOperation(const Type& value1, const Type& value2) const;
protected:

View File

@ -28,6 +28,57 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
Type Foam::fieldValues::fieldValueDelta::applyOperation
(
const Type& value1,
const Type& value2
) const
{
Type result = pTraits<Type>::zero;
switch (operation_)
{
case opAdd:
{
result = value1 + value2;
break;
}
case opSubtract:
{
result = value1 - value2;
break;
}
case opMin:
{
result = min(value1, value2);
break;
}
case opMax:
{
result = max(value1, value2);
break;
}
default:
{
FatalErrorIn
(
"Type Foam::fieldValues::fieldValueDelta::applyOperation"
"("
"const Type&, "
"const Type&"
") const"
)
<< "Unable to process operation "
<< operationTypeNames_[operation_]
<< abort(FatalError);
}
}
return result;
}
template<class Type>
void Foam::fieldValues::fieldValueDelta::processFields(bool& found)
{
@ -49,15 +100,18 @@ void Foam::fieldValues::fieldValueDelta::processFields(bool& found)
results1.lookup(fieldName) >> r1;
results2.lookup(fieldName) >> r2;
Type result = applyOperation(r1, r2);
if (log_)
{
Info<< " field: " << fieldName << ", delta: " << r2 - r1
Info<< " " << operationTypeNames_[operation_]
<< "(" << fieldName << ") = " << result
<< endl;
}
if (Pstream::master())
{
file()<< tab << r2 - r1;
file()<< tab << result;
}
found = true;