move files

This commit is contained in:
Dominik Demuth
2024-11-11 11:03:02 +01:00
parent f1749a0af2
commit 1eb3e3be2d
10 changed files with 20 additions and 26 deletions

53
utils/functions.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <vector>
#include <array>
#include <chrono>
#include <iostream>
#include <cmath>
#include <utility>
int nearest_index(const std::vector<double> &x_ref, const double x, int start=0) {
while (x > x_ref[start+1]) {
start++;
}
return start;
}
double lerp(const std::vector<double>& x_ref, const std::vector<double>& y_ref, const double x, const int i) {
/*
* Linear interpolation between two
*/
const double x_left = x_ref[i];
const double y_left = y_ref[i];
const double x_right = x_ref[i+1];
const double y_right = y_ref[i+1];
const double dydx = (y_right - y_left) / ( x_right - x_left );
return y_left + dydx * (x - x_left);
}
std::chrono::time_point<std::chrono::system_clock> printSteps(
/*
* Prints roughly every 10 seconds how many runs were done and gives a time estimation
*/
const std::chrono::time_point<std::chrono::system_clock> last_print_out,
const std::chrono::time_point<std::chrono::system_clock> start,
const int total,
const int steps
) {
const auto now = std::chrono::high_resolution_clock::now();
if (const std::chrono::duration<float> duration = now - last_print_out; duration.count() < 10.) {
return last_print_out;
}
const std::chrono::duration<float> duration = now - start;
const auto passed = duration.count();
std::cout << steps << " of " << total << " steps: " << passed << "s passed; ~" << passed * static_cast<float>(total-steps) / static_cast<float>(steps) << "s remaining\n";
return now;
}

14
utils/functions.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef RWSIM_FUNCTIONS_H
#define RWSIM_FUNCTIONS_H
#include <vector>
#include <chrono>
int nearest_index(const std::vector<double>&, double, int);
double lerp(const std::vector<double>&, const std::vector<double>&, double, int);
std::chrono::time_point<std::chrono::system_clock> printSteps(std::chrono::time_point<std::chrono::system_clock>, std::chrono::time_point<std::chrono::system_clock>, int, int);
#endif

137
utils/io.cpp Normal file
View File

@ -0,0 +1,137 @@
//
// Created by dominik on 8/14/24.
//
#include "io.h"
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <map>
#include <string>
#include <filesystem>
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") {
args.spectrum = true;
} else {
throw std::runtime_error("Unrecognized option: " + arg);
}
} 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 (args.parameter_file.empty()) {
throw std::invalid_argument("Missing parameter file");
}
if (args.motion_type.empty()) {
throw std::invalid_argument("Missing motion model");
}
return args;
}
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path& infile) {
if (!std::filesystem::exists(infile)) {
std::cerr << "File " << infile << " does not exist" << std::endl;
exit(1);
}
std::ifstream instream(infile);
std::unordered_map<std::string, double> parameter;
std::string line;
std::string delim = "=";
std::string key;
std::string value;
size_t delim_pos;
while (std::getline(instream, line)) {
// skip comment lines starting with #, and empty lines
if (line[0] == '#' || line.length() == 1) continue;
// strip spaces from line to have always key=value
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
// split at '=' character and add to map
delim_pos = line.find('=');
key = line.substr(0, delim_pos);
value = line.substr(delim_pos+1);
parameter[key] = std::stod(value);
}
// print result
std::cout << "Found parameter\n";
for (const auto& [key, value]: parameter) {
std::cout << " " << key << ": " << std::to_string(value) << "\n";
}
std::cout << std::endl;
return parameter;
}
void fid_write_out(const std::string& filename, const std::vector<double>& x, const std::vector<double>& y, const double tau, const double t_evo) {
auto size = x.size();
std::ostringstream sfile;
sfile << filename << "_";
sfile << std::setprecision(6) << std::scientific;
sfile << "tau=" << tau << "_tevo=" << t_evo << ".dat";
{
std::string outfile = sfile.str();
std::ofstream fid_file(outfile, std::ios::out);
for (unsigned int i = 0; i < size; i++) {
fid_file << x[i] << "\t" << y[i] << "\n";
}
}
}
void fid_write_out(const std::string& filename, const std::vector<double>& x, const std::map<double, std::vector<double>>& y, const double tau) {
auto size = x.size();
std::ostringstream sfile;
sfile << filename << "_";
sfile << std::setprecision(6) << std::scientific;
sfile << "tau=" << tau << ".dat";
{
std::string outfile = sfile.str();
std::ofstream fid_file(outfile, std::ios::out);
fid_file << "#";
for (const auto& [t_echo_j, _] : y) {
fid_file << t_echo_j << "\t";
}
fid_file << std::endl;
for (unsigned int i = 0; i < size; i++) {
fid_file << x[i];
for (const auto& [_, fid_j] : y) {
fid_file << "\t" << fid_j[i];
}
fid_file << "\n";
}
}
}

26
utils/io.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef RWSIM_IO_H
#define RWSIM_IO_H
#include <unordered_map>
#include <map>
#include <string>
#include <filesystem>
#include <vector>
struct Arguments {
std::string parameter_file{};
bool ste = false;
bool spectrum = false;
std::string motion_type{};
std::unordered_map<std::string, double> optional;
};
Arguments parse_args(int argc, char **argv);
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path&);
void fid_write_out(const std::string&, const std::vector<double>&, const std::vector<double>&, double, double);
void fid_write_out(const std::string&, const std::vector<double>&, const std::map<double, std::vector<double>>&, double tau);
#endif

60
utils/ranges.cpp Normal file
View File

@ -0,0 +1,60 @@
//
// Created by dominik on 8/14/24.
//
#include <vector>
#include <algorithm>
#include <cmath>
#include "ranges.h"
std::vector<double> arange(const int size, const double spacing=1.) {
std::vector<double> out(size);
std::generate(out.begin(), out.end(), [n = 0, spacing]() mutable { return n++ * spacing; });
return out;
}
std::vector<double> linspace(const double start, const double stop, const int steps) {
std::vector<double> range;
if (steps == 0) {
return range;
}
if (steps == 1) {
range.push_back(start);
return range;
}
const double stepsize = (stop-start) / (steps-1);
for (int i=0; i<steps; i++) {
range.push_back(start + stepsize * i);
}
return range;
}
std::vector<double> logspace(const double start, const double stop, const int steps) {
std::vector<double> range;
if (steps == 0) {
return range;
}
if (steps == 1) {
range.push_back(start);
return range;
}
const double logstart = std::log10(start);
const double logstop = std::log10(stop);
const double stepsize = (logstop-logstart) / (steps-1);
for (int i=0; i<steps; i++) {
range.push_back(pow(10, logstart + stepsize * i));
}
return range;
}

12
utils/ranges.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef RWSIM_RANGES_H
#define RWSIM_RANGES_H
#include <vector>
std::vector<double> arange(int, double);
std::vector<double> linspace(double, double, int);
std::vector<double> logspace(double, double, int);
#endif