diff --git a/src/pyvalem/formula.py b/src/pyvalem/formula.py index 034568e..5a6a3b1 100644 --- a/src/pyvalem/formula.py +++ b/src/pyvalem/formula.py @@ -315,6 +315,9 @@ def _parse_formula(self, formula): # photons, electrons, positrons and "M", denoting an unspecified # "third-body" in many reactions. Note that M does not have a defined # charge or mass. + if formula == "e": + # Quietly convert e to e-. + self.formula = formula = "e-" if formula in special_cases: for attr, val in special_cases[formula].items(): setattr(self, attr, val) diff --git a/src/pyvalem/reaction.py b/src/pyvalem/reaction.py index 4800cec..9c24f55 100644 --- a/src/pyvalem/reaction.py +++ b/src/pyvalem/reaction.py @@ -249,6 +249,7 @@ def _parse_species(self, lhs_str, rhs_str): ) ss = StatefulSpecies(ss_str) species.append((n, ss)) + ss_str = str(ss) if ss_str not in species_map: species_map[ss_str] = 0 species_map[ss_str] += n diff --git a/tests/test_formula.py b/tests/test_formula.py index a4ed070..2db2902 100644 --- a/tests/test_formula.py +++ b/tests/test_formula.py @@ -134,6 +134,7 @@ def test_special_formulas(self): ), "hv": ("hν", "hν", r"h\nu", "hv", 0, None, 0, {}), } + f["e"] = f["e-"] f["hν"] = f["hv"] for f_str in f: diff --git a/tests/test_reaction.py b/tests/test_reaction.py index 9247b3b..dd3d274 100644 --- a/tests/test_reaction.py +++ b/tests/test_reaction.py @@ -129,6 +129,8 @@ def test_reaction_equality(self): ], ["H + H + M -> H2 + M", "2H + M -> H2 + M"], ["Kr + hv -> Kr + 2hv", "Kr + hv -> Kr + hv + hv"], + ["Kr + hv -> Kr *", "Kr + hν -> Kr *"], + ["Kr + e- -> Kr- *", "Kr + e -> Kr- *"], ] for r1, r2 in equal: self.assertEqual(Reaction(r1), Reaction(r2)) @@ -186,14 +188,20 @@ def test_reaction_species_map(self): self.assertEqual(r.reactants_text_count_map, {"e-": 2, "C2": 1}) self.assertEqual(r.products_text_count_map, {"C-": 2}) r = Reaction("e- + e- + hv -> ", strict=False) - self.assertEqual(r.reactants_text_count_map, {"e-": 2, "hv": 1}) + self.assertEqual(r.reactants_text_count_map, {"e-": 2, "hν": 1}) self.assertEqual(r.products_text_count_map, {}) r = Reaction("1e- + 1e- + 2hv -> ", strict=False) - self.assertEqual(r.reactants_text_count_map, {"e-": 2, "hv": 2}) + self.assertEqual(r.reactants_text_count_map, {"e-": 2, "hν": 2}) r = Reaction("e- + O2 X(3Σ-g) -> ", strict=False) self.assertEqual(r.reactants_text_count_map, {"e-": 1, "O2 X(3Σ-g)": 1}) r = Reaction("e- + O2 X(3SIGMA-g) -> ", strict=False) - self.assertEqual(r.reactants_text_count_map, {"e-": 1, "O2 X(3SIGMA-g)": 1}) + self.assertEqual(r.reactants_text_count_map, {"e-": 1, "O2 X(3Σ-g)": 1}) + + def test_reaction_with_e(self): + r = Reaction("e + C2 + e -> C- + C-") + self.assertEqual(r.reactants_text_count_map, {"e-": 2, "C2": 1}) + self.assertEqual(r.products_text_count_map, {"C-": 2}) + self.assertEqual(repr(r), "2e- + C2 → C- + C-") if __name__ == "__main__":