-
Notifications
You must be signed in to change notification settings - Fork 839
/
materials.py
375 lines (326 loc) · 13.6 KB
/
materials.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
# coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.
"""
This module provides various representations of transformed structures. A
TransformedStructure is a structure that has been modified by undergoing a
series of transformations.
"""
import datetime
import json
import os
import re
from warnings import warn
from monty.json import MontyDecoder, MSONable, jsanitize
from pymatgen.core.structure import Structure
from pymatgen.io.cif import CifParser
from pymatgen.io.vasp.inputs import Poscar
from pymatgen.io.vasp.sets import MPRelaxSet
dec = MontyDecoder()
class TransformedStructure(MSONable):
"""
Container object for new structures that include history of
transformations.
Each transformed structure is made up of a sequence of structures with
associated transformation history.
"""
def __init__(self, structure, transformations=None, history=None, other_parameters=None):
"""
Initializes a transformed structure from a structure.
Args:
structure (Structure): Input structure
transformations ([Transformations]): List of transformations to
apply.
history (list): Previous history.
other_parameters (dict): Additional parameters to be added.
"""
self.final_structure = structure
self.history = history or []
self.other_parameters = other_parameters or {}
self._undone = []
transformations = transformations or []
for t in transformations:
self.append_transformation(t)
def undo_last_change(self):
"""
Undo the last change in the TransformedStructure.
Raises:
IndexError: If already at the oldest change.
"""
if len(self.history) == 0:
raise IndexError("Can't undo. Already at oldest change.")
if "input_structure" not in self.history[-1]:
raise IndexError("Can't undo. Latest history has no " "input_structure")
h = self.history.pop()
self._undone.append((h, self.final_structure))
s = h["input_structure"]
if isinstance(s, dict):
s = Structure.from_dict(s)
self.final_structure = s
def redo_next_change(self):
"""
Redo the last undone change in the TransformedStructure.
Raises:
IndexError: If already at the latest change.
"""
if len(self._undone) == 0:
raise IndexError("Can't redo. Already at latest change.")
h, s = self._undone.pop()
self.history.append(h)
self.final_structure = s
def __getattr__(self, name):
s = object.__getattribute__(self, "final_structure")
return getattr(s, name)
def __len__(self):
return len(self.history)
def append_transformation(self, transformation, return_alternatives=False, clear_redo=True):
"""
Appends a transformation to the TransformedStructure.
Args:
transformation: Transformation to append
return_alternatives: Whether to return alternative
TransformedStructures for one-to-many transformations.
return_alternatives can be a number, which stipulates the
total number of structures to return.
clear_redo: Boolean indicating whether to clear the redo list.
By default, this is True, meaning any appends clears the
history of undoing. However, when using append_transformation
to do a redo, the redo list should not be cleared to allow
multiple redos.
"""
if clear_redo:
self._undone = []
if return_alternatives and transformation.is_one_to_many:
ranked_list = transformation.apply_transformation(
self.final_structure, return_ranked_list=return_alternatives
)
input_structure = self.final_structure.as_dict()
alts = []
for x in ranked_list[1:]:
s = x.pop("structure")
actual_transformation = x.pop("transformation", transformation)
hdict = actual_transformation.as_dict()
hdict["input_structure"] = input_structure
hdict["output_parameters"] = x
self.final_structure = s
d = self.as_dict()
d["history"].append(hdict)
d["final_structure"] = s.as_dict()
alts.append(TransformedStructure.from_dict(d))
x = ranked_list[0]
s = x.pop("structure")
actual_transformation = x.pop("transformation", transformation)
hdict = actual_transformation.as_dict()
hdict["input_structure"] = self.final_structure.as_dict()
hdict["output_parameters"] = x
self.history.append(hdict)
self.final_structure = s
return alts
s = transformation.apply_transformation(self.final_structure)
hdict = transformation.as_dict()
hdict["input_structure"] = self.final_structure.as_dict()
hdict["output_parameters"] = {}
self.history.append(hdict)
self.final_structure = s
return None
def append_filter(self, structure_filter):
"""
Adds a filter.
Args:
structure_filter (StructureFilter): A filter implementating the
AbstractStructureFilter API. Tells transmuter waht structures
to retain.
"""
hdict = structure_filter.as_dict()
hdict["input_structure"] = self.final_structure.as_dict()
self.history.append(hdict)
def extend_transformations(self, transformations, return_alternatives=False):
"""
Extends a sequence of transformations to the TransformedStructure.
Args:
transformations: Sequence of Transformations
return_alternatives: Whether to return alternative
TransformedStructures for one-to-many transformations.
return_alternatives can be a number, which stipulates the
total number of structures to return.
"""
for t in transformations:
self.append_transformation(t, return_alternatives=return_alternatives)
def get_vasp_input(self, vasp_input_set=MPRelaxSet, **kwargs):
"""
Returns VASP input as a dict of vasp objects.
Args:
vasp_input_set (pymatgen.io.vaspio_set.VaspInputSet): input set
to create vasp input files from structures
"""
d = vasp_input_set(self.final_structure, **kwargs).get_vasp_input()
d["transformations.json"] = json.dumps(self.as_dict())
return d
def write_vasp_input(self, vasp_input_set=MPRelaxSet, output_dir=".", create_directory=True, **kwargs):
r"""
Writes VASP input to an output_dir.
Args:
vasp_input_set:
pymatgen.io.vaspio_set.VaspInputSet like object that creates
vasp input files from structures
output_dir: Directory to output files
create_directory: Create the directory if not present. Defaults to
True.
**kwargs: All keyword args supported by the VASP input set.
"""
vasp_input_set(self.final_structure, **kwargs).write_input(output_dir, make_dir_if_not_present=create_directory)
with open(os.path.join(output_dir, "transformations.json"), "w") as fp:
json.dump(self.as_dict(), fp)
def __str__(self):
output = [
"Current structure",
"------------",
str(self.final_structure),
"\nHistory",
"------------",
]
for h in self.history:
h.pop("input_structure", None)
output.append(str(h))
output.append("\nOther parameters")
output.append("------------")
output.append(str(self.other_parameters))
return "\n".join(output)
def set_parameter(self, key, value):
"""
Set a parameter
:param key: The string key
:param value: The value.
"""
self.other_parameters[key] = value
@property
def was_modified(self):
"""
Boolean describing whether the last transformation on the structure
made any alterations to it one example of when this would return false
is in the case of performing a substitution transformation on the
structure when the specie to replace isn't in the structure.
"""
return not self.final_structure == self.structures[-2]
@property
def structures(self):
"""
Copy of all structures in the TransformedStructure. A
structure is stored after every single transformation.
"""
hstructs = [Structure.from_dict(s["input_structure"]) for s in self.history if "input_structure" in s]
return hstructs + [self.final_structure]
@staticmethod
def from_cif_string(cif_string, transformations=None, primitive=True, occupancy_tolerance=1.0):
"""
Generates TransformedStructure from a cif string.
Args:
cif_string (str): Input cif string. Should contain only one
structure. For cifs containing multiple structures, please use
CifTransmuter.
transformations ([Transformations]): Sequence of transformations
to be applied to the input structure.
primitive (bool): Option to set if the primitive cell should be
extracted. Defaults to True. However, there are certain
instances where you might want to use a non-primitive cell,
e.g., if you are trying to generate all possible orderings of
partial removals or order a disordered structure.
occupancy_tolerance (float): If total occupancy of a site is
between 1 and occupancy_tolerance, the occupancies will be
scaled down to 1.
Returns:
TransformedStructure
"""
parser = CifParser.from_string(cif_string, occupancy_tolerance)
raw_string = re.sub(r"'", '"', cif_string)
cif_dict = parser.as_dict()
cif_keys = list(cif_dict.keys())
s = parser.get_structures(primitive)[0]
partial_cif = cif_dict[cif_keys[0]]
if "_database_code_ICSD" in partial_cif:
source = partial_cif["_database_code_ICSD"] + "-ICSD"
else:
source = "uploaded cif"
source_info = {
"source": source,
"datetime": str(datetime.datetime.now()),
"original_file": raw_string,
"cif_data": cif_dict[cif_keys[0]],
}
return TransformedStructure(s, transformations, history=[source_info])
@staticmethod
def from_poscar_string(poscar_string, transformations=None):
"""
Generates TransformedStructure from a poscar string.
Args:
poscar_string (str): Input POSCAR string.
transformations ([Transformations]): Sequence of transformations
to be applied to the input structure.
"""
p = Poscar.from_string(poscar_string)
if not p.true_names:
raise ValueError(
"Transformation can be craeted only from POSCAR " "strings with proper VASP5 element symbols."
)
raw_string = re.sub(r"'", '"', poscar_string)
s = p.structure
source_info = {
"source": "POSCAR",
"datetime": str(datetime.datetime.now()),
"original_file": raw_string,
}
return TransformedStructure(s, transformations, history=[source_info])
def as_dict(self):
"""
Dict representation of the TransformedStructure.
"""
d = self.final_structure.as_dict()
d["@module"] = self.__class__.__module__
d["@class"] = self.__class__.__name__
d["history"] = jsanitize(self.history)
d["last_modified"] = str(datetime.datetime.utcnow())
d["other_parameters"] = jsanitize(self.other_parameters)
return d
@classmethod
def from_dict(cls, d):
"""
Creates a TransformedStructure from a dict.
"""
s = Structure.from_dict(d)
return cls(s, history=d["history"], other_parameters=d.get("other_parameters", None))
def to_snl(self, authors, **kwargs):
"""
Generate SNL from TransformedStructure.
:param authors: List of authors
:param **kwargs: All kwargs supported by StructureNL.
:return: StructureNL
"""
if self.other_parameters:
warn("Data in TransformedStructure.other_parameters discarded " "during type conversion to SNL")
hist = []
for h in self.history:
snl_metadata = h.pop("_snl", {})
hist.append(
{
"name": snl_metadata.pop("name", "pymatgen"),
"url": snl_metadata.pop("url", "http://pypi.python.org/pypi/pymatgen"),
"description": h,
}
)
from pymatgen.util.provenance import StructureNL
return StructureNL(self.final_structure, authors, history=hist, **kwargs)
@classmethod
def from_snl(cls, snl):
"""
Create TransformedStructure from SNL.
Args:
snl (StructureNL): Starting snl
Returns:
TransformedStructure
"""
hist = []
for h in snl.history:
d = h.description
d["_snl"] = {"url": h.url, "name": h.name}
hist.append(d)
return cls(snl.structure, history=hist)