{ "cells": [ { "cell_type": "markdown", "id": "bbbaafbb-fd7d-4b73-a970-93506ba35d71", "metadata": { "tags": [] }, "source": [ "# Example use case: Zero-age stellar luminosity function\n", "\n", "In this notebook we compute the luminosity function of the zero-age main-sequence by running a population of single stars using binary_c. \n", "\n", "We start by loading in some standard Python modules and the binary_c module.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "bf6b8673-a2b5-4b50-ad1b-e90671f57470", "metadata": {}, "outputs": [], "source": [ "import os\n", "import math\n", "import matplotlib.pyplot as plt\n", "\n", "from binarycpython.utils.functions import temp_dir\n", "from binarycpython.utils.grid import Population\n", "\n", "TMP_DIR = temp_dir(\"notebooks\", \"notebook_luminosity\")\n", "\n", "# help(Population) # Uncomment this line to see the public functions of this object" ] }, { "cell_type": "markdown", "id": "f268eff3-4e08-4f6b-8b59-f22dba4d2074", "metadata": {}, "source": [ "## Setting up the Population object\n", "To set up and configure the population object we need to make a new instance of the `Population` object and configure it with the `.set()` function.\n", "\n", "In our case, we only need to set the maximum evolution time to something short, because we care only about zero-age main sequence stars which have, by definition, age zero." ] }, { "cell_type": "code", "execution_count": 2, "id": "79ab50b7-591f-4883-af09-116d1835a751", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "adding: max_evolution_time=0.1 to BSE_options\n", "adding: tmp_dir=/tmp/binary_c_python-izzard/notebooks/notebook_luminosity to grid_options\n", "verbosity is 1\n" ] } ], "source": [ "# Create population object\n", "population = Population()\n", "\n", "# If you want verbosity, set this before other things\n", "population.set(verbosity=1)\n", "\n", "# Setting values can be done via .set(<parameter_name>=<value>)\n", "# Values that are known to be binary_c_parameters are loaded into bse_options.\n", "# Those that are present in the default grid_options are set in grid_options\n", "# All other values that you set are put in a custom_options dict\n", "population.set(\n", " # binary_c physics options\n", " max_evolution_time=0.1, # maximum stellar evolution time in Myr\n", " tmp_dir=TMP_DIR,\n", ")\n", "\n", "# We can access the options through \n", "print(\"verbosity is\", population.grid_options['verbosity'])" ] }, { "cell_type": "markdown", "id": "f9a65554-36ab-4a04-96ca-9f1422c307fd", "metadata": {}, "source": [ "## Adding grid variables\n", "The main purpose of the Population object is to handle the population synthesis side of running a set of stars. The main method to do this with binarycpython, as is the case with Perl binarygrid, is to use grid variables. These are loops over a predefined range of values, where a probability will be assigned to the systems based on the chosen probability distributions.\n", "\n", "Usually we use either 1 mass grid variable, or a trio of mass, mass ratio and period (other notebooks cover these examples). We can, however, also add grid sampling for e.g. eccentricity, metallicity or other parameters. \n", "\n", "To add a grid variable to the population object we use `population.add_grid_variable`" ] }, { "cell_type": "code", "execution_count": 3, "id": "68c84521-9ae8-4020-af7a-5334173db969", "metadata": {}, "outputs": [], "source": [ "# help(population.add_grid_variable)" ] }, { "cell_type": "markdown", "id": "bd75cebe-2152-4025-b680-dc020b80889b", "metadata": {}, "source": [ "All the distribution functions that we can use are stored in the `binarycpython.utils.distribution_functions` or `binarycpython/utils/distribution_functions.py` on git. If you uncomment the help statement below you can see which functions are available now:" ] }, { "cell_type": "code", "execution_count": 4, "id": "048db541-3e92-4c5d-a25c-9c5a34b9c857", "metadata": { "scrolled": true, "tags": [] }, "outputs": [], "source": [ "import binarycpython.utils.distribution_functions\n", "# help(binarycpython.utils.distribution_functions)" ] }, { "cell_type": "markdown", "id": "2a9104fc-4136-4e53-8604-f24ad52fbe56", "metadata": {}, "source": [ "First let us set up some global variables that will be useful throughout. \n", "* The resolution is the number of stars we simulate in our model population.\n", "* The massrange is a list of the min and max masses\n", "* The total_probability is the theoretical integral of a probability density function, i.e. 1.0.\n", "* The binwidth sets the resolution of the final distribution. If set to 0.5, the bins in log*L* are 0.5dex wide." ] }, { "cell_type": "code", "execution_count": 5, "id": "aba3fe4e-18f2-4bb9-8e5c-4c6007ab038b", "metadata": {}, "outputs": [], "source": [ "# Set resolution and mass range that we simulate\n", "resolution = {\"M_1\": 40} # start with resolution = 10, and increase later if you want \"more accurate\" data\n", "massrange = (0.07, 100.0) # we work with stars of mass 0.07 to 100 Msun\n", "total_probability = 1.0 # theoretical integral of the mass probability density function over all masses \n", "# distribution binwidths : \n", "# (log10) luminosity distribution\n", "binwidth = { 'luminosity' : 0.5 }" ] }, { "cell_type": "markdown", "id": "1b3a007b-5c17-42a7-a981-7e268e6f545c", "metadata": {}, "source": [ "The next cell contains an example of adding the mass grid variable, sampling the phase space in linear mass *M*_1." ] }, { "cell_type": "code", "execution_count": 6, "id": "47979841-2c26-4b26-8945-603d013dc93a", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "add_grid_variable() got an unexpected keyword argument 'resolution'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_519112/518757914.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mtmp_dir\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mTMP_DIR\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m )\n\u001b[0;32m----> 6\u001b[0;31m population.add_grid_variable(\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"M_1\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mlongname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Primary mass\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: add_grid_variable() got an unexpected keyword argument 'resolution'" ] } ], "source": [ "# Mass\n", "population = Population()\n", "population.set(\n", " tmp_dir=TMP_DIR,\n", ")\n", "population.add_grid_variable(\n", " name=\"M_1\",\n", " longname=\"Primary mass\",\n", " valuerange=massrange,\n", " samplerfunc=\"const({min}, {max}, {res})\".format(min = massrange[0], max = massrange[1], res = resolution[\"M_1\"]),\n", " probdist=\"{probtot}/({max} - {min})\".format(probtot = total_probability, min = massrange[0], max = massrange[1]), # dprob/dm1 : all stars are equally likely so this is 1.0 / (Mmax - Mmin)\n", " dphasevol=\"dM_1\",\n", " parameter_name=\"M_1\",\n", " condition=\"\", # Impose a condition on this grid variable. Mostly for a check for yourself\n", ")" ] }, { "cell_type": "markdown", "id": "163f13ae-fec1-4ee8-b9d4-c1b75c19ff39", "metadata": {}, "source": [ "## Setting logging and handling the output\n", "By default, binary_c will not output anything (except for 'SINGLE STAR LIFETIME'). It is up to us to determine what will be printed. We can either do that by hardcoding the print statements into `binary_c` (see documentation binary_c) or we can use the custom logging functionality of binarycpython (see notebook `notebook_custom_logging.ipynb`), which is faster to set up and requires no recompilation of binary_c, but is somewhat more limited in its functionality. For our current purposes, it works perfectly well.\n", "\n", "After configuring what will be printed, we need to make a function to parse the output. This can be done by setting the parse_function parameter in the population object (see also notebook `notebook_individual_systems.ipynb`). \n", "\n", "In the code below we will set up both the custom logging and a parse function to handle that output." ] }, { "cell_type": "code", "execution_count": null, "id": "0c986215-93b1-4e30-ad79-f7c397e9ff7d", "metadata": {}, "outputs": [], "source": [ "# Create custom logging statement\n", "#\n", "# we check that the model number is zero, i.e. we're on the first timestep (stars are born on the ZAMS)\n", "# we make sure that the stellar type is <= MAIN_SEQUENCE, i.e. the star is a main-sequence star\n", "# we also check that the time is 0.0 (this is not strictly required, but good to show how it is done)\n", "#\n", "# The Printf statement does the outputting: note that the header string is ZERO_AGE_MAIN_SEQUENCE_STAR\n", "\n", "custom_logging_statement = \"\"\"\n", "if(stardata->model.model_number == 0 &&\n", " stardata->star[0].stellar_type <= MAIN_SEQUENCE &&\n", " stardata->model.time == 0)\n", "{\n", " /* Note that we use Printf - with a capital P! */\n", " Printf(\"ZERO_AGE_MAIN_SEQUENCE_STAR %30.12e %g %g %g %g\\\\n\",\n", " stardata->model.time, // 1\n", " stardata->common.zero_age.mass[0], // 2\n", " stardata->star[0].mass, // 3\n", " stardata->star[0].luminosity, // 4\n", " stardata->model.probability // 5\n", " );\n", "};\n", "\"\"\"\n", "\n", "population.set(\n", " C_logging_code=custom_logging_statement\n", ")\n" ] }, { "cell_type": "markdown", "id": "ae1f1f0c-1f8b-42d8-b051-cbf8c6b51514", "metadata": {}, "source": [ "The parse function must now catch lines that start with \"ZERO_AGE_MAIN_SEQUENCE_STAR\" and process the associated data." ] }, { "cell_type": "code", "execution_count": null, "id": "fd197154-a8ce-4865-8929-008d3483101a", "metadata": {}, "outputs": [], "source": [ "# import the bin_data function so we can construct finite-resolution probability distributions\n", "# import the datalinedict to make a dictionary from each line of data from binary_c\n", "from binarycpython.utils.functions import bin_data,datalinedict\n", "\n", "def parse_function(self, output):\n", " \"\"\"\n", " Example parse function\n", " \"\"\"\n", " \n", " # list of the data items\n", " parameters = [\"header\", \"time\", \"zams_mass\", \"mass\", \"luminosity\", \"probability\"]\n", " \n", " # Loop over the output.\n", " for line in output.splitlines():\n", " # obtain the line of data in dictionary form \n", " linedata = datalinedict(line,parameters)\n", " \n", " # Check the header and act accordingly\n", " if linedata['header'] == \"ZERO_AGE_MAIN_SEQUENCE_STAR\":\n", " \n", " # bin the log10(luminosity) to the nearest 0.1dex\n", " binned_log_luminosity = bin_data(math.log10(linedata['luminosity']),\n", " binwidth['luminosity'])\n", " \n", " # append the data to the results_dictionary \n", " self.grid_results['luminosity distribution'][binned_log_luminosity] += linedata['probability'] \n", " \n", " #print (self.grid_results)\n", " \n", " # verbose reporting\n", " #print(\"parse out results_dictionary=\",self.grid_results)\n", " \n", "# Add the parsing function\n", "population.set(\n", " parse_function=parse_function,\n", ")" ] }, { "cell_type": "markdown", "id": "91509ce5-ffe7-4937-aa87-6d7baac9ac04", "metadata": {}, "source": [ "## Evolving the grid\n", "Now that we configured all the main parts of the population object, we can actually run the population! Doing this is straightforward: `population.evolve()`\n", "\n", "This will start up the processing of all the systems. We can control how many cores are used by settings `num_cores`. By setting the `verbosity` of the population object to a higher value we can get a lot of verbose information about the run, but for now we will set it to 0.\n", "\n", "There are many grid_options that can lead to different behaviour of the evolution of the grid. Please do have a look at those: [grid options docs](https://ri0005.pages.surrey.ac.uk/binary_c-python/grid_options_descriptions.html), and try " ] }, { "cell_type": "code", "execution_count": null, "id": "8ea376c1-1e92-45af-8cab-9d7fdca564eb", "metadata": { "tags": [] }, "outputs": [], "source": [ "# set number of threads\n", "population.set(\n", " # verbose output is not required \n", " verbosity=0,\n", " # set number of threads (i.e. number of CPU cores we use)\n", " num_cores=2,\n", " )\n", "\n", "# Evolve the population - this is the slow, number-crunching step\n", "analytics = population.evolve() \n", "\n", "# Show the results (debugging)\n", "# print (population.grid_results)" ] }, { "cell_type": "markdown", "id": "91ab45c7-7d31-4543-aee4-127ab58e891f", "metadata": {}, "source": [ "After the run is complete, some technical report on the run is returned. I stored that in `analytics`. As we can see below, this dictionary is like a status report of the evolution. Useful for e.g. debugging." ] }, { "cell_type": "code", "execution_count": null, "id": "e1f0464b-0424-4022-b34b-5b744bc2c59d", "metadata": {}, "outputs": [], "source": [ "print(analytics)" ] }, { "cell_type": "code", "execution_count": null, "id": "05c6d132-abee-423e-b1a8-2039c8996fbc", "metadata": {}, "outputs": [], "source": [ "# make a plot of the luminosity distribution using Seaborn and Pandas\n", "import seaborn as sns\n", "import pandas as pd\n", "from binarycpython.utils.functions import pad_output_distribution\n", "\n", "# set up seaborn for use in the notebook\n", "sns.set(rc={'figure.figsize':(20,10)})\n", "sns.set_context(\"notebook\",\n", " font_scale=1.5,\n", " rc={\"lines.linewidth\":2.5})\n", " \n", "\n", "# this saves a lot of typing! \n", "ldist = population.grid_results['luminosity distribution']\n", "\n", "# pad the distribution with zeros where data is missing\n", "pad_output_distribution(ldist,\n", " binwidth['luminosity'])\n", "\n", "# make pandas dataframe from our sorted dictionary of data\n", "plot_data = pd.DataFrame.from_dict({'ZAMS luminosity distribution' : ldist})\n", "\n", "# make the plot\n", "p = sns.lineplot(data=plot_data)\n", "p.set_xlabel(\"$\\log_{10}$ ($L_\\mathrm{ZAMS}$ / L$_{☉}$)\")\n", "p.set_ylabel(\"Number of stars\")\n", "p.set(yscale=\"log\")" ] }, { "cell_type": "markdown", "id": "7d7b275e-be92-4d59-b44d-ef6f24023cc3", "metadata": {}, "source": [ "Does this look like a reasonable stellar luminosity function to you? The implication is that the most likely stellar luminosity is 10<sup>5.8</sup> L<sub>☉</sub>! Clearly, this is not very realistic... let's see what went wrong." ] }, { "cell_type": "markdown", "id": "e32c3bbf-390f-45da-ad9c-cc3e7c9449dc", "metadata": {}, "source": [ "## ZAMS Luminosity distribution with the initial mass function\n", "\n", "In the previous example, all the stars in our grid had an equal weighting. This is very unlikely to be true in reality: indeed, we know that low mass stars are far more likely than high mass stars. So we now include an initial mass function as a three-part power law based on Kroupa (2001). Kroupa's distribution is a three-part power law: we have a function that does this for us (it's very common to use power laws in astrophysics).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1f37d2c0-1108-4ab9-a309-20b1e6b6e3fd", "metadata": {}, "outputs": [], "source": [ "# Update the probability distribution to use the three-part power law IMF \n", "population.update_grid_variable(\n", " name=\"M_1\",\n", " probdist=\"three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 150, -1.3, -2.3, -2.3)\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "6f4463e8-1935-45f2-8c5f-e7b215f8dc47", "metadata": {}, "outputs": [], "source": [ "# Clean and re-evolve the population \n", "population.clean()\n", "analytics = population.evolve() \n", "\n", "# Show the results (debugging)\n", "# print (population.grid_results)" ] }, { "cell_type": "code", "execution_count": null, "id": "cfe45a9e-1121-43b6-b6b6-4de6f8946a18", "metadata": {}, "outputs": [], "source": [ "# plot luminosity distribution\n", "ldist = population.grid_results['luminosity distribution']\n", "\n", "# pad the distribution with zeros where data is missing\n", "pad_output_distribution(ldist,\n", " binwidth['luminosity'])\n", "\n", "# make pandas dataframe from our sorted dictionary of data\n", "plot_data = pd.DataFrame.from_dict({'ZAMS luminosity distribution' : ldist})\n", "\n", "# make the plot\n", "p = sns.lineplot(data=plot_data)\n", "p.set_xlabel(\"$\\log_{10}$ ($L_\\mathrm{ZAMS}$ / L$_{☉}$)\")\n", "p.set_ylabel(\"Number of stars\")\n", "p.set(yscale=\"log\")" ] }, { "cell_type": "markdown", "id": "0546f2f3-4732-4841-8ef3-565fbf6b9961", "metadata": {}, "source": [ "This distribution is peaked at low luminosity, as one expects from observations, but the resolution is clearly not great because it's not smooth - it's spiky! \n", "\n", "If you noticed above, the total probability of the grid was about 0.2. Given that the total probability of a probability distribution function should be 1.0, this shows that our sampling is (very) poor. \n", "\n", "We could simply increase the resolution to compensate, but this is very CPU intensive and a complete waste of time and resources. Instead, let's try sampling the masses of the stars in a smarter way." ] }, { "cell_type": "markdown", "id": "673031c9-7d80-45d4-b209-301c127d3edf", "metadata": { "tags": [] }, "source": [ "## A better-sampled grid\n", "\n", "The IMF has many more low-mass stars than high-mass stars. So, instead of sampling M1 linearly, we can sample it in log space. \n", "\n", "To do this we first rename the mass grid variable so that it is clear we are working in (natural) logarithmic phase space." ] }, { "cell_type": "code", "execution_count": null, "id": "5956f746-e3b9-4912-b75f-8eb0af66d3f6", "metadata": {}, "outputs": [], "source": [ "# Rename the old variable (M_1) because we want it to be called lnM_1 now\n", "population.rename_grid_variable(\"M_1\",\"lnM_1\")" ] }, { "cell_type": "markdown", "id": "532f691c-c1f6-46cc-84f2-970ec1216e40", "metadata": {}, "source": [ "Next, we change the spacing function so that it works in the log space. We also adapt the probability calculation so that it calculates dprob/dlnM = M * dprob/dM. Finally, we set the precode to compute M_1 because binary_c requires the actual mass, not the logarithm of the mass." ] }, { "cell_type": "code", "execution_count": null, "id": "108d470a-bb21-40b0-8387-2caa7ab0f923", "metadata": {}, "outputs": [], "source": [ "# update the sampling, note that the IMF is dprob/dM1, and the phase \n", "# space is now sampled in lnM1, so we multiply by M_1 to \n", "# because M * dprob/dM = dprob/dlnM\n", "population.update_grid_variable(\n", " name=\"lnM_1\",\n", " samplerfunc=\"const(math.log({min}), math.log({max}), {res})\".format(min = massrange[0], max = massrange[1], res = resolution[\"M_1\"]),\n", " probdist=\"three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 150, -1.3, -2.3, -2.3)*M_1\",\n", " dphasevol=\"dlnM_1\",\n", " parameter_name=\"M_1\",\n", " precode=\"M_1=math.exp(lnM_1)\",\n", ")\n", "# print(population.grid_options[\"_grid_variables\"]) # debugging" ] }, { "cell_type": "code", "execution_count": null, "id": "fb8db646-f3d0-4ccd-81ba-7fde23f29c79", "metadata": {}, "outputs": [], "source": [ "# Clean and re-evolve the population \n", "population.clean()\n", "analytics = population.evolve() \n", "\n", "# Show the results (debugging)\n", "# print (population.grid_results)" ] }, { "cell_type": "markdown", "id": "182b1094-5057-4ccf-bac6-9b0e560ad4f6", "metadata": {}, "source": [ "You should see that the total probability is very close to 1.0, as you would expect for a well-sampled grid. The total will never be exactly 1.0, but that is because we are running a simulation, not a perfect copy of reality." ] }, { "cell_type": "code", "execution_count": null, "id": "68ee1e56-21e5-48f4-b74c-50e48685ae94", "metadata": {}, "outputs": [], "source": [ "# plot luminosity distribution\n", "ldist = population.grid_results['luminosity distribution']\n", "\n", "# pad the distribution with zeros where data is missing\n", "pad_output_distribution(ldist,\n", " binwidth['luminosity'])\n", "\n", "# make pandas dataframe from our sorted dictionary of data\n", "plot_data = pd.DataFrame.from_dict({'ZAMS luminosity distribution' : ldist})\n", "\n", "# make the plot\n", "p = sns.lineplot(data=plot_data)\n", "p.set_xlabel(\"$\\log_{10}$ ($L_\\mathrm{ZAMS}$ / L$_{☉}$)\")\n", "p.set_ylabel(\"Number of stars\")\n", "p.set(yscale=\"log\")\n", "plt.show()" ] }, { "cell_type": "markdown", "id": "31fe91bb-177d-4e4e-90cf-298a3f8a8b61", "metadata": {}, "source": [ "Most stars are low mass red dwarfs, with small luminosities. Without the IMF weighting, our model population would have got this completely wrong! \n", "\n", "As you increase the resolution, you will see this curve becomes even smoother. The wiggles in the curve are (usually) sampling artefacts because the curve should monotonically brighten above about log(*L*/L<sub>☉</sub>)=-2. \n", " \n", "Remember you can play with the binwidth too. If you want a very accurate distribution you need a narrow binwidth, but then you'll also need high resolution (lots of stars) so lots of CPU time, hence cost, CO<sub>2</sub>, etc." ] }, { "cell_type": "markdown", "id": "ba032bd8-b4a2-4558-9fd9-8e1e03d7d162", "metadata": {}, "source": [ "Things to try:\n", "* Change the resolution to make the distributions smoother: what about error bars, how would you do that?\n", "* Different initial distributions: the Kroupa distribution isn't the only one out there\n", "* Change the metallicity and mass ranges\n", "* What about a non-constant star formation rate? This is more of a challenge!\n", "* What about evolved stars? Here we consider only the *zero-age* main sequnece. What about other main-sequence stars? What about stars in later phases of stellar evolution?\n", "* Binary stars! (see notebook_luminosity_function_binaries.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.5" } }, "nbformat": 4, "nbformat_minor": 5 }