void MolecularDynamics::assign_velocities(Float temperature) { ParticleIndexes ips = get_simulation_particle_indexes(); setup_degrees_of_freedom(ips); ParticlesTemp ps = IMP::internal::get_particle(get_model(), ips); boost::normal_distribution<Float> mrng(0., 1.); boost::variate_generator<RandomNumberGenerator &, boost::normal_distribution<Float> > sampler(random_number_generator, mrng); for (ParticlesTemp::iterator iter = ps.begin(); iter != ps.end(); ++iter) { Particle *p = *iter; LinearVelocity(p).set_velocity(algebra::Vector3D(sampler(), sampler(), sampler())); } Float rescale = sqrt(temperature / get_kinetic_temperature(get_kinetic_energy())); for (ParticlesTemp::iterator iter = ps.begin(); iter != ps.end(); ++iter) { Particle *p = *iter; LinearVelocity v(p); algebra::Vector3D velocity = v.get_velocity(); velocity *= rescale; v.set_velocity(velocity); } }
void MolecularDynamics::assign_velocities(Float temperature) { ParticleIndexes ips=get_simulation_particle_indexes(); setup_degrees_of_freedom(ips); ParticlesTemp ps= IMP::internal::get_particle(get_model(), ips); boost::normal_distribution<Float> mrng(0., 1.); boost::variate_generator<RandomNumberGenerator&, boost::normal_distribution<Float> > sampler(random_number_generator, mrng); for (ParticlesTemp::iterator iter = ps.begin(); iter != ps.end(); ++iter) { Particle *p = *iter; for (int i = 0; i < 3; ++i) { p->set_value(vs_[i], sampler()); } } Float rescale = sqrt(temperature/ get_kinetic_temperature(get_kinetic_energy())); for (ParticlesTemp::iterator iter = ps.begin(); iter != ps.end(); ++iter) { Particle *p = *iter; for (int i = 0; i < 3; ++i) { Float velocity = p->get_value(vs_[i]); velocity *= rescale; p->set_value(vs_[i], velocity); } } }
double Simulator::do_simulate(double time) { IMP_FUNCTION_LOG; set_was_used(true); ParticleIndexes ps = get_simulation_particle_indexes(); setup(ps); double target = current_time_ + time; boost::scoped_ptr<boost::progress_display> pgs; if (get_log_level() == PROGRESS) { pgs.reset(new boost::progress_display(time / max_time_step_)); } while (current_time_ < target) { last_time_step_ = do_step(ps, max_time_step_); current_time_ += last_time_step_; update_states(); if (get_log_level() == PROGRESS) { ++(*pgs); } } return Optimizer::get_scoring_function()->evaluate(false); }
double Simulator::do_simulate_wave(double time, double max_time_step_factor, double base) { IMP_FUNCTION_LOG; set_was_used(true); ParticleIndexes ps = get_simulation_particle_indexes(); setup(ps); double target = current_time_ + time; IMP_USAGE_CHECK(max_time_step_factor > 1.0, "simulate wave time step factor must be >1.0"); // build wave into cur_ts double wave_max_ts = max_time_step_factor * max_time_step_; std::vector<double> ts_seq; { int n_half = 2; // subwave length bool max_reached = false; double raw_wave_time = 0.0; do { double cur_ts = max_time_step_; // go up for (int i = 0; i < n_half; i++) { ts_seq.push_back(cur_ts); raw_wave_time += cur_ts; cur_ts *= base; if (cur_ts > wave_max_ts) { max_reached = true; break; } } // go down for (int i = 0; i < n_half; i++) { cur_ts /= base; ts_seq.push_back(cur_ts); raw_wave_time += cur_ts; if (cur_ts < max_time_step_) { break; } } n_half++; } while (!max_reached && raw_wave_time < time); // adjust to fit into time precisely unsigned int n = (unsigned int)std::ceil(time / raw_wave_time); double wave_time = time / n; double adjust = wave_time / raw_wave_time; IMP_LOG(PROGRESS, "Wave time step seq: "); for (unsigned int i = 0; i < ts_seq.size(); i++) { ts_seq[i] *= adjust; IMP_LOG(PROGRESS, ts_seq[i] << ", "); } IMP_LOG(PROGRESS, std::endl); } unsigned int i = 0; unsigned int k = ts_seq.size(); // n waves of k frames int orig_nf_left = (int)(time / max_time_step_); while (current_time_ < target) { last_time_step_ = do_step(ps, ts_seq[i++ % k]); current_time_ += last_time_step_; // emulate state updating by frames for the origin max_time_step // (for periodic optimizers) int nf_left = (int)((target - current_time_) / max_time_step_); while (orig_nf_left >= nf_left) { IMP_LOG(PROGRESS, "Updating states: " << orig_nf_left << "," << nf_left << " target time " << target << " current time " << current_time_ << std::endl); update_states(); // needs to move orig_nf_left--; } } IMP_LOG(PROGRESS, "Simulated for " << i << " actual frames with waves of " << k << " frames each" << std::endl); IMP_USAGE_CHECK(current_time_ >= target - 0.001 * max_time_step_, "simulations did not advance to target time for some reason"); return Optimizer::get_scoring_function()->evaluate(false); }
ParticlesTemp Simulator::get_simulation_particles() const { ParticleIndexes p = get_simulation_particle_indexes(); return IMP::internal::get_particle(get_model(), p); }