@@ -105,6 +105,24 @@ def terminate_zmq():
105105 logging .error (f"Error while terminating ZMQ port { port .address } : { e } " )
106106# --- ZeroMQ Integration End ---
107107
108+
109+ # NumPy Type Conversion Helper
110+ def convert_numpy_to_python (obj ):
111+ #Recursively convert numpy types to native Python types.
112+ #This is necessary because literal_eval cannot parse numpy representations
113+ #like np.float64(1.0), but can parse native Python types like 1.0.
114+ if isinstance (obj , np .generic ):
115+ # Convert numpy scalar types to Python native types
116+ return obj .item ()
117+ elif isinstance (obj , list ):
118+ return [convert_numpy_to_python (item ) for item in obj ]
119+ elif isinstance (obj , tuple ):
120+ return tuple (convert_numpy_to_python (item ) for item in obj )
121+ elif isinstance (obj , dict ):
122+ return {key : convert_numpy_to_python (value ) for key , value in obj .items ()}
123+ else :
124+ return obj
125+
108126# ===================================================================
109127# File & Parameter Handling
110128# ===================================================================
@@ -130,13 +148,15 @@ def safe_literal_eval(filename, defaultValue):
130148inpath = "./in" #must be rel path for local
131149outpath = "./out"
132150simtime = 0
151+ concore_params_file = os .path .join (inpath + "1" , "concore.params" )
152+ concore_maxtime_file = os .path .join (inpath + "1" , "concore.maxtime" )
133153
134154#9/21/22
135155# ===================================================================
136156# Parameter Parsing
137157# ===================================================================
138158try :
139- sparams_path = os . path . join ( inpath + "1" , "concore.params" )
159+ sparams_path = concore_params_file
140160 if os .path .exists (sparams_path ):
141161 with open (sparams_path , "r" ) as f :
142162 sparams = f .read ()
@@ -176,8 +196,7 @@ def tryparam(n, i):
176196def default_maxtime (default ):
177197 """Read maximum simulation time from file or use default."""
178198 global maxtime
179- maxtime_path = os .path .join (inpath + "1" , "concore.maxtime" )
180- maxtime = safe_literal_eval (maxtime_path , default )
199+ maxtime = safe_literal_eval (concore_maxtime_file , default )
181200
182201default_maxtime (100 )
183202
@@ -225,14 +244,15 @@ def read(port_identifier, name, initstr_val):
225244 return default_return_val
226245
227246 time .sleep (delay )
228- file_path = os .path .join (inpath + str (file_port_num ), name )
247+ file_path = os .path .join (inpath + str (file_port_num ), name )
229248 ins = ""
230249
231250 try :
232251 with open (file_path , "r" ) as infile :
233252 ins = infile .read ()
234253 except FileNotFoundError :
235254 ins = str (initstr_val )
255+ s += ins # Update s to break unchanged() loop
236256 except Exception as e :
237257 logging .error (f"Error reading { file_path } : { e } . Using default value." )
238258 return default_return_val
@@ -291,11 +311,8 @@ def write(port_identifier, name, val, delta=0):
291311
292312 # Case 2: File-based port
293313 try :
294- if isinstance (port_identifier , str ) and port_identifier in zmq_ports :
295- file_path = os .path .join ("../" + port_identifier , name )
296- else :
297- file_port_num = int (port_identifier )
298- file_path = os .path .join (outpath + str (file_port_num ), name )
314+ file_port_num = int (port_identifier )
315+ file_path = os .path .join (outpath + str (file_port_num ), name )
299316 except ValueError :
300317 logging .error (f"Error: Invalid port identifier '{ port_identifier } ' for file operation. Must be integer or ZMQ name." )
301318 return
@@ -310,7 +327,9 @@ def write(port_identifier, name, val, delta=0):
310327 try :
311328 with open (file_path , "w" ) as outfile :
312329 if isinstance (val , list ):
313- data_to_write = [simtime + delta ] + val
330+ # Convert numpy types to native Python types
331+ val_converted = convert_numpy_to_python (val )
332+ data_to_write = [simtime + delta ] + val_converted
314333 outfile .write (str (data_to_write ))
315334 simtime += delta
316335 else :
0 commit comments