def run(tgz, skip=[], atol_map={}): tgz_path = os.path.dirname(tgz) unpack_path = tgz.replace('.tgz', '') common.system("tar -C {0} -xzf {1}".format(tgz_path, tgz)) tr1 = io.read_lammps_md_txt("{0}/log.lammps".format(unpack_path)) tr2 = io.read_lammps_md_dcd("{0}/log.lammps".format(unpack_path)) for name in tr1.attr_lst: if name in skip: continue elif name in atol_map: set_atol(atol_map[name]) else: set_atol() x1 = getattr(tr1, name) x2 = getattr(tr2, name) print(name) tools.assert_all_types_almost_equal(x1, x2) # stress assert (tr1.stress[:, 0, 1] == tr1.stress[:, 1, 0]).all() assert (tr1.stress[:, 0, 2] == tr1.stress[:, 2, 0]).all() assert (tr1.stress[:, 1, 2] == tr1.stress[:, 2, 1]).all()
def run(tgz, skip=[], atol_map={}): tgz_path = os.path.dirname(tgz) unpack_path = tgz.replace('.tgz','') common.system("tar -C {0} -xzf {1}".format(tgz_path,tgz)) tr1 = io.read_lammps_md_txt("{0}/log.lammps".format(unpack_path)) tr2 = io.read_lammps_md_dcd("{0}/log.lammps".format(unpack_path)) for name in tr1.attr_lst: if name in skip: continue elif atol_map.has_key(name): set_atol(atol_map[name]) else: set_atol() x1 = getattr(tr1, name) x2 = getattr(tr2, name) print name tools.assert_all_types_almost_equal(x1, x2) # stress assert (tr1.stress[:,0,1] == tr1.stress[:,1,0]).all() assert (tr1.stress[:,0,2] == tr1.stress[:,2,0]).all() assert (tr1.stress[:,1,2] == tr1.stress[:,2,1]).all()
def test_mix(): symbols = ['H']*3 + ['Au']*7 natoms = len(symbols) st1nf = Structure(coords_frac=rand(natoms,3), symbols=symbols, cell=rand(3,3)) st2nf = Structure(coords_frac=rand(natoms,3), symbols=symbols, cell=rand(3,3)) st1f = Structure(coords_frac=rand(natoms,3), symbols=symbols, cell=rand(3,3), forces=rand(natoms,3)) st2f = Structure(coords_frac=rand(natoms,3), symbols=symbols, cell=rand(3,3), forces=rand(natoms,3)) for st1,st2 in [(st1f, st2f), (st1nf, st2nf)]: tr = crys.mix(st1, st2, alpha=np.linspace(0,1,20)) assert tr.nstep == 20 assert tr.coords_frac.shape == (20, st1.natoms, 3) for idx,st in [(0,st1), (-1, st2)]: tools.assert_dict_with_all_types_almost_equal(st.__dict__, tr[idx].__dict__, keys=st1.attr_lst) for x in [0.5, 0.9]: tr = crys.mix(st1, st2, alpha=np.array([x])) assert tr.nstep == 1 tools.assert_all_types_almost_equal(tr[0].coords, (1-x)*st1.coords + x*st2.coords) tools.assert_all_types_almost_equal(tr[0].cell, (1-x)*st1.cell + x*st2.cell) if tr.forces is not None: tools.assert_all_types_almost_equal(tr[0].forces, (1-x)*st1.forces + x*st2.forces)
def test_mix(): symbols = ['H'] * 3 + ['Au'] * 7 natoms = len(symbols) st1nf = Structure(coords_frac=rand(natoms, 3), symbols=symbols, cell=rand(3, 3)) st2nf = Structure(coords_frac=rand(natoms, 3), symbols=symbols, cell=rand(3, 3)) st1f = Structure(coords_frac=rand(natoms, 3), symbols=symbols, cell=rand(3, 3), forces=rand(natoms, 3)) st2f = Structure(coords_frac=rand(natoms, 3), symbols=symbols, cell=rand(3, 3), forces=rand(natoms, 3)) for st1, st2 in [(st1f, st2f), (st1nf, st2nf)]: tr = crys.mix(st1, st2, alpha=np.linspace(0, 1, 20)) assert tr.nstep == 20 assert tr.coords_frac.shape == (20, st1.natoms, 3) for idx, st in [(0, st1), (-1, st2)]: tools.assert_dict_with_all_types_almost_equal(st.__dict__, tr[idx].__dict__, keys=st1.attr_lst) for x in [0.5, 0.9]: tr = crys.mix(st1, st2, alpha=np.array([x])) assert tr.nstep == 1 tools.assert_all_types_almost_equal( tr[0].coords, (1 - x) * st1.coords + x * st2.coords) tools.assert_all_types_almost_equal( tr[0].cell, (1 - x) * st1.cell + x * st2.cell) if tr.forces is not None: tools.assert_all_types_almost_equal( tr[0].forces, (1 - x) * st1.forces + x * st2.forces)
def test_tools(): x1 = { 'a': 3, type(1): 'foo', 'b': { 'aa': 1, 'bb': np.array([1, 2]), 'cc': { 'aaa': np.array([1, 2.0]), 'bbb': np.array([2, 4.0]) } } } x2 = copy.deepcopy(x1) tools.assert_dict_with_all_types_equal(x1, x2) tools.assert_all_types_equal(x1, x2) # not equal array x2['b']['cc']['bbb'] *= 2.0 assert not tools.all_types_equal(x1, x2) # almost equal array and float x2 = copy.deepcopy(x1) x2['b']['cc']['bbb'] += 1e-5 x2['b']['aa'] += 1e-5 tools.assert_all_types_almost_equal(x1, x2) # sub-dict different (keys don't match) x2 = copy.deepcopy(x1) x2['b']['cc']['fff'] = 'extra' assert not tools.all_types_equal(x1, x2) # test only some keys of a dict tools.assert_dict_with_all_types_equal({ 'a': 1, 'b': 1 }, { 'a': 1, 'b': 3 }, keys=['a']) # simple stuff tools.assert_all_types_equal(1, 1) tools.assert_all_types_equal(1.0, 1.0) tools.assert_all_types_equal(1.0, 1) tools.assert_all_types_equal(1, 1.0) tools.assert_all_types_equal([1], [1]) tools.assert_all_types_equal([1], [1.0]) tools.assert_all_types_equal('a', 'a') tools.assert_all_types_almost_equal(1.0, 1.0 + 1e-5) tools.assert_all_types_almost_equal(np.array([1.0]), np.array([1.0 + 1e-5])) assert not tools.all_types_equal(1, 2) assert not tools.all_types_equal(1.0, 1.1) assert not tools.all_types_equal([1], [1, 2]) assert not tools.all_types_equal('a', 'b') # different types not allowed if strict=True assert not tools.all_types_equal(1.0, 1, strict=True) # test keys=[...], i.e. ignore some keys in both dicts x2 = copy.deepcopy(x1) x2['c'] = 1.0 assert tools.dict_with_all_types_equal(x1, x2, keys=['a', 'b', type(1)]) # fail on same array content but different dimensions a = np.random.rand(1, 2, 3) b = a[None, ...] # this is True and IMHO a numpy bug b/c the dimensions are # different assert (a == b).all() # must catch that here assert not tools.array_equal(a, b) a = np.random.rand(1, 2, 3) b = (a + 1e-8)[None, ...] assert not (a == b).all() # make sure they are numerically differrent assert np.allclose(a, b) # True but should be False assert not tools.array_almost_equal(a, b) # ok flt = 1.0 np_flt = np.array([1.0 + 1e-9])[0] assert not tools.all_types_equal(flt, np_flt) assert not tools.all_types_equal(np_flt, flt) assert tools.all_types_almost_equal(flt, np_flt) assert tools.all_types_almost_equal(np_flt, flt)
def test_tools(): x1 = {'a': 3, type(1): 'foo', 'b': {'aa': 1, 'bb': np.array([1,2]), 'cc': {'aaa': np.array([1,2.0]), 'bbb': np.array([2,4.0])}}} x2 = copy.deepcopy(x1) tools.assert_dict_with_all_types_equal(x1, x2) tools.assert_all_types_equal(x1, x2) # not equal array x2['b']['cc']['bbb'] *= 2.0 assert not tools.all_types_equal(x1, x2) # almost equal array and float x2 = copy.deepcopy(x1) x2['b']['cc']['bbb'] += 1e-5 x2['b']['aa'] += 1e-5 tools.assert_all_types_almost_equal(x1, x2) # sub-dict different (keys don't match) x2 = copy.deepcopy(x1) x2['b']['cc']['fff'] = 'extra' assert not tools.all_types_equal(x1, x2) # test only some keys of a dict tools.assert_dict_with_all_types_equal({'a':1,'b':1}, {'a':1, 'b':3}, keys=['a']) # simple stuff tools.assert_all_types_equal(1, 1) tools.assert_all_types_equal(1.0, 1.0) tools.assert_all_types_equal(1.0, 1) tools.assert_all_types_equal(1, 1.0) tools.assert_all_types_equal([1], [1]) tools.assert_all_types_equal([1], [1.0]) tools.assert_all_types_equal('a', 'a') tools.assert_all_types_almost_equal(1.0, 1.0+1e-5) tools.assert_all_types_almost_equal(np.array([1.0]), np.array([1.0+1e-5])) assert not tools.all_types_equal(1, 2) assert not tools.all_types_equal(1.0, 1.1) assert not tools.all_types_equal([1], [1,2]) assert not tools.all_types_equal('a', 'b') # different types not allowed if strict=True assert not tools.all_types_equal(1.0, 1, strict=True) # test keys=[...], i.e. ignore some keys in both dicts x2 = copy.deepcopy(x1) x2['c'] = 1.0 assert tools.dict_with_all_types_equal(x1, x2, keys=['a','b',type(1)]) # fail on same array content but different dimensions a = np.random.rand(1,2,3) b = a[None,...] # this is True and IMHO a numpy bug b/c the dimensions are # different assert (a==b).all() # must catch that here assert not tools.array_equal(a,b) a = np.random.rand(1,2,3) b = (a + 1e-8)[None,...] assert not (a==b).all() # make sure they are numerically differrent assert np.allclose(a,b) # True but should be False assert not tools.array_almost_equal(a,b) # ok