Skip to content

Commit e91d8e6

Browse files
abacus/stru: return spins only when atomic magnetic moment is specified (#800)
The old version will induce a bug that dpdata will always store the spin information, and when dpdata save it to lammps format, the spin information will also be written, which is invalid for a non-spin lammps job. Now, the spin information is saved to dpdata only when the magnetic moment of one atom is specified, which is necessary for a spin job. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Spin data is now processed to include only explicitly defined moments, ensuring accurate output. - **Documentation** - User-facing documentation has been updated to clarify when spin information appears. - **Tests** - Test entries for atomic positions have been streamlined by removing redundant markers. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: root <pxlxingliang> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 94d318b commit e91d8e6

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

dpdata/abacus/stru.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ def parse_pos(coords_lines, atom_names, celldm, cell):
362362

363363
ntype = len(atom_names)
364364
line_idx = 1 # starting line of first element
365+
define_atom_mag = False
365366
for it in range(ntype):
366367
atom_name = coords_lines[line_idx].split()[0]
367368
if atom_name != atom_names[it]:
@@ -390,6 +391,9 @@ def parse_pos(coords_lines, atom_names, celldm, cell):
390391
mag = [0, 0, atom_type_mag]
391392
mags.append(mag)
392393

394+
if imagmom is not None:
395+
define_atom_mag = True
396+
393397
line_idx += 1
394398
coords = np.array(coords) # need transformation!!!
395399

@@ -409,7 +413,11 @@ def parse_pos(coords_lines, atom_names, celldm, cell):
409413
if all([i is None for i in lambda_]):
410414
lambda_ = []
411415

412-
mags = np.array(mags)
416+
# here return the magnetic moment only when the atom magnetic moment is specified.
417+
if not define_atom_mag:
418+
mags = []
419+
else:
420+
mags = np.array(mags)
413421

414422
return atom_numbs, coords, move, mags, velocity, sc, lambda_
415423

@@ -438,10 +446,13 @@ def get_frame_from_stru(stru):
438446
439447
"cells": list of cell vectors,
440448
"coords": list of atomic coordinates,
441-
"spins": list of magnetic moments,
449+
"spins": list of magnetic moments, # return only when set "mag xxx" for each atom in STRU file
442450
"moves": list of move flags,
443451
}
444452
For some keys, if the information is not provided in the STRU file, then it will not be included in the dictionary.
453+
"spins" is designed for delta spin calculation, and when dpdata.System is write to lmp format, the spin will be written as magmom.
454+
But we should note that this file format is valid only for a spin lammps job, not for a normal job.
455+
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.
445456
"""
446457
if not os.path.isfile(stru):
447458
raise FileNotFoundError(f"ABACUS STRU file {stru} not found!!!")
@@ -472,8 +483,9 @@ def get_frame_from_stru(stru):
472483
"pp_files": pp_files,
473484
"cells": np.array([cell]),
474485
"coords": np.array([coords]),
475-
"spins": np.array([mags]),
476486
}
487+
if len(mags) > 0:
488+
data["spins"] = np.array([mags])
477489
if len(orb_files) > 0:
478490
data["orb_files"] = orb_files
479491
if len(dpks_descriptor) > 0:

tests/abacus.scf/stru_test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ Cartesian # Cartesian(Unit is LATTICE_CONSTANT)
2222
C
2323
0.0
2424
1
25-
5.192682633809 4.557725978258 4.436846615358 1 1 1 mag 0.000000000000 0.000000000000 0.000000000000
25+
5.192682633809 4.557725978258 4.436846615358 1 1 1
2626
H
2727
0.0
2828
4
29-
5.416431453540 4.011298860305 3.511161492417 0 0 0 mag 0.000000000000 0.000000000000 0.000000000000
30-
4.131588222365 4.706745191323 4.431136645083 1 0 1 mag 0.000000000000 0.000000000000 0.000000000000
31-
5.630930319126 5.521640894956 4.450356541303 1 0 1 mag 0.000000000000 0.000000000000 0.000000000000
32-
5.499851012568 4.003388899277 5.342621842622 0 1 1 mag 0.000000000000 0.000000000000 0.000000000000
29+
5.416431453540 4.011298860305 3.511161492417 0 0 0
30+
4.131588222365 4.706745191323 4.431136645083 1 0 1
31+
5.630930319126 5.521640894956 4.450356541303 1 0 1
32+
5.499851012568 4.003388899277 5.342621842622 0 1 1

0 commit comments

Comments
 (0)