def test_uig_eval_spline_0d_random(): cs = get_cosine_spline() for i in xrange(15): origin = np.random.uniform(-1, 1, 3) grid_cell = get_random_cell(0.2, 3) shape = np.random.randint(10, 20, 3) pbc = np.array([0, 0, 0]) uig = UniformGrid(origin, grid_cell.rvecs, shape, pbc) center = np.random.uniform(0, 2, 3) output1 = np.zeros(uig.shape) uig.eval_spline(cs, center, output1) x, y, z = np.indices(shape) x = x.ravel() y = y.ravel() z = z.ravel() rvecs = grid_cell.rvecs points = np.outer(x, rvecs[0]) + np.outer(y, rvecs[1]) + np.outer(z, rvecs[2]) points += origin distances = np.sqrt(((points-center)**2).sum(axis=1)) output2 = cs(distances) output2.shape = shape assert abs(output1 - output2).max() < 1e-10
def test_cell(): for i in xrange(12): chk = h5.File('horton.test.test_checkpoint.test_cell_%i' % i, driver='core', backing_store=False) cell1 = get_random_cell(1.0, i%4) coordinates = np.random.uniform(-1, 1, (5, 3)) numbers = np.random.randint(1, 11, 5) sys1 = System(coordinates, numbers, cell=cell1, chk=chk) del sys1 sys2 = System.from_file(chk) assert (sys2.cell.rvecs == cell1.rvecs).all() chk.close()
def setup_ranges_rcut(nvec): a = 10**np.random.uniform(-1, 1) cell = get_random_cell(a, nvec) origin = np.random.uniform(-3 * a, 3 * a, 3) center = np.random.uniform(-3 * a, 3 * a, 3) rcut = np.random.uniform(0.2 * a, 5 * a) ranges_begin, ranges_end = cell.get_ranges_rcut(origin - center, rcut) ranges_low = ranges_begin - 2 ranges_high = ranges_end + 2 return cell, origin, center, rcut, ranges_begin, ranges_end, ranges_low, ranges_high
def test_load_dump_consistency(): sys0 = System.from_file(context.get_fn('test/water_element.xyz')) sys0._cell = get_random_cell(5.0, 3) with tmpdir('horton.io.test.test_vasp.test_load_dump_consistency') as dn: sys0.to_file('%s/POSCAR' % dn) sys1 = System.from_file('%s/POSCAR' % dn) assert (sys1.numbers == [8, 1, 1]).all() assert abs(sys0.coordinates[1] - sys1.coordinates[0]).max() < 1e-10 assert abs(sys0.coordinates[0] - sys1.coordinates[1]).max() < 1e-10 assert abs(sys0.coordinates[2] - sys1.coordinates[2]).max() < 1e-10 assert abs(sys0.cell.rvecs - sys1.cell.rvecs).max() < 1e-10
def setup_ranges_rcut(nvec): a = 10**np.random.uniform(-1, 1) cell = get_random_cell(a, nvec) origin = np.random.uniform(-3*a, 3*a, 3) center = np.random.uniform(-3*a, 3*a, 3) rcut = np.random.uniform(0.2*a, 5*a) ranges_begin, ranges_end = cell.get_ranges_rcut(origin-center, rcut) ranges_low = ranges_begin-2 ranges_high = ranges_end+2 return cell, origin, center, rcut, ranges_begin, ranges_end, ranges_low, ranges_high
def setup_select_inside(nvec): a = 10**np.random.uniform(-1, 1) grid_cell = get_random_cell(a, nvec) nrep = 40 shape = np.array([nrep] * nvec) pbc = np.random.randint(0, 2, nvec) origin = np.random.uniform(-0.5 * nrep * a, 1.5 * nrep * a, 3) center = np.random.uniform(0, nrep * a, 3) rcut = np.random.uniform(0.1 * nrep * a) ranges_begin, ranges_end = grid_cell.get_ranges_rcut(origin - center, rcut) return grid_cell, origin, center, rcut, shape, pbc, ranges_begin, ranges_end
def test_symmetry_attrs(): generators = np.random.uniform(-1, 1, (5, 3, 4)) fracs = np.random.uniform(0, 1, (4, 3)) numbers = np.array([1, 6, 6, 1]) cell = get_random_cell(10.0, 3) s = Symmetry('boo', generators, fracs, numbers, cell) assert (s.labels == ['H0', 'C1', 'C2', 'H3']).all() s = Symmetry('boo', generators, fracs, numbers, cell, ['q', 'w', 'e', 'r']) assert s.name == 'boo' assert (s.generators == generators).all() assert s.natom == 4 assert s.fracs is s.fracs assert s.numbers is numbers assert s.cell is cell
def test_load_dump_consistency(): mol0 = IOData.from_file(context.get_fn('test/water_element.xyz')) mol0.cell = get_random_cell(5.0, 3) with tmpdir('horton.io.test.test_vasp.test_load_dump_consistency') as dn: mol0.to_file('%s/POSCAR' % dn) mol1 = IOData.from_file('%s/POSCAR' % dn) assert mol0.title == mol1.title assert (mol1.numbers == [8, 1, 1]).all() assert abs(mol0.coordinates[1] - mol1.coordinates[0]).max() < 1e-10 assert abs(mol0.coordinates[0] - mol1.coordinates[1]).max() < 1e-10 assert abs(mol0.coordinates[2] - mol1.coordinates[2]).max() < 1e-10 assert abs(mol0.cell.rvecs - mol1.cell.rvecs).max() < 1e-10
def test_symmetry_attrs(): generators = np.random.uniform(-1, 1, (5, 3, 4)) fracs = np.random.uniform(0, 1, (4, 3)) numbers = np.array([1, 6, 6, 1]) cell = get_random_cell(10.0, 3) s = Symmetry("boo", generators, fracs, numbers, cell) assert (s.labels == ["H0", "C1", "C2", "H3"]).all() s = Symmetry("boo", generators, fracs, numbers, cell, ["q", "w", "e", "r"]) assert s.name == "boo" assert (s.generators == generators).all() assert s.natom == 4 assert s.fracs is s.fracs assert s.numbers is numbers assert s.cell is cell
def test_pair_ewald3d_invariance_rcut(): np.random.seed(0) alpha_scale = 4.5 gcut_scale = 1.5 delta = np.random.normal(0, 1, 3) delta /= np.linalg.norm(delta) cell = get_random_cell(1.0, 3) results = [] for rcut in np.arange(10.0, 20.001, 1.0): alpha = alpha_scale / rcut gcut = gcut_scale * alpha results.append(pair_ewald(delta, cell, rcut, alpha, gcut)) results = np.array(results) assert abs(results - results.mean()).max() < 1e-7
def test_pair_ewald3d_invariance_rcut(): np.random.seed(0) alpha_scale = 4.5 gcut_scale = 1.5 delta = np.random.normal(0, 1, 3) delta /= np.linalg.norm(delta) cell = get_random_cell(1.0, 3) results = [] for rcut in np.arange(10.0, 20.001, 1.0): alpha = alpha_scale/rcut gcut = gcut_scale*alpha results.append(pair_ewald(delta, cell, rcut, alpha, gcut)) results = np.array(results) assert abs(results - results.mean()).max() < 1e-7
def setup_select_inside(nvec): a = 10**np.random.uniform(-1, 1) grid_cell = get_random_cell(a, nvec) nrep = 40 shape = np.array([nrep]*nvec) pbc = np.random.randint(0, 2, nvec) origin = np.random.uniform(-0.5*nrep*a, 1.5*nrep*a, 3) center = np.random.uniform(0, nrep*a, 3) rcut = np.random.uniform(0.1*nrep*a) ranges_begin, ranges_end = grid_cell.get_ranges_rcut(origin-center, rcut) return grid_cell, origin, center, rcut, shape, pbc, ranges_begin, ranges_end
def test_eval_spline_grid_3d_random(): npoint = 10 for i in xrange(10): cell = get_random_cell(1.0, 3) rvecs = cell.rvecs points = np.dot(np.random.normal(-2, 3, (npoint, 3)), rvecs) g = IntGrid(points, np.random.normal(0, 1.0, npoint)) cs = get_cosine_spline() output1 = np.zeros(npoint) center1 = np.random.uniform(-10, 10, 3) g.eval_spline(cs, center1, output1, cell) output2 = np.zeros(npoint) center2 = center1 + np.dot(np.random.randint(-3, 3, 3), rvecs) g.eval_spline(cs, center2, output2, cell) assert abs(output1 - output2).max() < 1e-10
def test_eval_spline_grid_3d_random(): npoint = 10 for i in xrange(10): cell = get_random_cell(1.0, 3) rvecs = cell.rvecs points = np.dot(np.random.normal(-2, 3, (npoint,3)), rvecs) g = IntGrid(points, np.random.normal(0, 1.0, npoint)) cs = get_cosine_spline() output1 = np.zeros(npoint) center1 = np.random.uniform(-10, 10, 3) g.eval_spline(cs, center1, output1, cell) output2 = np.zeros(npoint) center2 = center1 + np.dot(np.random.randint(-3, 3, 3), rvecs) g.eval_spline(cs, center2, output2, cell) assert abs(output1 - output2).max() < 1e-10
def test_uig_eval_spline_3d_random(): cs = get_cosine_spline() for i in xrange(30): origin = np.random.uniform(-1, 1, 3) grid_cell = get_random_cell(0.3, 3) shape = np.random.randint(10, 20, 3) pbc = np.array([1, 1, 1]) uig = UniformGrid(origin, grid_cell.rvecs, shape, pbc) rvecs = uig.get_cell().rvecs output1 = np.zeros(uig.shape) center1 = np.random.uniform(-3, 3, 3) uig.eval_spline(cs, center1, output1) output2 = np.zeros(uig.shape) center2 = center1 + np.dot(np.random.randint(-3, 3, 3), rvecs) uig.eval_spline(cs, center2, output2) assert abs(output1 - output2).max() < 1e-10
def test_eval_spline_grid_add_random(): npoint = 10 cs = get_cosine_spline() for i in xrange(10): cell = get_random_cell(1.0, np.random.randint(4)) points = np.random.normal(-2, 3, (npoint,3)) g = IntGrid(points, np.random.normal(0, 1.0, npoint)) output1 = np.zeros(npoint) center1 = np.random.uniform(-2, 2, 3) g.eval_spline(cs, center1, output1, cell) output2 = np.zeros(npoint) center2 = np.random.uniform(-2, 2, 3) g.eval_spline(cs, center2, output2, cell) output3 = np.zeros(npoint) g.eval_spline(cs, center1, output3, cell) g.eval_spline(cs, center2, output3, cell) assert abs(output1 + output2 - output3).max() < 1e-10
def test_uig_eval_spline_1d_random(): cs = get_cosine_spline() for i in xrange(15): origin = np.random.uniform(-1, 1, 3) grid_cell = get_random_cell(0.2, 3) shape = np.random.randint(10, 20, 3) pbc = np.array([0, 0, 0]) pbc[np.random.randint(3)] = 1 uig = UniformGrid(origin, grid_cell.rvecs, shape, pbc) tmp = uig.shape*pbc rvecs = grid_cell.rvecs*tmp.reshape(-1,1) output1 = np.zeros(uig.shape) center1 = np.random.uniform(0, 1, 3) uig.eval_spline(cs, center1, output1) output2 = np.zeros(uig.shape) center2 = center1 + np.dot(np.random.randint(-3, 3, 3), rvecs) uig.eval_spline(cs, center2, output2) assert abs(output1 - output2).max() < 1e-10
def test_eval_spline_grid_add_random(): npoint = 10 cs = get_cosine_spline() for i in xrange(10): cell = get_random_cell(1.0, np.random.randint(4)) points = np.random.normal(-2, 3, (npoint, 3)) g = IntGrid(points, np.random.normal(0, 1.0, npoint)) output1 = np.zeros(npoint) center1 = np.random.uniform(-2, 2, 3) g.eval_spline(cs, center1, output1, cell) output2 = np.zeros(npoint) center2 = np.random.uniform(-2, 2, 3) g.eval_spline(cs, center2, output2, cell) output3 = np.zeros(npoint) g.eval_spline(cs, center1, output3, cell) g.eval_spline(cs, center2, output3, cell) assert abs(output1 + output2 - output3).max() < 1e-10
def test_uig_eval_spline_add_random(): cs = get_cosine_spline() for i in xrange(20): origin = np.random.uniform(-1, 1, 3) grid_cell = get_random_cell(0.2, 3) shape = np.random.randint(10, 20, 3) pbc = np.random.randint(0, 2, 3).astype(int) uig = UniformGrid(origin, grid_cell.rvecs, shape, pbc) output1 = np.zeros(uig.shape) center1 = np.random.uniform(0, 1, 3) uig.eval_spline(cs, center1, output1) output2 = np.zeros(uig.shape) center2 = np.random.uniform(0, 1, 3) uig.eval_spline(cs, center2, output2) output3 = np.zeros(uig.shape) uig.eval_spline(cs, center1, output3) uig.eval_spline(cs, center2, output3) assert abs(output1 + output2 - output3).max() < 1e-10
def test_from_parameters3(): for i in xrange(10): cell0 = get_random_cell(1.0, 3) check_from_parameters(cell0)