From 3850702ca28b06fb21cbb99c55dc3dcb3360c236 Mon Sep 17 00:00:00 2001
From: "LEGOLAS\\lenz" <lenz@irmb.tu-bs.de>
Date: Thu, 5 Dec 2019 11:02:30 +0100
Subject: [PATCH] fixes a bug with WALE and implements infrastructure for
 Analyzers

---
 .../GridReaderGenerator/GridGenerator.cpp     |  3 +
 src/VirtualFluids_GPU/LBM/Simulation.cpp      | 81 +++++++++++++------
 src/VirtualFluids_GPU/LBM/Simulation.h        | 13 +++
 targets/apps/LBM/TGV_3D/TGV_3D.cpp            | 20 +++--
 4 files changed, 87 insertions(+), 30 deletions(-)

diff --git a/src/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/GridGenerator.cpp b/src/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/GridGenerator.cpp
index 19a71dfac..57c641217 100644
--- a/src/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/GridGenerator.cpp
+++ b/src/VirtualFluids_GPU/DataStructureInitializer/GridReaderGenerator/GridGenerator.cpp
@@ -60,6 +60,9 @@ void GridGenerator::allocArrays_CoordNeighborGeo()
 		para->cudaAllocF3SP(level);
 		cudaMemoryManager->cudaAllocNeighborWSB(level);
 
+        if(para->getUseWale())
+            cudaMemoryManager->cudaAllocTurbulentViscosity(level);
+
 		builder->getNodeValues(
 			para->getParH(level)->coordX_SP,
 			para->getParH(level)->coordY_SP,
diff --git a/src/VirtualFluids_GPU/LBM/Simulation.cpp b/src/VirtualFluids_GPU/LBM/Simulation.cpp
index 8bae869bf..c5528a14b 100644
--- a/src/VirtualFluids_GPU/LBM/Simulation.cpp
+++ b/src/VirtualFluids_GPU/LBM/Simulation.cpp
@@ -16,6 +16,7 @@
 #include "Output/MeasurePointWriter.hpp"
 #include "Output/AnalysisData.hpp"
 #include "Output/InterfaceDebugWriter.hpp"
+#include "Output/VeloASCIIWriter.hpp"
 //////////////////////////////////////////////////////////////////////////
 #include "Utilities/Buffer2D.hpp"
 #include "Utilities/StringUtil.hpp"
@@ -75,6 +76,16 @@ void Simulation::setFactories(std::shared_ptr<KernelFactory> kernelFactory, std:
 	this->preProcessorFactory = preProcessorFactory;
 }
 
+void Simulation::addKineticEnergyAnalyzer(uint tAnalyse)
+{
+    this->kineticEnergyAnalyzer = make_shared<KineticEnergyAnalyzer>(this->para, tAnalyse);
+}
+
+void Simulation::addEnstrophyAnalyzer(uint tAnalyse)
+{
+    this->enstrophyAnalyzer = make_shared<EnstrophyAnalyzer>(this->para, tAnalyse);
+}
+
 
 void Simulation::init(SPtr<Parameter> para, SPtr<GridProvider> gridProvider, std::shared_ptr<DataWriter> dataWriter, std::shared_ptr<CudaMemoryManager> cudaManager)
 {
@@ -186,10 +197,6 @@ void Simulation::init(SPtr<Parameter> para, SPtr<GridProvider> gridProvider, std
    }
    ////////////////////////////////////////////////////////////////////////////
 
-
-
-
-
    //////////////////////////////////////////////////////////////////////////
    //Allocate Memory for Drag Lift Calculation
    //////////////////////////////////////////////////////////////////////////
@@ -430,12 +437,29 @@ void Simulation::run()
 	{
 		getLastCudaError("before starting a kernel we get an execution failed");
 		if (para->getMaxLevel()>=1)
-         {
+        {
             updateGrid27(para.get(), comm, cudaManager.get(), pm, 1, para->getMaxLevel(), t, kernels);
-         }
-         ////////////////////////////////////////////////////////////////////////////////
-         // Collision and Propagation
-         ////////////////////////////////////////////////////////////////////////////////      
+        }
+
+        ////////////////////////////////////////////////////////////////////////////////
+        // Collision and Propagation
+        //////////////////////////////////////////////////////////////////////////////// 
+
+		//////////////////////////////////////////////////////////////////////////
+		//////////////////////////////////////////////////////////////////////////
+		//////////////////////////////////////////////////////////////////////////
+		//////////////////////////////////////////////////////////////////////////
+		
+		kernels.at(0)->run();
+		 
+		//////////////////////////////////////////////////////////////////////////
+		//////////////////////////////////////////////////////////////////////////
+		//////////////////////////////////////////////////////////////////////////
+		//////////////////////////////////////////////////////////////////////////
+
+
+
+     
 		 //if (t>para->getStartTurn()){
 			// //////////////////////////////////////////////////////////////////////////
 			// QVelDevice1h27(    para->getParD(0)->numberofthreads, para->getParD(0)->nx,           para->getParD(0)->ny,
@@ -468,20 +492,6 @@ void Simulation::run()
 		
 		 //////////////////////////////////////////////////////////////////////////
 		 //comp
-		 
-		 //////////////////////////////////////////////////////////////////////////
-
-		//////////////////////////////////////////////////////////////////////////
-		//Wale 
-		if (para->getUseWale())
-		{
-
-		} 
-		else
-		{
-			kernels.at(0)->run();
-
-		}
 
 
 		//CumulantOneChimCompSP27(
@@ -1172,6 +1182,9 @@ void Simulation::run()
             getLastCudaError("CalcMacSP27 execution failed"); 
 
 		} 
+
+
+
 		  //////////////////////////////////////////////////////////////////////////////////
 		  ////calculate the new forcing
 		  //if (((t%10) == 0) && (t >= 10)/*((t%para->getTStartOut()) == 0) && (t >= para->getTStartOut())*/)
@@ -1709,6 +1722,16 @@ void Simulation::run()
 
 
 
+      //////////////////////////////////////////////////////////////////////////
+      // run Analyzers for kinetic energy and enstrophy for TGV in 3D
+	  ////////////////////////////////////////////////////////////////////////////////
+	  if( this->kineticEnergyAnalyzer ) this->kineticEnergyAnalyzer->run(t);
+	  if( this->enstrophyAnalyzer     ) this->enstrophyAnalyzer->run(t);
+	  ////////////////////////////////////////////////////////////////////////////////
+
+
+
+
 	  ////////////////////////////////////////////////////////////////////////////////
 	  //Calc Median
 	  ////////////////////////////////////////////////////////////////////////////////
@@ -2068,6 +2091,18 @@ void Simulation::run()
 				   cudaManager->cudaCopyMedianPrint(lev);
 			   }
 
+			   //////////////////////////////////////////////////////////////////////////
+			   VeloASCIIWriter::writeVelocitiesAsTXT(para.get(), lev, t);
+			   //////////////////////////////////////////////////////////////////////////
+               if( this->kineticEnergyAnalyzer || this->enstrophyAnalyzer )
+               {
+                   std::string fname = para->getFName() + StringUtil::toString<int>(t) + "_t_";
+
+                   if (this->kineticEnergyAnalyzer) this->kineticEnergyAnalyzer->writeToFile(fname);
+                   if (this->enstrophyAnalyzer) this->enstrophyAnalyzer->writeToFile(fname);
+               }
+			   //////////////////////////////////////////////////////////////////////////
+
 
 			   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                if (para->getDiffOn()==true)
diff --git a/src/VirtualFluids_GPU/LBM/Simulation.h b/src/VirtualFluids_GPU/LBM/Simulation.h
index 34996ced2..8de2cfae4 100644
--- a/src/VirtualFluids_GPU/LBM/Simulation.h
+++ b/src/VirtualFluids_GPU/LBM/Simulation.h
@@ -8,6 +8,8 @@
 #include <VirtualFluidsDefinitions.h>
 
 #include "Output/LogWriter.hpp"
+#include "GPU/KineticEnergyAnalyzer.h"
+#include "GPU/EnstrophyAnalyzer.h"
 #include "Utilities/Buffer2D.hpp"
 #include "LBM/LB.h"
 
@@ -41,6 +43,9 @@ public:
 
 	void setFactories(std::shared_ptr<KernelFactory> kernelFactory, std::shared_ptr<PreProcessorFactory> preProcessorFactory);
 
+    void addKineticEnergyAnalyzer( uint tAnalyse );
+    void addEnstrophyAnalyzer    ( uint tAnalyse );
+
 protected:
 	std::shared_ptr<KernelFactory> kernelFactory;
 	std::shared_ptr<PreProcessorFactory> preProcessorFactory;
@@ -94,5 +99,13 @@ protected:
 	real *VxED,          *VyED,       *VzED,       *deltaVED;
 	real *VxWH,          *VyWH,       *VzWH,       *deltaVWH;
 	real *VxWD,          *VyWD,       *VzWD,       *deltaVWD;
+
+
+	////////////////////////////////////////////////////////////////////////////
+	SPtr<KineticEnergyAnalyzer> kineticEnergyAnalyzer;
+	////////////////////////////////////////////////////////////////////////////
+	SPtr<EnstrophyAnalyzer> enstrophyAnalyzer;
+	////////////////////////////////////////////////////////////////////////////
+
  };
 #endif
diff --git a/targets/apps/LBM/TGV_3D/TGV_3D.cpp b/targets/apps/LBM/TGV_3D/TGV_3D.cpp
index 5e87c4958..fac2cc777 100644
--- a/targets/apps/LBM/TGV_3D/TGV_3D.cpp
+++ b/targets/apps/LBM/TGV_3D/TGV_3D.cpp
@@ -91,6 +91,7 @@ uint nx = 64;
 uint gpuIndex = 0;
 
 bool useLimiter = false;
+bool useWale = false;
 
 std::string kernel( "CumulantK17Comp" );
 
@@ -241,6 +242,9 @@ void multipleLevel(const std::string& configPath)
     if( !useLimiter )
         para->setQuadricLimiters( 1000000.0, 1000000.0, 1000000.0 );
 
+    if( useWale )
+        para->setUseWale( true );
+
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -256,6 +260,9 @@ void multipleLevel(const std::string& configPath)
     sim.setFactories(kernelFactory, preProcessorFactory);
     sim.init(para, gridGenerator, fileWriter, cudaMemoryManager);
     
+    sim.addKineticEnergyAnalyzer( 10 );
+    sim.addEnstrophyAnalyzer( 10 );
+
     sim.run();
     sim.free();
 }
@@ -272,13 +279,7 @@ int main( int argc, char* argv[])
         try
         {
             //////////////////////////////////////////////////////////////////////////
-
-            //if( argc > 1 ) gpuIndex = atoi( argv[1] );
-
-            //if( argc > 2 ) nx = atoi( argv[2] );
-			std::string targetPath;
-
-			targetPath = __FILE__;
+			std::string targetPath( __FILE__ );
 
 #ifdef _WIN32
 			targetPath = targetPath.substr(0, targetPath.find_last_of('\\') + 1);
@@ -286,6 +287,8 @@ int main( int argc, char* argv[])
 			targetPath = targetPath.substr(0, targetPath.find_last_of('/') + 1);
 #endif
 
+            //////////////////////////////////////////////////////////////////////////
+
             if( cmdOptionExists( argv, argv+argc, "--Re" ) )
                 Re = atof( getCmdOption( argv, argv+argc, "--Re" ) );
 
@@ -304,6 +307,9 @@ int main( int argc, char* argv[])
             if( cmdOptionExists( argv, argv+argc, "--useLimiter" ) )
                 useLimiter = true;
 
+            if( cmdOptionExists( argv, argv+argc, "--useWale" ) )
+                useWale = true;
+
 			multipleLevel(targetPath + "config.txt");
 
             //////////////////////////////////////////////////////////////////////////
-- 
GitLab