diff --git a/rmgpy/rmg/model.py b/rmgpy/rmg/model.py index 18be219271..c0264817ea 100644 --- a/rmgpy/rmg/model.py +++ b/rmgpy/rmg/model.py @@ -118,14 +118,7 @@ def merge(self, other): # Add the unique species from other to the final model finalModel.species.extend(uniqueSpecies) - - # Renumber the unique species (to avoid name conflicts on save) - speciesIndex = 0 - for spec in finalModel.species: - if spec.label not in ['Ar','N2','Ne','He']: - spec.index = speciesIndex + 1 - speciesIndex += 1 - + # Make sure unique reactions only refer to species in the final model for rxn in uniqueReactions: for i, reactant in enumerate(rxn.reactants): diff --git a/rmgpy/tools/merge_models.py b/rmgpy/tools/merge_models.py index c59790aa90..90200c29f1 100644 --- a/rmgpy/tools/merge_models.py +++ b/rmgpy/tools/merge_models.py @@ -136,6 +136,7 @@ def get_models_to_merge(input_model_files): def combine_models(models): """ Takes in a list of ReactionModels and and merges them into a single ReactionModel + Reindexes species with the same label and index """ final_model = ReactionModel() for i, model in enumerate(models): @@ -160,4 +161,17 @@ def combine_models(models): '#{0:d}.'.format(i+1, Nrxn - Nrxn0, len(model.reactions))) print('The merged model has {0:d} species and {1:d} reactions' ''.format(len(final_model.species), len(final_model.reactions))) + + # ensure no species with same name and index + label_index_dict = {} + for s in final_model.species: + if s.label not in label_index_dict: + label_index_dict[s.label] = [s.index] + else: + if s.index in label_index_dict[s.label]: + # obtained a duplicate + s.index = max(label_index_dict[s.label]) + 1 + print("Reindexed {0} due to dublicate labels and index".format(s.label)) + label_index_dict[s.label].append(s.index) + return final_model