Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add with ids #364

Merged
merged 19 commits into from
Jul 21, 2021
Merged

Add with ids #364

merged 19 commits into from
Jul 21, 2021

Conversation

lukasturcani
Copy link
Owner

@lukasturcani lukasturcani commented Jul 9, 2021

Requested Reviewers: @andrewtarzia
Note for Reviewers: If you accept the review request add a 👍 to this post

A type error has been discovered!

a = stk.Alcohol(
    oxygen=stk.O(0), 
    hydrogen=stk.H(1), 
    atom=stk.C(2), 
    bonders=(stk.C(2), ),
    deleters=(stk.O(0), stk.H(1)),
)

clone = a.with_atoms({
    0: stk.C(0),
})

# "oxygen" is actually a carbon!
oxygen = clone.get_oxygen()

The solution to this type error is to make the return type of with_atoms() to return either FunctionalGroup or GenericFuncitonalGroup as appropriate, but not the most derived type.

However, this is a problem because during construction, we need to make clones, with different atom ids, but we also need to preserve the most derived type, because this information may be used by reaction factories to select an appropriate reaction. For example, you might want a specific bond order between alcohol and carboxylic acid functional groups.

To get around this, I'm adding with_ids, it's just likewith_atoms except it only lets you change atom ids but not atom types. This means that the most derived type can be returned. Internal use of with_atoms has been replaced by with_ids so that the most derived type can be preserved.

I am also adding Bond.with_ids so that you can use the same dict when using FunctionalGroup.with_ids for bonds as well. Otherwise the user would have to create an id map for the functional groups but an atom map for bonds, and that's kind of annoying.

Other code changes:

  • src/stk/molecular/functional_groups/.../functional_group.py:260: I removed the _with_atoms method and replaced it with a new implementation of with_atoms. The new implementation does not return a clone of the most derived type, instead it always returns a FunctionalGroup instance.
  • src/stk/molecular/functional_gorups/.../generic_functional_gorup.py:63 Replaced the _with_atoms implementation with an implementation of with_atoms, which always returns a GenericFunctionalGroup instance.
  • src/stk/molecular/molecules/building_block.py:692: Here I removed the use of with_atoms and did not replace it with with_ids. with_atoms was being used here to minimize memory usage. I.e. the functional groups held internally by the building block held the atoms from self._atoms, rather than the atoms the user provided. This means that the atoms which the user created / provided could be garbage collected. Because with_atoms loses the most derived type, it can no longer be used here and there is no way to replace its use here with with_ids.
  • src/stk/.../placements_summary/atom_batch.py:45: I replaced the use of internal lists with internal tuples, just so that the immutability is a bit more clear. The downside is an extra iteration, but immutability is more important in my opinion.

@lukasturcani lukasturcani changed the title Lukas/add with ids Add with ids Jul 9, 2021
@lukasturcani lukasturcani marked this pull request as ready for review July 9, 2021 13:05
Copy link
Collaborator

@andrewtarzia andrewtarzia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the fact that the tests all passed and the structures were maintained after these changes, means its all good!

@lukasturcani lukasturcani merged commit 6d214ca into master Jul 21, 2021
@lukasturcani lukasturcani deleted the lukas/add-with-ids branch July 21, 2021 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants