Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 20, 2024
1 parent 614fde7 commit 4339232
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 45 deletions.
13 changes: 8 additions & 5 deletions dpdata/lammps/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def safe_get_posi(lines, cell, orig=np.zeros(3), unwrap=False):


def get_spintype(keys):
key_sp = ["sp", "spx", "spy", "spz"]
key_sp = ["sp", "spx", "spy", "spz"]
key_csp = ["c_spin[1]", "c_spin[2]", "c_spin[3]", "c_spin[4]"]
lmp_sp_type = [key_sp, key_csp]
for k in range(2):
Expand All @@ -147,7 +147,7 @@ def safe_get_spin_force(lines):
sp_type = get_spintype(keys)
assert sp_type is not None, "Dump file does not contain spin!"
id_idx = keys.index("id") - 2
sp = keys.index(sp_type[0]) - 2
sp = keys.index(sp_type[0]) - 2
spx = keys.index(sp_type[1]) - 2
spy = keys.index(sp_type[2]) - 2
spz = keys.index(sp_type[3]) - 2
Expand Down Expand Up @@ -254,10 +254,10 @@ def system_data(lines, type_map=None, type_idx_zero=True, unwrap=False):
system["coords"] = [safe_get_posi(lines, cell, np.array(orig), unwrap)]
contain_spin = False
blk, head = _get_block(lines, "ATOMS")
if 'sp' in head:
if "sp" in head:
contain_spin = True
spin_force = safe_get_spin_force(lines)
system['spins'] = [spin_force[:, :1] * spin_force[:, 1:4]]
system["spins"] = [spin_force[:, :1] * spin_force[:, 1:4]]

Check warning on line 260 in dpdata/lammps/dump.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/dump.py#L258-L260

Added lines #L258 - L260 were not covered by tests
for ii in range(1, len(array_lines)):
bounds, tilt = get_dumpbox(array_lines[ii])
orig, cell = dumpbox2box(bounds, tilt)
Expand All @@ -271,7 +271,10 @@ def system_data(lines, type_map=None, type_idx_zero=True, unwrap=False):
safe_get_posi(array_lines[ii], cell, np.array(orig), unwrap)[idx]
)
if contain_spin:
system['spins'].append(safe_get_spin_force(array_lines[ii])[:, :1] * safe_get_spin_force(array_lines[ii])[:, 1:4])
system["spins"].append(

Check warning on line 274 in dpdata/lammps/dump.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/dump.py#L274

Added line #L274 was not covered by tests
safe_get_spin_force(array_lines[ii])[:, :1]
* safe_get_spin_force(array_lines[ii])[:, 1:4]
)
if contain_spin:
system["spins"] = np.array(system["spins"])

Check warning on line 279 in dpdata/lammps/dump.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/dump.py#L279

Added line #L279 was not covered by tests
system["cells"] = np.array(system["cells"])
Expand Down
26 changes: 14 additions & 12 deletions dpdata/lammps/lmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def get_posi(lines):
posis.append([float(jj) for jj in ii.split()[2:5]])
return np.array(posis)


def get_spins(lines):
atom_lines = get_atoms(lines)
if len(atom_lines[0].split()) < 8:
Expand All @@ -138,6 +139,7 @@ def get_spins(lines):
spins = np.array(spins_ori) * np.array(spins_norm)
return spins

Check warning on line 140 in dpdata/lammps/lmp.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/lmp.py#L134-L140

Added lines #L134 - L140 were not covered by tests


def get_lmpbox(lines):
box_info = []
tilt = np.zeros(3)
Expand All @@ -163,8 +165,8 @@ def system_data(lines, type_map=None, type_idx_zero=True):
system = {}
system["atom_numbs"] = get_natoms_vec(lines)
spins = get_spins(lines)
if spins is not None:
system['spins'] = np.array([spins])
if spins is not None:
system["spins"] = np.array([spins])

Check warning on line 169 in dpdata/lammps/lmp.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/lmp.py#L169

Added line #L169 was not covered by tests
system["atom_names"] = []
if type_map is None:
for ii in range(len(system["atom_numbs"])):
Expand Down Expand Up @@ -230,9 +232,9 @@ def from_system_data(system, f_idx=0):
+ ptr_float_fmt
+ "\n"
)
if 'spins' in system.keys():
if "spins" in system.keys():
coord_fmt = (

Check warning on line 236 in dpdata/lammps/lmp.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/lmp.py#L236

Added line #L236 was not covered by tests
coord_fmt.strip('\n')
coord_fmt.strip("\n")
+ " "
+ ptr_float_fmt
+ " "
Expand All @@ -243,19 +245,19 @@ def from_system_data(system, f_idx=0):
+ ptr_float_fmt
+ "\n"
)
spins_norm = np.linalg.norm(system['spins'][f_idx], axis=1)
spins_norm = np.linalg.norm(system["spins"][f_idx], axis=1)

Check warning on line 248 in dpdata/lammps/lmp.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/lmp.py#L248

Added line #L248 was not covered by tests
for ii in range(natoms):
if 'spins' in system.keys():
if "spins" in system.keys():
if spins_norm[ii] != 0:
ret += coord_fmt % (

Check warning on line 252 in dpdata/lammps/lmp.py

View check run for this annotation

Codecov / codecov/patch

dpdata/lammps/lmp.py#L251-L252

Added lines #L251 - L252 were not covered by tests
ii + 1,
system["atom_types"][ii] + 1,
system["coords"][f_idx][ii][0] - system["orig"][0],
system["coords"][f_idx][ii][1] - system["orig"][1],
system["coords"][f_idx][ii][2] - system["orig"][2],
system['spins'][f_idx][ii][0]/spins_norm[ii],
system['spins'][f_idx][ii][1]/spins_norm[ii],
system['spins'][f_idx][ii][2]/spins_norm[ii],
system["spins"][f_idx][ii][0] / spins_norm[ii],
system["spins"][f_idx][ii][1] / spins_norm[ii],
system["spins"][f_idx][ii][2] / spins_norm[ii],
spins_norm[ii],
)
else:
Expand All @@ -265,9 +267,9 @@ def from_system_data(system, f_idx=0):
system["coords"][f_idx][ii][0] - system["orig"][0],
system["coords"][f_idx][ii][1] - system["orig"][1],
system["coords"][f_idx][ii][2] - system["orig"][2],
system['spins'][f_idx][ii][0],
system['spins'][f_idx][ii][1],
system['spins'][f_idx][ii][2]+1,
system["spins"][f_idx][ii][0],
system["spins"][f_idx][ii][1],
system["spins"][f_idx][ii][2] + 1,
spins_norm[ii],
)
else:
Expand Down
21 changes: 12 additions & 9 deletions dpdata/plugins/vasp_deltaspin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np
from __future__ import annotations

import re

import numpy as np

import dpdata.vasp_deltaspin.outcar
import dpdata.vasp_deltaspin.poscar
from dpdata.format import Format
Expand All @@ -14,7 +17,7 @@ class VASPPoscarFormat(Format):
def from_system(self, file_name, **kwargs):
with open(file_name) as fp:
lines = [line.rstrip("\n") for line in fp]
with open(file_name[:-6] + 'INCAR') as fp:
with open(file_name[:-6] + "INCAR") as fp:
lines_incar = [line.rstrip("\n") for line in fp]
data = dpdata.vasp_deltaspin.poscar.to_system_data(lines, lines_incar)
data = uniq_atom_names(data)
Expand All @@ -38,10 +41,12 @@ def to_system(self, data, file_name, frame_idx=0, **kwargs):
with open(file_name, "w") as fp:
fp.write(w_str)

Check warning on line 42 in dpdata/plugins/vasp_deltaspin.py

View check run for this annotation

Codecov / codecov/patch

dpdata/plugins/vasp_deltaspin.py#L40-L42

Added lines #L40 - L42 were not covered by tests

with open(file_name[:-6] + 'INCAR') as fp:
with open(file_name[:-6] + "INCAR") as fp:
tmp_incar = fp.read()
res_incar = re.sub(r'MAGMOM[\s\S]*?\n\nM_CONST[\s\S]*?\n\n', m_str, tmp_incar, re.S)
with open(file_name[:-6] + 'INCAR', 'w') as fp:
res_incar = re.sub(

Check warning on line 46 in dpdata/plugins/vasp_deltaspin.py

View check run for this annotation

Codecov / codecov/patch

dpdata/plugins/vasp_deltaspin.py#L44-L46

Added lines #L44 - L46 were not covered by tests
r"MAGMOM[\s\S]*?\n\nM_CONST[\s\S]*?\n\n", m_str, tmp_incar, re.S
)
with open(file_name[:-6] + "INCAR", "w") as fp:
fp.write(res_incar)

Check warning on line 50 in dpdata/plugins/vasp_deltaspin.py

View check run for this annotation

Codecov / codecov/patch

dpdata/plugins/vasp_deltaspin.py#L49-L50

Added lines #L49 - L50 were not covered by tests


Expand Down Expand Up @@ -78,10 +83,10 @@ def from_labeled_system(
data["atom_types"],
data["cells"],
data["coords"],
data['spins'],
data["spins"],
data["energies"],
data["forces"],
data['mag_forces'],
data["mag_forces"],
tmp_virial,
) = dpdata.vasp_deltaspin.outcar.get_frames(
file_name,
Expand All @@ -100,5 +105,3 @@ def from_labeled_system(
data["virials"][ii] *= v_pref * vol
data = uniq_atom_names(data)
return data

Check warning on line 107 in dpdata/plugins/vasp_deltaspin.py

View check run for this annotation

Codecov / codecov/patch

dpdata/plugins/vasp_deltaspin.py#L101-L107

Added lines #L101 - L107 were not covered by tests


4 changes: 3 additions & 1 deletion dpdata/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,9 @@ class LabeledSystem(System):
DataType(
"forces", np.ndarray, (Axis.NFRAMES, Axis.NATOMS, 3), deepmd_name="force"
),
DataType("mag_forces", np.ndarray, (Axis.NFRAMES, Axis.NATOMS, 3), required=False),
DataType(
"mag_forces", np.ndarray, (Axis.NFRAMES, Axis.NATOMS, 3), required=False
),
DataType(
"virials",
np.ndarray,
Expand Down
10 changes: 7 additions & 3 deletions dpdata/vasp_deltaspin/outcar.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
import warnings

Expand Down Expand Up @@ -93,11 +95,13 @@ def get_frames(fname, begin=0, step=1, ml=False, convergence_check=True):
blk = get_outcar_block(fp, ml)
cc += 1

Check warning on line 96 in dpdata/vasp_deltaspin/outcar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/outcar.py#L95-L96

Added lines #L95 - L96 were not covered by tests

with open(fname[:-6] + 'OSZICAR') as f:
with open(fname[:-6] + "OSZICAR") as f:
oszicar_blk = f.read()
mag_blk = re.findall(r'Magnetic Force(.*?)\n \n', oszicar_blk, re.S)[-1].split('\n')
mag_blk = re.findall(r"Magnetic Force(.*?)\n \n", oszicar_blk, re.S)[-1].split(

Check warning on line 100 in dpdata/vasp_deltaspin/outcar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/outcar.py#L98-L100

Added lines #L98 - L100 were not covered by tests
"\n"
)
mag_force = [list(map(float, ss.split()[-3:])) for ss in mag_blk[1:]]
spin_blk = re.findall(r'MW_current(.*?)\n ion', oszicar_blk, re.S)[-1].split('\n')
spin_blk = re.findall(r"MW_current(.*?)\n ion", oszicar_blk, re.S)[-1].split("\n")
spin = [list(map(float, ss.split()[1:4])) for ss in spin_blk[1:]]
all_mag_forces.append(mag_force)
all_spins.append(spin)

Check warning on line 107 in dpdata/vasp_deltaspin/outcar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/outcar.py#L103-L107

Added lines #L103 - L107 were not covered by tests
Expand Down
35 changes: 20 additions & 15 deletions dpdata/vasp_deltaspin/poscar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/python3
from __future__ import annotations

import numpy as np

Expand Down Expand Up @@ -35,16 +36,16 @@ def _to_system_data_lower(lines, lines_incar, cartesian=True):
system["coords"] = np.array(system["coords"])

Check warning on line 36 in dpdata/vasp_deltaspin/poscar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/poscar.py#L26-L36

Added lines #L26 - L36 were not covered by tests

for idx, incar_param in enumerate(lines_incar):
if 'MAGMOM' in incar_param:
if "MAGMOM" in incar_param:
start_index = idx
elif 'M_CONSTR' in incar_param:
elif "M_CONSTR" in incar_param:
end_index = idx
system['spins'] = [lines_incar[start_index].replace('=', '').strip().split()[1:4]]
for idx in range(start_index+1, end_index-1):
system['spins'].append(lines_incar[idx].strip().split()[:3])
system['spins'] = np.array([system['spins']]).astype('float64')
count = np.sum(np.linalg.norm(system['spins'][0], axis=1) > 0)
spins_idx = np.where(np.cumsum(system['atom_numbs']) <= count)
system["spins"] = [lines_incar[start_index].replace("=", "").strip().split()[1:4]]
for idx in range(start_index + 1, end_index - 1):
system["spins"].append(lines_incar[idx].strip().split()[:3])
system["spins"] = np.array([system["spins"]]).astype("float64")
count = np.sum(np.linalg.norm(system["spins"][0], axis=1) > 0)
spins_idx = np.where(np.cumsum(system["atom_numbs"]) <= count)
return system

Check warning on line 49 in dpdata/vasp_deltaspin/poscar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/poscar.py#L38-L49

Added lines #L38 - L49 were not covered by tests


Expand Down Expand Up @@ -100,16 +101,20 @@ def from_system_data(system, f_idx=0, skip_zeros=True):

magmom_incar = "MAGMOM = "
mconstr_incar = "M_CONSTR = "
for idx, tmp_spin in enumerate(system['spins'][f_idx]):
for idx, tmp_spin in enumerate(system["spins"][f_idx]):
if idx == 0:
magmom_incar += f"{tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \ \n"
mconstr_incar += f"{tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \ \n"
elif idx == len(system['spins'][f_idx]) - 1:
magmom_incar += f" {tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \n"
magmom_incar += (

Check warning on line 106 in dpdata/vasp_deltaspin/poscar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/poscar.py#L102-L106

Added lines #L102 - L106 were not covered by tests
f"{tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \ \n"
)
mconstr_incar += (

Check warning on line 109 in dpdata/vasp_deltaspin/poscar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/poscar.py#L109

Added line #L109 was not covered by tests
f"{tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \ \n"
)
elif idx == len(system["spins"][f_idx]) - 1:
magmom_incar += f" {tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \n"
mconstr_incar += f" {tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \n"

Check warning on line 114 in dpdata/vasp_deltaspin/poscar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/poscar.py#L112-L114

Added lines #L112 - L114 were not covered by tests
else:
magmom_incar += f" {tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \ \n"
magmom_incar += f" {tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \ \n"
mconstr_incar += f" {tmp_spin[0]:10.5f} {tmp_spin[1]:10.5f} {tmp_spin[2]:10.5f} \ \n"
magmom_incar += '\n' + mconstr_incar + '\n'
magmom_incar += "\n" + mconstr_incar + "\n"

Check warning on line 118 in dpdata/vasp_deltaspin/poscar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/poscar.py#L116-L118

Added lines #L116 - L118 were not covered by tests

return ret, magmom_incar

Check warning on line 120 in dpdata/vasp_deltaspin/poscar.py

View check run for this annotation

Codecov / codecov/patch

dpdata/vasp_deltaspin/poscar.py#L120

Added line #L120 was not covered by tests

0 comments on commit 4339232

Please sign in to comment.