From 0821e26ecd8f9241f563e5afa2a588fef07fe438 Mon Sep 17 00:00:00 2001 From: Martin Glatzle Date: Thu, 8 Apr 2021 16:56:21 +0200 Subject: [PATCH] Tutorial for SSP particles. --- Stellar_particles.ipynb | 197 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 Stellar_particles.ipynb diff --git a/Stellar_particles.ipynb b/Stellar_particles.ipynb new file mode 100644 index 0000000..ff10d2d --- /dev/null +++ b/Stellar_particles.ipynb @@ -0,0 +1,197 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Stellar particles\n", + "This tutorial will teach you how to obtain spectral properties of stellar particles (e.g. representing single stellar populations, SSPs,from a hydrodynamic simulation) from BPASS using `hoki`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Prerequisites\n", + "In addition to `hoki`, you need a local copy of the BPASS v2.2.1 database (or at least the spectral data for the chab300 IMF). These data are too large to be included in this repository." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# specify the base path to your BPASS database here\n", + "base_path = '~/BPASSv2.2.1_release-07-18-Tuatara/'" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Imports/settings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from hoki import interpolators\n", + "from hoki.spec import bin_luminosity\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "import pandas as pd\n", + "import os\n", + "\n", + "%matplotlib inline\n", + "plt.style.use('tuto.mplstyle')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# The stellar particles\n", + "Here we just create a dataframe of three stellar particles, this would normally come from a star formation simulation or similar." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "star_parts = pd.DataFrame({\n", + " # absolute initial stellar metallicities\n", + " 'metallicity': np.array([1e-2, 1e-4, 1e-3]),\n", + " # log of stellar ages in yr\n", + " 'log_age': np.array([6.0, 8.3, 10]),\n", + " # stellar masses in 1e6 M_sun\n", + " 'mass': np.array([1, 2.5, 3])\n", + "})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Interpolators\n", + "Now let's create interpolator objects for the spectra and emissivities/luminosities provided by BPASS. These interpolator objects simply interpolate SSP properties on the BPASS age-metallicity grids." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "data_path = os.path.join(base_path, 'bpass_v2.2.1_imf_chab300')\n", + "imf = 'imf_chab300'\n", + "\n", + "spec_interp = interpolators.SpectraInterpolator(data_path, imf, lam_min=2e2, lam_max=2e3)\n", + "em_interp = interpolators.EmissivitiesInterpolator(data_path, imf)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Results\n", + "Let's do the interpolation and see what we get." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# interpolated SEDs and the corresponding wavelengths\n", + "wl, spectra = spec_interp(star_parts['metallicity'], star_parts['log_age'], star_parts['mass'])\n", + "\n", + "plt.figure(figsize=(10,5))\n", + "for spec in spectra:\n", + " plt.plot(wl, spec, lw=0.5)\n", + "plt.xlabel(r'Wavelength ($\\AA$)')\n", + "plt.ylabel(r'SED ($L_\\odot/\\AA$)')\n", + "plt.xscale('log')\n", + "plt.yscale('log')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# interpolated emissivities/luminosities\n", + "em = em_interp(star_parts['metallicity'], star_parts['log_age'], star_parts['mass'])\n", + "# let's add the to our dataframe (there's probably a more efficient way of doing this)\n", + "for col, name in zip(em.T, ['Nion', 'L_Halpha', 'L_FUV', 'L_NUV']):\n", + " star_parts[name] = col" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# this is what the dataframe looks like now\n", + "star_parts" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Binning the spectra\n", + "`hoki` provides functionality to bin the interpolated spectra, conserving luminosity. This is useful, e.g., when you want to perform a radiative transfer simulation and need to reduce the number of spectral sampling points from what is provided by BPASS." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "bins = np.geomspace(2.1e2, 1.9e3, num=20)\n", + "wl_binned, spectra_binned = bin_luminosity(wl, spectra, bins=bins)\n", + "\n", + "plt.figure(figsize=(10,5))\n", + "for spec in spectra:\n", + " plt.plot(wl, spec, lw=0.5)\n", + "for spec in spectra_binned:\n", + " plt.stairs(spec, edges=bins)\n", + "plt.xlabel(r'Wavelength ($\\AA$)')\n", + "plt.ylabel(r'SED ($L_\\odot/\\AA$)')\n", + "plt.xscale('log')\n", + "plt.yscale('log')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}