1
0
Fork 0
mirror of https://github.com/Findus23/halo_comparison.git synced 2024-09-19 16:03:50 +02:00
halo_comparison/spectra_computation.py

144 lines
4.5 KiB
Python
Raw Normal View History

2022-06-08 13:51:09 +02:00
# Call with spectra_computation.py <time> <kind>
# time = 'ics' for ICs, = 'z=1' for redshift z=1, = 'end' for final results
2022-06-08 13:51:09 +02:00
# kind = 'power' for power spectra comparing same resolution, 'cross' for comparing across all resolutions
import itertools
import subprocess
2022-06-08 13:51:09 +02:00
from multiprocessing import Pool, cpu_count
from sys import argv
from paths import base_dir, spectra_dir
2022-07-21 12:34:57 +02:00
from spectra_plot import waveforms
def run_spectra(
waveform: str, resolution_1: int, resolution_2: int, Lbox: int, time: str
):
2022-06-08 13:51:09 +02:00
print("starting")
setup_1 = f"{waveform}_{resolution_1}_{Lbox}"
setup_2 = f"{waveform}_{resolution_2}_{Lbox}"
# #For ICs: time == 'ics'
if time == "ics":
output_file = (
base_dir
/ f"spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_ics_{resolution_1}_{resolution_2}_cross_spectrum.txt"
)
if output_file.exists():
print(f"{output_file} already exists, skipping.")
return
subprocess.run(
[
str(spectra),
"--ngrid",
"2048",
"--format=4", # This seems to work, but is not as readable
"--output",
str(
base_dir
/ f"spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_ics_{resolution_1}_{resolution_2}"
),
"--input",
str(base_dir / f"{setup_1}/ics_{setup_1}.hdf5"),
"--input",
str(base_dir / f"{setup_2}/ics_{setup_2}.hdf5"),
],
check=True,
)
# #For evaluation of results at redshift z=1: time == 'z=1' | NOT ADAPTED FOR VSC5 YET!
elif time == "z=1":
output_file = (
base_dir
/ f"spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_a2_{resolution_1}_{resolution_2}_cross_spectrum.txt"
)
if output_file.exists():
print(f"{output_file} already exists, skipping.")
return
subprocess.run(
[
str(spectra),
"--ngrid",
"1024",
"--format=3",
"--output",
str(
base_dir
/ f"spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_a2_{resolution_1}_{resolution_2}"
),
"--input",
str(base_dir / f"{setup_1}/output_0002.hdf5"),
"--input",
str(base_dir / f"{setup_2}/output_0002.hdf5"),
],
check=True,
)
2022-06-08 13:51:09 +02:00
# #For evaluation of final results: time == 'end'
elif time == "end":
output_file = (
base_dir
/ f"spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_a4_{resolution_1}_{resolution_2}_cross_spectrum.txt"
)
if output_file.exists():
print(f"{output_file} already exists, skipping.")
return
subprocess.run(
[
str(spectra),
"--ngrid",
"2048",
"--format=3",
"--output",
str(
base_dir
/ f"spectra/{waveform}_{Lbox}/{waveform}_{Lbox}_a4_{resolution_1}_{resolution_2}"
),
"--input",
str(base_dir / f"{setup_1}/output_0004.hdf5"),
"--input",
str(base_dir / f"{setup_2}/output_0004.hdf5"),
],
check=True,
)
2022-06-08 13:51:09 +02:00
else:
raise ValueError(f"invalid time ({time})")
print("end")
2022-07-21 12:34:57 +02:00
def power_run(resolutions: list, Lbox: int, time: str):
2022-06-08 13:51:09 +02:00
args = []
2022-06-08 13:29:41 +02:00
for waveform in waveforms:
for resolution in resolutions:
args.append((waveform, resolution, resolution, Lbox, time))
2022-06-08 13:51:09 +02:00
return args
2022-07-21 12:34:57 +02:00
def cross_run(resolutions: list, Lbox: int, time: str):
2022-06-08 13:51:09 +02:00
args = []
2022-06-08 13:29:41 +02:00
for waveform in waveforms:
2022-06-08 13:51:09 +02:00
for res1, res2 in itertools.combinations(resolutions, 2):
args.append((waveform, res1, res2, Lbox, time))
2022-06-08 13:51:09 +02:00
return args
if __name__ == "__main__":
# input("are you sure you want to run this? This might need a large amount of memory")
2022-06-08 13:31:13 +02:00
Lbox = 100
resolutions = [128, 256, 512, 1024]
spectra = spectra_dir / "spectra"
2022-06-08 13:25:24 +02:00
time = argv[1]
if argv[2] == "power":
2022-07-21 12:34:57 +02:00
args = power_run(resolutions=resolutions, Lbox=Lbox, time=time)
elif argv[2] == "cross":
2022-07-21 12:34:57 +02:00
args = cross_run(resolutions=resolutions, Lbox=Lbox, time=time)
2022-06-08 13:51:09 +02:00
else:
raise ValueError("missing argv[2] (power|cross)")
with Pool(processes=1) as p:
2022-06-08 13:51:09 +02:00
p.starmap(run_spectra, args)