def test_concerted_reaction_filter(self):
     r, r_node = ReactionNetwork.concerted_reaction_filter("6,2+7", "2+1,3")
     self.assertEqual([[1, 6], [3, 7]], r)
     self.assertEqual([[1, 6], [3, 7], ["6,2+7", "2+1,3"]], r_node)
     r, r_node = ReactionNetwork.concerted_reaction_filter(
         "2+1,3+10", "6,2+7")
     self.assertEqual(r, None)
     self.assertEqual(r_node, None)
Exemple #2
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
    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)
Exemple #4
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,
        )
    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)],
        )
    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()
    def test_mols_w_cuttoff(self):
        with open(os.path.join(test_dir, "unittest_RN_pr_solved.pkl"),
                  "rb") as input:
            RN_pr_solved = pickle.load(input)

        RN_loaded = copy.deepcopy(RN_pr_solved)

        mols_to_keep, pruned_entries_list = ReactionNetwork.mols_w_cuttoff(
            RN_loaded, 0, build_pruned_network=False)

        self.assertEqual(len(mols_to_keep), 236)
    def test_parse_reaction_node(self):

        nodes = ["19+32,673", "41,992", "1+652,53+40", "4,6+5"]
        node_prod_react = []

        for node in nodes:
            r_p = ReactionNetwork.parse_reaction_node(node)
            node_prod_react.append(r_p)

        self.assertListEqual(
            node_prod_react,
            [([19, 32], [673]), ([41], [992]), ([1, 652], [40, 53]),
             ([4], [5, 6])],
        )
Exemple #9
0
    def generate_concerted_reactions(
        self,
        entry: MoleculeEntry,
    ) -> List[Tuple[List[int], List[int], float, float]]:
        """
        generate all the concerted reactions with intermediate mol_entry
        """
        (
            reactions,
            _,
        ) = ReactionNetwork.identify_concerted_rxns_for_specific_intermediate(
            entry,
            self.rn,
            mols_to_keep=[e.parameters["ind"] for e in self.rn.entries_list],
            single_elem_interm_ignore=self.single_elem_interm_ignore,
        )

        return_list = []

        for (reactants, products) in reactions:
            new_reactants = []
            new_products = []
            for reactant_id in reactants:
                if reactant_id is not None:
                    new_reactants.append(self.rn.entries_list[reactant_id])

            for product_id in products:
                if product_id is not None:
                    new_products.append(self.rn.entries_list[product_id])

            cs = ConcertedReaction(
                new_reactants,
                new_products,
                electron_free_energy=self.rn.electron_free_energy,
            )

            if cs:
                return_list.append((
                    list(cs.reactant_indices),
                    list(cs.product_indices),
                    cs.free_energy_A,
                    cs.free_energy_B,
                ))
            else:
                print("concerted reaction not created:")
                print("reactants:", reactants)
                print("products:", products)

        return return_list
    def test_generate_node_string(self):

        react_prod = [
            ([19, 32], [673]),
            ([41], [992]),
            ([1, 652], [40, 53]),
            ([4], [5, 6]),
        ]
        node_strings = []

        for rxn in react_prod:
            node_str = ReactionNetwork.generate_node_string(rxn[0], rxn[1])
            node_strings.append(node_str)

        self.assertListEqual(node_strings,
                             ["19+32,673", "41,992", "1+652,40+53", "4,5+6"])
    def test_identify_concerted_rxns_via_intermediates(self):
        with open(os.path.join(test_dir, "unittest_RN_pr_solved.pkl"), "rb") as input:
            RN_pr_solved = pickle.load(input)

        RN_loaded = copy.deepcopy(RN_pr_solved)

        with open(
            os.path.join(test_dir, "RN_unittest_pruned_mols_to_keep.json"), "rb"
        ) as handle:
            mols_to_keep = pickle.load(handle)

        reactions = ReactionNetwork.identify_concerted_rxns_via_intermediates(
            RN_loaded, mols_to_keep, single_elem_interm_ignore=["C1", "H1", "O1", "Li1"]
        )

        self.assertEqual(len(reactions), 2410)
Exemple #12
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())
Exemple #13
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,
        )
Exemple #14
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,
        )
Exemple #15
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
    def setUpClass(cls):
        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)

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

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

        cls.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:
                    cls.LiEC_reextended_entries.append(mol_entry)
            else:
                cls.LiEC_reextended_entries.append(mol_entry)

        cls.entries_box = EntriesBox(cls.LiEC_reextended_entries,
                                     remove_complexes=False)

        cls.RI = ReactionIterator(cls.entries_box)
        cls.RN = ReactionNetwork(cls.RI, add_concerteds=False)

        # set up input variables
        cls.LEDC_ind = None
        cls.LiEC_ind = None
        cls.EC_ind = None

        for entry in cls.entries_box.entries_dict["C3 H4 O3"][10][0]:
            if EC_mg.isomorphic_to(entry.mol_graph):
                cls.EC_ind = entry.parameters["ind"]
                break

        for entry in cls.entries_box.entries_dict["C4 H4 Li2 O6"][17][0]:
            if LEDC_mg.isomorphic_to(entry.mol_graph):
                cls.LEDC_ind = entry.parameters["ind"]
                break

        for entry in cls.entries_box.entries_dict["C3 H4 Li1 O3"][12][1]:
            if LiEC_mg.isomorphic_to(entry.mol_graph):
                cls.LiEC_ind = entry.parameters["ind"]
                break

        cls.Li1_ind = cls.entries_box.entries_dict["Li1"][0][1][0].parameters[
            "ind"]

        print("LEDC_ind:", cls.LEDC_ind)
        print("LiEC_ind:", cls.LiEC_ind)
        print("EC_ind:", cls.EC_ind)
        print("Li1_ind:", cls.Li1_ind)

        cls.RN_solved = copy.deepcopy(cls.RN)
        cls.RN_solved.solve_prerequisites([cls.EC_ind, cls.Li1_ind],
                                          weight="softplus")
Exemple #17
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
Exemple #18
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)
Exemple #19
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)
Exemple #20
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")
Exemple #21
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,
        )
Exemple #22
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()
Exemple #23
0
                                   validate_concerted_rxns)
import os
import sys
import pickle
from monty.serialization import loadfn
import time

benchmarking_dir = os.getcwd()

# keyword = sys.argv[1]
keyword = 'small'
if keyword == 'large':
    network_file = "large_reaction_network.pkl"
elif keyword == 'small':
    network_file = "small_reaction_network.pkl"
else:
    raise Exception("benchmarking.py takes 'large' or 'small' as a keyword to "
                    "select which network to benchmark.")

with open(
        os.path.join(benchmarking_dir, network_file),
        "rb",
) as input:
    RN_loaded = pickle.load(input)

start_time = time.time()
unique_rxns, all_rxns = ReactionNetwork.identify_concerted_rxns_via_intermediates(
    RN_loaded, single_elem_interm_ignore=[])
print('Concerted identified at: ', time.time() - start_time)
RN_concerted = ReactionNetwork.add_concerted_rxns(RN_loaded, unique_rxns)
print('Concerted added at: ', time.time() - start_time)
Exemple #24
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()
    def test_identify_concerted_rxns_via_intermediates(self):

        # v1 and v2 comparison
        # load reactions from v1 of concerted reactions
        v1 = loadfn(
            os.path.join(test_dir,
                         "identify_concerted_intermediate_list_v1.json"))
        # load RN
        with open(
                os.path.join(
                    test_dir,
                    "identify_concerted_via_intermediate_unittest_RN.pkl"),
                "rb",
        ) as input:
            RN_loaded = pickle.load(input)

        # process reaction list from v1 of concerted reaction such that Nones are removed,
        # A+B>B+A and repeated reactions are removed
        v1_processed = []
        for r_old in v1:
            a = r_old[0]
            b = r_old[1]
            if None in b:
                b.remove(None)
            if len(a) == 2 and len(b) == 2:
                inter = 0
                inter = set(a).intersection(set(b))
                for i in inter:
                    a.remove(i)
                    b.remove(i)
            a.sort()
            b.sort()
            c = [a, b]
            v1_processed.append(c)

        with open(
                os.path.join(
                    test_dir,
                    "identify_concerted_intermediate_list_v2_iter1.pkl"),
                "rb",
        ) as handle:
            v2_unique = pickle.load(handle)

        v1_set = set(map(lambda x: repr(x), v1_processed))
        v2_set = set(map(lambda x: repr(x), v2_unique))
        inter = list(v1_set.intersection(v2_set))
        v1_v2 = list(map(lambda y: literal_eval(y), v1_set - v2_set))
        v2_v1 = list(map(lambda y: literal_eval(y), v2_set - v1_set))

        # check if v2 missed any reactions found by v1
        missed_by_v2 = []
        for r in v1_v2:
            node_str = ReactionNetwork.generate_node_string(r[0], r[1])
            if node_str not in RN_loaded.graph.nodes:
                missed_by_v2.append(r)
        # checks number of same reactions in v1 and v2, number of reactions missed by v2,
        # and number of new reactions found by v2
        self.assertEqual(len(missed_by_v2), 0)
        self.assertEqual(len(v1_v2), 11)
        self.assertEqual(len(v2_v1), 0)
        self.assertEqual(len(inter), 29214)
        self.assertEqual(len(v2_unique), 29214)

        with open(
                os.path.join(
                    test_dir,
                    "identify_concerted_intermediate_list_v2_iter2.pkl"),
                "rb",
        ) as handle:
            v2_unique_iter2 = pickle.load(handle)

        v2_set_iter2 = set(map(lambda x: repr(x), v2_unique_iter2))
        inter_iter2 = list(v1_set.intersection(v2_set_iter2))
        self.assertEqual(len(inter_iter2), 0)
        self.assertEqual(len(v2_unique_iter2), 2100)

        # v2 and v3 comparison

        RN_loaded.matrix = None
        (
            v3_unique_iter1,
            v3_all_iter1,
        ) = RN_loaded.identify_concerted_rxns_via_intermediates(
            RN_loaded, single_elem_interm_ignore=[], update_matrix=True)

        v3_set = set(map(lambda x: repr(x), v3_unique_iter1))

        inter_v2_v3 = list(v2_set.intersection(v3_set))
        v2_v3 = list(map(lambda y: literal_eval(y), v2_set - v3_set))
        v3_v2 = list(map(lambda y: literal_eval(y), v3_set - v2_set))
        self.assertEqual(len(v3_unique_iter1), 29225)
        self.assertEqual(len(v2_v3), 0)
        self.assertEqual(len(v3_v2), 11)
        self.assertEqual(len(inter_v2_v3), 29214)
        new_by_v3 = []
        for r in v3_v2:
            node_str = ReactionNetwork.generate_node_string(r[0], r[1])
            if node_str not in RN_loaded.graph.nodes:
                new_by_v3.append(r)

        self.assertEqual(len(new_by_v3), 0)