Commit bfb0693b authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: delay evaluation of surfaces for surfaceFieldValue (issue #1202)

- complete any pending initialisation on write().
  Allows lazier evaluation until when the surfaces are actually needed.
parent 52101db7
......@@ -61,7 +61,7 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::regionTypeNames_
{ regionTypes::stFaceZone, "faceZone" },
{ regionTypes::stPatch, "patch" },
{ regionTypes::stSurface, "surface" },
{ regionTypes::stSampledSurface, "sampledSurface" },
{ regionTypes::stSampled, "sampledSurface" },
});
......@@ -118,7 +118,7 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::postOperationTypeNames_
const Foam::objectRegistry&
Foam::functionObjects::fieldValues::surfaceFieldValue::obr() const
{
if (regionType_ == stSurface)
if (stSurface == regionType_)
{
return mesh_.lookupObject<objectRegistry>(regionName_);
}
......@@ -135,7 +135,7 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setFaceZoneFaces()
{
FatalErrorInFunction
<< type() << " " << name() << ": "
<< regionTypeNames_[regionType_] << "(" << regionName_ << "):" << nl
<< regionTypeNames_[regionType_] << '(' << regionName_ << "):" << nl
<< " Unknown face zone name: " << regionName_
<< ". Valid face zones are: " << mesh_.faceZones().names()
<< nl << exit(FatalError);
......@@ -213,7 +213,7 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchFaces()
{
FatalErrorInFunction
<< type() << " " << name() << ": "
<< regionTypeNames_[regionType_] << "(" << regionName_ << "):" << nl
<< regionTypeNames_[regionType_] << '(' << regionName_ << "):" << nl
<< " Unknown patch name: " << regionName_
<< ". Valid patch names are: "
<< mesh_.boundaryMesh().names() << nl
......@@ -368,7 +368,7 @@ combineSurfaceGeometry
pointField& points
) const
{
if (regionType_ == stSurface)
if (stSurface == regionType_)
{
const surfMesh& s = dynamicCast<const surfMesh>(obr());
......@@ -398,9 +398,9 @@ combineSurfaceGeometry
points = s.points();
}
}
else if (surfacePtr_.valid())
else if (sampledPtr_.valid())
{
const sampledSurface& s = surfacePtr_();
const sampledSurface& s = sampledPtr_();
if (Pstream::parRun())
{
......@@ -436,15 +436,15 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::totalArea() const
{
scalar totalArea = 0;
if (regionType_ == stSurface)
if (stSurface == regionType_)
{
const surfMesh& s = dynamicCast<const surfMesh>(obr());
totalArea = gSum(s.magSf());
}
else if (surfacePtr_.valid())
else if (sampledPtr_.valid())
{
totalArea = gSum(surfacePtr_().magSf());
totalArea = gSum(sampledPtr_().magSf());
}
else
{
......@@ -481,20 +481,17 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::usesSf() const
}
void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
(
const dictionary& dict
)
bool Foam::functionObjects::fieldValues::surfaceFieldValue::update()
{
dict.readEntry("name", regionName_);
if (sampledPtr_.valid())
{
sampledPtr_->update();
}
totalArea_ = 0;
nFaces_ = 0;
faceId_.clear();
facePatchId_.clear();
faceFlip_.clear();
surfacePtr_.clear();
surfaceWriterPtr_.clear();
if (!needsUpdate_)
{
return false;
}
switch (regionType_)
{
......@@ -514,124 +511,33 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
nFaces_ = returnReduce(s.size(), sumOp<label>());
break;
}
case stSampledSurface:
case stSampled:
{
surfacePtr_ = sampledSurface::New
(
name(),
mesh_,
dict.subDict("sampledSurfaceDict")
);
surfacePtr_().update();
nFaces_ =
returnReduce(surfacePtr_().faces().size(), sumOp<label>());
nFaces_ = returnReduce(sampledPtr_->faces().size(), sumOp<label>());
break;
}
default:
{
FatalErrorInFunction
<< type() << " " << name() << ": "
<< int(regionType_) << "(" << regionName_ << "):"
<< nl << " Unknown region type. Valid region types are:"
<< regionTypeNames_ << nl
<< exit(FatalError);
}
// Compiler warning if we forgot an enumeration
}
if (nFaces_ == 0)
{
FatalErrorInFunction
<< type() << " " << name() << ": "
<< regionTypeNames_[regionType_] << "(" << regionName_ << "):" << nl
<< regionTypeNames_[regionType_] << '(' << regionName_ << "):" << nl
<< " Region has no faces" << exit(FatalError);
}
if (surfacePtr_.valid())
{
surfacePtr_().update();
}
totalArea_ = totalArea();
Info<< type() << " " << name() << ":" << nl
<< " operation = ";
if (postOperation_ != postOpNone)
{
Info<< postOperationTypeNames_[postOperation_] << '('
<< operationTypeNames_[operation_] << ')' << nl;
}
else
{
Info<< operationTypeNames_[operation_] << nl;
}
Info<< " total faces = " << nFaces_ << nl
Log
<< " total faces = " << nFaces_ << nl
<< " total area = " << totalArea_ << nl;
writeFileHeader(file());
weightFieldName_ = "none";
if (usesWeight())
{
if (regionType_ == stSampledSurface)
{
FatalIOErrorInFunction(dict)
<< "Cannot use weighted operation '"
<< operationTypeNames_[operation_]
<< "' for sampledSurface"
<< exit(FatalIOError);
}
if (dict.readIfPresent("weightField", weightFieldName_))
{
Info<< " weight field = " << weightFieldName_ << nl;
}
else
{
// Suggest possible alternative unweighted operation?
FatalIOErrorInFunction(dict)
<< "The '" << operationTypeNames_[operation_]
<< "' operation is missing a weightField." << nl
<< "Either provide the weightField, "
<< "use weightField 'none' to suppress weighting," << nl
<< "or use a different operation."
<< exit(FatalIOError);
}
}
// Backwards compatibility for v1612 and older
List<word> orientedFields;
if (dict.readIfPresent("orientedFields", orientedFields))
{
WarningInFunction
<< "The 'orientedFields' option is deprecated. These fields can "
<< "and have been added to the standard 'fields' list."
<< endl;
fields_.append(orientedFields);
}
if (writeFields_)
{
const word surfaceFormat(dict.get<word>("surfaceFormat"));
if (surfaceFormat != "none")
{
surfaceWriterPtr_.reset
(
surfaceWriter::New
(
surfaceFormat,
dict.subOrEmptyDict("formatOptions").
subOrEmptyDict(surfaceFormat)
).ptr()
);
Info<< " surfaceFormat = " << surfaceFormat << nl;
}
}
Info<< nl << endl;
needsUpdate_ = false;
return true;
}
......@@ -911,15 +817,15 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::surfaceFieldValue
)
),
weightFieldName_("none"),
needsUpdate_(true),
writeArea_(false),
totalArea_(0),
writeArea_(dict.lookupOrDefault("writeArea", false)),
nFaces_(0),
faceId_(),
facePatchId_(),
faceFlip_()
{
read(dict);
writeFileHeader(file());
}
......@@ -944,15 +850,15 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::surfaceFieldValue
)
),
weightFieldName_("none"),
needsUpdate_(true),
writeArea_(false),
totalArea_(0),
writeArea_(dict.lookupOrDefault("writeArea", false)),
nFaces_(0),
faceId_(),
facePatchId_(),
faceFlip_()
{
read(dict);
writeFileHeader(file());
}
......@@ -964,7 +870,106 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::read
)
{
fieldValue::read(dict);
initialise(dict);
weightFieldName_ = "none";
needsUpdate_ = true;
writeArea_ = dict.lookupOrDefault("writeArea", false);
totalArea_ = 0;
nFaces_ = 0;
faceId_.clear();
facePatchId_.clear();
faceFlip_.clear();
sampledPtr_.clear();
surfaceWriterPtr_.clear();
dict.readEntry("name", regionName_);
// Create sampled surface, but leave 'expired' (ie, no update) since it
// may depend on fields or data that do not yet exist
if (stSampled == regionType_)
{
sampledPtr_ = sampledSurface::New
(
name(),
mesh_,
dict.subDict("sampledSurfaceDict")
);
}
Info<< type() << " " << name() << ":" << nl
<< " operation = ";
if (postOperation_ != postOpNone)
{
Info<< postOperationTypeNames_[postOperation_] << '('
<< operationTypeNames_[operation_] << ')' << nl;
}
else
{
Info<< operationTypeNames_[operation_] << nl;
}
if (usesWeight())
{
if (stSampled == regionType_)
{
FatalIOErrorInFunction(dict)
<< "Cannot use weighted operation '"
<< operationTypeNames_[operation_]
<< "' for sampledSurface"
<< exit(FatalIOError);
}
if (dict.readIfPresent("weightField", weightFieldName_))
{
Info<< " weight field = " << weightFieldName_ << nl;
}
else
{
// Suggest possible alternative unweighted operation?
FatalIOErrorInFunction(dict)
<< "The '" << operationTypeNames_[operation_]
<< "' operation is missing a weightField." << nl
<< "Either provide the weightField, "
<< "use weightField 'none' to suppress weighting," << nl
<< "or use a different operation."
<< exit(FatalIOError);
}
}
// Backwards compatibility for v1612 and older
List<word> orientedFields;
if (dict.readIfPresent("orientedFields", orientedFields))
{
fields_.append(orientedFields);
WarningInFunction
<< "The 'orientedFields' option is deprecated. These fields can "
<< "and have been added to the standard 'fields' list."
<< endl;
}
if (writeFields_)
{
const word formatName(dict.get<word>("surfaceFormat"));
if (formatName != "none")
{
surfaceWriterPtr_.reset
(
surfaceWriter::New
(
formatName,
dict.subOrEmptyDict("formatOptions")
.subOrEmptyDict(formatName)
)
);
Info<< " surfaceFormat = " << formatName << nl;
}
}
Info<< nl << endl;
return true;
}
......@@ -972,10 +977,7 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::read
bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
{
if (surfacePtr_.valid())
{
surfacePtr_().update();
}
update();
if (operation_ != opNone)
{
......@@ -1002,14 +1004,14 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
vectorField Sf;
if (usesSf())
{
if (regionType_ == stSurface)
if (stSurface == regionType_)
{
const surfMesh& s = dynamicCast<const surfMesh>(obr());
Sf = s.Sf();
}
else if (surfacePtr_.valid())
else if (sampledPtr_.valid())
{
Sf = surfacePtr_().Sf();
Sf = sampledPtr_().Sf();
}
else
{
......@@ -1023,13 +1025,13 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
if (surfaceWriterPtr_.valid())
{
if (regionType_ == stSurface || surfacePtr_.valid())
if (withTopologicalMerge())
{
combineSurfaceGeometry(faces, points);
combineMeshGeometry(faces, points);
}
else
{
combineMeshGeometry(faces, points);
combineSurfaceGeometry(faces, points);
}
}
......
......@@ -79,27 +79,27 @@ Usage
Where the entries comprise:
\table
Property | Description | Required | Default
type | type name: surfaceFieldValue | yes |
log | write data to standard output | no | no
writeFields | Write the region field values | yes |
writeArea | Write the area of the surfaceFieldValue | no |
surfaceFormat | Output value format | no |
regionType | Face regionType: see below | yes |
name | Name of face regionType if required | no |
operation | Operation to perform | yes |
postOperation | Post-operation to perform | no | none
weightField | Name of field to apply weighting | no |
scaleFactor | Scale factor | no | 1
fields | List of fields to operate on | yes |
Property | Description | Required | Default
type | Type name: surfaceFieldValue | yes |
log | Write data to standard output | no | no
regionType | Face regionType: see below | yes |
name | Name for regionType | yes |
operation | Operation to perform | yes |
postOperation | Post-operation to perform | no | none
fields | List of fields to operate on | yes |
weightField | Name of field to apply weighting | no |
scaleFactor | Output value scaling factor | no | 1
writeArea | Write the surface area | no |
writeFields | Write the region field values | yes |
surfaceFormat | Output value format | no | none
\endtable
Where \c regionType is defined by
\plaintable
faceZone | Requires a \b name entry to specify the faceZone
patch | Requires a \b name entry to specify the patch
surface | Requires a \b name entry to specify the surfMesh
sampledSurface | Requires a \b sampledSurfaceDict sub-dictionary
faceZone | The \b name entry to specify the faceZone
patch | The \b name entry to specify the patch
surface | The \b name entry to specify the surfMesh
sampledSurface | A \b sampledSurfaceDict sub-dictionary and \b name
\endplaintable
The \c operation is one of:
......@@ -191,6 +191,7 @@ SourceFiles
namespace Foam
{
// Forward declarations
class sampledSurface;
class surfaceWriter;
......@@ -214,10 +215,10 @@ public:
//- Region type enumeration
enum regionTypes
{
stFaceZone, //!< Calculate on a faceZone
stPatch, //!< Calculate on a patch
stSurface, //!< Calculate with fields on a surfMesh
stSampledSurface //!< Sample onto surface and calculate
stFaceZone = 0x01, //!< Calculate on a faceZone
stPatch = 0x02, //!< Calculate on a patch
stSurface = 0x11, //!< Calculate with fields on a surfMesh
stSampled = 0x12 //!< Sample onto surface and calculate
};
//- Region type names
......@@ -336,7 +337,7 @@ private:
protected:
// Protected data
// Protected Data
//- Region type
regionTypes regionType_;
......@@ -350,12 +351,15 @@ protected:
//- Weight field name - optional
word weightFieldName_;
//- Total area of the surfaceFieldValue
scalar totalArea_;
//- Track if the surface needs an update
bool needsUpdate_;
//- Optionally write the area of the surfaceFieldValue
bool writeArea_;
//- Total area of the surfaceFieldValue
scalar totalArea_;
//- Global number of faces
label nFaces_;
......@@ -372,9 +376,8 @@ protected:
// (false: use as-is, true: negate)
boolList faceFlip_;
//- The sampledSurface (if operating on sampledSurface)
autoPtr<sampledSurface> surfacePtr_;
//- The sampledSurface (when operating on sampledSurface)
autoPtr<sampledSurface> sampledPtr_;
//- Surface writer
autoPtr<surfaceWriter> surfaceWriterPtr_;
......@@ -385,6 +388,12 @@ protected:
//- The volume mesh or surface registry being used
const objectRegistry& obr() const;
//- Can the surface definition sample surface-fields?
inline bool withSurfaceFields() const;
//- Can use mesh topological merge?
inline bool withTopologicalMerge() const;
//- Return the local list of face IDs
inline const labelList& faceId() const;
......@@ -408,8 +417,9 @@ protected:
template<class WeightType>
inline bool canWeight(const Field<WeightType>& weightField) const;
//- Initialise, e.g. face addressing
void initialise(const dictionary& dict);
//- Update the surface and surface information as required.
// Do nothing (and return false) if no update was required
bool update();
//- Return true if the field name is known and a valid type
template<class Type>
......@@ -420,7 +430,7 @@ protected:
tmp<Field<Type>> getFieldValues
(
const word& fieldName,
const bool mustGet = false
const bool mandatory = false
) const;
//- Apply the 'operation' to the values. Operation must preserve Type.
......@@ -533,7 +543,7 @@ public:
// Public Member Functions
//- Return the region type
inline const regionTypes& regionType() const;
inline regionTypes regionType() const;
//- Return the output directory
inline fileName outputDir() const;
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -29,6 +29,20 @@ License
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
inline bool Foam::functionObjects::fieldValues::surfaceFieldValue::
withSurfaceFields() const
{
return (stFaceZone == regionType_ || stPatch == regionType_);
}
inline bool Foam::functionObjects::fieldValues::surfaceFieldValue::
withTopologicalMerge() const
{
return (stFaceZone == regionType_ || stPatch == regionType_);
}