calculate the mass of galaxy of each stellar density slice

zk seazhan
  • 1
  • 21 Nov

Hello,
I have already calculated the stellar density projection for each slice, and now I am trying to calculate the mass of each galaxy in this slice. This is the code I used to calculate the stellar density projection. After reviewing your response in match-snapshot-particles-with-their-halosubhalo, I am still unable to compute the mass for each galaxy in this slice. Do you have any suggestions?

front = 'tngdm'
os.makedirs(f"{front}/star", exist_ok=True)

hdf5_file = 'sims.TNG/TNG300-1/simulation.hdf5'

part_len = 15625000000
loops = 500.0
bin_size = part_len/loops
x_min, x_max = 0,205000
y_min, y_max = 0,10250
z_min, z_max = 0,205000

total_particles_found = 0

for i in range(0, int(loops)):
    try:
        start = int(i*bin_size)
        stop = int((i+1)*bin_size - 1)

        print(f"Processing batch {i+1}/{int(loops)}: particles {start} to {stop}")

        with h5py.File(hdf5_file, 'r') as f:
            dm_positions = f['/Snapshots/99/PartType4/Coordinates'][start:stop, :]
            dm_mass = f['/Snapshots/99/PartType4/Masses'][start:stop]            

            w = np.where((dm_positions[:, 0] >= x_min) & (dm_positions[:, 0] <= x_max) &
                        (dm_positions[:, 1] >= y_min) & (dm_positions[:, 1] < y_max) &
                        (dm_positions[:, 2] >= z_min) & (dm_positions[:, 2] <= z_max))[0]

            if len(w) > 0:
                total_particles_found += len(w)
                print(f"Found {len(w)} particles in range for batch {i}")

                try:
                    coordinates_file = f"{front}/star/y9/coordinates_{i}.fits"
                    pos_table = Table([dm_positions[w, 0]/1000.0, 
                                     dm_positions[w, 1]/1000.0, 
                                     dm_positions[w, 2]/1000.0],
                                    names=('px', 'py', 'pz'))
                    pos_table.write(coordinates_file, format='fits', overwrite=True)


                    if os.path.exists(coordinates_file):
                        print(f"Successfully saved coordinates to {coordinates_file}")
                    else:
                        print(f"Warning: Failed to save coordinates file {coordinates_file}")


                    mass_file = f"{front}/star/y9/mass_{i}.fits"
                    mass_table = Table([dm_mass[w]],
                                        names=('masses',))
                    mass_table.write(mass_file, format='fits', overwrite=True)


                    if os.path.exists(mass_file):
                        print(f"Successfully saved density to {mass_file}")
                    else:
                        print(f"Warning: Failed to save density file {mass_file}")

                except Exception as e:
                    print(f"Error saving files for batch {i}: {str(e)}")
            else:
                print(f"No particles found in range for batch {i}")


        del dm_positions
        del dm_mass

    except Exception as e:
        print(f"Error processing batch {i}: {str(e)}")
        continue

print(f"Processing complete. Total particles found: {total_particles_found}")
Dylan Nelson
  • 21 Nov

What do you mean "the mass for each galaxy in this slice", what is your goal?

zk seazhan
  • 22 Nov

My main goal is to find the minimum galaxy mass of the stellar projection density field. And maybe the subhalo means the galaxy? I want to sum the stellar particles belonging to each subhalo to get the mass of each subhalo, but the calculation doesn't seem to provide subhalo-related information. However, if I follow the method you mentioned in the/match-snapshot-particles-with-their-halosubhalo , how can I obtain the stellar density field projection for each slice? I use the TNG300-1 data.

Dylan Nelson
  • 23 Nov

Every Subhalo (i.e. galaxy) has fields for stellar mass, and position (SubhaloMassType[4] and SubhaloPos). You can use these to find the minimum galaxy mass in any region of space, for example.

  • Page 1 of 1