17
17
import json
18
18
import os
19
19
import pickle
20
+ import warnings
20
21
from copy import deepcopy
21
22
from pathlib import Path
22
- import warnings
23
23
from time import process_time , time
24
24
25
25
import h5py
@@ -102,7 +102,7 @@ def __init__(
102
102
export_list = None ,
103
103
batch_path = None ,
104
104
export_sample_time = 0.1 ,
105
- ): # pylint: disable=too-many-statements
105
+ ): # pylint: disable=too-many-statements
106
106
"""
107
107
Initialize a MonteCarlo object.
108
108
@@ -165,7 +165,7 @@ def __init__(
165
165
166
166
if not os .path .exists (self .batch_path ):
167
167
os .makedirs (self .batch_path )
168
-
168
+
169
169
self .export_list = self .__check_export_list (export_list )
170
170
171
171
try :
@@ -191,7 +191,6 @@ def simulate(
191
191
light_mode = False ,
192
192
parallel = False ,
193
193
n_workers = None ,
194
-
195
194
): # pylint: disable=too-many-statements
196
195
"""
197
196
Runs the Monte Carlo simulation and saves all data.
@@ -246,7 +245,6 @@ def simulate(
246
245
else :
247
246
self .__run_in_serial (append , light_mode = light_mode )
248
247
249
-
250
248
def __run_in_serial (self , append , light_mode ):
251
249
"""
252
250
Runs the monte carlo simulation in serial mode.
@@ -468,7 +466,7 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
468
466
print (f"Number of simulations: { self .number_of_simulations } " )
469
467
470
468
# Creates n_workers processes then starts them
471
- for i in range (n_workers - 2 ): # leave 2 cores for the writer workers
469
+ for _ in range (n_workers - 2 ): # leave 2 cores for the writer workers
472
470
p = Process (
473
471
target = self .__run_simulation_worker ,
474
472
args = (
@@ -542,27 +540,24 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
542
540
input_writer_stop_event .set ()
543
541
results_writer_stop_event .set ()
544
542
545
- print ("Waiting for writer workers to join." )
546
- # join the writer workers
547
543
input_writer .join ()
548
544
results_writer .join ()
549
545
550
546
self .number_of_simulations = sim_counter .get_count ()
551
547
552
548
parallel_end = time ()
553
549
554
- print ("-" * 80 + "\n All workers joined, simulation complete." )
550
+ print ("-" * 80 + "\n " )
551
+ print ("All workers joined, simulation complete." )
555
552
print (
556
553
f"In total, { sim_counter .get_count () - idx_i } simulations were performed."
557
554
)
558
555
print (
559
- "Simulation took" ,
560
- parallel_end - parallel_start_time ,
561
- "seconds to run." ,
556
+ f"Simulation took { (parallel_end - parallel_start_time ):.2f} seconds to run."
562
557
)
563
558
564
559
finally :
565
- # ensure shared memory is realeased
560
+ # ensure shared memory is released
566
561
shared_inputs_buffer .close ()
567
562
shared_results_buffer .close ()
568
563
shared_inputs_buffer .unlink ()
@@ -649,22 +644,14 @@ def __run_simulation_worker(
649
644
if sim_idx == - 1 :
650
645
break
651
646
652
- env = sto_env .create_object ()
653
- rocket = sto_rocket .create_object ()
654
- rail_length = sto_flight ._randomize_rail_length ()
655
- inclination = sto_flight ._randomize_inclination ()
656
- heading = sto_flight ._randomize_heading ()
657
- initial_solution = sto_flight .initial_solution
658
- terminate_on_apogee = sto_flight .terminate_on_apogee
659
-
660
647
monte_carlo_flight = Flight (
661
- rocket = rocket ,
662
- environment = env ,
663
- rail_length = rail_length ,
664
- inclination = inclination ,
665
- heading = heading ,
666
- initial_solution = initial_solution ,
667
- terminate_on_apogee = terminate_on_apogee ,
648
+ rocket = sto_rocket . create_object () ,
649
+ environment = sto_env . create_object () ,
650
+ rail_length = sto_flight . _randomize_rail_length () ,
651
+ inclination = sto_flight . _randomize_inclination () ,
652
+ heading = sto_flight . _randomize_heading () ,
653
+ initial_solution = sto_flight . initial_solution ,
654
+ terminate_on_apogee = sto_flight . terminate_on_apogee ,
668
655
)
669
656
670
657
# Export to file
@@ -793,7 +780,7 @@ def __run_single_simulation(
793
780
terminate_on_apogee = self .flight .terminate_on_apogee ,
794
781
)
795
782
796
- self . _inputs_dict = dict (
783
+ inputs_dict = dict (
797
784
item
798
785
for d in [
799
786
self .environment .last_rnd_dict ,
@@ -802,7 +789,13 @@ def __run_single_simulation(
802
789
]
803
790
for item in d .items ()
804
791
)
805
- self ._inputs_dict ["idx" ] = sim_idx
792
+ inputs_dict ["idx" ] = sim_idx
793
+
794
+ inputs_dict = MonteCarlo .prepare_export_data (
795
+ inputs_dict , self .export_sample_time , remove_functions = True
796
+ )
797
+
798
+ self ._inputs_dict = inputs_dict
806
799
807
800
# Export inputs and outputs to file
808
801
if light_mode :
0 commit comments