File: db.py

package info (click to toggle)
python-ase 3.17.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,340 kB
  • sloc: python: 117,348; makefile: 91
file content (97 lines) | stat: -rw-r--r-- 2,790 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import time
from ase.test import cli
from ase.db import connect

cmd = """
ase build H | ase run emt -d testase.json &&
ase build H2O | ase run emt -d testase.json &&
ase build O2 | ase run emt -d testase.json &&
ase build H2 | ase run emt -f 0.02 -d testase.json &&
ase build O2 | ase run emt -f 0.02 -d testase.json &&
ase build -x fcc Cu | ase run emt -E 5,1 -d testase.json &&
ase db -v testase.json natoms=1,Cu=1 --delete --yes &&
ase db -v testase.json "H>0" -k hydro=1,abc=42,foo=bar &&
ase db -v testase.json "H>0" --delete-keys foo"""


def count(n, *args, **kwargs):
    m = len(list(con.select(columns=['id'], *args, **kwargs)))
    assert m == n, (m, n)


t0 = time.time()
for name in ['testase.json', 'testase.db', 'postgresql']:
    if name == 'postgresql':
        if os.environ.get('POSTGRES_DB'):  # gitlab-ci
            name = 'postgresql://ase:ase@postgres:5432/testase'
        else:
            name = os.environ.get('ASE_TEST_POSTGRES_URL')
            if name is None:
                continue

    con = connect(name)
    t1 = time.time()
    if 'postgres' in name:
        con.delete([row.id for row in con.select()])

    cli(cmd.replace('testase.json', name))
    assert con.get_atoms(H=1)[0].magmom == 1
    count(5)
    count(3, 'hydro')
    count(0, 'foo')
    count(3, abc=42)
    count(3, 'abc')
    count(0, 'abc,foo')
    count(3, 'abc,hydro')
    count(0, foo='bar')
    count(1, formula='H2')
    count(1, formula='H2O')
    count(3, 'fmax<0.1')
    count(1, '0.5<mass<1.5')
    count(5, 'energy')

    id = con.reserve(abc=7)
    assert con[id].abc == 7

    for key in ['calculator', 'energy', 'abc', 'name', 'fmax']:
        count(6, sort=key)
        count(6, sort='-' + key)

    cli('ase -T gui --terminal {}@3'.format(name))

    con.delete([id])

    if name != 'testase.json':  # transfer between db formats
        if name == 'testase.db':
            from_db = 'testase.json'
            factor = 2
        else:
            from_db = 'testase.db'
            factor = 3

        cli('ase db {} --insert-into {}'.format(from_db, name))

        count(5 * factor)
        count(3 * factor, 'hydro')
        count(0, 'foo')
        count(3 * factor, abc=42)
        count(3 * factor, 'abc')
        count(0, 'abc,foo')
        count(3 * factor, 'abc,hydro')
        count(0, foo='bar')
        count(factor, formula='H2')
        count(factor, formula='H2O')
        count(3 * factor, 'fmax<0.1')
        count(factor, '0.5<mass<1.5')
        count(5 * factor, 'energy')

    t2 = time.time()

    print('----------------------------------')
    print('Finnished test for {}'.format(name))
    print('runtime = {} sec'.format(t2 - t1))
    print('----------------------------------')


print('Total runtime = {} sec'.format(t2 - t0))