change saving

This commit is contained in:
Dominik Demuth 2024-12-01 19:23:58 +01:00
parent 60ef1b0bcf
commit 5b5aacff0b
19 changed files with 125 additions and 28 deletions

View File

@ -7,6 +7,7 @@
#include "tetrahedral.h"
#include <stdexcept>
#include <map>
Motion::Motion(std::string name, const double delta, const double eta, std::mt19937_64& rng) : m_name(std::move(name)), m_delta(delta), m_eta(eta), m_rng(rng) {
m_uni_dist = std::uniform_real_distribution(0., 1.);
@ -57,6 +58,12 @@ void Motion::setParameters(const std::unordered_map<std::string, double> &parame
m_eta = parameters.at("eta");
}
std::unordered_map<std::string, double> Motion::getParameters() const {
return std::unordered_map<std::string, double>{
{"delta", m_delta},
{"eta", m_eta}
};
}
std::ostream& operator<<(std::ostream& os, const Motion& m) {
os << m.getName();

View File

@ -21,7 +21,7 @@ public:
virtual double jump() = 0;
virtual void setParameters(const std::unordered_map<std::string, double>&);
[[nodiscard]] virtual std::unordered_map<std::string, double> getParameters() const;
[[nodiscard]] double getDelta() const { return m_delta; }
void setDelta(const double delta) { m_delta = delta; }
[[nodiscard]] double getEta() const { return m_eta; }
@ -29,6 +29,8 @@ public:
[[nodiscard]] std::string getName() const { return m_name; }
[[nodiscard]] double getInitOmega() const { return m_initial_omega; };
[[nodiscard]] virtual std::string toString() const = 0;
static Motion* createFromInput(const std::string& input, std::mt19937_64& rng);
protected:

View File

@ -29,3 +29,15 @@ void BimodalAngle::setParameters(const std::unordered_map<std::string, double> &
m_angle2 = parameter.at("angle2") * M_PI / 180.;
m_prob = parameter.at("probability1");
}
std::unordered_map<std::string, double> BimodalAngle::getParameters() const {
auto parameter = Motion::getParameters();
parameter["angle1"] = m_angle1 * 180 / M_PI;
parameter["angle2"] = m_angle2 * 180 / M_PI;
parameter["probality1"] = m_prob;
return parameter;
}
std::string BimodalAngle::toString() const {
return std::string{"BimodalAngle/angle1=" + std::to_string(m_angle1 * 180 / M_PI) + "/angle2=" + std::to_string(m_angle2 * 180 / M_PI) + "/probability1=" + std::to_string(m_prob)};
}

View File

@ -12,6 +12,8 @@ public:
void initialize() override;
double jump() override;
void setParameters(const std::unordered_map<std::string, double> &) override;
[[nodiscard]] std::unordered_map<std::string, double> getParameters() const override;
[[nodiscard]] std::string toString() const override;
protected:
double m_angle1{0};

View File

@ -25,3 +25,13 @@ void SmallAngle::setParameters(const std::unordered_map<std::string, double> &pa
m_chi = parameters.at("angle") * M_PI / 180.0;
Motion::setParameters(parameters);
}
std::unordered_map<std::string, double> SmallAngle::getParameters() const {
auto parameter = Motion::getParameters();
parameter["angle"] = m_chi * 180 / M_PI;
return parameter;
}
std::string SmallAngle::toString() const {
return std::string{"IsotropicAngle/angle=" + std::to_string(m_chi * 180 / M_PI)};
}

View File

@ -13,6 +13,8 @@ public:
void initialize() override;
double jump() override;
void setParameters(const std::unordered_map<std::string, double> &) override;
[[nodiscard]] std::unordered_map<std::string, double> getParameters() const override;
[[nodiscard]] std::string toString() const override;
private:
double m_chi{0};

View File

@ -6,6 +6,10 @@ RandomJump::RandomJump(const double delta, const double eta, std::mt19937_64 &rn
RandomJump::RandomJump(std::mt19937_64 &rng) : Motion(std::string("RandomJump"), rng) {}
std::string RandomJump::toString() const {
return {"RandomJump"};
}
void RandomJump::initialize() {
m_initial_omega = RandomJump::jump();
}

View File

@ -10,6 +10,8 @@ public:
RandomJump(double, double, std::mt19937_64&);
explicit RandomJump(std::mt19937_64&);
[[nodiscard]] std::string toString() const override;
void initialize() override;
double jump() override;
};

View File

@ -28,3 +28,7 @@ double TetrahedralJump::jump() {
return m_corners[m_corner_idx];
}
std::string TetrahedralJump::toString() const {
return {"FourSiteTetrahedral"};
}

View File

@ -14,6 +14,8 @@ public:
void initialize() override;
double jump() override;
[[nodiscard]] std::string toString() const override;
private:
const double m_beta{std::acos(-1/3.)};
@ -24,5 +26,4 @@ private:
};
#endif //RWSIM_MOTIONTETRAHEDRAL_H

View File

@ -72,8 +72,9 @@ void run_spectrum(
}
// write fid to files
save_parameter_to_file("timesignal", motion.getName(), dist.getName(), parameter, optional);
save_data_to_file("timesignal", motion.getName(), dist.getName(), t_fid, fid_dict, optional);
const auto path = make_directory(motion, dist);
save_parameter_to_file(std::string("timesignal"), path, parameter, optional);
save_data_to_file(std::string("timesignal"), path, t_fid, fid_dict, optional);
printEnd(start);
}
@ -163,10 +164,11 @@ void run_ste(
}
// write to files
save_parameter_to_file("ste", motion.getName(), dist.getName(), parameter, optional);
save_data_to_file("coscos", motion.getName(), dist.getName(), mixing_times, cc_dict, optional);
save_data_to_file("sinsin", motion.getName(), dist.getName(), mixing_times, ss_dict, optional);
save_data_to_file("f2", motion.getName(), dist.getName(), mixing_times, f2, optional);
const auto folders = make_directory(motion, dist);
save_parameter_to_file(std::string("ste"), folders, parameter, optional);
save_data_to_file(std::string("coscos"), folders, mixing_times, cc_dict, optional);
save_data_to_file(std::string("sinsin"), folders, mixing_times, ss_dict, optional);
save_data_to_file(std::string("f2"), folders, mixing_times, f2, optional);
printEnd(start);
}

View File

@ -16,6 +16,13 @@ void Distribution::setParameters(const std::unordered_map<std::string, double> &
m_tau = parameters.at("tau");
}
std::unordered_map<std::string, double> Distribution::getParameters() const {
return std::unordered_map<std::string, double>{
{"tau", m_tau},
};
}
Distribution* Distribution::createFromInput(const std::string& input, std::mt19937_64& rng) {
if (input == "Delta")
return new DeltaDistribution(rng);

View File

@ -16,11 +16,14 @@ public:
[[nodiscard]] std::string getName() const { return m_name; };
virtual void setParameters(const std::unordered_map<std::string, double>&);
[[nodiscard]] virtual std::unordered_map<std::string, double> getParameters() const;
virtual void initialize() = 0;
virtual void draw_tau() = 0;
[[nodiscard]] double tau_wait() const;
[[nodiscard]] virtual std::string toString() const = 0;
static Distribution* createFromInput(const std::string& input, std::mt19937_64& rng);
protected:

View File

@ -9,3 +9,7 @@ void DeltaDistribution::initialize() {
void DeltaDistribution::draw_tau() {}
std::string DeltaDistribution::toString() const {
return {"Delta/tau=" + std::to_string(m_tau)};
}

View File

@ -10,6 +10,8 @@ public:
void initialize() override;
void draw_tau() override;
[[nodiscard]] std::string toString() const override;
};
#endif //RWSIM_TIMESDELTA_H

View File

@ -9,6 +9,12 @@ void LogNormalDistribution::setParameters(const std::unordered_map<std::string,
Distribution::setParameters(parameters);
}
std::unordered_map<std::string, double> LogNormalDistribution::getParameters() const {
auto parameter = Distribution::getParameters();
parameter["sigma"] = m_sigma;
return parameter;
}
void LogNormalDistribution::initialize() {
m_distribution = std::lognormal_distribution(std::log(m_tau), m_sigma);
m_tau_jump = m_distribution(m_rng);
@ -17,3 +23,7 @@ void LogNormalDistribution::initialize() {
void LogNormalDistribution::draw_tau() {
m_tau_jump = m_distribution(m_rng);
}
std::string LogNormalDistribution::toString() const {
return {"LogNormal/tau=" + std::to_string(m_tau) + "/sigma=" + std::to_string(m_sigma)};
}

View File

@ -3,7 +3,6 @@
#include "base.h"
#include <random>
#include <set>
class LogNormalDistribution final : public Distribution {
public:
@ -11,6 +10,9 @@ public:
explicit LogNormalDistribution(std::mt19937_64 &rng);
void setParameters(const std::unordered_map<std::string, double> &) override;
[[nodiscard]] std::unordered_map<std::string, double> getParameters() const override;
[[nodiscard]] std::string toString() const override;
void initialize() override;
void draw_tau() override;

View File

@ -7,11 +7,15 @@
#include <complex>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <map>
#include <string>
#include <filesystem>
#include "../motions/base.h"
#include "../times/base.h"
class Motion;
std::pair<std::string, double> get_optional_parameter(std::vector<std::string>::const_iterator &it) {
std::string stripped_arg;
@ -124,16 +128,30 @@ std::unordered_map<std::string, double> read_parameter(const std::filesystem::pa
}
std::string make_directory(
const Motion& motion,
const Distribution& distribution
) {
std::ostringstream path_name;
path_name << motion.toString() << "/" << distribution.toString();
if (!std::filesystem::create_directories(path_name.str())) {
std::cout << "Created directory " << path_name.str() << std::endl;
}
return path_name.str();
}
void save_parameter_to_file(
const std::string& resulttype,
const std::string& motiontype,
const std::string& disttype,
std::unordered_map<std::string, double>& parameter,
std::unordered_map<std::string, double>& optional
const std::string& directory,
const std::unordered_map<std::string, double>& parameter,
const std::unordered_map<std::string, double>& optional
) {
std::ostringstream parameter_filename;
parameter_filename << resulttype << "_" << motiontype << "_" << disttype;
parameter_filename << directory << "/" << resulttype;
parameter_filename << std::setprecision(6) << std::scientific;
for (const auto& [key, value]: optional) {
@ -155,15 +173,15 @@ void save_parameter_to_file(
void save_data_to_file(
const std::string& resulttype,
const std::string& motiontype,
const std::string& disttype,
const std::string& directory,
const std::vector<double>& x,
const std::map<double, std::vector<double>>& y,
std::unordered_map<std::string, double>& optional
const std::unordered_map<std::string, double>& optional
) {
// make file name
std::ostringstream datafile_name;
datafile_name << resulttype << "_" << motiontype << "_" << disttype;
datafile_name << directory << "/" << resulttype;
datafile_name << std::setprecision(6) << std::scientific;
for (const auto& [key, value]: optional) {
datafile_name << "_" << key << "=" << value;
@ -196,15 +214,15 @@ void save_data_to_file(
void save_data_to_file(
const std::string& resulttype,
const std::string& motiontype,
const std::string& disttype,
const std::string& directory,
const std::vector<double>& x,
const std::vector<double>& y,
std::unordered_map<std::string, double>& optional
const std::unordered_map<std::string, double>& optional
) {
// make file name
std::ostringstream datafile_name;
datafile_name << resulttype << "_" << motiontype << "_" << disttype;
datafile_name << directory << "/" << resulttype;
datafile_name << std::setprecision(6) << std::scientific;
for (const auto& [key, value]: optional) {
datafile_name << "_" << key << "=" << value;

View File

@ -7,6 +7,9 @@
#include <filesystem>
#include <vector>
#include "../motions/base.h"
#include "../times/base.h"
struct Arguments {
std::string parameter_file{};
bool ste = false;
@ -18,12 +21,12 @@ struct Arguments {
Arguments parse_args(int argc, char* argv[]);
std::pair<std::string, double> get_optional_parameter(std::vector<std::string>::const_iterator &it);
std::unordered_map<std::string, double> read_parameter(const std::filesystem::path&);
std::string make_directory(const Motion&, const Distribution&);
void save_parameter_to_file(const std::string&, const std::string&, const std::string&, std::unordered_map<std::string, double>&, std::unordered_map<std::string, double>&);
void save_data_to_file(const std::string&, const std::string&, const std::string&, const std::vector<double>&, const std::map<double, std::vector<double>>&, std::unordered_map<std::string, double>&);
void save_data_to_file(const std::string&, const std::string&, const std::string&, const std::vector<double>&, const std::vector<double>&, std::unordered_map<std::string, double>&);
void save_parameter_to_file(const std::string&, const std::string&, const std::unordered_map<std::string, double>&, const std::unordered_map<std::string, double>&);
void save_data_to_file(const std::string&, const std::string&, const std::vector<double>&, const std::map<double, std::vector<double>>&, const std::unordered_map<std::string, double>&);
void save_data_to_file(const std::string&, const std::string&, const std::vector<double>&, const std::vector<double>&, const std::unordered_map<std::string, double>&);
#endif