From 610127213398c82d3c07ca6607de031d2c5424f7 Mon Sep 17 00:00:00 2001 From: sergio Date: Tue, 22 Jun 2021 08:46:28 -0700 Subject: [PATCH 1/2] ENH: Adding subMesh option to momentumError and div FOs 1) Adding subMesh capabilities to momentumError and div FOs. - A subMesh is created from cellZones. - The operators (div, etc) are only calculated in the subMesh. 2) Optionally, halo cells can be added to the cellZones. 3) New helper class to handle the subMesh creation and field mapping. --- .../Make/options | 1 + .../regionFunctionObject.H | 12 +- .../regionFunctionObjectTemplates.C | 43 +++- src/dynamicMesh/Make/files | 2 + src/dynamicMesh/zoneSubSet/zoneSubSet.C | 139 +++++++++++ src/dynamicMesh/zoneSubSet/zoneSubSet.H | 204 ++++++++++++++++ .../zoneSubSet/zoneSubSetTemplates.C | 70 ++++++ src/functionObjects/field/Make/options | 2 + src/functionObjects/field/div/div.C | 24 +- src/functionObjects/field/div/div.H | 23 +- src/functionObjects/field/div/divTemplates.C | 43 +++- .../field/fieldExpression/fieldExpression.C | 28 ++- .../field/fieldExpression/fieldExpression.H | 24 +- .../field/momentumError/momentumError.C | 221 +++++++++++++----- .../field/momentumError/momentumError.H | 17 +- src/functionObjects/tools/Make/files | 3 + src/functionObjects/tools/Make/options | 13 ++ .../functionObjects/Make/options | 6 +- .../rhoSimpleFoam/aerofoilNACA0012/Allrun | 14 +- .../aerofoilNACA0012/Allrun-parallel | 18 ++ .../rhoSimpleFoam/aerofoilNACA0012/Allrun.pre | 20 ++ .../aerofoilNACA0012/system/controlDict | 33 ++- .../aerofoilNACA0012/system/decomposeParDict | 27 +++ .../aerofoilNACA0012/system/fvSchemes | 1 + .../aerofoilNACA0012/system/topoSetDict | 37 +++ 25 files changed, 914 insertions(+), 111 deletions(-) create mode 100644 src/dynamicMesh/zoneSubSet/zoneSubSet.C create mode 100644 src/dynamicMesh/zoneSubSet/zoneSubSet.H create mode 100644 src/dynamicMesh/zoneSubSet/zoneSubSetTemplates.C create mode 100644 src/functionObjects/tools/Make/files create mode 100644 src/functionObjects/tools/Make/options create mode 100755 tutorials/compressible/rhoSimpleFoam/aerofoilNACA0012/Allrun-parallel create mode 100755 tutorials/compressible/rhoSimpleFoam/aerofoilNACA0012/Allrun.pre create mode 100644 tutorials/compressible/rhoSimpleFoam/aerofoilNACA0012/system/decomposeParDict create mode 100644 tutorials/compressible/rhoSimpleFoam/aerofoilNACA0012/system/topoSetDict diff --git a/applications/utilities/preProcessing/createExternalCoupledPatchGeometry/Make/options b/applications/utilities/preProcessing/createExternalCoupledPatchGeometry/Make/options index 3c22d1310b..2a75a610c1 100644 --- a/applications/utilities/preProcessing/createExternalCoupledPatchGeometry/Make/options +++ b/applications/utilities/preProcessing/createExternalCoupledPatchGeometry/Make/options @@ -6,4 +6,5 @@ EXE_INC = \ EXE_LIBS = \ -lfiniteVolume \ -lmeshTools \ + -ldynamicMesh \ -lfieldFunctionObjects diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H index d9fed7b56b..061b2300a7 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObject.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation - Copyright (C) 2016-2020 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -74,7 +74,6 @@ class regionFunctionObject : public stateFunctionObject { - protected: // Protected Member Data @@ -147,6 +146,15 @@ protected: bool cacheable = false ); + //- Store the field in an optional objectRegistry under the given name + template + bool storeInDb + ( + const word& fieldName, + const tmp& tfield, + const objectRegistry& obr + ); + //- Write field if present in the (sub) objectRegistry bool writeObject(const word& fieldName); diff --git a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C index f253d83e05..c1c0f740b6 100644 --- a/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C +++ b/src/OpenFOAM/db/functionObjects/regionFunctionObject/regionFunctionObjectTemplates.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2016 OpenFOAM Foundation - Copyright (C) 2016-2018 OpenCFD Ltd. + Copyright (C) 2016-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -122,16 +122,20 @@ bool Foam::functionObjects::regionFunctionObject::store return false; } - if (fieldName.size() && foundObject(fieldName)) - { - const ObjectType& field = lookupObject(fieldName); + ObjectType* fieldptr; + if + ( + !fieldName.empty() + && (fieldptr = getObjectPtr(fieldName)) != nullptr + ) + { // If there is a result field already registered, assign to the new // result field. Otherwise transfer ownership of the new result field to // the object registry - if (&field != &tfield()) + if (fieldptr != &tfield()) { - const_cast(field) = tfield; + (*fieldptr) = tfield; } else { @@ -156,4 +160,31 @@ bool Foam::functionObjects::regionFunctionObject::store } +template +bool Foam::functionObjects::regionFunctionObject::storeInDb +( + const word& fieldName, + const tmp& tfield, + const objectRegistry& obr +) +{ + ObjectType* fieldptr; + if + ( + !fieldName.empty() + && (fieldptr = obr.getObjectPtr(fieldName)) != nullptr + ) + { + (*fieldptr) = tfield; + } + else + { + tfield.ref().rename(fieldName); + obr.objectRegistry::store(tfield.ptr()); + } + + return true; +} + + // ************************************************************************* // diff --git a/src/dynamicMesh/Make/files b/src/dynamicMesh/Make/files index 08723a0583..eedcd7fe8c 100644 --- a/src/dynamicMesh/Make/files +++ b/src/dynamicMesh/Make/files @@ -138,4 +138,6 @@ polyMeshFilter/polyMeshFilter.C pointPatchDist/externalPointEdgePoint.C pointPatchDist/pointPatchDist.C +zoneSubSet/zoneSubSet.C + LIB = $(FOAM_LIBBIN)/libdynamicMesh diff --git a/src/dynamicMesh/zoneSubSet/zoneSubSet.C b/src/dynamicMesh/zoneSubSet/zoneSubSet.C new file mode 100644 index 0000000000..34b60e6c2d --- /dev/null +++ b/src/dynamicMesh/zoneSubSet/zoneSubSet.C @@ -0,0 +1,139 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2021 OpenCFD Ltd. +------------------------------------------------------------------------------- +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 "zoneSubSet.H" +#include "cellBitSet.H" +#include "haloToCell.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ +namespace Detail +{ + defineTypeNameAndDebug(zoneSubSet, 0); +} +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::Detail::zoneSubSet::correct() +{ + subsetter_.clear(); + haloCells_.clearStorage(); + + if (zoneMatcher_.empty()) + { + return false; + } + + // Select named zones + cellBitSet selectedCells + ( + subsetter_.baseMesh(), + subsetter_.baseMesh().cellZones().selection(zoneMatcher_) + ); + + if (debug) + { + Pout<< "Subsetting " + << selectedCells.addressing().count() + << " cells based on cellZones " + << flatOutput(zoneMatcher_) << endl; + } + + if (nLayers_ > 0) + { + // Add halo layer(s) + haloToCell haloSource(subsetter_.baseMesh(), nLayers_); + haloSource.verbose(false); + + // Before adding halo cells + haloCells_ = selectedCells.addressing(); + + haloSource.applyToSet(topoSetSource::ADD, selectedCells); + + // Halo cells: anything new, not in the original set + haloCells_ ^= selectedCells.addressing(); + } + + if (debug) + { + const label nHalo = haloCells_.count(); + const label nSubCell = selectedCells.addressing().count(); + + Info<< " overall " + << returnReduce(nSubCell, sumOp