Example #1
0
def test_ts_templates_find():

    templates = get_ts_templates(folder_path='/a/path/that/doesnt/exist')
    assert len(templates) == 0

    # Create a incorrectly formatted file, i.e. blank
    open('wrong_template.txt', 'w').close()
    templates = get_ts_templates(folder_path=os.getcwd())
    assert len(templates) == 0

    os.remove('wrong_template.txt')
Example #2
0
def get_template_ts_guess(reactant, product, bond_rearrangement, name, method, dist_thresh=4.0):
    """Get a transition state guess object by searching though the stored TS
    templates

    Arguments:
        reactant (autode.complex.ReactantComplex):
        bond_rearrangement (autode.bond_rearrangement.BondRearrangement):
        product (autode.complex.ProductComplex):
        method (autode.wrappers.base.ElectronicStructureMethod):
        name (str):
        keywords (list(str)): Keywords to use for the ElectronicStructureMethod

    Keyword Arguments:
        dist_thresh (float): distance above which a constrained optimisation
                             probably won't work due to the initial geometry
                             being too far away from the ideal (default: {4.0})

    Returns:
        TSGuess object: ts guess object
    """
    logger.info('Getting TS guess from stored TS template')
    active_bonds_and_dists_ts = {}

    # This will add edges so don't modify in place
    mol_graph = get_truncated_active_mol_graph(graph=reactant.graph,
                                               active_bonds=bond_rearrangement.all)
    ts_guess_templates = get_ts_templates()

    for ts_template in ts_guess_templates:

        if not template_matches(reactant=reactant, ts_template=ts_template,
                                truncated_graph=mol_graph):
            continue

        # Get the mapping from the matching template
        mapping = get_mapping_ts_template(larger_graph=mol_graph,
                                          smaller_graph=ts_template.graph)

        for active_bond in bond_rearrangement.all:
            i, j = active_bond
            try:
                active_bonds_and_dists_ts[active_bond] = ts_template.graph.edges[mapping[i],
                                                                                 mapping[j]]['distance']
            except KeyError:
                logger.warning(f'Couldn\'t find a mapping for bond {i}-{j}')

        if len(active_bonds_and_dists_ts) == len(bond_rearrangement.all):
            logger.info('Found a TS guess from a template')

            if any([reactant.get_distance(*bond) > dist_thresh for bond in bond_rearrangement.all]):
                logger.info(f'TS template has => 1 active bond distance larger than {dist_thresh}. Passing')
                pass

            else:
                return get_ts_guess_constrained_opt(reactant, method=method, keywords=method.keywords.opt, name=name,
                                                    distance_consts=active_bonds_and_dists_ts, product=product)

    logger.info('Could not find a TS guess from a template')
    return None
Example #3
0
def test_find_tss():

    Config.num_conformers = 1

    # Spoof ORCA install
    Config.ORCA.path = here

    # Don't run the calculation without a working XTB install
    if shutil.which('xtb') is None or not shutil.which('xtb').endswith('xtb'):
        return

    if os.path.exists('/dev/shm'):
        Config.ll_tmp_dir = '/dev/shm'

    Config.XTB.path = shutil.which('xtb')

    Config.ORCA.implicit_solvation_type = cpcm
    Config.make_ts_template = False
    Config.num_complex_sphere_points = 2
    Config.num_complex_random_rotations = 1

    # SN2 example
    flouride = Reactant(name='F-', smiles='[F-]')
    methyl_chloride = Reactant(name='CH3Cl', smiles='ClC')
    chloride = Product(name='Cl-', smiles='[Cl-]')
    methyl_flouride = Product(name='CH3F', smiles='CF')

    reaction = Reaction(flouride,
                        methyl_chloride,
                        chloride,
                        methyl_flouride,
                        name='sn2',
                        solvent_name='water')

    # Will work in data/locate_ts/transition_states
    reaction.locate_transition_state()

    assert reaction.ts is not None
    os.chdir('transition_states')
    assert reaction.ts.is_true_ts()
    os.chdir('..')

    reaction.ts.save_ts_template(folder_path=os.getcwd())
    assert os.path.exists('template0.txt')

    # There should now be a saved template
    templates = get_ts_templates(folder_path=os.getcwd())
    assert len(templates) == 1

    template = templates[0]
    assert template.solvent.name == 'water'
    assert template.mult == 1
    assert template.charge == -1

    assert template.graph.number_of_nodes() == 6

    # Reset the configuration
    Config.ll_tmp_dir = None
Example #4
0
def has_matching_ts_templates(reactant, bond_rearr):
    """
    See if there are any templates suitable to get a TS guess from a template

    Arguments:
        reactant (autode.complex.ReactantComplex):
        bond_rearr (autode.bond_rearrangement.BondRearrangement):

    Returns:
        bool:
    """

    mol_graph = get_truncated_active_mol_graph(graph=reactant.graph,
                                               active_bonds=bond_rearr.all)
    ts_guess_templates = get_ts_templates()

    for ts_template in ts_guess_templates:

        if template_matches(reactant=reactant, ts_template=ts_template,
                            truncated_graph=mol_graph):
            return True

    return False
Example #5
0
def test_ts_template():

    # Spoof XTB install
    Config.XTB.path = here
    Config.ts_template_folder_path = os.path.join(here, 'data', 'ts_guess')

    bond_rearr = BondRearrangement(breaking_bonds=[(2, 1)],
                                   forming_bonds=[(0, 2)])

    reac_shift = reac_complex.copy()

    reac_shift.set_atoms(atoms=[
        Atom('F', -3.0587, -0.8998, -0.2180),
        Atom('Cl', 0.3842, 0.86572, -1.65507),
        Atom('C', -1.3741, -0.0391, -0.9719),
        Atom('H', -1.9151, -0.0163, -1.9121),
        Atom('H', -1.6295, 0.6929, -0.2173),
        Atom('H', -0.9389, -0.9786, -0.6534)
    ])
    reac_shift.print_xyz_file()

    templates = get_ts_templates()
    assert len(templates) == 1
    assert templates[0].graph.number_of_nodes() == 6

    tsg_template = get_template_ts_guess(reac_shift,
                                         product_complex,
                                         name='template',
                                         bond_rearr=bond_rearr,
                                         method=XTB(),
                                         dist_thresh=4.0)

    # Reset the folder path to the default
    Config.ts_template_folder_path = None

    assert tsg_template is not None
Example #6
0
def test_ts_templates():

    templates = get_ts_templates(folder_path='/a/path/that/doesnt/exist')
    assert len(templates) == 0
Example #7
0
def test_find_tss():

    os.chdir(os.path.join(here, 'data', 'locate_ts'))
    Config.num_conformers = 1

    # Spoof ORCA install
    Config.ORCA.path = here

    # Don't run the calculation without a working XTB install
    if shutil.which('xtb') is None or not shutil.which('xtb').endswith('xtb'):
        return

    Config.XTB.path = shutil.which('xtb')

    Config.ORCA.implicit_solvation_type = 'cpcm'
    Config.make_ts_template = False
    Config.num_complex_sphere_points = 2
    Config.num_complex_random_rotations = 1

    # SN2 example
    flouride = Reactant(name='F-', smiles='[F-]')
    methyl_chloride = Reactant(name='CH3Cl', smiles='ClC')
    chloride = Product(name='Cl-', smiles='[Cl-]')
    methyl_flouride = Product(name='CH3F', smiles='CF')

    reaction = Reaction(flouride,
                        methyl_chloride,
                        chloride,
                        methyl_flouride,
                        name='sn2',
                        solvent_name='water')

    # Will work in data/locate_ts/transition_states
    reaction.locate_transition_state()

    assert reaction.ts is not None
    os.chdir(os.path.join(here, 'data', 'locate_ts', 'transition_states'))

    for filename in os.listdir(os.getcwd()):
        if filename.endswith(('.inp', '.png')):
            os.remove(filename)

    assert reaction.ts.is_true_ts()

    reaction.ts.save_ts_template(folder_path=os.getcwd())
    assert os.path.exists('template0.obj')

    # There should now be a saved template
    templates = get_ts_templates(folder_path=os.getcwd())
    assert len(templates) == 1

    template = templates[0]
    assert template.solvent.name == 'water'
    assert template.mult == 1
    assert template.charge == -1

    assert template.graph.number_of_nodes() == 6

    # Tidy the generated files
    os.remove('template0.obj')
    os.chdir(here)