From 0c6fd604fc70d587416a54873d42854bff0cf556 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Mon, 11 Nov 2024 14:07:21 +0100 Subject: [PATCH] change parameter via CLI options --- main.cpp | 5 ++++- test.py | 9 +++------ utils/io.cpp | 57 ++++++++++++++++++++++++++++++++++++++-------------- utils/io.h | 2 +- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/main.cpp b/main.cpp index 4d3be2f..4d06057 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,4 @@ - #include "utils/io.h" #include "simulation/sims.h" #include "motions/base.h" @@ -25,6 +24,10 @@ int main (const int argc, char *argv[]) { std::unordered_map parameter { read_parameter(args.parameter_file) }; + for (const auto& [key, value]: args.optional) { + parameter[key] = value; + } + std::random_device rd; std::mt19937_64 rng(rd()); diff --git a/test.py b/test.py index 8264084..4eb567e 100644 --- a/test.py +++ b/test.py @@ -17,10 +17,7 @@ def run_sims(taus, ste: bool = True, spectrum: bool = False, exec_file: str = '. arguments += ['BimodalAngle'] - # arguments += ['-TAU', '1'] - - with pathlib.Path(config_file).open('a') as f: - f.write(f'tau={tau}\n') + arguments += ['-TAU', f'{tau}'] subprocess.run(arguments) @@ -71,7 +68,7 @@ def post_process_spectrum(taus, apod, tpulse): def post_process_ste(taus): - tevo = np.linspace(1e-6, 120e-6, num=8) + tevo = np.linspace(1e-6, 120e-6, num=121) for i, tau in enumerate(taus): try: @@ -211,7 +208,7 @@ def ste(x, m0, t, beta, finfty): if __name__ == '__main__': - tau_values = np.geomspace(1e-2, 1e-6, num=1) # if num=1, tau is first value + tau_values = np.geomspace(1e-2, 1e2, num=2) # if num=1, tau is first value # parameter for spectrum simulations lb = 2e3 diff --git a/utils/io.cpp b/utils/io.cpp index dbd0156..d8fb4e1 100644 --- a/utils/io.cpp +++ b/utils/io.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -16,29 +17,56 @@ -Arguments parse_args(const int argc, char **argv) { +Arguments parse_args(const int argc, char* argv[]) { if (argc < 3) { throw std::runtime_error("Not enough arguments: missing parameter file"); } Arguments args; - for (int i=1; i input_args(argv + 1, argv + argc); + + for (auto it = input_args.begin(); it != input_args.end(); ++it) { + std::cout << *it << std::endl; + if (it->at(0) == '-') { + + if (*it == "--spectrum") { args.spectrum = true; - } else { - throw std::runtime_error("Unrecognized option: " + arg); + std::cout << "spectrum: " << args.spectrum << std::endl; + continue; } - } else if (args.parameter_file.empty()) { - args.parameter_file = arg; - } else if (args.motion_type.empty()) { - args.motion_type = arg; - } else { - throw std::invalid_argument("Too many options for parameter file"); + + if (*it == "--ste") { + args.ste = true; + std::cout << "ste: " << args.ste << std::endl; + continue; + } + + std::string stripped_arg; + if (it->size() > 2 && it->at(0) == '-' && it->at(1) == '-') { + stripped_arg = it->substr(2, it->size()); + } else if (it->size() > 1 && it->at(0) == '-') { + stripped_arg = it->substr(1, it->size()); + } + std::transform(stripped_arg.begin(), stripped_arg.end(), stripped_arg.begin(), [](unsigned char c) { return std::tolower(c); }); + const auto stripped_value = std::stod(*(++it), nullptr); + args.optional[stripped_arg] = stripped_value; + continue; } + + if (args.parameter_file.empty()) { + std::cout << *it << " is parameter_file" << std::endl; + args.parameter_file = *it; + continue; + } + + if (args.motion_type.empty()) { + std::cout << *it << " is motion model" << std::endl; + args.motion_type = *it; + continue; + } + + throw std::invalid_argument("too many positional arguments"); } if (args.parameter_file.empty()) { @@ -48,7 +76,6 @@ Arguments parse_args(const int argc, char **argv) { if (args.motion_type.empty()) { throw std::invalid_argument("Missing motion model"); } - return args; } diff --git a/utils/io.h b/utils/io.h index 5d36c70..cc6bf84 100644 --- a/utils/io.h +++ b/utils/io.h @@ -16,7 +16,7 @@ struct Arguments { std::unordered_map optional; }; -Arguments parse_args(int argc, char **argv); +Arguments parse_args(int argc, char* argv[]); std::unordered_map read_parameter(const std::filesystem::path&);