Skip to content

Commit b3dcfc6

Browse files
committed
Updated append mode
1 parent 918cbe0 commit b3dcfc6

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

docs/notebooks/monte_carlo_analysis/monte_carlo_class_usage.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@
585585
"metadata": {},
586586
"outputs": [],
587587
"source": [
588-
"test_dispersion.simulate(number_of_simulations=1000, append=False, parallel=True, n_workers=None)"
588+
"test_dispersion.simulate(number_of_simulations=1000, append=False, light_mode=True, parallel=True, n_workers=None)"
589589
]
590590
},
591591
{

requirements-optional.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ ipython
33
ipywidgets>=7.6.3
44
jsonpickle
55
timezonefinder
6-
imageio
6+
imageio
7+
h5py
8+
easygui

rocketpy/simulation/monte_carlo.py

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,17 @@ def __run_in_serial(self, append, light_mode):
221221
-------
222222
None
223223
"""
224-
225224
# Create data files for inputs, outputs and error logging
226225
open_mode = "a" if append else "w"
227226

228227
# Open files
229228
if light_mode:
229+
# get initial simulation index
230+
idx_i, idx_o = self.__get_light_indexes(
231+
self._input_file, self._output_file, append=append
232+
)
233+
234+
# open files in write/append mode
230235
input_file = open(self._input_file, open_mode, encoding="utf-8")
231236
output_file = open(self._output_file, open_mode, encoding="utf-8")
232237
error_file = open(self._error_file, open_mode, encoding="utf-8")
@@ -237,8 +242,8 @@ def __run_in_serial(self, append, light_mode):
237242
)
238243
error_file = open(self._error_file, open_mode, encoding="utf-8")
239244

240-
idx_i = self.__get_initial_sim_idx(input_file)
241-
idx_o = self.__get_initial_sim_idx(output_file)
245+
idx_i = self.__get_initial_sim_idx(input_file, light_mode=light_mode)
246+
idx_o = self.__get_initial_sim_idx(output_file, light_mode=light_mode)
242247

243248
if idx_i != idx_o:
244249
raise ValueError(
@@ -303,14 +308,6 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
303308
inputs_lock = manager.Lock()
304309
outputs_lock = manager.Lock()
305310
errors_lock = manager.Lock()
306-
queue = manager.JoinableQueue()
307-
308-
# Initialize queue
309-
for _ in range(self.number_of_simulations):
310-
queue.put("RUN")
311-
312-
for _ in range(n_workers):
313-
queue.put("STOP")
314311

315312
# Initialize write file
316313
open_mode = "a" if append else "w"
@@ -324,10 +321,15 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
324321

325322
# Initialize files
326323
if light_mode:
327-
with open(self._input_file, mode=open_mode) as _:
328-
pass # initialize file
329-
with open(self._output_file, mode=open_mode) as _:
330-
pass # initialize file
324+
# get initial simulation index
325+
idx_i, idx_o = self.__get_light_indexes(
326+
self._input_file, self._output_file, append=append
327+
)
328+
# open files in write/append mode
329+
with open(self._input_file, mode=open_mode) as f:
330+
pass # initialize file
331+
with open(self._output_file, mode=open_mode) as f:
332+
pass # initialize file
331333

332334
else:
333335
# Change file extensions to .h5
@@ -337,9 +339,9 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
337339

338340
# Initialize files and get initial simulation index
339341
with h5py.File(file_paths["input_file"], open_mode) as f:
340-
idx_i = self.__get_initial_sim_idx(f)
342+
idx_i = self.__get_initial_sim_idx(f, light_mode=light_mode)
341343
with h5py.File(file_paths["output_file"], open_mode) as f:
342-
idx_o = self.__get_initial_sim_idx(f)
344+
idx_o = self.__get_initial_sim_idx(f, light_mode=light_mode)
343345

344346
if idx_i != idx_o:
345347
raise ValueError(
@@ -352,6 +354,15 @@ def __run_in_parallel(self, append, light_mode, n_workers=None):
352354

353355
# Initialize simulation counter
354356
sim_counter = manager.SimCounter(idx_i)
357+
358+
queue = manager.JoinableQueue()
359+
360+
# Initialize queue
361+
for _ in range(self.number_of_simulations - sim_counter.get_count()):
362+
queue.put("RUN")
363+
364+
for _ in range(n_workers):
365+
queue.put("STOP")
355366

356367
print("\nStarting monte carlo analysis", end="\r")
357368
print(f"Number of simulations: {self.number_of_simulations}")
@@ -1065,7 +1076,7 @@ def inspect_object_attributes(obj):
10651076
return result
10661077

10671078
@staticmethod
1068-
def __get_initial_sim_idx(file):
1079+
def __get_initial_sim_idx(file, light_mode):
10691080
"""
10701081
Get the initial simulation index from the filename.
10711082
@@ -1079,12 +1090,51 @@ def __get_initial_sim_idx(file):
10791090
int
10801091
Initial simulation index.
10811092
"""
1093+
if light_mode:
1094+
lines = file.readlines()
1095+
return len(lines)
1096+
10821097
if len(file.keys()) == 0:
1083-
return 0
1098+
return 0 # light mode not using the simulation index
10841099

10851100
keys = [int(key) for key in file.keys()]
10861101
return max(keys) + 1
10871102

1103+
@staticmethod
1104+
def __get_light_indexes(input_file, output_file, append):
1105+
"""
1106+
Get the initial simulation index from the filename.
1107+
1108+
Parameters
1109+
----------
1110+
input_file : str
1111+
Name of the input file to be analyzed.
1112+
output_file : str
1113+
Name of the output file to be analyzed.
1114+
append : bool
1115+
If True, the results will be appended to the existing files. If
1116+
False, the files will be overwritten.
1117+
1118+
Returns
1119+
-------
1120+
int
1121+
Initial simulation index.
1122+
"""
1123+
if append:
1124+
try:
1125+
with open(input_file, 'r', encoding="utf-8") as f:
1126+
idx_i = MonteCarlo.__get_initial_sim_idx(f, light_mode=True)
1127+
with open(output_file, 'r', encoding="utf-8") as f:
1128+
idx_o = MonteCarlo.__get_initial_sim_idx(f, light_mode=True)
1129+
except OSError: # File not found, return 0
1130+
idx_i = 0
1131+
idx_o = 0
1132+
else:
1133+
idx_i = 0
1134+
idx_o = 0
1135+
1136+
return idx_i, idx_o
1137+
10881138
@staticmethod
10891139
def __dict_to_h5(h5_file, path, dic):
10901140
"""

0 commit comments

Comments
 (0)