@@ -596,96 +596,62 @@ def merge(self, other):
596596 Merge the partial network `other` into this network.
597597 """
598598 # Make sure the two partial networks have the same source configuration
599- # assert self.source == other.source
600- # Use sets for merging to ensure uniqueness
601- self .isomers = list (set (self .isomers ) | set (other .isomers ))
602- self .explored = list (set (self .explored ) | set (other .explored ))
603- self .reactants = list (set (self .reactants ) | set (other .reactants ))
604- self .products = list (set (self .products ) | set (other .products )) # <--- FIX FOR PRODUCTS
599+ assert self .source == other .source
600+
601+ # Merge isomers
602+ for isomer in other .isomers :
603+ if isomer not in self .isomers :
604+ self .isomers .append (isomer )
605+ # Merge explored
606+ for isomer in other .explored :
607+ if isomer not in self .explored :
608+ self .explored .append (isomer )
609+ # Merge reactants
610+ for reactants in other .reactants :
611+ if reactants not in self .reactants :
612+ self .reactants .append (reactants )
613+ # Merge products
614+ for products in other .products :
615+ if products not in self .products :
616+ self .products .append (products )
605617
606618 # However, products that have been explored are actually isomers
619+ # These should be removed from the list of products!
607620 products_to_remove = []
608- for products_set in self .products : # Iterate through the ProductSet objects
609- # Check if all species in the products_set are in the merged isomers list
610- all_species_in_isomer_list = True
611- for sp in products_set .species : # Assuming ProductSet has a 'species' attribute (list of Species)
612- if sp not in self .isomers :
613- all_species_in_isomer_list = False
614- break
615- if all_species_in_isomer_list and len (products_set .species ) == 1 : # Only remove if it's a single isomer
616- products_to_remove .append (products_set )
617-
618- for products_set in products_to_remove :
619- self .products .remove (products_set )
621+ for products in self .products :
622+ if len (products .species ) == 1 and products .species [0 ] in self .isomers :
623+ products_to_remove .append (products )
624+ for products in products_to_remove :
625+ self .products .remove (products )
620626
621- # Merge path reactions (needs careful __eq__ for Reaction objects)
622- # Use a temporary set for efficient uniqueness check
623- current_path_reactions_set = set (self .path_reactions )
627+ # Merge path reactions
624628 for reaction in other .path_reactions :
625- current_path_reactions_set .add (reaction ) # Set automatically handles duplicates if Reaction has __eq__/__hash__
626- self .path_reactions = list (current_path_reactions_set )
629+ found = False
630+ for rxn in self .path_reactions :
631+ if reaction .reactants == rxn .reactants and reaction .products == rxn .products :
632+ # NB the isEquivalent() method that used to be on the previous line also checked reverse direction.
633+ # I am not sure which is appropriate
634+ found = True
635+ break
636+ if not found :
637+ self .path_reactions .append (reaction )
627638
628- # Merge net reactions
629- current_net_reactions_set = set (self .net_reactions )
639+ # Also merge net reactions (so that when we update the network in the
640+ # future, we update the existing net reactions rather than making new ones)
641+ # Q: What to do when a net reaction exists in both networks being merged?
630642 for reaction in other .net_reactions :
631- current_net_reactions_set .add (reaction )
632- self .net_reactions = list (current_net_reactions_set )
643+ found = False
644+ for rxn in self .net_reactions :
645+ if reaction .reactants == rxn .reactants and reaction .products == rxn .products :
646+ # NB the isEquivalent() method that used to be on the previous line also checked reverse direction.
647+ # I am not sure which is appropriate
648+ found = True
649+ break
650+ if not found :
651+ self .net_reactions .append (reaction )
633652
653+ # Mark this network as invalid
634654 self .valid = False
635- # # Merge isomers
636- # for isomer in other.isomers:
637- # if isomer not in self.isomers:
638- # self.isomers.append(isomer)
639- # # Merge explored
640- # for isomer in other.explored:
641- # if isomer not in self.explored:
642- # self.explored.append(isomer)
643- # # Merge reactants
644- # for reactants in other.reactants:
645- # if reactants not in self.reactants:
646- # self.reactants.append(reactants)
647- # # Merge products
648- # for products in other.products:
649- # if products not in self.products:
650- # self.products.append(products)
651-
652- # # However, products that have been explored are actually isomers
653- # # These should be removed from the list of products!
654- # products_to_remove = []
655- # for products in self.products:
656- # if len(products.species) == 1 and products.species[0] in self.isomers:
657- # products_to_remove.append(products)
658- # for products in products_to_remove:
659- # self.products.remove(products)
660-
661- # # Merge path reactions
662- # for reaction in other.path_reactions:
663- # found = False
664- # for rxn in self.path_reactions:
665- # if reaction.reactants == rxn.reactants and reaction.products == rxn.products:
666- # # NB the isEquivalent() method that used to be on the previous line also checked reverse direction.
667- # # I am not sure which is appropriate
668- # found = True
669- # break
670- # if not found:
671- # self.path_reactions.append(reaction)
672-
673- # # Also merge net reactions (so that when we update the network in the
674- # # future, we update the existing net reactions rather than making new ones)
675- # # Q: What to do when a net reaction exists in both networks being merged?
676- # for reaction in other.net_reactions:
677- # found = False
678- # for rxn in self.net_reactions:
679- # if reaction.reactants == rxn.reactants and reaction.products == rxn.products:
680- # # NB the isEquivalent() method that used to be on the previous line also checked reverse direction.
681- # # I am not sure which is appropriate
682- # found = True
683- # break
684- # if not found:
685- # self.net_reactions.append(reaction)
686-
687- # # Mark this network as invalid
688- # self.valid = False
689655
690656 def update_configurations (self , reaction_model ):
691657 """
0 commit comments