Skip to content

Commit 3cf266b

Browse files
Fix parallel Monte Carlo simulation showing incorrect iteration count (#885)
* Fix parallel Monte Carlo iteration count display to show monotonically increasing completed count Co-authored-by: Gui-FernandesBR <[email protected]> * Add PR #806 to CHANGELOG.md Co-authored-by: Gui-FernandesBR <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Gui-FernandesBR <[email protected]>
1 parent 0c8790e commit 3cf266b

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Attention: The newest changes should be on top -->
4545

4646
### Fixed
4747

48+
- BUG: Fix parallel Monte Carlo simulation showing incorrect iteration count [#806](https://github.com/RocketPy-Team/RocketPy/pull/806)
4849
- BUG: Fix CSV column header spacing in FlightDataExporter [#864](https://github.com/RocketPy-Team/RocketPy/issues/864)
4950

5051

rocketpy/simulation/monte_carlo.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def __run_in_serial(self):
292292
with open(self.output_file, "a", encoding="utf-8") as f:
293293
f.write(outputs_json)
294294

295-
sim_monitor.print_update_status(sim_monitor.count)
295+
sim_monitor.print_update_status()
296296

297297
sim_monitor.print_final_status()
298298

@@ -430,7 +430,7 @@ def __sim_producer(self, seed, sim_monitor, mutex, error_event): # pylint: disa
430430
with open(self.output_file, "a", encoding="utf-8") as f:
431431
f.write(outputs_json)
432432

433-
sim_monitor.print_update_status(sim_idx)
433+
sim_monitor.print_update_status()
434434
finally:
435435
mutex.release()
436436

@@ -1208,6 +1208,7 @@ def __init__(self, initial_count, n_simulations, start_time):
12081208
self.count = initial_count
12091209
self.n_simulations = n_simulations
12101210
self.start_time = start_time
1211+
self.completed_count = 0
12111212

12121213
def keep_simulating(self):
12131214
return self.count < self.n_simulations
@@ -1216,25 +1217,24 @@ def increment(self):
12161217
self.count += 1
12171218
return self.count
12181219

1219-
def print_update_status(self, sim_idx):
1220+
def print_update_status(self):
12201221
"""Prints a message on the same line as the previous one and replaces
12211222
the previous message with the new one, deleting the extra characters
1222-
from the previous message.
1223-
1224-
Parameters
1225-
----------
1226-
sim_idx : int
1227-
Index of the current simulation.
1223+
from the previous message. This method increments the completed_count
1224+
to track how many simulations have finished (thread-safe when called
1225+
within a mutex-protected section).
12281226
12291227
Returns
12301228
-------
12311229
None
12321230
"""
1231+
self.completed_count += 1
12331232

1234-
average_time = (time() - self.start_time) / (self.count - self.initial_count)
1235-
estimated_time = int((self.n_simulations - self.count) * average_time)
1233+
average_time = (time() - self.start_time) / self.completed_count
1234+
remaining = self.n_simulations - self.initial_count - self.completed_count
1235+
estimated_time = int(remaining * average_time)
12361236

1237-
msg = f"Current iteration: {sim_idx:06d}"
1237+
msg = f"Iterations completed: {self.completed_count:06d}"
12381238
msg += f" | Average Time per Iteration: {average_time:.3f} s"
12391239
msg += f" | Estimated time left: {estimated_time} s"
12401240

0 commit comments

Comments
 (0)