diff --git a/apps/gpu/LBM/DrivenCavityMultiGPU/DrivenCavityMultiGPU.cpp b/apps/gpu/LBM/DrivenCavityMultiGPU/DrivenCavityMultiGPU.cpp index 4883cbcaae1bf6556d07f94ea87e7201aaad9feb..acab426b4868cc736710c883776c5626ec6b5753 100755 --- a/apps/gpu/LBM/DrivenCavityMultiGPU/DrivenCavityMultiGPU.cpp +++ b/apps/gpu/LBM/DrivenCavityMultiGPU/DrivenCavityMultiGPU.cpp @@ -60,7 +60,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -void multipleLevel(std::filesystem::path& configPath) +void runVirtualFluids(const vf::basics::ConfigurationFile& config) { vf::gpu::Communicator& communicator = vf::gpu::Communicator::getInstance(); @@ -68,9 +68,6 @@ void multipleLevel(std::filesystem::path& configPath) gridFactory->setTriangularMeshDiscretizationMethod(TriangularMeshDiscretizationMethod::POINT_IN_OBJECT); auto gridBuilder = MultipleGridBuilder::makeShared(gridFactory); - vf::basics::ConfigurationFile config; - std::cout << configPath << std::endl; - config.load(configPath.string()); SPtr<Parameter> para = std::make_shared<Parameter>(communicator.getNumberOfProcess(), communicator.getPID(), &config); BoundaryConditionFactory bcFactory = BoundaryConditionFactory(); GridScalingFactory scalingFactory = GridScalingFactory(); @@ -519,20 +516,9 @@ int main(int argc, char *argv[]) if (argv != NULL) { try { - ////////////////////////////////////////////////////////////////////////// - // 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("configDrivenCavityMultiGPU.txt"); - if (argc == 2) { - configName = argv[1]; - std::cout << "Using configFile command line argument: " << configName << std::endl; - } - - configPath.replace_filename(configName); - - multipleLevel(configPath); + VF_LOG_TRACE("For the default config path to work, execute the app from the project root."); + vf::basics::ConfigurationFile config = vf::basics::ConfigurationFile::loadConfig(argc, argv, "./apps/gpu/LBM/DrivenCavityMultiGPU/configDrivenCavityMultiGPU.txt"); + runVirtualFluids(config); ////////////////////////////////////////////////////////////////////////// } catch (const spdlog::spdlog_ex &ex) { diff --git a/apps/gpu/LBM/MusselOyster/MusselOyster.cpp b/apps/gpu/LBM/MusselOyster/MusselOyster.cpp index 48fa21471bbc5f4a6592dba6d4b874bc9ff5fac7..b583633b50542795fe4b27aca42c08cca1a5331c 100644 --- a/apps/gpu/LBM/MusselOyster/MusselOyster.cpp +++ b/apps/gpu/LBM/MusselOyster/MusselOyster.cpp @@ -84,7 +84,7 @@ const std::string simulationName("MusselOyster"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -void multipleLevel(std::filesystem::path &configPath) +void runVirtualFluids(const vf::basics::ConfigurationFile& config) { vf::gpu::Communicator &communicator = vf::gpu::Communicator::getInstance(); @@ -92,8 +92,6 @@ void multipleLevel(std::filesystem::path &configPath) gridFactory->setTriangularMeshDiscretizationMethod(TriangularMeshDiscretizationMethod::POINT_IN_OBJECT); auto gridBuilder = MultipleGridBuilder::makeShared(gridFactory); - vf::basics::ConfigurationFile config; - config.load(configPath.string()); SPtr<Parameter> para = std::make_shared<Parameter>(communicator.getNumberOfProcess(), communicator.getPID(), &config); BoundaryConditionFactory bcFactory = BoundaryConditionFactory(); @@ -187,8 +185,8 @@ void multipleLevel(std::filesystem::path &configPath) // height MUSSEL = 35.0 // height Oyster = 72.0 - auto bivalveSTL = std::make_shared<TriangularMesh>(stlPath + bivalveType + ".stl"); - auto bivalveRef_1_STL = nullptr; + SPtr<TriangularMesh> bivalveSTL = std::make_shared<TriangularMesh>(stlPath + bivalveType + ".stl"); + SPtr<TriangularMesh> bivalveRef_1_STL = nullptr; if (useLevels) bivalveRef_1_STL = std::make_shared<TriangularMesh>(stlPath + bivalveType + "_Level1.stl"); @@ -228,12 +226,12 @@ void multipleLevel(std::filesystem::path &configPath) gridBuilder->buildGrids(true); // buildGrids() has to be called before setting the BCs!!!! if (generatePart == 0) { - gridBuilder->findCommunicationIndices(CommunicationDirections::PZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PZ); gridBuilder->setCommunicationProcess(CommunicationDirections::PZ, 1); } if (generatePart == 1) { - gridBuilder->findCommunicationIndices(CommunicationDirections::MZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MZ); gridBuilder->setCommunicationProcess(CommunicationDirections::MZ, 0); } @@ -293,27 +291,27 @@ void multipleLevel(std::filesystem::path &configPath) gridBuilder->buildGrids(true); // buildGrids() has to be called before setting the BCs!!!! if (generatePart == 0) { - gridBuilder->findCommunicationIndices(CommunicationDirections::PX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PX); gridBuilder->setCommunicationProcess(CommunicationDirections::PX, 1); - gridBuilder->findCommunicationIndices(CommunicationDirections::PZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PZ); gridBuilder->setCommunicationProcess(CommunicationDirections::PZ, 2); } if (generatePart == 1) { - gridBuilder->findCommunicationIndices(CommunicationDirections::MX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MX); gridBuilder->setCommunicationProcess(CommunicationDirections::MX, 0); - gridBuilder->findCommunicationIndices(CommunicationDirections::PZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PZ); gridBuilder->setCommunicationProcess(CommunicationDirections::PZ, 3); } if (generatePart == 2) { - gridBuilder->findCommunicationIndices(CommunicationDirections::PX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PX); gridBuilder->setCommunicationProcess(CommunicationDirections::PX, 3); - gridBuilder->findCommunicationIndices(CommunicationDirections::MZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MZ); gridBuilder->setCommunicationProcess(CommunicationDirections::MZ, 0); } if (generatePart == 3) { - gridBuilder->findCommunicationIndices(CommunicationDirections::MX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MX); gridBuilder->setCommunicationProcess(CommunicationDirections::MX, 2); - gridBuilder->findCommunicationIndices(CommunicationDirections::MZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MZ); gridBuilder->setCommunicationProcess(CommunicationDirections::MZ, 1); } @@ -412,67 +410,67 @@ void multipleLevel(std::filesystem::path &configPath) gridBuilder->setPeriodicBoundaryCondition(false, false, false); if (generatePart == 0) { - gridBuilder->findCommunicationIndices(CommunicationDirections::PY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PY); gridBuilder->setCommunicationProcess(CommunicationDirections::PY, 1); - gridBuilder->findCommunicationIndices(CommunicationDirections::PX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PX); gridBuilder->setCommunicationProcess(CommunicationDirections::PX, 2); - gridBuilder->findCommunicationIndices(CommunicationDirections::PZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PZ); gridBuilder->setCommunicationProcess(CommunicationDirections::PZ, 4); } if (generatePart == 1) { - gridBuilder->findCommunicationIndices(CommunicationDirections::MY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MY); gridBuilder->setCommunicationProcess(CommunicationDirections::MY, 0); - gridBuilder->findCommunicationIndices(CommunicationDirections::PX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PX); gridBuilder->setCommunicationProcess(CommunicationDirections::PX, 3); - gridBuilder->findCommunicationIndices(CommunicationDirections::PZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PZ); gridBuilder->setCommunicationProcess(CommunicationDirections::PZ, 5); } if (generatePart == 2) { - gridBuilder->findCommunicationIndices(CommunicationDirections::PY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PY); gridBuilder->setCommunicationProcess(CommunicationDirections::PY, 3); - gridBuilder->findCommunicationIndices(CommunicationDirections::MX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MX); gridBuilder->setCommunicationProcess(CommunicationDirections::MX, 0); - gridBuilder->findCommunicationIndices(CommunicationDirections::PZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PZ); gridBuilder->setCommunicationProcess(CommunicationDirections::PZ, 6); } if (generatePart == 3) { - gridBuilder->findCommunicationIndices(CommunicationDirections::MY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MY); gridBuilder->setCommunicationProcess(CommunicationDirections::MY, 2); - gridBuilder->findCommunicationIndices(CommunicationDirections::MX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MX); gridBuilder->setCommunicationProcess(CommunicationDirections::MX, 1); - gridBuilder->findCommunicationIndices(CommunicationDirections::PZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PZ); gridBuilder->setCommunicationProcess(CommunicationDirections::PZ, 7); } if (generatePart == 4) { - gridBuilder->findCommunicationIndices(CommunicationDirections::PY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PY); gridBuilder->setCommunicationProcess(CommunicationDirections::PY, 5); - gridBuilder->findCommunicationIndices(CommunicationDirections::PX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PX); gridBuilder->setCommunicationProcess(CommunicationDirections::PX, 6); - gridBuilder->findCommunicationIndices(CommunicationDirections::MZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MZ); gridBuilder->setCommunicationProcess(CommunicationDirections::MZ, 0); } if (generatePart == 5) { - gridBuilder->findCommunicationIndices(CommunicationDirections::MY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MY); gridBuilder->setCommunicationProcess(CommunicationDirections::MY, 4); - gridBuilder->findCommunicationIndices(CommunicationDirections::PX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PX); gridBuilder->setCommunicationProcess(CommunicationDirections::PX, 7); - gridBuilder->findCommunicationIndices(CommunicationDirections::MZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MZ); gridBuilder->setCommunicationProcess(CommunicationDirections::MZ, 1); } if (generatePart == 6) { - gridBuilder->findCommunicationIndices(CommunicationDirections::PY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::PY); gridBuilder->setCommunicationProcess(CommunicationDirections::PY, 7); - gridBuilder->findCommunicationIndices(CommunicationDirections::MX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MX); gridBuilder->setCommunicationProcess(CommunicationDirections::MX, 4); - gridBuilder->findCommunicationIndices(CommunicationDirections::MZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MZ); gridBuilder->setCommunicationProcess(CommunicationDirections::MZ, 2); } if (generatePart == 7) { - gridBuilder->findCommunicationIndices(CommunicationDirections::MY, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MY); gridBuilder->setCommunicationProcess(CommunicationDirections::MY, 6); - gridBuilder->findCommunicationIndices(CommunicationDirections::MX, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MX); gridBuilder->setCommunicationProcess(CommunicationDirections::MX, 5); - gridBuilder->findCommunicationIndices(CommunicationDirections::MZ, LBM); + gridBuilder->findCommunicationIndices(CommunicationDirections::MZ); gridBuilder->setCommunicationProcess(CommunicationDirections::MZ, 3); } @@ -585,20 +583,9 @@ int main(int argc, char *argv[]) if (argv != NULL) { try { - ////////////////////////////////////////////////////////////////////////// - // 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("configMusselOyster.txt"); - if (argc == 2) { - configName = argv[1]; - std::cout << "Using configFile command line argument: " << configName << std::endl; - } - - configPath.replace_filename(configName); - - multipleLevel(configPath); + VF_LOG_TRACE("For the default config path to work, execute the app from the project root."); + vf::basics::ConfigurationFile config = vf::basics::ConfigurationFile::loadConfig(argc, argv, "./apps/gpu/LBM/MusselOyster/configMusselOyster.txt"); + runVirtualFluids(config); ////////////////////////////////////////////////////////////////////////// } catch (const spdlog::spdlog_ex &ex) { diff --git a/apps/gpu/LBM/SphereGPU/Sphere.cpp b/apps/gpu/LBM/SphereGPU/Sphere.cpp index f8010be8dc2eb52072b69d12b50161e019bd2e0a..707c3ab9cb7de038a94010e69e6f77f9799b4f7f 100644 --- a/apps/gpu/LBM/SphereGPU/Sphere.cpp +++ b/apps/gpu/LBM/SphereGPU/Sphere.cpp @@ -102,23 +102,8 @@ int main(int argc, char *argv[]) GridScalingFactory scalingFactory = GridScalingFactory(); 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()); - + VF_LOG_TRACE("For the default config path to work, execute the app from the project root."); + vf::basics::ConfigurationFile config = vf::basics::ConfigurationFile::loadConfig(argc, argv, "./apps/gpu/LBM/SphereGPU/config.txt"); para = std::make_shared<Parameter>(&config); } else { para = std::make_shared<Parameter>(); diff --git a/apps/gpu/LBM/SphereScaling/SphereScaling.cpp b/apps/gpu/LBM/SphereScaling/SphereScaling.cpp index d67ea9175b2637b3fef6414fda91f44acd7ac8ea..da80302e9e9b5b6f43c7eb3eea0ae8be08f22b93 100755 --- a/apps/gpu/LBM/SphereScaling/SphereScaling.cpp +++ b/apps/gpu/LBM/SphereScaling/SphereScaling.cpp @@ -63,7 +63,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -void multipleLevel(std::filesystem::path& configPath) +void runVirtualFluids(const vf::basics::ConfigurationFile& config) { vf::gpu::Communicator& communicator = vf::gpu::Communicator::getInstance(); @@ -71,8 +71,6 @@ void multipleLevel(std::filesystem::path& configPath) gridFactory->setTriangularMeshDiscretizationMethod(TriangularMeshDiscretizationMethod::POINT_IN_OBJECT); auto gridBuilder = MultipleGridBuilder::makeShared(gridFactory); - vf::basics::ConfigurationFile config; - config.load(configPath.string()); SPtr<Parameter> para = std::make_shared<Parameter>(communicator.getNumberOfProcess(), communicator.getPID(), &config); BoundaryConditionFactory bcFactory = BoundaryConditionFactory(); GridScalingFactory scalingFactory = GridScalingFactory(); @@ -659,20 +657,9 @@ int main(int argc, char *argv[]) if (argv != NULL) { try { - ////////////////////////////////////////////////////////////////////////// - // 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); - - multipleLevel(configPath); + VF_LOG_INFO("For the default config path to work, execute the app from the project root."); + vf::basics::ConfigurationFile config = vf::basics::ConfigurationFile::loadConfig(argc, argv, "./apps/gpu/LBM/SphereScaling/config.txt"); + runVirtualFluids(config); ////////////////////////////////////////////////////////////////////////// } catch (const spdlog::spdlog_ex &ex) { diff --git a/src/basics/config/ConfigurationFile.cpp b/src/basics/config/ConfigurationFile.cpp index 6e6ba21c0cec5de886b6ad125a662adacb9f1654..547468cd2fd6aadccbdc5017230fcf6cdb803733 100644 --- a/src/basics/config/ConfigurationFile.cpp +++ b/src/basics/config/ConfigurationFile.cpp @@ -8,6 +8,7 @@ #include <fstream> #include <iostream> #include <stdlib.h> +#include <filesystem> #include <basics/utilities/UbException.h> @@ -26,7 +27,7 @@ bool ConfigurationFile::load(const std::string& file) if (!inFile.good()) { - UB_THROW(UbException(UB_EXARGS, "Cannot read configuration file "+file+"!")); + UB_THROW(UbException(UB_EXARGS, "Cannot read configuration file " + file + "! Your current directory is " + std::filesystem::current_path().string() + ".")); } while (inFile.good() && ! inFile.eof()) diff --git a/src/basics/config/ConfigurationFile.h b/src/basics/config/ConfigurationFile.h index 3bf9e807280132afa81fa0473781896e87fbfd66..8ca12306e0ef321832efb19a2a428e6dc753df41 100644 --- a/src/basics/config/ConfigurationFile.h +++ b/src/basics/config/ConfigurationFile.h @@ -1,6 +1,8 @@ #ifndef BASICS_CONFIGURATIONFILE_H #define BASICS_CONFIGURATIONFILE_H +#include "Logger.h" +#include <filesystem> #include <map> #include <vector> #include <sstream> @@ -68,6 +70,23 @@ public: template<class T> T getValue(const std::string& key, T defaultValue) const; + static ConfigurationFile loadConfig(int argc, char *argv[], std::string configPath = "./config.txt") + { + // the config file's default path can be replaced by passing a command line argument + + if (argc > 1) + { + configPath = argv[1]; + VF_LOG_INFO("Using command line argument for config path: {}", configPath); + } else { + VF_LOG_INFO("Using default config path: {}", configPath); + } + + vf::basics::ConfigurationFile config; + config.load(configPath); + return config; + } + private: //! the container std::map<std::string, std::string> data;