From 8e30a4dc79347492f4e1c812345b686d88c92bb8 Mon Sep 17 00:00:00 2001
From: Anna <a.wellmann@tu-bs.de>
Date: Tue, 28 Jun 2022 10:39:52 +0000
Subject: [PATCH] Add ability to read config files

---
 .../gpu/FlowAroundSphere/FlowAroundSphere.cpp | 45 +++++++++++++------
 apps/gpu/FlowAroundSphere/config.txt          | 11 +++++
 .../VirtualFluids_GPU/Parameter/Parameter.cpp | 44 +++++++++++++++++-
 .../VirtualFluids_GPU/Parameter/Parameter.h   | 10 ++++-
 4 files changed, 93 insertions(+), 17 deletions(-)
 create mode 100644 apps/gpu/FlowAroundSphere/config.txt

diff --git a/apps/gpu/FlowAroundSphere/FlowAroundSphere.cpp b/apps/gpu/FlowAroundSphere/FlowAroundSphere.cpp
index 4cc501260..cdc1c07be 100644
--- a/apps/gpu/FlowAroundSphere/FlowAroundSphere.cpp
+++ b/apps/gpu/FlowAroundSphere/FlowAroundSphere.cpp
@@ -32,6 +32,7 @@
 //=======================================================================================
 #define _USE_MATH_DEFINES
 #include <exception>
+#include <filesystem>
 #include <fstream>
 #include <iostream>
 #include <math.h>
@@ -46,8 +47,8 @@
 #include "Core/LbmOrGks.h"
 #include "Core/Logger/Logger.h"
 #include "Core/VectorTypes.h"
-#include "config/ConfigurationFile.h"
 #include "PointerDefinitions.h"
+#include "config/ConfigurationFile.h"
 
 //////////////////////////////////////////////////////////////////////////
 
@@ -75,8 +76,7 @@ int main(int argc, char *argv[])
         //////////////////////////////////////////////////////////////////////////
         // Simulation parameters
         //////////////////////////////////////////////////////////////////////////
-        std::string outputPath("./output/Sphere");
-        std::string simulationName("Sphere");
+        const bool useConfigFile = true;
 
         const real L = 1.0;
         const real Re = 1000.0;
@@ -95,6 +95,34 @@ int main(int argc, char *argv[])
         logging::Logger::setDebugLevel(logging::Logger::Level::INFO_LOW);
         logging::Logger::timeStamp(logging::Logger::ENABLE);
         logging::Logger::enablePrintedRankNumbers(logging::Logger::ENABLE);
+        
+        //////////////////////////////////////////////////////////////////////////
+        // setup simulation parameters
+        //////////////////////////////////////////////////////////////////////////
+        SPtr<Parameter> para;
+        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);
+            vf::basics::ConfigurationFile config;
+            config.load(configPath);
+
+            para = std::make_shared<Parameter>(config);
+        } else {
+            para = std::make_shared<Parameter>();
+        }
 
         //////////////////////////////////////////////////////////////////////////
         // setup gridGenerator
@@ -120,12 +148,6 @@ int main(int argc, char *argv[])
 
         gridBuilder->buildGrids(LBM, false);
 
-        //////////////////////////////////////////////////////////////////////////
-        // setup simulation parameters
-        //////////////////////////////////////////////////////////////////////////
-
-        SPtr<Parameter> para =std::make_shared<Parameter>();
-        
         //////////////////////////////////////////////////////////////////////////
         // compute parameters in lattice units
         //////////////////////////////////////////////////////////////////////////
@@ -143,11 +165,6 @@ int main(int argc, char *argv[])
         // set parameters
         //////////////////////////////////////////////////////////////////////////
 
-        para->setOutputPath(outputPath);
-        para->setOutputPrefix(simulationName);
-
-        para->setPathAndFilename(para->getOutputPath() + "/" + para->getOutputPrefix());
-
         para->setPrintFiles(true);
 
         para->setVelocityLB(velocityLB);
diff --git a/apps/gpu/FlowAroundSphere/config.txt b/apps/gpu/FlowAroundSphere/config.txt
new file mode 100644
index 000000000..c70c0813f
--- /dev/null
+++ b/apps/gpu/FlowAroundSphere/config.txt
@@ -0,0 +1,11 @@
+##################################################
+# informations for Writing
+##################################################
+OutputPath = output/Sphere/
+OutputPrefix = Sphere
+
+##################################################
+# simulation time
+##################################################
+# TimeEnd=100000
+# TimeOut=1000
\ No newline at end of file
diff --git a/src/gpu/VirtualFluids_GPU/Parameter/Parameter.cpp b/src/gpu/VirtualFluids_GPU/Parameter/Parameter.cpp
index 1e79d5cf5..8aeaea979 100644
--- a/src/gpu/VirtualFluids_GPU/Parameter/Parameter.cpp
+++ b/src/gpu/VirtualFluids_GPU/Parameter/Parameter.cpp
@@ -35,10 +35,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include "Core/Logger/Logger.h"
 
 Parameter::Parameter()
 {
-    this->setOutputPath("C:/Output/");
+    this->setOutputPath("output/");
 
     this->setOutputPrefix("MyFile");
 
@@ -68,7 +69,7 @@ Parameter::Parameter()
 
     this->setPressureRatio((real)1.0);
 
-    this->setPathAndFilename(this->getOutputPath() + "/" + this->getOutputPrefix());
+    this->setPathAndFilename(this->getOutputPath() + this->getOutputPrefix());
 
     this->setlimitOfNodesForVTK((uint)30000000);
 
@@ -77,6 +78,41 @@ Parameter::Parameter()
     this->setIsADcalculationOn(false);
 }
 
+Parameter::Parameter(const vf::basics::ConfigurationFile &configData) : Parameter()
+{
+    readConfigData(configData);
+}
+
+void Parameter::readConfigData(const vf::basics::ConfigurationFile &configData){
+
+    if (configData.contains("OutputPath"))
+        this->setOutputPath(configData.getValue<std::string>("OutputPath"));
+
+    // deprecated, this is only in here for backward compatibility
+    if (configData.contains("Path")){
+        *logging::out << logging::Logger::INFO_INTERMEDIATE << "The config option \"Path\" is deprecated, please use \"OutputPath\" instead." << "\n";
+        this->setOutputPath(configData.getValue<std::string>("Path"));
+    }
+
+    if (configData.contains("OutputPrefix"))
+        this->setOutputPrefix(configData.getValue<std::string>("OutputPrefix"));
+
+    // deprecated, this is only in here for backward compatibility
+    if (configData.contains("Prefix")){
+        *logging::out << logging::Logger::INFO_INTERMEDIATE << "The config option \"Prefix\" is deprecated, please use \"OutputPrefix\" instead." << "\n";
+        this->setOutputPath(configData.getValue<std::string>("Prefix"));
+    }
+
+    // overwrite PathAndFilename when OutputPath or OutputPrefix are set in the config file
+    this->setPathAndFilename(this->getOutputPath() + this->getOutputPrefix());
+
+    if (configData.contains("TimeEnd"))
+        this->setTimestepEnd(configData.getValue<int>("TimeEnd"));
+
+    if (configData.contains("TimeOut"))
+        this->setTimestepOut(configData.getValue<int>("TimeOut"));
+}
+
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //init-method
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -125,6 +161,10 @@ void Parameter::setTimestepStartOut(uint timestepStartOut)
 }
 void Parameter::setOutputPath(std::string outputPath)
 {
+    // add missing slash to outputPath
+    if (outputPath.back() != '/')
+        outputPath += "/";
+
     this->outputPath = outputPath;
 }
 void Parameter::setOutputPrefix(std::string outputPrefix)
diff --git a/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h b/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h
index 776ba8382..908099dc7 100644
--- a/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h
+++ b/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h
@@ -114,6 +114,9 @@ public:
     ////////////////////////////////////////////////////////////////////////////
     //! \brief generate a new instance of parameter object, sets some defaults
     Parameter();
+    //! \brief generate a new instance of parameter object using a config file 
+    //! \details calls Parameter(): Variables which are not set in the config files use the defaults
+    Parameter(const vf::basics::ConfigurationFile &configData);
     
     ~Parameter() = default;
 
@@ -151,8 +154,10 @@ public:
     //! \param string "oPrefix" represents the file-name-prefix
     void setOutputPrefix(std::string outputPrefix);
     //! \brief sets the complete string for the vtk-files with results 
-    //! \param string "fname" represents the combination of path and prefix
+    //! \param string "pathAndFilename" represents the combination of path and prefix
     void setPathAndFilename(std::string pathAndFilename);
+    //! \brief sets the path for reading and writing the grid
+    void setGridPath(std::string gridPath);
     //! \brief sets the status, if Advection Diffusion should be calculated
     //! \param if calcAD is true, the Advection Diffusion will be calculated
     void setIsADcalculationOn(bool calcAD);
@@ -257,6 +262,9 @@ public:
     void setInitialConditionAD(std::function<void(real, real, real, real &)> initialConditionAD);
     std::function<void(real, real, real, real &)> &getInitialConditionAD();
 
+private:
+    //! read configuration data from a config file
+    void readConfigData(const vf::basics::ConfigurationFile &configData);
 private:
     //! \brief stencil for the LB simulation, number of node neighbors
     int D3Qxx;
-- 
GitLab