@@ -46,8 +46,22 @@ def read_extxyz(
46
46
cell = _parse_key_value (line , "Lattice" , "float" , 9 , filename )
47
47
cell = np .reshape (cell , (3 , 3 ))
48
48
49
- # PBC
50
- PBC = _parse_key_value (line , "PBC" , "int" , 3 , filename )
49
+ # PBC integer (0, 1) or str ("T", "F")
50
+ PBC = _parse_key_value (line , "PBC" , "str" , 3 , filename )
51
+ try :
52
+ PBC = [bool (int (i )) for i in PBC ]
53
+ except ValueError :
54
+ tmp = []
55
+ for p in PBC :
56
+ if p .lower () == "t" :
57
+ tmp .append (True )
58
+ elif p .lower () == "f" :
59
+ tmp .append (False )
60
+ else :
61
+ raise InputError (
62
+ f"Invalid PBC value { p } at line 2 of file { filename } ."
63
+ )
64
+ PBC = tmp
51
65
52
66
# energy is optional
53
67
try :
@@ -125,6 +139,7 @@ def write_extxyz(
125
139
energy : Optional [float ] = None ,
126
140
forces : Optional [np .ndarray ] = None ,
127
141
stress : Optional [List [float ]] = None ,
142
+ bool_as_str : bool = False ,
128
143
):
129
144
"""
130
145
Write configuration info to a file in extended xyz file_format.
@@ -139,6 +154,8 @@ def write_extxyz(
139
154
forces: Nx3 array, forces on atoms; If `None`, not write to file
140
155
stress: 1D array of size 6, stress on the cell in Voigt notation; If `None`,
141
156
not write to file
157
+ bool_as_str: If `True`, write PBC as "T" or "F"; otherwise, write PBC as 1 or 0.
158
+
142
159
"""
143
160
144
161
with open (filename , "w" ) as fout :
@@ -155,7 +172,10 @@ def write_extxyz(
155
172
else :
156
173
fout .write ("{:.15g} " .format (item ))
157
174
158
- PBC = [int (i ) for i in PBC ]
175
+ if bool_as_str :
176
+ PBC = ["T" if i else "F" for i in PBC ]
177
+ else :
178
+ PBC = [int (i ) for i in PBC ]
159
179
fout .write ('PBC="{} {} {}" ' .format (PBC [0 ], PBC [1 ], PBC [2 ]))
160
180
161
181
if energy is not None :
@@ -204,7 +224,7 @@ def _parse_key_value(
204
224
Args:
205
225
line: The string line.
206
226
key: Keyword to parse.
207
- dtype: Expected data type of value, `int` or `float `.
227
+ dtype: Expected data type of value, `int`, `float` or `str `.
208
228
size: Expected size of value.
209
229
filename: File name where the line comes from.
210
230
@@ -221,7 +241,7 @@ def _parse_key_value(
221
241
else :
222
242
value = value [value .index ("=" ) + 1 :]
223
243
value = value .lstrip (" " )
224
- value += " " # add an whitespace at end in case this is the last key
244
+ value += " " # add a whitespace at end in case this is the last key
225
245
value = value [: value .index (" " )]
226
246
value = value .split ()
227
247
except Exception as e :
@@ -237,6 +257,8 @@ def _parse_key_value(
237
257
value = [float (i ) for i in value ]
238
258
elif dtype == "int" :
239
259
value = [int (i ) for i in value ]
260
+ elif dtype == "str" :
261
+ pass
240
262
except Exception as e :
241
263
raise InputError (f"{ e } .\n Corrupted { key } data at line 2 of file { filename } ." )
242
264
0 commit comments