Skip to content

Commit 141faaa

Browse files
authored
Merge pull request #80 from Lonya0/main
Update preprocess.py for abacus not so accurate magmom process
2 parents 89758ab + 618bf01 commit 141faaa

1 file changed

Lines changed: 40 additions & 27 deletions

File tree

deeph/scripts/preprocess.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,46 @@ def collect_magmom_from_openmx(input_dir, output_dir, num_atom, mag_element):
2727

2828
np.savetxt(os.path.join(output_dir, "magmom.txt"), magmom_data)
2929

30-
def collect_magmom_from_abacus(input_dir, output_dir, num_atom, mag_element):
30+
def collect_magmom_from_abacus(input_dir, output_dir, abacus_suffix, num_atom, mag_element): #to use this feature, be sure to turn out_chg and out_mul in abacus INPUT file, if not, will use mag setting in STRU file, and this may loss accuracy or incorrect
3131
magmom_data = np.zeros((num_atom, 4))
32-
index_atom = 0
33-
34-
with open(os.path.join(input_dir, "STRU"), 'r') as file:
35-
lines = file.readlines()
36-
for k in range(len(lines)): # k = line index
37-
if lines[k].strip() == 'ATOMIC_POSITIONS':
38-
kk = k + 2 # kk = current line index
39-
while kk < len(lines):
40-
if lines[kk] == "\n": # for if empty line between two elements, as ABACUS accepts
41-
continue
42-
element_str = lines[kk].strip()
43-
element_amount = int(lines[kk + 2].strip())
44-
for j in range(element_amount):
45-
line = lines[kk + 3 + j].strip().split()
46-
if len(line) < 11: # check if magmom is included
47-
raise ValueError('this line do not contain magmom: {} in this file: {}'.format(line, input_dir))
48-
if line[7] != "angle1" and line[8] != "angle1": # check if magmom is in angle mode
49-
raise ValueError('mag in STRU should be mag * angle1 * angle2 *')
50-
if line[6] == "mag": # for if 'm' is included
51-
index_str = 7
52-
else:
53-
index_str = 8
54-
magmom_data[index_atom] = int(element_str in mag_element), line[index_str], line[index_str + 2], line[index_str + 4]
55-
index_atom += 1
56-
kk += 3 + element_amount
32+
33+
# using running_scf.log file with INPUT file out_chg and out_mul == 1
34+
cmd = f"grep 'Total Magnetism' {os.path.join(input_dir, 'OUT.' + abacus_suffix, 'running_scf.log')}"
35+
datas = os.popen(cmd).read().strip().splitlines()
36+
if datas:
37+
for index, data in enumerate(datas):
38+
element_str = data.split()[4]
39+
x, y, z = map(float, data.split('(')[-1].split(')')[0].split(','))
40+
vector = np.array([x, y, z])
41+
r = np.linalg.norm(vector)
42+
theta = np.degrees(np.arctan2(vector[1], vector[0]))
43+
phi = np.degrees(np.arccos(vector[2] / r))
44+
magmom_data[index] = int(element_str in mag_element), r, theta, phi
45+
else: # using STRU file magmom setting, THIS MAY CAUSE WRONG OUTPUT!
46+
index_atom = 0
47+
with open(os.path.join(input_dir, "STRU"), 'r') as file:
48+
lines = file.readlines()
49+
for k in range(len(lines)): # k = line index
50+
if lines[k].strip() == 'ATOMIC_POSITIONS':
51+
kk = k + 2 # kk = current line index
52+
while kk < len(lines):
53+
if lines[kk] == "\n": # for if empty line between two elements, as ABACUS accepts
54+
continue
55+
element_str = lines[kk].strip()
56+
element_amount = int(lines[kk + 2].strip())
57+
for j in range(element_amount):
58+
line = lines[kk + 3 + j].strip().split()
59+
if len(line) < 11: # check if magmom is included
60+
raise ValueError('this line do not contain magmom: {} in this file: {}'.format(line, input_dir))
61+
if line[7] != "angle1" and line[8] != "angle1": # check if magmom is in angle mode
62+
raise ValueError('mag in STRU should be mag * angle1 * angle2 *')
63+
if line[6] == "mag": # for if 'm' is included
64+
index_str = 7
65+
else:
66+
index_str = 8
67+
magmom_data[index_atom] = int(element_str in mag_element), line[index_str], line[index_str + 2], line[index_str + 4]
68+
index_atom += 1
69+
kk += 3 + element_amount
5770

5871
np.savetxt(os.path.join(output_dir, "magmom.txt"), magmom_data)
5972

@@ -161,7 +174,7 @@ def worker(index):
161174
num_atom, eval(config.get('magnetic_moment', 'magnetic_element')))
162175
elif interface == 'abacus':
163176
collect_magmom_from_abacus(
164-
abspath, os.path.abspath(relpath),
177+
abspath, os.path.abspath(relpath), abacus_suffix,
165178
num_atom, eval(config.get('magnetic_moment', 'magnetic_element')))
166179
else:
167180
raise ValueError('Magnetic moment can only be parsed from OpenMX or ABACUS output for now, but your interface is {}'.format(interface))

0 commit comments

Comments
 (0)