Skip to content

Commit a631313

Browse files
committed
feat: Update launcher.py
Now, each processor will create done.txt; used to check if a simulation is done. Update to handle sys.exit() instead of exit()
1 parent 8b75a20 commit a631313

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

launcher.py

+64-64
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
# Importamos #
5353

5454
import os
55+
import sys
5556
import subprocess
5657
from concurrent.futures import ProcessPoolExecutor
5758

@@ -132,7 +133,7 @@
132133
yes_no = input("¿Desea continuar? [y/[n]]\n")
133134
if yes_no.lower() not in ["y", "yes", "s", "si"]:
134135
print("Saliendo.")
135-
exit(1)
136+
sys.exit()
136137
## Ejecutable
137138
if not os.path.isfile(oprogr):
138139
msg = "Executable file {} does not exist.".format(oprogr)
@@ -146,7 +147,7 @@
146147
if tomfile and (not existe_otom):
147148
print("ERROR: Tau-Omega-Mass file {} does not exist.".format(otom))
148149
print("Saliendo.")
149-
exit(1)
150+
sys.exit()
150151
## Chaosfile
151152
if final_chaos is None:
152153
final_chaos = "sump"
@@ -174,7 +175,7 @@
174175
yes_no = input("Do you want to continue? y/[n]\n")
175176
if yes_no.lower() not in ["y", "yes", "s", "si"]:
176177
print("Saliendo.")
177-
exit(1)
178+
sys.exit()
178179

179180
if all_in_one and (not torque):
180181
print(
@@ -183,15 +184,15 @@
183184
yes_no = input("Do you want to continue? y/[n]\n")
184185
if yes_no.lower() not in ["y", "yes", "s", "si"]:
185186
print("Saliendo.")
186-
exit(1)
187+
sys.exit()
187188

188189
if torque and (not all_in_one):
189190
print("WARNING: All particles will be integrated independently,")
190191
print(" but each of them will induce torque to its asteroid.")
191192
yes_no = input("Do you want to continue? y/[n]\n")
192193
if yes_no.lower() not in ["y", "yes", "s", "si"]:
193194
print("Saliendo.")
194-
exit(1)
195+
sys.exit()
195196

196197

197198
# Leemos input
@@ -205,9 +206,11 @@
205206
# Obtener el número de líneas del archivo de partículas
206207
nsys = len(lines)
207208
if nsys == 0:
208-
print("No hay partículas para integrar.")
209-
exit(1)
210-
print("Cantidad total de partículas: {}".format(nsys))
209+
print("No hay partículas para integrar en el archivo %s." % partfile)
210+
print("Saliendo.")
211+
sys.exit()
212+
else:
213+
print("Cantidad total de partículas: {}".format(nsys))
211214

212215
# Ver si hay que hacer todo, o ya hay alguna realizadas
213216
new_simulation = True
@@ -229,46 +232,40 @@
229232
else "este directorio"
230233
)
231234
)
232-
if any(
233-
[
234-
os.path.isdir(os.path.join(wrk_dir, name))
235-
and name.startswith(pref)
236-
for name in os.listdir(wrk_dir)
237-
]
238-
):
239-
command = (
240-
f"find {pref}* -name 'chaos*{suffix}.out' "
241-
f"| sed -e 's/.*chaos\\([0-9]*\\){suffix}\\.out/\\1/' "
242-
f"| sort -n"
243-
)
244-
result = subprocess.run(
245-
command,
246-
shell=True,
247-
stdout=subprocess.PIPE,
248-
text=True,
249-
cwd=wrk_dir,
250-
)
251-
if result.returncode == 0:
252-
output_lines = result.stdout.splitlines()
253-
else:
254-
raise IOError("Error al leer integraciones ya realizadas.")
255-
done = set([int(cint) for cint in output_lines if cint != ""])
256-
missing_lines = [x for x in range(1, nsys + 1) if x not in done]
257-
print(" Cantidad de sistemas ya integrados: {}".format(len(done)))
258-
nsys = len(missing_lines)
259-
print(" Cantidad de sistemas a integrar: {}".format(nsys))
260-
new_simulation = False
235+
236+
# Lista de archivos done.txt
237+
file_list = []
238+
239+
# Recorre todas las subcarpetas en la carpeta raíz
240+
for subdir in os.listdir(wrk_dir):
241+
# Verifica si el nombre de la subcarpeta comienza con 'pref'
242+
this_list = []
243+
if subdir.startswith(pref):
244+
subdir_path = os.path.join(wrk_dir, subdir)
245+
# Recorre todos los archivos en la subcarpeta
246+
this_list = [os.path.join(subdir_path, "done.txt")
247+
for f in os.listdir(subdir_path)
248+
if f == "done.txt"]
249+
file_list.extend(this_list)
250+
251+
done = []
252+
for file in file_list:
253+
with open(file, "r") as f:
254+
done.extend([int(cint) for cint in f.readlines() if cint != ""])
255+
missing_lines = [x for x in range(1, nsys + 1) if x not in done]
256+
print(" Cantidad de sistemas ya integrados: {}".format(len(done)))
257+
nsys = len(missing_lines)
258+
print(" Cantidad de sistemas a integrar: {}".format(nsys))
259+
new_simulation = False
261260

262261

263262
# Hay que hacer?
264263
if len(missing_lines) == 0:
265264
print("Ya se han integrado todas las partículas.")
266-
exit(1)
267-
268-
269-
# Obtener el número de workers
270-
workers = min(max(1, min(int(workers), len(os.sched_getaffinity(0)))), nsys)
271-
print("Workers: {}\n".format(workers))
265+
else:
266+
# Obtener el número de workers
267+
workers = min(max(1, min(int(workers), len(os.sched_getaffinity(0)))), nsys)
268+
print("Workers: {}\n".format(workers))
272269

273270
# Argumentos. Estos son:
274271
args = " --nomapf"
@@ -326,16 +323,19 @@ def integrate_n(i):
326323
# ESTO SE ESTÁ EJECUTANDO EN LA SHELL #
327324
# print("Running: ./%s %s %s %s"%(program, args, this_args, my_line))
328325
# (Lines debe ser último porque termina en "\n") #
329-
p = subprocess.run(
330-
["./%s %s %s %s" % (program, args, this_args, my_line)],
331-
cwd=dirp,
332-
check=True,
333-
shell=True,
334-
)
335-
if p.returncode != 0:
326+
try:
327+
p = subprocess.run(
328+
["./%s %s %s %s" % (program, args, this_args, my_line)],
329+
cwd=dirp,
330+
check=True,
331+
shell=True,
332+
)
333+
except:
336334
print("The system %d has failed." % i)
337335
return
338336
print("System %d has been integrated." % i)
337+
with open(os.path.join(dirp, "done.txt"), "a") as f:
338+
f.write("%d\n" % i)
339339
return
340340

341341

@@ -383,13 +383,13 @@ def make_sum(final_chaos, suffix=""):
383383
outs.insert(-1, ".out")
384384
final_chaos = "".join(outs)
385385
command = ' '.join([file for file in file_list])
386-
p = subprocess.run(
387-
["cat %s > %s" % (command, final_chaos)],
388-
cwd=wrk_dir, check=True, shell=True
389-
)
390-
391-
if p.returncode != 0:
392-
print("Could not create % with cat."%final_chaos)
386+
try:
387+
p = subprocess.run(
388+
["cat %s > %s" % (command, final_chaos)],
389+
cwd=wrk_dir, check=True, shell=True
390+
)
391+
except:
392+
print("Could not create %s with cat."%final_chaos)
393393
print(" Trying with pure python...")
394394
with open(os.path.join(wrk_dir, final_chaos), "w") as f_out:
395395
for file in file_list:
@@ -441,13 +441,13 @@ def make_sal(salida, suffix=""):
441441
outs.insert(-1, ".out")
442442
salida = "".join(outs)
443443
command = ' '.join([file for file in file_list])
444-
p = subprocess.run(
445-
["cat %s > %s" % (command, salida)],
446-
cwd=wrk_dir, check=True, shell=True
447-
)
448-
449-
if p.returncode != 0:
450-
print("Could not create % with cat."%salida)
444+
try:
445+
p = subprocess.run(
446+
["cat %s > %s" % (command, salida)],
447+
cwd=wrk_dir, check=True, shell=True
448+
)
449+
except:
450+
print("Could not create %s with cat."%salida)
451451
print(" Trying with pure python...")
452452
with open(os.path.join(wrk_dir, salida), "w") as f_out:
453453
for file in file_list:

0 commit comments

Comments
 (0)