-
Notifications
You must be signed in to change notification settings - Fork 19
/
spatulate_oqmd
executable file
·302 lines (292 loc) · 12.8 KB
/
spatulate_oqmd
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
# coding: utf-8
""" This file scrapes structures from the OQMD SQL dump.
You probably don't want to be using this unless you have
MySQL set up exactly as I do.
Matthew Evans 2016
TO-DO
* fix pspots
* find k-points
"""
# matador modules
from matador.utils.cell_utils import cart2abc
# external libraries
import MySQLdb
import MySQLdb.cursors
import pymongo as pm
# standard library
from collections import defaultdict
from random import randint
from os.path import dirname, realpath
import argparse
import re
from traceback import print_exc
from ast import literal_eval
from sys import exit
class OQMDConverter:
""" The OQMDConverter class implements methods to scrape
a MySQL table of OQMD structures which can be found at:
http://oqmd.org/static/docs/getting_started.html.
"""
def __init__(self, dryrun=False, debug=False, verbosity=0, scratch=False, converged=False, label=None):
""" Connect to the relevant databases and
set off the scraper.
"""
self.import_count = 0
self.dryrun = dryrun
self.debug = debug
self.verbosity = verbosity
self.scratch = scratch
self.converged = converged
self.label = label
# set up I/O for text_id
if not self.dryrun:
try:
wordfile = open(dirname(realpath(__file__)) + '/scrapers/words', 'r')
nounfile = open(dirname(realpath(__file__)) + '/scrapers/nouns', 'r')
self.wlines = wordfile.readlines()
self.num_words = len(self.wlines)
self.nlines = nounfile.readlines()
self.num_nouns = len(self.nlines)
wordfile.close()
nounfile.close()
except Exception as oops:
exit(oops)
# connect to SQL database as root, with "use oqmd"
print('Connecting to OQMD SQL...')
self.oqmd = MySQLdb.connect(host='127.0.0.1',
user='root',
port=3306,
cursorclass=MySQLdb.cursors.DictCursor,
db='oqmd')
print('Successfully connected.')
# if not dryrunning, connect to OQMD or scratch MongoDB
if not self.dryrun:
self.client = pm.MongoClient()
self.db = self.client.crystals
if self.scratch:
self.repo = self.db.scratch
else:
self.repo = self.db.oqmd
# scrape structures from SQL database
self.sql2db()
if not self.dryrun:
# index new data by enthalpy
print('Indexing...')
self.repo.create_index([('enthalpy_per_atom', pm.ASCENDING)])
def sql2db(self):
""" Perform SQL query to scrape, and hop around the tables
to collect all data associated with one structure.
"""
# start by scraping all converged structures with chosen label
cursor = self.oqmd.cursor()
if self.converged:
cursor.execute("select count(label) from calculations where label in ('" +
self.label + "') and converged in ('1')")
else:
cursor.execute("select count(label) from calculations where label in ('" +
self.label + "')")
count = cursor.fetchone()['count(label)']
if count == 0:
exit('No structures found with that label.')
if self.converged:
print(count, 'converged structures found.')
else:
print(count, 'structures found.')
# select only converged structures
if self.converged:
cursor.execute("select * from calculations where label in ('" +
self.label + "') and converged in ('1')")
else:
cursor.execute("select * from calculations where label in ('" +
self.label + "')")
success_count = 0
for row in cursor:
calc_doc = row
if calc_doc is None:
continue
calculation_dict, success = self.oqmd_calculation2dict(calc_doc)
if not success:
continue
entry_id = calculation_dict['entry_id']
sql_query = "select * from structures where label in ('" + \
self.label + "') and entry_id in ('" + str(entry_id) + "')"
cursor.execute(sql_query)
# should only be one matching; revise at later date
struct_doc = cursor.fetchone()
structure_dict, success = self.oqmd_structure2dict(struct_doc)
if not success:
continue
structure_id = structure_dict['structure_id']
# grab spacegroup symbol from ID
spacegroup_id = structure_dict['spacegroup_id']
sql_query = "select * from spacegroups where number in \
('" + str(spacegroup_id) + "')"
cursor.execute(sql_query)
try:
space_group = cursor.fetchone()
structure_dict['space_group'] = space_group['hm']
except:
structure_dict['space_group'] = 'xxx'
if self.debug:
print('Failed to get space group.')
# get top level entry to scrape ICSD
sql_query = "select * from entries where id in ('" + str(entry_id) + "')"
cursor.execute(sql_query)
entry_doc = cursor.fetchone()
try:
if entry_doc['label'] is not None:
if 'icsd' in entry_doc['label']:
structure_dict['icsd'] = entry_doc['label'].split('-')[-1]
except:
if self.debug:
print_exc()
print('Failed to get ICSD CollCode.')
# if formation energy is positive, skip structure
sql_query = "select * from atoms where structure_id in \
('" + str(structure_id) + "')"
cursor.execute(sql_query)
atom_docs = cursor.fetchall()
if len(atom_docs) != structure_dict['num_atoms']:
print('Incorrect number of atoms! Skipping!')
continue
atoms_dict, success = self.oqmd_atoms2dict(atom_docs)
if not success:
continue
final_struct = calculation_dict.copy()
final_struct.update(structure_dict)
final_struct.update(atoms_dict)
final_struct['source'] = ['OQMD ' + str(final_struct['entry_id'])]
success_count += 1
if not self.dryrun:
self.import_count += self.oqmd_struct2db(final_struct)
if self.dryrun:
print('Successfully scraped', success_count, '/',
count, 'structures.')
if not self.dryrun:
print('Successfully imported', self.import_count, '/',
count, 'structures.')
return
def oqmd_struct2db(self, struct):
""" Insert completed Python dictionary into chosen
database, with generated text_id.
"""
plain_text_id = [self.wlines[randint(0, self.num_words-1)].strip(),
self.nlines[randint(0, self.num_nouns-1)].strip()]
struct['text_id'] = plain_text_id
struct_id = self.repo.insert_one(struct).inserted_id
if self.debug:
print('Inserted', struct_id)
return 1
def oqmd_calculation2dict(self, doc):
""" Take a calculation from oqmd.calculations and
scrape its settings, returning the ID to its structure
if possible.
"""
try:
calculation = defaultdict(list)
# try to get output ID first
calculation['entry_id'] = doc['entry_id']
calculation['input_id'] = doc['input_id']
# grab energies and pretend they are enthalpies
calculation['enthalpy'] = float(doc['energy'])
calculation['enthalpy_per_atom'] = float(doc['energy_pa'])
# grab settings
settings = literal_eval(doc['settings'])
calculation['cut_off_energy'] = settings['encut']
calculation['xc_functional'] = settings['potentials'][0]['xc'].upper()
calculation['elec_energy_tol'] = settings['ediff']
if settings['ispin'] == 2:
calculation['spin_polarized'] = True
# calculation['species_pot'] = settings['potentials']
except Exception:
if self.debug:
print('Scraping calculation failed.')
print_exc()
print(80*'─')
return dict(), False
return calculation, True
def oqmd_structure2dict(self, doc):
""" Create a dict containing structural information
from oqmd.structures.
"""
try:
structure = defaultdict(list)
structure['structure_id'] = doc['id']
structure['spacegroup_id'] = doc['spacegroup_id']
structure['num_atoms'] = doc['natoms']
# convert stoich from string to list to tuple
temp_stoich_list = [elem for elem in
re.split(r'([A-Z][a-z]*)',
doc['composition_id']) if elem]
for ind, item in enumerate(temp_stoich_list):
if ind % 2 == 0:
structure['stoichiometry'].append([str(temp_stoich_list[ind]),
int(temp_stoich_list[ind+1])])
atoms_per_fu = 0
for elem in structure['stoichiometry']:
atoms_per_fu += elem[1]
structure['num_fu'] = structure['num_atoms'] / atoms_per_fu
# get pressure from -1/3 Tr(stress), then convert to GPa
structure['pressure'] = 0.1*-(doc['sxx'] + doc['syy'] + doc['szz'])/3.0
structure['lattice_cart'].append([doc['x1'], doc['x2'], doc['x3']])
structure['lattice_cart'].append([doc['y1'], doc['y2'], doc['y3']])
structure['lattice_cart'].append([doc['z1'], doc['z2'], doc['z3']])
structure['lattice_abc'] = cart2abc(structure['lattice_cart'])
structure['cell_volume'] = doc['volume']
except Exception:
if self.debug:
print('Scraping structure failed.')
print_exc()
print(80*'─')
return dict(), False
return structure, True
def oqmd_atoms2dict(self, atom_docs):
""" Take list of atom documents matching the
structure_id and scrape them into a dict.
"""
try:
atoms = defaultdict(list)
max_force = 0
for doc in atom_docs:
atoms['atom_types'].append(doc['element_id'])
atoms['positions_frac'].append([doc['x'], doc['y'], doc['z']])
force_tmp = doc['fx']**2 + doc['fy']**2 + doc['fz']**2
if force_tmp > max_force:
max_force = force_tmp
atoms['spins'].append(doc['magmom'])
atoms['charges'].append(doc['charge'])
atoms['max_force_on_atom'] = max_force
except Exception:
if self.debug:
print('Scraping atoms failed.')
print_exc()
print(80*'─')
return dict(), False
return atoms, True
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Import OQMD (http://oqmd.org) structures into MongoDB database.',
epilog='Written by Matthew Evans (2016)')
parser.add_argument('-d', '--dryrun', action='store_true',
help='run the importer without connecting to the database')
parser.add_argument('-v', '--verbosity', action='count',
help='enable verbose output')
parser.add_argument('-l', '--label', type=str,
help='choose which OQMD calculation label to query, best options \
are fine_relax or relaxation.')
parser.add_argument('-conv', action='store_true',
help='allow only converged geom opts.')
parser.add_argument('--debug', action='store_true',
help='enable debug output to print every dict')
parser.add_argument('-s', '--scratch', action='store_true',
help='import to junk collection called scratch')
args = parser.parse_args()
if args.label is None:
exit('Please choose a label to scrape.')
importer = OQMDConverter(dryrun=args.dryrun,
debug=args.debug,
verbosity=args.verbosity,
scratch=args.scratch,
converged=args.conv,
label=args.label)