1# test read
2# https://wiki.openchemistry.org/Chemical_JSON
3import pytest
4import numpy as np
5
6from ase import io
7
8ethane = """{
9  "chemical json": 0,
10  "name": "ethane",
11  "inchi": "1/C2H6/c1-2/h1-2H3",
12  "formula": "C 2 H 6",
13  "atoms": {
14    "elements": {
15      "number": [  1,   6,   1,   1,   6,   1,   1,   1 ]
16    },
17    "coords": {
18      "3d": [  1.185080, -0.003838,  0.987524,
19               0.751621, -0.022441, -0.020839,
20               1.166929,  0.833015, -0.569312,
21               1.115519, -0.932892, -0.514525,
22              -0.751587,  0.022496,  0.020891,
23              -1.166882, -0.833372,  0.568699,
24              -1.115691,  0.932608,  0.515082,
25              -1.184988,  0.004424, -0.987522 ]
26    }
27  },
28  "bonds": {
29    "connections": {
30      "index": [ 0, 1,
31                 1, 2,
32                 1, 3,
33                 1, 4,
34                 4, 5,
35                 4, 6,
36                 4, 7 ]
37    },
38    "order": [ 1, 1, 1, 1, 1, 1, 1 ]
39  },
40  "properties": {
41    "molecular mass": 30.0690,
42    "melting point": -172,
43    "boiling point": -88
44  }
45}
46"""
47
48tio2 = """{
49  "chemicalJson": 1,
50  "name": "TiO2 rutile",
51  "formula": "Ti 2 O 4",
52  "unitCell": {
53    "a": 2.95812,
54    "b": 4.59373,
55    "c": 4.59373,
56    "alpha": 90.0,
57    "beta":  90.0,
58    "gamma": 90.0
59  },
60  "atoms": {
61    "elements": {
62      "number": [ 22, 22, 8, 8, 8, 8 ]
63    },
64    "coords": {
65      "3dFractional": [ 0.00000, 0.00000, 0.00000,
66                        0.50000, 0.50000, 0.50000,
67                        0.00000, 0.30530, 0.30530,
68                        0.00000, 0.69470, 0.69470,
69                        0.50000, 0.19470, 0.80530,
70                        0.50000, 0.80530, 0.19470 ]
71    }
72  }
73}
74"""
75
76
77def test_ethane():
78    fname = 'ethane.cml'
79    with open(fname, 'w') as fd:
80        fd.write(ethane)
81
82    atoms = io.read(fname)
83
84    assert str(atoms.symbols) == 'HCH2CH3'
85
86
87def test_rutile():
88    fname = 'TiO2_rutile.cml'
89    with open(fname, 'w') as fd:
90        fd.write(tio2)
91
92    atoms = io.read(fname)
93
94    assert atoms.pbc.all()
95    cell = atoms.cell
96
97    assert str(atoms.symbols) == 'Ti2O4'
98    assert atoms[1].position == pytest.approx(cell.diagonal() / 2)
99
100    assert cell[1, 1] == cell[2, 2]
101    assert cell == pytest.approx(np.diag(cell.diagonal()))
102