def calculate_crest_conformer_energies(molecules, charges, suffix): energies = {} for molecule in molecules: energy_file = f'{molecule}_{suffix}.ey' if exists(energy_file): # Read .ey file. with open(energy_file, 'r') as f: lines = f.readlines() for line in lines: energy = float(line.rstrip()) break else: print(f'>> calculating energy of {molecule}') xtb_energy = stko.XTBEnergy( xtb_path='/home/atarzia/software/xtb-190806/bin/xtb', output_dir=f'ey_{molecule}', num_cores=1, charge=charges[molecule], num_unpaired_electrons=0, electronic_temperature=300, unlimited_memory=True, solvent='DMSO', solvent_grid='verytight') energy = xtb_energy.get_energy(molecules[molecule]) # Save to .ey file. with open(energy_file, 'w') as f: f.write(f'{energy}\n') # to kJ/mol. energies[molecule] = energy * 2625.5 return energies
def calculate_energy(name, mol, ey_file, gfn_exec=None, charge=0, no_unpaired_e=0, solvent=None): """ Calculate GFN-xTB energy of molecule. """ if gfn_exec is None: gfn_exec = '/home/atarzia/software/xtb-190806/bin/xtb' print(f'....getting energy of {name}') if solvent is None: solvent_str = None solvent_grid = 'normal' else: solvent_str, solvent_grid = solvent xtb_energy = stko.XTBEnergy(xtb_path=gfn_exec, output_dir=f'{name}_ey', num_cores=6, charge=charge, num_unpaired_electrons=no_unpaired_e, electronic_temperature=300, unlimited_memory=True, calculate_free_energy=False, solvent=solvent_str, solvent_grid=solvent_grid) energy = xtb_energy.get_energy(mol) with open(ey_file, 'w') as f: f.write(str(energy))
def get_energy(name, cage, solvent=None): """ Get xTB energy of a cage. Parameters ---------- name : :class:`str` Name identifying cage. cage : :class:`stk.ConstructedMolecule` Cage to get energy of. solvent : :class:`tuple` of :class:`str`, optional Strings identifying solvent and grid for GBSA model in xTB. Returns ------- :class:`float` Energy of structure from GFN2-xTB in kJ/mol. """ energy_file = f'{name}_optc.ey' if exists(energy_file): # Read .ey file. with open(energy_file, 'r') as f: lines = f.readlines() for line in lines: energy = float(line.rstrip()) break else: print(f'calculating energy of {name}') # Extract energy. if solvent is None: solvent_str = None solvent_grid = 'normal' else: solvent_str, solvent_grid = solvent xtb_energy = stko.XTBEnergy( xtb_path='/home/atarzia/software/xtb-190806/bin/xtb', output_dir=f'calculations_output/cage_ey_{name}', num_cores=6, charge=4, num_unpaired_electrons=0, electronic_temperature=300, unlimited_memory=True, solvent=solvent_str, solvent_grid=solvent_grid) energy = xtb_energy.get_energy(cage) # Save to .ey file. with open(energy_file, 'w') as f: f.write(f'{energy}\n') return energy * 2625.5
def main(): if len(sys.argv) > 1: xtb_path = sys.argv[1] else: xtb_path = None bb1 = stk.BuildingBlock('NCCNCCN', [stk.PrimaryAminoFactory()]) # Run calculations. calculations = [] uff = stko.UFFEnergy() calculations.append(uff.calculate(bb1)) uff_results = uff.get_results(bb1) mmff = stko.MMFFEnergy() calculations.append(mmff.calculate(bb1)) mmff_results = mmff.get_results(bb1) print( uff_results, # Get energy from results. uff_results.get_energy(), uff_results.get_unit_string(), # Get energy directly through Calculator. uff.get_energy(bb1), ) print( mmff_results, # Get energy from results. mmff_results.get_energy(), mmff_results.get_unit_string(), # Get energy directly through Calculator. mmff.get_energy(bb1), ) if xtb_path is not None: print('doing XTB calculation.') xtb = stko.XTBEnergy( xtb_path=xtb_path, output_dir='output_directory/xtb_out', unlimited_memory=True, ) xtb_results = xtb.get_results(bb1) print(xtb_results) # Extract properties from the energy calculator for a given # molecule. total_energy = xtb_results.get_total_energy() homo_lumo_gap = xtb_results.get_homo_lumo_gap() fermi_levels = xtb_results.get_fermi_level() homo_lumo_orbitals = xtb_results.get_homo_lumo_orbitals() full_dipole_moments = xtb_results.get_full_dipole_moments() print( total_energy, homo_lumo_gap, homo_lumo_orbitals, fermi_levels, full_dipole_moments, ) # From results, vs from calculator. print(xtb.get_energy(bb1), total_energy) try: xtb_results.get_total_free_energy() except AttributeError: print('Expected fail') # Try yielded option. calculations.append(xtb.calculate((bb1))) # Run through yield statements using the `.calculate` method # all in once. for i in calculations: print( f'{i}: xtb will not output anything as you need a new ' 'Results.' ) ey = stko.EnergyResults(i, 'kcal mol-1') print(ey.get_energy(), ey.get_unit_string()) # Zipping calculations together. mols = [ stk.BuildingBlock('C1CCC1'), stk.BuildingBlock('C1CCCC1'), stk.BuildingBlock('C1CCCCC1'), ] for mol in mols: print( mol, stko.EnergyResults( mmff.calculate(mol), 'kcal mol-1' ).get_energy() )
def MOC_xtb_conformers(cage, cage_name, etemp, output_dir, conformer_dir, nc, free_e, charge, gfn_exec=None, opt=False, opt_level=None, solvent=None, handle_failure=False): """ Perform GFN2-xTB conformer scan of MOC. Parameters ---------- cage : :class:`stk.ConstructedMolecule` Cage to be optimised. cage_name : :class:`str` Name of cage. Returns ------- cage : :class:`stk.ConstructedMolecule` Optimised cage. """ if gfn_exec is None: gfn_exec = '/home/atarzia/software/xtb-190806/bin/xtb' if not exists(output_dir): os.mkdir(output_dir) if solvent is None: solvent_str = None solvent_grid = 'normal' else: solvent_str, solvent_grid = solvent print(f'..........doing XTB conformer sorting by ' f'energy of {cage_name}') conformers = glob.glob(f'{conformer_dir}/conf_*.xyz') ids = [] energies = [] min_energy = 10E20 for file in sorted(conformers): id = file.replace('.xyz', '').split('_')[-1] cage = cage.with_structure_from_file(file) opt_failed = False if opt: print(f'optimising conformer {id}') xtb_opt = stko.XTB(xtb_path=gfn_exec, output_dir=f'opt_{cage_name}_{id}', gfn_version=2, num_cores=nc, opt_level=opt_level, charge=charge, num_unpaired_electrons=free_e, max_runs=1, electronic_temperature=etemp, calculate_hessian=False, unlimited_memory=True, solvent=solvent_str, solvent_grid=solvent_grid) try: cage = xtb_opt.optimize(mol=cage) cage.write(join(f'{output_dir}', f'conf_{id}_opt.xyz')) except stko.XTBConvergenceError: if handle_failure: opt_failed = True print(f'optimising conformer {id}: FAILED') else: raise stko.XTBConvergenceError() print(f'..........calculating energy of {id} of {cage_name}') # Extract energy. xtb_energy = stko.XTBEnergy(xtb_path=gfn_exec, output_dir=f'ey_{cage_name}_{id}', num_cores=nc, charge=charge, num_unpaired_electrons=free_e, electronic_temperature=etemp, unlimited_memory=True, solvent=solvent_str, solvent_grid=solvent_grid) if handle_failure and opt_failed: energy = 10E24 else: energy = xtb_energy.get_energy(cage) if energy < min_energy: min_energy_conformer = file min_energy = energy ids.append(id) energies.append(energy) print('done', min_energy, min_energy_conformer) cage = cage.with_structure_from_file(min_energy_conformer) energies = [(i - min(energies)) * 2625.5 for i in energies] fig, ax = scatter_plot(X=ids, Y=energies, xtitle='conformer id', ytitle='rel. energy [kJmol$^{-1}$]', xlim=(0, 201), ylim=(-5, 1000)) fig.tight_layout() fig.savefig(join(output_dir, f'{cage_name}_conf_energies.pdf'), dpi=720, bbox_inches='tight') plt.close() return cage