Commit d26b4ee4 authored by Andrew Heather's avatar Andrew Heather
Browse files

ENH: fieldValues FOs updated following update to use functionObjectState

parent 40680a43
......@@ -79,8 +79,6 @@ void Foam::fieldValues::cellSource::setCellZoneCells()
{
case stCellZone:
{
dict().lookup("sourceName") >> sourceName_;
label zoneId = mesh().cellZones().findZoneID(sourceName_);
if (zoneId < 0)
......@@ -135,7 +133,8 @@ void Foam::fieldValues::cellSource::initialise(const dictionary& dict)
WarningIn
(
"Foam::fieldValues::cellSource::initialise(const dictionary&)"
) << type() << " " << name_ << ": "
)
<< type() << " " << name_ << ": "
<< sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
<< " Source has no cells - deactivating" << endl;
......@@ -145,44 +144,46 @@ void Foam::fieldValues::cellSource::initialise(const dictionary& dict)
volume_ = volume();
Info<< type() << " " << name_ << ":"
<< sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
<< " total cells = " << nCells_ << nl
<< " total volume = " << volume_
<< nl << endl;
if (log_)
{
Info<< type() << " " << name_ << ":"
<< sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl
<< " total cells = " << nCells_ << nl
<< " total volume = " << volume_
<< nl << endl;
}
if (dict.readIfPresent("weightField", weightFieldName_))
{
Info<< " weight field = " << weightFieldName_;
if (log_) Info << " weight field = " << weightFieldName_;
}
Info<< nl << endl;
if (log_) Info << nl << endl;
}
void Foam::fieldValues::cellSource::writeFileHeader(const label i)
void Foam::fieldValues::cellSource::writeFileHeader(Ostream& os) const
{
writeCommented(file(), "Source : ");
file() << sourceTypeNames_[source_] << " " << sourceName_ << endl;
writeCommented(file(), "Cells : ");
file() << nCells_ << endl;
writeCommented(file(), "Volume : ");
file() << volume_ << endl;
writeCommented(file(), "Time");
writeHeaderValue(os, "Source", sourceTypeNames_[source_]);
writeHeaderValue(os, "Name", sourceName_);
writeHeaderValue(os, "Cells", nCells_);
writeHeaderValue(os, "Volume", volume_);
writeHeaderValue(os, "Scale factor", scaleFactor_);
writeCommented(os, "Time");
if (writeVolume_)
{
file() << tab << "Volume";
os << tab << "Volume";
}
forAll(fields_, i)
{
file()
<< tab << operationTypeNames_[operation_]
os << tab << operationTypeNames_[operation_]
<< "(" << fields_[i] << ")";
}
file() << endl;
os << endl;
}
......@@ -204,7 +205,11 @@ Foam::fieldValues::cellSource::cellSource
weightFieldName_("none"),
writeVolume_(dict.lookupOrDefault("writeVolume", false))
{
read(dict);
if (active_)
{
read(dict);
writeFileHeader(file());
}
}
......@@ -218,11 +223,11 @@ Foam::fieldValues::cellSource::~cellSource()
void Foam::fieldValues::cellSource::read(const dictionary& dict)
{
fieldValue::read(dict);
if (active_)
{
// no additional info to read
fieldValue::read(dict);
// No additional info to read
initialise(dict);
}
}
......@@ -234,33 +239,34 @@ void Foam::fieldValues::cellSource::write()
if (active_)
{
if (Pstream::master())
file() << obr_.time().value();
// Construct weight field. Note: zero size indicates unweighted
scalarField weightField;
if (weightFieldName_ != "none")
{
file() << obr_.time().value();
weightField = setFieldValues<scalar>(weightFieldName_, true);
}
if (writeVolume_)
{
volume_ = volume();
if (Pstream::master())
{
file() << tab << volume_;
}
file() << tab << volume_;
if (log_) Info<< " total volume = " << volume_ << endl;
}
forAll(fields_, i)
{
const word& fieldName = fields_[i];
bool processed = false;
bool ok = false;
processed = processed || writeValues<scalar>(fieldName);
processed = processed || writeValues<vector>(fieldName);
processed = processed || writeValues<sphericalTensor>(fieldName);
processed = processed || writeValues<symmTensor>(fieldName);
processed = processed || writeValues<tensor>(fieldName);
ok = ok || writeValues<scalar>(fieldName, weightField);
ok = ok || writeValues<vector>(fieldName, weightField);
ok = ok || writeValues<sphericalTensor>(fieldName, weightField);
ok = ok || writeValues<symmTensor>(fieldName, weightField);
ok = ok || writeValues<tensor>(fieldName, weightField);
if (!processed)
if (!ok)
{
WarningIn("void Foam::fieldValues::cellSource::write()")
<< "Requested field " << fieldName
......@@ -269,10 +275,7 @@ void Foam::fieldValues::cellSource::write()
}
}
if (Pstream::master())
{
file()<< endl;
}
file()<< endl;
if (log_) Info<< endl;
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -62,11 +62,11 @@ Description
log | Write data to standard output | no | no
valueOutput | Write the raw output values | yes |
writeVolume | Write the volume of the cellSource | no |
source | cell source: see below | yes |
sourceName | name of cell source if required | no |
operation | operation to perform | yes |
weightField | name of field to apply weighting | no |
fields | list of fields to operate on | yes |
source | Cell source: see below | yes |
sourceName | Name of cell source if required | no |
operation | Operation to perform | yes |
weightField | Name of field to apply weighting | no |
fields | List of fields to operate on | yes |
\endtable
\linebreak
......@@ -228,7 +228,7 @@ protected:
) const;
//- Output file header information
virtual void writeFileHeader(const label i);
virtual void writeFileHeader(Ostream& os) const;
public:
......@@ -272,7 +272,11 @@ public:
//- Templated helper function to output field values
template<class Type>
bool writeValues(const word& fieldName);
bool writeValues
(
const word& fieldName,
const scalarField& weightField
);
//- Filter a field according to cellIds
template<class Type>
......
......@@ -87,52 +87,65 @@ Type Foam::fieldValues::cellSource::processValues
{
case opSum:
{
result = sum(values);
result = gSum(values);
break;
}
case opSumMag:
{
result = sum(cmptMag(values));
result = gSum(cmptMag(values));
break;
}
case opAverage:
{
result = sum(values)/values.size();
label n = returnReduce(values.size(), sumOp<label>());
result = gSum(values)/(scalar(n) + ROOTVSMALL);
break;
}
case opWeightedAverage:
{
result = sum(weightField*values)/sum(weightField);
label wSize = returnReduce(weightField.size(), sumOp<label>());
if (wSize > 0)
{
result = gSum(weightField*values)/(gSum(weightField) + ROOTVSMALL);
}
else
{
label n = returnReduce(values.size(), sumOp<label>());
result = gSum(values)/(scalar(n) + ROOTVSMALL);
}
break;
}
case opVolAverage:
{
result = sum(V*values)/sum(V);
result = gSum(values*V)/(gSum(V) + ROOTVSMALL);
break;
}
case opWeightedVolAverage:
{
result = sum(weightField*V*values)/sum(weightField*V);
result = gSum(weightField*V*values)/gSum(weightField*V);
break;
}
case opVolIntegrate:
{
result = sum(V*values);
result = gSum(V*values);
break;
}
case opMin:
{
result = min(values);
result = gMin(values);
break;
}
case opMax:
{
result = max(values);
result = gMax(values);
break;
}
case opCoV:
{
Type meanValue = sum(values*V)/sum(V);
const scalar sumV = gSum(V);
Type meanValue = gSum(V*values)/sumV;
const label nComp = pTraits<Type>::nComponents;
......@@ -142,7 +155,7 @@ Type Foam::fieldValues::cellSource::processValues
scalar mean = component(meanValue, d);
scalar& res = setComponent(result, d);
res = sqrt(sum(V*sqr(vals - mean))/sum(V))/mean;
res = sqrt(gSum(V*sqr(vals - mean))/sumV)/(mean + ROOTVSMALL);
}
break;
......@@ -160,7 +173,11 @@ Type Foam::fieldValues::cellSource::processValues
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
bool Foam::fieldValues::cellSource::writeValues(const word& fieldName)
bool Foam::fieldValues::cellSource::writeValues
(
const word& fieldName,
const scalarField& weightField
)
{
const bool ok = validField<Type>(fieldName);
......@@ -168,26 +185,13 @@ bool Foam::fieldValues::cellSource::writeValues(const word& fieldName)
{
Field<Type> values(setFieldValues<Type>(fieldName));
scalarField V(filterField(mesh().V()));
scalarField weightField(values.size(), 1.0);
if (weightFieldName_ != "none")
if (valueOutput_)
{
weightField = setFieldValues<scalar>(weightFieldName_, true);
}
// Combine onto master
combineFields(values);
combineFields(V);
combineFields(weightField);
if (Pstream::master())
{
Type result = processValues(values, V, weightField);
// Add to result dictionary, over-writing any previous entry
resultDict_.add(fieldName, result, true);
Field<Type> allValues(values);
combineFields(allValues);
if (valueOutput_)
if (Pstream::master())
{
IOField<Type>
(
......@@ -200,17 +204,29 @@ bool Foam::fieldValues::cellSource::writeValues(const word& fieldName)
IOobject::NO_READ,
IOobject::NO_WRITE
),
weightField*values
allValues
).write();
}
}
// Apply scale factor
values *= scaleFactor_;
Type result = processValues(values, V, weightField);
file()<< tab << result;
file()<< tab << result;
if (log_) Info<< " " << operationTypeNames_[operation_]
if (log_)
{
Info<< " " << operationTypeNames_[operation_]
<< "(" << sourceName_ << ") of " << fieldName
<< " = " << result << endl;
}
// write state/results information
const word& opName = operationTypeNames_[operation_];
word resultName = opName + '(' + sourceName_ + ',' + fieldName + ')';
this->setResult(resultName, result);
}
return ok;
......
......@@ -60,10 +60,13 @@ functions
// Output field values as well
valueOutput true;
// Output format for field values
surfaceFormat vtk;
// Type of source: patch/faceZone/sampledSurface
source patch;
// if patch or faceZone: name of patch or faceZone
// If patch or faceZone: name of patch or faceZone
sourceName movingWall;
//// if sampledSurface: dictionary with a sampledSurface
......@@ -96,6 +99,7 @@ functions
outputControl outputTime;
log true;
valueOutput true;
surfaceFormat vtk;
source faceZone;
sourceName f0;
operation sum;
......
......@@ -352,7 +352,7 @@ void Foam::fieldValues::faceSource::combineSurfaceGeometry
if (Pstream::parRun())
{
// dimension as fraction of mesh bounding box
// Dimension as fraction of mesh bounding box
scalar mergeDim = 1e-10*mesh().bounds().mag();
labelList pointsMap;
......@@ -400,8 +400,6 @@ Foam::scalar Foam::fieldValues::faceSource::totalArea() const
void Foam::fieldValues::faceSource::initialise(const dictionary& dict)
{
dict.lookup("sourceName") >> sourceName_;
switch (source_)
{
case stFaceZone:
......@@ -450,15 +448,14 @@ void Foam::fieldValues::faceSource::initialise(const dictionary& dict)
totalArea_ = totalArea();
Info<< type() << " " << name_ << ":" << nl
<< " total faces = " << nFaces_
<< nl
<< " total area = " << totalArea_
<< nl;
if (log_) Info
<< type() << " " << name_ << ":" << nl
<< " total faces = " << nFaces_ << nl
<< " total area = " << totalArea_ << nl;
if (dict.readIfPresent("weightField", weightFieldName_))
{
Info<< " weight field = " << weightFieldName_ << nl;
if (log_) Info << " weight field = " << weightFieldName_ << nl;
if (source_ == stSampledSurface)
{
......@@ -480,7 +477,7 @@ void Foam::fieldValues::faceSource::initialise(const dictionary& dict)
if (weightFieldName_ == "none")
{
dict.lookup("orientedWeightField") >> weightFieldName_;
Info<< " weight field = " << weightFieldName_ << nl;
if (log_) Info << " weight field = " << weightFieldName_ << nl;
orientWeightField_ = true;
}
else
......@@ -506,12 +503,7 @@ void Foam::fieldValues::faceSource::initialise(const dictionary& dict)
fields_.append(orientedFields);
}
if (dict.readIfPresent("scaleFactor", scaleFactor_))
{
Info<< " scale factor = " << scaleFactor_ << nl;
}
Info<< nl << endl;
if (log_) Info << nl << endl;
if (valueOutput_)
{
......@@ -530,29 +522,27 @@ void Foam::fieldValues::faceSource::initialise(const dictionary& dict)
}
void Foam::fieldValues::faceSource::writeFileHeader(const label i)
void Foam::fieldValues::faceSource::writeFileHeader(Ostream& os) const
{
writeCommented(file(), "Source : ");
file() << sourceTypeNames_[source_] << " " << sourceName_ << endl;
writeCommented(file(), "Faces : ");
file() << nFaces_ << endl;
writeCommented(file(), "Area : ");
file() << totalArea_ << endl;
writeCommented(file(), "Time");
writeHeaderValue(os, "Source", sourceTypeNames_[source_]);
writeHeaderValue(os, "Name", sourceName_);
writeHeaderValue(os, "Faces", nFaces_);
writeHeaderValue(os, "Total area", totalArea_);
writeHeaderValue(os, "Scale factor", scaleFactor_);
writeCommented(os, "Time");
if (writeArea_)
{
file() << tab << "Area";
os << tab << "Area";
}
forAll(fields_, i)
{
file()
<< tab << operationTypeNames_[operation_]
os << tab << operationTypeNames_[operation_]
<< "(" << fields_[i] << ")";
}
file() << endl;
os << endl;
}
......@@ -569,14 +559,14 @@ Foam::scalar Foam::fieldValues::faceSource::processValues
case opSumDirection:
{
vector n(dict_.lookup("direction"));
return sum(pos(values*(Sf & n))*mag(values));
return gSum(pos(values*(Sf & n))*mag(values));
}
case opSumDirectionBalance:
{
vector n(dict_.lookup("direction"));
const scalarField nv(values*(Sf & n));
return sum(pos(nv)*mag(values) - neg(nv)*mag(values));
return gSum(pos(nv)*mag(values) - neg(nv)*mag(values));
}
default:
{
......@@ -603,7 +593,7 @@ Foam::vector Foam::fieldValues::faceSource::processValues
n /= mag(n) + ROOTVSMALL;
const scalarField nv(n & values);
return sum(pos(nv)*n*(nv));
return gSum(pos(nv)*n*(nv));
}
case opSumDirectionBalance:
{
......@@ -611,16 +601,16 @@ Foam::vector Foam::fieldValues::faceSource::processValues
n /= mag(n) + ROOTVSMALL;
const scalarField nv(n & values);
return sum(pos(nv)*n*(nv));
return gSum(pos(nv)*n*(nv));
}
case opAreaNormalAverage:
{
scalar result = sum(values & Sf)/sum(mag(Sf));
scalar result = gSum(values & Sf)/gSum(mag(Sf));
return vector(result, 0.0, 0.0);
}
case opAreaNormalIntegrate:
{
scalar result = sum(values & Sf);
scalar result = gSum(values & Sf);
return vector(result, 0.0, 0.0);
}
default:
......@@ -649,14 +639,17 @@ Foam::fieldValues::faceSource::faceSource
weightFieldName_("none"),
orientWeightField_(false),
orientedFieldsStart_(labelMax),
scaleFactor_(1.0),
writeArea_(dict.lookupOrDefault("writeArea", false)),
nFaces_(0),
faceId_(),
facePatchId_(),
faceSign_()
{
read(dict);
if (active_)
{
read(dict);
writeFileHeader(file());
}
}
......@@ -670,10 +663,11 @@ Foam::fieldValues::faceSource::~faceSource()
void Foam::fieldValues::faceSource::read(const dictionary& dict)
{
fieldValue::read(dict);
if (active_)
{