@@ -72,6 +72,12 @@ class MonteCarlo:
72
72
Use help(MonteCarlo.plots) for more information.
73
73
number_of_simulations : int
74
74
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.
75
81
"""
76
82
77
83
def __init__ (self , filename , environment , rocket , flight , export_list = None ):
@@ -159,11 +165,11 @@ def simulate(self, number_of_simulations, append=False):
159
165
160
166
Notes
161
167
-----
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
163
169
interrupt the process and the files will be saved with the results
164
170
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`.
167
173
168
174
Important
169
175
---------
@@ -179,26 +185,28 @@ def simulate(self, number_of_simulations, append=False):
179
185
180
186
# initialize counters
181
187
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 ()
186
191
187
192
# Begin display
188
193
print ("Starting Monte Carlo analysis" , end = "\r " )
189
194
190
195
try :
191
- while self .iteration_count < self .number_of_simulations :
196
+ while self .__iteration_count < self .number_of_simulations :
192
197
self .__run_single_simulation (input_file , output_file )
193
198
except KeyboardInterrupt :
194
199
print ("Keyboard Interrupt, files saved." )
195
200
error_file .write (json .dumps (self ._inputs_dict , cls = RocketPyEncoder ) + "\n " )
196
201
self .__close_files (input_file , output_file , error_file )
197
202
except Exception as error :
198
- print (f"Error on iteration { self .iteration_count } : { error } " )
203
+ print (f"Error on iteration { self .__iteration_count } : { error } " )
199
204
error_file .write (json .dumps (self ._inputs_dict , cls = RocketPyEncoder ) + "\n " )
200
205
self .__close_files (input_file , output_file , error_file )
201
206
raise error
207
+ finally :
208
+ self .total_cpu_time = process_time () - self .__start_cpu_time
209
+ self .total_wall_time = time () - self .__start_time
202
210
203
211
self .__terminate_simulation (input_file , output_file , error_file )
204
212
@@ -220,7 +228,7 @@ def __run_single_simulation(self, input_file, output_file):
220
228
-------
221
229
None
222
230
"""
223
- self .iteration_count += 1
231
+ self .__iteration_count += 1
224
232
225
233
monte_carlo_flight = Flight (
226
234
rocket = self .rocket .create_object (),
@@ -249,12 +257,12 @@ def __run_single_simulation(self, input_file, output_file):
249
257
output_file = output_file ,
250
258
)
251
259
252
- average_time = (process_time () - self .start_cpu_time ) / self .iteration_count
260
+ average_time = (process_time () - self .__start_cpu_time ) / self .__iteration_count
253
261
estimated_time = int (
254
- (self .number_of_simulations - self .iteration_count ) * average_time
262
+ (self .number_of_simulations - self .__iteration_count ) * average_time
255
263
)
256
264
self .__reprint (
257
- f"Current iteration: { self .iteration_count :06d} | "
265
+ f"Current iteration: { self .__iteration_count :06d} | "
258
266
f"Average Time per Iteration: { average_time :.3f} s | "
259
267
f"Estimated time left: { estimated_time } s" ,
260
268
end = "\r " ,
@@ -300,9 +308,9 @@ def __terminate_simulation(self, input_file, output_file, error_file):
300
308
None
301
309
"""
302
310
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 "
306
314
)
307
315
308
316
self .__reprint (final_string + "Saving results." , flush = True )
0 commit comments