From 76d10790e3e49190e16ee49bafa4b4eeb08f7438 Mon Sep 17 00:00:00 2001 From: pkuLmq <1900011062@pku.edu.cn> Date: Sun, 31 Jul 2022 16:15:55 +0800 Subject: [PATCH 01/10] add AMR --- AMR_tidy/Make/files | 14 + AMR_tidy/Make/options | 22 + .../dynamicRefineFvMeshNew/dynamicMeshDict | 152 + .../dynamicRefineFvMeshNew.C | 1850 +++++++++ .../dynamicRefineFvMeshNew.H | 299 ++ .../multiCritRefinement.C | 637 +++ .../multiCritRefinement.H | 205 + AMR_tidy/hexRef/hexRef.C | 3619 +++++++++++++++++ AMR_tidy/hexRef/hexRef.H | 665 +++ AMR_tidy/hexRef/hexRef2/hexRef2.C | 2177 ++++++++++ AMR_tidy/hexRef/hexRef2/hexRef2.H | 226 + AMR_tidy/hexRef/hexRef4/hexRef4.C | 2546 ++++++++++++ AMR_tidy/hexRef/hexRef4/hexRef4.H | 215 + AMR_tidy/hexRef/hexRef4Axi/hexRef4Axi.C | 2572 ++++++++++++ AMR_tidy/hexRef/hexRef4Axi/hexRef4Axi.H | 215 + AMR_tidy/hexRef/hexRef8/hexRef8.C | 2366 +++++++++++ AMR_tidy/hexRef/hexRef8/hexRef8.H | 234 ++ AMR_tidy/hexRef/hexRefData.C | 343 ++ AMR_tidy/hexRef/hexRefData.H | 137 + AMR_tidy/hexRef/hexRefNew.C | 198 + AMR_tidy/hexRef/refinementHistory.C | 1793 ++++++++ AMR_tidy/hexRef/refinementHistory.H | 426 ++ 22 files changed, 20911 insertions(+) create mode 100644 AMR_tidy/Make/files create mode 100644 AMR_tidy/Make/options create mode 100644 AMR_tidy/dynamicRefineFvMeshNew/dynamicMeshDict create mode 100644 AMR_tidy/dynamicRefineFvMeshNew/dynamicRefineFvMeshNew.C create mode 100644 AMR_tidy/dynamicRefineFvMeshNew/dynamicRefineFvMeshNew.H create mode 100644 AMR_tidy/dynamicRefineFvMeshNew/multiCritRefinement.C create mode 100644 AMR_tidy/dynamicRefineFvMeshNew/multiCritRefinement.H create mode 100644 AMR_tidy/hexRef/hexRef.C create mode 100644 AMR_tidy/hexRef/hexRef.H create mode 100644 AMR_tidy/hexRef/hexRef2/hexRef2.C create mode 100644 AMR_tidy/hexRef/hexRef2/hexRef2.H create mode 100644 AMR_tidy/hexRef/hexRef4/hexRef4.C create mode 100644 AMR_tidy/hexRef/hexRef4/hexRef4.H create mode 100644 AMR_tidy/hexRef/hexRef4Axi/hexRef4Axi.C create mode 100644 AMR_tidy/hexRef/hexRef4Axi/hexRef4Axi.H create mode 100644 AMR_tidy/hexRef/hexRef8/hexRef8.C create mode 100644 AMR_tidy/hexRef/hexRef8/hexRef8.H create mode 100644 AMR_tidy/hexRef/hexRefData.C create mode 100644 AMR_tidy/hexRef/hexRefData.H create mode 100644 AMR_tidy/hexRef/hexRefNew.C create mode 100644 AMR_tidy/hexRef/refinementHistory.C create mode 100644 AMR_tidy/hexRef/refinementHistory.H diff --git a/AMR_tidy/Make/files b/AMR_tidy/Make/files new file mode 100644 index 00000000..66c8f4ce --- /dev/null +++ b/AMR_tidy/Make/files @@ -0,0 +1,14 @@ +hexRef/hexRef.C +hexRef/hexRefData.C +hexRef/refinementHistory.C +hexRef/hexRefNew.C + +hexRef/hexRef8/hexRef8.C +hexRef/hexRef4/hexRef4.C +hexRef/hexRef4Axi/hexRef4Axi.C +hexRef/hexRef2/hexRef2.C + +dynamicRefineFvMeshNew/multiCritRefinement.C +dynamicRefineFvMeshNew/dynamicRefineFvMeshNew.C + +LIB = $(FOAM_USER_LIBBIN)/libnewdynamicFvMesh diff --git a/AMR_tidy/Make/options b/AMR_tidy/Make/options new file mode 100644 index 00000000..7b24eb1f --- /dev/null +++ b/AMR_tidy/Make/options @@ -0,0 +1,22 @@ +EXE_INC = -std=c++14\ + -I$(LIB_SRC)/triSurface/lnInclude \ + -I$(LIB_SRC)/meshTools/lnInclude \ + -I$(LIB_SRC)/dynamicFvMesh/lnInclude \ + -I$(LIB_SRC)/dynamicMesh/lnInclude \ + -I$(LIB_SRC)/finiteVolume/lnInclude \ + -I$(LIB_SRC)/parallel/decompose/decompositionMethods/lnInclude \ + -I$(LIB_SRC)/finiteVolume/lnInclude \ + -I$(LIB_SRC)/triSurface/lnInclude \ + -I$(LIB_SRC)/mesh/extrudeModel/lnInclude + +LIB_LIBS = \ + -ltriSurface \ + -lmeshTools \ + -ldynamicMesh \ + -ldynamicFvMesh \ + -lfiniteVolume \ + -lextrudeModel \ + -ldecompositionMethods \ + -L$(FOAM_LIBBIN)/dummy \ + -lmetisDecomp -lptscotchDecomp -lscotchDecomp + diff --git a/AMR_tidy/dynamicRefineFvMeshNew/dynamicMeshDict b/AMR_tidy/dynamicRefineFvMeshNew/dynamicMeshDict new file mode 100644 index 00000000..7c020ccd --- /dev/null +++ b/AMR_tidy/dynamicRefineFvMeshNew/dynamicMeshDict @@ -0,0 +1,152 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: dev | +| \\ / A nd | Web: www.OpenFOAM.org | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object dynamicMeshDict; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +dynamicFvMesh dynamicRefineFvMesh; +// dynamicFvMesh staticFvMesh; + +multiCritRefinementControls +{ + enableMultiCritRefinementControl true; + + fields // must be scalarFields + ( + //alpha (min max refineLevel) + // refine cells where alpha in [0.01:0.99] with maximal 2 refinement layers + alpha.water + { + minValue 0.01; + maxValue 0.99; + refineLevel 2; + nAddLayers 2; // add two cell layers at the refinement border + } + ); + + interface // must be a scalarField (only one dictionary!) + ( + alpha.water // refine interface (found based on snGrad of alpha > 0.1) + { + innerRefLayers 2; // describes how many cell layers inside phase alpha are to be refined + outerRefLayers 5; // describes how many cell layers outside phase alpha are to be refined + + // optional settings: + maxRefineLevel 4; // max refinement layers; Default: maxRefinement from dynamicRefineFvMeshCoeffs is used + // to get slower than 2:1 refinement; add #nAddLayers between each refinement level at the interface + nAddLayers 1; //Default: 0 + } + ); + + gradients // must be scalars + ( + // arguments as in 'fields' + // min/max values are based on mag(fvc::grad(volScalarField)) * cellVolume + O2_1 + { + fielName O2; //default: dictName -> 02_1 + minValue 1.5; + maxValue 3.0; + refineLevel 1; + nAddLayers 0; //default: 0 + } + + O2_2 + { + fielName O2; + minValue 1.75; + maxValue 2.5; + refineLevel 2; + nAddLayers 0; //default: 0 + } + ); + + curls // must be vectors + ( + // arguments as in 'fields' + // min/max values are based on mag(fvc::curl(volVectorField)) + U (0.5 1 2) + { + minValue 0.5; + maxValue 1.0; + refineLevel 2; + nAddLayers 0; //default: 0 + } + ); + + regions + ( + boxToCell + { + minLevel 1; + + box (-1 0.001 0.002)(1 0.005 0.003); + } + + ); +} + +// Refinement +dynamicRefineFvMeshCoeffs +{ + // Refine every refineInterval timesteps + refineInterval 3; + + // Maximum refinement level (starts from 0) + maxRefinement 3; + + // Maximum cell limit (approximate) + maxCells 1000000; + + + field gamma; // volScalarField to base refinement on, or + // multiCritRefinementField; + // to use multiple criteria specified in + // multiCritRefinementControls + + // Which cells to un/refine: based on point values (simple averaging). + // - refine pointCells of point value inbetween minLevel..maxLevel + // - unrefine pointCells that are within nBufferLayers of points marked + // for refinement. + minLevel 0.01; + maxLevel 0.99; + + nBufferLayers 4; + + // Refine field inbetween lower..upper + lowerRefineLevel 0.5; // do not change + upperRefineLevel 3.5; // maxRefinement+0.5 + unrefineLevel -0.5; // If value < unrefineLevel unrefine + + // Newly introduced patch points optionally get projected onto a surface + //projectSurfaces ("fixedWalls4.stl"); + //projectPatches (fixedWalls); + // Maximum project distance + //projectDistance 1; + + // Fluxes to adapt. For newly created faces or split faces the flux + // gets estimated from an interpolated volVectorField ('velocity') + // First is name of the flux to adapt, second is velocity that will + // be interpolated and inner-producted with the face area vector. + correctFluxes ((phi U)); + + // List of non-flux surfaceFields to be mapped + // only for new internal faces (AMR refine) + mapSurfaceFields + ( + Uf + Uf_0 + ); +} + +// ************************************************************************* // diff --git a/AMR_tidy/dynamicRefineFvMeshNew/dynamicRefineFvMeshNew.C b/AMR_tidy/dynamicRefineFvMeshNew/dynamicRefineFvMeshNew.C new file mode 100644 index 00000000..d1548a60 --- /dev/null +++ b/AMR_tidy/dynamicRefineFvMeshNew/dynamicRefineFvMeshNew.C @@ -0,0 +1,1850 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2011-2016 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 . + +\*---------------------------------------------------------------------------*/ + +#include "dynamicRefineFvMeshNew.H" +#include "addToRunTimeSelectionTable.H" +#include "surfaceInterpolate.H" +#include "volFields.H" +#include "polyTopoChange.H" +#include "surfaceFields.H" +#include "syncTools.H" +#include "pointFields.H" +#include "sigFpe.H" +#include "cellSet.H" +#include "wedgePolyPatch.H" +#include "emptyPolyPatch.H" + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +namespace Foam +{ + defineTypeNameAndDebug(dynamicRefineFvMeshNew, 0); + addToRunTimeSelectionTable(dynamicFvMesh, dynamicRefineFvMeshNew, IOobject); + + + // Helper class for accessing max cell level of faces accross processor patches + template + class combineMaxOp + { + public: + void operator()(Type& x, const Type& y) const + { + x = max(x, y); + } + }; +} + +// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // + +Foam::label Foam::dynamicRefineFvMeshNew::count +( + const PackedBoolList& l, + const unsigned int val +) +{ + label n = 0; + forAll(l, i) + { + if (l.get(i) == val) + { + n++; + } + + // debug also serves to get-around Clang compiler trying to optimsie + // out this forAll loop under O3 optimisation + if (debug) + { + Info<< "n=" << n << endl; + } + } + + return n; +} + + +void Foam::dynamicRefineFvMeshNew::calculateProtectedCells +( + PackedBoolList& unrefineableCell +) const +{ + if (protectedCell_.empty()) + { + unrefineableCell.clear(); + return; + } + + const labelList& cellLevel = meshCutter_->cellLevel(); + + unrefineableCell = protectedCell_; + + // Get neighbouring cell level + labelList neiLevel(nFaces()-nInternalFaces()); + + for (label facei = nInternalFaces(); facei < nFaces(); facei++) + { + neiLevel[facei-nInternalFaces()] = cellLevel[faceOwner()[facei]]; + } + syncTools::swapBoundaryFaceList(*this, neiLevel); + + + while (true) + { + // Pick up faces on border of protected cells + boolList seedFace(nFaces(), false); + + forAll(faceNeighbour(), facei) + { + label own = faceOwner()[facei]; + bool ownProtected = unrefineableCell.get(own); + label nei = faceNeighbour()[facei]; + bool neiProtected = unrefineableCell.get(nei); + + if (ownProtected && (cellLevel[nei] > cellLevel[own])) + { + seedFace[facei] = true; + } + else if (neiProtected && (cellLevel[own] > cellLevel[nei])) + { + seedFace[facei] = true; + } + } + for (label facei = nInternalFaces(); facei < nFaces(); facei++) + { + label own = faceOwner()[facei]; + bool ownProtected = unrefineableCell.get(own); + if + ( + ownProtected + && (neiLevel[facei-nInternalFaces()] > cellLevel[own]) + ) + { + seedFace[facei] = true; + } + } + + syncTools::syncFaceList(*this, seedFace, orEqOp()); + + + // Extend unrefineableCell + bool hasExtended = false; + + for (label facei = 0; facei < nInternalFaces(); facei++) + { + if (seedFace[facei]) + { + label own = faceOwner()[facei]; + if (unrefineableCell.get(own) == 0) + { + unrefineableCell.set(own, 1); + hasExtended = true; + } + + label nei = faceNeighbour()[facei]; + if (unrefineableCell.get(nei) == 0) + { + unrefineableCell.set(nei, 1); + hasExtended = true; + } + } + } + for (label facei = nInternalFaces(); facei < nFaces(); facei++) + { + if (seedFace[facei]) + { + label own = faceOwner()[facei]; + if (unrefineableCell.get(own) == 0) + { + unrefineableCell.set(own, 1); + hasExtended = true; + } + } + } + + if (!returnReduce(hasExtended, orOp())) + { + break; + } + } +} + + +void Foam::dynamicRefineFvMeshNew::readDict() +{ + dictionary refineDict + ( + IOdictionary + ( + IOobject + ( + "dynamicMeshDict", + time().constant(), + *this, + IOobject::MUST_READ_IF_MODIFIED, + IOobject::NO_WRITE, + false + ) + ).subDict(typeName + "Coeffs") + ); + + List> fluxVelocities = List> + ( + refineDict.lookup("correctFluxes") + ); + // Rework into hashtable. + correctFluxes_.resize(fluxVelocities.size()); + forAll(fluxVelocities, i) + { + correctFluxes_.insert(fluxVelocities[i][0], fluxVelocities[i][1]); + }; + + if (refineDict.found("mapSurfaceFields")) + { + List surfFlds = List + ( + refineDict.lookup("mapSurfaceFields") + ); + // Rework into hashtable. + correctFluxes_.resize(surfFlds.size()); + forAll(surfFlds, i) + { + mapSurfaceFields_.insert(surfFlds[i], surfFlds[i]); + }; + } + + dumpLevel_ = Switch(refineDict.lookup("dumpLevel")); +} + + +// Refines cells, maps fields and recalculates (an approximate) flux +Foam::autoPtr +Foam::dynamicRefineFvMeshNew::refine +( + const labelList& cellsToRefine +) +{ + // Mesh changing engine. + polyTopoChange meshMod(*this); + + // Play refinement commands into mesh changer. + meshCutter_->setRefinement(cellsToRefine, meshMod); + + // Create mesh (with inflation), return map from old to new mesh. + //autoPtr map = meshMod.changeMesh(*this, true); + autoPtr map = meshMod.changeMesh(*this, false); + + Info<< "Refined from " + << returnReduce(map().nOldCells(), sumOp