Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Module to read atoms in chemical json file format. 

2 

3https://wiki.openchemistry.org/Chemical_JSON 

4""" 

5import json 

6import numpy as np 

7 

8from ase import Atoms 

9from ase.cell import Cell 

10 

11 

12# contract and lower case string 

13def contract(dictionary): 

14 dcopy = {} 

15 for key in dictionary: 

16 dcopy[key.replace(' ', '').lower()] = dictionary[key] 

17 return dcopy 

18 

19 

20def read_cml(fileobj): 

21 data = contract(json.load(fileobj)) 

22 atoms = Atoms() 

23 datoms = data['atoms'] 

24 

25 atoms = Atoms(datoms['elements']['number']) 

26 

27 if 'unitcell' in data: 

28 cell = data['unitcell'] 

29 a = cell['a'] 

30 b = cell['b'] 

31 c = cell['c'] 

32 alpha = cell['alpha'] 

33 beta = cell['beta'] 

34 gamma = cell['gamma'] 

35 atoms.cell = Cell.fromcellpar([a, b, c, alpha, beta, gamma]) 

36 atoms.pbc = True 

37 

38 coords = contract(datoms['coords']) 

39 if '3d' in coords: 

40 positions = np.array(coords['3d']).reshape(len(atoms), 3) 

41 atoms.set_positions(positions) 

42 else: 

43 positions = np.array(coords['3dfractional']).reshape(len(atoms), 3) 

44 atoms.set_scaled_positions(positions) 

45 

46 yield atoms