def test_msd_global_temp(self): """Tests diffusion via MSD for global gamma and temperature""" gamma = 9.4 kT = 0.37 dt = 0.5 system = self.system system.part.clear() p = system.part.add(pos=(0, 0, 0), id=0) system.time_step = dt system.thermostat.set_brownian(kT=kT, gamma=gamma, seed=41) system.cell_system.skin = 0.4 pos_obs = ParticlePositions(ids=(p.id, )) c_pos = Correlator(obs1=pos_obs, tau_lin=16, tau_max=100., delta_N=1, corr_operation="square_distance_componentwise", compress1="discard1") system.auto_update_accumulators.add(c_pos) system.integrator.run(30000) c_pos.finalize() # Check MSD msd = c_pos.result() tau = c_pos.lag_times() system.auto_update_accumulators.clear() def expected_msd(x): return 2. * kT / gamma * x for i in range(2, 6): np.testing.assert_allclose(msd[i], expected_msd(tau[i]), rtol=0.02)
# Integrate 5,000,000 steps. This can be done in one go as well. for i in range(SAMP_STEPS): if (i + 1) % 100 == 0: print('\rrun %i: %.0f%%' % (run + 1, (i + 1) * 100. / SAMP_STEPS), end='', flush=True) system.integrator.run(SAMP_LENGTH) print() # Finalize the accumulators and write to disk system.auto_update_accumulators.remove(msd) msd.finalize() np.savetxt( "{}/msd_{}_{}.dat".format(outdir, vel, run), np.column_stack( (msd.lag_times(), msd.sample_sizes(), msd.result().reshape([-1, 3])))) system.auto_update_accumulators.remove(vacf) vacf.finalize() np.savetxt( "{}/vacf_{}_{}.dat".format(outdir, vel, run), np.column_stack((vacf.lag_times(), vacf.sample_sizes(), vacf.result().reshape([-1, 1])))) system.auto_update_accumulators.remove(avacf) avacf.finalize() np.savetxt( "{}/avacf_{}_{}.dat".format(outdir, vel, run), np.column_stack((avacf.lag_times(), avacf.sample_sizes(), avacf.result().reshape([-1, 1]))))
c_vel = Correlator(obs1=vel_obs, tau_lin=16, tau_max=20., delta_N=1, corr_operation="scalar_product", compress1="discard1") system.auto_update_accumulators.add(c_pos) system.auto_update_accumulators.add(c_vel) system.integrator.run(1000000) c_pos.finalize() c_vel.finalize() msd = np.column_stack( (c_pos.lag_times(), c_pos.sample_sizes(), c_pos.result().reshape([-1, 3]))) vacf = np.column_stack( (c_vel.lag_times(), c_vel.sample_sizes(), c_vel.result().reshape([-1, 1]))) np.savetxt("msd.dat", msd) np.savetxt("vacf.dat", vacf) # Integral of vacf via Green-Kubo # D= 1/3 int_0^infty <v(t_0)v(t_0+t)> dt # Integrate with trapezoidal rule I = np.trapz(vacf[:, 2], vacf[:, 0]) ratio = 1. / 3. * I / (kT / gamma) print("Ratio of measured and expected diffusion coefficients from Green-Kubo:", ratio)