Skip to content

Commit

Permalink
Minimize reindexing in merge_models
Browse files Browse the repository at this point in the history
This commit replaces a complete reindex for merged models
with a smart (as in 'smart speaker') algorithm that only reindexes a
species when it is has both an identical label and index with another
species. This prevents conflicts and reduces unnecessary reindexing.
  • Loading branch information
goldmanm committed Jul 24, 2019
1 parent 0c5c468 commit a4f0320
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
9 changes: 1 addition & 8 deletions rmgpy/rmg/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 14 additions & 0 deletions rmgpy/tools/merge_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

0 comments on commit a4f0320

Please sign in to comment.