def angles(struct, pbc=False, mask_val=999.0, deg=True): """Python implementation of angles.""" nang = struct.natoms*(struct.natoms-1)*(struct.natoms-2) norm = np.linalg.norm anglesijk = np.ones((struct.natoms,)*3, dtype=float)*mask_val angleidx = np.array([x for x in permutations(range(struct.natoms),3)]) for ijk in angleidx: ii,jj,kk = ijk ci = struct.coords_frac[ii,:] cj = struct.coords_frac[jj,:] ck = struct.coords_frac[kk,:] dvij = ci - cj dvik = ci - ck if pbc: dvij = np.dot(crys.min_image_convention(dvij), struct.cell) dvik = np.dot(crys.min_image_convention(dvik), struct.cell) else: dvij = np.dot(dvij, struct.cell) dvik = np.dot(dvik, struct.cell) cang = np.dot(dvij, dvik) / norm(dvij) / norm(dvik) ceps = 1.0-2.2e-16 if cang > ceps: cang = 1.0 elif cang < -ceps: cang = -1.0 if deg: anglesijk[ii,jj,kk] = np.arccos(cang) * 180.0 / np.pi else: anglesijk[ii,jj,kk] = cang return anglesijk, angleidx
def angles(struct, pbc=False, mask_val=999.0, deg=True): """Python implementation of angles.""" nang = struct.natoms * (struct.natoms - 1) * (struct.natoms - 2) norm = np.linalg.norm anglesijk = np.ones((struct.natoms, ) * 3, dtype=float) * mask_val angleidx = np.array( [x for x in permutations(list(range(struct.natoms)), 3)]) for ijk in angleidx: ii, jj, kk = ijk ci = struct.coords_frac[ii, :] cj = struct.coords_frac[jj, :] ck = struct.coords_frac[kk, :] dvij = ci - cj dvik = ci - ck if pbc: dvij = np.dot(crys.min_image_convention(dvij), struct.cell) dvik = np.dot(crys.min_image_convention(dvik), struct.cell) else: dvij = np.dot(dvij, struct.cell) dvik = np.dot(dvik, struct.cell) cang = np.dot(dvij, dvik) / norm(dvij) / norm(dvik) ceps = 1.0 - 2.2e-16 if cang > ceps: cang = 1.0 elif cang < -ceps: cang = -1.0 if deg: anglesijk[ii, jj, kk] = np.arccos(cang) * 180.0 / np.pi else: anglesijk[ii, jj, kk] = cang return anglesijk, angleidx
def pydist(arr, cell, pbc=0): distvecs_frac = arr[:,None,:] - arr[None,...] if pbc == 1: distvecs_frac = crys.min_image_convention(distvecs_frac) distvecs = np.dot(distvecs_frac, cell) distsq = (distvecs**2.0).sum(axis=2) return distsq, distvecs, distvecs_frac
def pydist(coords_frac, cell, pbc=0): distvecs_frac = coords_frac[:,None,:] - coords_frac[None,...] if pbc == 1: distvecs_frac = crys.min_image_convention(distvecs_frac) distvecs = np.dot(distvecs_frac, cell) distsq = (distvecs**2.0).sum(axis=2) return distsq, distvecs, distvecs_frac
def pydist_bigmem(traj, pbc=True): # Pure numpy version w/ big temp arrays. Also slowest. # # (nstep, natoms, natoms, 3) distvecs_frac = traj.coords_frac[:,:,None,:] - \ traj.coords_frac[:,None,:,:] if pbc: distvecs_frac = crys.min_image_convention(distvecs_frac) distvecs = np.empty((nstep, natoms, natoms, 3)) for ii in range(traj.nstep): distvecs[ii,...] = np.dot(distvecs_frac[ii,...], traj.cell[ii,...]) # (nstep, natoms, natoms) dists = np.sqrt((distvecs**2.0).sum(axis=-1)) return dists
def pydist_bigmem(traj, pbc=True): # Pure numpy version w/ big temp arrays. Also slowest. # # (nstep, natoms, natoms, 3) distvecs_frac = traj.coords_frac[:,:,None,:] - \ traj.coords_frac[:,None,:,:] if pbc: distvecs_frac = crys.min_image_convention(distvecs_frac) distvecs = np.empty((nstep, natoms, natoms, 3)) for ii in range(traj.nstep): distvecs[ii, ...] = np.dot(distvecs_frac[ii, ...], traj.cell[ii, ...]) # (nstep, natoms, natoms) dists = np.sqrt((distvecs**2.0).sum(axis=-1)) return dists
def test_dist_traj(): natoms = 10 nstep = 100 cell = rand(nstep, 3, 3) stress = rand(nstep, 3, 3) forces = rand(nstep, natoms, 3) etot = rand(nstep) cryst_const = crys.cell2cc3d(cell, axis=0) coords_frac = np.random.rand(nstep, natoms, 3) coords = crys.coord_trans3d(coords=coords_frac, old=cell, new=num.extend_array(np.identity(3), nstep, axis=0), axis=1, timeaxis=0) assert cryst_const.shape == (nstep, 6) assert coords.shape == (nstep, natoms, 3) symbols = ['H'] * natoms traj = Trajectory( coords_frac=coords_frac, cell=cell, symbols=symbols, forces=forces, stress=stress, etot=etot, timestep=1, ) for pbc in [True, False]: # (nstep, natoms, natoms, 3) distvecs_frac = traj.coords_frac[:,:,None,:] - \ traj.coords_frac[:,None,:,:] assert distvecs_frac.shape == (nstep, natoms, natoms, 3) if pbc: distvecs_frac = crys.min_image_convention(distvecs_frac) distvecs = np.empty((nstep, natoms, natoms, 3)) for ii in range(traj.nstep): distvecs[ii, ...] = np.dot(distvecs_frac[ii, ...], traj.cell[ii, ...]) # (nstep, natoms, natoms) dists = np.sqrt((distvecs**2.0).sum(axis=-1)) assert np.allclose(dists, crys.distances_traj(traj, pbc=pbc))
def test_dist_traj(): natoms = 10 nstep = 100 cell = rand(nstep,3,3) stress = rand(nstep,3,3) forces = rand(nstep,natoms,3) etot=rand(nstep) cryst_const = crys.cell2cc3d(cell, axis=0) coords_frac = np.random.rand(nstep,natoms,3) coords = crys.coord_trans3d(coords=coords_frac, old=cell, new=num.extend_array(np.identity(3), nstep,axis=0), axis=1, timeaxis=0) assert cryst_const.shape == (nstep, 6) assert coords.shape == (nstep,natoms,3) symbols = ['H']*natoms traj = Trajectory(coords_frac=coords_frac, cell=cell, symbols=symbols, forces=forces, stress=stress, etot=etot, timestep=1, ) for pbc in [True, False]: # (nstep, natoms, natoms, 3) distvecs_frac = traj.coords_frac[:,:,None,:] - \ traj.coords_frac[:,None,:,:] assert distvecs_frac.shape == (nstep, natoms, natoms, 3) if pbc: distvecs_frac = crys.min_image_convention(distvecs_frac) distvecs = np.empty((nstep, natoms, natoms, 3)) for ii in range(traj.nstep): distvecs[ii,...] = np.dot(distvecs_frac[ii,...], traj.cell[ii,...]) # (nstep, natoms, natoms) dists = np.sqrt((distvecs**2.0).sum(axis=-1)) assert np.allclose(dists, crys.distances_traj(traj, pbc=pbc))