From d4f146371a457dabf0142eaa240af267a6f2331e Mon Sep 17 00:00:00 2001 From: ssun30 Date: Fri, 4 Oct 2024 16:27:19 -0400 Subject: [PATCH] Fixed Fragment test failure The `Fragment` class is now a subclass of `Molecule`. As such, some if statements for checking atom charges no longer work for Fragments since CuttingLabels don't have defined charges. These if statements were modified such that charge checks for `Fragments` ignore CuttingLabels, and will skip charge checks for `Molecules` if the species is an instance `Fragments`. --- rmgpy/data/kinetics/family.py | 5 ++--- rmgpy/reaction.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rmgpy/data/kinetics/family.py b/rmgpy/data/kinetics/family.py index 8ba433e34a..291e639380 100644 --- a/rmgpy/data/kinetics/family.py +++ b/rmgpy/data/kinetics/family.py @@ -1519,10 +1519,9 @@ def apply_recipe(self, reactant_structures, forward=True, unique=True, relabel_a # If product structures are Group objects and the reaction is in certain families # (families with charged substances), the charge of structures will be updated if isinstance(struct, Molecule): - struct.update_charge() + if not isinstance(struct, Fragment): + struct.update_charge() struct.update(sort_atoms=not self.save_order) - elif isinstance(struct, Fragment): - struct.update() elif isinstance(struct, Group): is_molecule = False struct.reset_ring_membership() diff --git a/rmgpy/reaction.py b/rmgpy/reaction.py index 90471acc19..80be78e154 100644 --- a/rmgpy/reaction.py +++ b/rmgpy/reaction.py @@ -1440,16 +1440,16 @@ def is_balanced(self): if not isinstance(atom, CuttingLabel): reactants_net_charge += atom.charge reactant_elements[atom.element] += 1 - elif isinstance(reactant, Molecule): - molecule = reactant - for atom in molecule.atoms: - reactants_net_charge += atom.charge - reactant_elements[atom.element] += 1 elif isinstance(reactant, Fragment): for atom in reactant.atoms: if not isinstance(atom, CuttingLabel): reactants_net_charge += atom.charge reactant_elements[atom.element] += 1 + elif isinstance(reactant, Molecule): + molecule = reactant + for atom in molecule.atoms: + reactants_net_charge += atom.charge + reactant_elements[atom.element] += 1 for product in self.products: if isinstance(product, Species): molecule = product.molecule[0] @@ -1457,16 +1457,16 @@ def is_balanced(self): if not isinstance(atom, CuttingLabel): products_net_charge += atom.charge product_elements[atom.element] += 1 - elif isinstance(product, Molecule): - molecule = product - for atom in molecule.atoms: - products_net_charge += atom.charge - product_elements[atom.element] += 1 elif isinstance(product, Fragment): for atom in product.atoms: if not isinstance(atom, CuttingLabel): products_net_charge += atom.charge product_elements[atom.element] += 1 + elif isinstance(product, Molecule): + molecule = product + for atom in molecule.atoms: + products_net_charge += atom.charge + product_elements[atom.element] += 1 for element in element_list: if reactant_elements[element] != product_elements[element]: