From 1cff1c289798fb4a9880db0082fda41df62f2ad4 Mon Sep 17 00:00:00 2001
From: schoen <schoen@irmb.tu-bs.de>
Date: Thu, 20 May 2021 23:38:11 +0200
Subject: [PATCH] add two testcases

---
 apps/gpu/Cases_1_3_5_7/CMakeLists.txt    |   3 +
 apps/gpu/Cases_1_3_5_7/Cases_1_3_5_7.cpp | 244 ++++++++++++++++++++++
 apps/gpu/Cases_2_4_6_8/CMakeLists.txt    |   3 +
 apps/gpu/Cases_2_4_6_8/Cases_2_4_6_8.cpp | 248 +++++++++++++++++++++++
 gpu.cmake                                |   2 +
 5 files changed, 500 insertions(+)
 create mode 100644 apps/gpu/Cases_1_3_5_7/CMakeLists.txt
 create mode 100644 apps/gpu/Cases_1_3_5_7/Cases_1_3_5_7.cpp
 create mode 100644 apps/gpu/Cases_2_4_6_8/CMakeLists.txt
 create mode 100644 apps/gpu/Cases_2_4_6_8/Cases_2_4_6_8.cpp

diff --git a/apps/gpu/Cases_1_3_5_7/CMakeLists.txt b/apps/gpu/Cases_1_3_5_7/CMakeLists.txt
new file mode 100644
index 000000000..b143c2d7a
--- /dev/null
+++ b/apps/gpu/Cases_1_3_5_7/CMakeLists.txt
@@ -0,0 +1,3 @@
+project(LidDrivenCavityGPU LANGUAGES CUDA CXX)
+
+vf_add_library(BUILDTYPE binary PRIVATE_LINK basics GridGenerator VirtualFluids_GPU GksMeshAdapter GksGpu FILES Cases_1_3_5_7.cpp)
diff --git a/apps/gpu/Cases_1_3_5_7/Cases_1_3_5_7.cpp b/apps/gpu/Cases_1_3_5_7/Cases_1_3_5_7.cpp
new file mode 100644
index 000000000..7278c4bdf
--- /dev/null
+++ b/apps/gpu/Cases_1_3_5_7/Cases_1_3_5_7.cpp
@@ -0,0 +1,244 @@
+//=======================================================================================
+// ____          ____    __    ______     __________   __      __       __        __         
+// \    \       |    |  |  |  |   _   \  |___    ___| |  |    |  |     /  \      |  |        
+//  \    \      |    |  |  |  |  |_)   |     |  |     |  |    |  |    /    \     |  |        
+//   \    \     |    |  |  |  |   _   /      |  |     |  |    |  |   /  /\  \    |  |        
+//    \    \    |    |  |  |  |  | \  \      |  |     |   \__/   |  /  ____  \   |  |____    
+//     \    \   |    |  |__|  |__|  \__\     |__|      \________/  /__/    \__\  |_______|   
+//      \    \  |    |   ________________________________________________________________    
+//       \    \ |    |  |  ______________________________________________________________|   
+//        \    \|    |  |  |         __          __     __     __     ______      _______    
+//         \         |  |  |_____   |  |        |  |   |  |   |  |   |   _  \    /  _____)   
+//          \        |  |   _____|  |  |        |  |   |  |   |  |   |  | \  \   \_______    
+//           \       |  |  |        |  |_____   |   \_/   |   |  |   |  |_/  /    _____  \   
+//            \ _____|  |__|        |________|   \_______/    |__|   |______/    (_______/   
+//
+//  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 Cases_1_3_5_7.cpp
+//! \ingroup Applications
+//! \author Martin Schoenherr, Stephan Lenz, Damilola Adekanye
+//=======================================================================================
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string>
+#include <sstream>
+#include <iostream>
+#include <stdexcept>
+#include <fstream>
+#include <exception>
+#include <memory>
+
+#include "PointerDefinitions.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+#include "Core/DataTypes.h"
+#include "Core/LbmOrGks.h"
+#include "Core/VectorTypes.h"
+#include "Core/Logger/Logger.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+#include "GridGenerator/grid/GridBuilder/LevelGridBuilder.h"
+#include "GridGenerator/grid/GridBuilder/MultipleGridBuilder.h"
+#include "GridGenerator/grid/BoundaryConditions/Side.h"
+#include "GridGenerator/grid/GridFactory.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+#include "VirtualFluids_GPU/LBM/Simulation.h"
+#include "VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/GridGenerator.h"
+#include "VirtualFluids_GPU/DataStructureInitializer/GridProvider.h"
+#include "VirtualFluids_GPU/Parameter/Parameter.h"
+#include "VirtualFluids_GPU/Output/FileWriter.h"
+#include "VirtualFluids_GPU/GPU/CudaMemoryManager.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+int main( int argc, char* argv[])
+{
+    srand (time(nullptr));
+    try
+    {
+        //////////////////////////////////////////////////////////////////////////
+        // Simulation parameters
+        //////////////////////////////////////////////////////////////////////////
+        std::string path("Insert Path to Results Directory Here");
+        std::string simulationName("Insert Simulation Name Here");
+
+        const real Re = 8950; //Set Reynolds number for given case
+        const real Sc = 1;    //Set Schmidt number
+        const real L  = 1;    //Set height of channel (H)
+        const uint nx = 100;  //Set the number of nodes discrtizing channel depth
+        const real Gate = L;  //Set distance to gate (x0)
+
+
+        // this testcase is implemented for LBM(GPU) only
+        LbmOrGks lbmOrGks = LBM;
+
+        //////////////////////////////////////////////////////////////////////////
+        // 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 gridGenerator
+        //////////////////////////////////////////////////////////////////////////
+
+        auto gridFactory = GridFactory::make();
+        gridFactory->setGridStrategy(Device::CPU);
+        auto gridBuilder = MultipleGridBuilder::makeShared(gridFactory);
+    
+        //////////////////////////////////////////////////////////////////////////
+        // create grid
+        //////////////////////////////////////////////////////////////////////////
+
+	    real dx = L / real(nx);
+
+	    gridBuilder->addCoarseGrid(-12.5 * L, 0 * L, 0 * L,
+	                               12.5 * L,  1.5 * L,  1 * L, dx);
+
+	    gridBuilder->setPeriodicBoundaryCondition(true, true, false);
+
+	    gridBuilder->buildGrids(lbmOrGks, false);
+
+		//////////////////////////////////////////////////////////////////////////
+        // branch between LBM and GKS
+        //////////////////////////////////////////////////////////////////////////
+
+        if( lbmOrGks == LBM )
+        {
+			SPtr<Parameter> para = Parameter::make();
+
+			real DeltaT = 2.5*(real)nx/para->getVelocityLB();
+
+            const uint timeStepOut = (int)DeltaT ;
+            const uint timeStepEnd = (int)DeltaT*12;
+            std::cout << "DeltaT=" << timeStepOut <<"\n";
+			//////////////////////////////////////////////////////////////////////////
+            // compute parameters in lattice units
+            //////////////////////////////////////////////////////////////////////////
+            const real velocityLB = para->getVelocityLB(); //Using default velocity in Parameter constructor (LB units)
+            const real dt = velocityLB/nx;
+            const real velocity = velocityLB * dx / dt;
+
+            const real viscosityLB = nx * velocityLB / Re; // LB units
+
+            const real g_r = (velocityLB*velocityLB)/nx; // 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->setOutputPath( path );
+            para->setOutputPrefix( simulationName );
+
+            para->setPathAndFilename(para->getOutputPath() + "/" + para->getOutputPrefix());
+
+            para->setPrintFiles(true);
+
+            para->setVelocityLB(velocityLB);
+            para->setViscosityLB(viscosityLB);
+
+            para->setVelocityRatio(velocity / velocityLB);
+
+            para->setTimestepOut( timeStepOut );
+            para->setTimestepEnd( timeStepEnd );
+
+			para->setIsADcalculationOn(true);
+            para->setIsBodyForceOn(true);
+
+            para->setSc( (real)Sc );
+            para->setNx( (real)nx );
+            para->setG_r( (real)g_r);
+			//////////////////////////////////////////////////////////////////////////
+			// set initial conditions
+			//////////////////////////////////////////////////////////////////////////
+			para->setInitialCondition([&](real coordX, real coordY, real coordZ, real& rho, real& vx, real& vy, real& vz) {
+				rho = (real)0.0;
+				vx  = (real)0.0;
+				vy  = (real)0.0;
+				vz  = (real)0.0;
+			});
+
+			// set concentration
+			if (para->getIsADcalculationOn())
+			{
+				para->setInitialConditionAD([&](real coordX, real coordY, real coordZ, real& concentration, real dist) {
+				    if (sqrt(coordX*coordX)<Gate + dist){
+                        concentration = (real)1.0;
+                    }
+				    else {
+				        concentration = (real)0.0;
+				    }
+				});
+			}
+
+			//////////////////////////////////////////////////////////////////////////
+            // set boundary conditions
+            //////////////////////////////////////////////////////////////////////////
+
+            gridBuilder->setNoSlipBoundaryCondition  (SideType::PZ);
+		    gridBuilder->setNoSlipBoundaryCondition  (SideType::MZ);
+    
+            //////////////////////////////////////////////////////////////////////////
+            // set copy mesh to simulation
+            //////////////////////////////////////////////////////////////////////////
+
+		    SPtr<CudaMemoryManager> cudaMemoryManager = CudaMemoryManager::make(para);
+
+            SPtr<GridProvider> gridGenerator = GridProvider::makeGridGenerator(gridBuilder, para, cudaMemoryManager);
+    
+            //////////////////////////////////////////////////////////////////////////
+            // run simulation
+            //////////////////////////////////////////////////////////////////////////
+
+            Simulation sim;
+            SPtr<FileWriter> fileWriter = SPtr<FileWriter>(new FileWriter());
+            sim.init(para, gridGenerator, fileWriter, cudaMemoryManager);
+            sim.run();
+            sim.free();
+        }
+
+	}
+    catch (const std::bad_alloc e)
+    {
+                
+        *logging::out << logging::Logger::LOGGER_ERROR << "Bad Alloc:" << e.what() << "\n";
+    }
+    catch (const std::exception& e)
+    {
+                
+        *logging::out << logging::Logger::LOGGER_ERROR << e.what() << "\n";
+    }
+    catch (std::string& s)
+    {
+                
+        *logging::out << logging::Logger::LOGGER_ERROR << s << "\n";
+    }
+    catch (...)
+    {
+        *logging::out << logging::Logger::LOGGER_ERROR << "Unknown exception!\n";
+    }
+
+   return 0;
+}
diff --git a/apps/gpu/Cases_2_4_6_8/CMakeLists.txt b/apps/gpu/Cases_2_4_6_8/CMakeLists.txt
new file mode 100644
index 000000000..bb4234a90
--- /dev/null
+++ b/apps/gpu/Cases_2_4_6_8/CMakeLists.txt
@@ -0,0 +1,3 @@
+project(LidDrivenCavityGPU LANGUAGES CUDA CXX)
+
+vf_add_library(BUILDTYPE binary PRIVATE_LINK basics GridGenerator VirtualFluids_GPU GksMeshAdapter GksGpu FILES Cases_2_4_6_8.cpp)
diff --git a/apps/gpu/Cases_2_4_6_8/Cases_2_4_6_8.cpp b/apps/gpu/Cases_2_4_6_8/Cases_2_4_6_8.cpp
new file mode 100644
index 000000000..b7f674543
--- /dev/null
+++ b/apps/gpu/Cases_2_4_6_8/Cases_2_4_6_8.cpp
@@ -0,0 +1,248 @@
+//=======================================================================================
+// ____          ____    __    ______     __________   __      __       __        __         
+// \    \       |    |  |  |  |   _   \  |___    ___| |  |    |  |     /  \      |  |        
+//  \    \      |    |  |  |  |  |_)   |     |  |     |  |    |  |    /    \     |  |        
+//   \    \     |    |  |  |  |   _   /      |  |     |  |    |  |   /  /\  \    |  |        
+//    \    \    |    |  |  |  |  | \  \      |  |     |   \__/   |  /  ____  \   |  |____    
+//     \    \   |    |  |__|  |__|  \__\     |__|      \________/  /__/    \__\  |_______|   
+//      \    \  |    |   ________________________________________________________________    
+//       \    \ |    |  |  ______________________________________________________________|   
+//        \    \|    |  |  |         __          __     __     __     ______      _______    
+//         \         |  |  |_____   |  |        |  |   |  |   |  |   |   _  \    /  _____)   
+//          \        |  |   _____|  |  |        |  |   |  |   |  |   |  | \  \   \_______    
+//           \       |  |  |        |  |_____   |   \_/   |   |  |   |  |_/  /    _____  \   
+//            \ _____|  |__|        |________|   \_______/    |__|   |______/    (_______/   
+//
+//  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 Cases_2_4_6_8.cpp
+//! \ingroup Applications
+//! \author Martin Schoenherr, Stephan Lenz, Damilola Adekanye
+//=======================================================================================
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <string>
+#include <sstream>
+#include <iostream>
+#include <stdexcept>
+#include <fstream>
+#include <exception>
+#include <memory>
+
+#include "PointerDefinitions.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+#include "Core/DataTypes.h"
+#include "Core/LbmOrGks.h"
+#include "Core/VectorTypes.h"
+#include "Core/Logger/Logger.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+#include "GridGenerator/grid/GridBuilder/LevelGridBuilder.h"
+#include "GridGenerator/grid/GridBuilder/MultipleGridBuilder.h"
+#include "GridGenerator/grid/BoundaryConditions/Side.h"
+#include "GridGenerator/grid/GridFactory.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+#include "VirtualFluids_GPU/LBM/Simulation.h"
+#include "VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/GridGenerator.h"
+#include "VirtualFluids_GPU/DataStructureInitializer/GridProvider.h"
+#include "VirtualFluids_GPU/Parameter/Parameter.h"
+#include "VirtualFluids_GPU/Output/FileWriter.h"
+#include "VirtualFluids_GPU/GPU/CudaMemoryManager.h"
+
+//////////////////////////////////////////////////////////////////////////
+
+int main( int argc, char* argv[])
+{
+    srand (time(nullptr));
+    try
+    {
+        //////////////////////////////////////////////////////////////////////////
+        // Simulation parameters
+        //////////////////////////////////////////////////////////////////////////
+        std::string path("Insert Path to Results Directory Here");
+        std::string simulationName("Insert Simulation Name Here");
+
+        const real Re = 1000; //Set Reynolds number for given case
+        const real Sc = 1;    //Set Schmidt number
+        const real L  = 1;    //Set height of channel (H)
+        const uint nx = 100;  //Set the number of nodes discrtizing channel depth
+        const real Gate = L;  //Set distance to gate (x0)
+
+
+        // this testcase is implemented for LBM-GPU only
+        LbmOrGks lbmOrGks = LBM;
+
+        //////////////////////////////////////////////////////////////////////////
+        // 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 gridGenerator
+        //////////////////////////////////////////////////////////////////////////
+
+        auto gridFactory = GridFactory::make();
+        gridFactory->setGridStrategy(Device::CPU);
+        auto gridBuilder = MultipleGridBuilder::makeShared(gridFactory);
+    
+        //////////////////////////////////////////////////////////////////////////
+        // create grid
+        //////////////////////////////////////////////////////////////////////////
+
+	    real dx = L / real(nx);
+
+	    gridBuilder->addCoarseGrid(0 * L, 0 * L, 0 * L,
+	                               15 * L,  1 * L,  1 * L, dx);
+
+	    gridBuilder->setPeriodicBoundaryCondition(false, false, false);
+
+	    gridBuilder->buildGrids(lbmOrGks, false);
+
+		//////////////////////////////////////////////////////////////////////////
+        // branch between LBM and GKS
+        //////////////////////////////////////////////////////////////////////////
+
+        if( lbmOrGks == LBM )
+        {
+			SPtr<Parameter> para = Parameter::make();
+
+			real DeltaT = 0.5*(real)nx/para->getVelocityLB();
+
+            const uint timeStepOut = (int)DeltaT ;
+            const uint timeStepEnd = (int)DeltaT*12;
+            std::cout << "DeltaT=" << timeStepOut <<"\n";
+			//////////////////////////////////////////////////////////////////////////
+            // compute parameters in lattice units
+            //////////////////////////////////////////////////////////////////////////
+            const real velocityLB = para->getVelocityLB(); //Using default velocity in Parameter constructor (LB units)
+            const real dt = velocityLB/nx;
+            const real velocity = velocityLB * dx / dt;
+
+            const real viscosityLB = nx * velocityLB / Re; // LB units
+
+            const real g_r = (velocityLB*velocityLB)/nx; // 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->setOutputPath( path );
+            para->setOutputPrefix( simulationName );
+
+            para->setPathAndFilename(para->getOutputPath() + "/" + para->getOutputPrefix());
+
+            para->setPrintFiles(true);
+
+            para->setVelocityLB(velocityLB);
+            para->setViscosityLB(viscosityLB);
+
+            para->setVelocityRatio(velocity / velocityLB);
+
+            para->setTimestepOut( timeStepOut );
+            para->setTimestepEnd( timeStepEnd );
+
+			para->setIsADcalculationOn(true);
+            para->setIsBodyForceOn(true);
+
+            para->setSc( (real)Sc );
+            para->setNx( (real)nx );
+            para->setG_r( (real)g_r);
+			//////////////////////////////////////////////////////////////////////////
+			// set initial conditions
+			//////////////////////////////////////////////////////////////////////////
+			para->setInitialCondition([&](real coordX, real coordY, real coordZ, real& rho, real& vx, real& vy, real& vz) {
+				rho = (real)0.0;
+				vx  = (real)0.0;
+				vy  = (real)0.0;
+				vz  = (real)0.0;
+			});
+
+			// set concentration
+			if (para->getIsADcalculationOn())
+			{
+				para->setInitialConditionAD([&](real coordX, real coordY, real coordZ, real& concentration, real dist) {
+				    if (sqrt(coordX*coordX)<Gate + dist){
+                        concentration = (real)1.0;
+                    }
+				    else {
+				        concentration = (real)0.0;
+				    }
+				});
+			}
+
+			//////////////////////////////////////////////////////////////////////////
+            // set boundary conditions
+            //////////////////////////////////////////////////////////////////////////
+
+            gridBuilder->setNoSlipBoundaryCondition  (SideType::PX);
+            gridBuilder->setNoSlipBoundaryCondition  (SideType::MX);
+            gridBuilder->setNoSlipBoundaryCondition  (SideType::PY);
+            gridBuilder->setNoSlipBoundaryCondition  (SideType::MY);
+            gridBuilder->setNoSlipBoundaryCondition  (SideType::PZ);
+		    gridBuilder->setNoSlipBoundaryCondition  (SideType::MZ);
+    
+            //////////////////////////////////////////////////////////////////////////
+            // set copy mesh to simulation
+            //////////////////////////////////////////////////////////////////////////
+
+		    SPtr<CudaMemoryManager> cudaMemoryManager = CudaMemoryManager::make(para);
+
+            SPtr<GridProvider> gridGenerator = GridProvider::makeGridGenerator(gridBuilder, para, cudaMemoryManager);
+    
+            //////////////////////////////////////////////////////////////////////////
+            // run simulation
+            //////////////////////////////////////////////////////////////////////////
+
+            Simulation sim;
+            SPtr<FileWriter> fileWriter = SPtr<FileWriter>(new FileWriter());
+            sim.init(para, gridGenerator, fileWriter, cudaMemoryManager);
+            sim.run();
+            sim.free();
+        }
+
+	}
+    catch (const std::bad_alloc e)
+    {
+                
+        *logging::out << logging::Logger::LOGGER_ERROR << "Bad Alloc:" << e.what() << "\n";
+    }
+    catch (const std::exception& e)
+    {
+                
+        *logging::out << logging::Logger::LOGGER_ERROR << e.what() << "\n";
+    }
+    catch (std::string& s)
+    {
+                
+        *logging::out << logging::Logger::LOGGER_ERROR << s << "\n";
+    }
+    catch (...)
+    {
+        *logging::out << logging::Logger::LOGGER_ERROR << "Unknown exception!\n";
+    }
+
+   return 0;
+}
diff --git a/gpu.cmake b/gpu.cmake
index ff11b0a60..f4f74ea74 100644
--- a/gpu.cmake
+++ b/gpu.cmake
@@ -16,3 +16,5 @@ add_subdirectory(src/gpu/GksMeshAdapter)
 add_subdirectory(src/gpu/GksGpu)
 
 add_subdirectory(apps/gpu/LidDrivenCavityGPU)
+add_subdirectory(apps/gpu/Cases_1_3_5_7)
+add_subdirectory(apps/gpu/Cases_2_4_6_8)
-- 
GitLab