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); } } }
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); } }
int main (int argc, char *argv[]) { int i, n_samples = 10; if(argc == 2){ n_particles = atoi(argv[1]); } else{printf("usage: %s <n_particles>\n", argv[0] );return 1;} ETA = 0.3; double tmp, x = 0.0f, err = 0.0f; printf("\nDIMENSION = %d\n\n", DIMENSION ); printf("N = %d\tETA = %f\n", n_particles, ETA ); for(i = 0; i < n_samples; i++){ init( ETA, 1.0f ); int t;for(t = 0; t < 50000; t++){ run(); if(t == 10000) reset(); } /* thermalization */ /* pressure */ tmp = SIGMA*dp/(2*get_kinetic_energy()*runtime); x += tmp; err += tmp*tmp; } char filename[64]; sprintf(filename,"data/%.2f_TL.dat",ETA); FILE *file = fopen(filename,"a"); x /= n_samples; err = sqrt((err/n_samples-x*x)/n_samples); fprintf(file, "%e\t%e\t%e\n", ETA, x, err ); fclose(file); return 0; }
static double get_invariant_nvt(const struct md *md) { struct nvt_data *data = (struct nvt_data *)md->data; double t_tau = cfg_get_double(md->state->cfg, "thermostat_tau"); double t_target = cfg_get_double(md->state->cfg, "temperature"); double kbt = BOLTZMANN * t_target; double t_virt = kbt * md->n_freedom * (data->chi_dt + data->chi * data->chi * t_tau * t_tau / 2.0); return md->potential_energy + get_kinetic_energy(md) + t_virt; }
double agent_body::get_sensor_value(agent_sensor_id const& sensor) const { switch(sensor) { case Position_X: return get_position().x; case Position_Y: return get_position().y; case WorldVelocity_X: return get_linear_velocity().x; case WorldVelocity_Y: return get_linear_velocity().y; case KineticEnergy: return get_kinetic_energy(); default: throw std::exception("Invalid Sensor Id"); } }
static double get_invariant_npt(const struct md *md) { struct npt_data *data = (struct npt_data *)md->data; double t_tau = cfg_get_double(md->state->cfg, "thermostat_tau"); double t_target = cfg_get_double(md->state->cfg, "temperature"); double p_tau = cfg_get_double(md->state->cfg, "barostat_tau"); double p_target = cfg_get_double(md->state->cfg, "pressure"); double kbt = BOLTZMANN * t_target; double volume = get_volume(md); double t_virt = kbt * md->n_freedom * (data->chi_dt + data->chi * data->chi * t_tau * t_tau / 2.0); double p_virt = p_target * volume + 3.0 * md->n_bodies * kbt * data->eta * data->eta * p_tau * p_tau / 2.0; return md->potential_energy + get_kinetic_energy(md) + t_virt + p_virt; }
size_t composite_body::initialize_state_value_bindings_(sv_bindings_t& bindings, sv_accessors_t& accessors) const { auto initial_count = bindings.size(); auto bound_id = accessors.size(); bindings[s_sv_names.left.at(StateValue::PositionX)] = bound_id++; accessors.push_back([this] { return get_com_position().x; }); bindings[s_sv_names.left.at(StateValue::PositionY)] = bound_id++; accessors.push_back([this] { return get_com_position().y; }); bindings[s_sv_names.left.at(StateValue::LinVelX)] = bound_id++; accessors.push_back([this] { return get_linear_velocity().x; }); bindings[s_sv_names.left.at(StateValue::LinVelY)] = bound_id++; accessors.push_back([this] { return get_linear_velocity().y; }); bindings[s_sv_names.left.at(StateValue::KE)] = bound_id++; accessors.push_back([this] { return get_kinetic_energy(); }); // TODO: what about joints added or removed dynamically? can these be supported? for(auto const& rev : m_revolutes) { rev.rev->initialize_state_value_bindings_(bindings, accessors, rev.name); } return bindings.size() - initial_count; }
Float HybridMonteCarlo::get_total_energy() const { return get_kinetic_energy()+get_potential_energy(); }
static double get_invariant_nve(const struct md *md) { return md->potential_energy + get_kinetic_energy(md); }
static double get_temperature(const struct md *md) { double ke = get_kinetic_energy(md); return 2.0 * ke / BOLTZMANN / md->n_freedom; }
static void time_integration_loop_hubble(Forceinfo *fi, Nbodyinfo *nb) { double zval; /* red shift at T=0 */ double tmpzz; double t, t0, dt1, tpresent; double W, K, E0, E, Q; double cm[3], cmv[3]; int step = 0; double tsnapout, timageout; t = 0.0; tpresent = 16.0; /* present time must be 16.0 */ zval = 24.0; tmpzz = pow(1.0/(zval+1), 3.0/2.0); t0 = tmpzz/(1.0-tmpzz)*tpresent; fi->eps = (zval+1)*pow(t0/(tpresent+t0), 2.0/3.0)*eps0; dt1 = dt0; tsnapout = dtsnapout; timageout = dtimageout; PR(t0, f); PR(dt0, f); PRL(dt1, f); PR(tstart,f); PR(tpresent,f); PR(dt,f); PRL(dtsnapout,f); PRL(dtimageout,f); if (cputime_flag) { vtc_turnon_cputime(); } vtc_init_cputime(); vtc_print_cputime("initial vtc_get_force start at"); #if DIRECT /* direct sum */ if (grape_flag) { vtc_set_scale(512.0, nb->m[0]); fi->calculator = GRAPE_FORCEONLY; vtc_get_force_direct(fi, nb); #if CALCPOTENTIAL fi->calculator = GRAPE_POTENTIALONLY; vtc_get_force_direct(fi, nb); fi->calculator = GRAPE_FORCEONLY; #endif } else { vtc_get_force_direct(fi, nb); } #else /* tree */ if (grape_flag) { fi->calculator = GRAPE_FORCEONLY; vtc_get_force_tree(fi, nb); #if CALCPOTENTIAL fi->calculator = GRAPE_POTENTIALONLY; vtc_get_force_tree(fi, nb); fi->calculator = GRAPE_FORCEONLY; #endif } else { vtc_get_force_tree(fi, nb); } #endif /* direct/tree */ vtc_print_cputime("initial vtc_get_force end at"); fprintf(stderr, "ninteraction: %e list_len_avg: %6.5f nwalk: %d\n", fi->ninteraction, (double)fi->ninteraction/nb->n, fi->nwalk); K = get_kinetic_energy(nb); #if CALCPOTENTIAL W = get_potential_energy(nb); E = E0 = W+K; PR(E0, f); PR(W, f); #endif PRL(K, f); plot_nbody(t, nb); /* calculate force at T=0 and quit */ if (snapout_flag && dtsnapout == 0.0) { static int nout = 0; char fn[255]; snapout_acc_flag = 1; sprintf(fn, snapoutfile, nout); PRL(fn, s); if (ionemo_flag) { write_binary(fn, t, nb); } else if (usepob_flag) { write_pob(fn, t, nb); } else { write_ascii(fn, t, nb); } vtc_print_cputime("exit at"); return; } while (t < tend) { if (fi->eps < eps) { double eps1; eps1 = (zval+1)*pow((t+t0)/(tpresent+t0), 2.0/3.0)*eps0; fi->eps = (eps1 < eps ? eps1 : eps); } else { fi->eps = eps; } if (dt1 < dt) { dt1 = dt0*(t+t0)/t0; } else { dt1 = dt; } if (step%outlogstep == outlogstep - 1 && cputime_flag) { vtc_turnon_cputime(); } else { vtc_turnoff_cputime(); } push_velocity(0.5*dt1, nb); push_position(dt1, nb); vtc_init_cputime(); #if DIRECT /* direct sum */ vtc_get_force_direct(fi, nb); #else /* tree */ vtc_get_force_tree(fi, nb); #endif /* direct/tree */ vtc_print_cputime("vtc_get_force end at"); push_velocity(0.5*dt1, nb); vtc_print_cputime("integration end at"); plot_nbody(t, nb); if (step%outlogstep == outlogstep - 1) { fprintf(stderr, "ninteraction: %e list_len_avg: %6.5f nwalk: %d\n", fi->ninteraction, fi->ninteraction/nb->n, fi->nwalk); K = get_kinetic_energy(nb); #if CALCPOTENTIAL if (grape_flag) { fi->calculator = GRAPE_POTENTIALONLY; #if DIRECT /* direct sum */ vtc_get_force_direct(fi, nb); #else /* tree */ vtc_get_force_tree(fi, nb); #endif /* direct/tree */ fi->calculator = GRAPE_FORCEONLY; } W = get_potential_energy(nb); E = W+K; Q = K/(K-E); fprintf(stderr, "\nT= %f K= %f Q= %f E= %f Eerr= %f Eabserr= %f\n", t, K, Q, E, (E0-E)/E0, E0-E); #else fprintf(stderr, "\nT= %f K= %f\n", t, K); #endif fprintf(stderr, "step: %d\n", step); fprintf(stderr, "T: %f current eps: %f dt: %f\n", t, fi->eps, dt1); get_cmterms(nb, cm, cmv); fprintf(stderr, "CM %15.13e %15.13e %15.13e\n", cm[0], cm[1], cm[2]); fprintf(stderr, "CMV %15.13e %15.13e %15.13e\n", cmv[0], cmv[1], cmv[2]); } if (tsnapout < t && snapout_flag) { static int nout = 0; char fn[255]; char cmd[255]; sprintf(fn, snapoutfile, nout); PRL(fn, s); if (ionemo_flag) { write_binary(fn, t, nb); } else if (usepob_flag) { write_pob(fn, t, nb); } else { write_ascii(fn, t, nb); } if (command_exec_flag) { sprintf(cmd, "%s %s %03d", command_name, fn, nout); fprintf(stderr, "execute %s\n", cmd); system(cmd); } nout++; PR(t, f); PRL(fn, s); tsnapout += dtsnapout; } if (timageout < t && imageout_flag) { static int nout = 0; char fn[255]; char cmd[255]; sprintf(fn, snapoutfile, nout); strcat(fn, ".gif"); PRL(fn, s); vtc_plotstar(nb->x, nb->n, t, image_scale, fn); nout++; PR(t, f); PRL(fn, s); timageout += dtimageout; } t += dt1; step++; vtc_print_cputime("exit at"); } /* main loop */ }
static void time_integration_loop(Forceinfo *fi, Nbodyinfo *nb) { int i, nstep; int outsnapstep, outimagestep; double W, K, E0, E, Q; double cm[3], cmv[3]; double t = tstart; nstep = (int)((tend-tstart)/dt+0.1); dt = (tend-tstart)/nstep; outsnapstep = (int) (dtsnapout/dt+0.1); outimagestep = (int) (dtimageout/dt+0.1); PR(tstart,f); PR(tend,f); PR(dt,f); PRL(dtsnapout,f); PRL(dtimageout,f); if (cputime_flag) { vtc_turnon_cputime(); } vtc_init_cputime(); vtc_print_cputime("initial vtc_get_force start at"); #if DIRECT /* direct sum */ if (grape_flag) { vtc_set_scale(512.0, nb->m[0]); #if MDGRAPE2 fi->calculator = GRAPE_FORCEONLY; #else fi->calculator = GRAPE; #endif /* MDGRAPE2 */ vtc_get_force_direct(fi, nb); #if CALCPOTENTIAL && MDGRAPE2 fi->calculator = GRAPE_POTENTIALONLY; vtc_get_force_direct(fi, nb); fi->calculator = GRAPE_FORCEONLY; #endif /* CALCPOTENTIAL && MDGRAPE2 */ } else { #if CALCPOTENTIAL fi->calculator = HOST; #endif /* CALCPOTENTIAL */ vtc_get_force_direct(fi, nb); } #else /* tree */ if (grape_flag) { #if NOFORCE fi->calculator = NOCALCULATOR; #elif MDGRAPE2 fi->calculator = GRAPE_FORCEONLY; #else fi->calculator = GRAPE; #endif /* NOFORCE */ vtc_get_force_tree(fi, nb); #if CALCPOTENTIAL && MDGRAPE2 fi->calculator = GRAPE_POTENTIALONLY; vtc_get_force_tree(fi, nb); fi->calculator = GRAPE_FORCEONLY; #endif /* CALCPOTENTIAL && MDGRAPE2 */ } else { #if CALCPOTENTIAL fi->calculator = HOST; #endif /* CALCPOTENTIAL */ vtc_get_force_tree(fi, nb); } #endif vtc_print_cputime("initial vtc_get_force end at"); fprintf(stderr, "ninteraction: %e list_len_avg: %6.5f nwalk: %d\n", fi->ninteraction, (double)fi->ninteraction/nb->n, fi->nwalk); K = get_kinetic_energy(nb); #if CALCPOTENTIAL W = get_potential_energy(nb); E = E0 = W+K; PR(E0, 20.17f); PR(W, 20.17f); #endif PRL(K, f); plot_nbody(t, nb); /* calculate force at T=0 and quit */ if (snapout_flag && dtsnapout == 0.0) { static int nout = 0; char fn[255]; snapout_acc_flag = 1; sprintf(fn, snapoutfile, nout); PRL(fn, s); if (ionemo_flag) { write_binary(fn, t, nb); } else if (usepob_flag) { write_pob(fn, t, nb); } else { write_ascii(fn, t, nb); } vtc_print_cputime("exit at"); #if USE_GM_API fprintf(stderr, "will m2_gm_finalize...\n"); m2_gm_finalize(); fprintf(stderr, "done m2_gm_finalize.\n"); #endif /* USE_GM_API */ } for (i = 0; i < nstep; i++) { if (i%outlogstep == outlogstep - 1 && cputime_flag) { vtc_turnon_cputime(); } else { vtc_turnoff_cputime(); } fprintf(stderr, "step %d\n", i); push_velocity(0.5*dt, nb); push_position(dt, nb); t = tstart+(i+1)*dt; vtc_init_cputime(); #if DIRECT /* direct sum */ vtc_get_force_direct(fi, nb); #else /* tree */ vtc_get_force_tree(fi, nb); #endif vtc_print_cputime("vtc_get_force end at"); push_velocity(0.5*dt, nb); vtc_print_cputime("integration end at"); plot_nbody(t, nb); if (i%outlogstep == outlogstep - 1) { fprintf(stderr, "ninteraction: %e list_len_avg: %6.5f nwalk: %d\n", fi->ninteraction, fi->ninteraction/nb->n, fi->nwalk); K = get_kinetic_energy(nb); #if CALCPOTENTIAL #if MDGRAPE2 if (grape_flag) { fi->calculator = GRAPE_POTENTIALONLY; #if DIRECT /* direct sum */ vtc_get_force_direct(fi, nb); #else /* tree */ vtc_get_force_tree(fi, nb); #endif /* direct/tree */ fi->calculator = GRAPE_FORCEONLY; } #endif /* MDGRAPE2 */ W = get_potential_energy(nb); E = W+K; Q = K/(K-E); fprintf(stderr, "\nT= %f K= %20.17f Q= %20.17f E= %20.17f Eerr= %20.17f Eabserr= %20.17f\n", t, K, Q, E, (E0-E)/E0, E0-E); #else /* CALCPOTENTIAL */ fprintf(stderr, "\nT= %f K= %20.17f\n", t, K); #endif /* CALCPOTENTIAL */ fprintf(stderr, "step: %d\n", i); get_cmterms(nb, cm, cmv); fprintf(stderr, "CM %15.13e %15.13e %15.13e\n", cm[0], cm[1], cm[2]); fprintf(stderr, "CMV %15.13e %15.13e %15.13e\n", cmv[0], cmv[1], cmv[2]); } if (i%outsnapstep == outsnapstep - 1 && snapout_flag) { static int nout = 0; char fn[255]; char cmd[255]; sprintf(fn, snapoutfile, nout); PRL(fn, s); if (ionemo_flag) { write_binary(fn, t, nb); } else if (usepob_flag) { write_pob(fn, t, nb); } else { write_ascii(fn, t, nb); } if (command_exec_flag) { sprintf(cmd, "%s %s %03d", command_name, fn, nout); fprintf(stderr, "execute %s\n", cmd); system(cmd); } nout++; PR(t, f); PRL(fn, s); } if (i%outimagestep == outimagestep - 1 && imageout_flag) { static int nout = 0; char fn[255]; char cmd[255]; char msg[255]; double center[2]; sprintf(fn, snapoutfile, nout); strcat(fn, ".gif"); PRL(fn, s); sprintf(msg, "t: %5.3f", t); center[0] = 0.0; center[1] = 0.0; // vtc_plotstar(fn, nb, msg, image_scale, center, 1e10, NULL, NULL); // vtc_plotstar(nb->x, nb->m, nb->n, t, image_scale, fn); nout++; PR(t, f); PRL(fn, s); } vtc_print_cputime("exit at"); } /* i loop */ }
int main() { int i; dt = 1e-13; H = 5e4; t_max = 500.0*100.*dt; Lx = Ly = Lz = pow(50.0/1e10,0.333333); assert(N % 2 == 0 ); srand(time(NULL)); rx[0] = 0.5 * Lx + 100.0 * 100.0 * a0; vx[0] = 0.0; ry[0] = 0.5 * Ly; vy[0] = sqrt(e*e / (me * 100.0 * 100.0 * a0)); rz[0] = 0.5 * Lz; vz[0] = 0.0; m[0] = me; q[0] = -e; type[0] = 1; rx[1] = 0.5 * Lx; vx[1] = 0.0; ry[1] = 0.5 * Ly; vy[1] = 0.0; rz[1] = 0.5 * Lz; vz[1] = 0.0; m[1] = mp; q[1] = e; type[1] = 2; U = K = KelXY = KelZ = KpXY = KpZ = 0.0; for(i = 0; i < N; ++i) {get_kinetic_energy(i);} update_forces(); E = K + U; initK = K; initU = U; initE = E; int st = 0; double t; double dx, dy, dz; FILE* f_en = fopen("energy","w"); FILE* f_pos = fopen("part_pos", "w"); get_distance_vector(0, 1, &dx, &dy, &dz); fprintf(f_pos,"%d\t%g\t%g\t%g\t%g\t%g\n",st, rx[0]/Lx, ry[0]/Ly, rx[1]/Lx, ry[1]/Ly, sqrt(dx*dx + dy*dy + dz*dz)); fprintf(f_en,"%d\t%g\t%g\t%g\n", st, E, initE, (1.0 - fabs(E/initE))); for(t = 0.0; t < t_max; t += dt) { U = K = KelXY = KelZ = KpXY = KpZ = 0.0; /*VV_step(dt);*/ B_step(dt); E = U + K; st++; get_distance_vector(0, 1, &dx, &dy, &dz); fprintf(f_pos,"%d\t%g\t%g\t%g\t%g\t%g\n",st, rx[0]/Lx, ry[0]/Ly, rx[1]/Lx, ry[1]/Ly, sqrt(dx*dx + dy*dy + dz*dz)); fprintf(f_en,"%d\t%g\t%g\t%g\n", st, E, initE, (1.0 - fabs(E/initE))); if(initE != 0.0 && fabs(initE - E) > 0.1 * fabs(initE)) { fprintf(stderr,"!\n"); return EXIT_FAILURE; } } return EXIT_SUCCESS; }