Skip to content

Commit

Permalink
change related variable name from 'spin' to 'spins'
Browse files Browse the repository at this point in the history
  • Loading branch information
yangtengleo committed Sep 9, 2024
1 parent d39e1f1 commit 6233b8f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 54 deletions.
28 changes: 14 additions & 14 deletions dpdata/deepmd/comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def _load_set(folder, nopbc: bool):
forces = _cond_load_data(os.path.join(folder, "force.npy"))
mag_forces = _cond_load_data(os.path.join(folder, "force_mag.npy"))
virs = _cond_load_data(os.path.join(folder, "virial.npy"))
spin = _cond_load_data(os.path.join(folder, "spin.npy"))
return cells, coords, eners, forces, virs, mag_forces, spin
spins = _cond_load_data(os.path.join(folder, "spin.npy"))
return cells, coords, eners, forces, virs, mag_forces, spins


def to_system_data(folder, type_map=None, labels=True):
Expand All @@ -47,14 +47,14 @@ def to_system_data(folder, type_map=None, labels=True):
all_forces = []
all_mag_forces = []
all_virs = []
all_spin = []
all_spins = []
for ii in sets:
cells, coords, eners, forces, virs, mag_forces, spin = _load_set(ii, data.get("nopbc", False))
cells, coords, eners, forces, virs, mag_forces, spins = _load_set(ii, data.get("nopbc", False))
nframes = np.reshape(cells, [-1, 3, 3]).shape[0]
all_cells.append(np.reshape(cells, [nframes, 3, 3]))
all_coords.append(np.reshape(coords, [nframes, -1, 3]))
if spin is not None:
all_spin.append(np.reshape(spin, [nframes, -1, 3]))
if spins is not None:
all_spins.append(np.reshape(spins, [nframes, -1, 3]))
if eners is not None:
eners = np.reshape(eners, [nframes])
if labels:
Expand All @@ -68,8 +68,8 @@ def to_system_data(folder, type_map=None, labels=True):
all_mag_forces.append(np.reshape(mag_forces, [nframes, -1, 3]))
data["cells"] = np.concatenate(all_cells, axis=0)
data["coords"] = np.concatenate(all_coords, axis=0)
if len(all_spin) > 0:
data["spin"] = np.concatenate(all_spin, axis=0)
if len(all_spins) > 0:
data["spins"] = np.concatenate(all_spins, axis=0)
if len(all_eners) > 0:
data["energies"] = np.concatenate(all_eners, axis=0)
if len(all_forces) > 0:
Expand Down Expand Up @@ -156,7 +156,7 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True):
forces = None
virials = None
mag_forces = None
spin = None
spins = None
if "energies" in data:
eners = np.reshape(data["energies"], [nframes]).astype(comp_prec)
if "forces" in data:
Expand All @@ -167,8 +167,8 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True):
atom_pref = np.reshape(data["atom_pref"], [nframes, -1]).astype(comp_prec)
if "mag_forces" in data:
mag_forces = np.reshape(data["mag_forces"], [nframes, -1]).astype(comp_prec)
if "spin" in data:
spin = np.reshape(data["spin"], [nframes, -1]).astype(comp_prec)
if "spins" in data:
spins = np.reshape(data["spins"], [nframes, -1]).astype(comp_prec)
# dump frame properties: cell, coord, energy, force and virial
nsets = nframes // set_size
if set_size * nsets < nframes:
Expand All @@ -190,8 +190,8 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True):
np.save(os.path.join(set_folder, "atom_pref"), atom_pref[set_stt:set_end])
if mag_forces is not None:
np.save(os.path.join(set_folder, "force_mag"), mag_forces[set_stt:set_end])
if spin is not None:
np.save(os.path.join(set_folder, "spin"), spin[set_stt:set_end])
if spins is not None:
np.save(os.path.join(set_folder, "spins"), spins[set_stt:set_end])
try:
os.remove(os.path.join(folder, "nopbc"))
except OSError:
Expand Down Expand Up @@ -219,7 +219,7 @@ def dump(folder, data, set_size=5000, comp_prec=np.float32, remove_sets=True):
"forces",
"virials",
"mag_forces",
"spin"
"spins"
):
# skip as these data contains specific rules
continue
Expand Down
10 changes: 5 additions & 5 deletions dpdata/deepmd/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def to_system_data(folder, type_map=None, labels=True):
data["virials"] = np.loadtxt(os.path.join(folder, "virial.raw"))
data["virials"] = np.reshape(data["virials"], [nframes, 3, 3])
if os.path.exists(os.path.join(folder, "spin.raw")):
data["spin"] = np.loadtxt(os.path.join(folder, "spin.raw"))
data["spin"] = np.reshape(data["spin"], [nframes, -1, 3])
data["spins"] = np.loadtxt(os.path.join(folder, "spin.raw"))
data["spins"] = np.reshape(data["spins"], [nframes, -1, 3])
if os.path.exists(os.path.join(folder, "force_mag.raw")):
data["mag_forces"] = np.loadtxt(os.path.join(folder, "force_mag.raw"))
data["mag_forces"] = np.reshape(data["mag_forces"], [nframes, -1, 3])
Expand Down Expand Up @@ -144,10 +144,10 @@ def dump(folder, data):
os.path.join(folder, "virial.raw"),
np.reshape(data["virials"], [nframes, 9]),
)
if "spin" in data:
if "spins" in data:
np.savetxt(
os.path.join(folder, "spin.raw"),
np.reshape(data["spin"], [nframes, -1]),
np.reshape(data["spins"], [nframes, -1]),
)
if "mag_forces" in data:
np.savetxt(
Expand Down Expand Up @@ -182,7 +182,7 @@ def dump(folder, data):
"forces",
"virials",
"mag_forces",
"spin"
"spins"
):
# skip as these data contains specific rules
continue
Expand Down
6 changes: 3 additions & 3 deletions dpdata/lammps/dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def system_data(lines, type_map=None, type_idx_zero=True, unwrap=False):
if 'sp' in head:
contain_spin = True
spin_force = safe_get_spin_force(lines)
system['spin'] = [spin_force[:, :1] * spin_force[:, 1:4]]
system['spins'] = [spin_force[:, :1] * spin_force[:, 1:4]]
for ii in range(1, len(array_lines)):
bounds, tilt = get_dumpbox(array_lines[ii])
orig, cell = dumpbox2box(bounds, tilt)
Expand All @@ -271,9 +271,9 @@ 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['spin'].append(safe_get_spin_force(array_lines[ii])[:, :1] * safe_get_spin_force(array_lines[ii])[:, 1:4])
system['spins'].append(safe_get_spin_force(array_lines[ii])[:, :1] * safe_get_spin_force(array_lines[ii])[:, 1:4])
if contain_spin:
system["spin"] = np.array(system["spin"])
system["spins"] = np.array(system["spins"])
system["cells"] = np.array(system["cells"])
system["coords"] = np.array(system["coords"])
return system
Expand Down
44 changes: 22 additions & 22 deletions dpdata/lammps/lmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,17 @@ def get_posi(lines):
posis.append([float(jj) for jj in ii.split()[2:5]])
return np.array(posis)

def get_spin(lines):
def get_spins(lines):
atom_lines = get_atoms(lines)
if len(atom_lines[0].split()) < 8:
return None
spin_ori = []
spin_norm = []
spins_ori = []
spins_norm = []
for ii in atom_lines:
spin_ori.append([float(jj) for jj in ii.split()[5:8]])
spin_norm.append([float(jj) for jj in ii.split()[-1:]])
spin = np.array(spin_ori) * np.array(spin_norm)
return spin
spins_ori.append([float(jj) for jj in ii.split()[5:8]])
spins_norm.append([float(jj) for jj in ii.split()[-1:]])
spins = np.array(spins_ori) * np.array(spins_norm)
return spins

def get_lmpbox(lines):
box_info = []
Expand All @@ -162,9 +162,9 @@ def get_lmpbox(lines):
def system_data(lines, type_map=None, type_idx_zero=True):
system = {}
system["atom_numbs"] = get_natoms_vec(lines)
spin = get_spin(lines)
if spin is not None:
system['spin'] = np.array([spin])
spins = get_spins(lines)
if spins is not None:
system['spins'] = np.array([spins])
system["atom_names"] = []
if type_map is None:
for ii in range(len(system["atom_numbs"])):
Expand Down Expand Up @@ -230,7 +230,7 @@ def from_system_data(system, f_idx=0):
+ ptr_float_fmt
+ "\n"
)
if 'spin' in system.keys():
if 'spins' in system.keys():
coord_fmt = (
coord_fmt.strip('\n')
+ " "
Expand All @@ -243,20 +243,20 @@ def from_system_data(system, f_idx=0):
+ ptr_float_fmt
+ "\n"
)
spin_norm = np.linalg.norm(system['spin'][f_idx], axis=1)
spins_norm = np.linalg.norm(system['spins'][f_idx], axis=1)
for ii in range(natoms):
if 'spin' in system.keys():
if spin_norm[ii] != 0:
if 'spins' in system.keys():
if spins_norm[ii] != 0:
ret += coord_fmt % (
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['spin'][f_idx][ii][0]/spin_norm[ii],
system['spin'][f_idx][ii][1]/spin_norm[ii],
system['spin'][f_idx][ii][2]/spin_norm[ii],
spin_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:
ret += coord_fmt % (
Expand All @@ -265,10 +265,10 @@ 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['spin'][f_idx][ii][0],
system['spin'][f_idx][ii][1],
system['spin'][f_idx][ii][2]+1,
spin_norm[ii],
system['spins'][f_idx][ii][0],
system['spins'][f_idx][ii][1],
system['spins'][f_idx][ii][2]+1,
spins_norm[ii],
)
else:
ret += coord_fmt % (
Expand Down
2 changes: 1 addition & 1 deletion dpdata/plugins/vasp_deltaspin.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def from_labeled_system(
data["atom_types"],
data["cells"],
data["coords"],
data['spin'],
data['spins'],
data["energies"],
data["forces"],
data['mag_forces'],
Expand Down
4 changes: 2 additions & 2 deletions dpdata/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class System:
DataType("orig", np.ndarray, (3,)),
DataType("cells", np.ndarray, (Axis.NFRAMES, 3, 3)),
DataType("coords", np.ndarray, (Axis.NFRAMES, Axis.NATOMS, 3)),
DataType("spin", np.ndarray, (Axis.NFRAMES, Axis.NATOMS, 3), required=False),
DataType("spins", np.ndarray, (Axis.NFRAMES, Axis.NATOMS, 3), required=False),
DataType(
"real_atom_types", np.ndarray, (Axis.NFRAMES, Axis.NATOMS), required=False
),
Expand Down Expand Up @@ -712,7 +712,7 @@ def affine_map(self, trans, f_idx: int | numbers.Integral = 0):
self.data["cells"][f_idx] = np.matmul(self.data["cells"][f_idx], trans)
self.data["coords"][f_idx] = np.matmul(self.data["coords"][f_idx], trans)
try:
self.data["spin"][f_idx] = np.matmul(self.data["spin"][f_idx], trans)
self.data["spins"][f_idx] = np.matmul(self.data["spins"][f_idx], trans)
except:
pass

Expand Down
14 changes: 7 additions & 7 deletions dpdata/vasp_deltaspin/poscar.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def _to_system_data_lower(lines, lines_incar, cartesian=True):
start_index = idx
elif 'M_CONSTR' in incar_param:
end_index = idx
system['spin'] = [lines_incar[start_index].replace('=', '').strip().split()[1:4]]
system['spins'] = [lines_incar[start_index].replace('=', '').strip().split()[1:4]]
for idx in range(start_index+1, end_index-1):
system['spin'].append(lines_incar[idx].strip().split()[:3])
system['spin'] = np.array([system['spin']]).astype('float64')
count = np.sum(np.linalg.norm(system['spin'][0], axis=1) > 0)
spin_idx = np.where(np.cumsum(system['atom_numbs']) <= count)
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


Expand Down Expand Up @@ -100,11 +100,11 @@ 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['spin'][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['spin'][f_idx]) - 1:
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"
else:
Expand Down

0 comments on commit 6233b8f

Please sign in to comment.