def test_correct(): coords = rand(20, 3) cell = rand(3, 3) st = crys.Structure(coords=coords, cell=cell, symbols=['H']) # new cartesian coord sys, created by rotating the old E=identity(3) by 3 # random angles angles = [rand() * 360, rand() * 180, rand() * 360] rmat = euler_rotation(*tuple(angles)) newcoords = np.array([np.dot(rmat, x) for x in st.coords]) newcell = np.array([np.dot(rmat, x) for x in st.cell]) st2 = crys.align_cart(st, cart=rmat, eps=1e-3) assert np.allclose(newcoords, np.dot(st.coords, rmat.T)) assert np.allclose( newcoords, crys.coord_trans(st.coords, old=np.identity(3), new=rmat)) assert np.allclose(newcell, np.dot(st.cell, rmat.T)) assert np.allclose(newcell, crys.coord_trans(st.cell, old=np.identity(3), new=rmat)) assert np.allclose(st2.coords, newcoords)
def test_frac2cart(): coords_frac = rand(20, 3) coords_frac_copy = coords_frac.copy() cell = rand(3, 3) c1 = np.dot(coords_frac, cell) c2 = _flib.frac2cart(coords_frac, cell) c3 = crys.coord_trans(coords_frac, old=cell, new=np.identity(3)) assert (coords_frac == coords_frac_copy).all() assert np.allclose(c1, c2) assert np.allclose(c1, c3) assert c2.flags.f_contiguous
def test_frac2cart(): coords_frac = rand(20,3) coords_frac_copy = coords_frac.copy() cell = rand(3,3) c1 = np.dot(coords_frac, cell) c2 = _flib.frac2cart(coords_frac, cell) c3 = crys.coord_trans(coords_frac, old=cell, new=np.identity(3)) assert (coords_frac == coords_frac_copy).all() assert np.allclose(c1, c2) assert np.allclose(c1, c3) assert c2.flags.f_contiguous
def test_cart2frac(): coords = rand(20, 3) coords_copy = coords.copy() cell = rand(3, 3) c1 = np.dot(coords, np.linalg.inv(cell)) c2 = np.linalg.solve(cell.T, coords.T).T c3 = _flib.cart2frac(coords, cell) c4 = crys.coord_trans(coords, new=cell, old=np.identity(3)) assert (coords == coords_copy).all() assert np.allclose(c1, c2) assert np.allclose(c1, c3) assert np.allclose(c1, c4) assert c3.flags.f_contiguous
def test_cart2frac(): coords = rand(20,3) coords_copy = coords.copy() cell = rand(3,3) c1 = np.dot(coords, np.linalg.inv(cell)) c2 = np.linalg.solve(cell.T, coords.T).T c3 = _flib.cart2frac(coords, cell) c4 = crys.coord_trans(coords, new=cell, old=np.identity(3)) assert (coords == coords_copy).all() assert np.allclose(c1, c2) assert np.allclose(c1, c3) assert np.allclose(c1, c4) assert c3.flags.f_contiguous
def test_correct(): coords = rand(20,3) cell = rand(3,3) st = crys.Structure(coords=coords, cell=cell, symbols=['H']) # new cartesian coord sys, created by rotating the old E=identity(3) by 3 # random angles angles = [rand()*360, rand()*180, rand()*360] rmat = euler_rotation(*tuple(angles)) newcoords = np.array([np.dot(rmat, x) for x in st.coords]) newcell = np.array([np.dot(rmat, x) for x in st.cell]) st2 = crys.align_cart(st, cart=rmat, eps=1e-3) assert np.allclose(newcoords, np.dot(st.coords, rmat.T)) assert np.allclose(newcoords, crys.coord_trans(st.coords, old=np.identity(3), new=rmat)) assert np.allclose(newcell, np.dot(st.cell, rmat.T)) assert np.allclose(newcell, crys.coord_trans(st.cell, old=np.identity(3), new=rmat)) assert np.allclose(st2.coords, newcoords)
def test_coord_trans(): natoms = 10 cell = np.array([[3, 0, 0], [1.1, 5, -0.04], [-0.33, 1.5, 7]]) cryst_const = crys.cell2cc(cell) coords_frac = rand(natoms, 3) coords = crys.coord_trans(coords=coords_frac, old=cell, new=np.identity(3)) st = crys.Structure(coords=coords, cell=cell) assert np.allclose(coords_frac, st.coords_frac) st = crys.Structure(coords_frac=coords_frac, cell=cell) assert np.allclose(coords, st.coords) st = crys.Structure(coords=coords, cell=cell) assert np.allclose(cryst_const, st.cryst_const)
def test_coord_trans(): natoms = 10 cell = np.array([[3,0,0], [1.1,5,-0.04], [-0.33,1.5,7]]) cryst_const = crys.cell2cc(cell) coords_frac = rand(natoms,3) coords = crys.coord_trans(coords=coords_frac, old=cell, new=np.identity(3)) st = crys.Structure(coords=coords, cell=cell) assert np.allclose(coords_frac, st.coords_frac) st = crys.Structure(coords_frac=coords_frac, cell=cell) assert np.allclose(coords, st.coords) st = crys.Structure(coords=coords, cell=cell) assert np.allclose(cryst_const, st.cryst_const)
def test_pdos_coord_trans(): coords = {} # cartesian: first axis is the time axis coords['cart'] = rand((100, 10, 3)) # cartesian scaled, e.g. Angstrom instead of Bohr coords['cart2'] = coords['cart'] * 5 # some other coord sys cell1 = rand((3, 3)) # coord_trans: axis=-1 specifies the "x,y,z"-axis of dimension 3 coords['cell1'] = coord_trans(coords['cart'], old=np.identity(3), new=cell1, axis=-1) dos = {} for key, val in coords.items(): dos[key] = pdos(val) np.testing.assert_array_almost_equal(dos['cart'], dos['cart2']) assert not tools.array_almost_equal(dos['cart'], dos['cell1'])
def test_pdos_coord_trans(): coords = {} # cartesian: first axis is the time axis coords['cart'] = rand((100, 10, 3)) # cartesian scaled, e.g. Angstrom instead of Bohr coords['cart2'] = coords['cart']*5 # some other coord sys cell1 = rand((3,3)) # coord_trans: axis=-1 specifies the "x,y,z"-axis of dimension 3 coords['cell1'] = coord_trans(coords['cart'], old=np.identity(3), new=cell1, axis=-1) dos = {} for key, val in coords.iteritems(): dos[key] = pdos(val) np.testing.assert_array_almost_equal(dos['cart'], dos['cart2']) assert not tools.array_almost_equal(dos['cart'], dos['cell1'])
def test_struct(): natoms = 10 cell = np.array([[3, 0, 0], [1.1, 5, -0.04], [-0.33, 1.5, 7]]) cryst_const = crys.cell2cc(cell) coords_frac = rand(natoms, 3) coords = crys.coord_trans(coords=coords_frac, old=cell, new=np.identity(3)) symbols = ['H'] * natoms stress = rand(3, 3) forces = rand(natoms, 3) # Use ``cell`` instead of ``cryst_const` as input such that # atoms.get_cell() test passes (see below for why -- cell orientation) st = Structure(coords_frac=coords_frac, symbols=symbols, cell=cell, stress=stress, forces=forces, etot=42) # Test if all getters work. for name in st.attr_lst: print(name) st.try_set_attr(name) assert getattr(st, name) is not None, "attr None: %s" % name assert eval('st.get_%s()' % name) is not None, "getter returns None: %s" % name aaae(coords_frac, st.coords_frac) aaae(cryst_const, st.cryst_const) aaae(coords, st.coords) assert st.natoms == natoms st = Structure(coords_frac=coords_frac, symbols=symbols, cell=cell) aaae(coords, st.get_coords()) # Cell calculated from cryst_const has defined orientation in space which may be # different from the original `cell`, but the volume and underlying cryst_const # must be the same. st = Structure(coords_frac=coords_frac, symbols=symbols, cryst_const=cryst_const) assert st.get_cell() is not None np.testing.assert_almost_equal(crys.volume_cell(cell), crys.volume_cell(st.get_cell())) aaae(cryst_const, crys.cell2cc(st.get_cell())) # units st = Structure(coords_frac=coords_frac, cell=cell, symbols=symbols, stress=stress, forces=forces, units={ 'length': 2, 'forces': 3, 'stress': 4 }) aaae(2 * coords, st.coords) aaae(3 * forces, st.forces) aaae(4 * stress, st.stress) traj = crys.struct2traj(st) assert traj.is_traj # copy(): Assert everything has another memory location = is a new copy of # the object. IntTypes are NOT copied by copy.deepcopy(), which we use in # Structure.copy(), apparently b/c they are always automatically copied # before in-place operations. Same for float type. # # >>> a=10; b=a; print id(a); print id(b) # 36669152 # 36669152 # >>> a*=100; print id(a); print id(b) # 72538264 # 36669152 # >>> a # 100 # >>> b # 10 # # >>> a=[1,2,3]; b=a; print id(a); print id(b) # 72624320 # 72624320 # >>> a[0] = 44; print id(a); print id(b) # 72624320 # 72624320 # >>> a # [44, 2, 3] # >>> b # [44, 2, 3] st2 = st.copy() for name in st.attr_lst: val = getattr(st, name) if val is not None and not (isinstance(val, int) or \ isinstance(val, float)): val2 = getattr(st2, name) assert id(val2) != id(val) assert_all_types_equal(val2, val)
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_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_coord_trans(): #----------------------------------------------------------- # 2D #----------------------------------------------------------- c_X = rand(20,3) # basis vecs are assumed to be rows X = rand(3,3)*5 Y = rand(3,3)*3 # transform and back-transform c_Y = coord_trans(c_X, old=X, new=Y) c_X2 = coord_trans(c_Y, old=Y, new=X) aaae(c_X, c_X2) # simple dot product must produce same cartesian results: # X . v_X = I . v_I = v_I X = np.identity(3) Y = rand(3,3)*3 c_X = rand(20,3) c_Y = coord_trans(c_X, old=X, new=Y) # normal back-transform c_X2 = coord_trans(c_Y, old=Y, new=X) # 2 forms w/ dot(), assume: basis vecs = rows of X and Y c_X3 = np.dot(c_Y, Y) c_X4 = np.dot(Y.T, c_Y.T).T aaae(c_X, c_X2) aaae(c_X, c_X3) aaae(c_X, c_X4) # some textbook example # v_I = np.array([1.0,1.5]) I = np.identity(2) # basis vecs as rows X = sqrt(2)/2.0*np.array([[1,-1],[1,1]]).T Y = np.array([[1,1],[0,1]]).T # "identity" transform aaae(coord_trans(v_I,I,I), v_I) # v in basis X and Y v_X = coord_trans(v_I,I,X) v_Y = coord_trans(v_I,I,Y) aaae(v_X, np.array([1.76776695, 0.35355339])) aaae(v_Y, np.array([-0.5, 1.5])) # back-transform aaae(coord_trans(v_X,X,I), v_I) aaae(coord_trans(v_Y,Y,I), v_I) # higher "x,y,z"-dims: 4-vectors c_X = rand(20,4) X = rand(4,4)*5 Y = rand(4,4)*3 c_Y = coord_trans(c_X, old=X, new=Y) c_X2 = coord_trans(c_Y, old=Y, new=X) aaae(c_X, c_X2) #----------------------------------------------------------- # 3D #----------------------------------------------------------- # x,y,z case c_X = rand(20,3,10) X = rand(3,3)*5 Y = rand(3,3)*3 c_Y = coord_trans(c_X, old=X, new=Y, axis=1) c_X2 = coord_trans(c_Y, old=Y, new=X, axis=1) aaae(c_X, c_X2) c_X = rand(20,10,3) c_Y = coord_trans(c_X, old=X, new=Y, axis=-1) c_X2 = coord_trans(c_Y, old=Y, new=X, axis=-1) aaae(c_X, c_X2) c_X = rand(3,20,10) c_Y = coord_trans(c_X, old=X, new=Y, axis=0) c_X2 = coord_trans(c_Y, old=Y, new=X, axis=0) aaae(c_X, c_X2) # 3d, higher "x,y,z"-dims, i.e. 4-vectors: trajectory of 5 atoms, 10 steps, # "4d-coordinates" c_X = rand(20,4,10) X = rand(4,4)*5 Y = rand(4,4)*3 c_Y = coord_trans(c_X, old=X, new=Y, axis=1) c_X2 = coord_trans(c_Y, old=Y, new=X, axis=1) aaae(c_X, c_X2) #----------------------------------------------------------- # ND #----------------------------------------------------------- # arbitrary collection of 4-vectors c_X = rand(20,4,10,8) X = rand(4,4)*5 Y = rand(4,4)*3 c_Y = coord_trans(c_X, old=X, new=Y, axis=1) c_X2 = coord_trans(c_Y, old=Y, new=X, axis=1) aaae(c_X, c_X2) #----------------------------------------------------------- # special case 3d #----------------------------------------------------------- # Note that axis=1 is always the xyz-axis (length 3) if the timeaxis # (length 10) would be removed from all arrays (2d case then). c_X = rand(20,3,10) X = rand(3,3,10)*5 Y = rand(3,3,10)*3 c_Y = coord_trans3d(c_X, old=X, new=Y, axis=1, timeaxis=2) c_X2 = coord_trans3d(c_Y, old=Y, new=X, axis=1, timeaxis=2) aaae(c_X, c_X2) c_X = rand(20,10,3) X = rand(3,10,3)*5 Y = rand(3,10,3)*3 c_Y = coord_trans3d(c_X, old=X, new=Y, axis=1, timeaxis=1) c_X2 = coord_trans3d(c_Y, old=Y, new=X, axis=1, timeaxis=1) aaae(c_X, c_X2) c_X = rand(10,20,3) X = rand(10,3,3)*5 Y = rand(10,3,3)*3 c_Y = coord_trans3d(c_X, old=X, new=Y, axis=1, timeaxis=0) c_X2 = coord_trans3d(c_Y, old=Y, new=X, axis=1, timeaxis=0) aaae(c_X, c_X2)
def test_struct(): natoms = 10 cell = np.array([[3,0,0], [1.1,5,-0.04], [-0.33,1.5,7]]) cryst_const = crys.cell2cc(cell) coords_frac = rand(natoms,3) coords = crys.coord_trans(coords=coords_frac, old=cell, new=np.identity(3)) symbols = ['H']*natoms stress = rand(3,3) forces = rand(natoms,3) # Use ``cell`` instead of ``cryst_const` as input such that # atoms.get_cell() test passes (see below for why -- cell orientation) st = Structure(coords_frac=coords_frac, symbols=symbols, cell=cell, stress=stress, forces=forces, etot=42) # Test if all getters work. for name in st.attr_lst: print name st.try_set_attr(name) assert getattr(st, name) is not None, "attr None: %s" %name assert eval('st.get_%s()'%name) is not None, "getter returns None: %s" %name aaae(coords_frac, st.coords_frac) aaae(cryst_const, st.cryst_const) aaae(coords, st.coords) assert st.natoms == natoms st = Structure(coords_frac=coords_frac, symbols=symbols, cell=cell) aaae(coords, st.get_coords()) # Cell calculated from cryst_const has defined orientation in space which may be # different from the original `cell`, but the volume and underlying cryst_const # must be the same. st = Structure(coords_frac=coords_frac, symbols=symbols, cryst_const=cryst_const) assert st.get_cell() is not None np.testing.assert_almost_equal(crys.volume_cell(cell), crys.volume_cell(st.get_cell())) aaae(cryst_const, crys.cell2cc(st.get_cell())) # units st = Structure(coords_frac=coords_frac, cell=cell, symbols=symbols, stress=stress, forces=forces, units={'length': 2, 'forces': 3, 'stress': 4}) aaae(2*coords, st.coords) aaae(3*forces, st.forces) aaae(4*stress, st.stress) traj = crys.struct2traj(st) assert traj.is_traj # copy(): Assert everything has another memory location = is a new copy of # the object. IntTypes are NOT copied by copy.deepcopy(), which we use in # Structure.copy(), apparently b/c they are always automatically copied # before in-place operations. Same for float type. # # >>> a=10; b=a; print id(a); print id(b) # 36669152 # 36669152 # >>> a*=100; print id(a); print id(b) # 72538264 # 36669152 # >>> a # 100 # >>> b # 10 # # >>> a=[1,2,3]; b=a; print id(a); print id(b) # 72624320 # 72624320 # >>> a[0] = 44; print id(a); print id(b) # 72624320 # 72624320 # >>> a # [44, 2, 3] # >>> b # [44, 2, 3] st2 = st.copy() for name in st.attr_lst: val = getattr(st,name) if val is not None and not (isinstance(val, types.IntType) or \ isinstance(val, types.FloatType)): val2 = getattr(st2,name) assert id(val2) != id(val) assert_all_types_equal(val2, val)