Skip to content

Commit 222148f

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 46592e9 commit 222148f

2 files changed

Lines changed: 29 additions & 30 deletions

File tree

dpdata/abacus/scf.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -257,34 +257,34 @@ def parse_stru_pos(pos_line):
257257

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

260-
def get_atom_mag_cartesian(atommag,angle1, angle2):
261-
'''Transform atommag, angle1, angle2 to magmom in cartesian coordinates.
262-
260+
261+
def get_atom_mag_cartesian(atommag, angle1, angle2):
262+
"""Transform atommag, angle1, angle2 to magmom in cartesian coordinates.
263+
263264
Parameters
264265
----------
265266
atommag : float/list of float/None
266267
Atom magnetic moment.
267268
angle1 : float/None
268269
value of angle1.
269270
angle2 : float/None
270-
value of angle2.
271-
271+
value of angle2.
272272
ABACUS support defining mag, angle1, angle2 at the same time.
273273
angle1 is the angle between z-axis and real spin (in degrees).
274-
angle2 is the angle between x-axis and real spin projection in xy-plane (in degrees).
274+
angle2 is the angle between x-axis and real spin projection in xy-plane (in degrees).
275275
If only mag is defined, then transfer it to magmom directly.
276276
And if mag, angle1, angle2 are defined, then mag is only the norm of magmom, and the direction is defined by angle1 and angle2.
277-
'''
277+
"""
278278
if atommag is None:
279279
return None
280280
if not (isinstance(atommag, list) or isinstance(atommag, float)):
281281
raise RuntimeError(f"Invalid atommag: {atommag}")
282-
282+
283283
if angle1 is None and angle2 is None:
284284
if isinstance(atommag, list):
285285
return atommag
286286
else:
287-
return [0,0,atommag]
287+
return [0, 0, atommag]
288288
else:
289289
a1 = 0
290290
a2 = 0
@@ -296,10 +296,12 @@ def get_atom_mag_cartesian(atommag,angle1, angle2):
296296
mag_norm = np.linalg.norm(atommag)
297297
else:
298298
mag_norm = atommag
299-
return [mag_norm*np.sin(np.radians(a1))*np.cos(np.radians(a2)),
300-
mag_norm*np.sin(np.radians(a1))*np.sin(np.radians(a2)),
301-
mag_norm*np.cos(np.radians(a1))]
302-
299+
return [
300+
mag_norm * np.sin(np.radians(a1)) * np.cos(np.radians(a2)),
301+
mag_norm * np.sin(np.radians(a1)) * np.sin(np.radians(a2)),
302+
mag_norm * np.cos(np.radians(a1)),
303+
]
304+
303305

304306
def get_coords(celldm, cell, geometry_inlines, inlines=None):
305307
coords_lines = get_stru_block(geometry_inlines, "ATOMIC_POSITIONS")
@@ -319,7 +321,7 @@ def get_coords(celldm, cell, geometry_inlines, inlines=None):
319321
line_idx = 1 # starting line of first element
320322
for it in range(ntype):
321323
atom_names.append(coords_lines[line_idx].split()[0])
322-
atom_type_mag = float(coords_lines[line_idx+1].split()[0])
324+
atom_type_mag = float(coords_lines[line_idx + 1].split()[0])
323325
line_idx += 2
324326
atom_numbs.append(int(coords_lines[line_idx].split()[0]))
325327
line_idx += 1
@@ -346,11 +348,11 @@ def get_coords(celldm, cell, geometry_inlines, inlines=None):
346348
velocity.append(ivelocity)
347349
sc.append(iconstrain)
348350
lambda_.append(ilambda1)
349-
351+
350352
# calculate the magnetic moment in cartesian coordinates
351353
mag = get_atom_mag_cartesian(imagmom, iangle1, iangle2)
352354
if mag is None:
353-
mag = [0,0,atom_type_mag]
355+
mag = [0, 0, atom_type_mag]
354356
mags.append(mag)
355357

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

526528
celldm, cell = get_cell(geometry_inlines)
527-
atom_names, natoms, types, coords, move, magmom = get_coords( # here the magmom is the initial magnetic moment in STRU
528-
celldm, cell, geometry_inlines, inlines
529+
atom_names, natoms, types, coords, move, magmom = (
530+
get_coords( # here the magmom is the initial magnetic moment in STRU
531+
celldm, cell, geometry_inlines, inlines
532+
)
529533
)
530-
531-
534+
532535
magmom, magforce = get_mag_force(outlines)
533536
if len(magmom) > 0:
534537
magmom = magmom[-1:]

tests/test_abacus_spin.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,19 @@ def test_md(self):
155155
np.testing.assert_almost_equal(
156156
data["mag_forces"], sys2.data["mag_forces"], decimal=8
157157
)
158-
158+
159159
def test_read_stru_spin(self):
160160
mysys = dpdata.System("abacus.spin/STRU.spin", fmt="abacus/stru")
161161
self.assertTrue("spins" in mysys.data)
162162
print(mysys.data["spins"])
163-
163+
164164
"""
165165
0.0000000000 0.000000000 0.000000000 mag 0 0 2
166166
0.1000000000 0.1000000000 0.1000000000 mag 3
167167
0.2000000000 0.2000000000 0.2000000000 mag 3 angle1 90
168168
0.3000000000 0.3000000000 0.3000000000 mag 3 4 0 angle1 90 angle2 90
169169
"""
170-
np.testing.assert_almost_equal(mysys.data["spins"][0][0],[0,0,2],decimal=8)
171-
np.testing.assert_almost_equal(mysys.data["spins"][0][1],[0,0,3],decimal=8)
172-
np.testing.assert_almost_equal(mysys.data["spins"][0][2],[3,0,0],decimal=8)
173-
np.testing.assert_almost_equal(mysys.data["spins"][0][3],[0,5,0],decimal=8)
174-
175-
176-
177-
170+
np.testing.assert_almost_equal(mysys.data["spins"][0][0], [0, 0, 2], decimal=8)
171+
np.testing.assert_almost_equal(mysys.data["spins"][0][1], [0, 0, 3], decimal=8)
172+
np.testing.assert_almost_equal(mysys.data["spins"][0][2], [3, 0, 0], decimal=8)
173+
np.testing.assert_almost_equal(mysys.data["spins"][0][3], [0, 5, 0], decimal=8)

0 commit comments

Comments
 (0)