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 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 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_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 load_rho(coordinates, numbers, fn_cube, ref_ugrid, stride, chop): '''Load densities from a file, reduce by stride, chop and check ugrid **Arguments:** coordinates An array with shape (N, 3) containing atomic coordinates. numbers A vector with shape (N,) containing atomic numbers. fn_cube The cube file with the electron density. ref_ugrid A reference ugrid that must match the one from the density cube file (after reduction). stride The reduction factor. chop The number of slices to chop of the grid in each direction. ''' if fn_cube is None: # Load the built-in database of proatoms natom = len(numbers) numbers = np.unique(numbers) proatomdb = ProAtomDB.from_refatoms(numbers, max_kation=0, max_anion=0, agspec='fine') # Construct the pro-density rho = np.zeros(ref_ugrid.shape) for i in xrange(natom): spline = proatomdb.get_spline(numbers[i]) ref_ugrid.eval_spline(spline, coordinates[i], rho) else: # Load cube mol_rho = IOData.from_file(fn_cube) rho = mol_rho.cube_data ugrid = mol_rho.grid # Reduce grid size if stride > 1: rho, ugrid = reduce_data(rho, ugrid, stride, chop) # Compare with ref_ugrid (only shape) if (ugrid.shape != ref_ugrid.shape).any(): raise ValueError( 'The densities file does not contain the same amount if information as the potential file.' ) return rho
def test_plot_atoms(): padb = ProAtomDB.from_refatoms(numbers=[8, 1], max_cation=1, max_anion=1) with tmpdir('horton.scripts.test.test_atomdb.test_plot_atoms') as dn: plot_atoms(padb, dn) fns = [ '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)
def test_plot_atoms(): padb = ProAtomDB.from_refatoms(numbers=[8, 1], max_cation=1, max_anion=1) with tmpdir("horton.scripts.test.test_atomdb.test_plot_atoms") as dn: plot_atoms(padb, dn) fns = [ "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)
def load_rho(system, fn_cube, ref_ugrid, stride, chop): '''Load densities from a file, reduce by stride, chop and check ugrid **Arguments:** system A Horton system object for the current system. This is only used to construct the pro-density. fn_cube The cube file with the electron density. ref_ugrid A reference ugrid that must match the one from the density cube file (after reduction). stride The reduction factor. chop The number of slices to chop of the grid in each direction. ''' if fn_cube is None: # Load the built-in database of proatoms numbers = np.unique(system.numbers) proatomdb = ProAtomDB.from_refatoms(numbers, max_kation=0, max_anion=0, agspec='fine') # Construct the pro-density rho = np.zeros(ref_ugrid.shape) for i in xrange(system.natom): spline = proatomdb.get_spline(system.numbers[i]) ref_ugrid.eval_spline(spline, system.coordinates[i], rho) else: # Load cube sys = System.from_file(fn_cube) rho = sys.extra['cube_data'] ugrid = sys.grid # Reduce grid size if stride > 1: rho, ugrid = reduce_data(rho, ugrid, stride, chop) # Compare with ref_ugrid (only shape) if (ugrid.shape != ref_ugrid.shape).any(): raise ValueError( 'The densities file does not contain the same amount if information as the potential file.' ) return rho
def load_rho(coordinates, numbers, fn_cube, ref_ugrid, stride, chop): '''Load densities from a file, reduce by stride, chop and check ugrid **Arguments:** coordinates An array with shape (N, 3) containing atomic coordinates. numbers A vector with shape (N,) containing atomic numbers. fn_cube The cube file with the electron density. ref_ugrid A reference ugrid that must match the one from the density cube file (after reduction). stride The reduction factor. chop The number of slices to chop of the grid in each direction. ''' if fn_cube is None: # Load the built-in database of proatoms natom = len(numbers) numbers = np.unique(numbers) proatomdb = ProAtomDB.from_refatoms(numbers, max_cation=0, max_anion=0, agspec='fine') # Construct the pro-density rho = np.zeros(ref_ugrid.shape) for i in xrange(natom): spline = proatomdb.get_spline(numbers[i]) ref_ugrid.eval_spline(spline, coordinates[i], rho) else: # Load cube mol_rho = IOData.from_file(fn_cube) rho = mol_rho.cube_data ugrid = mol_rho.grid # Reduce grid size if stride > 1: rho, ugrid = reduce_data(rho, ugrid, stride, chop) # Compare with ref_ugrid (only shape) if (ugrid.shape != ref_ugrid.shape).any(): raise ValueError('The densities file does not contain the same amount if information as the potential file.') return rho
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 load_rho(system, fn_cube, ref_ugrid, stride, chop): '''Load densities from a file, reduce by stride, chop and check ugrid **Arguments:** system A Horton system object for the current system. This is only used to construct the pro-density. fn_cube The cube file with the electron density. ref_ugrid A reference ugrid that must match the one from the density cube file (after reduction). stride The reduction factor. chop The number of slices to chop of the grid in each direction. ''' if fn_cube is None: # Load the built-in database of proatoms numbers = np.unique(system.numbers) proatomdb = ProAtomDB.from_refatoms(numbers, max_kation=0, max_anion=0, agspec='fine') # Construct the pro-density rho = np.zeros(ref_ugrid.shape) for i in xrange(system.natom): spline = proatomdb.get_spline(system.numbers[i]) ref_ugrid.eval_spline(spline, system.coordinates[i], rho) else: # Load cube sys = System.from_file(fn_cube) rho = sys.extra['cube_data'] ugrid = sys.grid # Reduce grid size if stride > 1: rho, ugrid = reduce_data(rho, ugrid, stride, chop) # Compare with ref_ugrid (only shape) if (ugrid.shape != ref_ugrid.shape).any(): raise ValueError('The densities file does not contain the same amount if information as the potential file.') return rho
def main_convert(args): # The atomic grid specification agspec = AtomicGridSpec(args.grid) # The program is detected based on the run script that is present run_scripts = glob("run_*.sh") if len(run_scripts) != 1: raise RuntimeError( 'Found %i run_*.sh scripts while exactly one is needed to know which program was used to run the atomic computations.' % len(run_scripts)) program = atom_programs[run_scripts[0][4:-3]] # Loop over all sensible directories energy_table = EnergyTable() records = [] for dn_state in sorted(glob("[01]??_??_[01]??_q[+-]??")): number = int(dn_state[:3]) pop = int(dn_state[7:10]) cases = [] for dn_mult in sorted(glob('%s/mult??' % dn_state)): if log.do_medium: log('Loading from', dn_mult) data, energy = program.load_atom(dn_mult) if energy is None: if log.do_medium: log('No (sensible) results found: ', dn_mult) continue cases.append((energy, data)) if len(cases) == 0: if log.do_medium: log('Nothing found in: ', dn_state) continue # Get the lowest in energy and write to chk file cases.sort() energy, data = cases[0] # Add case to energy table energy_table.add(number, pop, energy) # Write atom to HORTON file if possible if data is not None: data.to_file('%s/horton.h5' % dn_state) # Construct a record for the proatomdb records.append(ProAtomRecord.from_iodata(data, agspec)) # Release memory data = None del cases # Let user know we are alive. if log.do_medium: log('Succesfull: ', dn_state) # Report energies if log.do_medium: energy_table.log() # Write out atoms file proatomdb = ProAtomDB(records) proatomdb.to_file('atoms.h5') if log.do_medium: log('Written atoms.h5') # Make nice figures plot_atoms(proatomdb)
def main_convert(args): # The atomic grid specification agspec = AtomicGridSpec(args.grid) # The program is detected based on the run script that is present run_scripts = glob("run_*.sh") if len(run_scripts) != 1: raise RuntimeError('Found %i run_*.sh scripts while exactly one is needed to know which program was used to run the atomic computations.' % len(run_scripts)) program = atom_programs[run_scripts[0][4:-3]] # Loop over all sensible directories energy_table = EnergyTable() records = [] for dn_state in sorted(glob("[01]??_??_[01]??_q[+-]??")): number = int(dn_state[:3]) pop = int(dn_state[7:10]) cases = [] for dn_mult in sorted(glob('%s/mult??' % dn_state)): if log.do_medium: log('Loading from', dn_mult) data, energy = program.load_atom(dn_mult) if energy is None: if log.do_medium: log('No (sensible) results found: ', dn_mult) continue cases.append((energy, data)) if len(cases) == 0: if log.do_medium: log('Nothing found in: ', dn_state) continue # Get the lowest in energy and write to chk file cases.sort() energy, data = cases[0] # Add case to energy table energy_table.add(number, pop, energy) # Write atom to HORTON file if possible if data is not None: data.to_file('%s/horton.h5' % dn_state) # Construct a record for the proatomdb records.append(ProAtomRecord.from_iodata(data, agspec)) # Release memory data = None del cases # Let user know we are alive. if log.do_medium: log('Succesfull: ', dn_state) # Report energies if log.do_medium: energy_table.log() # Write out atoms file proatomdb = ProAtomDB(records) proatomdb.to_file('atoms.h5') if log.do_medium: log('Written atoms.h5') # Make nice figures plot_atoms(proatomdb)