@@ -327,9 +327,9 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
327
327
)
328
328
# open files in write/append mode
329
329
with open (self ._input_file , mode = open_mode ) as f :
330
- pass # initialize file
330
+ pass # initialize file
331
331
with open (self ._output_file , mode = open_mode ) as f :
332
- pass # initialize file
332
+ pass # initialize file
333
333
334
334
else :
335
335
# Change file extensions to .h5
@@ -353,16 +353,7 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
353
353
pass # initialize file
354
354
355
355
# Initialize simulation counter
356
- sim_counter = manager .SimCounter (idx_i )
357
-
358
- queue = manager .JoinableQueue ()
359
-
360
- # Initialize queue
361
- for _ in range (self .number_of_simulations - sim_counter .get_count ()):
362
- queue .put ("RUN" )
363
-
364
- for _ in range (n_workers ):
365
- queue .put ("STOP" )
356
+ sim_counter = manager .SimCounter (idx_i , self .number_of_simulations )
366
357
367
358
print ("\n Starting monte carlo analysis" , end = "\r " )
368
359
print (f"Number of simulations: { self .number_of_simulations } " )
@@ -379,7 +370,6 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
379
370
inputs_lock ,
380
371
outputs_lock ,
381
372
errors_lock ,
382
- queue ,
383
373
light_mode ,
384
374
file_paths ,
385
375
),
@@ -413,12 +403,11 @@ def __run_simulation_worker(
413
403
inputs_lock ,
414
404
outputs_lock ,
415
405
errors_lock ,
416
- queue ,
417
406
light_mode ,
418
407
file_paths ,
419
408
):
420
409
"""
421
- Runs a simulation from a queue .
410
+ Worker code to execute a simulation in a process .
422
411
423
412
Parameters
424
413
----------
@@ -452,11 +441,9 @@ def __run_simulation_worker(
452
441
"""
453
442
try :
454
443
while True :
455
- if queue .get () == "STOP" :
456
- break
457
-
458
444
sim_idx = sim_counter .increment ()
459
- sim_start = time ()
445
+ if sim_idx == - 1 :
446
+ break
460
447
461
448
env = sto_env .create_object ()
462
449
rocket = sto_rocket .create_object ()
@@ -534,12 +521,19 @@ def __run_simulation_worker(
534
521
MonteCarlo .__dict_to_h5 (h5_file , '/' , export_outputs )
535
522
outputs_lock .release ()
536
523
537
- sim_end = time ()
524
+ average_time = (
525
+ time () - sim_counter .get_intial_time ()
526
+ ) / sim_counter .get_count ()
527
+ estimated_time = int (
528
+ (sim_counter .get_n_simulations () - sim_counter .get_count ())
529
+ * average_time
530
+ )
538
531
539
- print (
540
- f"Current iteration: { sim_idx } | "
541
- f"Time of Iteration { sim_end - sim_start :.2f} seconds to run." ,
542
- end = "\n " ,
532
+ sim_counter .reprint (
533
+ f"Current iteration: { sim_idx :06d} | "
534
+ f"Average Time per Iteration: { average_time :.3f} s | "
535
+ f"Estimated time left: { estimated_time } s" ,
536
+ end = "\r " ,
543
537
flush = True ,
544
538
)
545
539
@@ -1126,7 +1120,7 @@ def __get_light_indexes(input_file, output_file, append):
1126
1120
idx_i = MonteCarlo .__get_initial_sim_idx (f , light_mode = True )
1127
1121
with open (output_file , 'r' , encoding = "utf-8" ) as f :
1128
1122
idx_o = MonteCarlo .__get_initial_sim_idx (f , light_mode = True )
1129
- except OSError : # File not found, return 0
1123
+ except OSError : # File not found, return 0
1130
1124
idx_i = 0
1131
1125
idx_o = 0
1132
1126
else :
@@ -1182,12 +1176,51 @@ def __init__(self):
1182
1176
1183
1177
1184
1178
class SimCounter :
1185
- def __init__ (self , initial_count ):
1179
+ def __init__ (self , initial_count , n_simulations ):
1186
1180
self .count = initial_count
1181
+ self .n_simulations = n_simulations
1182
+ self ._last_print_len = 0 # used to print on the same line
1183
+ self .initial_time = time ()
1184
+
1185
+ def increment (self ):
1186
+ if self .count >= self .n_simulations :
1187
+ return - 1
1187
1188
1188
- def increment (self ) -> int :
1189
1189
self .count += 1
1190
1190
return self .count - 1
1191
1191
1192
- def get_count (self ) -> int :
1192
+ def get_count (self ):
1193
1193
return self .count
1194
+
1195
+ def get_n_simulations (self ):
1196
+ return self .n_simulations
1197
+
1198
+ def get_intial_time (self ):
1199
+ return self .initial_time
1200
+
1201
+ def reprint (self , msg , end = "\n " , flush = False ):
1202
+ """Prints a message on the same line as the previous one and replaces
1203
+ the previous message with the new one, deleting the extra characters
1204
+ from the previous message.
1205
+
1206
+ Parameters
1207
+ ----------
1208
+ msg : str
1209
+ Message to be printed.
1210
+ end : str, optional
1211
+ String appended after the message. Default is a new line.
1212
+ flush : bool, optional
1213
+ If True, the output is flushed. Default is False.
1214
+
1215
+ Returns
1216
+ -------
1217
+ None
1218
+ """
1219
+
1220
+ len_msg = len (msg )
1221
+ if len_msg < self ._last_print_len :
1222
+ msg += " " * (self ._last_print_len - len_msg )
1223
+ else :
1224
+ self ._last_print_len = len_msg
1225
+
1226
+ print (msg , end = end , flush = flush )
0 commit comments