tips.rst 1.8 KB
Newer Older
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
1 2 3 4
===============
Tips and tricks
===============

5 6 7 8 9 10 11 12
In order to get the most out out the tips below (and ASE in general), it
is a good idea to get to know the Python language and the NumPy library well.
See:

* https://www.python.org/
* https://docs.scipy.org/doc/numpy/

.. contents::
13 14


Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
15 16 17 18 19 20
Atoms objects
=============

Species
-------

21 22 23 24
>>> from ase import Atoms
>>> atoms = Atoms('CH4')
>>> len(set(atoms.numbers))  # number of species
2
25 26
>>> set(atoms.get_chemical_symbols())  # set of species
{'C', 'H'}
27

Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
28

29 30
Indexing
--------
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
31

32
>>> atoms
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
33
Atoms(symbols='CH4', pbc=False)
34 35 36
>>> [atom.index for atom in atoms if atom.symbol == 'H']
[1, 2, 3, 4]
>>> atoms[[atom.index for atom in atoms if atom.symbol == 'H']]
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
37
Atoms(symbols='H4', pbc=False)
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
38 39 40

Indexing with lists of booleans:

41
>>> atoms.numbers == 1
42
array([False,  True,  True,  True,  True], dtype=bool)
43
>>> atoms[atoms.numbers == 1]
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
44
Atoms(symbols='H4', pbc=False)
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
45

46
Three equivalent ways to delete carbon atoms:
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
47

48 49 50
>>> del atoms[atoms.numbers == 6]
>>> del atoms[[atom.index for atom in atoms if atom.symbol == 'C']]
>>> del atoms[[atom.symbol == 'C' for atom in atoms]]
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
51

52 53 54 55
Swap the positions of two atoms with index 3 and 4:

>>> atoms.positions[[3, 4]] = atoms.positions[[4, 3]]

Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
56 57 58 59 60 61 62

Trajectories
============

Append one trajectory to the end of another
-------------------------------------------

63 64 65 66 67 68 69 70
.. testsetup::

    from ase.io import write
    from ase import Atoms
    write('t1.traj', Atoms('H'))
    write('t2.traj', Atoms('H'))
    write('abc.traj', Atoms('H'))

Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
71
>>> from ase.io import Trajectory
72
>>> t1 = Trajectory('t1.traj', 'a')
Jens Jørgen Mortensen's avatar
Jens Jørgen Mortensen committed
73 74 75 76 77 78
>>> t2 = Trajectory('t2.traj')
>>> for atoms in t2:
...     t1.write(atoms)
>>> t1.close()


79 80 81 82 83 84 85
Input/output
============

Convert from one format to another
----------------------------------

>>> from ase.io import read, write
86
>>> write('abc.xyz', read('abc.traj'))