Skip to content

Commit

Permalink
clean up config
Browse files Browse the repository at this point in the history
  • Loading branch information
Benedikt Moritz Maurer committed Oct 6, 2024
1 parent 49b82dc commit c074f01
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion dev/app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace dpc = flash::data_processing::config;
namespace tp = flash::types;

po::variables_map loadCommandLineArguments(int argc, char* argv[]) {

po::options_description desc("Allowed options");
desc.add_options()("help,h", "produce help message")(
"config-file,i", po::value<std::string>()->default_value("config.yaml"), "set path to a config.yaml");
Expand All @@ -29,6 +30,7 @@ po::variables_map loadCommandLineArguments(int argc, char* argv[]) {
}

dpc::Config setupConfigParameters(po::variables_map commandLineArguments) {

auto configFile = commandLineArguments["config-file"].as<std::string>();
std::cout << "Load configuration from " << configFile << std::endl;
try {
Expand All @@ -53,7 +55,6 @@ int main(int argc, char* argv[]) {
auto commandLineArguments = loadCommandLineArguments(argc, argv);
auto configParameters = setupConfigParameters(commandLineArguments);



return 0;
}
6 changes: 4 additions & 2 deletions dev/libs/data_processing/include/data_processing/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#include <tuple>
#include <vector>

#include <types/types.h>
#include <yaml-cpp/yaml.h>

#include <types/types.h>

namespace flash {
namespace data_processing {
namespace config {
Expand Down Expand Up @@ -52,7 +53,7 @@ class InvalidConfigurationException : public std::exception {
};

/// @brief Options for random seed.
enum class Seed { fixed, clock };
enum class Seed { fixed, clock, invalid };

/// @brief Allowed Keys in config.yaml
static const std::vector<std::string> getAllowedKeys() {
Expand Down Expand Up @@ -85,6 +86,7 @@ static const std::vector<std::string> getMandatoryKeys() {
return allowedKeys;
}

/// @brief Default configuration values for non mandatory keys.
struct DefaultConfig {
types::size max_cvt_Itereations = 1000;
types::real_dp cvt_convergence_criterium = 1.e-5;
Expand Down
8 changes: 8 additions & 0 deletions dev/libs/data_processing/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Seed nodeToSeed(const YAML::Node &node, const std::string &key, const Seed &defa
} catch (std::out_of_range) {
throw InvalidConfigurationException("Invalid option for seed.");
}
return Seed::invalid;
}

template <typename T>
Expand All @@ -25,6 +26,7 @@ T nodeToValue(const YAML::Node &node, std::string msg) {
} catch (...) {
throw InvalidConfigurationException(msg);
}
return T();
}

template <typename T>
Expand All @@ -37,6 +39,7 @@ T nodeToValueWithDefault(const YAML::Node &node, const std::string &key, const T
} catch (...) {
throw InvalidConfigurationException(msg);
}
return T();
}

template <typename T, tp::size N>
Expand All @@ -51,6 +54,7 @@ auto nodeToArray(const YAML::Node &node, bool (*rule)(T value), std::string msg)
} catch (...) {
throw InvalidConfigurationException(msg);
}
return std::array<T, N>();
}

Config::Config(const YAML::Node &configYamlNode, const std::vector<std::string> &allowedKeys,
Expand Down Expand Up @@ -167,4 +171,8 @@ void Config::validate() {
if (cvt_convergence_criterium == 0) {
throw InvalidConfigurationException("cvt_convergence_criterium must be > 0.");
}
// Rule for seed_source
if (seed_source == Seed::invalid) {
throw InvalidConfigurationException("seed_source is invalid.");
}
}
12 changes: 7 additions & 5 deletions dev/tests/data_processing/src/config_test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#define CATCH_CONFIG_MAIN

#include <data_processing/config.h>

#include <catch2/catch.hpp>
#include <yaml-cpp/yaml.h>

#include <data_processing/config.h>
#include <types/types.h>

namespace ftp = flash::types;
namespace tp = flash::types;
using namespace flash::data_processing::config;

/**
Expand Down Expand Up @@ -49,7 +51,7 @@ TEST_CASE("Test Config construction from a valid YAML::node that defines all par
REQUIRE(params.n_isdf_wscr_occupied == 4);
REQUIRE(params.n_isdf_wscr_unoccupied == 5);
REQUIRE(params.max_lanczos_iterations == 200);
REQUIRE(params.omega_range == std::array<ftp::real_dp, 2>{1.0, 2.0});
REQUIRE(params.omega_range == std::array<tp::real_dp, 2>{1.0, 2.0});
REQUIRE(params.n_omega == 1500);
REQUIRE(params.max_cvt_iterations == 333);
REQUIRE(params.cvt_convergence_criterium == 1e-6);
Expand All @@ -66,7 +68,7 @@ TEST_CASE("Test Config construction from a valid YAML::node that defines not all
REQUIRE(params.n_isdf_wscr_occupied == 4);
REQUIRE(params.n_isdf_wscr_unoccupied == 5);
REQUIRE(params.max_lanczos_iterations == 200);
REQUIRE(params.omega_range == std::array<ftp::real_dp, 2>{1.0, 2.0});
REQUIRE(params.omega_range == std::array<tp::real_dp, 2>{1.0, 2.0});
REQUIRE(params.n_omega == 1500);
REQUIRE(params.max_cvt_iterations == 1000);
REQUIRE(params.cvt_convergence_criterium == 1e-5);
Expand Down

0 comments on commit c074f01

Please sign in to comment.