def test_velocity_traj(): # Test Trajectory.get_velocity() against velocities output from CP2K. The # agreement is very good. Works only for fixed-cell MDs, however! dr = 'files/cp2k/md/nvt_print_low' base = os.path.dirname(dr) fn = '%s/cp2k.out' %dr print common.backtick('tar -C {0} -xzf {1}.tgz'.format(base,dr)) tr = io.read_cp2k_md(fn) # read from data file v1 = tr.velocity.copy() # If tr.velocity != None, then get_velocity() doesn't calculate it. Then, # it simply returns tr.velocity, which is what we of course usually want. tr.velocity = None # calculate from coords + time step, b/c of central diffs, only steps 1:-1 # are the same v2 = tr.get_velocity() print ">>>> np.abs(v1).max()", np.abs(v1).max() print ">>>> np.abs(v1).min()", np.abs(v1).min() print ">>>> np.abs(v2).max()", np.abs(v2).max() print ">>>> np.abs(v2).min()", np.abs(v2).min() print ">>>> np.abs(v1-v2).max()", np.abs(v1-v2).max() print ">>>> np.abs(v1-v2).min()", np.abs(v1-v2).min() assert np.allclose(v1[1:-1,...], v2[1:-1,...], atol=1e-4) ##from pwtools import mpl ##fig,ax = mpl.fig_ax() ##ax.plot(v1[1:-1,:,0], 'b') ##ax.plot(v2[1:-1,:,0], 'r') ##mpl.plt.show() shape = (100,10,3) arr = np.random.rand(*shape) assert crys.velocity_traj(arr, axis=0).shape == shape assert crys.velocity_traj(arr, axis=0, endpoints=False).shape == (98,10,3)
def test_velocity_traj(): # Test Trajectory.get_velocity() against velocities output from CP2K. The # agreement is very good. Works only for fixed-cell MDs, however! dr = 'files/cp2k/md/nvt_print_low' base = os.path.dirname(dr) fn = '%s/cp2k.out' %dr print(common.backtick('tar -C {0} -xzf {1}.tgz'.format(base,dr))) tr = io.read_cp2k_md(fn) # read from data file v1 = tr.velocity.copy() # If tr.velocity != None, then get_velocity() doesn't calculate it. Then, # it simply returns tr.velocity, which is what we of course usually want. tr.velocity = None # calculate from coords + time step, b/c of central diffs, only steps 1:-1 # are the same v2 = tr.get_velocity() print(">>>> np.abs(v1).max()", np.abs(v1).max()) print(">>>> np.abs(v1).min()", np.abs(v1).min()) print(">>>> np.abs(v2).max()", np.abs(v2).max()) print(">>>> np.abs(v2).min()", np.abs(v2).min()) print(">>>> np.abs(v1-v2).max()", np.abs(v1-v2).max()) print(">>>> np.abs(v1-v2).min()", np.abs(v1-v2).min()) assert np.allclose(v1[1:-1,...], v2[1:-1,...], atol=1e-4) ##from pwtools import mpl ##fig,ax = mpl.fig_ax() ##ax.plot(v1[1:-1,:,0], 'b') ##ax.plot(v2[1:-1,:,0], 'r') ##mpl.plt.show() shape = (100,10,3) arr = np.random.rand(*shape) assert crys.velocity_traj(arr, axis=0).shape == shape assert crys.velocity_traj(arr, axis=0, endpoints=False).shape == (98,10,3)
def pdos(coords_arr_3d, axis=0): f, d = pd.direct_pdos(velocity_traj(coords_arr_3d, axis=axis)) return d
# Use most settings (nfreq, ...) from above. Create random array of x,y,z time # traces for `natoms` atoms. Each x,y,z trajectory is a sum of sin's (`coords` # in the 1d case). natoms = 5 coords = np.empty((nstep, natoms, 3)) # `nfreq` frequencies for each x,y,z component of each atom freqs = rand(natoms, 3, nfreq) * fmax for i in range(natoms): for k in range(3): # vector w/ frequencies: freqs[i,k,:] <=> f_j, j=0, ..., nfreq-1 # sum_j sin(2*pi*f_j*t) coords[:, i, k] = np.sin(2 * pi * freqs[i, k, :][:, None] * taxis).sum(axis=0) ##arr = coords arr = crys.velocity_traj(coords) massvec = rand(natoms) # no mass weighting M = None f4, y4n = pydos.vacf_pdos(arr, dt=dt, m=M, mirr=True) f5, y5n = pydos.direct_pdos(arr, dt=dt, m=M) # with mass weighting M = massvec f6, y6nm = pydos.vacf_pdos(arr, dt=dt, m=M, mirr=True) f7, y7nm = pydos.direct_pdos(arr, dt=dt, m=M) if use_fourier: # For each atom, write an array (time.shape[0], 3) with coords at all time # steps, run fourier.x on that, sum up the power spectra. No mass
# Use most settings (nfreq, ...) from above. Create random array of x,y,z time # traces for `natoms` atoms. Each x,y,z trajectory is a sum of sin's (`coords` # in the 1d case). natoms = 5 coords = np.empty((nstep, natoms, 3)) # `nfreq` frequencies for each x,y,z component of each atom freqs = rand(natoms, 3, nfreq)*fmax for i in range(natoms): for k in range(3): # vector w/ frequencies: freqs[i,k,:] <=> f_j, j=0, ..., nfreq-1 # sum_j sin(2*pi*f_j*t) coords[:,i,k] = np.sin(2*pi*freqs[i,k,:][:,None]*taxis).sum(axis=0) ##arr = coords arr = crys.velocity_traj(coords) massvec = rand(natoms) # no mass weighting M = None f4, y4n = pydos.vacf_pdos(arr, dt=dt, m=M, mirr=True) f5, y5n = pydos.direct_pdos(arr, dt=dt, m=M) # with mass weighting M = massvec f6, y6nm = pydos.vacf_pdos(arr, dt=dt, m=M, mirr=True) f7, y7nm = pydos.direct_pdos(arr, dt=dt, m=M) if use_fourier: # For each atom, write an array (time.shape[0], 3) with coords at all time # steps, run fourier.x on that, sum up the power spectra. No mass