From dd658eec1f3d716fa18ceb990b924b38721ecdeb Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 11 Jan 2019 11:20:10 +0100 Subject: Improve grid refinement interface By deriving RefiningGrid2D from Grid2D to provide methods specific to refining subgrids. --- apps/adrian/poiseuille2d/poiseuille2d.cpp | 28 ++++---- src/refinement/grid2D.cpp | 1 + src/refinement/grid2D.h | 32 +++++++-- src/refinement/grid2D.hh | 104 ++++++++++++++++++++---------- 4 files changed, 110 insertions(+), 55 deletions(-) diff --git a/apps/adrian/poiseuille2d/poiseuille2d.cpp b/apps/adrian/poiseuille2d/poiseuille2d.cpp index e71e51e..3d263fa 100644 --- a/apps/adrian/poiseuille2d/poiseuille2d.cpp +++ b/apps/adrian/poiseuille2d/poiseuille2d.cpp @@ -24,7 +24,9 @@ */ #include "olb2D.h" +#ifndef OLB_PRECOMPILED #include "olb2D.hh" +#endif #include @@ -187,18 +189,15 @@ int main(int argc, char* argv[]) auto coarseGrid = Grid2D::make(coarseCuboid, N, baseTau, Re); prepareGeometry(coarseGrid->getConverter(), coarseGrid->getSuperGeometry()); - const Vector wantedFineExtend {3.0, 1.5}; - const Vector fineOrigin = coarseGrid->alignOriginToGrid({0.8, (ly-wantedFineExtend[1])/2}); - const Vector fineExtend = coarseGrid->alignExtendToGrid(wantedFineExtend); + const Vector fineExtend {3.0, 1.5}; + const Vector fineOrigin {0.8, (ly-fineExtend[1])/2}; auto fineGrid = &coarseGrid->refine(fineOrigin, fineExtend); prepareGeometry(fineGrid->getConverter(), fineGrid->getSuperGeometry()); - const T coarseDeltaX = coarseGrid->getConverter().getPhysDeltaX(); - - IndicatorCuboid2D refinedOverlap(fineExtend - 4*coarseDeltaX, fineOrigin + 2*coarseDeltaX); - coarseGrid->getSuperGeometry().rename(1,0,refinedOverlap); - coarseGrid->getSuperGeometry().rename(2,0,refinedOverlap); + auto refinedOverlap = fineGrid->getRefinedOverlap(); + coarseGrid->getSuperGeometry().rename(1,0,*refinedOverlap); + coarseGrid->getSuperGeometry().rename(2,0,*refinedOverlap); BGKdynamics coarseBulkDynamics( coarseGrid->getConverter().getLatticeRelaxationFrequency(), @@ -221,18 +220,15 @@ int main(int argc, char* argv[]) sOnLatticeBoundaryCondition2D fineSBoundaryCondition(fineGrid->getSuperLattice()); createLocalBoundaryCondition2D(fineSBoundaryCondition); - const Vector wantedFineExtend2 {0.6, 0.4}; - const Vector fineOrigin2 = fineGrid->alignOriginToGrid({1.05, (ly-wantedFineExtend2[1])/2}); - const Vector fineExtend2 = fineGrid->alignExtendToGrid(wantedFineExtend2); + const Vector fineExtend2 {0.6, 0.4}; + const Vector fineOrigin2 {1.05, (ly-fineExtend2[1])/2}; auto fineGrid2 = &fineGrid->refine(fineOrigin2, fineExtend2); prepareGeometry(fineGrid2->getConverter(), fineGrid2->getSuperGeometry()); - const T fine2DeltaX = fineGrid->getConverter().getPhysDeltaX(); - - IndicatorCuboid2D refinedOverlap2(fineExtend2 - 4*fine2DeltaX, fineOrigin2 + 2*fine2DeltaX); - fineGrid->getSuperGeometry().rename(1,0,refinedOverlap2); - fineGrid->getSuperGeometry().rename(2,0,refinedOverlap2); + auto refinedOverlap2 = fineGrid2->getRefinedOverlap(); + fineGrid->getSuperGeometry().rename(1,0,*refinedOverlap2); + fineGrid->getSuperGeometry().rename(2,0,*refinedOverlap2); prepareLattice( fineGrid->getConverter(), diff --git a/src/refinement/grid2D.cpp b/src/refinement/grid2D.cpp index b371d0c..86ca284 100644 --- a/src/refinement/grid2D.cpp +++ b/src/refinement/grid2D.cpp @@ -30,5 +30,6 @@ namespace olb { template class Grid2D; +template class RefiningGrid2D; } diff --git a/src/refinement/grid2D.h b/src/refinement/grid2D.h index 1d15170..d782651 100644 --- a/src/refinement/grid2D.h +++ b/src/refinement/grid2D.h @@ -34,23 +34,27 @@ #include "geometry/superGeometry2D.h" #include "communication/loadBalancer.h" #include "functors/analytical/indicator/indicatorF2D.h" +#include "utilities/functorPtr.h" namespace olb { template class DESCRIPTOR> class FineCoupler2D; template class DESCRIPTOR> class CoarseCoupler2D; +template class DESCRIPTOR> class RefiningGrid2D; template class DESCRIPTOR> class Grid2D { -private: +protected: + FunctorPtr> _domainF; + std::unique_ptr> _converter; std::unique_ptr> _cuboids; std::unique_ptr> _balancer; std::unique_ptr> _geometry; std::unique_ptr> _lattice; - std::vector>> _fineGrids; + std::vector>> _fineGrids; std::vector>> _fineCouplers; std::vector>> _coarseCouplers; @@ -58,7 +62,7 @@ private: public: static std::unique_ptr> make(IndicatorF2D& domainF, int resolution, T tau, int re); - Grid2D(IndicatorF2D& domainF, int resolution, T tau, int re); + Grid2D(FunctorPtr>&& domainF, int resolution, T tau, int re); UnitConverter& getConverter(); CuboidGeometry2D& getCuboidGeometry(); @@ -76,8 +80,26 @@ public: Vector alignOriginToGrid(Vector physR) const; Vector alignExtendToGrid(Vector physR) const; - Grid2D& refine(IndicatorF2D& domainF); - Grid2D& refine(Vector origin, Vector extend); + RefiningGrid2D& refine(Vector origin, Vector extend, bool addCouplers=true); + +}; + +template class DESCRIPTOR> +class RefiningGrid2D : public Grid2D { +private: + const Vector _origin; + const Vector _extend; + + Grid2D& _parentGrid; + +public: + RefiningGrid2D(Grid2D& parentGrid, Vector origin, Vector extend); + + Vector getOrigin() const; + Vector getExtend() const; + + /// Indicates the subdomain of the coarse grid rendered moot by refinement + std::unique_ptr> getRefinedOverlap() const; }; diff --git a/src/refinement/grid2D.hh b/src/refinement/grid2D.hh index 99a2563..aa82240 100644 --- a/src/refinement/grid2D.hh +++ b/src/refinement/grid2D.hh @@ -27,6 +27,7 @@ #include "grid2D.h" #include "coupler2D.hh" +#include "utilities/vectorHelpers.h" #include "communication/heuristicLoadBalancer.h" namespace olb { @@ -43,7 +44,8 @@ std::unique_ptr> Grid2D::make( } template class DESCRIPTOR> -Grid2D::Grid2D(IndicatorF2D& domainF, int resolution, T tau, int re): +Grid2D::Grid2D(FunctorPtr>&& domainF, int resolution, T tau, int re): + _domainF(std::move(domainF)), _converter(new UnitConverterFromResolutionAndRelaxationTime( resolution, // resolution: number of voxels per charPhysL tau, // latticeRelaxationTime: relaxation time, has to be greater than 0.5! @@ -53,7 +55,7 @@ Grid2D::Grid2D(IndicatorF2D& domainF, int resolution, T tau, in T{1}, // physDensity: physical density in __kg / m^3__ T{1})), _cuboids(new CuboidGeometry2D( - domainF, + *_domainF, _converter->getConversionFactorLength(), #ifdef PARALLEL_MODE_MPI singleton::mpi().getSize() @@ -155,19 +157,6 @@ CoarseCoupler2D& Grid2D::addCoarseCoupling( return *_coarseCouplers.back(); } -template class DESCRIPTOR> -Grid2D& Grid2D::refine(IndicatorF2D& domainF) -{ - _fineGrids.emplace_back( - new Grid2D( - domainF, - 2*getConverter().getResolution(), - 2*getConverter().getLatticeRelaxationTime() - 0.5, - getConverter().getReynoldsNumber() - )); - return *_fineGrids.back(); -} - template class DESCRIPTOR> Vector Grid2D::alignOriginToGrid(Vector physR) const { @@ -180,37 +169,84 @@ template class DESCRIPTOR> Vector Grid2D::alignExtendToGrid(Vector extend) const { const T deltaX = _converter->getPhysDeltaX(); - return floor(extend / deltaX) * deltaX; + return util::floor(extend / deltaX) * deltaX; } template class DESCRIPTOR> -Grid2D& Grid2D::refine(Vector origin, Vector extend) +RefiningGrid2D& Grid2D::refine( + Vector wantedOrigin, Vector wantedExtend, bool addCouplers) { - IndicatorCuboid2D fineCuboid(extend, origin); - auto& fineGrid = refine(fineCuboid); + if (addCouplers) { + auto& fineGrid = refine(wantedOrigin, wantedExtend, false); + + const Vector origin = fineGrid.getOrigin(); + const Vector extend = fineGrid.getExtend(); + + const Vector extendX = {extend[0],0}; + const Vector extendY = {0,extend[1]}; - const Vector extendX = {extend[0],0}; - const Vector extendY = {0,extend[1]}; + addFineCoupling(fineGrid, origin, extendY); + addFineCoupling(fineGrid, origin + extendX, extendY); + addFineCoupling(fineGrid, origin + extendY, extendX); + addFineCoupling(fineGrid, origin, extendX); - addFineCoupling(fineGrid, origin, extendY); - addFineCoupling(fineGrid, origin + extendX, extendY); - addFineCoupling(fineGrid, origin + extendY, extendX); - addFineCoupling(fineGrid, origin, extendX); + const T coarseDeltaX = getConverter().getPhysDeltaX(); + const Vector innerOrigin = origin + coarseDeltaX; + const Vector innerExtendX = extendX - Vector {2*coarseDeltaX,0}; + const Vector innerExtendY = extendY - Vector {0,2*coarseDeltaX}; - const T coarseDeltaX = getConverter().getPhysDeltaX(); - const Vector innerOrigin = origin + coarseDeltaX; - const Vector innerExtendX = extendX - Vector {2*coarseDeltaX,0}; - const Vector innerExtendY = extendY - Vector {0,2*coarseDeltaX}; + addCoarseCoupling(fineGrid, innerOrigin, innerExtendY); + addCoarseCoupling(fineGrid, innerOrigin + innerExtendX, innerExtendY); + addCoarseCoupling(fineGrid, innerOrigin + innerExtendY, innerExtendX); + addCoarseCoupling(fineGrid, innerOrigin, innerExtendX); - addCoarseCoupling(fineGrid, innerOrigin, innerExtendY); - addCoarseCoupling(fineGrid, innerOrigin + innerExtendX, innerExtendY); - addCoarseCoupling(fineGrid, innerOrigin + innerExtendY, innerExtendX); - addCoarseCoupling(fineGrid, innerOrigin, innerExtendX); + return fineGrid; + } + else { + const Vector origin = alignOriginToGrid(wantedOrigin); + const Vector extend = alignExtendToGrid(wantedExtend); - return fineGrid; + _fineGrids.emplace_back( + new RefiningGrid2D(*this, origin, extend)); + return *_fineGrids.back(); + } } +template class DESCRIPTOR> +RefiningGrid2D::RefiningGrid2D( + Grid2D& parentGrid, Vector origin, Vector extend): + Grid2D( + std::unique_ptr>(new IndicatorCuboid2D(extend, origin)), + 2*parentGrid.getConverter().getResolution(), + 2*parentGrid.getConverter().getLatticeRelaxationTime() - 0.5, + parentGrid.getConverter().getReynoldsNumber()), + _origin(origin), + _extend(extend), + _parentGrid(parentGrid) +{ } + +template class DESCRIPTOR> +Vector RefiningGrid2D::getOrigin() const +{ + return _origin; +} + +template class DESCRIPTOR> +Vector RefiningGrid2D::getExtend() const +{ + return _extend; +} + +template class DESCRIPTOR> +std::unique_ptr> RefiningGrid2D::getRefinedOverlap() const +{ + const T coarseDeltaX = _parentGrid.getConverter().getPhysDeltaX(); + + return std::unique_ptr>( + new IndicatorCuboid2D(_extend - 4*coarseDeltaX, _origin + 2*coarseDeltaX)); +} + } #endif -- cgit v1.2.3