-
Notifications
You must be signed in to change notification settings - Fork 62
/
utils.py
413 lines (341 loc) · 13.4 KB
/
utils.py
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import os
import stat
import mgzip
import click
import shutil
import logging
import itertools
import multiprocessing
from enum import Enum
from glob import glob
from fnmatch import fnmatch
from datetime import datetime
from collections import defaultdict
from pymatgen import Structure
from atomate.vasp.database import VaspCalcDb
from fireworks.fw_config import FW_BLOCK_FORMAT
from mongogrant.client import Client
from atomate.vasp.drones import VaspDrone
from pymongo.errors import DocumentTooLarge
from emmet.core.utils import group_structures
from emmet.cli import SETTINGS
logger = logging.getLogger("emmet")
perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP
class EmmetCliError(Exception):
pass
class ReturnCodes(Enum):
"""codes to print command exit message in github issue comments"""
SUCCESS = "COMPLETED"
ERROR = "encountered ERROR"
WARNING = "exited with WARNING"
SUBMITTED = "submitted to SLURM"
def structures_match(s1, s2):
return bool(len(list(group_structures([s1, s2]))) == 1)
def ensure_indexes(indexes, colls):
created = defaultdict(list)
for index in indexes:
for coll in colls:
keys = [k.rsplit("_", 1)[0] for k in coll.index_information().keys()]
if index not in keys:
coll.ensure_index(index)
created[coll.full_name].append(index)
if created:
indexes = ", ".join(created[coll.full_name])
logger.debug(f"Created the following index(es) on {coll.full_name}:\n{indexes}")
def calcdb_from_mgrant(spec_or_dbfile):
if os.path.exists(spec_or_dbfile):
return VaspCalcDb.from_db_file(spec_or_dbfile)
client = Client()
role = "rw" # NOTE need write access to source to ensure indexes
host, dbname_or_alias = spec_or_dbfile.split("/", 1)
auth = client.get_auth(host, dbname_or_alias, role)
if auth is None:
raise Exception("No valid auth credentials available!")
return VaspCalcDb(
auth["host"],
27017,
auth["db"],
"tasks",
auth["username"],
auth["password"],
authSource=auth["db"],
)
def get_meta_from_structure(struct):
d = {"formula_pretty": struct.composition.reduced_formula}
d["nelements"] = len(set(struct.composition.elements))
d["nsites"] = len(struct)
d["is_ordered"] = struct.is_ordered
d["is_valid"] = struct.is_valid()
return d
def aggregate_by_formula(coll, q, key=None):
query = {"$and": [q, SETTINGS.exclude]}
query.update(SETTINGS.base_query)
nested = False
if key is None:
for k in SETTINGS.aggregation_keys:
q = {k: {"$exists": 1}}
q.update(SETTINGS.base_query)
doc = coll.find_one(q)
if doc:
key = k
nested = int("snl" in doc)
break
else:
raise ValueError(
f"could not find one of the aggregation keys {SETTINGS.aggregation_keys} in {coll.full_name}!"
)
push = {k.split(".")[-1]: f"${k}" for k in structure_keys[nested]}
return coll.aggregate(
[
{"$match": query},
{"$sort": {"nelements": 1, "nsites": 1}},
{"$group": {"_id": f"${key}", "structures": {"$push": push}}},
],
allowDiskUse=True,
batchSize=1,
)
def load_structure(dct):
s = Structure.from_dict(dct)
s.remove_oxidation_states()
return s.get_primitive_structure()
# a utility function to get us a slice of an iterator, as an iterator
# when working with iterators maximum lazyness is preferred
def iterator_slice(iterator, length):
iterator = iter(iterator)
while True:
res = tuple(itertools.islice(iterator, length))
if not res:
break
yield res
def chunks(lst, n):
return [lst[i : i + n] for i in range(0, len(lst), n)]
def get_subdir(dn):
return dn.rstrip(os.sep).rsplit(os.sep, 1)[-1]
def get_timestamp_dir(prefix="launcher"):
time_now = datetime.utcnow().strftime(FW_BLOCK_FORMAT)
return "_".join([prefix, time_now])
def is_vasp_dir(list_of_files):
for f in list_of_files:
if f.startswith("INCAR"):
return True
def make_block(base_path):
ctx = click.get_current_context()
run = ctx.parent.parent.params["run"]
block = get_timestamp_dir(prefix="block")
block_dir = os.path.join(base_path, block)
if run:
os.mkdir(block_dir)
return block_dir
def get_symlinked_path(root, base_path_index):
"""organize directory in block_*/launcher_* via symbolic links"""
ctx = click.get_current_context()
run = ctx.parent.parent.params["run"]
root_split = root.split(os.sep)
base_path = os.sep.join(root_split[:base_path_index])
if root_split[base_path_index].startswith("block_"):
block_dir = os.sep.join(root_split[: base_path_index + 1])
else:
all_blocks = glob(os.path.join(base_path, "block_*/"))
for block_dir in all_blocks:
p = os.path.join(block_dir, "launcher_*/")
if len(glob(p)) < 300:
break
else:
# didn't find a block with < 300 launchers
block_dir = make_block(base_path)
if root_split[-1].startswith("launcher_"):
launch_dir = os.path.join(block_dir, root_split[-1])
if not os.path.exists(launch_dir):
if run:
os.rename(root, launch_dir)
logger.debug(f"{root} -> {launch_dir}")
else:
launch = get_timestamp_dir(prefix="launcher")
launch_dir = os.path.join(block_dir, launch)
if run:
os.rename(root, launch_dir)
os.symlink(launch_dir, root)
logger.debug(f"{root} -> {launch_dir}")
return launch_dir
def create_orig_inputs(vaspdir):
ctx = click.get_current_context()
run = ctx.parent.parent.params["run"]
for inp in ["INCAR", "KPOINTS", "POTCAR", "POSCAR"]:
input_path = os.path.join(vaspdir, inp)
if not glob(input_path + ".orig*"):
matches = glob(input_path + "*")
if matches:
input_path = matches[0]
orig_path = input_path.replace(inp, inp + ".orig")
if run:
shutil.copyfile(input_path, orig_path)
logger.debug(f"{input_path} -> {orig_path}")
# https://stackoverflow.com/a/34073559
class VaspDirsGenerator:
def __init__(self):
self.gen = get_vasp_dirs()
def __iter__(self):
self.value = yield from self.gen
def get_vasp_dirs():
ctx = click.get_current_context()
run = ctx.parent.parent.params["run"]
nmax = ctx.parent.params["nmax"]
pattern = ctx.parent.params["pattern"]
base_path = ctx.parent.params["directory"].rstrip(os.sep)
base_path_index = len(base_path.split(os.sep))
if pattern:
pattern_split = pattern.split(os.sep)
pattern_split_len = len(pattern_split)
counter = 0
for root, dirs, files in os.walk(base_path, topdown=True):
if counter == nmax:
break
level = len(root.split(os.sep)) - base_path_index
if pattern and dirs and pattern_split_len > level:
p = pattern_split[level]
dirs[:] = [d for d in dirs if fnmatch(d, p)]
for d in dirs:
dn = os.path.join(root, d)
st = os.stat(dn)
if not bool(st.st_mode & perms):
raise EmmetCliError(f"Insufficient permissions {st.st_mode} for {dn}.")
if is_vasp_dir(files):
gzipped = False
for f in files:
fn = os.path.join(root, f)
if os.path.islink(fn):
if run:
os.unlink(fn)
logger.warning(f"Unlinked {fn}.")
else:
logger.warning(f"Would unlink {fn}.")
continue
st = os.stat(fn)
if not bool(st.st_mode & perms):
raise EmmetCliError(
f"Insufficient permissions {st.st_mode} for {fn}."
)
if run and not f.endswith(".gz"):
fn_gz = fn + ".gz"
if os.path.exists(fn_gz):
os.remove(fn_gz) # remove left-over gz (cancelled job)
with open(fn, "rb") as fo, mgzip.open(fn_gz, "wb", thread=0) as fw:
fw.write(fo.read())
os.remove(fn) # remove original
shutil.chown(fn_gz, group="matgen")
gzipped = True
# NOTE skip symlink'ing on MP calculations from the early days
vasp_dir = get_symlinked_path(root, base_path_index)
# vasp_dir = root
create_orig_inputs(vasp_dir)
dirs[:] = [] # don't descend further (i.e. ignore relax1/2)
logger.log(logging.INFO if gzipped else logging.DEBUG, vasp_dir)
yield vasp_dir
counter += 1
return counter
def reconstruct_command(sbatch=False):
ctx = click.get_current_context()
command = []
for level, (cmd, params) in enumerate(
zip(
ctx.command_path.split(),
[ctx.grand_parent.params, ctx.parent.params, ctx.params],
)
):
command.append(cmd)
if level:
command.append("\\\n")
for k, v in params.items():
k = k.replace("_", "-")
if v:
if isinstance(v, bool):
if (sbatch and k != "sbatch" and k != "bb") or not sbatch:
command.append(f"--{k}")
elif isinstance(v, str):
command.append(f'--{k}="{v}"')
elif isinstance(v, tuple) or isinstance(v, list):
for x in v:
command.append(f'--{k}="{x}"')
command.append("\\\n")
else:
command.append(f"--{k}={v}")
if level:
command.append("\\\n")
return " ".join(command).strip().strip("\\")
def parse_vasp_dirs(vaspdirs, tag, task_ids):
process = multiprocessing.current_process()
name = process.name
chunk_idx = int(name.rsplit("-")[1]) - 1
logger.info(f"{name} starting.")
tags = [tag, SETTINGS.year_tags[-1]]
ctx = click.get_current_context()
spec_or_dbfile = ctx.parent.parent.params["spec_or_dbfile"]
target = calcdb_from_mgrant(spec_or_dbfile)
sbxn = list(filter(None, target.collection.distinct("sbxn")))
logger.info(f"Using sandboxes {sbxn}.")
no_dupe_check = ctx.parent.parent.params["no_dupe_check"]
run = ctx.parent.parent.params["run"]
projection = {"tags": 1, "task_id": 1}
count = 0
kwargs = {}
for p in ["chgcar", "aeccar"]:
par = f"parse_{p}"
kwargs[par] = ctx.params[par]
drone = VaspDrone(additional_fields={"tags": tags}, **kwargs)
for vaspdir in vaspdirs:
logger.info(f"{name} VaspDir: {vaspdir}")
launcher = get_subdir(vaspdir)
query = {"dir_name": {"$regex": launcher}}
docs = list(
target.collection.find(query, projection).sort([("_id", -1)]).limit(1)
)
if docs:
if no_dupe_check:
logger.warning(f"FORCING re-parse of {launcher}!")
else:
if run:
shutil.rmtree(vaspdir)
logger.warning(f"{name} {launcher} already parsed -> removed.")
else:
logger.warning(f"{name} {launcher} already parsed -> would remove.")
continue
try:
task_doc = drone.assimilate(vaspdir)
except Exception as ex:
logger.error(f"Failed to assimilate {vaspdir}: {ex}")
continue
task_doc["sbxn"] = sbxn
manual_taskid = isinstance(task_ids, dict)
task_id = task_ids[launcher] if manual_taskid else task_ids[chunk_idx][count]
task_doc["task_id"] = task_id
logger.info(f"Using {task_id} for {launcher}.")
if docs:
# make sure that task gets the same tags as the previously parsed task
if docs[0]["tags"]:
task_doc["tags"] += docs[0]["tags"]
logger.info(f"Adding existing tags {docs[0]['tags']} to {tags}.")
if run:
if task_doc["state"] == "successful":
if docs and no_dupe_check:
target.collection.remove({"task_id": task_id})
logger.warning(f"Removed previously parsed task {task_id}!")
try:
target.insert_task(task_doc, use_gridfs=True)
except DocumentTooLarge:
logger.warning(f"{name} Remove normalmode_eigenvecs and retry ...")
task_doc["calcs_reversed"][0]["output"].pop("normalmode_eigenvecs")
try:
target.insert_task(task_doc, use_gridfs=True)
except DocumentTooLarge:
logger.warning(
f"{name} Also remove force_constants and retry ..."
)
task_doc["calcs_reversed"][0]["output"].pop("force_constants")
target.insert_task(task_doc, use_gridfs=True)
if target.collection.count(query):
shutil.rmtree(vaspdir)
logger.info(f"{name} Successfully parsed and removed {launcher}.")
count += 1
else:
count += 1
return count