surfaceFieldValue: Added the ability to specify multiple patches

The 'select' control can now take the value 'patches' in which case a
'patches' control will be used to specify a list of patches, rather than
just a single patch. The 'patches' and 'patch' controls now also both
support wildcards.
This commit is contained in:
Will Bainbridge
2024-08-04 14:02:16 +01:00
parent bb6f2ab003
commit 74a1c352b2
4 changed files with 247 additions and 308 deletions

View File

@ -55,11 +55,12 @@ template<>
const char* Foam::NamedEnum
<
Foam::functionObjects::fieldValues::surfaceFieldValue::selectionTypes,
3
4
>::names[] =
{
"faceZone",
"patch",
"patches",
"sampledSurface"
};
@ -89,7 +90,7 @@ const char* Foam::NamedEnum
const Foam::NamedEnum
<
Foam::functionObjects::fieldValues::surfaceFieldValue::selectionTypes,
3
4
> Foam::functionObjects::fieldValues::surfaceFieldValue::selectionTypeNames;
const Foam::NamedEnum
@ -101,21 +102,26 @@ const Foam::NamedEnum
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::functionObjects::fieldValues::surfaceFieldValue::setFaceZoneFaces()
void Foam::functionObjects::fieldValues::surfaceFieldValue::setFaceZoneFaces
(
const word& zoneName
)
{
label zoneId = mesh_.faceZones().findIndex(selectionName_);
const label zoneId = mesh_.faceZones().findIndex(zoneName);
if (zoneId < 0)
{
FatalErrorInFunction
<< type() << " " << name() << ": "
<< selectionTypeNames[selectionType_]
<< "(" << selectionName_ << "):" << nl
<< " Unknown face zone name: " << selectionName_
<< "(" << zoneName << "):" << nl
<< " Unknown face zone name: " << zoneName
<< ". Valid face zones are: " << mesh_.faceZones().toc()
<< nl << exit(FatalError);
}
selectionName_ = zoneName;
// Ensure addressing is built on all processes
mesh_.polyBFacePatches();
mesh_.polyBFacePatchFaces();
@ -172,44 +178,45 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setFaceZoneFaces()
}
void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchFaces()
void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchesFaces
(
const labelList& patchis
)
{
const label patchId = mesh_.boundaryMesh().findIndex(selectionName_);
selectionName_.clear();
if (patchId < 0)
{
FatalErrorInFunction
<< type() << " " << name() << ": "
<< selectionTypeNames[selectionType_]
<< "(" << selectionName_ << "):" << nl
<< " Unknown patch name: " << selectionName_
<< ". Valid patch names are: "
<< mesh_.boundaryMesh().names() << nl
<< exit(FatalError);
}
const fvPatch& fvp = mesh_.boundary()[patchId];
faceId_ = identityMap(fvp.size());
facePatchId_ = labelList(fvp.size(), patchId);
faceSign_ = labelList(fvp.size(), 1);
// If we have selected a cyclic, also include any associated processor
// cyclic faces
forAll(mesh_.boundary(), patchi)
faceId_.clear();
facePatchId_.clear();
faceSign_.clear();
forAll(patchis, i)
{
const label patchi = patchis[i];
const fvPatch& fvp = mesh_.boundary()[patchi];
if
(
isA<processorCyclicFvPatch>(fvp)
&& refCast<const processorCyclicFvPatch>(fvp).referPatchIndex()
== patchId
)
selectionName_.append((i ? " " : "") + fvp.name());
faceId_.append(identityMap(fvp.size()));
facePatchId_.append(labelList(fvp.size(), patchi));
faceSign_.append(labelList(fvp.size(), 1));
// If we have selected a cyclic, also include any associated processor
// cyclic faces
forAll(mesh_.boundary(), pcPatchj)
{
faceId_.append(identityMap(fvp.size()));
facePatchId_.append(labelList(fvp.size(), patchi));
faceSign_.append(labelList(fvp.size(), 1));
const fvPatch& pcFvp = mesh_.boundary()[pcPatchj];
if
(
isA<processorCyclicFvPatch>(pcFvp)
&& refCast<const processorCyclicFvPatch>(pcFvp).referPatchIndex()
== patchi
)
{
faceId_.append(identityMap(pcFvp.size()));
facePatchId_.append(labelList(pcFvp.size(), pcPatchj));
faceSign_.append(labelList(pcFvp.size(), 1));
}
}
}
@ -217,18 +224,61 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchFaces()
}
void Foam::functionObjects::fieldValues::surfaceFieldValue::sampledSurfaceFaces
void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchesFaces
(
const wordReList& patchNames
)
{
labelList patchis;
forAll(patchNames, i)
{
const labelList patchiis =
mesh_.boundaryMesh().findIndices(patchNames[i]);
if (patchiis.empty())
{
FatalErrorInFunction
<< type() << " " << this->name() << ": "
<< selectionTypeNames[selectionType_]
<< "(" << patchNames[i] << "):" << nl
<< " Unknown patch name: " << patchNames[i]
<< ". Valid patch names are: "
<< mesh_.boundaryMesh().names() << nl
<< exit(FatalError);
}
patchis.append(patchiis);
}
setPatchesFaces(patchis);
}
void Foam::functionObjects::fieldValues::surfaceFieldValue::setPatchFaces
(
const wordRe& patchName
)
{
setPatchesFaces(wordReList(1, patchName));
selectionName_ =
patchName.isPattern() ? '"' + patchName + '"' : string(patchName);
}
void
Foam::functionObjects::fieldValues::surfaceFieldValue::setSampledSurfaceFaces
(
const dictionary& dict
)
{
surfacePtr_ = sampledSurface::New
(
name(),
mesh_,
dict.subDict("sampledSurfaceDict")
);
surfacePtr_ = sampledSurface::New(name(), mesh_, dict);
surfacePtr_().update();
selectionName_ = surfacePtr_().name();
nFaces_ = returnReduce(surfacePtr_().faces().size(), sumOp<label>());
}
@ -419,21 +469,28 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
{
case selectionTypes::faceZone:
{
dict.lookupBackwardsCompatible({"faceZone", "name"})
>> selectionName_;
setFaceZoneFaces();
setFaceZoneFaces
(
dict.lookupBackwardsCompatible<word>({"faceZone", "name"})
);
break;
}
case selectionTypes::patch:
{
dict.lookupBackwardsCompatible({"patch", "name"}) >> selectionName_;
setPatchFaces();
setPatchFaces
(
dict.lookupBackwardsCompatible<wordRe>({"patch", "name"})
);
break;
}
case selectionTypes::patches:
{
setPatchesFaces(dict.lookup<wordReList>("patches"));
break;
}
case selectionTypes::sampledSurface:
{
sampledSurfaceFaces(dict);
selectionName_ = surfacePtr_().name();
setSampledSurfaceFaces(dict.subDict("sampledSurfaceDict"));
break;
}
default:
@ -441,7 +498,6 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
FatalErrorInFunction
<< type() << " " << name() << ": "
<< selectionTypeNames[selectionType_]
<< "(" << selectionName_ << "):" << nl
<< " Unknown selection type. Valid selection types are:"
<< selectionTypeNames.sortedToc() << nl << exit(FatalError);
}
@ -452,7 +508,7 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::initialise
FatalErrorInFunction
<< type() << " " << name() << ": "
<< selectionTypeNames[selectionType_]
<< "(" << selectionName_ << "):" << nl
<< "(" << selectionName_.c_str() << "):" << nl
<< " selection has no faces" << exit(FatalError);
}
@ -509,10 +565,10 @@ void Foam::functionObjects::fieldValues::surfaceFieldValue::writeFileHeader
{
if (operation_ != operationType::none)
{
writeCommented(file(), "Selection type : ");
writeCommented(file(), "Selection : ");
file()
<< selectionTypeNames[selectionType_] << " "
<< selectionName_ << endl;
<< selectionTypeNames[selectionType_] << "("
<< selectionName_.c_str() << ")" << endl;
writeCommented(file(), "Faces : ");
file() << nFaces_ << endl;
writeCommented(file(), "Area : ");
@ -612,7 +668,7 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::surfaceFieldValue
dict.lookupBackwardsCompatible({"select", "regionType"})
)
),
selectionName_(word::null),
selectionName_(string::null),
operation_(operationTypeNames_.read(dict.lookup("operation"))),
weightFieldNames_(),
scaleFactor_(1),
@ -642,7 +698,7 @@ Foam::functionObjects::fieldValues::surfaceFieldValue::surfaceFieldValue
dict.lookupBackwardsCompatible({"select", "regionType"})
)
),
selectionName_(word::null),
selectionName_(string::null),
operation_(operationTypeNames_.read(dict.lookup("operation"))),
weightFieldNames_(),
scaleFactor_(1),
@ -721,33 +777,6 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
Log << " total area = " << totalArea_ << endl;
}
// Write the surface geometry
if (writeFields_)
{
faceList faces;
pointField points;
if (selectionType_ == selectionTypes::sampledSurface)
{
combineSurfaceGeometry(faces, points);
}
else
{
combineMeshGeometry(faces, points);
}
if (Pstream::master())
{
surfaceWriterPtr_->write
(
outputDir(),
selectionTypeNames[selectionType_] + ("_" + selectionName_),
points,
faces
);
}
}
// Construct the sign and weight fields and the surface normals
const scalarField signs
(
@ -767,14 +796,49 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
: (signs*filterField(mesh_.Sf()))()
);
// Process the fields
// Create storage for the values
#define DeclareValues(fieldType, nullArg) \
PtrList<Field<fieldType>> fieldType##Values(fields_.size());
FOR_ALL_FIELD_TYPES(DeclareValues);
#undef DeclareValues
// Process the fields in turn. Get the values and store if necessary.
// Compute the operation and write into the file and the log.
forAll(fields_, i)
{
const word& fieldName = fields_[i];
bool ok = false;
#define writeValuesFieldType(fieldType, none) \
ok = ok || writeValues<fieldType>(fieldName, signs, weights, Sf);
{ \
const bool typeOk = validField<fieldType>(fieldName); \
\
if (typeOk) \
{ \
tmp<Field<fieldType>> values = \
getFieldValues<fieldType>(fieldName); \
\
writeValues<fieldType> \
( \
fieldName, \
values(), \
signs, \
weights, \
Sf \
); \
\
if (writeFields_) \
{ \
fieldType##Values.set \
( \
i, \
getFieldValues<fieldType>(fieldName).ptr() \
); \
} \
} \
\
ok = ok || typeOk; \
}
FOR_ALL_FIELD_TYPES(writeValuesFieldType);
#undef writeValuesFieldType
@ -784,13 +848,53 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::write()
}
}
// Finalise
// Finalise the file and the log
if (anyFields && operation_ != operationType::none && Pstream::master())
{
file() << endl;
}
Log << endl;
// Write a surface file with the values if specified
if (writeFields_)
{
faceList faces;
pointField points;
if (selectionType_ == selectionTypes::sampledSurface)
{
combineSurfaceGeometry(faces, points);
}
else
{
combineMeshGeometry(faces, points);
}
if (Pstream::master())
{
surfaceWriterPtr_->write
(
baseFileDir()/name()/time_.name(),
word(selectionTypeNames[selectionType_])
+ "("
+ (
selectionType_ == selectionTypes::patches
? selectionName_.replace(" ", ",").c_str()
: selectionName_
)
+ ")",
points,
faces,
fields_,
false
#define ValuesParameter(fieldType, nullArg) \
, fieldType##Values
FOR_ALL_FIELD_TYPES(ValuesParameter)
#undef ValuesParameter
);
}
}
return true;
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -191,11 +191,12 @@ public:
{
faceZone,
patch,
patches,
sampledSurface
};
//- Selection type names
static const NamedEnum<selectionTypes, 3> selectionTypeNames;
static const NamedEnum<selectionTypes, 4> selectionTypeNames;
//- Operation type enumeration
@ -226,13 +227,19 @@ private:
// Private Member Functions
//- Set faces to evaluate based on a face zone
void setFaceZoneFaces();
void setFaceZoneFaces(const word&);
//- Set faces to evaluate based on a patch
void setPatchFaces();
//- Set faces to evaluate based on a list of patch indices
void setPatchesFaces(const labelList&);
//- Set faces to evaluate based on a list of patch names
void setPatchesFaces(const wordReList&);
//- Set faces to evaluate based on a patch name
void setPatchFaces(const wordRe&);
//- Set faces according to sampledSurface
void sampledSurfaceFaces(const dictionary&);
void setSampledSurfaceFaces(const dictionary&);
//- Combine mesh faces and points from multiple processors
void combineMeshGeometry
@ -266,7 +273,7 @@ protected:
selectionTypes selectionType_;
//- Name of face selection (patch, faceZone, etc.)
word selectionName_;
string selectionName_;
//- Operation to apply to values
operationType operation_;
@ -424,37 +431,9 @@ public:
// Public Member Functions
//- Return the selection type
inline const selectionTypes& selectionType() const;
//- Return the selection name
inline const word& selectionName() const;
//- Return the local list of face IDs
inline const labelList& faceId() const;
//- Return the local list of patch ID per face
inline const labelList& facePatch() const;
//- Return the list of +1/-1 representing face flip map
inline const labelList& faceSign() const;
//- Return the output directory
inline fileName outputDir() const;
//- Templated helper function to output field values
template<class Type>
bool writeValues
(
const word& fieldName,
const scalarField& signs,
const scalarField& weights,
const vectorField& Sf
);
//- Templated helper function to output field values
template<class Type, class ResultType>
bool writeValues
void writeValues
(
const word& fieldName,
const Field<Type>& values,
@ -505,10 +484,6 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "surfaceFieldValueI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "surfaceFieldValueTemplates.C"
#endif

View File

@ -1,74 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "surfaceFieldValue.H"
#include "Time.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline
const Foam::functionObjects::fieldValues::surfaceFieldValue::selectionTypes&
Foam::functionObjects::fieldValues::surfaceFieldValue::selectionType() const
{
return selectionType_;
}
inline const Foam::word&
Foam::functionObjects::fieldValues::surfaceFieldValue::selectionName() const
{
return selectionName_;
}
inline const Foam::labelList&
Foam::functionObjects::fieldValues::surfaceFieldValue::faceId() const
{
return faceId_;
}
inline const Foam::labelList&
Foam::functionObjects::fieldValues::surfaceFieldValue::facePatch() const
{
return facePatchId_;
}
inline const Foam::labelList&
Foam::functionObjects::fieldValues::surfaceFieldValue::faceSign() const
{
return faceSign_;
}
inline Foam::fileName
Foam::functionObjects::fieldValues::surfaceFieldValue::outputDir() const
{
return baseFileDir()/name()/"surface"/time_.name();
}
// ************************************************************************* //

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2024 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -282,95 +282,7 @@ processValuesTypeType
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
bool Foam::functionObjects::fieldValues::surfaceFieldValue::writeValues
(
const word& fieldName,
const scalarField& signs,
const scalarField& weights,
const vectorField& Sf
)
{
const bool ok = validField<Type>(fieldName);
if (ok)
{
// Get the values
Field<Type> values(getFieldValues<Type>(fieldName));
// Write raw values on surface if specified
if (writeFields_)
{
faceList faces;
pointField points;
if (selectionType_ == selectionTypes::sampledSurface)
{
combineSurfaceGeometry(faces, points);
}
else
{
combineMeshGeometry(faces, points);
}
Field<Type> writeValues(weights*values);
combineFields(writeValues);
if (Pstream::master())
{
surfaceWriterPtr_->write
(
outputDir(),
fieldName
+ '_' + selectionTypeNames[selectionType_]
+ '_' + selectionName_,
points,
faces,
false,
fieldName,
writeValues
);
}
}
// Do the operation
if (operation_ != operationType::none)
{
// Apply scale factor
values *= scaleFactor_;
bool ok = false;
#define writeValuesFieldType(fieldType, none) \
ok = \
ok \
|| writeValues<Type, fieldType> \
( \
fieldName, \
values, \
signs, \
weights, \
Sf \
);
FOR_ALL_FIELD_TYPES(writeValuesFieldType);
#undef writeValuesFieldType
if (!ok)
{
FatalErrorInFunction
<< "Operation " << operationTypeNames_[operation_]
<< " not available for values of type "
<< pTraits<Type>::typeName
<< exit(FatalError);
}
}
}
return ok;
}
template<class Type, class ResultType>
bool Foam::functionObjects::fieldValues::surfaceFieldValue::writeValues
void Foam::functionObjects::fieldValues::surfaceFieldValue::writeValues
(
const word& fieldName,
const Field<Type>& values,
@ -379,26 +291,48 @@ bool Foam::functionObjects::fieldValues::surfaceFieldValue::writeValues
const vectorField& Sf
)
{
ResultType result;
if (processValues(values, signs, weights, Sf, result))
// Do the operation
if (operation_ != operationType::none)
{
// Add to result dictionary, over-writing any previous entry
resultDict_.add(fieldName, result, true);
bool ok = false;
if (Pstream::master())
{
file() << tab << result;
Log << " " << operationTypeNames_[operation_]
<< "(" << selectionName_ << ") of " << fieldName
<< " = " << result << endl;
#define writeValuesFieldType(fieldType, none) \
{ \
fieldType result; \
\
const bool typeOk = \
processValues(values, signs, weights, Sf, result); \
\
if (typeOk) \
{ \
/* Add to result dictionary, over-writing any previous entry */\
resultDict_.add(fieldName, result, true); \
\
/* Write into the file and the log */ \
if (Pstream::master()) \
{ \
file() << tab << result; \
\
Log << " " << operationTypeNames_[operation_] \
<< "(" << selectionName_ << ") of " << fieldName \
<< " = " << result << endl; \
} \
} \
\
ok = ok || typeOk; \
}
FOR_ALL_FIELD_TYPES(writeValuesFieldType);
#undef writeValuesFieldType
return true;
if (!ok)
{
FatalErrorInFunction
<< "Operation " << operationTypeNames_[operation_]
<< " not available for values of type "
<< pTraits<Type>::typeName
<< exit(FatalError);
}
}
return false;
}