@@ -886,6 +886,92 @@ def test_write(self):
886886 incar = Incar .from_file (tmp_file )
887887 assert incar == self .incar
888888
889+ def test_from_str_comment_handling (self ):
890+ incar_str = r"""
891+ # A = 0
892+ ! B=1
893+ SIGMA = 0.05 # random comment (known float tag)
894+ EDIFF = 1e-6 ! another comment (known float tag)
895+ ALGO = Normal # comment (unknown tag -> inferred as str)
896+ GGA = PE ! comment (unknown tag -> inferred as str)
897+ """
898+ incar = Incar .from_str (incar_str )
899+
900+ assert set (incar .keys ()) == {"SIGMA" , "EDIFF" , "ALGO" , "GGA" }
901+ assert incar ["SIGMA" ] == approx (0.05 )
902+ assert incar ["EDIFF" ] == approx (1e-6 )
903+ assert incar ["ALGO" ] == "Normal"
904+ assert incar ["GGA" ] == "Pe"
905+
906+ def test_from_str_semicolon_separated_statements (self ):
907+ # Test interaction between semicolon and comment
908+ incar_str = r"""
909+ ENMAX = 400; ALGO = Fast ! A = 0
910+ ENCUT = 500; ISMEAR = 0 # B=1
911+ PREC = Accurate ; LREAL = Auto ! precision and projection scheme
912+ IBRION = 2; ISIF = 3; NSW = 100 # three statements in one line
913+ """
914+ incar = Incar .from_str (incar_str )
915+
916+ assert set (incar .keys ()) == {
917+ "ENMAX" ,
918+ "ALGO" ,
919+ "ENCUT" ,
920+ "ISMEAR" ,
921+ "PREC" ,
922+ "LREAL" ,
923+ "IBRION" ,
924+ "ISIF" ,
925+ "NSW" ,
926+ }
927+
928+ assert incar ["ENMAX" ] == 400
929+ assert incar ["ALGO" ] == "Fast"
930+ assert incar ["ENCUT" ] == 500
931+ assert incar ["ISMEAR" ] == 0
932+ assert incar ["PREC" ] == "Accurate"
933+ assert incar ["LREAL" ] == "Auto"
934+ assert incar ["IBRION" ] == 2
935+ assert incar ["ISIF" ] == 3
936+ assert incar ["NSW" ] == 100
937+
938+ def test_from_str_line_continuation_with_backslash (self ):
939+ # Test line continuation with backslash
940+ incar_str = r"""
941+ ALGO = Normal # \ This backslash should be ignored
942+ ENMAX = 200 ! \ This backslash should be ignored
943+ MAGMOM = 0 0 1.0 0 0 -1.0 \
944+ 0 0 1.0 0 0 -1.0 \
945+ 6*0
946+ """
947+ incar = Incar .from_str (incar_str )
948+
949+ assert set (incar .keys ()) == {"ALGO" , "ENMAX" , "MAGMOM" }
950+ assert incar ["ALGO" ] == "Normal"
951+ assert incar ["ENMAX" ] == 200
952+
953+ assert incar ["MAGMOM" ] == [0 , 0 , 1.0 , 0 , 0 , - 1.0 , 0 , 0 , 1.0 , 0 , 0 , - 1.0 ] + [0.0 ] * 6
954+
955+ def test_from_str_multiline_string (self ):
956+ incar_str = r"""
957+ # Multi-line string with embedded comments
958+ WANNIER90_WIN = "begin Projections # should NOT be capitalized
959+ Fe:d ; Fe:p # comment inside string
960+ End Projections ! random comment
961+ " # comment after closing quote
962+ """
963+ incar = Incar .from_str (incar_str )
964+
965+ assert set (incar .keys ()) == {"WANNIER90_WIN" }
966+
967+ # Comments inside the string would be lost
968+ assert (
969+ incar ["WANNIER90_WIN" ]
970+ == """begin Projections
971+ Fe:d ; Fe:p
972+ End Projections"""
973+ )
974+
889975 def test_get_str (self ):
890976 incar_str = self .incar .get_str (pretty = True , sort_keys = True )
891977 expected = """ALGO = Damped
@@ -1003,6 +1089,7 @@ def test_types(self):
10031089
10041090 def test_proc_types (self ):
10051091 assert Incar .proc_val ("HELLO" , "-0.85 0.85" ) == "-0.85 0.85"
1092+ # `ML_MODE` should always be lower case
10061093 assert Incar .proc_val ("ML_MODE" , "train" ) == "train"
10071094 assert Incar .proc_val ("ML_MODE" , "RUN" ) == "run"
10081095 assert Incar .proc_val ("ALGO" , "fast" ) == "Fast"
0 commit comments