From d2254e2c0cd3c995e50f44c4016e354d94805f8c Mon Sep 17 00:00:00 2001
From: Anna Wellmann <a.wellmann@tu-bs.de>
Date: Mon, 11 Jul 2022 09:32:54 +0000
Subject: [PATCH] Make the sphere app more similar to open source

---
 apps/gpu/LBM/SphereGPU/Sphere.cpp | 353 ++++++++++++++++--------------
 1 file changed, 186 insertions(+), 167 deletions(-)

diff --git a/apps/gpu/LBM/SphereGPU/Sphere.cpp b/apps/gpu/LBM/SphereGPU/Sphere.cpp
index f0d629afe..e63166a73 100644
--- a/apps/gpu/LBM/SphereGPU/Sphere.cpp
+++ b/apps/gpu/LBM/SphereGPU/Sphere.cpp
@@ -1,4 +1,35 @@
-
+//=======================================================================================
+// ____          ____    __    ______     __________   __      __       __        __
+// \    \       |    |  |  |  |   _   \  |___    ___| |  |    |  |     /  \      |  |
+//  \    \      |    |  |  |  |  |_)   |     |  |     |  |    |  |    /    \     |  |
+//   \    \     |    |  |  |  |   _   /      |  |     |  |    |  |   /  /\  \    |  |
+//    \    \    |    |  |  |  |  | \  \      |  |     |   \__/   |  /  ____  \   |  |____
+//     \    \   |    |  |__|  |__|  \__\     |__|      \________/  /__/    \__\  |_______|
+//      \    \  |    |   ________________________________________________________________
+//       \    \ |    |  |  ______________________________________________________________|
+//        \    \|    |  |  |         __          __     __     __     ______      _______
+//         \         |  |  |_____   |  |        |  |   |  |   |  |   |   _  \    /  _____)
+//          \        |  |   _____|  |  |        |  |   |  |   |  |   |  | \  \   \_______
+//           \       |  |  |        |  |_____   |   \_/   |   |  |   |  |_/  /    _____  |
+//            \ _____|  |__|        |________|   \_______/    |__|   |______/    (_______/
+//
+//  This file is part of VirtualFluids. VirtualFluids 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.
+//
+//  VirtualFluids 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 VirtualFluids (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
+//
+//! \file LidDrivenCavity.cpp
+//! \ingroup Applications
+//! \author Martin Schoenherr, Stephan Lenz, Anna Wellmann
+//=======================================================================================
 #define _USE_MATH_DEFINES
 #include <exception>
 #include <filesystem>
@@ -13,15 +44,11 @@
 //////////////////////////////////////////////////////////////////////////
 
 #include "Core/DataTypes.h"
-#include "PointerDefinitions.h"
-
-#include "Core/StringUtilities/StringUtil.h"
-
+#include "Core/LbmOrGks.h"
+#include "Core/Logger/Logger.h"
 #include "Core/VectorTypes.h"
-
-#include <basics/config/ConfigurationFile.h>
-
-#include <logger/Logger.h>
+#include "PointerDefinitions.h"
+#include "config/ConfigurationFile.h"
 
 //////////////////////////////////////////////////////////////////////////
 
@@ -30,187 +57,179 @@
 #include "GridGenerator/grid/GridBuilder/MultipleGridBuilder.h"
 #include "GridGenerator/grid/GridFactory.h"
 
-#include "GridGenerator/io/GridVTKWriter/GridVTKWriter.h"
-#include "GridGenerator/io/STLReaderWriter/STLReader.h"
-#include "GridGenerator/io/STLReaderWriter/STLWriter.h"
-#include "GridGenerator/io/SimulationFileWriter/SimulationFileWriter.h"
-
 #include "GridGenerator/geometries/Sphere/Sphere.h"
+#include "GridGenerator/geometries/TriangularMesh/TriangularMesh.h"
 
 //////////////////////////////////////////////////////////////////////////
 
-#include "VirtualFluids_GPU/BoundaryConditions/BoundaryConditionFactory.h"
-#include "VirtualFluids_GPU/Communication/Communicator.h"
 #include "VirtualFluids_GPU/DataStructureInitializer/GridProvider.h"
-#include "VirtualFluids_GPU/DataStructureInitializer/GridReaderFiles/GridReader.h"
 #include "VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/GridGenerator.h"
+#include "VirtualFluids_GPU/GPU/CudaMemoryManager.h"
+#include "VirtualFluids_GPU/Communication/Communicator.h"
 #include "VirtualFluids_GPU/LBM/Simulation.h"
 #include "VirtualFluids_GPU/Output/FileWriter.h"
 #include "VirtualFluids_GPU/Parameter/Parameter.h"
+#include "VirtualFluids_GPU/BoundaryConditions/BoundaryConditionFactory.h"
+#include "VirtualFluids_GPU/BoundaryConditions/BoundaryConditionFactory.h"
 
-#include "VirtualFluids_GPU/GPU/CudaMemoryManager.h"
-
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-//
-//          U s e r    s e t t i n g s
-//
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-const real L = 1.0;
-const real dSphere = 0.2;
-const real Re = 1000.0; // related to the sphere's diameter
-const real velocity = 1.0;
-const real dt = (real)0.5e-3;
-const uint nx = 64;
-
-const uint timeStepOut = 10000;
-const uint timeStepEnd = 100000;
-
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////
 
-void multipleLevel(const std::string &configPath)
+int main(int argc, char *argv[])
 {
-    logging::Logger::addStream(&std::cout);
-    logging::Logger::setDebugLevel(logging::Logger::Level::INFO_LOW);
-    logging::Logger::timeStamp(logging::Logger::ENABLE);
-    logging::Logger::enablePrintedRankNumbers(logging::Logger::ENABLE);
-
-    vf::gpu::Communicator &communicator = vf::gpu::Communicator::getInstance();
-
-    auto gridFactory = GridFactory::make();
-    gridFactory->setTriangularMeshDiscretizationMethod(TriangularMeshDiscretizationMethod::POINT_IN_OBJECT);
-    auto gridBuilder = MultipleGridBuilder::makeShared(gridFactory);
-
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-    real dx = L / real(nx);
-
-    gridBuilder->addCoarseGrid(-1.0 * L, -0.5 * L, -0.5 * L, 
-                                6.0 * L,  0.5 * L,  0.5 * L, dx);
-
-    Object *sphere = new Sphere(0.0, 0.0, 0.0, dSphere / 2.0);
-    gridBuilder->addGeometry(sphere);
-
-    gridBuilder->setPeriodicBoundaryCondition(false, false, false);
-
-    gridBuilder->buildGrids(LBM, false); // buildGrids() has to be called before setting the BCs!!!!
-
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-    vf::basics::ConfigurationFile config;
-    config.load(configPath);
-
-    SPtr<Parameter> para =
-        std::make_shared<Parameter>(config, communicator.getNummberOfProcess(), communicator.getPID());
-    BoundaryConditionFactory bcFactory = BoundaryConditionFactory();
-
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-    const real velocityLB = velocity * dt / dx; // LB units
-
-    const real viscosityLB = (dSphere / dx) * velocityLB / Re; // LB units
-
-    VF_LOG_INFO("velocity  [dx/dt] = {}", velocityLB);
-    VF_LOG_INFO("viscosity [dx^2/dt] = {}", viscosityLB);
-
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-    para->setDevices(std::vector<uint>{ (uint)0 });
-
-    para->setFName(para->getOutputPath() + "/" + para->getOutputPrefix());
-
-    para->setPrintFiles(true);
-
-    para->setMaxLevel(2);
-
-    para->setVelocity(velocityLB);
-    para->setViscosity(viscosityLB);
-
-    para->setVelocityRatio(velocity / velocityLB);
-    para->setDensityRatio((real)1.0);
-
-    // para->setMainKernel("CumulantK17CompChim");
-
-    // para->setInitialCondition([&](real coordX, real coordY, real coordZ, real &rho, real &vx, real &vy, real &vz) {
-    //     rho = (real)0.0;
-    //     vx =  (real)velocityLB;
-    //     vy =  (real)0.0;
-    //     vz =  (real)0.0;
-    // });
-
-    para->setTOut(timeStepOut);
-    para->setTEnd(timeStepEnd);
-
-    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-    gridBuilder->setVelocityBoundaryCondition(SideType::MX, velocityLB, 0.0, 0.0);
-
-    gridBuilder->setSlipBoundaryCondition(SideType::PY, 0.0, 0.0, 0.0);
-    gridBuilder->setSlipBoundaryCondition(SideType::MY, 0.0, 0.0, 0.0);
-    gridBuilder->setSlipBoundaryCondition(SideType::PZ, 0.0, 0.0, 0.0);
-    gridBuilder->setSlipBoundaryCondition(SideType::MZ, 0.0, 0.0, 0.0);
-
-    // gridBuilder->setNoSlipBoundaryCondition(SideType::GEOMETRY); // not working yet, use veloBC
-    gridBuilder->setVelocityBoundaryCondition(SideType::GEOMETRY, 0.0, velocityLB * 2.0, 0.0);
-    gridBuilder->setPressureBoundaryCondition(SideType::PX, 0.0); // set pressure boundary condition last
-
-    bcFactory.setNoSlipBoundaryCondition(BoundaryConditionFactory::NoSlipBC::NoSlipCompressible);
-    bcFactory.setVelocityBoundaryCondition(BoundaryConditionFactory::VelocityBC::VelocityCompressible);
-    bcFactory.setSlipBoundaryCondition(BoundaryConditionFactory::SlipBC::SlipCompressible);
-    bcFactory.setPressureBoundaryCondition(BoundaryConditionFactory::PressureBC::PressureNonEquilibriumCompressible);
-    bcFactory.setGeometryBoundaryCondition(BoundaryConditionFactory::NoSlipBC::NoSlipCompressible);
-    // bcFactory.setGeometryBoundaryCondition(BoundaryConditionFactory::VelocityBC::VelocityCompressible);
-
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
-    // gridBuilder->writeGridsToVtk("grid/");
+    try {
+        //////////////////////////////////////////////////////////////////////////
+        // Simulation parameters
+        //////////////////////////////////////////////////////////////////////////
+
+        const bool useConfigFile = true;
+
+        const real L = 1.0;
+        const real dSphere = 0.2;
+        const real Re = 1000.0; // related to the sphere's diameter
+        const real velocity = 1.0;
+        const real dt = (real)0.5e-3;
+        const uint nx = 64;
+
+        const uint timeStepOut = 10000;
+        const uint timeStepEnd = 100000;
+
+        //////////////////////////////////////////////////////////////////////////
+        // setup logger
+        //////////////////////////////////////////////////////////////////////////
+
+        logging::Logger::addStream(&std::cout);
+        logging::Logger::setDebugLevel(logging::Logger::Level::INFO_LOW);
+        logging::Logger::timeStamp(logging::Logger::ENABLE);
+        logging::Logger::enablePrintedRankNumbers(logging::Logger::ENABLE);
+
+        //////////////////////////////////////////////////////////////////////////
+        // setup simulation parameters (with or without config file)
+        //////////////////////////////////////////////////////////////////////////
+
+        vf::gpu::Communicator& communicator = vf::gpu::Communicator::getInstance();;
+        SPtr<Parameter> para;
+        BoundaryConditionFactory bcFactory = BoundaryConditionFactory();
+        vf::basics::ConfigurationFile config;
+        if (useConfigFile) {
+            //////////////////////////////////////////////////////////////////////////
+            // read simulation parameters from config file
+            //////////////////////////////////////////////////////////////////////////
+
+            // assuming that a config files is stored parallel to this file.
+            std::filesystem::path configPath = __FILE__;
+
+            // the config file's default name can be replaced by passing a command line argument
+            std::string configName("config.txt");
+            if (argc == 2) {
+                configName = argv[1];
+                std::cout << "Using configFile command line argument: " << configName << std::endl;
+            }
+
+            configPath.replace_filename(configName);
+            config.load(configPath.string());
+
+            para = std::make_shared<Parameter>(config);
+        } else {
+            para = std::make_shared<Parameter>();
+        }
+
+        //////////////////////////////////////////////////////////////////////////
+        // setup gridGenerator
+        //////////////////////////////////////////////////////////////////////////
+
+        auto gridFactory = GridFactory::make();
+        gridFactory->setTriangularMeshDiscretizationMethod(TriangularMeshDiscretizationMethod::POINT_IN_OBJECT);
+        auto gridBuilder = MultipleGridBuilder::makeShared(gridFactory);
+
+        //////////////////////////////////////////////////////////////////////////
+        // create grid
+        //////////////////////////////////////////////////////////////////////////
+
+        real dx = L / real(nx);
+        gridBuilder->addCoarseGrid(-1.0 * L, -0.5 * L, -0.5 * L,
+                                      6.0 * L,    0.5 * L,    0.5 * L, dx);
+
+        // use primitive
+        // Object *sphere = new Sphere(0.0, 0.0, 0.0, dSphere / 2.0);
+
+        // use stl
+        std::string stlPath = "stl/sphere02.stl";
+        if (useConfigFile && config.contains("STLPath")) {
+            stlPath = config.getValue<std::string>("STLPath");
+        }
+        std::cout << "Reading stl from " << stlPath << "." << std::endl;
+        Object *sphere = TriangularMesh::make(stlPath);
+
+        gridBuilder->addGeometry(sphere);
+        gridBuilder->setPeriodicBoundaryCondition(false, false, false);
+        gridBuilder->buildGrids(LBM, false);  // buildGrids() has to be called before setting the BCs!!!!
+
+        //////////////////////////////////////////////////////////////////////////
+        // compute parameters in lattice units
+        //////////////////////////////////////////////////////////////////////////
+
+        const real velocityLB = velocity * dt / dx; // LB units
+        const real viscosityLB =  (dSphere / dx) * velocityLB / Re; // LB units
+
+        *logging::out << logging::Logger::INFO_HIGH << "velocity  [dx/dt] = " << velocityLB << " \n";
+        *logging::out << logging::Logger::INFO_HIGH << "viscosity [dx^2/dt] = " << viscosityLB << "\n";
+
+        //////////////////////////////////////////////////////////////////////////
+        // set parameters
+        //////////////////////////////////////////////////////////////////////////
+
+        para->setPrintFiles(true);
 
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+        para->setVelocityLB(velocityLB);
+        para->setViscosityLB(viscosityLB);
+
+        para->setVelocityRatio(velocity / velocityLB);
+        para->setDensityRatio((real)1.0);
+
+        para->setTimestepOut(timeStepOut);
+        para->setTimestepEnd(timeStepEnd);
 
-    auto cudaMemoryManager = std::make_shared<CudaMemoryManager>(para);
+        //////////////////////////////////////////////////////////////////////////
+        // set boundary conditions
+        //////////////////////////////////////////////////////////////////////////
 
-    auto gridGenerator = GridProvider::makeGridGenerator(gridBuilder, para, cudaMemoryManager, communicator);
+        gridBuilder->setVelocityBoundaryCondition(SideType::MX, velocityLB, 0.0, 0.0);
+
+        gridBuilder->setSlipBoundaryCondition(SideType::PY, 0.0, 0.0, 0.0);
+        gridBuilder->setSlipBoundaryCondition(SideType::MY, 0.0, 0.0, 0.0);
+        gridBuilder->setSlipBoundaryCondition(SideType::PZ, 0.0, 0.0, 0.0);
+        gridBuilder->setSlipBoundaryCondition(SideType::MZ, 0.0, 0.0, 0.0);
 
-    Simulation sim(para, cudaMemoryManager, communicator, *gridGenerator, &bcFactory);
-    sim.run();
+        gridBuilder->setVelocityBoundaryCondition(SideType::GEOMETRY, 0.0, 0.0, 0.0);
+        gridBuilder->setPressureBoundaryCondition(SideType::PX, 0.0); // set pressure boundary condition last
+
+        bcFactory.setVelocityBoundaryCondition(BoundaryConditionFactory::VelocityBC::VelocityCompressible);
+        bcFactory.setSlipBoundaryCondition(BoundaryConditionFactory::SlipBC::SlipCompressible);
+        bcFactory.setPressureBoundaryCondition(BoundaryConditionFactory::PressureBC::PressureNonEquilibriumCompressible);
+        bcFactory.setGeometryBoundaryCondition(BoundaryConditionFactory::NoSlipBC::NoSlipCompressible);
+        
+        //////////////////////////////////////////////////////////////////////////
+        // setup to copy mesh to simulation
+        //////////////////////////////////////////////////////////////////////////
 
-    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-}
+        auto cudaMemoryManager = std::make_shared<CudaMemoryManager>(para);
+        SPtr<GridProvider> gridGenerator = GridProvider::makeGridGenerator(gridBuilder, para, cudaMemoryManager, communicator);
 
-int main(int argc, char *argv[])
-{
-    try {
-        vf::logging::Logger::initalizeLogger();
+        //////////////////////////////////////////////////////////////////////////
+        // run simulation
+        //////////////////////////////////////////////////////////////////////////
 
-        // assuming that the config files is stored parallel to this file.
-        std::filesystem::path filePath = __FILE__;
-        filePath.replace_filename("config.txt");
+        Simulation sim(para, cudaMemoryManager, communicator, *gridGenerator, &bcFactory);
+        sim.run();
 
-        multipleLevel(filePath.string());
-    } catch (const spdlog::spdlog_ex &ex) {
-        std::cout << "Log initialization failed: " << ex.what() << std::endl;
     } catch (const std::bad_alloc &e) {
-        VF_LOG_CRITICAL("Bad Alloc: {}", e.what());
+        *logging::out << logging::Logger::LOGGER_ERROR << "Bad Alloc:" << e.what() << "\n";
     } catch (const std::exception &e) {
-        VF_LOG_CRITICAL("exception: {}", e.what());
+        *logging::out << logging::Logger::LOGGER_ERROR << e.what() << "\n";
+    } catch (std::string &s) {
+        *logging::out << logging::Logger::LOGGER_ERROR << s << "\n";
     } catch (...) {
-        VF_LOG_CRITICAL("Unknown exception!");
+        *logging::out << logging::Logger::LOGGER_ERROR << "Unknown exception!\n";
     }
 
     return 0;
-- 
GitLab