Search code examples
pyiron

How to use masking with structures to modify only selected atoms?


I want to modify the position of atoms that fulfil a specific condition.

Currently, I can apply translation to all atoms or one atom but not multiple selected atoms.

from pyiron import Project
pr = Project('test')
uc = pr.create_ase_bulk('Fe',cubic=True)
uc.set_repeat(2)
shift = [.1,.2,.3]
uc.positions

For example, I want to translate only atoms whose Z coordinate is less than 1:

uc[uc.positions[:,2] < 1].positions += shift
uc.positions

Neither it moves atoms nor it returns any error.

However, translating only one atom or all atoms are working properly.

uc.positions += shift
uc.positions
uc[0].position += shift
uc[0]

Solution

  • This one should work:

    uc.positions[uc.positions[:,2] < 1] += shift
    

    In principle when you are working with positions, cell etc., it's always safer to use the setter/getter of the attributes (and not the setter/getter of Atoms). I personally use the setter of Atoms only to change atom species, e.g. uc[0] = 'Ni'.