def test_script_input_orca(): with tmpdir( 'horton.scripts.test.test_atomdb.test_script_input_orca') as dn: fn_template = 'template_atomdb_orca.in' copy_files(dn, [fn_template]) check_script( 'horton-atomdb.py input orca H,13 %s --no-hund' % fn_template, dn) fns = [ '001__h_003_q-02/mult02/atom.in', '001__h_003_q-02/mult04/atom.in', '001__h_002_q-01/mult01/atom.in', '001__h_002_q-01/mult03/atom.in', '001__h_001_q+00/mult02/atom.in', '013_al_015_q-02/mult04/atom.in', '013_al_015_q-02/mult02/atom.in', '013_al_014_q-01/mult03/atom.in', '013_al_014_q-01/mult05/atom.in', '013_al_014_q-01/mult01/atom.in', '013_al_013_q+00/mult02/atom.in', '013_al_013_q+00/mult04/atom.in', '013_al_012_q+01/mult01/atom.in', '013_al_012_q+01/mult03/atom.in', '013_al_011_q+02/mult02/atom.in', '013_al_010_q+03/mult01/atom.in', 'run_orca.sh', ] check_files(dn, fns)
def test_script_convert_g03(): with tmpdir( 'horton.scripts.test.test_atomdb.test_script_convert_g03') as dn: copy_atom_output('atom_001_001_hf_sto3g.fchk', 1, 0, 2, dn, 'atom.fchk') copy_atom_output('atom_001_002_hf_sto3g.fchk', 1, -1, 1, dn, 'atom.fchk') copy_atom_output('atom_008_007_hf_sto3g.fchk', 8, +1, 4, dn, 'atom.fchk') copy_atom_output('atom_008_008_hf_sto3g.fchk', 8, 0, 3, dn, 'atom.fchk') copy_atom_output('atom_008_009_hf_sto3g.fchk', 8, -1, 2, dn, 'atom.fchk') make_fake_run_script('g03', dn) check_script('horton-atomdb.py convert', dn) # check presence of files fns = [ 'atoms.h5', 'dens_001__h.png', 'rdens_001__h.png', 'fukui_001__h.png', 'rfukui_001__h.png', 'dens_008__o.png', 'rdens_008__o.png', 'fukui_008__o.png', 'rfukui_008__o.png', ] check_files(dn, fns) # load proatomdb file and check some contents padb = ProAtomDB.from_file(os.path.join(dn, 'atoms.h5')) assert padb.get_numbers() == [1, 8] assert padb.get_charges(1) == [0, -1] assert padb.get_charges(8) == [+1, 0, -1]
def check_script_lta(fn_sym, suffix): with tmpdir('horton.scripts.test.test_cpart.test_script_lta_coarse_h_%s' % suffix) as dn: # prepare files if fn_sym is not None: copy_files(dn, [fn_sym]) write_atomdb_refatoms(dn) # write a random cube file fn_cube = 'dens.cube' sys = write_random_lta_cube(dn, fn_cube) # run the script fn_h5 = '%s_cpart.h5' % fn_cube[:-5] if fn_sym is None: check_script('horton-cpart.py %s %s:cpart/h_r1 h atoms.h5' % (fn_cube, fn_h5), dn) else: check_script('horton-cpart.py %s %s:cpart/h_r1 h atoms.h5 --symmetry=%s' % (fn_cube, fn_h5, fn_sym), dn) # check the output check_files(dn, [fn_h5]) with h5.File(os.path.join(dn, fn_h5)) as f: assert 'cpart' in f assert 'h_r1' in f['cpart'] if fn_sym is not None: assert 'symmetry' in f['cpart/h_r1'] assert 'charges' in f['cpart/h_r1/symmetry'] assert 'cartesian_multipoles' in f['cpart/h_r1/symmetry'] for name, ds in f['cpart/h_r1/symmetry'].iteritems(): assert ds.shape[0] == sys.extra['symmetry'].natom assert ds.shape[1] == 2
def check_script_water_sto3g(scheme, do_deriv=True): with tmpdir('horton.scripts.test.test_wpart.test_script_water_sto3g_%s' % scheme) as dn: fn_fchk = 'water_sto3g_hf_g03.fchk' copy_files(dn, [fn_fchk]) if scheme == 'b': check_script( 'horton-wpart.py %s water_sto3g_hf_g03_wpart.h5:wpart %s --debug' % (fn_fchk, scheme), dn) else: write_atomdb_sto3g(dn, do_deriv) check_script( 'horton-wpart.py %s water_sto3g_hf_g03_wpart.h5:wpart %s atoms.h5 --slow' % (fn_fchk, scheme), dn) fn_h5 = 'water_sto3g_hf_g03_wpart.h5' check_files(dn, [fn_h5]) with h5.File(os.path.join(dn, fn_h5)) as f: assert 'wpart' in f assert abs(f['wpart/charges'][:].sum()) < 1e-2 assert 'wpart/spin_charges' not in f assert 'atom_00000' in f['wpart'] for s in 'density_decomposition', 'hartree_decomposition': assert s in f['wpart']['atom_00000'] assert 'spline_00000' in f['wpart']['atom_00000'][s] assert 'rtransform' in f['wpart']['atom_00000'][s][ 'spline_00000'].attrs assert 'extrapolation' in f['wpart']['atom_00000'][s][ 'spline_00000'].attrs assert 'y' in f['wpart']['atom_00000'][s]['spline_00000'] assert 'd' in f['wpart']['atom_00000'][s]['spline_00000'] if scheme != 'b': assert 'spline_prodensity' in f['wpart']['atom_00000'] assert 'spline_prohartree' in f['wpart']['atom_00000']
def check_script_jbw_coarse(scheme): with tmpdir('horton.scripts.test.test_cpart.test_script_jbw_coarse_%s' % scheme) as dn: fn_cube = 'jbw_coarse_aedens.cube' copy_files(dn, [fn_cube]) write_atomdb_refatoms(dn) fn_h5 = 'foobar.h5' check_script('horton-cpart.py %s foobar.h5:cpart/%s_r1 %s atoms.h5' % (fn_cube, scheme, scheme), dn) check_files(dn, [fn_h5]) with h5.File(os.path.join(dn, fn_h5)) as f: assert 'cpart' in f assert scheme + '_r1' in f['cpart']
def test_script_convert_g09(): with tmpdir('horton.scripts.test.test_atomdb.test_script_convert_g09') as dn: copy_atom_output('atom_014_013_hf_lan.fchk', 14, +1, 2, dn, 'atom.fchk') make_fake_run_script('g09', dn) check_script('horton-atomdb.py convert --grid medium', dn) # check presence of files fns = ['atoms.h5', 'dens_014_si.png', 'rdens_014_si.png', 'fukui_014_si.png', 'rfukui_014_si.png'] check_files(dn, fns) # load proatomdb file and check some contents padb = ProAtomDB.from_file(os.path.join(dn, 'atoms.h5')) assert padb.get_numbers() == [14] assert padb.get_charges(14) == [+1] assert padb.get_rgrid(14).size == 49
def test_scripts_symmetry(): # Write the cube file to the tmpdir and run scripts with tmpdir('horton.scripts.test.test_espfit.test_scripts_symmetry') as dn: # prepare files sys = write_random_lta_cube(dn, 'esp.cube') copy_files(dn, ['lta_gulp.cif']) # run scripts check_script('horton-esp-cost.py esp.cube esp.h5 --wnear=0:1.0:0.5 --rcut=4 --alpha-scale=0.1', dn) check_files(dn, ['esp.h5']) check_script('horton-esp-fit.py esp.h5 other.h5 --symmetry esp.cube lta_gulp.cif', dn) sys_sym = System.from_file('%s/lta_gulp.cif' % dn) with h5.File(os.path.join(dn, 'other.h5')) as f: assert 'symmetry' in f assert f['symmetry/charges'].shape == (sys_sym.extra['symmetry'].natom, 2)
def test_script(): with tmpdir('horton.scripts.test.test_hdf2csv.test_script') as dn: with h5.File('%s/test.h5' % dn) as f: fill_hdf5(f) check_script('horton-hdf2csv.py test.h5:/ test.csv', dn) check_files(dn, ['test.h5', 'test.csv']) with open('%s/test.csv' % dn) as f: r = csv.reader(f) rows = [row for row in r] with h5.File('%s/test.h5' % dn) as f: assert rows[0][0] == 'Converted data from test.h5:/' assert len(rows[1]) == 0 assert len(rows[2]) == 2 assert rows[2][0] == 'Dataset' assert rows[2][1] == 'bar/foo' assert len(rows[3]) == 3 assert rows[3][0] == 'Shape' assert rows[3][1] == '5' assert rows[3][2] == '2' assert float(rows[4][0]) == f['bar/foo'][0,0] assert float(rows[8][1]) == f['bar/foo'][4,1] assert len(rows[9]) == 0 assert len(rows[22]) == 2 assert rows[22][0] == 'Dataset' assert rows[22][1] == 'test2' assert len(rows[23]) == 3 assert rows[23][0] == 'Shape' assert rows[23][1] == '5' assert rows[23][2] == '2' assert float(rows[24][0]) == f['test2'][0,0] assert float(rows[28][1]) == f['test2'][4,1] assert rows[29] == [] assert len(rows[30]) == 2 assert rows[30][0] == 'Dataset' assert rows[30][1] == 'zzz' assert len(rows[31]) == 4 assert rows[31][0] == 'Shape' assert rows[31][1] == '5' assert rows[31][2] == '2' assert rows[31][3] == '5' assert float(rows[32][0]) == f['zzz'][0,0,0] assert float(rows[32][1]) == f['zzz'][0,1,0] assert rows[32][2] == '' assert float(rows[32][3]) == f['zzz'][0,0,1] assert float(rows[32][4]) == f['zzz'][0,1,1] assert rows[32][5] == '' assert len(rows[32]) == 3*5-1 assert float(rows[36][1]) == f['zzz'][4,1,0] assert float(rows[36][3*5-2]) == f['zzz'][4,1,4] assert rows[-1] == []
def check_script_input_gaussian(binary): with tmpdir('horton.scripts.test.test_atomdb.test_script_input_%s' % binary) as dn: fn_template = 'template_atomdb_gaussian.in' fn_basis1 = 'include_atomdb_gaussian_basis.001_000_00' fn_basis8 = 'include_atomdb_gaussian_basis.008_000_00' copy_files(dn, [fn_template, fn_basis1, fn_basis8]) check_script('horton-atomdb.py input %s 1,O %s' % (binary, fn_template), dn) fns = [ '001__h_003_q-02/mult02/atom.in', '001__h_002_q-01/mult01/atom.in', '001__h_001_q+00/mult02/atom.in', '008__o_010_q-02/mult01/atom.in', '008__o_009_q-01/mult02/atom.in', '008__o_008_q+00/mult03/atom.in', '008__o_007_q+01/mult04/atom.in', '008__o_006_q+02/mult03/atom.in', '008__o_005_q+03/mult02/atom.in', 'run_%s.sh' % binary, ] check_files(dn, fns)
def test_script_input_cp2k(): with tmpdir('horton.scripts.test.test_atomdb.test_script_input_cp2k') as dn: fn_template = 'template_atomdb_cp2k.in' fn_valence = 'include_atomdb_cp2k_valence.inc' fn_ppot = 'include_atomdb_cp2k_ppot.inc' copy_files(dn, [fn_template, fn_valence, fn_ppot]) check_script('horton-atomdb.py input cp2k Ca,F %s' % fn_template, dn) fns = [ '020_ca_021_q-01/mult02/atom.in', '020_ca_020_q+00/mult01/atom.in', '020_ca_019_q+01/mult02/atom.in', '020_ca_018_q+02/mult01/atom.in', '020_ca_017_q+03/mult02/atom.in', '009__f_010_q-01/mult01/atom.in', '009__f_009_q+00/mult02/atom.in', '009__f_008_q+01/mult03/atom.in', '009__f_007_q+02/mult04/atom.in', 'run_cp2k.sh', ] check_files(dn, fns)
def check_script_lta(fn_sym, suffix, do_spin=False): with tmpdir('horton.scripts.test.test_cpart.test_script_lta_coarse_h_%s' % suffix) as dn: # prepare files if fn_sym is not None: copy_files(dn, [fn_sym]) write_atomdb_refatoms(dn) # write a random cube file fn_cube = 'dens.cube' mol = write_random_lta_cube(dn, fn_cube) # if needed, write a random spin cube file if do_spin: fn_spin = 'spin.cube' molspin = write_random_lta_cube(dn, fn_spin) # run the script fn_h5 = '%s_cpart.h5' % fn_cube[:-5] opts = '' if not (fn_sym is None): opts += ' --symmetry=%s' % fn_sym if do_spin: opts += ' --spindens=%s' % fn_spin check_script( 'horton-cpart.py %s %s:cpart/h_r1 h atoms.h5 %s' % (fn_cube, fn_h5, opts), dn) # check the output check_files(dn, [fn_h5]) with h5.File(os.path.join(dn, fn_h5)) as f: assert 'cpart' in f assert 'h_r1' in f['cpart'] assert 'charges' in f['cpart/h_r1'] if do_spin: assert 'spin_charges' in f['cpart/h_r1'] if fn_sym is not None: assert 'symmetry' in f['cpart/h_r1'] assert 'charges' in f['cpart/h_r1/symmetry'] if do_spin: assert 'spin_charges' in f['cpart/h_r1/symmetry'] assert 'cartesian_multipoles' in f['cpart/h_r1/symmetry'] for name, ds in f['cpart/h_r1/symmetry'].iteritems(): assert ds.shape[0] == mol.symmetry.natom assert ds.shape[1] == 2
def test_script_convert_cp2k(): with tmpdir('horton.scripts.test.test_atomdb.test_script_convert_cp2k') as dn: copy_atom_output('atom_op2.cp2k.out', 8, +2, 3, dn, 'atom.cp2k.out') copy_atom_output('atom_op1.cp2k.out', 8, +1, 4, dn, 'atom.cp2k.out') copy_atom_output('atom_o.cp2k.out', 8, 0, 2, dn, 'atom.cp2k.out') copy_atom_output('atom_om1.cp2k.out', 8, -1, 1, dn, 'atom.cp2k.out') copy_atom_output('atom_om2.cp2k.out', 8, -2, 0, dn, 'atom.cp2k.out') make_fake_run_script('cp2k', dn) check_script('horton-atomdb.py convert', dn) # check presence of files fns = ['atoms.h5', 'dens_008__o.png', 'rdens_008__o.png', 'fukui_008__o.png', 'rfukui_008__o.png'] check_files(dn, fns) # load proatomdb file and check some contents padb = ProAtomDB.from_file(os.path.join(dn, 'atoms.h5')) assert padb.get_numbers() == [8] assert padb.get_charges(8) == [+2, +1, 0, -1, -2] assert not padb.get_record(8, -2).safe assert padb.get_rgrid(8).size == 71
def check_script_water_sto3g(scheme, do_deriv=True): with tmpdir('horton.scripts.test.test_wpart.test_script_water_sto3g_%s' % scheme) as dn: fn_fchk = 'water_sto3g_hf_g03.fchk' copy_files(dn, [fn_fchk]) if scheme == 'b': check_script( 'horton-wpart.py %s water_sto3g_hf_g03_wpart.h5:wpart/%s %s --debug' % (fn_fchk, scheme, scheme), dn) else: write_atomdb_sto3g(dn, do_deriv) check_script( 'horton-wpart.py %s water_sto3g_hf_g03_wpart.h5:wpart/%s %s atoms.h5' % (fn_fchk, scheme, scheme), dn) fn_h5 = 'water_sto3g_hf_g03_wpart.h5' check_files(dn, [fn_h5]) with h5.File(os.path.join(dn, fn_h5)) as f: assert 'wpart' in f assert scheme in f['wpart']
def check_script_ch3_rohf_sto3g(scheme, do_deriv=True): with tmpdir( 'horton.scripts.test.test_wpart.test_script_ch3_rohf_sto3g_%s' % scheme) as dn: fn_fchk = 'ch3_rohf_sto3g_g03.fchk' copy_files(dn, [fn_fchk]) if scheme == 'b': check_script( 'horton-wpart.py %s foo.h5:wpart %s --debug' % (fn_fchk, scheme), dn) else: write_atomdb_sto3g(dn, do_deriv) check_script( 'horton-wpart.py %s foo.h5:wpart %s atoms.h5' % (fn_fchk, scheme), dn) fn_h5 = 'foo.h5' check_files(dn, [fn_h5]) with h5.File(os.path.join(dn, fn_h5)) as f: assert 'wpart' in f assert abs(f['wpart/charges'][:].sum()) < 1e-2 assert abs(f['wpart/spin_charges'][:].sum() - 1) < 1e-2
def test_example_pt_mp2_water_cc_pvdz(): check_script('./mp2_water_cc-pvdz.py', context.get_fn('examples/perturbation_theory'))
def test_example_pt_mp2_water_431g(): check_script('./mp2_water_4-31g.py', context.get_fn('examples/perturbation_theory'))
def test_example_localization(): check_script('./water_pm.py', context.get_fn('examples/localization'))
def test_script(): with tmpdir('horton.scripts.test.test_cubehead.test_script') as dn: fn_fchk = 'water_sto3g_hf_g03.fchk' copy_files(dn, [fn_fchk]) check_script('horton-cubehead.py %s cubehead.txt' % fn_fchk, dn) check_files(dn, ['cubehead.txt'])
def test_example_hamiltonian_hubbard(): check_script('horton-regression-test.py ./hubbard.py', context.get_fn('examples/hamiltonian'))
def test_example_hamiltonian_even_tempered_li(): check_script('horton-regression-test.py ./even_tempered_li.py', context.get_fn('examples/hamiltonian'))
def test_example_hubbard(): check_script('./hubbard.py', context.get_fn('examples/hamiltonian'))
def test_example_pt_mp2_water_def2_svpd(): check_script('./mp2_water_def2-svpd.py', context.get_fn('examples/perturbation_theory'))
def test_example_003_o3lyp_water(): check_script('./run.py', context.get_fn('examples/003_o3lyp_water'))
def test_scripts(): # Generate some random system with random esp data natom = 5 numbers = np.random.randint(1, 20, natom) coordinates = np.random.uniform(0, 10, (natom, 3)) origin = np.zeros(3, float) grid_rvecs = np.identity(3, float) * 1.0 shape = np.array([10, 10, 10]) pbc = np.ones(3, int) ugrid = UniformGrid(origin, grid_rvecs, shape, pbc) esp_cube_data = np.random.uniform(-1, 1, shape) rho_cube_data = np.random.uniform(-1, 1, shape) mol_esp = IOData(coordinates=coordinates, numbers=numbers, grid=ugrid, cube_data=esp_cube_data) mol_rho = mol_esp.copy() mol_rho.cube_data = rho_cube_data # Write the cube file to the tmpdir and run scripts (run 1) with tmpdir('horton.scripts.test.test_espfit.test_scripts') as dn: mol_esp.to_file(os.path.join(dn, 'esp.cube')) check_script('horton-esp-cost.py esp.cube esp.h5 --wnear=0:1.0:0.5', dn) check_script('horton-esp-fit.py esp.h5 other.h5', dn) check_script('horton-esp-test.py esp.h5 other.h5:charges foo.h5', dn) check_script('horton-esp-gen.py other.h5:charges esp.cube gen.h5', dn) check_files(dn, ['esp.h5', 'other.h5', 'foo.h5', 'gen.h5']) # Write the cube file to the tmpdir and run scripts (run 2) with tmpdir('horton.scripts.test.test_espfit.test_scripts2') as dn: mol_esp.to_file(os.path.join(dn, 'esp.cube')) mol_rho.to_file(os.path.join(dn, 'rho.cube')) check_script( 'horton-esp-cost.py esp.cube esp.h5 --wnear=0:1.0:0.5 --wdens=rho.cube --wsave=weight.cube', dn) check_files(dn, ['esp.h5', 'weight.cube']) check_script('horton-esp-fit.py esp.h5 other.h5', dn) check_script('horton-esp-test.py esp.h5 other.h5:charges foo.h5', dn) check_script('horton-esp-gen.py other.h5:charges esp.cube gen.h5', dn) check_files(dn, ['esp.h5', 'other.h5', 'foo.h5', 'gen.h5'])
def test_example_grid_expectation_r(): check_script('horton-regression-test.py ./expectation_r.py', context.get_fn('examples/grid'))
def test_example_even_tempered_li(): check_script('./even_tempered_li.py', context.get_fn('examples/hamiltonian'))
def test_scripts(): with tmpdir('horton.scripts.test.test_convert.test_scripts') as dn: fn_fchk = 'water_sto3g_hf_g03.fchk' copy_files(dn, [fn_fchk]) check_script('horton-convert.py %s test.xyz' % fn_fchk, dn)
def test_example_expectation_r(): check_script('./expectation_r.py', context.get_fn('examples/grid'))
def test_example_004_wpart(): check_script('./run.py', context.get_fn('examples/004_wpart'))