From 705566b3f6a92c03cae87ea50caf767342b7cd13 Mon Sep 17 00:00:00 2001 From: Simon Praetorius <simon.praetorius@tu-dresden.de> Date: Mon, 11 Jun 2012 15:08:45 +0000 Subject: [PATCH] Initfile-parser extended --- AMDiS/src/AMDiS.cc | 5 ++++- AMDiS/src/DOFIndexed.h | 14 ++++++++---- AMDiS/src/Global.h | 15 +++++++++++++ AMDiS/src/Initfile.cc | 45 ++++++++++++++++++++++++++++----------- AMDiS/src/Initfile.h | 12 +---------- AMDiS/src/io/VtkWriter.cc | 2 +- 6 files changed, 63 insertions(+), 30 deletions(-) diff --git a/AMDiS/src/AMDiS.cc b/AMDiS/src/AMDiS.cc index 1343b757..04bc8713 100644 --- a/AMDiS/src/AMDiS.cc +++ b/AMDiS/src/AMDiS.cc @@ -41,7 +41,10 @@ namespace AMDiS { Zoltan_Initialize(argc, argv, &zoltanVersion); #endif #endif - + + Parameters::clearData(); + Parameters::readArgv(argc, argv); + if (initFileName == "") { TEST_EXIT(argc >= 2)("No init file!\n"); Parameters::init(string(argv[1])); diff --git a/AMDiS/src/DOFIndexed.h b/AMDiS/src/DOFIndexed.h index 64a41fdb..46238a1c 100644 --- a/AMDiS/src/DOFIndexed.h +++ b/AMDiS/src/DOFIndexed.h @@ -92,20 +92,26 @@ namespace AMDiS { template<typename T> class DOFIndexed : public DOFIndexedBase { + public: + typedef T value_type; + typedef DegreeOfFreedom size_type; + typedef value_type& reference; + typedef value_type const& const_reference; + public: virtual ~DOFIndexed() {} /// Returns iterator to the begin of container - virtual typename std::vector<T>::iterator begin() = 0; + virtual typename std::vector<value_type>::iterator begin() = 0; /// Returns iterator to the end of container - virtual typename std::vector<T>::iterator end() = 0; + virtual typename std::vector<value_type>::iterator end() = 0; /// Returns container element at index i - virtual T& operator[](DegreeOfFreedom i) = 0; + virtual reference operator[](size_type i) = 0; /// Returns container element at index i - virtual const T& operator[](DegreeOfFreedom i) const = 0; + virtual const_reference operator[](size_type i) const = 0; }; void mv(MatrixTranspose transpose, diff --git a/AMDiS/src/Global.h b/AMDiS/src/Global.h index 14c63e3e..db5a51d3 100644 --- a/AMDiS/src/Global.h +++ b/AMDiS/src/Global.h @@ -57,6 +57,7 @@ #endif #include <boost/algorithm/string.hpp> +#include <boost/algorithm/string/trim.hpp> #include "boost/tuple/tuple.hpp" #include "AMDiS_fwd.h" @@ -123,6 +124,20 @@ namespace AMDiS { } }; + /// check for file existence + inline bool file_exists(const std::string filename) + { + return access(filename.c_str(), F_OK) == 0; + }; + + /// trim std::string + inline std::string trim(const std::string& oldStr) + { + std::string swap(oldStr); + boost::algorithm::trim(swap); + return swap; + }; + // ===== some simple template functions ====================================== template<typename T> inline T abs(T a) diff --git a/AMDiS/src/Initfile.cc b/AMDiS/src/Initfile.cc index 8c44c17c..2ae36ed7 100644 --- a/AMDiS/src/Initfile.cc +++ b/AMDiS/src/Initfile.cc @@ -39,7 +39,6 @@ namespace AMDiS { void Initfile::init(std::string in) { initIntern(); - singlett->clear(); fn_include_list.clear(); singlett->read(in); singlett->getInternalParameters(); @@ -84,8 +83,8 @@ namespace AMDiS { Parser parser(sw); // add parameter to map after variable replacement - std::string paramName = variableReplacement(InitfileInternal::trim(parser.name)); - std::string paramValue = variableReplacement(InitfileInternal::trim(parser.value)); + std::string paramName = variableReplacement(trim(parser.name)); + std::string paramValue = variableReplacement(trim(parser.value)); paramValue = variableEvaluation(paramValue); operator[](paramName) = paramValue; @@ -164,7 +163,7 @@ namespace AMDiS { std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar)); inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varParam); - posVar = inputSwap.find_first_of('$',posVarEnd); + posVar = inputSwap.find_first_of('$',posVarBegin); } return inputSwap; @@ -174,16 +173,11 @@ namespace AMDiS { { std::string whitespaces = " \t\r\f"; std::string inputSwap = input; - size_t posVar = inputSwap.find_first_of('$'); + size_t posVar = inputSwap.find("$("); while (posVar != string::npos) { size_t posVarBegin, posVarEnd; - if(inputSwap[posVar+1] == '(') { // $(expr) - posVarEnd = inputSwap.find_first_of(')',posVar + 2); - posVarBegin = posVar + 1; - } else { - posVar = inputSwap.find_first_of('$',posVar+1); - continue; - } + posVarEnd = inputSwap.find_first_of(')',posVar + 2); + posVarBegin = posVar + 1; std::string varName = inputSwap.substr(posVarBegin + 1 , posVarEnd - posVarBegin - 1); double value = 0.0; @@ -193,7 +187,7 @@ namespace AMDiS { std::string replaceName = inputSwap.substr(posVar , posVarEnd - posVar + (posVarBegin - posVar)); inputSwap.replace(inputSwap.find(replaceName), replaceName.length(), varName); - posVar = inputSwap.find_first_of('$'); + posVar = inputSwap.find("$(",posVarBegin); } return inputSwap; @@ -201,11 +195,36 @@ namespace AMDiS { void Initfile::readArgv(int argc, char **argv) { + initIntern(); for (int i = 0; i < argc; i++) { if (strcmp("-rs", argv[i]) == 0) { std::string input(argv[i + 1]); add("argv->rs", input, 0); } + else if (strcmp("-parameters", argv[i]) == 0) { + std::string input(argv[i + 1]); + int found = input.find_first_of(';'); + + std::vector<std::string> parameters; + while (found != static_cast<int>(std::string::npos)) { + if (found > 2) { + parameters.push_back(input.substr(0, found).c_str()); + } + input = input.substr(found + 1); + found = input.find_first_of(';'); + } + if (input.length() > 2) + parameters.push_back(input.c_str()); + + for (size_t i = 0; i < parameters.size(); i++) { + int found = input.find_first_of(':'); + if (found != static_cast<int>(std::string::npos)) { + std::string value = input.substr(found+1); + set(input.substr(0, found), value, 0); + } + } + + } } } diff --git a/AMDiS/src/Initfile.h b/AMDiS/src/Initfile.h index 61fbf23b..79622592 100644 --- a/AMDiS/src/Initfile.h +++ b/AMDiS/src/Initfile.h @@ -24,7 +24,6 @@ #include <typeinfo> #include "FixVec.h" -#include <boost/algorithm/string/trim.hpp> #include <boost/lexical_cast.hpp> #include <boost/numeric/conversion/cast.hpp> @@ -132,16 +131,7 @@ namespace AMDiS { ">\nParser message: '" + m + "'") {} }; - - - /// trim std::string - inline std::string trim(const std::string& oldStr) - { - std::string swap(oldStr); - boost::algorithm::trim(swap); - return swap; - } - + /// return the delimiter or throw an exception if there is no known /// delimiter in value diff --git a/AMDiS/src/io/VtkWriter.cc b/AMDiS/src/io/VtkWriter.cc index 51b333e4..5f4e47e8 100644 --- a/AMDiS/src/io/VtkWriter.cc +++ b/AMDiS/src/io/VtkWriter.cc @@ -199,7 +199,7 @@ namespace AMDiS { { WorldVector<DOFVector<double>*> valuesWV; for (int i =0 ; i < valuesWV.getSize(); i++) - valuesWV[i] = new DOFVector<double>(values->getFeSpace(), "valuesWV_i"); + valuesWV[i] = new DOFVector<double>(values->getFeSpace(), "values["+boost::lexical_cast<std::string>(i)+"]"); transform(values, &valuesWV); writeFile(valuesWV, filename, writeParallel); for (int i = 0; i < valuesWV.getSize(); i++) -- GitLab