Statistiques
| Révision :

root / ase / visualize / __init__.py @ 1

Historique | Voir | Annoter | Télécharger (1,54 ko)

1
import os
2
import tempfile
3

    
4
from ase.io import write
5
import ase.parallel as parallel
6
from ase.old import OldASEListOfAtomsWrapper
7

    
8
def view(atoms, data=None, viewer='ag', repeat=None, block=False):
9
    # Ignore for parallel calculations:
10
    if parallel.size != 1:
11
        return
12

    
13
    if hasattr(atoms, 'GetUnitCell'):
14
        # Convert old ASE ListOfAtoms to new style.
15
        atoms = OldASEListOfAtomsWrapper(atoms).copy()
16

    
17
    vwr = viewer.lower()
18
    
19
    if vwr == 'ag':
20
        format = 'traj'
21
        if repeat is None:
22
            command = 'ag'
23
        else:
24
            command = 'ag --repeat=%d,%d,%d' % tuple(repeat)
25
            repeat = None
26
    elif vwr == 'vmd':
27
        format = 'cube'
28
        command = 'vmd'
29
    elif vwr == 'rasmol':
30
        format = 'pdb'
31
        command = 'rasmol -pdb'
32
    elif vwr == 'xmakemol':
33
        format = 'xyz'
34
        command = 'xmakemol -f'
35
    elif vwr == 'gopenmol':
36
        format = 'xyz'
37
        command = 'rungOpenMol'
38
    elif vwr == 'avogadro':
39
        format = 'cube'
40
        command = 'avogadro'
41
    else:
42
        raise RuntimeError('Unknown viewer: ' + viewer)
43

    
44
    fd, filename = tempfile.mkstemp('.' + format, 'ase-')
45
    fd = os.fdopen(fd, 'w')
46
    if repeat is not None:
47
        atoms = atoms.repeat()
48
    if data is None:
49
        write(fd, atoms, format=format)
50
    else:
51
        write(fd, atoms, format=format, data=data)
52
    fd.close()
53
    if block:
54
        os.system('%s %s' % (command, filename))
55
    else:
56
        os.system('%s %s &' % (command, filename))
57
    os.system('(sleep 60; rm %s) &' % filename)