Skip to content

Commit

Permalink
ENH: Update file naming convention for MonteCarlo simulation outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gui-FernandesBR committed Mar 5, 2024
1 parent 4ac7187 commit 8fba210
Showing 1 changed file with 25 additions and 29 deletions.
54 changes: 25 additions & 29 deletions rocketpy/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MonteCarlo:
When running a new simulation, this parameter represents the
initial part of the export filenames. For example, if the value
is 'filename', the exported output files will be named
'filename.disp_outputs.txt'. When analyzing the results of a
'filename.outputs.txt'. When analyzing the results of a
previous simulation, this parameter should be set to the .txt
file containing the outputs of the previous monte carlo analysis.
environment : StochasticEnvironment
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(self, filename, environment, rocket, flight, export_list=None):
When running a new simulation, this parameter represents the
initial part of the export filenames. For example, if the value
is 'filename', the exported output files will be named
'filename.disp_outputs.txt'. When analyzing the results of a
'filename.outputs.txt'. When analyzing the results of a
previous simulation, this parameter should be set to the .txt
file containing the outputs of the previous monte carlo
analysis.
Expand Down Expand Up @@ -117,17 +117,17 @@ def __init__(self, filename, environment, rocket, flight, export_list=None):
try:
self.import_inputs()
except FileNotFoundError:
self._input_file = f"{filename}.disp_inputs.txt"
self._input_file = f"{filename}.inputs.txt"

try:
self.import_outputs()
except FileNotFoundError:
self._output_file = f"{filename}.disp_outputs.txt"
self._output_file = f"{filename}.outputs.txt"

try:
self.import_errors()
except FileNotFoundError:
self._error_file = f"{filename}.disp_errors.txt"
self._error_file = f"{filename}.errors.txt"

def simulate(self, number_of_simulations, append=False):
"""
Expand Down Expand Up @@ -241,9 +241,9 @@ def __finalize_simulation(self, input_file, output_file, error_file):
self.__close_files(input_file, output_file, error_file)

# resave the files on self and calculate post simulation attributes
self.input_file = f"{self.filename}.disp_inputs.txt"
self.output_file = f"{self.filename}.disp_outputs.txt"
self.error_file = f"{self.filename}.disp_errors.txt"
self.input_file = f"{self.filename}.inputs.txt"
self.output_file = f"{self.filename}.outputs.txt"
self.error_file = f"{self.filename}.errors.txt"

print(f"Results saved to {self._output_file}")

Expand Down Expand Up @@ -425,9 +425,9 @@ def set_inputs_log(self):
"""Sets inputs_log from a file into an attribute for easy access"""
# TODO: add pickle package to deal with parachute triggers and Function objects
self.inputs_log = []
with open(self.input_file, mode="r", encoding="utf-8") as disp_inputs:
with open(self.input_file, mode="r", encoding="utf-8") as inputs:
# Loop through each line in the file
for line in disp_inputs:
for line in inputs:
# swap "<" and ">" to "'"
# this is done to interpret the trigger functions
# Find the index of the first and last occurrences of '<' and '>'
Expand Down Expand Up @@ -455,8 +455,8 @@ def set_outputs_log(self):
"""Sets outputs_log from a file into an attribute for easy access"""
self.outputs_log = []
# Loop through each line in the file
with open(self.output_file, mode="r", encoding="utf-8") as disp_outputs:
for line in disp_outputs:
with open(self.output_file, mode="r", encoding="utf-8") as outputs:
for line in outputs:
# Skip comments lines
if line[0] != "{":
continue
Expand All @@ -469,8 +469,8 @@ def set_errors_log(self):
"""Sets errors_log log from a file into an attribute for easy access"""
self.errors_log = []
# Loop through each line in the file
with open(self.error_file, mode="r", encoding="utf-8") as disp_errors:
for line in disp_errors:
with open(self.error_file, mode="r", encoding="utf-8") as errors:
for line in errors:
# Skip comments lines
if line[0] != "{":
continue
Expand All @@ -484,8 +484,8 @@ def set_num_of_loaded_sims(self):
# Calculate the number of flights simulated
self.num_of_loaded_sims = 0
# Loop through each line in the file
with open(self.output_file, mode="r", encoding="utf-8") as disp_outputs:
for line in disp_outputs:
with open(self.output_file, mode="r", encoding="utf-8") as outputs:
for line in outputs:
# Skip comments lines
if line[0] != "{":
continue
Expand Down Expand Up @@ -530,12 +530,12 @@ def import_outputs(self, filename=None):
filepath = filename if filename else self.filename

try:
with open(f"{filepath}.disp_outputs.txt", "r+", encoding="utf-8"):
self.output_file = f"{filepath}.disp_outputs.txt"
with open(f"{filepath}.outputs.txt", "r+", encoding="utf-8"):
self.output_file = f"{filepath}.outputs.txt"
# Print the number of flights simulated
print(
f"A total of {self.num_of_loaded_sims} simulations results were loaded from"
f" the following output file: {filepath}.disp_outputs.txt\n"
f" the following output file: {filepath}.outputs.txt\n"
)
except FileNotFoundError:
with open(filepath, "r+", encoding="utf-8"):
Expand Down Expand Up @@ -564,12 +564,10 @@ def import_inputs(self, filename=None):
filepath = filename if filename else self.filename

try:
with open(f"{filepath}.disp_inputs.txt", "r+", encoding="utf-8"):
self.input_file = f"{filepath}.disp_inputs.txt"
with open(f"{filepath}.inputs.txt", "r+", encoding="utf-8"):
self.input_file = f"{filepath}.inputs.txt"
# Print the number of flights simulated
print(
f"The following input file was imported: {filepath}.disp_inputs.txt\n"
)
print(f"The following input file was imported: {filepath}.inputs.txt\n")
except FileNotFoundError:
with open(filepath, "r+", encoding="utf-8"):
self.input_file = filepath
Expand All @@ -594,12 +592,10 @@ def import_errors(self, filename=None):
filepath = filename if filename else self.filename

try:
with open(f"{filepath}.disp_errors.txt", "r+", encoding="utf-8"):
self.error_file = f"{filepath}.disp_errors.txt"
with open(f"{filepath}.errors.txt", "r+", encoding="utf-8"):
self.error_file = f"{filepath}.errors.txt"
# Print the number of flights simulated
print(
f"The following error file was imported: {filepath}.disp_errors.txt\n"
)
print(f"The following error file was imported: {filepath}.errors.txt\n")
except FileNotFoundError:
with open(filepath, "r+", encoding="utf-8"):
self.error_file = filepath
Expand Down

0 comments on commit 8fba210

Please sign in to comment.