Beispiel #1
0
def test_reaction_warnings():
    test_reac = Reactant(name='test', smiles='C')
    test_reac.energy = -1

    test_prod = Product(name='test', smiles='C')
    test_prod.energy = -1.03187251

    tsguess = TSguess(atoms=test_reac.atoms,
                      reactant=ReactantComplex(test_reac),
                      product=ProductComplex())
    tsguess.bond_rearrangement = BondRearrangement()
    ts = TransitionState(tsguess)
    ts.energy = -0.98
    ts.imaginary_frequencies = [-100]

    reaction = Reaction(test_reac, test_prod)
    reaction.ts = None

    # Should be some warning with no TS
    assert len(
        plotting.get_reaction_profile_warnings(reactions=[reaction])) > 10

    # Should be no warnings  with a TS that exists and has an energy and one
    # imaginary freq
    reaction.ts = ts
    warnings = plotting.get_reaction_profile_warnings(reactions=[reaction])
    assert 'None' in warnings
Beispiel #2
0
def test_plot_reaction_profile():

    r = Reactant(name='reactant', smiles='C')
    p = Product(name='product', smiles='C')
    tsguess = TSguess(atoms=r.atoms,
                      reactant=ReactantComplex(r),
                      product=ProductComplex(p))
    tsguess.bond_rearrangement = BondRearrangement()
    ts = TransitionState(tsguess)
    reaction = Reaction(r, p)
    reaction.ts = ts

    plotting.plot_reaction_profile(reactions=[reaction],
                                   units=KjMol,
                                   name='test')

    assert os.path.exists('test_reaction_profile.png')
    os.remove('test_reaction_profile.png')

    with pytest.raises(AssertionError):
        plotting.plot_reaction_profile(reactions=[reaction],
                                       units=KjMol,
                                       name='test',
                                       free_energy=True,
                                       enthalpy=True)
    return None
Beispiel #3
0
def test_multistep_reaction():

    Config.num_conformers = 1

    # Spoof installs
    Config.lcode = 'xtb'
    Config.XTB.path = here

    Config.hcode = 'orca'
    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 forwards then backwards example
    forwards = Reaction('CCl.[F-]>>CF.[Cl-]',
                        name='sn2_forwards',
                        solvent_name='water')

    backwards = Reaction('CF.[Cl-]>>CCl.[F-]',
                         name='sn2_backwards',
                         solvent_name='water')

    reaction = MultiStepReaction(forwards, backwards)
    reaction.calculate_reaction_profile()

    assert reaction.reactions is not None
Beispiel #4
0
def test_calculate_reaction_profile_energies():

    test_reac = Reactant(name='test', smiles='C')
    test_reac.energy = -1

    test_prod = Product(name='test', smiles='C')
    test_prod.energy = -1.03187251

    tsguess = TSguess(atoms=test_reac.atoms, reactant=ReactantComplex(test_reac), product=ProductComplex())
    tsguess.bond_rearrangement = BondRearrangement()
    ts = TransitionState(tsguess)
    ts.energy = -0.96812749

    reaction = Reaction(test_reac, test_prod)
    reaction.ts = ts

    energies = plotting.calculate_reaction_profile_energies(reactions=[reaction],
                                                            units=KcalMol)

    # Energies have been set to ∆E = -20 and ∆E‡ = 20 kcal mol-1 respectively
    assert energies[0] == 0
    assert 19 < energies[1] < 21
    assert -21 < energies[2] < -19

    # Copying the reaction should give relative energies [0, 20, -20, 0, -40]

    energies = plotting.calculate_reaction_profile_energies(reactions=[reaction, deepcopy(reaction)],
                                                            units=KcalMol)

    # Energies have been set to ∆E = -20 and ∆E‡ = 20 kcal mol-1 respectively
    assert energies[0] == 0
    assert -0.1 < energies[3] < 0.1
    assert -41 < energies[4] < -39
Beispiel #5
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
Beispiel #6
0
def test_isomorphic_reactant_product():

    r_water = Reactant(name='h2o', smiles='O')
    r_methane = Reactant(name='methane', smiles='C')

    p_water = Product(name='h2o', smiles='O')
    p_methane = Product(name='methane', smiles='C')

    # Reaction where the reactant and product complexes are isomorphic
    # should return no TS
    reaction = Reaction(r_water, r_methane, p_water, p_methane)
    reaction.locate_transition_state()

    assert reaction.ts is None
Beispiel #7
0
def test_plot_reaction_profile():

    r = Reactant(name='reactant', smiles='C')
    p = Product(name='product', smiles='C')
    tsguess = TSguess(atoms=r.atoms,
                      reactant=ReactantComplex(r),
                      product=ProductComplex(p))
    tsguess.bond_rearrangement = BondRearrangement()
    ts = TransitionState(tsguess)
    reaction = Reaction(r, p)
    reaction.ts = ts

    plotting.plot_reaction_profile(reactions=[reaction],
                                   units=KjMol,
                                   name='test_reaction')

    assert os.path.exists('test_reaction_reaction_profile.png')
    os.remove('test_reaction_reaction_profile.png')
Beispiel #8
0
def test_plot_reaction_profile():
    # only tests the file is created with the right name
    os.chdir(os.path.join(here, 'data'))

    r = Reactant(name='reactant', smiles='C')
    p = Product(name='product', smiles='C')
    tsguess = TSguess(atoms=r.atoms, reactant=ReactantComplex(r), product=ProductComplex(p))
    tsguess.bond_rearrangement = BondRearrangement()
    ts = TransitionState(tsguess)
    reaction = Reaction(r, p)
    reaction.ts = ts

    plotting.plot_reaction_profile(reactions=[reaction], units=KjMol, name='test_reaction')

    assert os.path.exists('test_reaction_reaction_profile.png')

    os.remove('test_reaction_reaction_profile.png')
    os.chdir(here)
Beispiel #9
0
def test_bonds():

    h1 = Atom(atomic_symbol='H', x=0.0, y=0.0, z=0.0)
    h2 = Atom(atomic_symbol='H', x=0.0, y=0.0, z=0.7)
    h3 = Atom(atomic_symbol='H', x=0.0, y=0.0, z=1.7)

    hydrogen = Reactant(name='H2', atoms=[h1, h2], charge=0, mult=1)
    h = Reactant(name='H', atoms=[h3], charge=0, mult=2)

    reac = ReactantComplex(hydrogen, h)

    prod_h2 = Product(name='H2', atoms=[h1, h2], charge=0, mult=1)
    prod_h = Product(name='H', atoms=[h3], charge=0, mult=2)

    fbond = FormingBond(atom_indexes=(1, 2), species=reac)
    bbond = BreakingBond(atom_indexes=(0, 1), species=reac, reaction=Reaction(hydrogen, h, prod_h2, prod_h))

    assert fbond.curr_dist == 1.0
    assert 0.6 < fbond.final_dist < 0.8

    assert 2.0 < bbond.final_dist < 2.5
    assert bbond.curr_dist == 0.7
Beispiel #10
0
def test_get_ts_guess_2dscan():

    ch3cl_f = Reactant(name='CH3Cl_F-',
                       charge=-1,
                       mult=1,
                       atoms=[
                           Atom('F', -4.14292, -0.24015, 0.07872),
                           Atom('Cl', 1.63463, 0.09787, -0.02490),
                           Atom('C', -0.14523, -0.00817, 0.00208),
                           Atom('H', -0.47498, -0.59594, -0.86199),
                           Atom('H', -0.45432, -0.49900, 0.93234),
                           Atom('H', -0.56010, 1.00533, -0.04754)
                       ])

    ch3f_cl = Product(name='CH3Cl_F-',
                      charge=-1,
                      mult=1,
                      atoms=[
                          Atom('F', 1.63463, 0.09787, -0.02490),
                          Atom('Cl', -4.14292, -0.24015, 0.07872),
                          Atom('C', -0.14523, -0.00817, 0.00208),
                          Atom('H', -0.47498, -0.59594, -0.86199),
                          Atom('H', -0.45432, -0.49900, 0.93234),
                          Atom('H', -0.56010, 1.00533, -0.04754)
                      ])

    #         H                H
    #   F-   C--Cl     ->   F--C         Cl-
    #       H H               H H
    pes = pes_2d.PES2d(reactant=ReactantComplex(ch3cl_f),
                       product=ProductComplex(ch3f_cl),
                       r1s=np.linspace(4.0, 1.5, 9),
                       r1_idxs=(0, 2),
                       r2s=np.linspace(1.78, 4.0, 8),
                       r2_idxs=(1, 2))

    pes.calculate(name='SN2_PES', method=xtb, keywords=xtb.keywords.low_opt)

    assert pes.species[0, 1] is not None
    assert -13.13 < pes.species[0, 1].energy < -13.11
    assert pes.species.shape == (9, 8)
    assert pes.rs.shape == (9, 8)
    assert type(pes.rs[0, 1]) == tuple
    assert pes.rs[1, 1] == (np.linspace(4.0, 1.5,
                                        9)[1], np.linspace(1.78, 4.0, 8)[1])

    # Fitting the surface with a 2D polynomial up to order 3 in r1 and r2 i.e.
    #  r1^3r2^3
    pes.fit(polynomial_order=3)
    assert pes.coeff_mat is not None
    assert pes.coeff_mat.shape == (4, 4)  # Includes r1^0 etc.

    pes.print_plot(name='pes_plot')
    assert os.path.exists('pes_plot.png')
    os.remove('pes_plot.png')

    # Products should be made on this surface
    assert pes.products_made()

    # Get the TS guess from this surface calling all the above functions
    reactant = ReactantComplex(ch3cl_f)
    fbond = FormingBond(atom_indexes=(0, 2), species=reactant)
    fbond.final_dist = 1.5

    bbond = BreakingBond(atom_indexes=(1, 2),
                         species=reactant,
                         reaction=Reaction(ch3cl_f, ch3f_cl))
    bbond.final_dist = 4.0

    ts_guess = pes_2d.get_ts_guess_2d(reactant=reactant,
                                      product=ProductComplex(ch3f_cl),
                                      bond1=fbond,
                                      bond2=bbond,
                                      polynomial_order=3,
                                      name='SN2_PES',
                                      method=xtb,
                                      keywords=xtb.keywords.low_opt,
                                      dr=0.3)
    assert ts_guess is not None
    assert ts_guess.n_atoms == 6
    assert ts_guess.energy is None
    assert 1.9 < ts_guess.get_distance(0, 2) < 2.1
    assert 1.9 < ts_guess.get_distance(1, 2) < 2.0
Beispiel #11
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)