From 17fa484a72b647a2806e642bbd8fa616d1776f3f Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Wed, 6 Feb 2019 21:51:46 +0100 Subject: Initial extraction of common cylinder2d model setup functions I am not quite happy with how this looks right now but at least both validation examples are for the most part condensed to only differ in their refinement setup. Perhaps a SchaeferTurek-specific "Solver" would further improve reproducibility? --- apps/adrian/cylinder2d/common/model.h | 222 ++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 apps/adrian/cylinder2d/common/model.h (limited to 'apps/adrian/cylinder2d/common') diff --git a/apps/adrian/cylinder2d/common/model.h b/apps/adrian/cylinder2d/common/model.h new file mode 100644 index 0000000..89b276b --- /dev/null +++ b/apps/adrian/cylinder2d/common/model.h @@ -0,0 +1,222 @@ +/* + * 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. + */ + +namespace SchaeferTurek { + +const T deltaR = cylinderD / N; // coarse lattice spacing + +const Vector modelOrigin {0.0, 0.0}; +const Vector modelExtend {22*cylinderD + deltaR, 4.1*cylinderD + deltaR}; + +const Vector cylinderCenter {2*cylinderD, 2*cylinderD + deltaR/2}; + +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 {cylinderCenter[0], cylinderCenter[1]}; + IndicatorCircle2D obstacle(cylinderOrigin, cylinderD/2); + sGeometry.rename(1,5,obstacle); + } + + sGeometry.clean(); + sGeometry.innerClean(); + sGeometry.checkForErrors(); + + clout << "Prepare Geometry ... OK" << std::endl; +} + +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(); + 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, converter.getPhysDeltaX()/2); + + sLattice.defineU(sGeometry, 3, u); + } +} + +void getResults(Grid2D& grid, + const std::string& prefix, + int iT) +{ + auto& converter = grid.getConverter(); + auto& sLattice = grid.getSuperLattice(); + auto& sGeometry = grid.getSuperGeometry(); + + SuperVTMwriter2D vtmWriter(prefix); + 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, int iT, bool print=true) +{ + 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] { cylinderCenter[0] - radiusCylinder, cylinderCenter[1] }; + const T point2[2] { cylinderCenter[0] + radiusCylinder, cylinderCenter[1] }; + + T pressureInFrontOfCylinder, pressureBehindCylinder; + intpolatePressure(&pressureInFrontOfCylinder, point1); + intpolatePressure(&pressureBehindCylinder, point2); + const T pressureDrop = pressureInFrontOfCylinder - pressureBehindCylinder; + + const int input[3] {}; + T drag[dragF.getTargetDim()] {}; + dragF(drag, input); + + static Gnuplot gplot("results"); + gplot.setData(converter.getPhysTime(iT), {drag[0], drag[1], pressureDrop}, {"drag", "lift", "deltaP"}, "bottom right", {'l','l'}); + gplot.writePNG(); + + if (print) { + OstreamManager clout(std::cout, "measurement"); + clout << "pressureDrop=" << pressureDrop + << "; drag=" << drag[0] + << "; lift=" << drag[1] + << endl; + } +} + +} -- cgit v1.2.3