From 958f23f04093b39aa7be50dec5e4d589b6275277 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 4 Feb 2019 16:52:28 +0100 Subject: Add cylinder2d resolution redistribution example i.e. cylinder2d_optimized_grid contains roughly as many grid cells as a uniformly resolved N=12 lattice but models the cylinder in more detail and is more stable. --- apps/adrian/cylinder2d_optimized_grid/Makefile | 105 ++++++ .../cylinder2d_optimized_grid/cylinder2d.cpp | 373 +++++++++++++++++++++ .../cylinder2d_optimized_grid/definitions.mk | 30 ++ apps/adrian/cylinder2d_optimized_grid/module.mk | 29 ++ .../cylinder2d_outflow_refinement/cylinder2d.cpp | 2 +- 5 files changed, 538 insertions(+), 1 deletion(-) create mode 100644 apps/adrian/cylinder2d_optimized_grid/Makefile create mode 100644 apps/adrian/cylinder2d_optimized_grid/cylinder2d.cpp create mode 100644 apps/adrian/cylinder2d_optimized_grid/definitions.mk create mode 100644 apps/adrian/cylinder2d_optimized_grid/module.mk (limited to 'apps') diff --git a/apps/adrian/cylinder2d_optimized_grid/Makefile b/apps/adrian/cylinder2d_optimized_grid/Makefile new file mode 100644 index 0000000..a953954 --- /dev/null +++ b/apps/adrian/cylinder2d_optimized_grid/Makefile @@ -0,0 +1,105 @@ +# This file is part of the OpenLB library +# +# Copyright (C) 2007 Mathias Krause +# E-mail contact: info@openlb.net +# The most recent release of OpenLB can be downloaded at +# +# +# This program 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 2 +# of the License, or (at your option) any later version. +# +# This program 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 this program; if not, write to the Free +# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. + +########################################################################### +## definitions + +include definitions.mk + +include $(ROOT)/global.mk + +OBJECTS := $(foreach file, $(SRC), $(PWD)/$(file:.cpp=.o)) +DEPS := $(foreach file, $(SRC), $(PWD)/$(file:.cpp=.d)) + +########################################################################### +## all + +all : depend compile link + + +########################################################################### +## dependencies + +depend : $(DEPS) + +$(PWD)/%.d : %.cpp + @echo Create dependencies for $< + @$(SHELL) -ec '$(CXX) -M $(CXXFLAGS) $(IDIR) $< \ + | sed -e "s!$*\.o!$(PWD)\/$*\.o!1" > .tmpfile; \ + cp -f .tmpfile $@;' + +########################################################################### +## compile + +compile : $(OBJECTS) + +$(PWD)/%.o: %.cpp + @echo Compile $< + $(CXX) $(CXXFLAGS) $(IDIR) -c $< -o $@ + +########################################################################### +## clean + +clean : cleanrub cleanobj cleandep + +cleanrub: + @echo Clean rubbish files + @rm -f *~ core .tmpfile tmp/*.* $(OUTPUT) + @rm -f tmp/vtkData/*.* tmp/vtkData/data/*.* tmp/imageData/*.* tmp/gnuplotData/*.* tmp/gnuplotData/data/*.* + +cleanobj: + @echo Clean object files + @rm -f $(OBJECTS) + +cleandep: + @echo Clean dependencies files + @rm -f $(DEPS) + +cleanbuild: + @cd $(ROOT); \ + $(MAKE) cleanlib; + +########################################################################### +## update lib + +$(ROOT)/$(LIBDIR)/lib$(LIB).a : + @cd $(ROOT); \ + $(MAKE) all + +########################################################################### +## link + +link: $(OUTPUT) + +$(OUTPUT): $(OBJECTS) $(ROOT)/$(LIBDIR)/lib$(LIB).a + @echo Link $@ + $(CXX) $(foreach file, $(SRC), $(file:.cpp=.o)) $(LDFLAGS) -L$(ROOT)/$(LIBDIR) -l$(LIB) -lz -o $@ + +########################################################################### +## include dependencies + +ifneq "$(strip $(wildcard *.d))" "" + include $(foreach file,$(DEPS),$(file)) +endif + +########################################################################### +########################################################################### diff --git a/apps/adrian/cylinder2d_optimized_grid/cylinder2d.cpp b/apps/adrian/cylinder2d_optimized_grid/cylinder2d.cpp new file mode 100644 index 0000000..74ad91b --- /dev/null +++ b/apps/adrian/cylinder2d_optimized_grid/cylinder2d.cpp @@ -0,0 +1,373 @@ +/* + * Lattice Boltzmann grid refinement sample, written in C++, + * using the OpenLB library + * + * Copyright (C) 2019 Adrian Kummerländer + * E-mail contact: info@openlb.net + * The most recent release of OpenLB can be downloaded at + * + * + * This program 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 2 + * of the License, or (at your option) any later version. + * + * This program 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 this program; if not, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "olb2D.h" +#ifndef OLB_PRECOMPILED +#include "olb2D.hh" +#endif + +#include + +using namespace olb; + +typedef double T; + +#define DESCRIPTOR descriptors::D2Q9Descriptor + +/// Setup geometry relative to cylinder diameter as defined by [SchaeferTurek96] +const T cylinderD = 0.1; +const int N = 5; // resolution of the cylinder +const T deltaR = cylinderD / N; // coarse lattice spacing +const T lx = 22*cylinderD + deltaR; // length of the channel +const T ly = 4.1*cylinderD + deltaR; // height of the channel +const T cylinderX = 2*cylinderD; +const T cylinderY = 2*cylinderD + deltaR/2; + +const T Re = 100.; // Reynolds number +const T tau = 0.51; // relaxation time +const T maxPhysT = 16.; // max. simulation time in s, SI unit + +const Characteristics PhysCharacteristics( + 0.1, // char. phys. length + 1.0, // char. phys. velocity + 0.1/Re, // phsy. kinematic viscosity + 1.0); // char. phys. density + +void prepareGeometry(Grid2D& grid, Vector origin, Vector extend) +{ + OstreamManager clout(std::cout,"prepareGeometry"); + clout << "Prepare Geometry ..." << std::endl; + + auto& converter = grid.getConverter(); + auto& sGeometry = grid.getSuperGeometry(); + + sGeometry.rename(0,1); + + const T physSpacing = converter.getPhysDeltaX(); + + // Set material number for channel walls + { + const Vector wallExtend { extend[0]+physSpacing/2, physSpacing/2 }; + const Vector wallOrigin = origin - physSpacing/4; + + IndicatorCuboid2D lowerWall(wallExtend, wallOrigin); + sGeometry.rename(1,2,lowerWall); + } + { + const Vector wallExtend { extend[0]+physSpacing/2, physSpacing/2 }; + const Vector wallOrigin { origin[0]-physSpacing/4, extend[1]-physSpacing/4 }; + + IndicatorCuboid2D upperWall(wallExtend, wallOrigin); + sGeometry.rename(1,2,upperWall); + } + + // Set material number for inflow and outflow + { + const Vector inflowExtend { physSpacing/2, extend[1]+physSpacing/4 }; + const Vector inflowOrigin = origin - physSpacing/4; + + IndicatorCuboid2D inflow(inflowExtend, inflowOrigin); + sGeometry.rename(1,3,inflow); + } + { + const Vector outflowExtend { physSpacing/2, extend[1]+physSpacing/4 }; + const Vector outflowOrigin { extend[0]-physSpacing/4, origin[0]-physSpacing/4 }; + + IndicatorCuboid2D outflow(outflowExtend, outflowOrigin); + sGeometry.rename(1,4,outflow); + } + + // Set material number for vertically centered cylinder + { + const Vector cylinderOrigin = origin + Vector {cylinderX, cylinderY}; + IndicatorCircle2D obstacle(cylinderOrigin, cylinderD/2); + sGeometry.rename(1,5,obstacle); + } + + sGeometry.clean(); + sGeometry.innerClean(); + sGeometry.checkForErrors(); + + clout << "Prepare Geometry ... OK" << std::endl; +} + +void disableRefinedArea(Grid2D& coarseGrid, + RefiningGrid2D& fineGrid) +{ + auto& sGeometry = coarseGrid.getSuperGeometry(); + auto refinedOverlap = fineGrid.getRefinedOverlap(); + sGeometry.reset(*refinedOverlap); +} + +void prepareLattice(Grid2D& grid) +{ + OstreamManager clout(std::cout,"prepareLattice"); + clout << "Prepare lattice ..." << std::endl; + + auto& converter = grid.getConverter(); + auto& sGeometry = grid.getSuperGeometry(); + auto& sLattice = grid.getSuperLattice(); + + Dynamics& bulkDynamics = grid.addDynamics( + std::unique_ptr>( + new BGKdynamics( + grid.getConverter().getLatticeRelaxationFrequency(), + instances::getBulkMomenta()))); + + sOnLatticeBoundaryCondition2D& sBoundaryCondition = grid.getOnLatticeBoundaryCondition(); + //createInterpBoundaryCondition2D(sBoundaryCondition); + createLocalBoundaryCondition2D(sBoundaryCondition); + + const T omega = converter.getLatticeRelaxationFrequency(); + + sLattice.defineDynamics(sGeometry, 0, &instances::getNoDynamics()); + sLattice.defineDynamics(sGeometry, 1, &bulkDynamics); // bulk + sLattice.defineDynamics(sGeometry, 2, &bulkDynamics); // walls + sLattice.defineDynamics(sGeometry, 3, &bulkDynamics); // inflow + sLattice.defineDynamics(sGeometry, 4, &bulkDynamics); // outflow + sLattice.defineDynamics(sGeometry, 5, &instances::getBounceBack()); // cylinder + + sBoundaryCondition.addVelocityBoundary(sGeometry, 2, omega); + sBoundaryCondition.addVelocityBoundary(sGeometry, 3, omega); + sBoundaryCondition.addPressureBoundary(sGeometry, 4, omega); + + AnalyticalConst2D rho0(1.0); + AnalyticalConst2D u0(0.0, 0.0); + + auto materials = sGeometry.getMaterialIndicator({1, 2, 3, 4}); + sLattice.defineRhoU(materials, rho0, u0); + sLattice.iniEquilibrium(materials, rho0, u0); + + sLattice.initialize(); + + clout << "Prepare lattice ... OK" << std::endl; + sGeometry.print(); +} + +void setBoundaryValues(Grid2D& grid, int iT) +{ + auto& converter = grid.getConverter(); + auto& sGeometry = grid.getSuperGeometry(); + auto& sLattice = grid.getSuperLattice(); + + const int iTmaxStart = converter.getLatticeTime(0.4*16); + const int iTupdate = 5; + + if ( iT % iTupdate == 0 && iT <= iTmaxStart ) { + PolynomialStartScale StartScale(iTmaxStart, 1); + + T iTvec[1] { T(iT) }; + T frac[1] { }; + StartScale(frac, iTvec); + + const T maxVelocity = converter.getCharLatticeVelocity() * 3./2. * frac[0]; + Poiseuille2D u(sGeometry, 3, maxVelocity, deltaR/2); + + sLattice.defineU(sGeometry, 3, u); + } +} + +void getResults(const std::string& prefix, + Grid2D& grid, + int iT) +{ + OstreamManager clout(std::cout,"getResults"); + + auto& converter = grid.getConverter(); + auto& sLattice = grid.getSuperLattice(); + auto& sGeometry = grid.getSuperGeometry(); + + SuperVTMwriter2D vtmWriter(prefix + "cylinder2d"); + SuperLatticePhysVelocity2D velocity(sLattice, converter); + SuperLatticePhysPressure2D pressure(sLattice, converter); + SuperLatticeGeometry2D geometry(sLattice, sGeometry); + SuperLatticeKnudsen2D knudsen(sLattice); + vtmWriter.addFunctor(geometry); + vtmWriter.addFunctor(velocity); + vtmWriter.addFunctor(pressure); + vtmWriter.addFunctor(knudsen); + + if (iT==0) { + vtmWriter.createMasterFile(); + } + + vtmWriter.write(iT); +} + +void takeMeasurements(Grid2D& grid) +{ + static T maxDrag = 0.0; + + OstreamManager clout(std::cout,"measurement"); + + auto& sLattice = grid.getSuperLattice(); + auto& sGeometry = grid.getSuperGeometry(); + auto& converter = grid.getConverter(); + + SuperLatticePhysPressure2D pressure(sLattice, converter); + AnalyticalFfromSuperF2D intpolatePressure(pressure, true); + SuperLatticePhysDrag2D dragF(sLattice, sGeometry, 5, converter); + + const T radiusCylinder = cylinderD/2; + + const T point1[2] { cylinderX - radiusCylinder, cylinderY }; + const T point2[2] { cylinderX + radiusCylinder, cylinderY }; + + T pressureInFrontOfCylinder, pressureBehindCylinder; + intpolatePressure(&pressureInFrontOfCylinder, point1); + intpolatePressure(&pressureBehindCylinder, point2); + + T pressureDrop = pressureInFrontOfCylinder - pressureBehindCylinder; + clout << "pressureDrop=" << pressureDrop; + + const int input[3] {}; + T drag[dragF.getTargetDim()] {}; + dragF(drag, input); + if (drag[0] > maxDrag) { + maxDrag = drag[0]; + }; + clout << "; drag=" << drag[0] << "; maxDrag: " << maxDrag << "; lift=" << drag[1] << endl; +} + +int main(int argc, char* argv[]) +{ + olbInit(&argc, &argv); + singleton::directories().setOutputDir("./tmp/"); + OstreamManager clout(std::cout,"main"); + + const Vector coarseOrigin {0.0, 0.0}; + const Vector coarseExtend {lx, ly}; + IndicatorCuboid2D coarseCuboid(coarseExtend, coarseOrigin); + + Grid2D coarseGrid( + coarseCuboid, + RelaxationTime(tau), + N, + PhysCharacteristics); + const Vector domainOrigin = coarseGrid.getSuperGeometry().getStatistics().getMinPhysR(0); + const Vector domainExtend = coarseGrid.getSuperGeometry().getStatistics().getPhysExtend(0); + + prepareGeometry(coarseGrid, domainOrigin, domainExtend); + + const auto coarseDeltaX = coarseGrid.getConverter().getPhysDeltaX(); + + const Vector fineOutflowExtend {1*cylinderD, domainExtend[1]}; + const Vector fineOutflowOrigin {domainExtend[0]-1*cylinderD, 0}; + + auto& fineOutflowGrid = coarseGrid.refine(fineOutflowOrigin, fineOutflowExtend, false); + prepareGeometry(fineOutflowGrid, domainOrigin, domainExtend); + + { + const Vector origin = fineOutflowGrid.getOrigin(); + const Vector extend = fineOutflowGrid.getExtend(); + + const Vector extendY = {0,extend[1]}; + + coarseGrid.addFineCoupling(fineOutflowGrid, origin, extendY); + coarseGrid.addCoarseCoupling(fineOutflowGrid, origin + Vector {coarseDeltaX,0}, extendY); + + IndicatorCuboid2D refined(extend, origin + Vector {2*coarseDeltaX,0}); + coarseGrid.getSuperGeometry().reset(refined); + } + + const Vector fineOutflowExtend2 {0.5*cylinderD, domainExtend[1]}; + const Vector fineOutflowOrigin2 {domainExtend[0]-0.5*cylinderD, 0}; + + auto& fineOutflowGrid2 = fineOutflowGrid.refine(fineOutflowOrigin2, fineOutflowExtend2, false); + prepareGeometry(fineOutflowGrid2, domainOrigin, domainExtend); + + { + const Vector origin = fineOutflowGrid2.getOrigin(); + const Vector extend = fineOutflowGrid2.getExtend(); + + const Vector extendY = {0,extend[1]}; + + fineOutflowGrid.addFineCoupling(fineOutflowGrid2, origin, extendY); + fineOutflowGrid.addCoarseCoupling(fineOutflowGrid2, origin + Vector {coarseDeltaX,0}, extendY); + + IndicatorCuboid2D refined(extend, origin + Vector {coarseDeltaX,0}); + fineOutflowGrid.getSuperGeometry().reset(refined); + } + + const Vector fineExtend {10.5*cylinderD, domainExtend[1]-2*coarseDeltaX}; + const Vector fineOrigin {0.5*cylinderD, coarseDeltaX}; + + auto& fineGrid = coarseGrid.refine(fineOrigin, fineExtend); + prepareGeometry(fineGrid, domainOrigin, domainExtend); + disableRefinedArea(coarseGrid, fineGrid); + + const Vector fineExtend2 {5*cylinderD, fineGrid.getExtend()[1]-2*coarseDeltaX}; + const Vector fineOrigin2 {1*cylinderD, (domainExtend[1]-fineExtend2[1])/2}; + + auto& fineGrid2 = fineGrid.refine(fineOrigin2, fineExtend2); + prepareGeometry(fineGrid2, domainOrigin, domainExtend); + disableRefinedArea(fineGrid, fineGrid2); + + const Vector fineExtend3 {1.25*cylinderD, 1.25*cylinderD}; + const Vector fineOrigin3 {cylinderX-fineExtend3[0]/2, cylinderY-fineExtend3[1]/2}; + + auto& fineGrid3 = fineGrid2.refine(fineOrigin3, fineExtend3); + prepareGeometry(fineGrid3, domainOrigin, domainExtend); + disableRefinedArea(fineGrid2, fineGrid3); + + prepareLattice(coarseGrid); + prepareLattice(fineOutflowGrid); + prepareLattice(fineOutflowGrid2); + prepareLattice(fineGrid); + prepareLattice(fineGrid2); + prepareLattice(fineGrid3); + + clout << "Total number of active cells: " << coarseGrid.getActiveVoxelN() << endl; + clout << "Starting simulation..." << endl; + + const int statIter = coarseGrid.getConverter().getLatticeTime(0.5); + Timer timer( + coarseGrid.getConverter().getLatticeTime(maxPhysT), + coarseGrid.getSuperGeometry().getStatistics().getNvoxel()); + timer.start(); + + for (int iT = 0; iT <= coarseGrid.getConverter().getLatticeTime(maxPhysT); ++iT) { + setBoundaryValues(coarseGrid, iT); + + coarseGrid.collideAndStream(); + + if (iT == 0 || iT%statIter == 0) { + timer.update(iT); + timer.printStep(); + + getResults("level0_", coarseGrid, iT); + getResults("level1_outflow_", fineOutflowGrid, iT); + getResults("level2_outflow_", fineOutflowGrid2, iT); + getResults("level1_", fineGrid, iT); + getResults("level2_", fineGrid2, iT); + getResults("level3_", fineGrid3, iT); + + takeMeasurements(fineGrid3); + } + } + + timer.stop(); + timer.printSummary(); +} diff --git a/apps/adrian/cylinder2d_optimized_grid/definitions.mk b/apps/adrian/cylinder2d_optimized_grid/definitions.mk new file mode 100644 index 0000000..6584906 --- /dev/null +++ b/apps/adrian/cylinder2d_optimized_grid/definitions.mk @@ -0,0 +1,30 @@ +# This file is part of the OpenLB library +# +# Copyright (C) 2007 Mathias Krause +# E-mail contact: info@openlb.net +# The most recent release of OpenLB can be downloaded at +# +# +# This program 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 2 +# of the License, or (at your option) any later version. +# +# This program 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 this program; if not, write to the Free +# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. + + +########################################################################### +########################################################################### +## DEFINITIONS TO BE CHANGED + +ROOT := ../../.. +SRC := cylinder2d.cpp +OUTPUT := cylinder2d diff --git a/apps/adrian/cylinder2d_optimized_grid/module.mk b/apps/adrian/cylinder2d_optimized_grid/module.mk new file mode 100644 index 0000000..1190482 --- /dev/null +++ b/apps/adrian/cylinder2d_optimized_grid/module.mk @@ -0,0 +1,29 @@ +# This file is part of the OpenLB library +# +# Copyright (C) 2017 Markus Mohrhard +# E-mail contact: info@openlb.net +# The most recent release of OpenLB can be downloaded at +# +# +# This program 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 2 +# of the License, or (at your option) any later version. +# +# This program 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 this program; if not, write to the Free +# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. + +current_dir := $(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))) + +include global.mk +include rules.mk +include $(addsuffix definitions.mk, $(current_dir)) + +$(eval $(call sample,$(current_dir)$(OUTPUT),$(addprefix $(current_dir), $(SRC)))) diff --git a/apps/adrian/cylinder2d_outflow_refinement/cylinder2d.cpp b/apps/adrian/cylinder2d_outflow_refinement/cylinder2d.cpp index 598f828..8b6a49c 100644 --- a/apps/adrian/cylinder2d_outflow_refinement/cylinder2d.cpp +++ b/apps/adrian/cylinder2d_outflow_refinement/cylinder2d.cpp @@ -318,7 +318,7 @@ int main(int argc, char* argv[]) clout << "Total number of active cells: " << coarseGrid.getActiveVoxelN() << endl; clout << "Starting simulation..." << endl; - const int statIter = coarseGrid.getConverter().getLatticeTime(0.05); + const int statIter = coarseGrid.getConverter().getLatticeTime(0.5); Timer timer( coarseGrid.getConverter().getLatticeTime(maxPhysT), coarseGrid.getSuperGeometry().getStatistics().getNvoxel()); -- cgit v1.2.3