black without mortimer

This commit is contained in:
Noe Brucy
2022-11-28 18:01:18 +01:00
parent 7548ef7e0a
commit fa396178c6
14 changed files with 371 additions and 304 deletions
+88 -78
View File
@@ -30,7 +30,7 @@ from pymses.analysis import (
cube3d,
)
from pymses.filters import CellsToPoints, RegionFilter
from pymses.sources.hop.hop import HOP
from pymses.sources.hop.hop import HOP
try:
from fil_finder import FilFinder2D
@@ -220,9 +220,9 @@ def pspec2d(map2D):
pmap = pspec.pcube(fmap)
# Use the power map and the fft to compute the powerspectrum
# This is typically an histogram of k weighted by the fourier transform value
pspec, kbins, pspec2, fbins = pspec.pspectrum(pmap, kmap, kbins, 1, 0)
power, kbins, power2, fbins = pspec.pspectrum(pmap, kmap, kbins, 1, 0)
# Return bin center and power spectrum
return 0.5 * (kbins[1:] + kbins[:-1]), pspec
return 0.5 * (kbins[1:] + kbins[:-1]), power
def degrade_map(dmap, nnew, integrate=False):
@@ -387,11 +387,12 @@ class SnapshotProcessor(HDF5Container):
# Convert time unit
if isinstance(unit_time, str):
factor = self.get_nml(unit_time)
unit_time = U.Unit(
name=os.path.basename(unit_time),
base_unit=self.info["unit_time"],
coeff=factor)
factor = self.get_nml(unit_time)
unit_time = U.Unit(
name=os.path.basename(unit_time),
base_unit=self.info["unit_time"],
coeff=factor,
)
time_in_right_unit = self.time * self.info["unit_time"].express(unit_time)
if self.params.astrophysix.generate:
@@ -405,17 +406,16 @@ class SnapshotProcessor(HDF5Container):
try:
self.init_pymses()
except:
except IOError:
self.logger.error("Pymses not initialized", exc_info=1)
self.def_rules()
def init_pymses(self):
# If ratarmount was used
if os.path.exists(f"{self.path}/output_{self.num:05}/output_{self.num:05}"):
path = f"{self.path}/output_{self.num:05}"
else:
else:
path = self.path
self._ro = pymses.RamsesOutput(
path,
@@ -494,21 +494,21 @@ class SnapshotProcessor(HDF5Container):
far_cut_depth=distance,
up_vector=ax_v,
map_max_size=self.params.pymses.map_size,
)
)
# Initialize HDF5 group
try:
self.open()
if "/maps" not in self.save:
self.save.create_group("/", "maps", "2D maps")
self.save.root.maps._v_attrs.center = center
self.save.root.maps._v_attrs.radius = self._radius
self.save.root.maps._v_attrs.im_extent = im_extent
except:
self.logger.error("Error in HDF5", exc_info=1)
raise
finally:
self.close()
try:
self.open()
if "/maps" not in self.save:
self.save.create_group("/", "maps", "2D maps")
self.save.root.maps._v_attrs.center = center
self.save.root.maps._v_attrs.radius = self._radius
self.save.root.maps._v_attrs.im_extent = im_extent
except Exception() as e:
self.logger.error("Error in HDF5", exc_info=1)
raise e
finally:
self.close()
def load_data(self, points_src, filename, save, keys=None):
"""
@@ -613,13 +613,13 @@ class SnapshotProcessor(HDF5Container):
return pos
def getter_vect_r(self, dset, name_vect):
""" Radial component of a vector """
"""Radial component of a vector"""
r = self.getter_pos_disk(dset)[:, :2]
ur = np.transpose((np.transpose(r) / np.sqrt(np.sum(r ** 2, axis=1))))
return np.einsum("ij, ij -> i", dset[name_vect][:, :2], ur)
def getter_vect_phi(self, dset, name_vect):
""" Azimuthal component of a vector """
"""Azimuthal component of a vector"""
r = self.getter_pos_disk(dset)[:, :2]
r_norm = np.sqrt(np.sum(r ** 2, axis=1))
@@ -638,7 +638,7 @@ class SnapshotProcessor(HDF5Container):
return pos
def oct_getter_vect_r(self, dset, name_vect):
""" Radial component of a vector """
"""Radial component of a vector"""
r = self.oct_getter_pos_disk(dset)[:, :, :2]
ur = np.transpose(
(np.transpose(r, (2, 0, 1)) / np.sqrt(np.sum(r ** 2, axis=2))), (1, 2, 0)
@@ -646,7 +646,7 @@ class SnapshotProcessor(HDF5Container):
return np.einsum("ikj, ikj -> ik", dset[name_vect][:, :, :2], ur)
def oct_getter_vect_phi(self, dset, name_vect):
""" Azimuthal component of a vector """
"""Azimuthal component of a vector"""
r = self.oct_getter_pos_disk(dset)[:, :, :2]
r_norm = np.sqrt(np.sum(r ** 2, axis=2))
@@ -660,7 +660,7 @@ class SnapshotProcessor(HDF5Container):
return self.oct_getter_vect_r(dset, "vel")
def oct_getter_vphi(self, dset):
""" Azimuthal velocity """
"""Azimuthal velocity"""
return self.oct_getter_vect_phi(dset, "vel")
def datacube(self, getter, level=None, unit=U.none):
@@ -685,22 +685,19 @@ class SnapshotProcessor(HDF5Container):
if level is None:
level = self.get_nml("amr_params/levelmin")
size = 1.0
cam = Camera(
center=self.params.pymses.center,
line_of_sight_axis="z",
region_size=[size, size],
distance=size / 2.0,
far_cut_depth=size / 2.0,
up_vector="y",
map_max_size=2 ** level,
)
center=self.params.pymses.center,
line_of_sight_axis="z",
region_size=[size, size],
distance=size / 2.0,
far_cut_depth=size / 2.0,
up_vector="y",
map_max_size=2 ** level,
)
cube = extractor.process(
cam, cube_size=1.0, resolution=2 ** level
).data
cube = extractor.process(cam, cube_size=1.0, resolution=2 ** level).data
return cube
def slice(self, getter, ax_los="z", z=0.0, unit=U.none):
@@ -731,8 +728,9 @@ class SnapshotProcessor(HDF5Container):
datamap = slicing.SliceMap(self._amr, self._cam[ax_los], op, z=z)
return datamap.map.T
def ax_avg(self, oct_getter, ax_los, unit=U.none, mass_weighted=True, surf_qty=False):
def ax_avg(
self, oct_getter, ax_los, unit=U.none, mass_weighted=True, surf_qty=False
):
"""
Map of the average of a quantity (given by getter) along an axis (ax_los)
Returns 2D array if getter returns a scalar quantity
@@ -847,7 +845,15 @@ class SnapshotProcessor(HDF5Container):
self.unload_cells()
return data
def vol_pdf(self, getter, bins=100, old_unit=None, unit=None, logbins=False, weight_func=vol_func):
def vol_pdf(
self,
getter,
bins=100,
old_unit=None,
unit=None,
logbins=False,
weight_func=vol_func,
):
self.load_cells()
data = getter(self.cells)
if old_unit is not None and unit is not None:
@@ -1057,7 +1063,9 @@ class SnapshotProcessor(HDF5Container):
dmap_omega = rt_omega.process(self._cam[ax_los]).map.T
return dmap_omega
def _toomreQ_disk(self, ax_los, omega_approx=False, G1_units=False, coarsen_factor=1):
def _toomreQ_disk(
self, ax_los, omega_approx=False, G1_units=False, coarsen_factor=1
):
"""
Compute the Toomre Q parameter
"""
@@ -1404,8 +1412,8 @@ class SnapshotProcessor(HDF5Container):
def _sinks(self):
csv_name = f"{self.path}/output_{self.num:05}/sink_{self.num:05}.csv"
if not os.path.exists(csv_name): # If ratarmount was used
csv_name = f"{self.path}/output_{self.num:05}/output_{self.num:05}/sink_{self.num:05}.csv"
if not os.path.exists(csv_name): # If ratarmount was used
csv_name = f"{self.path}/output_{self.num:05}/output_{self.num:05}/sink_{self.num:05}.csv"
f = open(csv_name)
first_line = f.readline()
@@ -1449,11 +1457,9 @@ class SnapshotProcessor(HDF5Container):
def pspec(self, overwrite_file=False, **kwargs):
outfile = self.pspec_filename
if overwrite_file or not os.path.exists(self.pspec_filename):
pspec.pspec(repo=self.path, iouts=[self.num], outfile=outfile, **kwargs)
pspec.pspec(repo=self.path, iouts=[self.num], outfile=outfile, **kwargs)
return np.array([self.pspec_filename])
def _write_particles(self):
"""Ensure particles are written in the hdf5 file"""
if not os.path.exists(self.parts_filename) and not self.parts_loaded:
@@ -1604,50 +1610,57 @@ class SnapshotProcessor(HDF5Container):
"""
# Selection of cells
mask = self.cells["rho"] * self.info["unit_density"].express(U.H_cc) > threshold_density
mask = (
self.cells["rho"] * self.info["unit_density"].express(U.H_cc)
> threshold_density
)
ncells = np.sum(mask)
# fill the matrice with ID, x,y,z and masses of particles
cells_group = np.zeros((ncells, 6))
cells_group[:,0] = np.arange(ncells) # index
position = self.cells["pos"][mask] * self.info["unit_length"].express(U.pc)
cells_group[:,1:4] = position # position
density = self.cells["rho"][mask] * self.info["unit_density"].express(U.Msun / U.pc**3)
size = self.cells["dx"][mask]*self.info["unit_length"].express(U.pc)
cells_group[:,4] = density * size**3 # mass
cells_group[:, 0] = np.arange(ncells) # index
position = self.cells["pos"][mask] * self.info["unit_length"].express(U.pc)
cells_group[:, 1:4] = position # position
density = self.cells["rho"][mask] * self.info["unit_density"].express(
U.Msun / U.pc ** 3
)
size = self.cells["dx"][mask] * self.info["unit_length"].express(U.pc)
cells_group[:, 4] = density * size ** 3 # mass
# save file.txt
head = str(ncells)
np.savetxt(
self.filename[:-3] + '_hop.txt',
self.filename[:-3] + "_hop.txt",
cells_group[:, :-1],
fmt='%10d %.10e %.10e %.10e %.10e',
fmt="%10d %.10e %.10e %.10e %.10e",
header=head,
delimiter=' ',
comments=' '
)
delimiter=" ",
comments=" ",
)
# save file.den
f = open(self.filename[:-3] + '_hop.den','wb')
f.write(pack('I', ncells))
f = open(self.filename[:-3] + "_hop.den", "wb")
f.write(pack("I", ncells))
self.cells["rho"][mask].astype("f").tofile(f)
f.close()
# exec HOP algo
h = HOP(self.filename[:-3] + '_hop.txt', os.path.dirname(self.filename))
h.process_hop()
h = HOP(self.filename[:-3] + "_hop.txt", os.path.dirname(self.filename))
h.process_hop()
# get the igroup array
group_ids = h.get_group_ids()
# sort it and apply the sorting to the coordinates
# this means that the particules of group 1 are written first then of group 2 etc...
# this means that the particules of group 1 are written first then of group 2 etc...
ind_sort = np.argsort(group_ids)
cells_group = cells_group[ind_sort]
cells_group[6] = group_ids[ind_sort]
# Make it a pandas' DataFrame
cells_group = pd.DataFrame(cells_group, header=["id", "x", "y", "z", "mass", "group"])
cells_group = pd.DataFrame(
cells_group, header=["id", "x", "y", "z", "mass", "group"]
)
self.clumps = cells_group
@@ -1658,7 +1671,6 @@ class SnapshotProcessor(HDF5Container):
cells_group = self.make_clump_hop()
cells_group.groupby("group")
def def_rules(self):
self.rules = {
@@ -1714,8 +1726,7 @@ class SnapshotProcessor(HDF5Container):
dependencies=["slice_rho"],
unit=self.info["unit_pressure"] / self.info["unit_density"],
),
"levels": Rule(self._levels, "Max level within line of sight", "/maps"
),
"levels": Rule(self._levels, "Max level within line of sight", "/maps"),
"jeans": Rule(
self._jeans,
"Jeans length slice",
@@ -1769,8 +1780,7 @@ class SnapshotProcessor(HDF5Container):
},
),
"pspec": Rule(self.pspec, "Power spectrum", "/hdf5"),
"write_particles": Rule(self._write_particles, "Particles file", "/hdf5"
),
"write_particles": Rule(self._write_particles, "Particles file", "/hdf5"),
"write_cells": Rule(self._write_cells, "Cells file", "/hdf5"),
"filaments": Rule(
self._filaments,
@@ -1986,8 +1996,10 @@ class SnapshotProcessor(HDF5Container):
# Norm
generic_rule(
field + "_norm", partial(norm_getter, field), self.unit_key[field],
oct_getter=partial(oct_norm_getter, field)
field + "_norm",
partial(norm_getter, field),
self.unit_key[field],
oct_getter=partial(oct_norm_getter, field),
)
else:
@@ -1998,8 +2010,6 @@ class SnapshotProcessor(HDF5Container):
unit = self.rules[name].unit
self.rules["rad_avg_" + name] = Rule(
partial(self._rad_avg, name),
"Azimuthal average of {}".format(name),