-
Notifications
You must be signed in to change notification settings - Fork 835
/
advanced_transformations.py
2136 lines (1836 loc) · 91.8 KB
/
advanced_transformations.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""This module implements more advanced transformations."""
from __future__ import annotations
import logging
import math
import warnings
from fractions import Fraction
from itertools import groupby, product
from math import gcd
from string import ascii_lowercase
from typing import TYPE_CHECKING
import numpy as np
from joblib import Parallel, delayed
from monty.dev import requires
from monty.fractions import lcm
from monty.json import MSONable
from pymatgen.analysis.adsorption import AdsorbateSiteFinder
from pymatgen.analysis.bond_valence import BVAnalyzer
from pymatgen.analysis.energy_models import SymmetryModel
from pymatgen.analysis.ewald import EwaldSummation
from pymatgen.analysis.gb.grain import GrainBoundaryGenerator
from pymatgen.analysis.local_env import MinimumDistanceNN
from pymatgen.analysis.structure_matcher import SpinComparator, StructureMatcher
from pymatgen.analysis.structure_prediction.substitution_probability import SubstitutionPredictor
from pymatgen.command_line.enumlib_caller import EnumError, EnumlibAdaptor
from pymatgen.command_line.mcsqs_caller import run_mcsqs
from pymatgen.core import DummySpecies, Element, Species, Structure, get_el_sp
from pymatgen.core.surface import SlabGenerator
from pymatgen.electronic_structure.core import Spin
from pymatgen.io.ase import AseAtomsAdaptor
from pymatgen.io.icet import IcetSQS
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.transformations.standard_transformations import (
OrderDisorderedStructureTransformation,
SubstitutionTransformation,
SupercellTransformation,
)
from pymatgen.transformations.transformation_abc import AbstractTransformation
try:
import hiphive
except ImportError:
hiphive = None
if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
from typing import Any, Callable, Literal
__author__ = "Shyue Ping Ong, Stephen Dacek, Anubhav Jain, Matthew Horton, Alex Ganose"
logger = logging.getLogger(__name__)
class ChargeBalanceTransformation(AbstractTransformation):
"""This is a transformation that disorders a structure to make it charge
balanced, given an oxidation state-decorated structure.
"""
def __init__(self, charge_balance_sp):
"""
Args:
charge_balance_sp: specie to add or remove. Currently only removal
is supported.
"""
self.charge_balance_sp = str(charge_balance_sp)
def apply_transformation(self, structure: Structure):
"""Apply the transformation.
Args:
structure: Input Structure
Returns:
Charge balanced structure.
"""
charge = structure.charge
specie = get_el_sp(self.charge_balance_sp)
num_to_remove = (charge / specie.oxi_state) if specie.oxi_state else 0.0
num_in_structure = structure.composition[specie]
removal_fraction = num_to_remove / num_in_structure
if removal_fraction < 0:
raise ValueError("addition of specie not yet supported by ChargeBalanceTransformation")
trans = SubstitutionTransformation({self.charge_balance_sp: {self.charge_balance_sp: 1 - removal_fraction}})
return trans.apply_transformation(structure)
def __repr__(self):
return f"Charge Balance Transformation : Species to remove = {self.charge_balance_sp}"
class SuperTransformation(AbstractTransformation):
"""This is a transformation that is inherently one-to-many. It is constructed
from a list of transformations and returns one structure for each
transformation. The primary use for this class is extending a transmuter
object.
"""
def __init__(self, transformations, nstructures_per_trans=1):
"""
Args:
transformations ([transformations]): List of transformations to apply
to a structure. One transformation is applied to each output
structure.
nstructures_per_trans (int): If the transformations are one-to-many and,
nstructures_per_trans structures from each transformation are
added to the full list. Defaults to 1, i.e., only best structure.
"""
self._transformations = transformations
self.nstructures_per_trans = nstructures_per_trans
def apply_transformation(self, structure: Structure, return_ranked_list: bool | int = False):
"""Apply the transformation.
Args:
structure: Input Structure
return_ranked_list (bool | int, optional): If return_ranked_list is int, that number of structures
is returned. If False, only the single lowest energy structure is returned. Defaults to False.
Returns:
Structures with all transformations applied.
"""
if not return_ranked_list:
raise ValueError("SuperTransformation has no single best structure output. Must use return_ranked_list")
structures = []
for t in self._transformations:
if t.is_one_to_many:
for d in t.apply_transformation(structure, return_ranked_list=self.nstructures_per_trans):
d["transformation"] = t
structures.append(d)
else:
structures.append({"transformation": t, "structure": t.apply_transformation(structure)})
return structures
def __repr__(self):
return f"Super Transformation : Transformations = {' '.join(map(str, self._transformations))}"
@property
def is_one_to_many(self) -> bool:
"""Transform one structure to many."""
return True
class MultipleSubstitutionTransformation:
"""Perform multiple substitutions on a structure. For example, can do a
fractional replacement of Ge in LiGePS with a list of species, creating one
structure for each substitution. Ordering is done using a dummy element so
only one ordering must be done per substitution oxidation state. Charge
balancing of the structure is optionally performed.
Note:
There are no checks to make sure that removal fractions are possible and rounding
may occur. Currently charge balancing only works for removal of species.
"""
def __init__(
self,
sp_to_replace,
r_fraction,
substitution_dict,
charge_balance_species=None,
order=True,
):
"""Perform multiple fractional substitutions on a transmuter.
Args:
sp_to_replace: species to be replaced
r_fraction: fraction of that specie to replace
substitution_dict: dictionary of the format
{2: ["Mg", "Ti", "V", "As", "Cr", "Ta", "N", "Nb"],
3: ["Ru", "Fe", "Co", "Ce", "As", "Cr", "Ta", "N", "Nb"],
4: ["Ru", "V", "Cr", "Ta", "N", "Nb"],
5: ["Ru", "W", "Mn"]
}
The number is the charge used for each of the list of elements
(an element can be present in multiple lists)
charge_balance_species: If specified, will balance the charge on
the structure using that specie.
order: Whether to order the structures.
"""
self.sp_to_replace = sp_to_replace
self.r_fraction = r_fraction
self.substitution_dict = substitution_dict
self.charge_balance_species = charge_balance_species
self.order = order
def apply_transformation(self, structure: Structure, return_ranked_list: bool | int = False):
"""Apply the transformation.
Args:
structure: Input Structure
return_ranked_list (bool | int, optional): If return_ranked_list is int, that number of structures
is returned. If False, only the single lowest energy structure is returned. Defaults to False.
Returns:
Structures with all substitutions applied.
"""
if not return_ranked_list:
raise ValueError(
"MultipleSubstitutionTransformation has no single"
" best structure output. Must use return_ranked_list."
)
outputs = []
for charge, el_list in self.substitution_dict.items():
sign = "+" if charge > 0 else "-"
dummy_sp = f"X{charge}{sign}"
mapping = {
self.sp_to_replace: {
self.sp_to_replace: 1 - self.r_fraction,
dummy_sp: self.r_fraction,
}
}
trans = SubstitutionTransformation(mapping) # type: ignore[arg-type]
dummy_structure = trans.apply_transformation(structure)
if self.charge_balance_species is not None:
cbt = ChargeBalanceTransformation(self.charge_balance_species)
dummy_structure = cbt.apply_transformation(dummy_structure)
if self.order:
trans = OrderDisorderedStructureTransformation()
dummy_structure = trans.apply_transformation(dummy_structure)
for el in el_list:
sign = "+" if charge > 0 else "-"
st = SubstitutionTransformation({f"X{charge}+": f"{el}{charge}{sign}"})
new_structure = st.apply_transformation(dummy_structure)
outputs.append({"structure": new_structure})
return outputs
def __repr__(self):
return f"Multiple Substitution Transformation : Substitution on {self.sp_to_replace}"
@property
def is_one_to_many(self) -> bool:
"""Transform one structure to many."""
return True
class EnumerateStructureTransformation(AbstractTransformation):
"""Order a disordered structure using enumlib. For complete orderings, this
generally produces fewer structures that the OrderDisorderedStructure
transformation, and at a much faster speed.
"""
def __init__(
self,
min_cell_size: int = 1,
max_cell_size: int = 1,
symm_prec: float = 0.1,
refine_structure: bool = False,
enum_precision_parameter: float = 0.001,
check_ordered_symmetry: bool = True,
max_disordered_sites: int | None = None,
sort_criteria: str | Callable = "ewald",
timeout: float | None = None,
n_jobs: int = -1,
):
"""
Args:
min_cell_size:
The minimum cell size wanted. Must be an int. Defaults to 1.
max_cell_size:
The maximum cell size wanted. Must be an int. Defaults to 1.
symm_prec:
Tolerance to use for symmetry.
refine_structure:
This parameter has the same meaning as in enumlib_caller.
If you are starting from a structure that has been relaxed via
some electronic structure code, it is usually much better to
start with symmetry determination and then obtain a refined
structure. The refined structure have cell parameters and
atomic positions shifted to the expected symmetry positions,
which makes it much less sensitive precision issues in enumlib.
If you are already starting from an experimental cif, refinement
should have already been done and it is not necessary. Defaults
to False.
enum_precision_parameter (float): Finite precision parameter for
enumlib. Default of 0.001 is usually ok, but you might need to
tweak it for certain cells.
check_ordered_symmetry (bool): Whether to check the symmetry of
the ordered sites. If the symmetry of the ordered sites is
lower, the lowest symmetry ordered sites is included in the
enumeration. This is important if the ordered sites break
symmetry in a way that is important getting possible
structures. But sometimes including ordered sites
slows down enumeration to the point that it cannot be
completed. Switch to False in those cases. Defaults to True.
max_disordered_sites (int):
An alternate parameter to max_cell size. Will sequentially try
larger and larger cell sizes until (i) getting a result or (ii)
the number of disordered sites in the cell exceeds
max_disordered_sites. Must set max_cell_size to None when using
this parameter.
sort_criteria (str or callable): Sort by Ewald energy ("ewald", must have oxidation states and slow) or
M3GNet relaxed energy ("m3gnet_relax", which is the most accurate but most expensive and provides
pre-relaxed structures - needs m3gnet package installed) or by M3GNet static energy ("m3gnet_static")
or by number of sites ("nsites", the fastest, the default). The expense of m3gnet_relax or m3gnet_static
can be worth it if it significantly reduces the number of structures to be considered. m3gnet_relax
speeds up the subsequent DFT calculations. Alternatively, a callable can be supplied that returns a
(Structure, energy) tuple.
timeout (float): timeout in minutes to pass to EnumlibAdaptor.
n_jobs (int): Number of parallel jobs used to compute energy criteria. This is used only when the Ewald
or m3gnet or callable sort_criteria is used. Default is -1, which uses all available CPUs.
"""
self.symm_prec = symm_prec
self.min_cell_size = min_cell_size
self.max_cell_size = max_cell_size
self.refine_structure = refine_structure
self.enum_precision_parameter = enum_precision_parameter
self.check_ordered_symmetry = check_ordered_symmetry
self.max_disordered_sites = max_disordered_sites
self.sort_criteria = sort_criteria
self.timeout = timeout
self.n_jobs = n_jobs
if max_cell_size and max_disordered_sites:
raise ValueError("Cannot set both max_cell_size and max_disordered_sites!")
def apply_transformation(
self, structure: Structure, return_ranked_list: bool | int = False
) -> Structure | list[dict]:
"""Get either a single ordered structure or a sequence of all ordered
structures.
Args:
structure: Structure to order.
return_ranked_list (bool | int, optional): If return_ranked_list is int, that number of structures
is returned. If False, only the single lowest energy structure is returned. Defaults to False.
Returns:
Depending on returned_ranked list, either a transformed structure
or a list of dictionaries, where each dictionary is of the form
{"structure" = .... , "other_arguments"}
The list of ordered structures is ranked by Ewald energy / atom, if
the input structure is an oxidation state decorated structure.
Otherwise, it is ranked by number of sites, with smallest number of
sites first.
"""
try:
num_to_return = int(return_ranked_list)
except ValueError:
num_to_return = 1
if self.refine_structure:
finder = SpacegroupAnalyzer(structure, self.symm_prec)
structure = finder.get_refined_structure()
contains_oxidation_state = all(hasattr(sp, "oxi_state") and sp.oxi_state != 0 for sp in structure.elements)
structures = None
if structure.is_ordered:
warnings.warn(
f"Enumeration skipped for structure with composition {structure.composition} because it is ordered"
)
structures = [structure.copy()]
if self.max_disordered_sites:
n_disordered = sum(1 for site in structure if not site.is_ordered)
if n_disordered > self.max_disordered_sites:
raise ValueError(f"Too many disordered sites! ({n_disordered} > {self.max_disordered_sites})")
max_cell_sizes: Iterable[int] = range(
self.min_cell_size,
math.floor(self.max_disordered_sites / n_disordered) + 1,
)
else:
max_cell_sizes = [self.max_cell_size]
for max_cell_size in max_cell_sizes:
adaptor = EnumlibAdaptor(
structure,
min_cell_size=self.min_cell_size,
max_cell_size=max_cell_size,
symm_prec=self.symm_prec,
refine_structure=False,
enum_precision_parameter=self.enum_precision_parameter,
check_ordered_symmetry=self.check_ordered_symmetry,
timeout=self.timeout,
)
try:
adaptor.run()
structures = adaptor.structures
if structures:
break
except EnumError:
warnings.warn(f"Unable to enumerate for {max_cell_size = }")
if structures is None:
raise ValueError("Unable to enumerate")
original_latt = structure.lattice
inv_latt = np.linalg.inv(original_latt.matrix)
ewald_matrices = {}
m3gnet_model = None
if not callable(self.sort_criteria) and self.sort_criteria.startswith("m3gnet"):
import matgl
from matgl.ext.ase import M3GNetCalculator, Relaxer
if self.sort_criteria == "m3gnet_relax":
potential = matgl.load_model("M3GNet-MP-2021.2.8-PES")
m3gnet_model = Relaxer(potential=potential)
elif self.sort_criteria == "m3gnet":
potential = matgl.load_model("M3GNet-MP-2021.2.8-PES")
m3gnet_model = M3GNetCalculator(potential=potential, stress_weight=0.01)
def _get_stats(struct):
if callable(self.sort_criteria):
struct, energy = self.sort_criteria(struct)
return {
"num_sites": len(struct),
"energy": energy,
"structure": struct,
}
if contains_oxidation_state and self.sort_criteria == "ewald":
new_latt = struct.lattice
transformation = np.dot(new_latt.matrix, inv_latt)
transformation = tuple(tuple(int(round(cell)) for cell in row) for row in transformation)
if transformation not in ewald_matrices:
s_supercell = structure * transformation
ewald = EwaldSummation(s_supercell)
ewald_matrices[transformation] = ewald
else:
ewald = ewald_matrices[transformation]
energy = ewald.compute_sub_structure(struct)
return {"num_sites": len(struct), "energy": energy, "structure": struct}
if self.sort_criteria.startswith("m3gnet"):
if self.sort_criteria == "m3gnet_relax":
relax_results = m3gnet_model.relax(struct)
energy = float(relax_results["trajectory"].energies[-1])
struct = relax_results["final_structure"]
elif self.sort_criteria == "m3gnet":
atoms = AseAtomsAdaptor().get_atoms(struct)
m3gnet_model.calculate(atoms)
energy = float(m3gnet_model.results["energy"])
else:
raise ValueError("Unsupported sort criteria.")
return {"num_sites": len(struct), "energy": energy, "structure": struct}
return {"num_sites": len(struct), "structure": struct}
all_structures = Parallel(n_jobs=self.n_jobs)(delayed(_get_stats)(struct) for struct in structures)
def sort_func(struct):
return (
struct["energy"] / struct["num_sites"]
if callable(self.sort_criteria)
or self.sort_criteria.startswith("m3gnet")
or (contains_oxidation_state and self.sort_criteria == "ewald")
else struct["num_sites"]
)
self._all_structures = sorted(all_structures, key=sort_func)
if return_ranked_list:
return self._all_structures[0:num_to_return]
return self._all_structures[0]["structure"]
def __repr__(self):
return "EnumerateStructureTransformation"
@property
def is_one_to_many(self) -> bool:
"""Transform one structure to many."""
return True
class SubstitutionPredictorTransformation(AbstractTransformation):
"""This transformation takes a structure and uses the structure
prediction module to find likely site substitutions.
"""
def __init__(self, threshold=1e-2, scale_volumes=True, **kwargs):
"""
Args:
threshold: Threshold for substitution.
scale_volumes: Whether to scale volumes after substitution.
**kwargs: Args for SubstitutionProbability class lambda_table, alpha.
"""
self.kwargs = kwargs
self.threshold = threshold
self.scale_volumes = scale_volumes
self._substitutor = SubstitutionPredictor(threshold=threshold, **kwargs)
def apply_transformation(self, structure: Structure, return_ranked_list: bool | int = False):
"""Apply the transformation.
Args:
structure: Input Structure
return_ranked_list (bool | int, optional): If return_ranked_list is int, that number of structures
is returned. If False, only the single lowest energy structure is returned. Defaults to False.
Returns:
Predicted Structures.
"""
if not return_ranked_list:
raise ValueError("SubstitutionPredictorTransformation doesn't support returning 1 structure")
preds = self._substitutor.composition_prediction(structure.composition, to_this_composition=False)
preds.sort(key=lambda x: x["probability"], reverse=True)
outputs = []
for pred in preds:
st = SubstitutionTransformation(pred["substitutions"])
output = {
"structure": st.apply_transformation(structure),
"probability": pred["probability"],
"threshold": self.threshold,
"substitutions": {},
}
# dictionary keys have to be converted to strings for JSON
for key, value in pred["substitutions"].items():
output["substitutions"][str(key)] = str(value)
outputs.append(output)
return outputs
def __repr__(self):
return "SubstitutionPredictorTransformation"
@property
def is_one_to_many(self) -> bool:
"""Transform one structure to many."""
return True
class MagOrderParameterConstraint(MSONable):
"""This class can be used to supply MagOrderingTransformation
to just a specific subset of species or sites that satisfy the
provided constraints. This can be useful for setting an order
parameters for, for example, ferrimagnetic structures which
might order on certain motifs, with the global order parameter
dependent on how many sites satisfy that motif.
"""
def __init__(
self,
order_parameter,
species_constraints=None,
site_constraint_name=None,
site_constraints=None,
):
"""
Args:
order_parameter (float): any number from 0.0 to 1.0,
typically 0.5 (antiferromagnetic) or 1.0 (ferromagnetic)
species_constraints (list): str or list of strings
of Species symbols that the constraint should apply to
site_constraint_name (str): name of the site property
that the constraint should apply to, e.g. "coordination_no"
site_constraints (list): list of values of the site
property that the constraints should apply to.
"""
# validation
if site_constraints and site_constraints != [None] and not site_constraint_name:
raise ValueError("Specify the name of the site constraint.")
if not site_constraints and site_constraint_name:
raise ValueError("Please specify some site constraints.")
if not isinstance(species_constraints, list):
species_constraints = [species_constraints]
if not isinstance(site_constraints, list):
site_constraints = [site_constraints]
if order_parameter > 1 or order_parameter < 0:
raise ValueError("Order parameter must lie between 0 and 1")
if order_parameter != 0.5:
warnings.warn(
"Use care when using a non-standard order parameter, "
"though it can be useful in some cases it can also "
"lead to unintended behavior. Consult documentation."
)
self.order_parameter = order_parameter
self.species_constraints = species_constraints
self.site_constraint_name = site_constraint_name
self.site_constraints = site_constraints
def satisfies_constraint(self, site):
"""Check if a periodic site satisfies the constraint."""
if not site.is_ordered:
return False
satisfies_constraints = self.species_constraints and str(site.specie) in self.species_constraints
if self.site_constraint_name and self.site_constraint_name in site.properties:
prop = site.properties[self.site_constraint_name]
satisfies_constraints = prop in self.site_constraints
return satisfies_constraints
class MagOrderingTransformation(AbstractTransformation):
"""This transformation takes a structure and returns a list of collinear
magnetic orderings. For disordered structures, make an ordered
approximation first.
"""
def __init__(self, mag_species_spin, order_parameter=0.5, energy_model=None, **kwargs):
"""
Args:
mag_species_spin: A mapping of elements/species to their
spin magnitudes, e.g. {"Fe3+": 5, "Mn3+": 4}
order_parameter (float or list): if float, a specifies a
global order parameter and can take values from 0.0 to 1.0
(e.g. 0.5 for antiferromagnetic or 1.0 for ferromagnetic), if
list has to be a list of
pymatgen.transformations.advanced_transformations.MagOrderParameterConstraint
to specify more complicated orderings, see documentation for
MagOrderParameterConstraint more details on usage
energy_model: Energy model to rank the returned structures,
see :mod: `pymatgen.analysis.energy_models` for more information (note
that this is not necessarily a physical energy). By default, returned
structures use SymmetryModel() which ranks structures from most
symmetric to least.
kwargs: Additional kwargs that are passed to
EnumerateStructureTransformation such as min_cell_size etc.
"""
# checking for sensible order_parameter values
if isinstance(order_parameter, float):
# convert to constraint format
order_parameter = [
MagOrderParameterConstraint(
order_parameter=order_parameter,
species_constraints=list(mag_species_spin),
)
]
elif isinstance(order_parameter, list):
ops = [isinstance(item, MagOrderParameterConstraint) for item in order_parameter]
if not any(ops):
raise ValueError("Order parameter not correctly defined.")
else:
raise ValueError("Order parameter not correctly defined.")
self.mag_species_spin = mag_species_spin
# store order parameter constraints as dicts to save implementing
# to/from dict methods for MSONable compatibility
self.order_parameter = [op.as_dict() for op in order_parameter]
self.energy_model = energy_model or SymmetryModel()
self.enum_kwargs = kwargs
@staticmethod
def determine_min_cell(disordered_structure):
"""Determine the smallest supercell that is able to enumerate
the provided structure with the given order parameter.
"""
def lcm(n1, n2):
"""Find least common multiple of two numbers."""
return n1 * n2 / gcd(n1, n2)
# assumes all order parameters for a given species are the same
mag_species_order_parameter = {}
mag_species_occurrences = {}
for site in disordered_structure:
if not site.is_ordered:
# this very hacky bit of code only works because we know
# that on disordered sites in this class, all species are the same
# but have different spins, and this is comma-delimited
sp = str(next(iter(site.species))).split(",", maxsplit=1)[0]
if sp in mag_species_order_parameter:
mag_species_occurrences[sp] += 1
else:
op = max(site.species.values())
mag_species_order_parameter[sp] = op
mag_species_occurrences[sp] = 1
smallest_n = []
for sp, order_parameter in mag_species_order_parameter.items():
denom = Fraction(order_parameter).limit_denominator(100).denominator
num_atom_per_specie = mag_species_occurrences[sp]
n_gcd = gcd(denom, num_atom_per_specie)
smallest_n.append(lcm(int(n_gcd), denom) / n_gcd)
return max(smallest_n)
@staticmethod
def _add_dummy_species(structure, order_parameters):
"""
Args:
structure: ordered Structure
order_parameters: list of MagOrderParameterConstraints.
Returns:
A structure decorated with disordered
DummySpecies on which to perform the enumeration.
Note that the DummySpecies are super-imposed on
to the original sites, to make it easier to
retrieve the original site after enumeration is
performed (this approach is preferred over a simple
mapping since multiple species may have the same
DummySpecies, depending on the constraints specified).
This approach can also preserve site properties even after
enumeration.
"""
dummy_struct = structure.copy()
def generate_dummy_specie():
"""Generator which returns DummySpecies symbols Mma, Mmb, etc."""
subscript_length = 1
while True:
for subscript in product(ascii_lowercase, repeat=subscript_length):
yield "Mm" + "".join(subscript)
subscript_length += 1
dummy_species_gen = generate_dummy_specie()
# one dummy species for each order parameter constraint
dummy_species_symbols = [next(dummy_species_gen) for i in range(len(order_parameters))]
dummy_species = [
{
DummySpecies(symbol, spin=Spin.up): constraint.order_parameter,
DummySpecies(symbol, spin=Spin.down): 1 - constraint.order_parameter,
}
for symbol, constraint in zip(dummy_species_symbols, order_parameters)
]
for site in dummy_struct:
satisfies_constraints = [c.satisfies_constraint(site) for c in order_parameters]
if satisfies_constraints.count(True) > 1:
# site should either not satisfy any constraints, or satisfy
# one constraint
raise ValueError(f"Order parameter constraints conflict for site: {site.specie}, {site.properties}")
if any(satisfies_constraints):
dummy_specie_idx = satisfies_constraints.index(True)
dummy_struct.append(dummy_species[dummy_specie_idx], site.coords, site.lattice)
return dummy_struct
@staticmethod
def _remove_dummy_species(structure):
"""Structure with dummy species removed, but their corresponding spin properties
merged with the original sites. Used after performing enumeration.
"""
if not structure.is_ordered:
raise RuntimeError("Something went wrong with enumeration.")
sites_to_remove = []
logger.debug(f"Dummy species structure:\n{structure}")
for idx, site in enumerate(structure):
if isinstance(site.specie, DummySpecies):
sites_to_remove.append(idx)
spin = getattr(site.specie, "spin", 0)
neighbors = structure.get_neighbors(
site,
0.05, # arbitrary threshold, needs to be << any bond length
# but >> floating point precision issues
include_index=True,
)
if len(neighbors) != 1:
raise RuntimeError(f"This shouldn't happen, found {neighbors=}")
orig_site_idx = neighbors[0][2]
orig_specie = structure[orig_site_idx].specie
new_specie = Species(
orig_specie.symbol,
getattr(orig_specie, "oxi_state", None),
spin=spin,
)
structure.replace(
orig_site_idx,
new_specie,
properties=structure[orig_site_idx].properties,
)
structure.remove_sites(sites_to_remove)
logger.debug(f"Structure with dummy species removed:\n{structure}")
return structure
def _add_spin_magnitudes(self, structure: Structure):
"""Replaces Spin.up/Spin.down with spin magnitudes specified by mag_species_spin.
Args:
structure (Structure): Structure to modify.
Returns:
Structure: Structure with spin magnitudes added.
"""
for idx, site in enumerate(structure):
if getattr(site.specie, "spin", None):
spin = site.specie.spin
spin = getattr(site.specie, "spin", None)
sign = int(spin) if spin else 0
if spin:
# this very hacky bit of code only works because we know
# that on disordered sites in this class, all species are the same
# but have different spins, and this is comma-delimited
sp = str(site.specie).split(",", maxsplit=1)[0]
new_spin = sign * self.mag_species_spin.get(sp, 0)
new_specie = Species(
site.specie.symbol,
getattr(site.specie, "oxi_state", None),
spin=new_spin,
)
structure.replace(idx, new_specie, properties=site.properties)
logger.debug(f"Structure with spin magnitudes:\n{structure}")
return structure
def apply_transformation(
self, structure: Structure, return_ranked_list: bool | int = False
) -> Structure | list[Structure]:
"""Apply MagOrderTransformation to an input structure.
Args:
structure (Structure): Any ordered structure.
return_ranked_list (bool | int, optional): If return_ranked_list is int, that number of structures
is returned. If False, only the single lowest energy structure is returned. Defaults to False.
Raises:
ValueError: On disordered structures.
Returns:
Structure | list[Structure]: Structure(s) after MagOrderTransformation.
"""
if not structure.is_ordered:
raise ValueError("Create an ordered approximation of your input structure first.")
# retrieve order parameters
order_parameters = [MagOrderParameterConstraint.from_dict(op_dict) for op_dict in self.order_parameter]
# add dummy species on which to perform enumeration
structure = self._add_dummy_species(structure, order_parameters)
# trivial case
if structure.is_ordered:
structure = self._remove_dummy_species(structure)
return [structure] if return_ranked_list > 1 else structure
enum_kwargs = self.enum_kwargs.copy()
enum_kwargs["min_cell_size"] = max(int(self.determine_min_cell(structure)), enum_kwargs.get("min_cell_size", 1))
if enum_kwargs.get("max_cell_size"):
if enum_kwargs["min_cell_size"] > enum_kwargs["max_cell_size"]:
warnings.warn(
f"Specified max cell size ({enum_kwargs['max_cell_size']}) is "
"smaller than the minimum enumerable cell size "
f"({enum_kwargs['min_cell_size']}), changing max cell size to "
f"{enum_kwargs['min_cell_size']}"
)
enum_kwargs["max_cell_size"] = enum_kwargs["min_cell_size"]
else:
enum_kwargs["max_cell_size"] = enum_kwargs["min_cell_size"]
trafo = EnumerateStructureTransformation(**enum_kwargs)
alls = trafo.apply_transformation(structure, return_ranked_list=return_ranked_list)
# handle the fact that EnumerateStructureTransformation can either
# return a single Structure or a list
if isinstance(alls, Structure):
# remove dummy species and replace Spin.up or Spin.down
# with spin magnitudes given in mag_species_spin arg
alls = self._remove_dummy_species(alls)
alls = self._add_spin_magnitudes(alls) # type: ignore[arg-type]
else:
for idx in range(len(alls)):
alls[idx]["structure"] = self._remove_dummy_species(alls[idx]["structure"]) # type: ignore[index]
alls[idx]["structure"] = self._add_spin_magnitudes(alls[idx]["structure"]) # type: ignore[index, arg-type]
try:
num_to_return = int(return_ranked_list)
except ValueError:
num_to_return = 1
if num_to_return == 1 or not return_ranked_list:
return alls[0]["structure"] if num_to_return else alls # type: ignore[return-value, index]
# remove duplicate structures and group according to energy model
matcher = StructureMatcher(comparator=SpinComparator())
def key(struct: Structure) -> int:
return SpacegroupAnalyzer(struct, 0.1).get_space_group_number()
out = []
for _, group in groupby(sorted((dct["structure"] for dct in alls), key=key), key): # type: ignore[arg-type, index]
group = list(group) # type: ignore
grouped = matcher.group_structures(group)
out.extend([{"structure": g[0], "energy": self.energy_model.get_energy(g[0])} for g in grouped])
self._all_structures = sorted(out, key=lambda dct: dct["energy"])
return self._all_structures[0:num_to_return] # type: ignore
@property
def is_one_to_many(self) -> bool:
"""Transform one structure to many."""
return True
def find_codopant(target: Species, oxidation_state: float, allowed_elements: Sequence[str] | None = None) -> Species:
"""Find the element from "allowed elements" that (i) possesses the desired
"oxidation state" and (ii) is closest in ionic radius to the target specie.
Args:
target (Species): provides target ionic radius.
oxidation_state (float): co-dopant oxidation state.
allowed_elements (list[str]): List of allowed elements. If None,
all elements are tried.
Returns:
Species: with oxidation_state that has ionic radius closest to target.
"""
ref_radius = target.ionic_radius
if ref_radius is None:
raise ValueError(f"Target species {target} has no ionic radius.")
candidates: list[tuple[float, Species]] = []
symbols = allowed_elements or [el.symbol for el in Element]
for sym in symbols:
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
sp = Species(sym, oxidation_state)
radius = sp.ionic_radius
if radius is not None:
candidates.append((radius, sp))
except Exception:
pass
return min(candidates, key=lambda tup: abs(tup[0] / ref_radius - 1))[1]
class DopingTransformation(AbstractTransformation):
"""A transformation that performs doping of a structure."""
def __init__(
self,
dopant,
ionic_radius_tol=float("inf"),
min_length=10,
alio_tol=0,
codopant=False,
max_structures_per_enum=100,
allowed_doping_species=None,
**kwargs,
):
"""
Args:
dopant (Species-like): e.g. Al3+. Must have oxidation state.
ionic_radius_tol (float): e.g. Fractional allowable ionic radii
mismatch for dopant to fit into a site. Default of inf means
that any dopant with the right oxidation state is allowed.
min_length (float): Min. lattice parameter between periodic
images of dopant. Defaults to 10A for now.
alio_tol (int): If this is not 0, attempt will be made to dope
sites with oxidation_states +- alio_tol of the dopant. e.g.
1 means that the ions like Ca2+ and Ti4+ are considered as
potential doping sites for Al3+.
codopant (bool): If True, doping will be carried out with a
codopant to maintain charge neutrality. Otherwise, vacancies
will be used.
max_structures_per_enum (float): Maximum number of structures to
return per enumeration. Note that there can be more than one
candidate doping site, and each site enumeration will return at
max max_structures_per_enum structures. Defaults to 100.
allowed_doping_species (list): Species that are allowed to be
doping sites. This is an inclusionary list. If specified,
any sites which are not
**kwargs:
Same keyword args as EnumerateStructureTransformation,
i.e., min_cell_size, etc.
"""
self.dopant = get_el_sp(dopant)
self.ionic_radius_tol = ionic_radius_tol
self.min_length = min_length
self.alio_tol = alio_tol
self.codopant = codopant
self.max_structures_per_enum = max_structures_per_enum
self.allowed_doping_species = allowed_doping_species
self.kwargs = kwargs
def apply_transformation(self, structure: Structure, return_ranked_list: bool | int = False):
"""
Args:
structure (Structure): Input structure to dope
return_ranked_list (bool | int, optional): If return_ranked_list is int, that number of structures.
is returned. If False, only the single lowest energy structure is returned. Defaults to False.
Returns:
list[dict] | Structure: each dict has shape {"structure": Structure, "energy": float}.
"""
comp = structure.composition
logger.info(f"Composition: {comp}")
for sp in comp:
try:
sp.oxi_state # noqa: B018
except AttributeError:
analyzer = BVAnalyzer()
structure = analyzer.get_oxi_state_decorated_structure(structure)
comp = structure.composition
break
ox = self.dopant.oxi_state
radius = self.dopant.ionic_radius