From 49ffeab72e5bf2c0202caf449ffa451cddb274a4 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 6 Mar 2025 13:49:15 +0800 Subject: [PATCH 1/2] abacus/stru: return spins only when atomic magnetic moment is specified --- dpdata/abacus/stru.py | 21 +++++++++++++++++---- tests/abacus.scf/stru_test | 10 +++++----- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/dpdata/abacus/stru.py b/dpdata/abacus/stru.py index 131aba79f..97a13a905 100644 --- a/dpdata/abacus/stru.py +++ b/dpdata/abacus/stru.py @@ -362,6 +362,7 @@ def parse_pos(coords_lines, atom_names, celldm, cell): ntype = len(atom_names) line_idx = 1 # starting line of first element + define_atom_mag = False for it in range(ntype): atom_name = coords_lines[line_idx].split()[0] if atom_name != atom_names[it]: @@ -389,6 +390,9 @@ def parse_pos(coords_lines, atom_names, celldm, cell): if mag is None: mag = [0, 0, atom_type_mag] mags.append(mag) + + if imagmom is not None: + define_atom_mag = True line_idx += 1 coords = np.array(coords) # need transformation!!! @@ -409,7 +413,11 @@ def parse_pos(coords_lines, atom_names, celldm, cell): if all([i is None for i in lambda_]): lambda_ = [] - mags = np.array(mags) + # here return the magnetic moment only when the atom magnetic moment is specified. + if not define_atom_mag: + mags = [] + else: + mags = np.array(mags) return atom_numbs, coords, move, mags, velocity, sc, lambda_ @@ -438,10 +446,14 @@ def get_frame_from_stru(stru): "cells": list of cell vectors, "coords": list of atomic coordinates, - "spins": list of magnetic moments, + "spins": list of magnetic moments, # return only when set "mag xxx" for each atom in STRU file "moves": list of move flags, } For some keys, if the information is not provided in the STRU file, then it will not be included in the dictionary. + + "spins" is designed for delta spin calculation, and when dpdata.System is write to lmp format, the spin will be written as magmom. + But we should note that this file format is valid only for a spin lammps job, not for a normal job. + If you want to use dpgen to run the non-spin job, then you should not define "mag x x x" in the STRU file. """ if not os.path.isfile(stru): raise FileNotFoundError(f"ABACUS STRU file {stru} not found!!!") @@ -471,9 +483,10 @@ def get_frame_from_stru(stru): "masses": np.array(masses), "pp_files": pp_files, "cells": np.array([cell]), - "coords": np.array([coords]), - "spins": np.array([mags]), + "coords": np.array([coords]) } + if len(mags) > 0: + data["spins"] = np.array([mags]) if len(orb_files) > 0: data["orb_files"] = orb_files if len(dpks_descriptor) > 0: diff --git a/tests/abacus.scf/stru_test b/tests/abacus.scf/stru_test index c3a1917db..e40364093 100644 --- a/tests/abacus.scf/stru_test +++ b/tests/abacus.scf/stru_test @@ -22,11 +22,11 @@ Cartesian # Cartesian(Unit is LATTICE_CONSTANT) C 0.0 1 -5.192682633809 4.557725978258 4.436846615358 1 1 1 mag 0.000000000000 0.000000000000 0.000000000000 +5.192682633809 4.557725978258 4.436846615358 1 1 1 H 0.0 4 -5.416431453540 4.011298860305 3.511161492417 0 0 0 mag 0.000000000000 0.000000000000 0.000000000000 -4.131588222365 4.706745191323 4.431136645083 1 0 1 mag 0.000000000000 0.000000000000 0.000000000000 -5.630930319126 5.521640894956 4.450356541303 1 0 1 mag 0.000000000000 0.000000000000 0.000000000000 -5.499851012568 4.003388899277 5.342621842622 0 1 1 mag 0.000000000000 0.000000000000 0.000000000000 +5.416431453540 4.011298860305 3.511161492417 0 0 0 +4.131588222365 4.706745191323 4.431136645083 1 0 1 +5.630930319126 5.521640894956 4.450356541303 1 0 1 +5.499851012568 4.003388899277 5.342621842622 0 1 1 From 0ffc2deda09217b37d6c12f758449e2b5bfcff8d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 6 Mar 2025 08:26:37 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dpdata/abacus/stru.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dpdata/abacus/stru.py b/dpdata/abacus/stru.py index 97a13a905..cda25d00a 100644 --- a/dpdata/abacus/stru.py +++ b/dpdata/abacus/stru.py @@ -390,7 +390,7 @@ def parse_pos(coords_lines, atom_names, celldm, cell): if mag is None: mag = [0, 0, atom_type_mag] mags.append(mag) - + if imagmom is not None: define_atom_mag = True @@ -450,7 +450,6 @@ def get_frame_from_stru(stru): "moves": list of move flags, } For some keys, if the information is not provided in the STRU file, then it will not be included in the dictionary. - "spins" is designed for delta spin calculation, and when dpdata.System is write to lmp format, the spin will be written as magmom. But we should note that this file format is valid only for a spin lammps job, not for a normal job. If you want to use dpgen to run the non-spin job, then you should not define "mag x x x" in the STRU file. @@ -483,7 +482,7 @@ def get_frame_from_stru(stru): "masses": np.array(masses), "pp_files": pp_files, "cells": np.array([cell]), - "coords": np.array([coords]) + "coords": np.array([coords]), } if len(mags) > 0: data["spins"] = np.array([mags])