def test_write_mol(): units={'forces': Ha / eV} nstep = 2 cell2d = np.random.rand(3,3) cell3d = np.random.rand(nstep,3,3) # fractional coords2d_frac = np.array([[0.5, 0.5, 0.5], [1,1,1]]) # fractional, 2 time steps: (2,2,3) = (nstep, natoms, 3) coords3d_frac = np.array([coords2d_frac, coords2d_frac[...,None]*0.8]) # cartesian = coords3d_frac + cell2d (fixed cell). For varialbe cell cases # below, cell3d is used! coords3d_cart = crys.coord_trans(coords3d_frac, old=cell2d, new=np.identity(3), axis=-1) coords2d_cart = coords3d_cart[0,...] symbols = ['H']*2 forces2d = np.random.random(coords2d_frac.shape) forces3d = np.random.random(coords3d_frac.shape) # --- AXSF --------------------------------------------------------------- # fixed cell, forces=0 axsf_fn = pj(testdir, 'foo.axsf') io.write_axsf(axsf_fn, Trajectory(units=units,coords_frac=coords3d_frac, cell=cell2d, symbols=symbols), ) arr = np.loadtxt(StringIO( common.backtick("grep -A3 PRIMVEC %s | egrep -v -e '--|PRIMVEC'" %axsf_fn))) np.testing.assert_array_almost_equal(arr, np.concatenate((cell2d, cell2d), axis=0)) arr = np.loadtxt(StringIO( common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" %axsf_fn))) arr2 = np.vstack((coords3d_cart[0,...],coords3d_cart[1,...])) np.testing.assert_array_almost_equal(arr, arr2) # fixed cell, forces3d, coords_frac axsf_fn = pj(testdir, 'foo3.axsf') io.write_axsf(axsf_fn, Trajectory(units=units,coords_frac=coords3d_frac, cell=cell2d, symbols=symbols, forces=forces3d), ) arr = np.loadtxt(StringIO( common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" %axsf_fn))) t0 = np.concatenate((coords3d_cart[0,...], forces3d[0,...]), axis=-1) t1 = np.concatenate((coords3d_cart[1,...], forces3d[1,...]), axis=-1) arr2 = np.vstack((t0,t1)) print arr print arr2 print "----------------" np.testing.assert_array_almost_equal(arr, arr2) # variable cell, forces3d, coords_frac axsf_fn = pj(testdir, 'foo4.axsf') io.write_axsf(axsf_fn, Trajectory(units=units,coords_frac=coords3d_frac, cell=cell3d, symbols=symbols, forces=forces3d)) arr = np.loadtxt(StringIO( common.backtick("grep -A3 PRIMVEC %s | grep -v -e '--' -e 'PRIMVEC'" %axsf_fn))) arr2 = np.vstack((cell3d[0,...], cell3d[1,...])) print arr print arr2 print "----------------" np.testing.assert_array_almost_equal(arr, arr2) arr = np.loadtxt(StringIO( common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" %axsf_fn))) t0 = np.concatenate((np.dot(coords3d_frac[0,...], cell3d[0,...]), forces3d[0,...]), axis=-1) t1 = np.concatenate((np.dot(coords3d_frac[1,...], cell3d[1,...]), forces3d[1,...]), axis=-1) arr2 = np.vstack((t0,t1)) print arr print arr2 print "----------------" np.testing.assert_array_almost_equal(arr, arr2) # single struct, coords_cart axsf_fn = pj(testdir, 'foo6.axsf') io.write_axsf(axsf_fn, Structure(units=units,coords=coords2d_cart, cell=cell2d, symbols=symbols, forces=forces2d)) arr = np.loadtxt(StringIO( common.backtick("grep -A3 PRIMVEC %s | grep -v -e '--' -e 'PRIMVEC'" %axsf_fn))) arr2 = cell2d print arr print arr2 print "----------------" np.testing.assert_array_almost_equal(arr, arr2) arr = np.loadtxt(StringIO( common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" %axsf_fn))) arr2 = np.concatenate((coords2d_cart, forces2d), axis=1) print arr print arr2 print "----------------" np.testing.assert_array_almost_equal(arr, arr2) # --- XYZ ---------------------------------------------------------------- # Use cell, coords, etc from above # input: coords_frac xyz_fn = pj(testdir, 'foo_frac_input.xyz') io.write_xyz(xyz_fn, Trajectory(units=units,coords_frac=coords3d_frac, cell=cell2d, symbols=symbols), name='foo') arr = np.loadtxt(StringIO( common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" %xyz_fn))) arr2 = np.concatenate([coords3d_cart[0,...], coords3d_cart[1,...]], axis=0) np.testing.assert_array_almost_equal(arr, arr2) # input: coords_cart, cell=None xyz_fn = pj(testdir, 'foo_cart_input.xyz') io.write_xyz(xyz_fn, Trajectory(units=units,coords=coords3d_cart, symbols=symbols), name='foo') arr = np.loadtxt(StringIO( common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" %xyz_fn))) arr2 = np.concatenate((coords3d_cart[0,...], coords3d_cart[1,...]), axis=0) np.testing.assert_array_almost_equal(arr, arr2) # input: coords2d_frac, cell=cell2d xyz_fn = pj(testdir, 'foo_cart_input.xyz') io.write_xyz(xyz_fn, Structure(units=units,coords_frac=coords2d_frac, cell=cell2d, symbols=symbols), name='foo') arr = np.loadtxt(StringIO( common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" %xyz_fn))) arr2 = coords2d_cart np.testing.assert_array_almost_equal(arr, arr2)
def test_rpdf(): have_vmd = os.system('which vmd > /dev/null 2>&1') == 0 for name in ['rand_3d', 'aln_ibrav0_sc', 'aln_ibrav2_sc']: print(("name: %s" % name)) dd = 'files/rpdf' if name == 'rand_3d': # 2 Trajectory = 2 selections cell = np.loadtxt(pj(dd, name + '.cell.txt')) coords_frac = [ load_old(pj(dd, name + '.coords0.txt')), load_old(pj(dd, name + '.coords1.txt')) ] trajs = [ crys.Trajectory(coords_frac=cf, cell=cell) for cf in coords_frac ] for tr in trajs: assert tr.coords_frac.shape == (20, 10, 3) assert tr.nstep == 20 assert tr.natoms == 10 else: # one Structure struct = parse.CifFile(pj(dd, name + '.cif')).get_struct() trajs = [struct] cell = struct.cell ret = crys.rpdf(trajs, rmax=5.0, dr=0.05, pbc=True) # rpdf() -- compere w/ ref results = { 'rad': ret[:, 0], 'hist': ret[:, 1], 'num_int': ret[:, 2], 'rmax_auto': np.array(crys.rmax_smith(cell)), } for key, val in results.items(): print((" key: %s" % key)) ref_fn = pj(dd, "result.%s.%s.txt" % (key, name)) print((" reference file: %s" % ref_fn)) ref = np.loadtxt(ref_fn) if doplot: plt.figure() plt.plot(ref, '.-', label='ref') plt.plot(val, '.-', label='val') plt.legend() plt.title(key) else: # decimal=3 b/c ref data created w/ older implementation, # slight numerical noise np.testing.assert_array_almost_equal(ref, val, decimal=3) print((" key: %s ... ok" % key)) # API if name.startswith('aln_'): sy = np.array(trajs[0].symbols) ret1 = crys.rpdf(trajs, dr=0.1, amask=[sy == 'Al', sy == 'N']) ret2 = crys.rpdf(trajs, dr=0.1, amask=['Al', 'N']) aae(ret1, ret2) # API: # [traj, traj] # [traj] # traj traj = crys.Trajectory(coords_frac=rand(100, 20, 3), cell=np.identity(3) * 20, symbols=['O'] * 5 + ['H'] * 15) ret1 = crys.rpdf([traj, traj], rmax=5.0, dr=0.05, pbc=True) ret2 = crys.rpdf([traj], rmax=5.0, dr=0.05, pbc=True) aae(ret1, ret2) ret3 = crys.rpdf(traj, rmax=5.0, dr=0.05, pbc=True) aae(ret1, ret3) # dmask ret = crys.rpdf(traj, rmax=5.0, dr=0.05, dmask='>=2.0') msk = ret[:, 0] >= 2.0 imsk = np.invert(msk) assert (ret[msk, 1] > 0.0).any() assert (ret[imsk, 1] == 0.0).all() ret = crys.rpdf(traj, rmax=5.0, dr=0.05, dmask='{d}>=2.0') assert (ret[msk, 1] > 0.0).any() assert (ret[imsk, 1] == 0.0).all() ret = crys.rpdf(traj, rmax=5.0, dr=0.05, dmask='({d} >= 1.0) & ({d} <= 3.0)') msk = (ret[:, 0] >= 1.0) & (ret[:, 0] <= 3.0) imsk = np.invert(msk) assert (ret[msk, 1] > 0.0).any() assert (ret[imsk, 1] == 0.0).all() if have_vmd: # slicefirst and API print("vmd_measure_gofr: slicefirst ...") traj = crys.Trajectory(coords_frac=rand(100, 20, 3), cell=np.identity(3) * 20, symbols=['O'] * 5 + ['H'] * 15) for first, last, step in [(0, -1, 1), (20, 80, 10)]: ret = [] for sf in [True, False]: print("first=%i, last=%i, step=%i, slicefirst=%s" % (first, last, step, sf)) tmp = crys.vmd_measure_gofr( traj, dr=0.1, rmax='auto', sel=['all', 'all'], slicefirst=sf, first=first, last=last, step=step, usepbc=1, tmpdir=testdir, verbose=False, ) ret.append(tmp) assert np.allclose(ret[0][:, 0], ret[1][:, 0]) assert np.allclose(ret[0][:, 1], ret[1][:, 1]) assert np.allclose(ret[0][:, 2], ret[1][:, 2]) # API call_vmd_measure_gofr() trajfn = pj(testdir, 'vmd_xsf_call_vmd_measure_gofr') data = io.write_axsf(trajfn, traj) ret = crys.call_vmd_measure_gofr(trajfn, dr=0.1, rmax=10, sel=['all', 'all'], fntype='xsf', first=0, last=-1, step=1, usepbc=1, datafn=None, scriptfn=None, logfn=None, tmpdir=testdir, verbose=False) # compare results, up to L/2 = rmax_auto = 10 = rmax_smith(cell) # all-all, hist will differ rmax = 10 vmd = crys.vmd_measure_gofr(traj, dr=0.1, sel=['all', 'all'], rmax=10) pwt = crys.rpdf(traj, dr=0.1, amask=None, rmax=10) assert np.allclose(vmd[:-1, 0], pwt[:, 0]) # rad ##assert np.allclose(vmd[:-1,1], pwt[:,1]) # hist assert np.allclose(vmd[:-1, 2], pwt[:, 2]) # num_int # 2 selections, all ok sy = np.array(traj.symbols) vmd = crys.vmd_measure_gofr(traj, dr=0.1, sel=['name O', 'name H'], rmax=10) pwt = crys.rpdf(traj, dr=0.1, amask=[sy == 'O', sy == 'H'], rmax=10) assert np.allclose(vmd[:-1, 0], pwt[:, 0]) # rad assert np.allclose(vmd[:-1, 1], pwt[:, 1]) # hist assert np.allclose(vmd[:-1, 2], pwt[:, 2]) # num_int if doplot: plt.show()
def test_write_mol(): units = {'forces': Ha / eV} nstep = 2 cell2d = np.random.rand(3, 3) cell3d = np.random.rand(nstep, 3, 3) # fractional coords2d_frac = np.array([[0.5, 0.5, 0.5], [1, 1, 1]]) # fractional, 2 time steps: (2,2,3) = (nstep, natoms, 3) coords3d_frac = np.array([coords2d_frac, coords2d_frac * 0.8]) # cartesian = coords3d_frac + cell2d (fixed cell). For varialbe cell cases # below, cell3d is used! coords3d_cart = crys.coord_trans(coords3d_frac, old=cell2d, new=np.identity(3), axis=-1) coords2d_cart = coords3d_cart[0, ...] symbols = ['H'] * 2 forces2d = np.random.random(coords2d_frac.shape) forces3d = np.random.random(coords3d_frac.shape) # --- AXSF --------------------------------------------------------------- # fixed cell, forces=0 axsf_fn = pj(testdir, 'foo.axsf') io.write_axsf( axsf_fn, Trajectory(units=units, coords_frac=coords3d_frac, cell=cell2d, symbols=symbols), ) arr = np.loadtxt( StringIO( common.backtick("grep -A3 PRIMVEC %s | grep -vE -e '--|PRIMVEC'" % axsf_fn))) np.testing.assert_array_almost_equal( arr, np.concatenate((cell2d, cell2d), axis=0)) arr = np.loadtxt( StringIO(common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" % axsf_fn))) arr2 = np.vstack((coords3d_cart[0, ...], coords3d_cart[1, ...])) np.testing.assert_array_almost_equal(arr, arr2) # fixed cell, forces3d, coords_frac axsf_fn = pj(testdir, 'foo3.axsf') io.write_axsf( axsf_fn, Trajectory(units=units, coords_frac=coords3d_frac, cell=cell2d, symbols=symbols, forces=forces3d), ) arr = np.loadtxt( StringIO(common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" % axsf_fn))) t0 = np.concatenate((coords3d_cart[0, ...], forces3d[0, ...]), axis=-1) t1 = np.concatenate((coords3d_cart[1, ...], forces3d[1, ...]), axis=-1) arr2 = np.vstack((t0, t1)) print(arr) print(arr2) print("----------------") np.testing.assert_array_almost_equal(arr, arr2) # variable cell, forces3d, coords_frac axsf_fn = pj(testdir, 'foo4.axsf') io.write_axsf( axsf_fn, Trajectory(units=units, coords_frac=coords3d_frac, cell=cell3d, symbols=symbols, forces=forces3d)) arr = np.loadtxt( StringIO( common.backtick( "grep -A3 PRIMVEC %s | grep -v -e '--' -e 'PRIMVEC'" % axsf_fn))) arr2 = np.vstack((cell3d[0, ...], cell3d[1, ...])) print(arr) print(arr2) print("----------------") np.testing.assert_array_almost_equal(arr, arr2) arr = np.loadtxt( StringIO(common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" % axsf_fn))) t0 = np.concatenate( (np.dot(coords3d_frac[0, ...], cell3d[0, ...]), forces3d[0, ...]), axis=-1) t1 = np.concatenate( (np.dot(coords3d_frac[1, ...], cell3d[1, ...]), forces3d[1, ...]), axis=-1) arr2 = np.vstack((t0, t1)) print(arr) print(arr2) print("----------------") np.testing.assert_array_almost_equal(arr, arr2) # single struct, coords_cart axsf_fn = pj(testdir, 'foo6.axsf') io.write_axsf( axsf_fn, Structure(units=units, coords=coords2d_cart, cell=cell2d, symbols=symbols, forces=forces2d)) arr = np.loadtxt( StringIO( common.backtick( "grep -A3 PRIMVEC %s | grep -v -e '--' -e 'PRIMVEC'" % axsf_fn))) arr2 = cell2d print(arr) print(arr2) print("----------------") np.testing.assert_array_almost_equal(arr, arr2) arr = np.loadtxt( StringIO(common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" % axsf_fn))) arr2 = np.concatenate((coords2d_cart, forces2d), axis=1) print(arr) print(arr2) print("----------------") np.testing.assert_array_almost_equal(arr, arr2) # --- XYZ ---------------------------------------------------------------- # Use cell, coords, etc from above # input: coords_frac xyz_fn = pj(testdir, 'foo_frac_input.xyz') io.write_xyz(xyz_fn, Trajectory(units=units, coords_frac=coords3d_frac, cell=cell2d, symbols=symbols), name='foo') arr = np.loadtxt( StringIO(common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" % xyz_fn))) arr2 = np.concatenate([coords3d_cart[0, ...], coords3d_cart[1, ...]], axis=0) np.testing.assert_array_almost_equal(arr, arr2) # input: coords_cart, cell=None xyz_fn = pj(testdir, 'foo_cart_input.xyz') io.write_xyz(xyz_fn, Trajectory(units=units, coords=coords3d_cart, symbols=symbols), name='foo') arr = np.loadtxt( StringIO(common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" % xyz_fn))) arr2 = np.concatenate((coords3d_cart[0, ...], coords3d_cart[1, ...]), axis=0) np.testing.assert_array_almost_equal(arr, arr2) # input: coords2d_frac, cell=cell2d xyz_fn = pj(testdir, 'foo_cart_input.xyz') io.write_xyz(xyz_fn, Structure(units=units, coords_frac=coords2d_frac, cell=cell2d, symbols=symbols), name='foo') arr = np.loadtxt( StringIO(common.backtick("sed -nre 's/^H(.*)/\\1/gp' %s" % xyz_fn))) arr2 = coords2d_cart np.testing.assert_array_almost_equal(arr, arr2)
def test_rpdf(): have_vmd = os.system("which vmd > /dev/null 2>&1") == 0 for name in ["rand_3d", "aln_ibrav0_sc", "aln_ibrav2_sc"]: print ("name: %s" % name) dd = "files/rpdf" if name == "rand_3d": # 2 Trajectory = 2 selections cell = np.loadtxt(pj(dd, name + ".cell.txt")) coords_frac = [load_old(pj(dd, name + ".coords0.txt")), load_old(pj(dd, name + ".coords1.txt"))] trajs = [crys.Trajectory(coords_frac=cf, cell=cell) for cf in coords_frac] for tr in trajs: assert tr.coords_frac.shape == (20, 10, 3) assert tr.nstep == 20 assert tr.natoms == 10 else: # one Structure struct = parse.CifFile(pj(dd, name + ".cif")).get_struct() trajs = [struct] cell = struct.cell ret = crys.rpdf(trajs, rmax=5.0, dr=0.05, pbc=True) # rpdf() -- compere w/ ref results = { "rad": ret[:, 0], "hist": ret[:, 1], "num_int": ret[:, 2], "rmax_auto": np.array(crys.rmax_smith(cell)), } for key, val in results.iteritems(): print (" key: %s" % key) ref_fn = pj(dd, "result.%s.%s.txt" % (key, name)) print (" reference file: %s" % ref_fn) ref = np.loadtxt(ref_fn) if doplot: plt.figure() plt.plot(ref, ".-", label="ref") plt.plot(val, ".-", label="val") plt.legend() plt.title(key) else: # decimal=3 b/c ref data created w/ older implementation, # slight numerical noise np.testing.assert_array_almost_equal(ref, val, decimal=3) print (" key: %s ... ok" % key) # API if name.startswith("aln_"): sy = np.array(trajs[0].symbols) ret1 = crys.rpdf(trajs, dr=0.1, amask=[sy == "Al", sy == "N"]) ret2 = crys.rpdf(trajs, dr=0.1, amask=["Al", "N"]) aae(ret1, ret2) # API: # [traj, traj] # [traj] # traj traj = crys.Trajectory(coords_frac=rand(100, 20, 3), cell=np.identity(3) * 20, symbols=["O"] * 5 + ["H"] * 15) ret1 = crys.rpdf([traj, traj], rmax=5.0, dr=0.05, pbc=True) ret2 = crys.rpdf([traj], rmax=5.0, dr=0.05, pbc=True) aae(ret1, ret2) ret3 = crys.rpdf(traj, rmax=5.0, dr=0.05, pbc=True) aae(ret1, ret3) # dmask ret = crys.rpdf(traj, rmax=5.0, dr=0.05, dmask=">=2.0") msk = ret[:, 0] >= 2.0 imsk = np.invert(msk) assert (ret[msk, 1] > 0.0).any() assert (ret[imsk, 1] == 0.0).all() ret = crys.rpdf(traj, rmax=5.0, dr=0.05, dmask="{d}>=2.0") assert (ret[msk, 1] > 0.0).any() assert (ret[imsk, 1] == 0.0).all() ret = crys.rpdf(traj, rmax=5.0, dr=0.05, dmask="({d} >= 1.0) & ({d} <= 3.0)") msk = (ret[:, 0] >= 1.0) & (ret[:, 0] <= 3.0) imsk = np.invert(msk) assert (ret[msk, 1] > 0.0).any() assert (ret[imsk, 1] == 0.0).all() if have_vmd: # slicefirst and API print "vmd_measure_gofr: slicefirst ..." traj = crys.Trajectory(coords_frac=rand(100, 20, 3), cell=np.identity(3) * 20, symbols=["O"] * 5 + ["H"] * 15) for first, last, step in [(0, -1, 1), (20, 80, 10)]: ret = [] for sf in [True, False]: print "first=%i, last=%i, step=%i, slicefirst=%s" % (first, last, step, sf) tmp = crys.vmd_measure_gofr( traj, dr=0.1, rmax="auto", sel=["all", "all"], slicefirst=sf, first=first, last=last, step=step, usepbc=1, tmpdir=testdir, verbose=False, ) ret.append(tmp) assert np.allclose(ret[0][:, 0], ret[1][:, 0]) assert np.allclose(ret[0][:, 1], ret[1][:, 1]) assert np.allclose(ret[0][:, 2], ret[1][:, 2]) # API call_vmd_measure_gofr() trajfn = pj(testdir, "vmd_xsf_call_vmd_measure_gofr") data = io.write_axsf(trajfn, traj) ret = crys.call_vmd_measure_gofr( trajfn, dr=0.1, rmax=10, sel=["all", "all"], fntype="xsf", first=0, last=-1, step=1, usepbc=1, datafn=None, scriptfn=None, logfn=None, tmpdir=testdir, verbose=False, ) # compare results, up to L/2 = rmax_auto = 10 = rmax_smith(cell) # all-all, hist will differ rmax = 10 vmd = crys.vmd_measure_gofr(traj, dr=0.1, sel=["all", "all"], rmax=10) pwt = crys.rpdf(traj, dr=0.1, amask=None, rmax=10) assert np.allclose(vmd[:-1, 0], pwt[:, 0]) # rad ##assert np.allclose(vmd[:-1,1], pwt[:,1]) # hist assert np.allclose(vmd[:-1, 2], pwt[:, 2]) # num_int # 2 selections, all ok sy = np.array(traj.symbols) vmd = crys.vmd_measure_gofr(traj, dr=0.1, sel=["name O", "name H"], rmax=10) pwt = crys.rpdf(traj, dr=0.1, amask=[sy == "O", sy == "H"], rmax=10) assert np.allclose(vmd[:-1, 0], pwt[:, 0]) # rad assert np.allclose(vmd[:-1, 1], pwt[:, 1]) # hist assert np.allclose(vmd[:-1, 2], pwt[:, 2]) # num_int if doplot: plt.show()