cpp/simulation/sims.cpp

184 lines
7.2 KiB
C++
Raw Normal View History

2024-11-11 10:03:02 +00:00
#include "sims.h"
#include "../motions/base.h"
#include "../times/base.h"
#include "../utils/functions.h"
#include "../utils/ranges.h"
#include "../utils/io.h"
2024-08-16 17:55:27 +00:00
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <chrono>
void run_spectrum(std::unordered_map<std::string, double>& parameter, Motion& motion, Distribution& dist) {
const int num_walker = static_cast<int>(parameter["num_walker"]);
// time axis for all time signals
2024-08-20 15:51:49 +00:00
const int num_acq = static_cast<int>(parameter["num_acq"]);
const std::vector<double> t_fid = arange(num_acq, parameter["dwell_time"]);
2024-08-16 17:55:27 +00:00
const std::vector<double> echo_times = linspace(parameter["techo_start"], parameter["techo_stop"], static_cast<int>(parameter["techo_steps"]));
2024-08-20 15:51:49 +00:00
// make timesignal vectors and set them to zero
2024-08-16 17:55:27 +00:00
std::map<double, std::vector<double>> fid_dict;
for (auto t_evo_i: echo_times) {
fid_dict[t_evo_i] = std::vector<double>(num_acq);
std::fill(fid_dict[t_evo_i].begin(), fid_dict[t_evo_i].end(), 0.);
}
2024-08-20 15:51:49 +00:00
// calculate min length of a trajectory
2024-08-16 17:55:27 +00:00
const double tmax = *std::max_element(echo_times.begin(), echo_times.end()) * 2 + t_fid.back();
2024-08-20 15:51:49 +00:00
// set parameter in distribution and motion model
2024-11-10 14:52:54 +00:00
const double tau = parameter.at("tau");
2024-08-20 15:51:49 +00:00
dist.setTau(tau);
2024-11-10 14:52:54 +00:00
motion.setParameters(parameter);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
const auto start = std::chrono::system_clock::now();
auto last_print_out = std::chrono::system_clock::now();
const time_t start_time = std::chrono::system_clock::to_time_t(start);
std::cout << "Start tau = " << tau << "s : " << ctime(&start_time);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
// let the walker walk
for (int mol_i = 0; mol_i < num_walker; mol_i++){
std::vector<double> traj_time{};
std::vector<double> traj_phase{};
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
make_trajectory(motion, dist, tmax, traj_time, traj_phase);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
for (auto& [t_echo_j, fid_j] : fid_dict) {
// get phase at echo pulse
int current_pos = nearest_index(traj_time, t_echo_j, 0);
const double phase_techo = lerp(traj_time, traj_phase, t_echo_j, current_pos);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
for (int acq_idx = 0; acq_idx < num_acq; acq_idx++) {
const double real_time = t_fid[acq_idx] + 2 * t_echo_j;
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
current_pos = nearest_index(traj_time, real_time, current_pos);
const double phase_acq = lerp(traj_time, traj_phase, real_time, current_pos);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
fid_j[acq_idx] += std::cos(phase_acq - 2 * phase_techo) / num_walker;
2024-08-16 17:55:27 +00:00
}
2024-08-20 15:51:49 +00:00
last_print_out = printSteps(last_print_out, start, num_walker, mol_i);
2024-08-16 17:55:27 +00:00
}
2024-08-20 15:51:49 +00:00
}
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
// write fid to files
fid_write_out("fid", t_fid, fid_dict, tau);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
const auto end = std::chrono::system_clock::now();
std::chrono::duration<float> duration = end - start;
const time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "End tau = " << tau << "s : " << ctime(&end_time);
std::cout << "Duration: " << duration.count() << "s\n" << std::endl;
2024-08-16 17:55:27 +00:00
}
2024-08-20 15:51:49 +00:00
2024-08-16 17:55:27 +00:00
void run_ste(std::unordered_map<std::string, double>& parameter, Motion& motion, Distribution& dist) {
const int num_walker = static_cast<int>(parameter[std::string("num_walker")]);
2024-08-20 15:51:49 +00:00
const int num_mix_times = static_cast<int>(parameter[std::string("tmix_steps")]);
2024-08-16 17:55:27 +00:00
const std::vector<double> evolution_times = linspace(parameter["tevo_start"], parameter["tevo_stop"], static_cast<int>(parameter["tevo_steps"]));
2024-08-20 15:51:49 +00:00
const std::vector<double> mixing_times = logspace(parameter["tmix_start"], parameter["tmix_stop"], num_mix_times);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
// make ste decay vectors and set them to zero
std::map<double, std::vector<double>> cc_dict;
std::map<double, std::vector<double>> ss_dict;
2024-08-16 17:55:27 +00:00
for (auto t_evo_i: evolution_times) {
2024-08-20 15:51:49 +00:00
cc_dict[t_evo_i] = std::vector<double>(num_mix_times);
ss_dict[t_evo_i] = std::vector<double>(num_mix_times);
std::fill(ss_dict[t_evo_i].begin(), ss_dict[t_evo_i].end(), 0.);
2024-08-16 17:55:27 +00:00
}
// each trajectory must have a duration of at least tmax
const double tmax = *std::max_element(evolution_times.begin(), evolution_times.end()) * 2 + *std::max_element(mixing_times.begin(), mixing_times.end());
2024-08-20 15:51:49 +00:00
// set parameter in distribution and motion model
2024-11-10 14:52:54 +00:00
const double tau = parameter.at("tau");
2024-08-20 15:51:49 +00:00
dist.setTau(tau);
2024-11-10 14:52:54 +00:00
motion.setParameters(parameter);
2024-08-20 15:51:49 +00:00
2024-08-18 11:21:27 +00:00
const auto start = std::chrono::system_clock::now();
2024-09-16 17:52:51 +00:00
auto last_print_out = std::chrono::system_clock::now();
2024-08-18 11:21:27 +00:00
const time_t start_time = std::chrono::system_clock::to_time_t(start);
std::cout << "Start tau = " << tau << "s : " << ctime(&start_time);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
// let the walker walk
2024-08-18 11:21:27 +00:00
for (int mol_i = 0; mol_i < num_walker; mol_i++){
std::vector<double> traj_time{};
std::vector<double> traj_phase{};
2024-08-16 17:55:27 +00:00
2024-08-18 11:21:27 +00:00
make_trajectory(motion, dist, tmax, traj_time, traj_phase);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
for (auto& [t_evo_j, _] : cc_dict) {
auto& cc_j = cc_dict[t_evo_j];
auto& ss_j = ss_dict[t_evo_j];
// get phase at beginning of mixing time
2024-08-18 11:21:27 +00:00
int current_pos = nearest_index(traj_time, t_evo_j, 0);
2024-08-20 15:51:49 +00:00
const double dephased = lerp(traj_time, traj_phase, t_evo_j, current_pos);
const double cc_tevo = std::cos(dephased);
const double ss_tevo = std::sin(dephased);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
for (int mix_idx = 0; mix_idx < num_mix_times; mix_idx++) {
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
// get phase at end of mixing time
const double time_end_mix = mixing_times[mix_idx] + t_evo_j;
current_pos = nearest_index(traj_time, time_end_mix, current_pos);
const double phase_mix_end = lerp(traj_time, traj_phase, time_end_mix, current_pos);
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
// get phase at echo position
const double time_echo = mixing_times[mix_idx] + 2 * t_evo_j;
current_pos = nearest_index(traj_time, time_echo, current_pos);
const double rephased = lerp(traj_time, traj_phase, time_echo, current_pos) - phase_mix_end;
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
cc_j[mix_idx] += cc_tevo * std::cos(rephased) / num_walker;
ss_j[mix_idx] += ss_tevo * std::sin(rephased) / num_walker;
2024-08-16 17:55:27 +00:00
}
}
2024-09-16 17:52:51 +00:00
last_print_out = printSteps(last_print_out, start, num_walker, mol_i);
2024-08-18 11:21:27 +00:00
}
2024-08-16 17:55:27 +00:00
2024-08-20 15:51:49 +00:00
// write to files
fid_write_out("coscos", mixing_times, cc_dict, tau);
fid_write_out("sinsin", mixing_times, ss_dict, tau);
2024-08-16 17:55:27 +00:00
2024-08-18 11:21:27 +00:00
const auto end = std::chrono::system_clock::now();
const std::chrono::duration<float> duration = end - start;
const time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "End tau = " << tau << "s : " << ctime(&end_time);
std::cout << "Duration: " << duration.count() << "s\n" << std::endl;
2024-08-16 17:55:27 +00:00
}
2024-09-16 17:52:51 +00:00
void make_trajectory(Motion& motion, Distribution& dist, const double t_max, std::vector<double>& out_time, std::vector<double>& out_phase) {
2024-08-16 17:55:27 +00:00
// Starting position
double t_passed = 0;
double phase = 0;
2024-08-18 11:21:27 +00:00
motion.initialize();
2024-09-16 17:52:51 +00:00
dist.initialize();
2024-08-18 11:21:27 +00:00
2024-08-16 17:55:27 +00:00
out_time.emplace_back(t_passed);
out_phase.emplace_back(0);
while (t_passed < t_max) {
const double t = dist.tau_wait();
t_passed += t;
phase += motion.jump() * t;
out_time.emplace_back(t_passed);
out_phase.emplace_back(phase);
}
}