Skip to content

Commit 4b7278c

Browse files
ENH: Slightly changes the MonteCarlo class to track total wall time and total CPU time
1 parent ac34bc9 commit 4b7278c

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

rocketpy/simulation/monte_carlo.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ class MonteCarlo:
7272
Use help(MonteCarlo.plots) for more information.
7373
number_of_simulations : int
7474
Number of simulations to be run.
75+
total_wall_time : float
76+
The total elapsed real-world time from the start to the end of the
77+
simulation, including all waiting times and delays.
78+
total_cpu_time : float
79+
The total CPU time spent running the simulation, excluding the time
80+
spent waiting for I/O operations or other processes to complete.
7581
"""
7682

7783
def __init__(self, filename, environment, rocket, flight, export_list=None):
@@ -159,11 +165,11 @@ def simulate(self, number_of_simulations, append=False):
159165
160166
Notes
161167
-----
162-
If you start running the simulations but needs to stop it, you can
168+
If you need to stop the simulations after starting them, you can
163169
interrupt the process and the files will be saved with the results
164170
until the last iteration. You can then load the results and continue
165-
the simulation by running the method simulate again with the same
166-
number of simulations and setting `append=True`.
171+
the simulation by running the ``simulate`` method again with the
172+
same number of simulations and setting `append=True`.
167173
168174
Important
169175
---------
@@ -179,26 +185,28 @@ def simulate(self, number_of_simulations, append=False):
179185

180186
# initialize counters
181187
self.number_of_simulations = number_of_simulations
182-
# TODO: these should be private attributes, double underscore
183-
self.iteration_count = self.num_of_loaded_sims if append else 0
184-
self.start_time = time()
185-
self.start_cpu_time = process_time()
188+
self.__iteration_count = self.num_of_loaded_sims if append else 0
189+
self.__start_time = time()
190+
self.__start_cpu_time = process_time()
186191

187192
# Begin display
188193
print("Starting Monte Carlo analysis", end="\r")
189194

190195
try:
191-
while self.iteration_count < self.number_of_simulations:
196+
while self.__iteration_count < self.number_of_simulations:
192197
self.__run_single_simulation(input_file, output_file)
193198
except KeyboardInterrupt:
194199
print("Keyboard Interrupt, files saved.")
195200
error_file.write(json.dumps(self._inputs_dict, cls=RocketPyEncoder) + "\n")
196201
self.__close_files(input_file, output_file, error_file)
197202
except Exception as error:
198-
print(f"Error on iteration {self.iteration_count}: {error}")
203+
print(f"Error on iteration {self.__iteration_count}: {error}")
199204
error_file.write(json.dumps(self._inputs_dict, cls=RocketPyEncoder) + "\n")
200205
self.__close_files(input_file, output_file, error_file)
201206
raise error
207+
finally:
208+
self.total_cpu_time = process_time() - self.__start_cpu_time
209+
self.total_wall_time = time() - self.__start_time
202210

203211
self.__terminate_simulation(input_file, output_file, error_file)
204212

@@ -220,7 +228,7 @@ def __run_single_simulation(self, input_file, output_file):
220228
-------
221229
None
222230
"""
223-
self.iteration_count += 1
231+
self.__iteration_count += 1
224232

225233
monte_carlo_flight = Flight(
226234
rocket=self.rocket.create_object(),
@@ -249,12 +257,12 @@ def __run_single_simulation(self, input_file, output_file):
249257
output_file=output_file,
250258
)
251259

252-
average_time = (process_time() - self.start_cpu_time) / self.iteration_count
260+
average_time = (process_time() - self.__start_cpu_time) / self.__iteration_count
253261
estimated_time = int(
254-
(self.number_of_simulations - self.iteration_count) * average_time
262+
(self.number_of_simulations - self.__iteration_count) * average_time
255263
)
256264
self.__reprint(
257-
f"Current iteration: {self.iteration_count:06d} | "
265+
f"Current iteration: {self.__iteration_count:06d} | "
258266
f"Average Time per Iteration: {average_time:.3f} s | "
259267
f"Estimated time left: {estimated_time} s",
260268
end="\r",
@@ -300,9 +308,9 @@ def __terminate_simulation(self, input_file, output_file, error_file):
300308
None
301309
"""
302310
final_string = (
303-
f"Completed {self.iteration_count} iterations. Total CPU time: "
304-
f"{process_time() - self.start_cpu_time:.1f} s. Total wall time: "
305-
f"{time() - self.start_time:.1f} s\n"
311+
f"Completed {self.__iteration_count} iterations. Total CPU time: "
312+
f"{process_time() - self.__start_cpu_time:.1f} s. Total wall time: "
313+
f"{time() - self.__start_time:.1f} s\n"
306314
)
307315

308316
self.__reprint(final_string + "Saving results.", flush=True)

0 commit comments

Comments
 (0)