-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
194 lines (154 loc) · 5.02 KB
/
utils.py
File metadata and controls
194 lines (154 loc) · 5.02 KB
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
# -*- coding: utf-8 -*-
"""utils.py"""
import collections.abc
from aiida import orm
from aiida.engine import calcfunction
from aiida.plugins import DataFactory
UpfData = DataFactory("pseudo.upf")
def update_dict(d, u):
"""update dict by7 another dict, update include all hierarchy
return the update dict instead of change the input dict"""
# pylint: disable=invalid-name
import copy
ret = copy.deepcopy(d)
for k, v in u.items():
if isinstance(v, collections.abc.Mapping):
ret[k] = update_dict(ret.get(k, {}), v)
else:
ret[k] = v
return ret
def parse_label(label):
"""parse standard pseudo label to dict of pseudo info"""
element, type, z, tool, family, *version = label.split(".")
version = ".".join(version)
if type == "nc":
full_type = "NC"
if type == "us":
full_type = "Ultrasoft"
if type == "paw":
full_type = "PAW"
return {
"element": element,
"type": type,
"z": z,
"tool": tool,
"family": family,
"version": version,
"representive_label": f"{z}|{full_type}|{family}|{tool}|{version}",
"concise_label": f"{z}|{type}|{family}|{version}",
"full_label": f"{element}|{z}|{full_type}|{family}|{tool}|{version}",
}
def parse_upf(upf_content: str) -> dict:
"""
:param upf_content:
:param check: if check the integrity of the pp file
:return:
"""
from upf_to_json import upf_to_json
upf_dict = upf_to_json(upf_content, None)
return upf_dict["pseudo_potential"]
def helper_parse_upf(upf: UpfData) -> dict:
"""parser upf"""
header = parse_upf(upf.get_content())["header"]
return header
def to_valid_key(name):
"""
convert name into a valid key name which contain only alphanumeric and underscores
"""
import re
valid_name = re.sub(r"[^\w\s]", "_", name)
return valid_name
@calcfunction
def convergence_analysis(xy: orm.List, criteria: orm.Dict):
"""
xy is a list of xy tuple [(x1, y1), (x2, y2), ...] and
criteria is a dict of {'mode': 'a', 'bounds': (0.0, 0.2)}
"""
# sort xy
sorted_xy = sorted(xy.get_list(), key=lambda k: k[0], reverse=True)
criteria = criteria.get_dict()
mode = criteria["mode"]
cutoff, value = sorted_xy[0]
if mode == 0:
bounds = criteria["bounds"]
eps = criteria["eps"]
# from max cutoff, after some x all y is out of bound
for x, y in sorted_xy:
if bounds[0] - eps < y < bounds[1] + eps:
cutoff, value = x, y
else:
break
return {
"cutoff": orm.Float(cutoff),
"value": orm.Float(value),
}
def reset_pseudos_for_magnetic(pseudo, structure):
"""
override pseudos setting
required for O, Mn, Cr typical configuration and
diamond configuration of all magnetic elements where
the kind names varies for sites
"""
pseudos = {}
for kind_name in structure.get_kind_names():
pseudos[kind_name] = pseudo
return pseudos
def get_magnetic_inputs(structure: orm.StructureData):
"""
To set initial magnet to the magnetic system, need to set magnetic order to
every magnetic element site, with certain pw starting_mainetization parameters.
! Only for typical configurations of magnetic elements.
"""
MAG_INIT_Mn = {
"Mn1": 0.5,
"Mn2": -0.3,
"Mn3": 0.5,
"Mn4": -0.3,
} # pylint: disable=invalid-name
MAG_INIT_O = {
"O1": 0.5,
"O2": 0.5,
"O3": -0.5,
"O4": -0.5,
} # pylint: disable=invalid-name
MAG_INIT_Cr = {"Cr1": 0.5, "Cr2": -0.5} # pylint: disable=invalid-name
mag_structure = orm.StructureData(cell=structure.cell, pbc=structure.pbc)
kind_name = structure.get_kind_names()[0]
# ferromagnetic
if kind_name in ["Fe", "Co", "Ni"]:
for i, site in enumerate(structure.sites):
mag_structure.append_atom(position=site.position, symbols=kind_name)
parameters = {
"SYSTEM": {
"nspin": 2,
"starting_magnetization": {kind_name: 0.2},
},
}
#
if kind_name in ["Mn", "O", "Cr"]:
for i, site in enumerate(structure.sites):
mag_structure.append_atom(
position=site.position, symbols=kind_name, name=f"{kind_name}{i + 1}"
)
if kind_name == "Mn":
parameters = {
"SYSTEM": {
"nspin": 2,
"starting_magnetization": MAG_INIT_Mn,
},
}
if kind_name == "O":
parameters = {
"SYSTEM": {
"nspin": 2,
"starting_magnetization": MAG_INIT_O,
},
}
if kind_name == "Cr":
parameters = {
"SYSTEM": {
"nspin": 2,
"starting_magnetization": MAG_INIT_Cr,
},
}
return mag_structure, parameters