Commit 2090ac6e authored by Simon Spannagel's avatar Simon Spannagel
Browse files

Merge branch 'p-meshconv-units' into 'master'

MeshConverter: Check Units before Run

See merge request allpix-squared/allpix-squared!876
parents ec30e28f 3340251c
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -112,3 +112,7 @@ output_path = "test"
my_switch = true
my_other_switch = 0
```

{{% alert title="Note" color="info" %}}
In some places, providing configuration variables with units is mandatory. In case the respective input should be interpreted as base units, or without units such as a weighting potential, the parameter can be provided as empty string, i.e. `observable_units = ""`.
{{% /alert %}}
+91 −86
Original line number Diff line number Diff line
@@ -65,18 +65,9 @@ int main(int argc, char** argv) {

    int return_code = 0;

    try {

    // Register the default set of units with this executable:
    allpix::register_units();

        // If no arguments are provided, print the help:
        bool print_help = false;
        if(argc == 1) {
            print_help = true;
            return_code = 1;
        }

    // Add stream and set default logging level
    Log::addStream(std::cout);

@@ -84,19 +75,26 @@ int main(int argc, char** argv) {
    std::signal(SIGQUIT, interrupt_handler);
    std::signal(SIGINT, interrupt_handler);

    // If no arguments are provided, print the help:
    bool print_help = false;
    if(argc == 1) {
        print_help = true;
        return_code = 1;
    }

    std::string file_prefix;
    std::string init_file_prefix;
    std::string log_file_name;

    std::string conf_file_name;
        allpix::LogLevel log_level = allpix::LogLevel::NONE;

    for(int i = 1; i < argc; i++) {
        if(strcmp(argv[i], "-h") == 0) {
            print_help = true;
        } else if(strcmp(argv[i], "-v") == 0 && (i + 1 < argc)) {
            try {
                    log_level = Log::getLevelFromString(std::string(argv[++i]));
                auto log_level = Log::getLevelFromString(std::string(argv[++i]));
                Log::setReportingLevel(log_level);
            } catch(std::invalid_argument& e) {
                LOG(ERROR) << "Invalid verbosity level \"" << std::string(argv[i]) << "\", ignoring overwrite";
                return_code = 1;
@@ -152,35 +150,35 @@ int main(int argc, char** argv) {
        return return_code;
    }

    // NOTE: this stream should be available for the duration of the logging
    std::ofstream log_file;
    if(!log_file_name.empty()) {
        log_file.open(log_file_name, std::ios_base::out | std::ios_base::trunc);
        if(!log_file.good()) {
            LOG(FATAL) << "Cannot write to provided log file! Check if permissions are sufficient.";
            Log::finish();
            return 1;
        }
        Log::addStream(log_file);
    }

    try {
        std::ifstream file(conf_file_name);
        allpix::ConfigReader reader(file, conf_file_name);
        allpix::Configuration config = reader.getHeaderConfiguration();

        auto log_level = Log::getReportingLevel();
        if(log_level == allpix::LogLevel::NONE) {
            auto log_level_string = config.get<std::string>("log_level", "INFO");
            std::transform(log_level_string.begin(), log_level_string.end(), log_level_string.begin(), ::toupper);
            try {
                log_level = Log::getLevelFromString(log_level_string);
                Log::setReportingLevel(log_level);
            } catch(std::invalid_argument& e) {
                LOG(ERROR) << "Log level \"" << log_level_string
                           << "\" specified in the configuration is invalid, defaulting to INFO instead";
                log_level = allpix::LogLevel::INFO;
            }
        }

        // Set log level:
        Log::setReportingLevel(log_level);

        // NOTE: this stream should be available for the duration of the logging
        std::ofstream log_file;
        if(!log_file_name.empty()) {
            log_file.open(log_file_name, std::ios_base::out | std::ios_base::trunc);
            if(!log_file.good()) {
                LOG(FATAL) << "Cannot write to provided log file! Check if permissions are sufficient.";
                Log::finish();
                return 1;
                Log::setReportingLevel(allpix::LogLevel::INFO);
            }
            Log::addStream(log_file);
        }

        LOG(STATUS) << "Welcome to the Mesh Converter Tool of Allpix^2 " << ALLPIX_PROJECT_VERSION;
@@ -201,6 +199,13 @@ int main(int argc, char** argv) {
        auto regions = config.getArray<std::string>("region");
        auto observable = config.get<std::string>("observable");
        const auto units = config.get<std::string>("observable_units");
        // Test if this unit is valid:
        try {
            (void)Units::get(units);
        } catch(std::invalid_argument& e) {
            throw allpix::InvalidValueError(config, "observable_units", e.what());
        }

        const auto vector_field = config.get<bool>("vector_field", (observable == "ElectricField"));

        const auto interpolate = config.get<bool>("interpolate", true);