Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a function that could deal with the magnetic_force and spin when using the ABACUS's deltaspin module #657

Closed
wants to merge 126 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
126 commits
Select commit Hold shift + click to select a range
d031d8a
Update scf.py
RenguangLiu May 15, 2024
2640f22
Update comp.py
RenguangLiu May 15, 2024
bda3de0
Create dependency_links.txt
RenguangLiu May 15, 2024
139dd20
Create PKG-INFO
RenguangLiu May 15, 2024
3ffb7c2
Revert "Create dependency_links.txt"
RenguangLiu May 15, 2024
3aecdd7
Revert "Create PKG-INFO"
RenguangLiu May 15, 2024
22b12f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
d69452e
Update scf.py
RenguangLiu May 15, 2024
f1b26b6
Update comp.py
RenguangLiu May 15, 2024
85f57fb
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 15, 2024
cea87b0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
17d3c22
Update scf.py
RenguangLiu May 15, 2024
dd308d0
Update comp.py
RenguangLiu May 15, 2024
9cb65c6
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 15, 2024
de64d1c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
ce3d4b3
Update scf.py
RenguangLiu May 15, 2024
9cd9d81
Update scf.py
RenguangLiu May 15, 2024
f179751
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
f9f4876
Update comp.py
RenguangLiu May 15, 2024
e1f02fd
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 15, 2024
11603d3
Update scf.py
RenguangLiu May 20, 2024
e292c83
Update system.py
RenguangLiu May 20, 2024
4e81bb6
Create dependency_links.txt
RenguangLiu May 20, 2024
ecd6c25
Create PKG-INFO
RenguangLiu May 20, 2024
0fdf461
Create SOURCES.txt
RenguangLiu May 20, 2024
0290fc7
Create top_level.txt
RenguangLiu May 20, 2024
f7340d8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2024
0d9fb2b
Delete dependency_links.txt
RenguangLiu May 20, 2024
49b5edb
Delete PKG-INFO
RenguangLiu May 20, 2024
4db5ccd
Delete SOURCES.txt
RenguangLiu May 20, 2024
5255275
Delete top_level.txt
RenguangLiu May 20, 2024
65231d4
Update system.py
RenguangLiu May 20, 2024
03df336
Merge branch 'devel' into my-fix-branch
RenguangLiu May 20, 2024
e252b51
update
RenguangLiu May 20, 2024
0fbe9c3
For rebasing to the devel
May 20, 2024
546c4b0
Update scf.py
RenguangLiu May 15, 2024
00fa468
Update comp.py
RenguangLiu May 15, 2024
2193bc1
Create dependency_links.txt
RenguangLiu May 15, 2024
be33874
Create PKG-INFO
RenguangLiu May 15, 2024
6df967d
Revert "Create dependency_links.txt"
RenguangLiu May 15, 2024
dd39ee5
Revert "Create PKG-INFO"
RenguangLiu May 15, 2024
e90c9f6
Update scf.py
RenguangLiu May 15, 2024
3572431
Update comp.py
RenguangLiu May 15, 2024
ca092cb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
3733cd7
Update scf.py
RenguangLiu May 15, 2024
bb34f0e
Update comp.py
RenguangLiu May 15, 2024
45ea1a7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
5c56724
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
d7f132a
Update scf.py
RenguangLiu May 15, 2024
db86cf9
Update scf.py
RenguangLiu May 15, 2024
7a0815b
Update comp.py
RenguangLiu May 15, 2024
7beb60d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
7404dcf
Update scf.py
RenguangLiu May 20, 2024
694a3c7
Update system.py
RenguangLiu May 20, 2024
575daca
Create dependency_links.txt
RenguangLiu May 20, 2024
28be0c4
Create PKG-INFO
RenguangLiu May 20, 2024
86bf7e5
Create SOURCES.txt
RenguangLiu May 20, 2024
13437a3
Create top_level.txt
RenguangLiu May 20, 2024
a055802
Delete dependency_links.txt
RenguangLiu May 20, 2024
4dc19f5
Delete PKG-INFO
RenguangLiu May 20, 2024
e87be36
Delete SOURCES.txt
RenguangLiu May 20, 2024
a7831a3
Delete top_level.txt
RenguangLiu May 20, 2024
df5cbde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2024
230887d
update
RenguangLiu May 20, 2024
fe24447
For rebasing to the devel
May 20, 2024
0e41ebb
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
May 20, 2024
9373272
Create dependency_links.txt
RenguangLiu May 15, 2024
2e35cd3
Create PKG-INFO
RenguangLiu May 15, 2024
cb82071
Revert "Create dependency_links.txt"
RenguangLiu May 15, 2024
e6d487d
Revert "Create PKG-INFO"
RenguangLiu May 15, 2024
f27d43a
Update scf.py
RenguangLiu May 15, 2024
36fbc2e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
472e8dd
Update scf.py
RenguangLiu May 15, 2024
d5c2acd
Update comp.py
RenguangLiu May 15, 2024
cf5583a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
e176e9e
Create dependency_links.txt
RenguangLiu May 20, 2024
bc8d037
Create PKG-INFO
RenguangLiu May 20, 2024
7646799
Create SOURCES.txt
RenguangLiu May 20, 2024
8fa43da
Create top_level.txt
RenguangLiu May 20, 2024
75e2c0c
Delete dependency_links.txt
RenguangLiu May 20, 2024
b88f24c
Delete PKG-INFO
RenguangLiu May 20, 2024
331e636
Delete SOURCES.txt
RenguangLiu May 20, 2024
48828c8
Delete top_level.txt
RenguangLiu May 20, 2024
081a2e5
update
RenguangLiu May 20, 2024
6bc6b33
For rebasing to the devel
May 20, 2024
71dacf5
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 20, 2024
929702b
Create dependency_links.txt
RenguangLiu May 15, 2024
6d592ae
Create PKG-INFO
RenguangLiu May 15, 2024
f5481f8
Revert "Create dependency_links.txt"
RenguangLiu May 15, 2024
d75684f
Revert "Create PKG-INFO"
RenguangLiu May 15, 2024
4d3ca3e
Update scf.py
RenguangLiu May 15, 2024
f294072
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
d893017
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
5bef166
Update scf.py
RenguangLiu May 15, 2024
e54bfc2
Update comp.py
RenguangLiu May 15, 2024
1e471fa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2024
5143f43
Update scf.py
RenguangLiu May 20, 2024
2411840
Create dependency_links.txt
RenguangLiu May 20, 2024
3bbec17
Create PKG-INFO
RenguangLiu May 20, 2024
5738c08
Create SOURCES.txt
RenguangLiu May 20, 2024
a26adae
Create top_level.txt
RenguangLiu May 20, 2024
4917e91
Delete dependency_links.txt
RenguangLiu May 20, 2024
0451d81
Delete PKG-INFO
RenguangLiu May 20, 2024
99c44e2
Delete SOURCES.txt
RenguangLiu May 20, 2024
3b5266c
Delete top_level.txt
RenguangLiu May 20, 2024
1acafd4
For rebasing to the devel
May 20, 2024
43aabf8
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 20, 2024
814a6a9
add the deltaspin test unit
RenguangLiu May 20, 2024
1a49e2c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2024
303f63b
Update test_abacus_deltaspin.py
RenguangLiu May 20, 2024
fe844e3
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 20, 2024
569e58a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2024
48480a4
Update scf.py
RenguangLiu May 20, 2024
29c4b76
Update system.py
RenguangLiu May 20, 2024
fbfd3f7
Update test_abacus_deltaspin.py
RenguangLiu May 20, 2024
cdb3849
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 20, 2024
fdc5d3f
Update test_abacus_deltaspin.py
RenguangLiu May 20, 2024
37795f5
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 20, 2024
32d2fe9
unittest for deltaspin
RenguangLiu May 21, 2024
0e27ad1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 21, 2024
ad1623f
Update test_abacus_deltaspin.py
RenguangLiu May 21, 2024
96c74a2
Merge branch 'my-fix-branch' of https://github.com/flinky-ai/dpdata i…
RenguangLiu May 21, 2024
12dcb8f
Merge branch 'devel' into my-fix-branch
njzjz May 22, 2024
42452af
change into LF
RenguangLiu May 23, 2024
7ee3f1e
Normalize all the line endings
May 23, 2024
361b293
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions dpdata/abacus/scf.py
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,44 @@ def get_energy(outlines):
return Etot, False


def get_magnetic(outlines, natoms):
"""
Extracts magnetic moments from the output lines.

Parameters
----------
outlines (list of str) : The lines from the output file.
natoms (int) : The number of atoms.

Returns
-------
np.ndarray: An array of magnetic moments.
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
"""
Mag = []

# 定义正则表达式以匹配所需的数字
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
mag_pattern = re.compile(
r"Total Magnetism on atom:.*\(([\d.\-]+),\s*([\d.\-]+),\s*([\d.\-]+)\)"
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
)

for line in outlines:
# 使用正则表达式匹配磁性信息
match = mag_pattern.search(line)
if match:
# 提取匹配的数字并转换为浮点数,然后将元组转换为列表
Mag.append([float(num) for num in match.groups()])
# 如果Mag的长度达到了natoms,说明已经收集完所有原子的磁矩
if len(Mag) == natoms:
break

# 如果没有找到任何磁矩信息,返回一个空的NumPy数组
if len(Mag) == 0:
return np.array([[]])
else:
# 将Mag转换为NumPy数组并返回
return np.array(Mag)


RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
def collect_force(outlines):
force = []
for i, line in enumerate(outlines):
Expand Down Expand Up @@ -193,6 +231,65 @@ def get_force(outlines, natoms):
return np.array(force[-1]) # only return the last force


def collect_mag_force(outlines):
"""
Collects magnetic forces from the output lines.

Parameters
----------
outlines : The lines from the output file.

Returns
-------
list: A list of magnetic forces.
"""
mag_force = []
for i, line in enumerate(outlines):
if "Magnetic force (Ry/uB)" in line:
value_pattern = re.compile(
r"^\s*ATOM\s+(\d+)\s+[-+]?[0-9]+([eE][-+]?[0-9]+)?\s+[-+]?[0-9]+([eE][-+]?[0-9]+)?\s+[-+]?[0-9]+([eE][-+]?[0-9]+)?\s*$"
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
)
j = i
# find the first line of force
noforce = False
while not value_pattern.match(outlines[j]):
j += 1
if (
j >= i + 10
): # if can not find the first line of force in 10 lines, then stop
warnings.warn("Warning: can not find the first line of force")
noforce = True
break
if noforce:
break

mag_force.append([])
while value_pattern.match(outlines[j]):
mag_force[-1].append([float(ii) for ii in outlines[j].split()[1:4]])
j += 1
return mag_force # only return the last force


def get_mag_force(outlines, natoms):
"""
Extracts magnetic forces from the output lines.

Parameters
----------
outlines (list of str) : The lines from the output file.
natoms (int) : The number of atoms.

Returns
-------
np.ndarray: An array of magnetic forces.
"""
mag_force = collect_mag_force(outlines)
if len(mag_force) == 0:
return [[]]
else:
return np.array(mag_force[-1]) # only return the last force


def collect_stress(outlines):
stress = []
for i, line in enumerate(outlines):
Expand Down Expand Up @@ -230,6 +327,34 @@ def get_stress(outlines):
return np.array(stress[-1]) * kbar2evperang3 # only return the last stress


def check_deltaspin(path_in):
"""
Checks if the deltaspin feature is enabled in the input file.

Parameters
----------
path_in : The path to the input file.

Returns
-------
bool: True if deltaspin is enabled, None otherwise.
"""
try:
with open(path_in) as file:
for line in file:
# 移除行首尾的空白字符,然后以空格分割
parts = line.strip().split()
# 检查是否是sc_mag_switch参数行
if len(parts) >= 2 and parts[0] == "sc_mag_switch":
# 检查参数值是否为1
return True if parts[1] == "1" else None
# 文件已读完,没有找到sc_mag_switch参数,返回None
return None
except FileNotFoundError:
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
print(f"File not found: {path_in}")
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
return None


def get_frame(fname):
data = {
"atom_names": [],
Expand All @@ -239,6 +364,11 @@ def get_frame(fname):
"coords": [],
"energies": [],
"forces": [],
"spin": [],
"mag_forces": [],
"coords_deltaspin": [],
"force_deltaspin": [],
"deltaspin": [],
}

if isinstance(fname, str):
Expand All @@ -256,6 +386,7 @@ def get_frame(fname):

geometry_path_in = get_geometry_in(fname, inlines)
path_out = get_path_out(fname, inlines)
deltaspin = check_deltaspin(path_in)
if not (CheckFile(geometry_path_in) and CheckFile(path_out)):
return data

Expand All @@ -280,10 +411,36 @@ def get_frame(fname):
if stress is not None:
stress *= np.abs(np.linalg.det(cell))

if deltaspin is not None:
spin = get_magnetic(outlines, natoms)
sp_norm = 1.49
virtual_len = 0.3
mag_forces = get_mag_force(outlines, natoms)
coords_deltaspin = np.hstack((coords, coords - spin / sp_norm * virtual_len))
force_deltaspin = np.hstack((force, mag_forces))
else:
mag_forces = None
coords_deltaspin = None
force_deltaspin = None

# print(force_deltaspin)
# print(coords_deltaspin)
# print(spin)
# print(mag_forces)
# print(force)
# print(coords)
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved

data["cells"] = cell[np.newaxis, :, :]
data["coords"] = coords[np.newaxis, :, :]
data["energies"] = np.array(energy)[np.newaxis]
data["forces"] = force[np.newaxis, :, :]
if deltaspin is not None:
data["mag_forces"] = mag_forces[np.newaxis, :, :]
data["spin"] = spin[np.newaxis, :, :]
data["coords_deltaspin"] = coords_deltaspin[np.newaxis, :, :]
data["force_deltaspin"] = force_deltaspin[np.newaxis, :, :]
data["deltaspin"] = deltaspin
RenguangLiu marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +380 to +384
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, these data types should be added to DTYPES, otherwise I don't think indexing, slicing, or appending will work.

dpdata/dpdata/system.py

Lines 1193 to 1198 in 1d87e82

DTYPES: tuple[DataType, ...] = System.DTYPES + (
DataType("energies", np.ndarray, (Axis.NFRAMES,)),
DataType("forces", np.ndarray, (Axis.NFRAMES, Axis.NATOMS, 3)),
DataType("virials", np.ndarray, (Axis.NFRAMES, 3, 3), required=False),
DataType("atom_pref", np.ndarray, (Axis.NFRAMES, Axis.NATOMS), required=False),
)


if stress is not None:
data["virials"] = stress[np.newaxis, :, :]
data["orig"] = np.zeros(3)
Expand All @@ -295,6 +452,10 @@ def get_frame(fname):
# print("energy = ", data['energies'])
# print("force = ", data['forces'])
# print("virial = ", data['virials'])
# print("spin = ", data['spin'])
# print("mag_forces = ", data['mag_forces'])
# print("force_deltaspin = ", data['force_deltaspin'])
# print("coords_deltaspin = ", data['coords_deltaspin'])
Comment on lines +397 to +400
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented codes should be removed

return data


Expand Down
28 changes: 26 additions & 2 deletions dpdata/deepmd/comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,24 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True):
nframes = data["cells"].shape[0]
cells = np.reshape(data["cells"], [nframes, 9]).astype(comp_prec)
coords = np.reshape(data["coords"], [nframes, -1]).astype(comp_prec)
if "coords_deltaspin" in data:
coords_deltaspin = np.reshape(data["coords_deltaspin"], [nframes, -1]).astype(
comp_prec
)
eners = None
forces = None
virials = None
force_deltaspin = None
if "deltaspin" in data:
deltaspin = data["deltaspin"]
if "energies" in data:
eners = np.reshape(data["energies"], [nframes]).astype(comp_prec)
if "forces" in data:
forces = np.reshape(data["forces"], [nframes, -1]).astype(comp_prec)
if "force_deltaspin" in data:
force_deltaspin = np.reshape(data["force_deltaspin"], [nframes, -1]).astype(
comp_prec
)
if "virials" in data:
virials = np.reshape(data["virials"], [nframes, 9]).astype(comp_prec)
if "atom_pref" in data:
Expand All @@ -154,11 +165,21 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True):
set_folder = os.path.join(folder, "set.%03d" % ii)
os.makedirs(set_folder)
np.save(os.path.join(set_folder, "box"), cells[set_stt:set_end])
np.save(os.path.join(set_folder, "coord"), coords[set_stt:set_end])
if deltaspin is not None:
np.save(
os.path.join(set_folder, "coord"), coords_deltaspin[set_stt:set_end]
)
else:
np.save(os.path.join(set_folder, "coord"), coords[set_stt:set_end])
if eners is not None:
np.save(os.path.join(set_folder, "energy"), eners[set_stt:set_end])
if forces is not None:
np.save(os.path.join(set_folder, "force"), forces[set_stt:set_end])
if deltaspin is not None:
np.save(
os.path.join(set_folder, "force"), force_deltaspin[set_stt:set_end]
)
else:
np.save(os.path.join(set_folder, "force"), forces[set_stt:set_end])
if virials is not None:
np.save(os.path.join(set_folder, "virial"), virials[set_stt:set_end])
if "atom_pref" in data:
Expand All @@ -185,6 +206,9 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True):
"energies",
"forces",
"virials",
"force_deltaspin",
"coords_deltaspin",
"deltaspin",
):
# skip as these data contains specific rules
continue
Expand Down
Loading