Ejemplo n.º 1
0
    def test_graph_representation(self):

        # set up RN
        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)

        # set up input variables
        LiEC_ind = None
        EC_minus_ind = None
        Li_ind = None
        LiEC_RN_entry = None
        EC_minus_RN_entry = None
        Li_RN_entry = None

        for entry in RN.entries["C3 H4 Li1 O3"][12][0]:
            if self.LiEC_mg.isomorphic_to(entry.mol_graph):
                LiEC_ind = entry.parameters["ind"]
                LiEC_RN_entry = entry
                break
        for entry in RN.entries["C3 H4 O3"][10][-1]:
            if self.EC_mg.isomorphic_to(entry.mol_graph):
                EC_minus_ind = entry.parameters["ind"]
                EC_minus_RN_entry = entry
                break
        for entry in RN.entries["Li1"][0][1]:
            Li_ind = entry.parameters["ind"]
            Li_RN_entry = entry
            break

        # perform calc
        reaction = CoordinationBondChangeReaction(
            LiEC_RN_entry, [EC_minus_RN_entry, Li_RN_entry])
        graph = reaction.graph_representation()

        # assert
        self.assertCountEqual(
            list(graph.nodes),
            [
                LiEC_ind,
                EC_minus_ind,
                Li_ind,
                str(LiEC_ind) + "," + str(EC_minus_ind) + "+" + str(Li_ind),
                str(EC_minus_ind) + "+PR_" + str(Li_ind) + "," + str(LiEC_ind),
                str(Li_ind) + "+PR_" + str(EC_minus_ind) + "," + str(LiEC_ind),
            ],
        )
        self.assertEqual(len(graph.edges), 7)
        self.assertEqual(
            graph.get_edge_data(
                LiEC_ind,
                str(LiEC_ind) + "," + str(EC_minus_ind) + "+" +
                str(Li_ind))["softplus"],
            1.5036425808336291,
        )
        self.assertEqual(
            graph.get_edge_data(
                LiEC_ind,
                str(Li_ind) + "+PR_" + str(EC_minus_ind) + "," +
                str(LiEC_ind)),
            None,
        )
Ejemplo n.º 2
0
    def test_build(self):
        # set up RN
        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries,
                                                electron_free_energy=-2.15)

        # perfrom calc
        RN.build()

        # assert
        EC_ind = None
        LEDC_ind = None
        LiEC_ind = None
        for entry in RN.entries["C3 H4 Li1 O3"][12][1]:
            if self.LiEC_mg.isomorphic_to(entry.mol_graph):
                LiEC_ind = entry.parameters["ind"]
                break
        for entry in RN.entries["C3 H4 O3"][10][0]:
            if self.EC_mg.isomorphic_to(entry.mol_graph):
                EC_ind = entry.parameters["ind"]
                break
        for entry in RN.entries["C4 H4 Li2 O6"][17][0]:
            if self.LEDC_mg.isomorphic_to(entry.mol_graph):
                LEDC_ind = entry.parameters["ind"]
                break
        Li1_ind = RN.entries["Li1"][0][1][0].parameters["ind"]
        self.assertEqual(len(RN.entries_list), 569)
        self.assertEqual(EC_ind, 456)
        self.assertEqual(LEDC_ind, 511)
        self.assertEqual(Li1_ind, 556)
        self.assertEqual(LiEC_ind, 424)

        self.assertEqual(len(RN.graph.edges), 19824)
        self.assertEqual(len(RN.graph.nodes), 7415)
Ejemplo n.º 3
0
    def __init__(
        self,
        input_entries,
        single_elem_interm_ignore=["C1", "H1", "O1", "Li1", "P1", "F1"],
        electron_free_energy=-2.15,
        temperature=ROOM_TEMP,
        solvent_dielectric=18.5,
        solvent_refractive_index=1.415,
    ):

        self.rn = ReactionNetwork.from_input_entries(
            input_entries,
            electron_free_energy=electron_free_energy,
            temperature=temperature,
            solvent_dielectric=solvent_dielectric,
            solvent_refractive_index=solvent_refractive_index,
        )
        self.rn.build()
        self.single_elem_interm_ignore = single_elem_interm_ignore

        # generator state

        self.current_chunk = self.rn.reactions
        self.chunk_index = 0
        self.intermediate_index = -1
        self.previously_seen_reactions = set()
Ejemplo n.º 4
0
    def test_generate(self):

        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)
        reactions, families = IntramolSingleBondChangeReaction.generate(
            RN.entries)
        self.assertEqual(len(reactions), 93)

        for r in reactions:
            # TODO (mjwen) this is never run for two reasons:
            #  1. It should be:
            #  if r.reactant == self.LiEC_RO_entry:
            #     self.assertEqual(r.product.entry_id, self.LiEC_entry.entry_id)
            #  since this class generates bond formation reactions
            #  Even, after fixing 1, there is still another problem.
            #  2. There are multiple MoleculeEntry with the same `formula`,`Nbonds`,
            #  and `charge` as self.LiEC_entry. In `setUpClass`, one of such
            #  MoleculeEntry is set to self.LiEC_entry,
            #  but in IntramolSingleBondChangeReaction, another MoleculeEntry will be
            #  used as the reactant. This happens because in both `setUpClass` and
            #  `IntramolSingleBondChangeReaction`, the code `break` when one entry is
            #  found.
            #  To fix this, we can either clean the input data to make sure there is
            #  only one LiEC, or we do some bookkeeping in `setUpClass` and then make
            #  the correct check.

            # if r.reactant == self.LiEC_entry:
            #    self.assertEqual(r.product.entry_id, self.LiEC_RO_entry.entry_id)
            pass
Ejemplo n.º 5
0
    def test_add_reactions(self):

        # set up RN
        RN = ReactionNetwork.from_input_entries(
            self.LiEC_reextended_entries, electron_free_energy=-2.15
        )

        # set up input variables
        EC_0_entry = None
        EC_minus_entry = None

        # print(RN.entries["C3 H4 O3"].keys())

        for entry in RN.entries["C3 H4 O3"][10][0]:
            if self.EC_mg.isomorphic_to(entry.mol_graph):
                EC_0_entry = entry
                break
        for entry in RN.entries["C3 H4 O3"][10][-1]:
            if self.EC_mg.isomorphic_to(entry.mol_graph):
                EC_minus_entry = entry
                break

        redox = RedoxReaction(EC_0_entry, EC_minus_entry)
        redox.electron_free_energy = -2.15
        redox_graph = redox.graph_representation()

        # run calc
        RN.add_reaction(redox_graph)

        # assert
        self.assertEqual(list(RN.graph.nodes), ["456,455", 456, 455, "455,456"])
        self.assertEqual(
            list(RN.graph.edges),
            [("456,455", 455), (456, "456,455"), (455, "455,456"), ("455,456", 456)],
        )
Ejemplo n.º 6
0
    def test_generate(self):

        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)
        reactions, families = IntermolecularReaction.generate(RN.entries)

        self.assertEqual(len(reactions), 3673)

        for r in reactions:
            if r.reactant_ids[0] == self.LiEC_RO_entry.entry_id:
                if (r.product_ids[0] == self.C2H4_entry.entry_id
                        or r.product_ids[1] == self.C2H4_entry.entry_id):
                    self.assertTrue(r.pro0_formula == "C1 Li1 O3"
                                    or r.pro1_formula == "C1 Li1 O3")
                    self.assertTrue(r.pro0_charge == 0 or r.pro1_charge == 0)
                    self.assertTrue(r.pro0_free_energy()
                                    == self.C1Li1O3_entry.free_energy()
                                    or r.pro1_free_energy()
                                    == self.C1Li1O3_entry.free_energy())
Ejemplo n.º 7
0
    def test_graph_representation(self):

        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)
        # print(RN.entries["C3 H4 Li1 O3"][11][0][0].molecule)

        LiEC_ind = None
        LiEC_RO_ind = None
        LiEC_RN_entry = None
        LiEC_RO_RN_entry = None
        for entry in RN.entries["C3 H4 Li1 O3"][12][0]:
            if self.LiEC_mg.isomorphic_to(entry.mol_graph):
                LiEC_ind = entry.parameters["ind"]
                LiEC_RN_entry = entry
                break
        for entry in RN.entries["C3 H4 Li1 O3"][11][0]:
            if self.LiEC_RO_mg.isomorphic_to(entry.mol_graph):
                LiEC_RO_ind = entry.parameters["ind"]
                LiEC_RO_RN_entry = entry
                break
        reaction = IntramolSingleBondChangeReaction(LiEC_RN_entry,
                                                    LiEC_RO_RN_entry)
        reaction.electron_free_energy = -2.15
        graph = reaction.graph_representation()
        print(graph.nodes, graph.edges)
        print(
            graph.get_edge_data(LiEC_ind,
                                str(LiEC_ind) + "," + str(LiEC_RO_ind)))
        self.assertCountEqual(
            list(graph.nodes),
            [
                LiEC_ind,
                LiEC_RO_ind,
                str(LiEC_ind) + "," + str(LiEC_RO_ind),
                str(LiEC_RO_ind) + "," + str(LiEC_ind),
            ],
        )
        self.assertEqual(len(graph.edges), 4)
        self.assertEqual(
            graph.get_edge_data(LiEC_ind,
                                str(LiEC_ind) + "," +
                                str(LiEC_RO_ind))["softplus"],
            0.15092362164364986,
        )
Ejemplo n.º 8
0
    def test_graph_representation(self):

        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)

        EC_0_ind = None
        EC_1_ind = None
        EC_0_RN_entry = None
        EC_1_RN_entry = None
        for entry in RN.entries["C3 H4 O3"][10][0]:
            if self.EC_mg.isomorphic_to(entry.mol_graph):
                EC_0_ind = entry.parameters["ind"]
                EC_0_RN_entry = entry
                break
        for entry in RN.entries["C3 H4 O3"][10][1]:
            if self.EC_mg.isomorphic_to(entry.mol_graph):
                EC_1_ind = entry.parameters["ind"]
                EC_1_RN_entry = entry
                break

        reaction = RedoxReaction(EC_0_RN_entry, EC_1_RN_entry)
        reaction.electron_free_energy = -2.15
        graph = reaction.graph_representation()

        self.assertCountEqual(
            list(graph.nodes),
            [
                EC_0_ind,
                EC_1_ind,
                str(EC_0_ind) + "," + str(EC_1_ind),
                str(EC_1_ind) + "," + str(EC_0_ind),
            ],
        )
        self.assertEqual(len(graph.edges), 4)
        self.assertEqual(
            graph.get_edge_data(EC_0_ind,
                                str(EC_0_ind) + "," +
                                str(EC_1_ind))["softplus"],
            5.629805462349386,
        )
Ejemplo n.º 9
0
    def __init__(
        self,
        input_entries,
        single_elem_interm_ignore=["C1", "H1", "O1", "Li1", "P1", "F1"],
    ):

        self.rn = ReactionNetwork.from_input_entries(input_entries)
        self.rn.build()
        self.single_elem_interm_ignore = single_elem_interm_ignore

        # generator state

        first_chunk = []
        for reaction in self.rn.reactions:
            first_chunk.append((
                list(reaction.reactant_indices),
                list(reaction.product_indices),
                reaction.free_energy_A,
                reaction.free_energy_B,
            ))

        self.current_chunk = first_chunk
        self.chunk_index = 0
        self.intermediate_index = -1
Ejemplo n.º 10
0
)

mol_entries = [
    H2O,
    H2O_1,
    H2O_1p,
    H2,
    H2_1,
    H2_1p,
    OH,
    OH_1,
    OH_1p,
    O2,
    O2_1p,
    O2_1,
    O,
    O_1,
    O_1p,
    H,
    H_1p,
    H_1,
]

reaction_network = ReactionNetwork.from_input_entries(
    mol_entries, electron_free_energy=-2.15
)
reaction_network.build()
pickle_in = open(os.path.join(test_dir, "h2o_test_network.pickle"), "wb")
pickle.dump(reaction_network, pickle_in)
pickle_in.close()
Ejemplo n.º 11
0
    def test_generate(self):

        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)
        reactions, families = CoordinationBondChangeReaction.generate(
            RN.entries)
        self.assertEqual(len(reactions), 50)
Ejemplo n.º 12
0
    def test_graph_representation(self):

        # set up RN
        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)

        # set up input variables
        C2H4_ind = None
        LiEC_RO_ind = None
        C1Li1O3_ind = None
        C2H4_RN_entry = None
        LiEC_RO_RN_entry = None
        C1Li1O3_RN_entry = None

        for entry in RN.entries["C2 H4"][5][0]:
            if self.C2H4_mg.isomorphic_to(entry.mol_graph):
                C2H4_ind = entry.parameters["ind"]
                C2H4_RN_entry = entry
                break
        for entry in RN.entries["C3 H4 Li1 O3"][11][0]:
            if self.LiEC_RO_mg.isomorphic_to(entry.mol_graph):
                LiEC_RO_ind = entry.parameters["ind"]
                LiEC_RO_RN_entry = entry
                break
        for entry in RN.entries["C1 Li1 O3"][5][0]:
            if self.C1Li1O3_mg.isomorphic_to(entry.mol_graph):
                C1Li1O3_ind = entry.parameters["ind"]
                C1Li1O3_RN_entry = entry
                break

        # perform calc
        reaction = IntermolecularReaction(LiEC_RO_RN_entry,
                                          [C2H4_RN_entry, C1Li1O3_RN_entry])
        graph = reaction.graph_representation()

        # assert
        self.assertCountEqual(
            list(graph.nodes),
            [
                LiEC_RO_ind,
                C2H4_ind,
                C1Li1O3_ind,
                str(LiEC_RO_ind) + "," + str(C1Li1O3_ind) + "+" +
                str(C2H4_ind),
                str(C2H4_ind) + "+PR_" + str(C1Li1O3_ind) + "," +
                str(LiEC_RO_ind),
                str(C1Li1O3_ind) + "+PR_" + str(C2H4_ind) + "," +
                str(LiEC_RO_ind),
            ],
        )
        self.assertEqual(len(graph.edges), 7)
        self.assertEqual(
            graph.get_edge_data(
                LiEC_RO_ind,
                str(LiEC_RO_ind) + "," + str(C1Li1O3_ind) + "+" +
                str(C2H4_ind),
            )["softplus"],
            0.5828092060367285,
        )
        self.assertEqual(
            graph.get_edge_data(
                LiEC_RO_ind,
                str(C2H4_ind) + "+PR_" + str(C1Li1O3_ind) + "," +
                str(LiEC_RO_ind),
            ),
            None,
        )
Ejemplo n.º 13
0
    def test_generate(self):

        RN = ReactionNetwork.from_input_entries(self.LiEC_reextended_entries)
        reactions, families = RedoxReaction.generate(RN.entries)

        self.assertEqual(len(reactions), 273)
Ejemplo n.º 14
0
def get_entries():
    if ob:
        LiEC_reextended_entries = []
        entries = loadfn(os.path.join(test_dir,
                                      "LiEC_reextended_entries.json"))
        for entry in entries:
            if "optimized_molecule" in entry["output"]:
                mol = entry["output"]["optimized_molecule"]
            else:
                mol = entry["output"]["initial_molecule"]
            E = float(entry["output"]["final_energy"])
            H = float(entry["output"]["enthalpy"])
            S = float(entry["output"]["entropy"])
            mol_entry = MoleculeEntry(
                molecule=mol,
                energy=E,
                enthalpy=H,
                entropy=S,
                entry_id=entry["task_id"],
            )
            if mol_entry.formula == "Li1":
                if mol_entry.charge == 1:
                    LiEC_reextended_entries.append(mol_entry)
            else:
                LiEC_reextended_entries.append(mol_entry)

        RN = ReactionNetwork.from_input_entries(LiEC_reextended_entries)

        EC_mg = MoleculeGraph.with_local_env_strategy(
            Molecule.from_file(os.path.join(test_dir, "EC.xyz")),
            OpenBabelNN())
        EC_mg = metal_edge_extender(EC_mg)

        LiEC_mg = MoleculeGraph.with_local_env_strategy(
            Molecule.from_file(os.path.join(test_dir, "LiEC.xyz")),
            OpenBabelNN())
        LiEC_mg = metal_edge_extender(LiEC_mg)

        LiEC_RO_mg = MoleculeGraph.with_local_env_strategy(
            Molecule.from_file(os.path.join(test_dir, "LiEC_RO.xyz")),
            OpenBabelNN())
        LiEC_RO_mg = metal_edge_extender(LiEC_RO_mg)

        C2H4_mg = MoleculeGraph.with_local_env_strategy(
            Molecule.from_file(os.path.join(test_dir, "C2H4.xyz")),
            OpenBabelNN())
        C2H4_mg = metal_edge_extender(C2H4_mg)

        C1Li1O3_mg = MoleculeGraph.with_local_env_strategy(
            Molecule.from_file(os.path.join(test_dir, "C1Li1O3.xyz")),
            OpenBabelNN())
        C1Li1O3_mg = metal_edge_extender(C1Li1O3_mg)

        LiEC_entry = None
        LiEC_plus_entry = None
        EC_minus_entry = None
        EC_0_entry = None
        EC_1_entry = None
        LiEC_RO_entry = None
        C2H4_entry = None
        C1Li1O3_entry = None
        Li_entry = None

        for entry in RN.entries_list:
            if (entry.formula == "C3 H4 O3" and entry.num_bonds == 10
                    and EC_mg.isomorphic_to(entry.mol_graph)):
                if entry.charge == -1:
                    if EC_minus_entry is not None:
                        if EC_minus_entry.get_free_energy(
                        ) >= entry.get_free_energy():
                            EC_minus_entry = entry
                    else:
                        EC_minus_entry = entry
                elif entry.charge == 0:
                    if EC_0_entry is not None:
                        if EC_0_entry.get_free_energy(
                        ) >= entry.get_free_energy():
                            EC_0_entry = entry
                    else:
                        EC_0_entry = entry
                elif entry.charge == 1:
                    if EC_1_entry is not None:
                        if EC_1_entry.get_free_energy(
                        ) >= entry.get_free_energy():
                            EC_1_entry = entry
                    else:
                        EC_1_entry = entry
            elif (entry.formula == "C3 H4 Li1 O3" and entry.num_bonds == 12
                  and LiEC_mg.isomorphic_to(entry.mol_graph)):
                if entry.charge == 0:
                    if LiEC_entry is not None:
                        if LiEC_entry.get_free_energy(
                        ) >= entry.get_free_energy():
                            LiEC_entry = entry
                    else:
                        LiEC_entry = entry
                elif entry.charge == 1:
                    if LiEC_plus_entry is not None:
                        if LiEC_plus_entry.get_free_energy(
                        ) >= entry.get_free_energy():
                            LiEC_plus_entry = entry
                    else:
                        LiEC_plus_entry = entry
            elif (entry.formula == "C3 H4 Li1 O3" and entry.charge == 0
                  and entry.num_bonds == 11
                  and LiEC_RO_mg.isomorphic_to(entry.mol_graph)):
                if LiEC_RO_entry is not None:
                    if LiEC_RO_entry.get_free_energy(
                    ) >= entry.get_free_energy():
                        LiEC_RO_entry = entry
                else:
                    LiEC_RO_entry = entry

            elif (entry.formula == "C2 H4" and entry.charge == 0
                  and entry.num_bonds == 5
                  and C2H4_mg.isomorphic_to(entry.mol_graph)):
                if C2H4_entry is not None:
                    if C2H4_entry.get_free_energy() >= entry.get_free_energy():
                        C2H4_entry = entry
                else:
                    C2H4_entry = entry

            elif (entry.formula == "C1 Li1 O3" and entry.charge == 0
                  and entry.num_bonds == 5
                  and C1Li1O3_mg.isomorphic_to(entry.mol_graph)):
                if C1Li1O3_entry is not None:
                    if C1Li1O3_entry.get_free_energy(
                    ) >= entry.get_free_energy():
                        C1Li1O3_entry = entry
                else:
                    C1Li1O3_entry = entry
            elif entry.formula == "Li1" and entry.charge == 1 and entry.num_bonds == 0:
                if Li_entry is not None:
                    if Li_entry.get_free_energy() >= entry.get_free_energy():
                        Li_entry = entry
                else:
                    Li_entry = entry

        return {
            "entries": LiEC_reextended_entries,
            "RN": RN,
            "LiEC": LiEC_entry,
            "LiEC_plus": LiEC_plus_entry,
            "EC_1": EC_1_entry,
            "EC_0": EC_0_entry,
            "EC_-1": EC_minus_entry,
            "LiEC_RO": LiEC_RO_entry,
            "C2H4": C2H4_entry,
            "C1Li1O3": C1Li1O3_entry,
            "Li": Li_entry,
        }

    else:
        return None
Ejemplo n.º 15
0
    E = float(entry["output"]["final_energy"])
    H = float(entry["output"]["enthalpy"])
    S = float(entry["output"]["entropy"])
    mol_entry = MoleculeEntry(
        molecule=mol,
        energy=E,
        enthalpy=H,
        entropy=S,
        entry_id=entry["task_id"],
    )
    if mol_entry.formula == "Li1":
        if mol_entry.charge == 1:
            LiEC_reextended_entries.append(mol_entry)
    else:
        LiEC_reextended_entries.append(mol_entry)
reaction_network = ReactionNetwork.from_input_entries(
    LiEC_reextended_entries, electron_free_energy=-2.15)
reaction_network.build()
for entry in reaction_network.entries["C3 H4 Li1 O3"][12][1]:
    if LiEC_mg.isomorphic_to(entry.mol_graph):
        LiEC_ind = entry.parameters["ind"]
        break
for entry in reaction_network.entries["C3 H4 O3"][10][0]:
    if EC_mg.isomorphic_to(entry.mol_graph):
        EC_ind = entry.parameters["ind"]
        break
for entry in reaction_network.entries["C4 H4 Li2 O6"][17][0]:
    if LEDC_mg.isomorphic_to(entry.mol_graph):
        LEDC_ind = entry.parameters["ind"]
        break
Li1_ind = reaction_network.entries["Li1"][0][1][0].parameters["ind"]
pickle_in = open(os.path.join(test_dir, "unittest_RN_build.pkl"), "wb")
Ejemplo n.º 16
0
from mrnet.network.reaction_network import (
    ReactionNetwork
)
import os
import pickle
from monty.serialization import loadfn

benchmarking_dir = os.getcwd()

entries = loadfn(os.path.join(benchmarking_dir, "mrnet_all_of_entries_16334.json"))
RN = ReactionNetwork.from_input_entries(entries)
RN.build()
pickle_in = open(
    os.path.join(benchmarking_dir, benchmarking_dir + "large_reaction_network.pkl"),
    "wb",
)
pickle.dump(RN, pickle_in)
pickle_in.close()