change parameter via CLI options

This commit is contained in:
Dominik Demuth 2024-11-11 14:07:21 +01:00
parent 79ad5794b7
commit 0c6fd604fc
4 changed files with 50 additions and 23 deletions

View File

@ -1,5 +1,4 @@
#include "utils/io.h" #include "utils/io.h"
#include "simulation/sims.h" #include "simulation/sims.h"
#include "motions/base.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) }; std::unordered_map parameter { read_parameter(args.parameter_file) };
for (const auto& [key, value]: args.optional) {
parameter[key] = value;
}
std::random_device rd; std::random_device rd;
std::mt19937_64 rng(rd()); std::mt19937_64 rng(rd());

View File

@ -17,10 +17,7 @@ def run_sims(taus, ste: bool = True, spectrum: bool = False, exec_file: str = '.
arguments += ['BimodalAngle'] arguments += ['BimodalAngle']
# arguments += ['-TAU', '1'] arguments += ['-TAU', f'{tau}']
with pathlib.Path(config_file).open('a') as f:
f.write(f'tau={tau}\n')
subprocess.run(arguments) subprocess.run(arguments)
@ -71,7 +68,7 @@ def post_process_spectrum(taus, apod, tpulse):
def post_process_ste(taus): 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): for i, tau in enumerate(taus):
try: try:
@ -211,7 +208,7 @@ def ste(x, m0, t, beta, finfty):
if __name__ == '__main__': 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 # parameter for spectrum simulations
lb = 2e3 lb = 2e3

View File

@ -7,6 +7,7 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <complex>
#include <vector> #include <vector>
#include <iomanip> #include <iomanip>
#include <unordered_map> #include <unordered_map>
@ -16,29 +17,56 @@
Arguments parse_args(const int argc, char **argv) { Arguments parse_args(const int argc, char* argv[]) {
if (argc < 3) { if (argc < 3) {
throw std::runtime_error("Not enough arguments: missing parameter file"); throw std::runtime_error("Not enough arguments: missing parameter file");
} }
Arguments args; Arguments args;
for (int i=1; i<argc; i++) { const std::vector<std::string> input_args(argv + 1, argv + argc);
if (std::string arg = argv[i]; arg[0] == '-') {
if (arg == "--ste") { for (auto it = input_args.begin(); it != input_args.end(); ++it) {
args.ste = true; std::cout << *it << std::endl;
} else if (arg == "--spectrum") { if (it->at(0) == '-') {
if (*it == "--spectrum") {
args.spectrum = true; args.spectrum = true;
} else { std::cout << "spectrum: " << args.spectrum << std::endl;
throw std::runtime_error("Unrecognized option: " + arg); continue;
} }
} else if (args.parameter_file.empty()) {
args.parameter_file = arg; if (*it == "--ste") {
} else if (args.motion_type.empty()) { args.ste = true;
args.motion_type = arg; std::cout << "ste: " << args.ste << std::endl;
} else { continue;
throw std::invalid_argument("Too many options for parameter file");
} }
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()) { if (args.parameter_file.empty()) {
@ -48,7 +76,6 @@ Arguments parse_args(const int argc, char **argv) {
if (args.motion_type.empty()) { if (args.motion_type.empty()) {
throw std::invalid_argument("Missing motion model"); throw std::invalid_argument("Missing motion model");
} }
return args; return args;
} }

View File

@ -16,7 +16,7 @@ struct Arguments {
std::unordered_map<std::string, double> optional; std::unordered_map<std::string, double> optional;
}; };
Arguments parse_args(int argc, char **argv); Arguments parse_args(int argc, char* argv[]);
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path&); std::unordered_map<std::string, double> read_parameter(const std::filesystem::path&);