Use unique_ptr instead of raw pointer
This commit is contained in:
75
src/sims.cpp
75
src/sims.cpp
@@ -19,7 +19,8 @@ void run_spectrum(
|
||||
std::unordered_map<std::string, double>& parameter,
|
||||
std::unordered_map<std::string, double> optional,
|
||||
motions::BaseMotion& motion,
|
||||
times::BaseDistribution& dist
|
||||
times::BaseDistribution& dist,
|
||||
std::mt19937_64& rng
|
||||
) {
|
||||
const int num_walker = static_cast<int>(parameter["num_walker"]);
|
||||
|
||||
@@ -47,23 +48,19 @@ void run_spectrum(
|
||||
|
||||
// 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{};
|
||||
std::vector<double> traj_omega{};
|
||||
|
||||
make_trajectory(motion, dist, tmax, traj_time, traj_phase, traj_omega);
|
||||
auto traj = make_trajectory(motion, dist, tmax, rng);
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
|
||||
for (int acq_idx = 0; acq_idx < num_acq; acq_idx++) {
|
||||
const double real_time = t_fid[acq_idx] + 2 * t_echo_j;
|
||||
|
||||
current_pos = nearest_index(traj_time, real_time, current_pos);
|
||||
const double phase_acq = lerp(traj_time, traj_phase, real_time, current_pos);
|
||||
current_pos = nearest_index(traj.time, real_time, current_pos);
|
||||
const double phase_acq = lerp(traj.time, traj.phase, real_time, current_pos);
|
||||
|
||||
fid_j[acq_idx] += std::cos(phase_acq - 2 * phase_techo) / num_walker;
|
||||
}
|
||||
@@ -84,7 +81,8 @@ void run_ste(
|
||||
std::unordered_map<std::string, double>& parameter,
|
||||
std::unordered_map<std::string, double> optional,
|
||||
motions::BaseMotion& motion,
|
||||
times::BaseDistribution& dist
|
||||
times::BaseDistribution& dist,
|
||||
std::mt19937_64& rng
|
||||
) {
|
||||
const int num_walker = static_cast<int>(parameter[std::string("num_walker")]);
|
||||
|
||||
@@ -116,17 +114,13 @@ void run_ste(
|
||||
|
||||
// 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{};
|
||||
std::vector<double> traj_omega{};
|
||||
|
||||
make_trajectory(motion, dist, tmax, traj_time, traj_phase, traj_omega);
|
||||
auto traj = make_trajectory(motion, dist, tmax, rng);
|
||||
|
||||
int f2_pos = 0;
|
||||
for (int f2_idx=0; f2_idx < num_mix_times; f2_idx++) {
|
||||
const double t_mix_f2 = mixing_times[f2_idx];
|
||||
f2_pos = nearest_index(traj_time, t_mix_f2, f2_pos);
|
||||
f2[f2_idx] += traj_omega[f2_pos] * motion.getInitOmega() / num_walker;
|
||||
f2_pos = nearest_index(traj.time, t_mix_f2, f2_pos);
|
||||
f2[f2_idx] += traj.omega[f2_pos] * motion.getInitOmega() / num_walker;
|
||||
}
|
||||
|
||||
|
||||
@@ -135,26 +129,26 @@ void run_ste(
|
||||
auto& ss_j = ss_dict[t_evo_j];
|
||||
|
||||
// get phase at beginning of mixing time
|
||||
int current_pos = nearest_index(traj_time, t_evo_j, 0);
|
||||
const double dephased = lerp(traj_time, traj_phase, t_evo_j, current_pos);
|
||||
int current_pos = nearest_index(traj.time, t_evo_j, 0);
|
||||
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);
|
||||
|
||||
for (int mix_idx = 0; mix_idx < num_mix_times; mix_idx++) {
|
||||
// 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);
|
||||
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);
|
||||
|
||||
// get phase at position of 4th pulse
|
||||
const double time_pulse4 = time_end_mix + tpulse4;
|
||||
current_pos = nearest_index(traj_time, time_pulse4, current_pos);
|
||||
const double phase_4pulse = lerp(traj_time, traj_phase, time_pulse4, current_pos);
|
||||
current_pos = nearest_index(traj.time, time_pulse4, current_pos);
|
||||
const double phase_4pulse = lerp(traj.time, traj.phase, time_pulse4, current_pos);
|
||||
|
||||
// get phase at echo position
|
||||
const double time_echo = time_pulse4 + tpulse4 + t_evo_j;
|
||||
current_pos = nearest_index(traj_time, time_echo, current_pos);
|
||||
double rephased = lerp(traj_time, traj_phase, time_echo, current_pos) + phase_mix_end - 2*phase_4pulse;
|
||||
current_pos = nearest_index(traj.time, time_echo, current_pos);
|
||||
double rephased = lerp(traj.time, traj.phase, time_echo, current_pos) + phase_mix_end - 2*phase_4pulse;
|
||||
|
||||
cc_j[mix_idx] += cc_tevo * std::cos(rephased) / num_walker;
|
||||
ss_j[mix_idx] += ss_tevo * std::sin(rephased) / num_walker;
|
||||
@@ -174,37 +168,38 @@ void run_ste(
|
||||
}
|
||||
|
||||
|
||||
void make_trajectory(
|
||||
Trajectory make_trajectory(
|
||||
motions::BaseMotion& motion,
|
||||
times::BaseDistribution& dist,
|
||||
const double t_max,
|
||||
std::vector<double>& out_time,
|
||||
std::vector<double>& out_phase,
|
||||
std::vector<double>& out_omega
|
||||
std::mt19937_64& rng
|
||||
) {
|
||||
// Starting position
|
||||
double t_passed = 0;
|
||||
double phase = 0;
|
||||
|
||||
motion.initialize();
|
||||
dist.initialize();
|
||||
motion.initialize(rng);
|
||||
dist.initialize(rng);
|
||||
|
||||
double omega = motion.getInitOmega();
|
||||
|
||||
out_time.emplace_back(t_passed);
|
||||
out_phase.emplace_back(phase);
|
||||
out_omega.emplace_back(omega);
|
||||
Trajectory traj;
|
||||
traj.time.emplace_back(t_passed);
|
||||
traj.phase.emplace_back(phase);
|
||||
traj.omega.emplace_back(omega);
|
||||
|
||||
while (t_passed < t_max) {
|
||||
const double t = dist.tau_wait();
|
||||
const double t = dist.tau_wait(rng);
|
||||
t_passed += t;
|
||||
omega = motion.jump();
|
||||
omega = motion.jump(rng);
|
||||
phase += omega * t;
|
||||
|
||||
out_time.emplace_back(t_passed);
|
||||
out_phase.emplace_back(phase);
|
||||
out_omega.emplace_back(omega);
|
||||
traj.time.emplace_back(t_passed);
|
||||
traj.phase.emplace_back(phase);
|
||||
traj.omega.emplace_back(omega);
|
||||
}
|
||||
|
||||
return traj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user