Create dynamics interface to combine motion and dist
This commit is contained in:
41
src/sims.cpp
41
src/sims.cpp
@@ -1,5 +1,6 @@
|
||||
#include "sims.h"
|
||||
#include "utils/functions.h"
|
||||
#include "utils/io.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
@@ -10,14 +11,12 @@ void run_simulation(
|
||||
Experiment& experiment,
|
||||
std::unordered_map<std::string, double>& parameter,
|
||||
std::unordered_map<std::string, double>& optional,
|
||||
motions::BaseMotion& motion,
|
||||
times::BaseDistribution& dist,
|
||||
Dynamics& dynamics,
|
||||
std::mt19937_64& rng
|
||||
) {
|
||||
const int num_walker = static_cast<int>(parameter["num_walker"]);
|
||||
|
||||
dist.setParameters(parameter);
|
||||
motion.setParameters(parameter);
|
||||
dynamics.setParameters(parameter);
|
||||
experiment.setup(parameter, optional);
|
||||
|
||||
const auto start = printStart(optional);
|
||||
@@ -31,13 +30,11 @@ void run_simulation(
|
||||
thread_rngs.emplace_back(rng());
|
||||
}
|
||||
|
||||
// Create per-thread clones of motion, distribution, and experiment
|
||||
std::vector<std::unique_ptr<motions::BaseMotion>> thread_motions;
|
||||
std::vector<std::unique_ptr<times::BaseDistribution>> thread_dists;
|
||||
// Create per-thread clones of dynamics and experiment
|
||||
std::vector<std::unique_ptr<Dynamics>> thread_dynamics;
|
||||
std::vector<std::unique_ptr<Experiment>> thread_experiments;
|
||||
for (int i = 0; i < num_threads; i++) {
|
||||
thread_motions.push_back(motion.clone());
|
||||
thread_dists.push_back(dist.clone());
|
||||
thread_dynamics.push_back(dynamics.clone());
|
||||
thread_experiments.push_back(experiment.clone());
|
||||
}
|
||||
|
||||
@@ -48,14 +45,13 @@ void run_simulation(
|
||||
{
|
||||
const int tid = omp_get_thread_num();
|
||||
auto& local_rng = thread_rngs[tid];
|
||||
auto& local_motion = *thread_motions[tid];
|
||||
auto& local_dist = *thread_dists[tid];
|
||||
auto& local_dynamics = *thread_dynamics[tid];
|
||||
auto& local_experiment = *thread_experiments[tid];
|
||||
|
||||
#pragma omp for schedule(static)
|
||||
for (int mol_i = 0; mol_i < num_walker; mol_i++) {
|
||||
auto traj = make_trajectory(local_motion, local_dist, experiment.tmax(), local_rng);
|
||||
local_experiment.accumulate(traj, local_motion.getInitOmega(), num_walker);
|
||||
auto traj = make_trajectory(local_dynamics, experiment.tmax(), local_rng);
|
||||
local_experiment.accumulate(traj, local_dynamics.getInitOmega(), num_walker);
|
||||
|
||||
if (tid == 0) {
|
||||
#pragma omp atomic
|
||||
@@ -73,24 +69,23 @@ void run_simulation(
|
||||
experiment.merge(*thread_experiments[i]);
|
||||
}
|
||||
|
||||
experiment.save(motion, dist);
|
||||
const auto directory = make_directory(dynamics.toString());
|
||||
experiment.save(directory);
|
||||
printEnd(start);
|
||||
}
|
||||
|
||||
|
||||
Trajectory make_trajectory(
|
||||
motions::BaseMotion& motion,
|
||||
times::BaseDistribution& dist,
|
||||
Dynamics& dynamics,
|
||||
const double t_max,
|
||||
std::mt19937_64& rng
|
||||
) {
|
||||
double t_passed = 0;
|
||||
double phase = 0;
|
||||
|
||||
motion.initialize(rng);
|
||||
dist.initialize(rng);
|
||||
dynamics.initialize(rng);
|
||||
|
||||
double omega = motion.getInitOmega();
|
||||
double omega = dynamics.getInitOmega();
|
||||
|
||||
Trajectory traj;
|
||||
traj.time.emplace_back(t_passed);
|
||||
@@ -98,10 +93,10 @@ Trajectory make_trajectory(
|
||||
traj.omega.emplace_back(omega);
|
||||
|
||||
while (t_passed < t_max) {
|
||||
const double t = dist.tau_wait(rng);
|
||||
t_passed += t;
|
||||
omega = motion.jump(rng);
|
||||
phase += omega * t;
|
||||
auto [dt, new_omega] = dynamics.next(rng);
|
||||
phase += omega * dt;
|
||||
t_passed += dt;
|
||||
omega = new_omega;
|
||||
|
||||
traj.time.emplace_back(t_passed);
|
||||
traj.phase.emplace_back(phase);
|
||||
|
||||
Reference in New Issue
Block a user