@@ -314,7 +314,7 @@ def get_atom_mag_cartesian(atommag, angle1, angle2):
314314 ]
315315
316316
317- def get_carteisan_coords (coords , coord_type , celldm , cell ):
317+ def get_cartesian_coords (coords , coord_type , celldm , cell ):
318318 """Transform the atomic coordinates to cartesian coordinates.
319319
320320 Args:
@@ -378,7 +378,7 @@ def parse_pos(coords_lines, atom_names, celldm, cell):
378378 parse_pos_oneline (coords_lines [line_idx ])
379379 )
380380
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 ))
382382
383383 move .append (imove )
384384 velocity .append (ivelocity )
@@ -422,6 +422,25 @@ def parse_pos(coords_lines, atom_names, celldm, cell):
422422 return atom_numbs , coords , move , mags , velocity , sc , lambda_
423423
424424
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+
425444def get_frame_from_stru (stru ):
426445 """Read the ABACUS STRU file and return the dpdata frame.
427446
@@ -473,6 +492,7 @@ def get_frame_from_stru(stru):
473492 blocks ["ATOMIC_POSITIONS" ], atom_names , celldm , cell
474493 )
475494
495+ cell , coords = right_hand_rule (cell , coords )
476496 data = {
477497 "atom_names" : atom_names ,
478498 "atom_numbs" : atom_numbs ,
@@ -731,66 +751,68 @@ def process_file_input(file_input, atom_names, input_name):
731751 out += "0.0\n "
732752 out += str (data ["atom_numbs" ][iele ]) + "\n "
733753 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
735757 iout = f"{ data ['coords' ][frame_idx ][iatomtype , 0 ]:.12f} { data ['coords' ][frame_idx ][iatomtype , 1 ]:.12f} { data ['coords' ][frame_idx ][iatomtype , 2 ]:.12f} "
736758 # add flags for move, velocity, mag, angle1, angle2, and sc
737759 if move is not None :
738760 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
741763 ):
742764 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 ]]
744766 )
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"
747769 else :
748770 iout += " 1 1 1"
749771
750772 if (
751773 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
754776 ):
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 ]])
756778
757779 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 ]
760782 ) 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} "
764786
765787 if angle1 is not None and isinstance (
766- ndarray2list (angle1 [natom_tot ]), (int , float )
788+ ndarray2list (angle1 [iatomtype ]), (int , float )
767789 ):
768- iout += " angle1 " + f"{ angle1 [natom_tot ]:.12f} "
790+ iout += " angle1 " + f"{ angle1 [iatomtype ]:.12f} "
769791
770792 if angle2 is not None and isinstance (
771- ndarray2list (angle2 [natom_tot ]), (int , float )
793+ ndarray2list (angle2 [iatomtype ]), (int , float )
772794 ):
773- iout += " angle2 " + f"{ angle2 [natom_tot ]:.12f} "
795+ iout += " angle2 " + f"{ angle2 [iatomtype ]:.12f} "
774796
775797 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 ]
778800 ) in [1 , 3 ]:
779801 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 ]]
781803 )
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"
784806
785807 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 ]
788810 ) in [1 , 3 ]:
789811 iout += " lambda " + " " .join (
790- [f"{ ii :.12f} " for ii in lambda_ [natom_tot ]]
812+ [f"{ ii :.12f} " for ii in lambda_ [iatomtype ]]
791813 )
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} "
794816
795817 out += iout + "\n "
796818 natom_tot += 1
0 commit comments