pythonasexyz

Adding string column to ext-xyz file using python ASE


I have an ase Atom class defined as such:

atoms = ase.Atoms(elements, np.array(flat_AP), cell=rand_box_dim, pbc=True)

Written as an ext-xyz file, this gives:

9
Lattice="2.172266536730066 0.0 0.0 0.0 2.497767468610632 0.0 0.0 0.0 2.6921538042558764" Properties=species:S:1:pos:R:3 pbc="T T T"
C        0.00000000       0.00000000       0.00000000
H        0.00000000      -0.04846279      -0.98881311
H        0.00000000      -0.83210588       0.53637656
C        1.08613327       0.68275479       0.35079970
H        1.08613327       1.51486066      -0.18557686
H        1.08613327       0.73121757       1.33961280
C        2.17226654       0.00000000       0.00000000
H        2.17226654      -0.83210588       0.53637656
H        2.17226654      -0.04846279      -0.98881311

I can introduce a new column for a property 'molid' as such:

atoms.arrays['molid'] = np.ones(len(atoms),dtype='int')

Which gives me:

9
Lattice="2.172266536730066 0.0 0.0 0.0 2.497767468610632 0.0 0.0 0.0 2.6921538042558764" Properties=species:S:1:pos:R:3:molid:I:1 pbc="T T T"
C        0.00000000       0.00000000       0.00000000 1
H        0.00000000      -0.04846279      -0.98881311 1
H        0.00000000      -0.83210588       0.53637656 1
C        1.08613327       0.68275479       0.35079970 1
H        1.08613327       1.51486066      -0.18557686 1
H        1.08613327       0.73121757       1.33961280 1
C        2.17226654       0.00000000       0.00000000 1
H        2.17226654      -0.83210588       0.53637656 1
H        2.17226654      -0.04846279      -0.98881311 1

Note that it adds the property 'molid' to the Properties= section of the file.

My question is - how would I be able to do the same thing for the property 'type', e.g. ['CT', 'HC', 'HC', 'CT', 'HC', 'HC', 'CT', 'HC', 'HC'], and add this to the ext-xyz file as a column?

I have tried this:

types = []
[types.append("CT") if x%3 == 0 else types.append("HC") for x in range(len(atoms))]
atoms.arrays['types'] = types

Which returns this error: AttributeError: 'list' object has no attribute 'dtype'


Solution

  • Solved: it turns out numpy arrays are able to take strings as input!

    [types.append("CT") if x%3 == 0 else types.append("HC") for x in range(len(atoms))]
    types = np.array(types)
    atoms.arrays['type'] = types