.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/z_extensions/plot_gadget_simulation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_z_extensions_plot_gadget_simulation.py: ============================================== Initial Conditions for Gadget-4 Simulations ============================================== In this example, we'll use Pisces to generate a galaxy cluster merger model and export it as initial conditions for a Gadget-4 simulation. We will: 1. Construct two simple spherical galaxy cluster models. 2. Arrange them on a collision trajectory. 3. Convert these models into **Gadget-4–compatible** particle data. This tutorial illustrates how analytical models in Pisces can be transformed into realistic, particle-based initial conditions for large-scale hydrodynamic simulations. .. GENERATED FROM PYTHON SOURCE LINES 21-28 Setup ----- Import the required modules. We'll use the :class:`~pisces.models.galaxy_clusters.spherical.SphericalGalaxyClusterModel` to construct equilibrium cluster models and the :class:`~pisces.extensions.simulation.gadget.frontends.Gadget4Frontend` to export them into Gadget-4 format. .. GENERATED FROM PYTHON SOURCE LINES 28-40 .. code-block:: Python import tempfile import matplotlib.pyplot as plt import numpy as np import unyt from pisces.extensions.simulation import gadget, initial_conditions from pisces.models.galaxy_clusters import SphericalGalaxyClusterModel from pisces.particles import Gadget4ParticleDataset from pisces.profiles import NFWDensityProfile .. GENERATED FROM PYTHON SOURCE LINES 41-48 Density Profiles ---------------- We define Navarro–Frenk–White (NFW) profiles for both the total matter and gas components of a single galaxy cluster. These parameters correspond to a modest-mass cluster and will be used to define the equilibrium structure for each model. .. GENERATED FROM PYTHON SOURCE LINES 48-59 .. code-block:: Python rho_tot = unyt.unyt_quantity(5e6, "Msun/kpc**3") # Total density normalization r_s_tot = unyt.unyt_quantity(200, "kpc") # Total scale radius rho_gas = unyt.unyt_quantity(5e5, "Msun/kpc**3") # Gas density normalization r_s_gas = unyt.unyt_quantity(220, "kpc") # Gas scale radius # Create the NFW profiles total_density = NFWDensityProfile(rho_0=rho_tot, r_s=r_s_tot) gas_density = NFWDensityProfile(rho_0=rho_gas, r_s=r_s_gas) .. GENERATED FROM PYTHON SOURCE LINES 60-68 Cluster Model Construction -------------------------- Using these profiles, we now build a self-consistent spherical cluster model defined on a logarithmic radial grid. The :class:`~pisces.models.galaxy_clusters.SphericalGalaxyClusterModel` automatically computes the hydrostatic equilibrium structure (gas pressure, mass, potential, etc.) from the provided density fields. .. GENERATED FROM PYTHON SOURCE LINES 68-85 .. code-block:: Python tmpdir = tempfile.TemporaryDirectory() filename = f"{tmpdir.name}/cluster_model.h5" rmin = unyt.unyt_quantity(1.0, "kpc") rmax = unyt.unyt_quantity(3.0, "Mpc") model = SphericalGalaxyClusterModel.from_density_and_total_density( gas_density, total_density, filename, min_radius=rmin, max_radius=rmax, num_points=500, overwrite=True, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Preparing metadata...: 0%| | 0/7 [00:00 0 density_image[nonzero] = mhist[nonzero] / (np.diff(x_edges)[0] * np.diff(y_edges)[0]) velocity_image[nonzero] = vhist[nonzero] / mhist[nonzero] # Create figure with three vertically stacked panels fig, axes = plt.subplots(3, 1, figsize=(6, 7), sharex=True, gridspec_kw={"hspace": 0.0}) extent = [x_edges[0], x_edges[-1], y_edges[0], y_edges[-1]] # --- Particle Count --- img0 = axes[0].imshow(chist.T, origin="lower", extent=extent, norm="log", cmap="viridis") plt.colorbar(img0, ax=axes[0], fraction=0.045, pad=0.01, label="Particle Count") # --- Mass Density --- img1 = axes[1].imshow(density_image.T, origin="lower", extent=extent, norm="log", cmap="viridis") plt.colorbar(img1, ax=axes[1], fraction=0.045, pad=0.01, label=r"Mass Density (M$_\odot$/kpc$^2$)") # --- X-Velocity Field --- img2 = axes[2].imshow(velocity_image.T, origin="lower", extent=extent, cmap="seismic") plt.colorbar(img2, ax=axes[2], fraction=0.045, pad=0.01, label=r"$v_x$ (km/s)") # --- Labels and layout --- axes[-1].set_xlabel("x (kpc)") for ax in axes: ax.set_ylabel("y (kpc)") ax.set_aspect("equal") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/z_extensions/images/sphx_glr_plot_gadget_simulation_001.png :alt: plot gadget simulation :srcset: /auto_examples/z_extensions/images/sphx_glr_plot_gadget_simulation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 14.929 seconds) .. _sphx_glr_download_auto_examples_z_extensions_plot_gadget_simulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gadget_simulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gadget_simulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gadget_simulation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_