Reading the particles of a FLAMINGO halo, straight from COSMA

Reading the particles of a FLAMINGO halo, straight from COSMA#

Added in version 2.8.0: Reading SOAP catalogues and accessing remote hdfstream servers requires version 2.8.0 or later.

This tutorial interfaces with the FLAMINGO online data release using pynbody. We will:

  1. open a SOAP halo catalogue, identify the most massive halo, and find its centre and its HaloCatalogueIndex;

  2. read the region of the corresponding snapshot around that halo;

  3. keep the particles that SOAP’s membership information assigns to the halo;

  4. make a picture and a density profile.

Nothing needs to be downloaded in advance. Both the halo catalogue and the snapshot are read directly from the hdfstream service at COSMA, by passing remote_dir to pynbody.load(). Only the metadata, and the arrays and regions actually used, cross the network.

Note

You need the hdfstream module (pip install hdfstream) and a network connection. This tutorial is generated from an ipython notebook, which you can access directly using the download link at the top right.

Connecting to COSMA#

We start by opening the root of the FLAMINGO hdfstream service. The resulting object behaves like a directory, and is what we pass to pynbody as remote_dir.

In [2]:
import hdfstream
import numpy as np

import pynbody

root_dir = hdfstream.open("https://dataweb.cosma.dur.ac.uk:8443/hdfstream", "/")
simulation = "FLAMINGO/L1_m10/L1_m10_DMO"
root_dir[simulation]
Out[2]:
<Remote directory /FLAMINGO/L1_m10/L1_m10_DMO with 6 sub-directories, 7 files>

Opening the SOAP halo catalogue#

SOAP catalogues tabulate many properties for every subhalo, computed in different ways: the bound subhalo, spherical overdensities, fixed apertures and so on. Pynbody opens a SOAP catalogue like a snapshot whose “particles” are the subhalos, all in the family halo. We load the \(z=0\) catalogue of the 1 Gpc dark-matter-only run:

In [3]:
soap = pynbody.load(f"{simulation}/SOAP-HBT/halo_properties_0077.hdf5", remote_dir=root_dir)
soap
Out[3]:
<SOAPSnap "FLAMINGO/L1_m10/L1_m10_DMO/SOAP-HBT/halo_properties_0077.hdf5" len=1627212>

Pynbody recognises the file as a SOAP catalogue (pynbody.snapshot.soap.SOAPSnap). As with any snapshot, the cosmology and units are read from the file:

In [4]:
soap.properties
Out[4]:
{'a': np.float64(1.0),
 'h': np.float64(0.681),
 'omegaM0': np.float64(0.30461099999999997),
 'omegaL0': np.float64(0.693922),
 'omegaB0': np.float64(0.0486),
 'omegaC0': np.float64(0.256011),
 'omegaNu0': np.float64(0.0013890794316214667),
 'boxsize': Unit("3.09e+27 a cm"),
 'time': Unit("4.34e+17 s")}

Every property in the catalogue is available under its path in the HDF5 file. These are extremely numerous:

In [5]:
len(soap.loadable_keys())
Out[5]:
417
To narrow things down a bit, we just look at those properties pertaining to total masses:
In [6]:
sorted({key for key in soap.loadable_keys() if 'TotalMass' in key})
Out[6]:
['BoundSubhalo/TotalMass',
 'ExclusiveSphere/1000kpc/TotalMass',
 'ExclusiveSphere/100kpc/TotalMass',
 'ExclusiveSphere/10kpc/TotalMass',
 'ExclusiveSphere/3000kpc/TotalMass',
 'ExclusiveSphere/300kpc/TotalMass',
 'ExclusiveSphere/30kpc/TotalMass',
 'ExclusiveSphere/500kpc/TotalMass',
 'ExclusiveSphere/50kpc/TotalMass',
 'InclusiveSphere/1000kpc/TotalMass',
 'InclusiveSphere/100kpc/TotalMass',
 'InclusiveSphere/10kpc/TotalMass',
 'InclusiveSphere/3000kpc/TotalMass',
 'InclusiveSphere/300kpc/TotalMass',
 'InclusiveSphere/30kpc/TotalMass',
 'InclusiveSphere/500kpc/TotalMass',
 'InclusiveSphere/50kpc/TotalMass',
 'ProjectedAperture/100kpc/projx/TotalMass',
 'ProjectedAperture/100kpc/projy/TotalMass',
 'ProjectedAperture/100kpc/projz/TotalMass',
 'ProjectedAperture/10kpc/projx/TotalMass',
 'ProjectedAperture/10kpc/projy/TotalMass',
 'ProjectedAperture/10kpc/projz/TotalMass',
 'ProjectedAperture/30kpc/projx/TotalMass',
 'ProjectedAperture/30kpc/projy/TotalMass',
 'ProjectedAperture/30kpc/projz/TotalMass',
 'ProjectedAperture/50kpc/projx/TotalMass',
 'ProjectedAperture/50kpc/projy/TotalMass',
 'ProjectedAperture/50kpc/projz/TotalMass',
 'SO/1000_crit/TotalMass',
 'SO/100_crit/TotalMass',
 'SO/200_crit/TotalMass',
 'SO/200_mean/TotalMass',
 'SO/2500_crit/TotalMass',
 'SO/500_crit/TotalMass',
 'SO/50_crit/TotalMass',
 'SO/5xR_500_crit/TotalMass',
 'SO/BN98/TotalMass']

Only arrays that are actually used are transferred from COSMA. Because a SOAP catalogue has many masses for each halo (bound, \(M_{200c}\), \(M_{500c}\), within various apertures, …), pynbody does not guess which one you mean by mass. You ask for the one you want by name. Here, we find the halo with the largest \(M_{200c}\):

In [7]:
m200c = soap['SO/200_crit/TotalMass']
i = np.argmax(m200c)

print(f"Most massive halo is row {i}, with M200c = {m200c.in_units('Msol')[i]:.3e} Msol")
Most massive halo is row 206947, with M200c = 2.340e+15 Msol

The one array that is given a pynbody name is pos, which is the halo centre (InputHalos/HaloCentre) that SOAP uses as the reference point for all its apertures. Having pos lets pynbody’s spatial filters, centring etc. work on the catalogue. We’ll need the halo’s centre, its \(R_{200c}\), and the index that links it to its particles in the snapshot:

In [8]:
target_pos = soap['pos'][i]
target_index = soap['InputHalos/HaloCatalogueIndex'][i]
r200c = soap['SO/200_crit/SORadius'][i] * soap['SO/200_crit/SORadius'].units

print("Centre:", target_pos, soap['pos'].units)
print("HaloCatalogueIndex:", target_index)
print("R200c:", r200c.in_units("Mpc a"), "Mpc a")
Centre: [115.97934688 966.26593688 908.51006688] 3.09e+24 cm a
HaloCatalogueIndex: 1129761
R200c: 2.7890624986519925 Mpc a

As a quick illustration of treating the catalogue like a snapshot, we can use a pynbody filter to find the other haloes with \(M_{200c} > 10^{13}\,\mathrm{M}_{\odot}\) within 10 Mpc of this one:

In [9]:
neighbourhood = soap[pynbody.filt.Sphere("10 Mpc a", target_pos) & pynbody.filt.HighPass('SO/200_crit/TotalMass', '1e13 Msol')]
print(f"{len(neighbourhood)} haloes; M200c / Msol =", np.sort(neighbourhood['SO/200_crit/TotalMass'].in_units('Msol'))[::-1])
5 haloes; M200c / Msol = [2.3398284e+15 6.9279651e+13 1.6159919e+13 1.0399948e+13 1.0299948e+13]

Reading the halo’s region of the snapshot#

The snapshot is far too large to read in full (\(1800^3\) particles), but SWIFT snapshots carry a spatial index. Passing a filter as take_region makes pynbody read only the cells that overlap it. We read a sphere of radius 8 (comoving) Mpc around the halo, which comfortably encloses its particles. The snapshot is read from COSMA in the same way as the catalogue:

In [10]:
region = pynbody.filt.Sphere("8 Mpc a", target_pos)
snap = pynbody.load(f"{simulation}/snapshots/flamingo_0077/flamingo_0077.hdf5",
                    remote_dir=root_dir, take_region=region)
snap
Out[10]:
<SwiftSnap "FLAMINGO/L1_m10/L1_m10_DMO/snapshots/flamingo_0077/flamingo_0077.hdf5" len=341057>

Whole cells are read, so snap holds more particles than lie within the sphere itself. Each particle carries HaloCatalogueIndex, the index of the halo SOAP assigned it to (or -1). Selecting the particles with our halo’s index gives us its particles. As a check, their number matches that recorded in the catalogue:

In [11]:
halo = snap[snap['HaloCatalogueIndex'] == target_index]

print(f"Read {len(snap)} particles, of which {len(halo)} belong to the halo")
print("Number of bound DM particles according to SOAP:", soap['BoundSubhalo/NumberOfDarkMatterParticles'][i])
Read 341057 particles, of which 54809 belong to the halo
Number of bound DM particles according to SOAP: 54809

Making plots#

First, the same sanity check as the original example: a dotplot of the halo’s particles (red), alongside all the other particles read in (grey).

In [12]:
fig, ax = plt.subplots(figsize=(5, 5))
ax.plot(snap['x'].in_units('Mpc a'), snap['y'].in_units('Mpc a'), ',', color='0.7')
ax.plot(halo['x'].in_units('Mpc a'), halo['y'].in_units('Mpc a'), ',', color='C3')
plt.xlim(target_pos[0]-8, target_pos[0]+8)
plt.ylim(target_pos[1]-8, target_pos[1]+8)
ax.set_aspect('equal')
ax.set_xlabel(r"$x / \mathrm{Mpc}$ (comoving)")
ax.set_ylabel(r"$y / \mathrm{Mpc}$ (comoving)");
../_images/tutorials_flamingo_halo_particles_23_0.png

Now let’s use more of pynbody. We centre the snapshot on the halo, using the SOAP centre, and make an SPH-style projected density image of the region, marking \(R_{200c}\). Pynbody works out smoothing lengths for the dark matter particles automatically.

In [13]:
snap.translate(-target_pos)
snap.physical_units()

pynbody.plot.image(snap, width="8 Mpc", units="Msol kpc^-2", cmap="twilight", resolution=500)
r200c_kpc = float(r200c.in_units("kpc", **snap.conversion_context()))
plt.gca().add_patch(plt.Circle((0, 0), r200c_kpc, fill=False, color='w', ls='--'));
../_images/tutorials_flamingo_halo_particles_25_0.png

Finally, the spherically averaged density profile of the halo’s own particles, out to beyond \(R_{200c}\):

In [14]:
p = pynbody.analysis.Profile(halo, rmin="50 kpc", rmax="5 Mpc", nbins=40, type='log', ndim=3)
p_all = pynbody.analysis.Profile(snap, rmin="50 kpc", rmax="5 Mpc", nbins=40, type='log', ndim=3)

fig, ax = plt.subplots()
ax.loglog(p['rbins'].in_units('kpc'), p['density'].in_units('Msol kpc^-3'), label='Bound particles')
ax.loglog(p_all['rbins'].in_units('kpc'), p_all['density'].in_units('Msol kpc^-3'),":", label='All particles')
ax.axvline(r200c_kpc, color='k', ls='--', label='$R_{200c}$')
ax.set_xlabel(r"$r / \mathrm{kpc}$")
ax.set_ylabel(r"$\rho / \mathrm{M_{\odot}\,kpc^{-3}}$")
ax.legend();
../_images/tutorials_flamingo_halo_particles_27_0.png