@@ -314,7 +314,7 @@ def get_atom_mag_cartesian(atommag, angle1, angle2):
314
314
]
315
315
316
316
317
- def get_carteisan_coords (coords , coord_type , celldm , cell ):
317
+ def get_cartesian_coords (coords , coord_type , celldm , cell ):
318
318
"""Transform the atomic coordinates to cartesian coordinates.
319
319
320
320
Args:
@@ -378,7 +378,7 @@ def parse_pos(coords_lines, atom_names, celldm, cell):
378
378
parse_pos_oneline (coords_lines [line_idx ])
379
379
)
380
380
381
- coords .append (get_carteisan_coords (np .array (pos ), coord_type , celldm , cell ))
381
+ coords .append (get_cartesian_coords (np .array (pos ), coord_type , celldm , cell ))
382
382
383
383
move .append (imove )
384
384
velocity .append (ivelocity )
@@ -422,6 +422,25 @@ def parse_pos(coords_lines, atom_names, celldm, cell):
422
422
return atom_numbs , coords , move , mags , velocity , sc , lambda_
423
423
424
424
425
+ def right_hand_rule (
426
+ cell : np .ndarray , coord : np .ndarray
427
+ ) -> tuple [np .ndarray , np .ndarray ]:
428
+ """Rotate the cell and coord to make the cell fit the right-hand rule.
429
+
430
+ Args:
431
+ cell (np.ndarray): the cell vectors.
432
+ coord (np.ndarray): the atomic coordinates in cartesian.
433
+
434
+ Returns
435
+ -------
436
+ tuple: the rotated cell and coord.
437
+ """
438
+ if np .linalg .det (cell ) < 0 :
439
+ cell = - cell
440
+ coord = - coord
441
+ return cell , coord
442
+
443
+
425
444
def get_frame_from_stru (stru ):
426
445
"""Read the ABACUS STRU file and return the dpdata frame.
427
446
@@ -473,6 +492,7 @@ def get_frame_from_stru(stru):
473
492
blocks ["ATOMIC_POSITIONS" ], atom_names , celldm , cell
474
493
)
475
494
495
+ cell , coords = right_hand_rule (cell , coords )
476
496
data = {
477
497
"atom_names" : atom_names ,
478
498
"atom_numbs" : atom_numbs ,
@@ -731,66 +751,68 @@ def process_file_input(file_input, atom_names, input_name):
731
751
out += "0.0\n "
732
752
out += str (data ["atom_numbs" ][iele ]) + "\n "
733
753
for iatom in range (data ["atom_numbs" ][iele ]):
734
- iatomtype = np .nonzero (data ["atom_types" ] == iele )[0 ][iatom ]
754
+ iatomtype = np .nonzero (data ["atom_types" ] == iele )[0 ][
755
+ iatom
756
+ ] # it is the atom index
735
757
iout = f"{ data ['coords' ][frame_idx ][iatomtype , 0 ]:.12f} { data ['coords' ][frame_idx ][iatomtype , 1 ]:.12f} { data ['coords' ][frame_idx ][iatomtype , 2 ]:.12f} "
736
758
# add flags for move, velocity, mag, angle1, angle2, and sc
737
759
if move is not None :
738
760
if (
739
- isinstance (ndarray2list (move [natom_tot ]), (list , tuple ))
740
- and len (move [natom_tot ]) == 3
761
+ isinstance (ndarray2list (move [iatomtype ]), (list , tuple ))
762
+ and len (move [iatomtype ]) == 3
741
763
):
742
764
iout += " " + " " .join (
743
- ["1" if ii else "0" for ii in move [natom_tot ]]
765
+ ["1" if ii else "0" for ii in move [iatomtype ]]
744
766
)
745
- elif isinstance (ndarray2list (move [natom_tot ]), (int , float , bool )):
746
- iout += " 1 1 1" if move [natom_tot ] else " 0 0 0"
767
+ elif isinstance (ndarray2list (move [iatomtype ]), (int , float , bool )):
768
+ iout += " 1 1 1" if move [iatomtype ] else " 0 0 0"
747
769
else :
748
770
iout += " 1 1 1"
749
771
750
772
if (
751
773
velocity is not None
752
- and isinstance (ndarray2list (velocity [natom_tot ]), (list , tuple ))
753
- and len (velocity [natom_tot ]) == 3
774
+ and isinstance (ndarray2list (velocity [iatomtype ]), (list , tuple ))
775
+ and len (velocity [iatomtype ]) == 3
754
776
):
755
- iout += " v " + " " .join ([f"{ ii :.12f} " for ii in velocity [natom_tot ]])
777
+ iout += " v " + " " .join ([f"{ ii :.12f} " for ii in velocity [iatomtype ]])
756
778
757
779
if mag is not None :
758
- if isinstance (ndarray2list (mag [natom_tot ]), (list , tuple )) and len (
759
- mag [natom_tot ]
780
+ if isinstance (ndarray2list (mag [iatomtype ]), (list , tuple )) and len (
781
+ mag [iatomtype ]
760
782
) in [1 , 3 ]:
761
- iout += " mag " + " " .join ([f"{ ii :.12f} " for ii in mag [natom_tot ]])
762
- elif isinstance (ndarray2list (mag [natom_tot ]), (int , float )):
763
- iout += " mag " + f"{ mag [natom_tot ]:.12f} "
783
+ iout += " mag " + " " .join ([f"{ ii :.12f} " for ii in mag [iatomtype ]])
784
+ elif isinstance (ndarray2list (mag [iatomtype ]), (int , float )):
785
+ iout += " mag " + f"{ mag [iatomtype ]:.12f} "
764
786
765
787
if angle1 is not None and isinstance (
766
- ndarray2list (angle1 [natom_tot ]), (int , float )
788
+ ndarray2list (angle1 [iatomtype ]), (int , float )
767
789
):
768
- iout += " angle1 " + f"{ angle1 [natom_tot ]:.12f} "
790
+ iout += " angle1 " + f"{ angle1 [iatomtype ]:.12f} "
769
791
770
792
if angle2 is not None and isinstance (
771
- ndarray2list (angle2 [natom_tot ]), (int , float )
793
+ ndarray2list (angle2 [iatomtype ]), (int , float )
772
794
):
773
- iout += " angle2 " + f"{ angle2 [natom_tot ]:.12f} "
795
+ iout += " angle2 " + f"{ angle2 [iatomtype ]:.12f} "
774
796
775
797
if sc is not None :
776
- if isinstance (ndarray2list (sc [natom_tot ]), (list , tuple )) and len (
777
- sc [natom_tot ]
798
+ if isinstance (ndarray2list (sc [iatomtype ]), (list , tuple )) and len (
799
+ sc [iatomtype ]
778
800
) in [1 , 3 ]:
779
801
iout += " sc " + " " .join (
780
- ["1" if ii else "0" for ii in sc [natom_tot ]]
802
+ ["1" if ii else "0" for ii in sc [iatomtype ]]
781
803
)
782
- elif isinstance (ndarray2list (sc [natom_tot ]), (int , float , bool )):
783
- iout += " sc " + "1" if sc [natom_tot ] else "0"
804
+ elif isinstance (ndarray2list (sc [iatomtype ]), (int , float , bool )):
805
+ iout += " sc " + "1" if sc [iatomtype ] else "0"
784
806
785
807
if lambda_ is not None :
786
- if isinstance (ndarray2list (lambda_ [natom_tot ]), (list , tuple )) and len (
787
- lambda_ [natom_tot ]
808
+ if isinstance (ndarray2list (lambda_ [iatomtype ]), (list , tuple )) and len (
809
+ lambda_ [iatomtype ]
788
810
) in [1 , 3 ]:
789
811
iout += " lambda " + " " .join (
790
- [f"{ ii :.12f} " for ii in lambda_ [natom_tot ]]
812
+ [f"{ ii :.12f} " for ii in lambda_ [iatomtype ]]
791
813
)
792
- elif isinstance (ndarray2list (lambda_ [natom_tot ]), (int , float )):
793
- iout += " lambda " + f"{ lambda_ [natom_tot ]:.12f} "
814
+ elif isinstance (ndarray2list (lambda_ [iatomtype ]), (int , float )):
815
+ iout += " lambda " + f"{ lambda_ [iatomtype ]:.12f} "
794
816
795
817
out += iout + "\n "
796
818
natom_tot += 1
0 commit comments