-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.py
1148 lines (905 loc) · 46 KB
/
analyzer.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
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.
"""
This module provides some useful functions for dealing with magnetic Structures
(e.g. Structures with associated magmom tags).
"""
from __future__ import annotations
import logging
import os
import warnings
from collections import namedtuple
from enum import Enum, unique
from typing import Any
import numpy as np
from monty.serialization import loadfn
from scipy.signal import argrelextrema
from scipy.stats import gaussian_kde
from pymatgen.core.structure import DummySpecies, Element, Species, Structure
from pymatgen.electronic_structure.core import Magmom
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.symmetry.groups import SpaceGroup
from pymatgen.transformations.advanced_transformations import (
MagOrderingTransformation,
MagOrderParameterConstraint,
)
from pymatgen.transformations.standard_transformations import (
AutoOxiStateDecorationTransformation,
)
from pymatgen.util.typing import VectorLike
__author__ = "Matthew Horton"
__copyright__ = "Copyright 2017, The Materials Project"
__version__ = "0.1"
__maintainer__ = "Matthew Horton"
__email__ = "mkhorton@lbl.gov"
__status__ = "Development"
__date__ = "Feb 2017"
MODULE_DIR = os.path.dirname(os.path.abspath(__file__))
# MODULE_DIR = '/home/skim52/anaconda3/envs/e3nn_new/lib/python3.8/site-packages/pymatgen/analysis/magnetism'
try:
DEFAULT_MAGMOMS = loadfn(os.path.join(MODULE_DIR, "default_magmoms.yaml"))
except Exception:
warnings.warn("Could not load default_magmoms.yaml, falling back to VASPIncarBase.yaml")
DEFAULT_MAGMOMS = loadfn(os.path.join(MODULE_DIR, "../../io/vasp/VASPIncarBase.yaml"))
DEFAULT_MAGMOMS = DEFAULT_MAGMOMS["MAGMOM"]
@unique
class Ordering(Enum):
"""
Enumeration defining possible magnetic orderings.
"""
FM = "FM" # Ferromagnetic
AFM = "AFM" # Antiferromagnetic
FiM = "FiM" # Ferrimagnetic
NM = "NM" # Non-magnetic
Unknown = "Unknown"
@unique
class OverwriteMagmomMode(Enum):
"""
Enumeration defining different modes for analyzer.
"""
none = "none"
respect_sign = "respect_sign"
respect_zero = "respect_zeros"
replace_all = "replace_all"
normalize = "normalize"
class CollinearMagneticStructureAnalyzer:
"""
A class which provides a few helpful methods to analyze
collinear magnetic structures.
"""
def __init__(
self,
structure: Structure,
# ordering: str,
overwrite_magmom_mode: OverwriteMagmomMode | str = "none",
round_magmoms: bool = False,
detect_valences: bool = False,
make_primitive: bool = True,
default_magmoms: dict = None,
set_net_positive: bool = True,
threshold: float = 0.00,
threshold_nonmag: float = 0.1,
):
"""
If magnetic moments are not defined, moments will be
taken either from default_magmoms.yaml (similar to the
default magmoms in MPRelaxSet, with a few extra definitions)
or from a specie:magmom dict provided by the default_magmoms
kwarg.
Input magmoms can be replaced using the 'overwrite_magmom_mode'
kwarg. This can be:
* "none" to do nothing,
* "respect_sign" which will overwrite existing magmoms with
those from default_magmoms but will keep sites with positive magmoms
positive, negative magmoms negative and zero magmoms zero,
* "respect_zeros", which will give a ferromagnetic structure
(all positive magmoms from default_magmoms) but still keep sites with
zero magmoms as zero,
* "replace_all" which will try to guess initial magmoms for
all sites in the structure irrespective of input structure
(this is most suitable for an initial DFT calculation),
* "replace_all_if_undefined" is the same as "replace_all" but only if
no magmoms are defined in input structure, otherwise it will respect
existing magmoms.
* "normalize" will normalize magmoms to unity, but will respect sign
(used for comparing orderings), magmoms < theshhold will be set to zero
Args:
structure: input Structure object
overwrite_magmom_mode: "respect_sign", "respect_zeros", "replace_all",
"replace_all_if_undefined", "normalize" (default "none")
round_magmoms: will round input magmoms to
specified number of decimal places if integer is supplied, if set
to a float will try and group magmoms together using a kernel density
estimator of provided width, and extracting peaks of the estimator
detect_valences: if True, will attempt to assign valences
to input structure
make_primitive: if True, will transform to primitive
magnetic cell
default_magmoms: (optional) dict specifying default magmoms
set_net_positive: if True, will change sign of magnetic
moments such that the net magnetization is positive. Argument will be
ignored if mode "respect_sign" is used.
threshold: number (in Bohr magnetons) below which magmoms
will be rounded to zero
threshold_nonmag: number (in Bohr magneton)
below which nonmagnetic ions (with no magmom specified
in default_magmoms) will be rounded to zero
"""
if default_magmoms:
self.default_magmoms = default_magmoms
else:
self.default_magmoms = DEFAULT_MAGMOMS
structure = structure.copy()
# check for disorder
if not structure.is_ordered:
raise NotImplementedError("Not implemented for disordered structures, make ordered approximation first.")
if detect_valences:
trans = AutoOxiStateDecorationTransformation()
try:
structure = trans.apply_transformation(structure)
except ValueError:
warnings.warn(f"Could not assign valences for {structure.composition.reduced_formula}")
# check to see if structure has magnetic moments
# on site properties or species spin properties,
# prioritize site properties
has_magmoms = bool(structure.site_properties.get("magmom", False))
has_spin = False
for comp in structure.species_and_occu:
for sp, occu in comp.items():
if getattr(sp, "spin", False):
has_spin = True
# perform input sanitation ...
# rest of class will assume magnetic moments
# are stored on site properties:
# this is somewhat arbitrary, arguments can
# be made for both approaches
if has_magmoms and has_spin:
raise ValueError(
"Structure contains magnetic moments on both "
"magmom site properties and spin species "
"properties. This is ambiguous. Remove one or "
"the other."
)
if has_magmoms:
if None in structure.site_properties["magmom"]:
warnings.warn(
"Be careful with mixing types in your magmom "
"site properties. Any 'None' magmoms have been "
"replaced with zero."
)
magmoms = [m if m else 0 for m in structure.site_properties["magmom"]]
elif has_spin:
magmoms = [getattr(sp, "spin", 0) for sp in structure.species]
structure.remove_spin()
else:
# no magmoms present, add zero magmoms for now
magmoms = [0] * len(structure)
# and overwrite magmoms with default magmoms later unless otherwise stated
if overwrite_magmom_mode == "replace_all_if_undefined":
overwrite_magmom_mode = "replace_all"
# test to see if input structure has collinear magmoms
self.is_collinear = Magmom.are_collinear(magmoms)
if not self.is_collinear:
warnings.warn(
"This class is not designed to be used with "
"non-collinear structures. If your structure is "
"only slightly non-collinear (e.g. canted) may still "
"give useful results, but use with caution."
)
# this is for collinear structures only, make sure magmoms
# are all floats
magmoms = list(map(float, magmoms))
# set properties that should be done /before/ we process input magmoms
self.total_magmoms = sum(magmoms)
self.magnetization = sum(magmoms) / structure.volume
# round magmoms on magnetic ions below threshold to zero
# and on non magnetic ions below threshold_nonmag
magmoms = [
m
if abs(m) > threshold and a.species_string in self.default_magmoms
else m
if abs(m) > threshold_nonmag and a.species_string not in self.default_magmoms
else 0
for (m, a) in zip(magmoms, structure.sites)
]
# overwrite existing magmoms with default_magmoms
if overwrite_magmom_mode not in (
"none",
"respect_sign",
"respect_zeros",
"replace_all",
"replace_all_if_undefined",
"normalize",
):
raise ValueError("Unsupported mode.")
for idx, site in enumerate(structure):
if site.species_string in self.default_magmoms:
# look for species first, e.g. Fe2+
default_magmom = self.default_magmoms[site.species_string]
elif isinstance(site.specie, Species) and str(site.specie.element) in self.default_magmoms:
# look for element, e.g. Fe
default_magmom = self.default_magmoms[str(site.specie.element)]
else:
default_magmom = 0
# overwrite_magmom_mode = "respect_sign" will change magnitude of
# existing moments only, and keep zero magmoms as
# zero: it will keep the magnetic ordering intact
if overwrite_magmom_mode == "respect_sign":
set_net_positive = False
if magmoms[idx] > 0:
magmoms[idx] = default_magmom
elif magmoms[idx] < 0:
magmoms[idx] = -default_magmom
# overwrite_magmom_mode = "respect_zeros" will give a ferromagnetic
# structure but will keep zero magmoms as zero
elif overwrite_magmom_mode == "respect_zeros":
if magmoms[idx] != 0:
magmoms[idx] = default_magmom
# overwrite_magmom_mode = "replace_all" will ignore input magmoms
# and give a ferromagnetic structure with magnetic
# moments on *all* atoms it thinks could be magnetic
elif overwrite_magmom_mode == "replace_all":
magmoms[idx] = default_magmom
# overwrite_magmom_mode = "normalize" set magmoms magnitude to 1
elif overwrite_magmom_mode == "normalize":
if magmoms[idx] != 0:
magmoms[idx] = int(magmoms[idx] / abs(magmoms[idx]))
# round magmoms, used to smooth out computational data
magmoms = self._round_magmoms(magmoms, round_magmoms) if round_magmoms else magmoms # type: ignore
if set_net_positive:
sign = np.sum(magmoms)
if sign < 0:
magmoms = [-x for x in magmoms]
structure.add_site_property("magmom", magmoms)
if make_primitive:
structure = structure.get_primitive_structure(use_site_props=True)
self.structure = structure
# orderings = {'FM': Ordering.FM, 'FiM': Ordering.FiM, 'AFM': Ordering.AFM, 'NM': Ordering.NM, 'Unknown': Ordering.Unknown}
# self.ordering = orderings[ordering]
@staticmethod
def _round_magmoms(magmoms: VectorLike, round_magmoms_mode: int | float) -> np.ndarray:
"""If round_magmoms_mode is an integer, simply round to that number
of decimal places, else if set to a float will try and round
intelligently by grouping magmoms.
"""
if isinstance(round_magmoms_mode, int):
# simple rounding to number of decimal places
magmoms = np.around(magmoms, decimals=round_magmoms_mode)
elif isinstance(round_magmoms_mode, float):
try:
# get range of possible magmoms, pad by 50% just to be safe
range_m = max([max(magmoms), abs(min(magmoms))]) * 1.5
# construct kde, here "round_magmoms_mode" is the width of the kde
kernel = gaussian_kde(magmoms, bw_method=round_magmoms_mode)
# with a linearly spaced grid 1000x finer than width
xgrid = np.linspace(-range_m, range_m, int(1000 * range_m / round_magmoms_mode))
# and evaluate the kde on this grid, extracting the maxima of the kde peaks
kernel_m = kernel.evaluate(xgrid)
extrema = xgrid[argrelextrema(kernel_m, comparator=np.greater)]
# round magmoms to these extrema
magmoms = [extrema[(np.abs(extrema - m)).argmin()] for m in magmoms]
except Exception as e:
# TODO: typically a singular matrix warning, investigate this
warnings.warn("Failed to round magmoms intelligently, falling back to simple rounding.")
warnings.warn(str(e))
# and finally round roughly to the number of significant figures in our kde width
num_decimals = len(str(round_magmoms_mode).split(".")[1]) + 1
magmoms = np.around(magmoms, decimals=num_decimals)
return np.array(magmoms)
def get_structure_with_spin(self) -> Structure:
"""Returns a Structure with species decorated with spin values instead
of using magmom site properties.
"""
structure = self.structure.copy()
structure.add_spin_by_site(structure.site_properties["magmom"])
structure.remove_site_property("magmom")
return structure
def get_structure_with_only_magnetic_atoms(self, make_primitive: bool = True) -> Structure:
"""Returns a Structure with only magnetic atoms present.
Args:
make_primitive: Whether to make structure primitive after
removing non-magnetic atoms (Default value = True)
Returns: Structure
"""
sites = [site for site in self.structure if abs(site.properties["magmom"]) > 0]
structure = Structure.from_sites(sites)
if make_primitive:
structure = structure.get_primitive_structure(use_site_props=True)
return structure
def get_nonmagnetic_structure(self, make_primitive: bool = True) -> Structure:
"""Returns a Structure without magnetic moments defined.
Args:
make_primitive: Whether to make structure primitive after
removing magnetic information (Default value = True)
Returns:
Structure
"""
structure = self.structure.copy()
structure.remove_site_property("magmom")
if make_primitive:
structure = structure.get_primitive_structure()
return structure
def get_ferromagnetic_structure(self, make_primitive: bool = True) -> Structure:
"""Returns a Structure with all magnetic moments positive
or zero.
Args:
make_primitive: Whether to make structure primitive after
making all magnetic moments positive (Default value = True)
Returns:
Structure
"""
structure = self.structure.copy()
structure.add_site_property("magmom", [abs(m) for m in self.magmoms])
if make_primitive:
structure = structure.get_primitive_structure(use_site_props=True)
return structure
@property
def is_magnetic(self) -> bool:
"""Convenience property, returns True if any non-zero magmoms present."""
return any(map(abs, self.structure.site_properties["magmom"]))
@property
def magmoms(self) -> np.ndarray:
"""Convenience property, returns magmoms as a numpy array."""
return np.array(self.structure.site_properties["magmom"])
@property
def types_of_magnetic_species(
self,
) -> tuple[Element | Species | DummySpecies, ...]:
"""Equivalent to Structure.types_of_specie but only returns
magnetic species.
Returns: types of Species as a list
"""
if self.number_of_magnetic_sites > 0:
structure = self.get_structure_with_only_magnetic_atoms()
return tuple(sorted(structure.types_of_species))
return tuple()
@property
def types_of_magnetic_specie(
self,
) -> tuple[Element | Species | DummySpecies, ...]:
"""
Specie->Species rename. Used to maintain backwards compatibility.
"""
return self.types_of_magnetic_species
@property
def magnetic_species_and_magmoms(self) -> dict[str, Any]:
"""Returns a dict of magnetic species and the magnitude of
their associated magmoms. Will return a list if there are
multiple magmoms per species.
Returns: dict of magnetic species and magmoms
"""
structure = self.get_ferromagnetic_structure()
magtypes: dict = {str(site.specie): set() for site in structure if site.properties["magmom"] != 0}
for site in structure:
if site.properties["magmom"] != 0:
magtypes[str(site.specie)].add(site.properties["magmom"])
for sp, magmoms in magtypes.items():
if len(magmoms) == 1:
magtypes[sp] = magmoms.pop()
else:
magtypes[sp] = sorted(list(magmoms))
return magtypes
@property
def number_of_magnetic_sites(self) -> int:
"""Number of magnetic sites present in structure."""
return int(np.sum([abs(m) > 0 for m in self.magmoms]))
def number_of_unique_magnetic_sites(self, symprec: float = 1e-3, angle_tolerance: float = 5) -> int:
"""
Args:
symprec: same as in SpacegroupAnalyzer (Default value = 1e-3)
angle_tolerance: same as in SpacegroupAnalyzer (Default value = 5)
Returns: Number of symmetrically-distinct magnetic sites present
in structure.
"""
structure = self.get_nonmagnetic_structure()
sga = SpacegroupAnalyzer(structure, symprec=symprec, angle_tolerance=angle_tolerance)
symm_structure = sga.get_symmetrized_structure()
num_unique_mag_sites = 0
for group_of_sites in symm_structure.equivalent_sites:
if group_of_sites[0].specie in self.types_of_magnetic_species:
num_unique_mag_sites += 1
return num_unique_mag_sites
@property
def ordering(self) -> Ordering:
"""Applies heuristics to return a magnetic ordering for a collinear
magnetic structure. Result is not guaranteed for correctness.
Returns: Ordering Enum ('FiM' is used as the abbreviation for
ferrimagnetic)
"""
# the new version of the api allows us to retrieve the ordering...
if not self.is_collinear:
warnings.warn("Detecting ordering in non-collinear structures not yet implemented.")
return Ordering.Unknown
if "magmom" not in self.structure.site_properties:
# maybe this was a non-spin-polarized calculation, or we've
# lost the magnetic moment information
return Ordering.Unknown
magmoms = self.magmoms
max_magmom = max(magmoms)
total_magnetization = abs(sum(magmoms))
is_potentially_ferromagnetic = np.all(magmoms >= 0) or np.all(magmoms <= 0)
if total_magnetization > 0 and is_potentially_ferromagnetic:
return Ordering.FM
if total_magnetization > 0:
return Ordering.FiM
if max_magmom > 0:
return Ordering.AFM
return Ordering.NM
def get_exchange_group_info(self, symprec: float = 1e-2, angle_tolerance: float = 5.0) -> tuple[str, int]:
"""Returns the information on the symmetry of the Hamiltonian
describing the exchange energy of the system, taking into
account relative direction of magnetic moments but not their
absolute direction.
This is not strictly accurate (e.g. some/many atoms will
have zero magnetic moments), but defining symmetry this
way is a useful way of keeping track of distinct magnetic
orderings within pymatgen.
Args:
symprec: same as SpacegroupAnalyzer (Default value = 1e-2)
angle_tolerance: same as SpacegroupAnalyzer (Default value = 5.0)
Returns:
spacegroup_symbol, international_number
"""
structure = self.get_structure_with_spin()
return structure.get_space_group_info(symprec=symprec, angle_tolerance=angle_tolerance)
def matches_ordering(self, other: Structure) -> bool:
"""Compares the magnetic orderings of one structure with another.
Args:
other: Structure to compare
Returns: True or False
"""
a = CollinearMagneticStructureAnalyzer(
self.structure, overwrite_magmom_mode="normalize"
).get_structure_with_spin()
# sign of spins doesn't matter, so we're comparing both
# positive and negative versions of the structure
# this code is possibly redundant, but is included out of
# an abundance of caution
b_positive = CollinearMagneticStructureAnalyzer(other, overwrite_magmom_mode="normalize", make_primitive=False)
b_negative = b_positive.structure.copy()
b_negative.add_site_property("magmom", np.multiply(-1, b_negative.site_properties["magmom"]))
b_negative = CollinearMagneticStructureAnalyzer(
b_negative, overwrite_magmom_mode="normalize", make_primitive=False
)
b_positive = b_positive.get_structure_with_spin()
b_negative = b_negative.get_structure_with_spin()
return a.matches(b_positive) or a.matches(b_negative)
def __str__(self):
"""
Sorts a Structure (by fractional coordinate), and
prints sites with magnetic information. This is
useful over Structure.__str__ because sites are in
a consistent order, which makes visual comparison between
two identical Structures with different magnetic orderings
easier.
"""
frac_coords = self.structure.frac_coords
sorted_indices = np.lexsort((frac_coords[:, 2], frac_coords[:, 1], frac_coords[:, 0]))
s = Structure.from_sites([self.structure[idx] for idx in sorted_indices])
# adapted from Structure.__repr__
outs = ["Structure Summary", repr(s.lattice)]
outs.append("Magmoms Sites")
for site in s:
if site.properties["magmom"] != 0:
prefix = f"{site.properties['magmom']:+.2f} "
else:
prefix = " "
outs.append(prefix + repr(site))
return "\n".join(outs)
class MagneticStructureEnumerator:
"""Combines MagneticStructureAnalyzer and MagOrderingTransformation to
automatically generate a set of transformations for a given structure
and produce a list of plausible magnetic orderings.
"""
available_strategies = (
"ferromagnetic",
"antiferromagnetic",
"ferrimagnetic_by_motif",
"ferrimagnetic_by_species",
"antiferromagnetic_by_motif",
"nonmagnetic",
)
def __init__(
self,
structure: Structure,
default_magmoms: dict[str, float] | None = None,
strategies: list[str]
| tuple[str, ...] = (
"ferromagnetic",
"antiferromagnetic",
),
automatic: bool = True,
truncate_by_symmetry: bool = True,
transformation_kwargs: dict | None = None,
):
"""
This class will try generated different collinear
magnetic orderings for a given input structure.
If the input structure has magnetic moments defined, it
is possible to use these as a hint as to which elements are
magnetic, otherwise magnetic elements will be guessed
(this can be changed using default_magmoms kwarg).
Args:
structure: input structure
default_magmoms: (optional, defaults provided) dict of
magnetic elements to their initial magnetic moments in µB, generally
these are chosen to be high-spin since they can relax to a low-spin
configuration during a DFT electronic configuration
strategies: different ordering strategies to use, choose from:
ferromagnetic, antiferromagnetic, antiferromagnetic_by_motif,
ferrimagnetic_by_motif and ferrimagnetic_by_species (here, "motif",
means to use a different ordering parameter for symmetry inequivalent
sites)
automatic: if True, will automatically choose sensible strategies
truncate_by_symmetry: if True, will remove very unsymmetrical
orderings that are likely physically implausible
transformation_kwargs: keyword arguments to pass to
MagOrderingTransformation, to change automatic cell size limits, etc.
"""
self.logger = logging.getLogger(self.__class__.__name__)
self.structure = structure
# decides how to process input structure, which sites are magnetic
self.default_magmoms = default_magmoms
# different strategies to attempt, default is usually reasonable
self.strategies = list(strategies)
# and whether to automatically add strategies that may be appropriate
self.automatic = automatic
# and whether to discard low symmetry structures
self.truncate_by_symmetry = truncate_by_symmetry
# other settings
self.num_orderings = 64
self.max_unique_sites = 8
# kwargs to pass to transformation (ultimately to enumlib)
default_transformation_kwargs = {"check_ordered_symmetry": False, "timeout": 5}
transformation_kwargs = transformation_kwargs or {}
transformation_kwargs.update(default_transformation_kwargs)
self.transformation_kwargs = transformation_kwargs
# our magnetically ordered structures will be
# stored here once generated and also store which
# transformation created them, this is used for
# book-keeping/user interest, and
# is be a list of strings in ("fm", "afm",
# "ferrimagnetic_by_species", "ferrimagnetic_by_motif",
# "afm_by_motif", "input_structure")
self.ordered_structures: list[Structure] = []
self.ordered_structure_origins: list[str] = []
formula = structure.composition.reduced_formula
# to process disordered magnetic structures, first make an
# ordered approximation
if not structure.is_ordered:
raise ValueError(f"Please obtain an ordered approximation of the input structure ({formula}).")
# CollinearMagneticStructureAnalyzer is used throughout:
# it can tell us whether the input is itself collinear (if not,
# this workflow is not appropriate), and has many convenience
# methods e.g. magnetic structure matching, etc.
self.input_analyzer = CollinearMagneticStructureAnalyzer(
structure, default_magmoms=default_magmoms, overwrite_magmom_mode="none"
)
# this workflow enumerates structures with different combinations
# of up and down spin and does not include spin-orbit coupling:
# if your input structure has vector magnetic moments, this
# workflow is not appropriate
if not self.input_analyzer.is_collinear:
raise ValueError(f"Input structure ({formula}) is non-collinear.")
self.sanitized_structure = self._sanitize_input_structure(structure)
# we will first create a set of transformations
# and then apply them to our input structure
self.transformations = self._generate_transformations(self.sanitized_structure)
self._generate_ordered_structures(self.sanitized_structure, self.transformations)
@staticmethod
def _sanitize_input_structure(input_structure: Structure) -> Structure:
"""Sanitize our input structure by removing magnetic information
and making primitive.
Args:
input_structure: Structure
Returns: Structure
"""
input_structure = input_structure.copy()
# remove any annotated spin
input_structure.remove_spin()
# sanitize input structure: first make primitive ...
input_structure = input_structure.get_primitive_structure(use_site_props=False)
# ... and strip out existing magmoms, which can cause conflicts
# with later transformations otherwise since sites would end up
# with both magmom site properties and Species spins defined
if "magmom" in input_structure.site_properties:
input_structure.remove_site_property("magmom")
return input_structure
def _generate_transformations(self, structure: Structure) -> dict[str, MagOrderingTransformation]:
"""The central problem with trying to enumerate magnetic orderings is
that we have to enumerate orderings that might plausibly be magnetic
ground states, while not enumerating orderings that are physically
implausible. The problem is that it is not always obvious by e.g.
symmetry arguments alone which orderings to prefer. Here, we use a
variety of strategies (heuristics) to enumerate plausible orderings,
and later discard any duplicates that might be found by multiple
strategies. This approach is not ideal, but has been found to be
relatively robust over a wide range of magnetic structures.
Args:
structure: A sanitized input structure (_sanitize_input_structure)
Returns: A dict of a transformation class instance (values) and name of
enumeration strategy (keys)
Returns: dict of Transformations keyed by strategy
"""
formula = structure.composition.reduced_formula
transformations: dict[str, MagOrderingTransformation] = {}
# analyzer is used to obtain information on sanitized input
analyzer = CollinearMagneticStructureAnalyzer(
structure,
default_magmoms=self.default_magmoms,
overwrite_magmom_mode="replace_all",
)
if not analyzer.is_magnetic:
raise ValueError(
"Not detected as magnetic, add a new default magmom for the element you believe may be magnetic?"
)
# now we can begin to generate our magnetic orderings
self.logger.info(f"Generating magnetic orderings for {formula}")
mag_species_spin = analyzer.magnetic_species_and_magmoms
types_mag_species = sorted(
analyzer.types_of_magnetic_species,
key=lambda sp: analyzer.default_magmoms.get(str(sp), 0),
reverse=True,
)
num_mag_sites = analyzer.number_of_magnetic_sites
num_unique_sites = analyzer.number_of_unique_magnetic_sites()
# enumerations become too slow as number of unique sites (and thus
# permutations) increase, 8 is a soft limit, this can be increased
# but do so with care
if num_unique_sites > self.max_unique_sites:
raise ValueError("Too many magnetic sites to sensibly perform enumeration.")
# maximum cell size to consider: as a rule of thumb, if the primitive cell
# contains a large number of magnetic sites, perhaps we only need to enumerate
# within one cell, whereas on the other extreme if the primitive cell only
# contains a single magnetic site, we have to create larger supercells
if "max_cell_size" not in self.transformation_kwargs:
# TODO: change to 8 / num_mag_sites ?
self.transformation_kwargs["max_cell_size"] = max(1, int(4 / num_mag_sites))
self.logger.info(f"Max cell size set to {self.transformation_kwargs['max_cell_size']}")
# when enumerating ferrimagnetic structures, it's useful to detect
# symmetrically distinct magnetic sites, since different
# local environments can result in different magnetic order
# (e.g. inverse spinels)
# initially, this was done by co-ordination number, but is
# now done by a full symmetry analysis
sga = SpacegroupAnalyzer(structure)
structure_sym = sga.get_symmetrized_structure()
wyckoff = ["n/a"] * len(structure)
for indices, symbol in zip(structure_sym.equivalent_indices, structure_sym.wyckoff_symbols):
for index in indices:
wyckoff[index] = symbol
is_magnetic_sites = [site.specie in types_mag_species for site in structure]
# we're not interested in sites that we don't think are magnetic,
# set these symbols to None to filter them out later
wyckoff = [
symbol if is_magnetic_site else "n/a" for symbol, is_magnetic_site in zip(wyckoff, is_magnetic_sites)
]
structure.add_site_property("wyckoff", wyckoff)
wyckoff_symbols = set(wyckoff) - {"n/a"}
# if user doesn't specifically request ferrimagnetic orderings,
# we apply a heuristic as to whether to attempt them or not
if self.automatic:
if (
"ferrimagnetic_by_motif" not in self.strategies
and len(wyckoff_symbols) > 1
and len(types_mag_species) == 1
):
self.strategies += ["ferrimagnetic_by_motif"]
if (
"antiferromagnetic_by_motif" not in self.strategies
and len(wyckoff_symbols) > 1
and len(types_mag_species) == 1
):
self.strategies += ["antiferromagnetic_by_motif"]
if "ferrimagnetic_by_species" not in self.strategies and len(types_mag_species) > 1:
self.strategies += ["ferrimagnetic_by_species"]
# we start with a ferromagnetic ordering
if "ferromagnetic" in self.strategies:
# TODO: remove 0 spins !
fm_structure = analyzer.get_ferromagnetic_structure()
# store magmom as spin property, to be consistent with output from
# other transformations
fm_structure.add_spin_by_site(fm_structure.site_properties["magmom"])
fm_structure.remove_site_property("magmom")
# we now have our first magnetic ordering...
self.ordered_structures.append(fm_structure)
self.ordered_structure_origins.append("fm")
# we store constraint(s) for each strategy first,
# and then use each to perform a transformation later
all_constraints: dict[str, Any] = {}
# ...to which we can add simple AFM cases first...
if "antiferromagnetic" in self.strategies:
constraint = MagOrderParameterConstraint(
0.5,
# TODO: update MagOrderParameterConstraint in
# pymatgen to take types_mag_species directly
species_constraints=list(map(str, types_mag_species)),
)
all_constraints["afm"] = [constraint]
# allows for non-magnetic sublattices
if len(types_mag_species) > 1:
for sp in types_mag_species:
constraints = [MagOrderParameterConstraint(0.5, species_constraints=str(sp))]
all_constraints[f"afm_by_{sp}"] = constraints
# ...and then we also try ferrimagnetic orderings by motif if a
# single magnetic species is present...
if "ferrimagnetic_by_motif" in self.strategies and len(wyckoff_symbols) > 1:
# these orderings are AFM on one local environment, and FM on the rest
for symbol in wyckoff_symbols:
constraints = [
MagOrderParameterConstraint(0.5, site_constraint_name="wyckoff", site_constraints=symbol),
MagOrderParameterConstraint(
1.0,
site_constraint_name="wyckoff",
site_constraints=list(wyckoff_symbols - {symbol}),
),
]
all_constraints[f"ferri_by_motif_{symbol}"] = constraints
# and also try ferrimagnetic when there are multiple magnetic species
if "ferrimagnetic_by_species" in self.strategies:
sp_list = [str(site.specie) for site in structure]
num_sp = {sp: sp_list.count(str(sp)) for sp in types_mag_species}
total_mag_sites = sum(num_sp.values())
for sp in types_mag_species:
# attempt via a global order parameter
all_constraints[f"ferri_by_{sp}"] = num_sp[sp] / total_mag_sites
# attempt via afm on sp, fm on remaining species
constraints = [
MagOrderParameterConstraint(0.5, species_constraints=str(sp)),
MagOrderParameterConstraint(
1.0,
species_constraints=list(map(str, set(types_mag_species) - {sp})),
),
]
all_constraints[f"ferri_by_{sp}_afm"] = constraints
# ...and finally, we can try orderings that are AFM on one local
# environment, and non-magnetic on the rest -- this is less common
# but unless explicitly attempted, these states are unlikely to be found
if "antiferromagnetic_by_motif" in self.strategies:
for symbol in wyckoff_symbols:
constraints = [
MagOrderParameterConstraint(0.5, site_constraint_name="wyckoff", site_constraints=symbol)
]
all_constraints[f"afm_by_motif_{symbol}"] = constraints
# and now construct all our transformations for each strategy
transformations = {}
for name, constraints in all_constraints.items():
trans = MagOrderingTransformation(
mag_species_spin, order_parameter=constraints, **self.transformation_kwargs
)
transformations[name] = trans
return transformations
def _generate_ordered_structures(
self,
sanitized_input_structure: Structure,
transformations: dict[str, MagOrderingTransformation],
):
"""Apply our input structure to our list of transformations and output a list
of ordered structures that have been pruned for duplicates and for those
with low symmetry (optional).
Args:
sanitized_input_structure: A sanitized input structure
(_sanitize_input_structure)
transformations: A dict of transformations (values) and name of
enumeration strategy (key), the enumeration strategy name is just
for record keeping
Returns: None (sets self.ordered_structures
and self.ordered_structures_origins instance variables)