structure-optim.py 1.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
import os
import pickle
import json
import tqdm
import random
import ase
from ase.optimize import BFGS
from neurochem_calculator import calc, path

keep_ratio = 0.01  # reduce the size of generated file by discarding
mol_count = 0
with open(os.path.join(path, 'nist-dataset/result.json')) as f:
    pickle_objects = []
14
    for i in tqdm.tqdm(json.load(f), desc='Optim'):
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        if random.random() > keep_ratio:
            continue
        atoms = i['atoms']
        natoms = len(atoms)
        species = []
        coordinates = []
        for atype, x, y, z in atoms:
            species.append(atype)
            coordinates.append([x, y, z])
        mol = ase.Atoms(species, positions=coordinates)
        mol.set_calculator(calc())
        opt = BFGS(mol, logfile='/dev/null')
        opt.run()
        mol.set_calculator(None)
        pickle_objects.append(mol)
        mol_count += 1

    dumpfile = os.path.join(
        path, '../../tests/test_data/NeuroChemOptimized/all')
    with open(dumpfile, 'wb') as f:
        pickle.dump(pickle_objects, f)