Skip to content

WIP: Magic array

Ask Hjorth Larsen requested to merge magic-array into master

This adds 'magic' arrays which look like arrays but can behave differently.

Currently implemented/tested for atoms.tags. Tags behave like some zeros but don't allocate any data until someone sets a value.

However this mechanism is probably necessary if we want to support a more complex concept of 'species' than a list of strings. For example changing 'H' to 'Pu' should (normally) also change the mass. This will be possible with 'magic' arrays (but is not implemented here).

This has some advantages:

  • Improves our ability to represent uninitialized or "default" values (first discussed in #3).
  • We can do atoms.tags[:3] = 1. No need to get_tags(), then modify, then set_tags(). No worry about initializing the tags with a particular length.
  • Allows writing the properties proposed in !1351 without getting unwanted 'forgetfulness'.
  • Ability to set atoms.velocities[:3] += 7 and have this immediately reflected in the momenta (not implemented yet).

Of course this is complex which is bad, and we wouldn't like to break things too much. This shouldn't break anything because:

  • Nothing new is stored on Atoms
  • No existing method is changed
  • Everything works on top of atoms.arrays which is persistent (does not change). It could also work directly on Atoms, but I prefer that it receives the minimal data necessary.

Problems not yet entirely solved by this (unless we make possibly invasive changes):

  • Additional data like atoms.info['spacegroup'] going out of sync if someone changes positions/cell etc. If the magic arrays are found to be reliable, it could be done at some point though.
  • Implement the concept of 'species' on top of this. This would involve atoms.arrays['numbers'] sometimes being something else than physical atomic numbers, and a number of callbacks to properly support editing these things (for example changing the species should probably also reset the mass).

Comments would be highly appreciated.

Merge request reports