Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions rocketpy/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,13 @@ def simulate(

self.__setup_files(append)

if parallel:
self.__run_in_parallel(n_workers)
else:
self.__run_in_serial()

self.__terminate_simulation()
try:
if parallel:
self.__run_in_parallel(n_workers)
else:
self.__run_in_serial()
finally:
self.__terminate_simulation()

def __setup_files(self, append):
"""
Expand Down Expand Up @@ -388,16 +389,22 @@ def _append_simulation_record(self, inputs_json, outputs_json):
previous_input_size = os.path.getsize(input_path)
except OSError:
previous_input_size = 0
try:
previous_output_size = os.path.getsize(output_path)
except OSError:
previous_output_size = 0

with open(input_path, "a", encoding="utf-8") as f:
f.write(inputs_json)

try:
with open(output_path, "a", encoding="utf-8") as f:
f.write(outputs_json)
except Exception:
except BaseException:
with open(input_path, "rb+") as f:
f.truncate(previous_input_size)
with open(output_path, "rb+") as f:
f.truncate(previous_output_size)
raise

def __run_in_serial(self):
Expand All @@ -413,6 +420,7 @@ def __run_in_serial(self):
n_simulations=self.number_of_simulations,
start_time=time(),
)
inputs_json = ""
try:
while sim_monitor.keep_simulating():
sim_monitor.increment()
Expand All @@ -423,15 +431,18 @@ def __run_in_serial(self):
outputs_json = self.__evaluate_flight_outputs(flight, sim_monitor.count)

self._append_simulation_record(inputs_json, outputs_json)
inputs_json = ""

sim_monitor.print_update_status()

sim_monitor.print_final_status()

except KeyboardInterrupt:
print("Keyboard interrupt received. Files saved.")
with open(self._error_file, "a", encoding="utf-8") as f:
f.write(inputs_json)
if inputs_json:
with open(self._error_file, "a", encoding="utf-8") as f:
f.write(inputs_json)
raise

except Exception as error:
print(f"Error on iteration {sim_monitor.count}: {error}")
Expand Down Expand Up @@ -471,20 +482,20 @@ def __run_in_parallel(self, n_workers=None):
processes = []
seeds = np.random.SeedSequence().spawn(n_workers)

for seed in seeds:
sim_producer = multiprocess.Process(
target=self.__sim_producer,
args=(
seed,
sim_monitor,
mutex,
simulation_error_event,
),
)
processes.append(sim_producer)
sim_producer.start()

try:
for seed in seeds:
sim_producer = multiprocess.Process(
target=self.__sim_producer,
args=(
seed,
sim_monitor,
mutex,
simulation_error_event,
),
)
sim_producer.start()
processes.append(sim_producer)

for sim_producer in processes:
sim_producer.join()

Expand All @@ -500,14 +511,12 @@ def __run_in_parallel(self, n_workers=None):

# Handle error from the main process
# pylint: disable=broad-except
except (Exception, KeyboardInterrupt) as error:
except (Exception, KeyboardInterrupt):
simulation_error_event.set()

for sim_producer in processes:
sim_producer.join()

if not isinstance(error, KeyboardInterrupt):
raise error
raise

def __validate_number_of_workers(self, n_workers):
if n_workers is None or n_workers > os.cpu_count():
Expand Down
Loading
Loading