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 "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());

View File

@ -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

View File

@ -7,6 +7,7 @@
#include <fstream>
#include <iostream>
#include <algorithm>
#include <complex>
#include <vector>
#include <iomanip>
#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) {
throw std::runtime_error("Not enough arguments: missing parameter file");
}
Arguments args;
for (int i=1; i<argc; i++) {
if (std::string arg = argv[i]; arg[0] == '-') {
if (arg == "--ste") {
args.ste = true;
} else if (arg == "--spectrum") {
const std::vector<std::string> 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;
}

View File

@ -16,7 +16,7 @@ struct Arguments {
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&);