# Reaction coordinate

A TS workflow runs on one [StereoReactionGraph](../reference/StereoReactionGraph.md#protomol.StereoReactionGraph) (a [ReactionGraph](../reference/ReactionGraph.md#protomol.ReactionGraph) if stereo doesn't matter). The graph says which bonds break and form, which stereo sites change, and what the two sides are.


# Bond changes

H abstraction, CH4 + OH → CH3 + H2O (atoms: C0, H1-H4, O5, H6):

``` python
>>> import protomol as pm
>>> ts = pm.ReactionGraph(
...     symbols=["C", "H", "H", "H", "H", "O", "H"],
...     bonds=[(0, 1), (0, 2), (0, 3), (0, 4), (5, 6), (1, 5)],
...     breaking=[(0, 1)],
...     forming=[(1, 5)],
... )
>>> ts.breaking, ts.forming
(((0, 1),), ((1, 5),))
>>> ts.spin
1
```

A saddle-point search scans or constrains the distances in `breaking` and `forming`. That is the whole reaction coordinate. `bonds` holds every bond of the TS, the changing ones included, so `pm.neighbors` gives the neighbours on either side:

``` python
>>> pm.neighbors(ts, 1)
(0, 5)
```


# Stereo that changes

Each slot of a [StereoChange](../reference/StereoChange.md#protomol.StereoChange) (`reactant`, `product`, `ts`) holds the LS of that site on that side. `None` means the side has no stereo element there. An element whose configuration isn't chosen is an undetermined LS (`parity=None`), not `None`.

SN2 at a stereocentre, I⁻ + CHFClBr → CHFClI + Br⁻ (atoms: C0, F1, Cl2, Br3, H4, I5). The centre's LS changes, so it is a [StereoChange](../reference/StereoChange.md#protomol.StereoChange). Backside attack inverts the centre: I5 takes the place of Br3 with the opposite parity. At the TS the centre is five-coordinate, a [TrigonalBipyramidal](../reference/TrigonalBipyramidal.md#protomol.TrigonalBipyramidal) with I5 and Br3 axial. Its configuration follows from the reactant's. A constructor takes the slots as given, so this hand-built graph leaves it undetermined. The TS-slot rules are the low-T scheme's: [low_t](../reference/low_t.md#protomol.low_t) sets the slot, and so does the reader [from_reaction_smiles](../reference/from_reaction_smiles.md#protomol.from_reaction_smiles), while [reaction_graph](../reference/reaction_graph.md#protomol.reaction_graph) and [ts_graphs](../reference/ts_graphs.md#protomol.ts_graphs), which build a reaction graph from its sides, leave it empty.

``` python
>>> rct = pm.Tetrahedral(atoms=(0, 1, 2, 3, 4), parity=1)
>>> prd = pm.Tetrahedral(atoms=(0, 1, 2, 5, 4), parity=-1)
>>> tbp = pm.TrigonalBipyramidal(atoms=(0, 5, 3, 1, 2, 4))
>>> ts = pm.StereoReactionGraph(
...     symbols=["C", "F", "Cl", "Br", "H", "I"],
...     bonds=[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5)],
...     breaking=[(0, 3)],
...     forming=[(0, 5)],
...     charge=-1,
...     stereo_changes=[pm.StereoChange(reactant=rct, product=prd, ts=tbp)],
... )
>>> change = pm.stereo_at(ts, 0)
>>> change.reactant == rct, change.product == prd, change.ts.kind
(True, True, 'TrigonalBipyramidal')
>>> pm.stereo_at(pm.reaction_graph(pm.reactants(ts), pm.products(ts)), 0).ts is None
True
>>> pm.stereo_at(pm.low_t(ts), 0).ts
TrigonalBipyramidal(kind='TrigonalBipyramidal', atoms=(0, 3, 5, 1, 2, 4), parity=1)
```

Each change at a TS site, an LS with an atom in a breaking or forming bond (or, with no bond change, at the changing site), gets its `ts` slot by rule from the two sides: an LS on one side only carries over (an abstraction centre is still pyramidal at the TS, an attacked face is fixed by the attack), inversion at a centre gives the backside [TrigonalBipyramidal](../reference/TrigonalBipyramidal.md#protomol.TrigonalBipyramidal) and retention a [Tetrahedral](../reference/Tetrahedral.md#protomol.Tetrahedral) with `None` where the leaving and attacking atoms meet, a lone-pair inversion is planar (`None`). The rules give the same slot in either direction.

A stereo change needs no bond change. In the but-2-en-1-yl radical, CH2•-CH=CH-CH3 (atoms: C0-C3, H4-H10), resonance gives C1=C2 its double-bond character. Rotating about it turns (Z) into (E). The TS graph has no `breaking` or `forming` bonds, only a [StereoChange](../reference/StereoChange.md#protomol.StereoChange) at the bond (1, 2). A [PlanarBond](../reference/PlanarBond.md#protomol.PlanarBond) lists (a, b, end1, end2, c, d) with a cis to c, so (Z) has C0 cis to C3 and (E) has C0 cis to H7.

At the TS the bond is twisted about 90°, which makes it a chiral axis: an [AtropBond](../reference/AtropBond.md#protomol.AtropBond). Rotating one way or the other gives mirror-image TSs (parity 1 or -1). These are distinct saddle points, so they are two channels, and a kinetic model needs both. This graph leaves the axis undetermined, standing for either one:

``` python
>>> z = pm.PlanarBond(atoms=(0, 6, 1, 2, 3, 7), parity=0)
>>> e = pm.PlanarBond(atoms=(0, 6, 1, 2, 7, 3), parity=0)
>>> twist = pm.AtropBond(atoms=(0, 6, 1, 2, 3, 7))
>>> ts = pm.StereoReactionGraph(
...     symbols=["C", "C", "C", "C"] + ["H"] * 7,
...     bonds=[(0, 1), (1, 2), (2, 3), (0, 4), (0, 5), (1, 6), (2, 7), (3, 8), (3, 9), (3, 10)],
...     stereo_changes=[pm.StereoChange(reactant=z, product=e, ts=twist)],
... )
>>> ts.breaking, ts.forming, ts.spin
((), (), 1)
>>> change = pm.stereo_at(ts, (1, 2))
>>> change.reactant == z, change.product == e, change.ts.parity
(True, True, None)
>>> # the C1-C2 bond, whose stereo changes, in magenta
>>> pm.display(ts)  # doctest: +SKIP
```

This C1=C2 bond is a low-T stereo element: the other resonance form puts the double bond at C0=C1, so a high-T scheme (InChI) drops it. `protomol.examples` has one of the two channels, with the axis determined:

``` python
>>> channel = pm.examples.stereoisomerization_ts()
>>> change = pm.stereo_at(channel, (1, 2))
>>> change.reactant == z, change.product == e, change.ts.parity
(True, True, 1)
```

[ts_graphs](../reference/ts_graphs.md#protomol.ts_graphs) gives the channel with its `ts` slot empty; [low_t](../reference/low_t.md#protomol.low_t) sets it to the twist, undetermined, and [stereoisomers](../reference/stereoisomers.md#protomol.stereoisomers) gives both twisted TSs:

``` python
>>> (rotation,) = pm.ts_graphs(pm.from_smiles(r"[CH2]/C=C\C"), pm.from_smiles(r"[CH2]/C=C/C"))
>>> pm.stereo_at(rotation, (1, 2)).ts is None
True
>>> twists = pm.stereoisomers(pm.low_t(rotation))
>>> sorted(pm.stereo_at(ts, (1, 2)).ts.parity for ts in twists)
[-1, 1]
>>> any(pm.isomorphism(channel, ts) is not None for ts in twists)
True
```


# Bifurcations

When OH adds to 1-fluoropropyne, the alkenyl radical's (Z) or (E) configuration is set after the TS: one TS leads to both products. The `ts` slot stays undetermined, and each product configuration is its own channel. [transition_state](../reference/transition_state.md#protomol.transition_state) drops what the TS doesn't fix, so both channels give one TS:

``` python
>>> alkyne = pm.from_smiles("CC#CF.[OH]")
>>> (either,) = pm.ts_graphs(alkyne, pm.from_smiles("CC(O)=[C]F"))
>>> first, second = pm.stereoisomers(pm.low_t(either))   # one per product configuration
>>> pm.stereo_at(first, (1, 2)).ts.parity is None
True
>>> first == second, pm.transition_state(first) == pm.transition_state(second)
(False, True)
```


# The two sides, and the IRC end points

[reactants](../reference/reactants.md#protomol.reactants) and [products](../reference/products.md#protomol.products) give each side as a [StereoMolGraph](../reference/StereoMolGraph.md#protomol.StereoMolGraph) in the TS's atom order, with the unchanged LS and the matching slot of each change. [reverse](../reference/reverse.md#protomol.reverse) swaps the two:

``` python
>>> rct = pm.reactants(ts)
>>> prd = pm.products(ts)
>>> type(rct).__name__, rct.symbols == ts.symbols
('StereoMolGraph', True)
>>> pm.stereo_at(rct, (1, 2)) == z, pm.stereo_at(prd, (1, 2)) == e
(True, True)
>>> pm.reactants(pm.reverse(ts)) == prd
True
```

The sides index the IRC end points too, so [geometry_mismatches](../reference/geometry_mismatches.md#protomol.geometry_mismatches) checks each end point against the side it should be: the bonds, perceived from covalent radii, and the parity of every LS. Here an embedding of each side stands in for the IRC's end points:

``` python
>>> fwd, bwd = pm.embed(prd), pm.embed(rct)   # stand in for the IRC's end points
>>> pm.geometry_mismatches(prd, fwd)
GeometryMismatches(missing_bonds=(), extra_bonds=(), reacting_bonds=(), stereo=())
>>> pm.geometry_mismatches(prd, bwd).stereo   # the wrong end: (Z) at C1=C2
((1, 2),)
```

The TS graph itself is checked against a TS geometry: the bonds that neither break nor form, the breaking and forming bonds, which must be partial (`reacting_bonds`), the LS that don't change, and each determined `ts` slot. A planar end point contradicts neither twist, but a twisted geometry agrees with only one of the two mirror-image TSs:

``` python
>>> import autogeom
>>> twisted = autogeom.twist(bwd, (1, 2), 90, degrees=True)
>>> plus = pm.examples.stereoisomerization_ts()     # AtropBond parity 1
>>> twist = pm.stereo_at(plus, (1, 2)).ts
>>> minus = pm.with_stereo(plus, pm.StereoChange(reactant=z, product=e, ts=pm.invert(twist)))
>>> [pm.geometry_mismatches(g, bwd).stereo for g in (plus, minus)]
[(), ()]
>>> sorted(pm.geometry_mismatches(g, twisted).stereo for g in (plus, minus))
[(), ((1, 2),)]
```

> **Note: Note**
>
> Planned: Phase 4 ([reacting_core](../reference/reacting_core.md#protomol.reacting_core)).

``` python
core = pm.reacting_core(ts)     # atoms CREST freezes in a TS conformer search
```

Each side may hold several molecules. Splitting a side into its molecules, each with its own atom map, is not planned yet:

``` python
for mol, atom_map in pm.components(prd):   # PROPOSED: name and return type
    ...
```
