mirror of
https://github.com/Findus23/halo_comparison.git
synced 2024-09-19 16:03:50 +02:00
Parallelize spectra
This commit is contained in:
parent
2c95a1b29e
commit
090f16acde
1 changed files with 61 additions and 35 deletions
|
@ -1,57 +1,80 @@
|
||||||
#Call with spectra_computation.py <time> <kind>
|
# Call with spectra_computation.py <time> <kind>
|
||||||
#time = 'ics' for ICs, = 'end' for final results
|
# time = 'ics' for ICs, = 'end' for final results
|
||||||
#kind = 'power' for power spectra comparing same resolution, 'cross' for comparing across all resolutions
|
# kind = 'power' for power spectra comparing same resolution, 'cross' for comparing across all resolutions
|
||||||
|
|
||||||
import itertools
|
import itertools
|
||||||
import re
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from multiprocessing import Pool, cpu_count
|
||||||
from sys import argv
|
from sys import argv
|
||||||
import wave
|
|
||||||
|
|
||||||
from paths import base_dir, spectra_dir
|
from paths import base_dir, spectra_dir
|
||||||
|
|
||||||
|
|
||||||
def run_spectra(waveform: str, resolution_1: int, resolution_2: int, Lbox: int, time: str):
|
def run_spectra(waveform: str, resolution_1: int, resolution_2: int, Lbox: int, time: str):
|
||||||
|
print("starting")
|
||||||
setup_1 = f'{waveform}_{resolution_1}_{Lbox}'
|
setup_1 = f'{waveform}_{resolution_1}_{Lbox}'
|
||||||
setup_2 = f'{waveform}_{resolution_2}_{Lbox}'
|
setup_2 = f'{waveform}_{resolution_2}_{Lbox}'
|
||||||
|
|
||||||
# #For ICs: time == 'ics'
|
# #For ICs: time == 'ics'
|
||||||
if time == 'ics':
|
if time == 'ics':
|
||||||
subprocess.run([str(spectra),
|
subprocess.run([
|
||||||
'--ngrid',
|
str(spectra),
|
||||||
'1024',
|
'--ngrid',
|
||||||
'--format=4', #This seems to work, but is not as readable
|
'1024',
|
||||||
'--output',
|
'--format=4', # This seems to work, but is not as readable
|
||||||
str(base_dir / f'spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_ics_{resolution_1}_{resolution_2}'),
|
'--output',
|
||||||
'--input',
|
str(base_dir / f'spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_ics_{resolution_1}_{resolution_2}'),
|
||||||
str(base_dir / f'{setup_1}/ics_{setup_1}.hdf5'),
|
'--input',
|
||||||
'--input',
|
str(base_dir / f'{setup_1}/ics_{setup_1}.hdf5'),
|
||||||
str(base_dir / f'{setup_2}/ics_{setup_2}.hdf5')
|
'--input',
|
||||||
|
str(base_dir / f'{setup_2}/ics_{setup_2}.hdf5')
|
||||||
], check=True)
|
], check=True)
|
||||||
|
print("end")
|
||||||
|
|
||||||
# #For evaluation of final results: time == 'end'
|
# #For evaluation of final results: time == 'end'
|
||||||
if time == 'end':
|
elif time == 'end':
|
||||||
subprocess.run([str(spectra),
|
subprocess.run([
|
||||||
'--ngrid',
|
str(spectra),
|
||||||
'1024',
|
'--ngrid',
|
||||||
'--format=3',
|
'1024',
|
||||||
'--output',
|
'--format=3',
|
||||||
str(base_dir / f'spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_a4_{resolution_1}_{resolution_2}'),
|
'--output',
|
||||||
'--input',
|
str(base_dir / f'spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_a4_{resolution_1}_{resolution_2}'),
|
||||||
str(base_dir / f'{setup_1}/output_0004.hdf5'),
|
'--input',
|
||||||
'--input',
|
str(base_dir / f'{setup_1}/output_0004.hdf5'),
|
||||||
str(base_dir / f'{setup_2}/output_0004.hdf5')
|
'--input',
|
||||||
|
str(base_dir / f'{setup_2}/output_0004.hdf5')
|
||||||
], check=True)
|
], check=True)
|
||||||
|
else:
|
||||||
|
raise ValueError(f"invalid time ({time})")
|
||||||
|
|
||||||
|
|
||||||
def power_run(waveforms: list, resolutions: list, Lbox: int, time: str):
|
def power_run(waveforms: list, resolutions: list, Lbox: int, time: str):
|
||||||
|
args = []
|
||||||
for waveform in waveforms:
|
for waveform in waveforms:
|
||||||
for resolution in resolutions:
|
for resolution in resolutions:
|
||||||
run_spectra(waveform=waveform, resolution_1=resolution, resolution_2=resolution, Lbox=Lbox, time=time)
|
args.append((
|
||||||
|
waveform,
|
||||||
|
resolution,
|
||||||
|
resolution,
|
||||||
|
Lbox,
|
||||||
|
time
|
||||||
|
))
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
def cross_run(waveforms: list, resolutions: list, Lbox: int, time: str):
|
def cross_run(waveforms: list, resolutions: list, Lbox: int, time: str):
|
||||||
|
args = []
|
||||||
for waveform in waveforms:
|
for waveform in waveforms:
|
||||||
for resolution_pair in itertools.combinations(resolutions, 2):
|
for res1, res2 in itertools.combinations(resolutions, 2):
|
||||||
run_spectra(waveform=waveform, resolution_1=resolution_pair[0], resolution_2=resolution_pair[1], Lbox=Lbox, time=time)
|
args.append({
|
||||||
|
"waveform": waveform,
|
||||||
|
"resolution_1": res1,
|
||||||
|
"resolution_2": res2,
|
||||||
|
"Lbox": Lbox,
|
||||||
|
"time": time
|
||||||
|
})
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -64,8 +87,11 @@ if __name__ == '__main__':
|
||||||
time = argv[1]
|
time = argv[1]
|
||||||
|
|
||||||
if argv[2] == 'power':
|
if argv[2] == 'power':
|
||||||
power_run(waveforms=waveforms, resolutions=resolutions, Lbox=Lbox, time=time)
|
args = power_run(waveforms=waveforms, resolutions=resolutions, Lbox=Lbox, time=time)
|
||||||
|
|
||||||
elif argv[2] == 'cross':
|
elif argv[2] == 'cross':
|
||||||
cross_run(waveforms=waveforms, resolutions=resolutions, Lbox=Lbox, time=time)
|
args = cross_run(waveforms=waveforms, resolutions=resolutions, Lbox=Lbox, time=time)
|
||||||
|
else:
|
||||||
|
raise ValueError("missing argv[2] (power|cross)")
|
||||||
|
with Pool(processes=cpu_count() // 2) as p:
|
||||||
|
p.starmap(run_spectra, args)
|
||||||
|
|
Loading…
Reference in a new issue