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 Nov 5, 2024
1 parent 46592e9 commit 222148f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
43 changes: 23 additions & 20 deletions dpdata/abacus/scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,34 +257,34 @@ def parse_stru_pos(pos_line):

return pos, move, velocity, magmom, angle1, angle2, constrain, lambda1

def get_atom_mag_cartesian(atommag,angle1, angle2):
'''Transform atommag, angle1, angle2 to magmom in cartesian coordinates.

def get_atom_mag_cartesian(atommag, angle1, angle2):
"""Transform atommag, angle1, angle2 to magmom in cartesian coordinates.
Parameters
----------
atommag : float/list of float/None
Atom magnetic moment.
angle1 : float/None
value of angle1.
angle2 : float/None
value of angle2.
value of angle2.
ABACUS support defining mag, angle1, angle2 at the same time.
angle1 is the angle between z-axis and real spin (in degrees).
angle2 is the angle between x-axis and real spin projection in xy-plane (in degrees).
angle2 is the angle between x-axis and real spin projection in xy-plane (in degrees).
If only mag is defined, then transfer it to magmom directly.
And if mag, angle1, angle2 are defined, then mag is only the norm of magmom, and the direction is defined by angle1 and angle2.
'''
"""
if atommag is None:
return None
if not (isinstance(atommag, list) or isinstance(atommag, float)):
raise RuntimeError(f"Invalid atommag: {atommag}")

if angle1 is None and angle2 is None:
if isinstance(atommag, list):
return atommag
else:
return [0,0,atommag]
return [0, 0, atommag]
else:
a1 = 0
a2 = 0
Expand All @@ -296,10 +296,12 @@ def get_atom_mag_cartesian(atommag,angle1, angle2):
mag_norm = np.linalg.norm(atommag)
else:
mag_norm = atommag
return [mag_norm*np.sin(np.radians(a1))*np.cos(np.radians(a2)),
mag_norm*np.sin(np.radians(a1))*np.sin(np.radians(a2)),
mag_norm*np.cos(np.radians(a1))]

return [
mag_norm * np.sin(np.radians(a1)) * np.cos(np.radians(a2)),
mag_norm * np.sin(np.radians(a1)) * np.sin(np.radians(a2)),
mag_norm * np.cos(np.radians(a1)),
]


def get_coords(celldm, cell, geometry_inlines, inlines=None):
coords_lines = get_stru_block(geometry_inlines, "ATOMIC_POSITIONS")
Expand All @@ -319,7 +321,7 @@ def get_coords(celldm, cell, geometry_inlines, inlines=None):
line_idx = 1 # starting line of first element
for it in range(ntype):
atom_names.append(coords_lines[line_idx].split()[0])
atom_type_mag = float(coords_lines[line_idx+1].split()[0])
atom_type_mag = float(coords_lines[line_idx + 1].split()[0])
line_idx += 2
atom_numbs.append(int(coords_lines[line_idx].split()[0]))
line_idx += 1
Expand All @@ -346,11 +348,11 @@ def get_coords(celldm, cell, geometry_inlines, inlines=None):
velocity.append(ivelocity)
sc.append(iconstrain)
lambda_.append(ilambda1)

# calculate the magnetic moment in cartesian coordinates
mag = get_atom_mag_cartesian(imagmom, iangle1, iangle2)
if mag is None:
mag = [0,0,atom_type_mag]
mag = [0, 0, atom_type_mag]
mags.append(mag)

line_idx += 1
Expand Down Expand Up @@ -524,11 +526,12 @@ def get_frame(fname):
outlines = fp.read().split("\n")

celldm, cell = get_cell(geometry_inlines)
atom_names, natoms, types, coords, move, magmom = get_coords( # here the magmom is the initial magnetic moment in STRU
celldm, cell, geometry_inlines, inlines
atom_names, natoms, types, coords, move, magmom = (
get_coords( # here the magmom is the initial magnetic moment in STRU
celldm, cell, geometry_inlines, inlines
)
)



magmom, magforce = get_mag_force(outlines)
if len(magmom) > 0:
magmom = magmom[-1:]
Expand Down
16 changes: 6 additions & 10 deletions tests/test_abacus_spin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,19 @@ def test_md(self):
np.testing.assert_almost_equal(
data["mag_forces"], sys2.data["mag_forces"], decimal=8
)

def test_read_stru_spin(self):
mysys = dpdata.System("abacus.spin/STRU.spin", fmt="abacus/stru")
self.assertTrue("spins" in mysys.data)
print(mysys.data["spins"])

"""
0.0000000000 0.000000000 0.000000000 mag 0 0 2
0.1000000000 0.1000000000 0.1000000000 mag 3
0.2000000000 0.2000000000 0.2000000000 mag 3 angle1 90
0.3000000000 0.3000000000 0.3000000000 mag 3 4 0 angle1 90 angle2 90
"""
np.testing.assert_almost_equal(mysys.data["spins"][0][0],[0,0,2],decimal=8)
np.testing.assert_almost_equal(mysys.data["spins"][0][1],[0,0,3],decimal=8)
np.testing.assert_almost_equal(mysys.data["spins"][0][2],[3,0,0],decimal=8)
np.testing.assert_almost_equal(mysys.data["spins"][0][3],[0,5,0],decimal=8)




np.testing.assert_almost_equal(mysys.data["spins"][0][0], [0, 0, 2], decimal=8)
np.testing.assert_almost_equal(mysys.data["spins"][0][1], [0, 0, 3], decimal=8)
np.testing.assert_almost_equal(mysys.data["spins"][0][2], [3, 0, 0], decimal=8)
np.testing.assert_almost_equal(mysys.data["spins"][0][3], [0, 5, 0], decimal=8)

0 comments on commit 222148f

Please sign in to comment.