{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "pm-29TK1-nFj"
      },
      "source": [
        "## Analyzing MESA output with TULIPS - Lab 2"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wlMF5Fnltf62"
      },
      "source": [
        "TULIPS is open-source python software, you can find more information and tutorials about it [here](https://astro-tulips.readthedocs.io/en/latest/installation.html)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DPcyJinD-yqd"
      },
      "source": [
        "## Install TULIPS\n",
        "The installation on the Google collab virtual machine can take a few minutes (note that it is normally only installed once when you install it on your own laptop)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "x6RcZxmV-otu"
      },
      "outputs": [],
      "source": [
        "!pip install astro-tulips"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "YAuUnfZI-nFk",
        "scrolled": true
      },
      "outputs": [],
      "source": [
        "# Interactive matplotlib plotting for jupyter lab\n",
        "%matplotlib inline\n",
        "\n",
        "# Packages you will need for this tutorial\n",
        "import matplotlib.pyplot as plt\n",
        "import mesaPlot as mp\n",
        "import tulips\n",
        "import numpy as np\n",
        "from IPython.display import Video"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gRjhW_YKORQD"
      },
      "source": [
        "We now have all the packages and software installed that we need for this tutorial!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "x0lo7ofg-nFl"
      },
      "source": [
        "## Loading data from the MESA model"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "z9tHyVZb-nFl"
      },
      "source": [
        "Upload the MESA output files. To do so:\n",
        "1. Download all the files in the google drive link [here](https://drive.google.com/file/d/1TdQyhbeAyR26h06TjoVqOkXcX-lhDOrm/view?usp=sharing) as a zipped file.\n",
        "2. Upload the files as shown below (this can take some time)"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import files\n",
        "uploaded = files.upload() # This opens an option to upload files. Upload the zipped files here"
      ],
      "metadata": {
        "id": "74DAXFIcZa87"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "You can see the zipped file appear in the `Files` side bar on the left (with the folder icon). Let's unzip the files - Make sure to add the name of your uploaded file"
      ],
      "metadata": {
        "id": "1JYaUgQ0ZkCY"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import zipfile\n",
        "# Replace by the name of your uploaded file\n",
        "with zipfile.ZipFile(\"Profiles.zip\", \"r\") as zip_ref:\n",
        "  zip_ref.extractall(\"Profiles\")"
      ],
      "metadata": {
        "id": "BmfefAlsZmtZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kJ5DRwqx3hkp"
      },
      "source": [
        "Now that the data has been uploaded and unzipped, we can analyze it. Let's read the output from the donor star"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "hfiauRwl-nFm"
      },
      "outputs": [],
      "source": [
        "DONOR_DIR = \"/content/Profiles/Profiles/LOGS1/\" # Change to your local path of the uploaded file\n",
        "\n",
        "m1 = mp.MESA() # Create MESA object for the donor star\n",
        "m1.log_fold = DONOR_DIR # Find the data file\n",
        "m1.loadHistory(filename_in=DONOR_DIR + \"history1.data\") # Load the MESA history  data"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "KCXUxTCiHm1V"
      },
      "source": [
        "**Task:** Now load data from the accretor star. Call the object containing the data `m2`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "HMBSAWDBHhLt"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2gcGncld-nFm"
      },
      "source": [
        "### Accessing properties\n",
        "\n",
        "After the history output file has been loaded, properties such as the mass, age or radius of the star can be accessed through the `hist` property. These are all stored as lists of values (numpy arrays)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "W8BAp22P-nFn"
      },
      "source": [
        "To have a look at the different properties that are stored, type `m1.hist.` and press `Tab`. This will show you a list of the information that can be accessed. You can use these properties to make simple plots. For more documentation about `mesaPlot`, have a look at the [mesaPlot](https://github.com/rjfarmer/mesaplot) GitHub page."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "X2Ai4w5N-nFn"
      },
      "outputs": [],
      "source": [
        "star_age = m1.hist.star_age # Access star age in years\n",
        "center_h1 = m1.hist.center_h1 # Access the mass fraction of hydrogen in the center of the star\n",
        "log_R = m1.hist.log_R # Access star's log_10 radius in solar radius units.\n",
        "R = 10**log_R # To find the radius, use the power 10\n",
        "lg_mtr = m1.hist.lg_mtransfer_rate # Log mass transfer rate in Msun/yr"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4RscHZPhIU74"
      },
      "source": [
        "Let's check the radius at the beginning of the simulation (ZAMS)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "4stPGnkc-nFo"
      },
      "outputs": [],
      "source": [
        "print(R[0])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-7coYoh9I1ac"
      },
      "source": [
        "Let's check the radius at the end of the simulation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Fm0pur3wIqLe"
      },
      "outputs": [],
      "source": [
        "print(R[-1])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NUpGCrsQ-nFo"
      },
      "source": [
        "**Task:** Can you find the total mass of the donor and accretor star at the begining and end of the simulation?\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PkSgJ0YZ-nFo"
      },
      "source": [
        "The default unit is in solar masses"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "810nKS5y-nFo"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "MBfAMRRBbUAv"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Q:** What has changed and why?\n",
        "\n",
        "**A:** The donor star has transferred about 4Msun of its outer layers to the accretor star"
      ],
      "metadata": {
        "id": "aOWa7fZRbPGe"
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "R0jFMFh9-nFo"
      },
      "source": [
        "# Your first TULIPS plot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MdNAwgSv3qAw"
      },
      "source": [
        "Now let's use TULIPS to check the apparent size and color of the donor star at different times in its evolution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1PnrmQNz-nFp"
      },
      "outputs": [],
      "source": [
        "fig, ax = tulips.perceived_color(m1, time_ind=0, show_hrd_ticks_and_labels=True,\n",
        "                                 fig_size=(6,6))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YtipyKmK-nFp"
      },
      "source": [
        "\n",
        "> **Note**: `time_ind` refers to an index that follows every model stored in a MESA `history.data` file. This means the first model is at index 0 and the last is at index -1. It depends on your model which age this corresponds to. The powerful thing about `time_ind` is that it lets you find out some specific times very easily through numpy functions. For example, if you want to visualize a star at the point moment when it finishes hydrogen burning (end of the main seequence), you can look for the index where the central hydrogen mass fraction is very small, like `end_hburn_index = np.where(m1.hist.center_h1 < 1e-4)[0][0]`.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sg972iZhDfLv"
      },
      "source": [
        "### Find the apparent size and color of the star when it is halfway through hydrogen burning"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "EgBEdGU4DsqH"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UhBzqDj-Da2I"
      },
      "source": [
        "## Your first TULIPS animation"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "6jMsvKT7D8gP"
      },
      "source": [
        "By changing the `time_ind` property from one number to an array, we can now create a video of the evolution of the star that will be saved as `output_name.mp4`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AqQM1SN9-nFp"
      },
      "outputs": [],
      "source": [
        "fig, ax = tulips.perceived_color(m1, # MESA model\n",
        "                                 time_ind=(0, -1, 20), # Index range considered for the movie: from start to end, every 10 model_numbers\n",
        "                                 fig_size=(5,5),\n",
        "                       show_hrd_ticks_and_labels=True,\n",
        "                       output_fname=\"perceived-color-donor\" # Output file name in .mp4 format\n",
        "                       )\n",
        "plt.close(fig=fig)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "3GWy6tDgFTA_"
      },
      "outputs": [],
      "source": [
        "# Reading the output Video created\n",
        "Video(\"perceived-color-donor.mp4\", embed=True, width=600, height=600)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0i4_1cMmJca9"
      },
      "source": [
        "**Task:** Observe what happens at the moment when the star reaches its maximum radius.\n",
        "\n",
        "*   What happens to the luminosity of the star?\n",
        "*   What happens to its color (or effective temperature)?"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Answer**:\n"
      ],
      "metadata": {
        "id": "0WvRjxVUPdHM"
      }
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "KJxphcjVJLic"
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AljsalF6-nFp"
      },
      "source": [
        "## Evolution of the accretor star\n",
        "**Task:** Check the perceived color evolution of the accretor star\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "6wUPSx-EMsOZ"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "N1VPE1-yNRHW"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Questions:**\n",
        "* What happens to the radius of the star?\n",
        "* What happens to its color (or effective temperature)?"
      ],
      "metadata": {
        "id": "tUgmAkgBQlhI"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Answers**\n",
        "\n"
      ],
      "metadata": {
        "id": "zM_yjGN1QnHb"
      }
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "yTSOpsKHJWJU"
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jENoWdnuOHz0"
      },
      "source": [
        "# Checking the change in interior nuclear energy generation and energy loss"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "We can also observe the processes that take place inside the star and determine its evolution. TULIPS creates a visualization of these processes (usually shown as a Kippenhahn diagram) by showing the interior structure as nested circles whose radius is proportional to the enclosed mass within the circle. The outer circle is proportional to the total mass of the star.\n"
      ],
      "metadata": {
        "id": "B4VRDPELRpEh"
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "h56OaZh2QaAT"
      },
      "source": [
        "### For the donor"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "1o8hZQ0CPJPh"
      },
      "outputs": [],
      "source": [
        "fig, ax = tulips.energy_and_mixing(m1, # MESA model of the donor\n",
        "                                   time_ind=(0, -1, 20), # Index range considered for the movie: from start to end, every 10 model_numbers\n",
        "                                   output_fname=\"energy_movie_donor\" # Video file name\n",
        "                         )\n",
        "plt.close(fig=fig)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "EZKhm8alA6hi"
      },
      "outputs": [],
      "source": [
        "# Reading the output Video created\n",
        "Video(\"energy_movie_donor.mp4\", embed=True, width=700, height=700)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zG3JBKb1GiCI"
      },
      "source": [
        "**Questions**\n",
        "*   Which changes in mass and in energy generation do you observe?\n",
        "\n",
        "* At which points do you observe changes in mass?\n",
        "\n",
        "*  What is happening at the end of the simulation?\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Answers**\n"
      ],
      "metadata": {
        "id": "KEYTssjmSYAp"
      }
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "9jHCvd7aJj6P"
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "LleWpYoq_oRk"
      },
      "source": [
        "### For the accretor"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B7-4ht3IAlZg"
      },
      "source": [
        "**Task:** Check the change in the energy generation for the accretor from the onset of mass transfer to the end of the simulation"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TO-Wl2T3Azfl"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AIY7nJjJPoSm"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-cnBMSaj_o3h"
      },
      "source": [
        "**Questions**\n",
        "*   Which changes in mass and in energy generation do you observe?\n",
        "\n",
        "* At which points do you observe changes in mass?\n",
        "\n",
        "*  What is happening at the end of the simulation?\n"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "**Answers**\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "szcbO8-3UZnR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "A7SNo65lJqR6"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Checking the change in chemical composition"
      ],
      "metadata": {
        "id": "0RJUcxu5VK4r"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "We can look at the interior composition of stars and check how mass transfer affects their composition. The TULIPS diagrams show which isotopes are present in the stellar model, but also how the composition changes from the center to the stellar surface. Similar to the other diagrams, TULIPS represents the stellar object as a circle, divided into rings. To show the interior composition, TULIPS uses nested pie-charts. For every ring (layer inside the star), it shows the mass fraction of each isotope present in that layer by using different colors.\n",
        "\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "KdwHxWBf00Ru"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Accretor star"
      ],
      "metadata": {
        "id": "exHBQJpJ4HwD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "fig, ax = tulips.chemical_profile(m2,\n",
        "                                  axis_lim=1.7, # Rescaling the axis limits to show the accretor star\n",
        "                        time_ind=(0, -1, 1), fig_size=(4,4),\n",
        "                        hrd_inset=True, num_rings=200,\n",
        "                        output_fname=\"acc_comp\",\n",
        "                        fps=5 # frame per second\n",
        "                        )\n",
        "plt.close(fig=fig)"
      ],
      "metadata": {
        "id": "0sMcM5_z38fQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Reading the output Video created\n",
        "Video(\"acc_comp.mp4\", embed=True, width=700, height=700)"
      ],
      "metadata": {
        "id": "7CYk72uJ4hlY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "* How does the composition change with time and in which layers of the star?\n",
        "* What is the biggest change induced by mass transfer?\n",
        "* How different is this evolution compared to a single star?"
      ],
      "metadata": {
        "id": "Lv9I6oS758Dh"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Donor star"
      ],
      "metadata": {
        "id": "5iedM07u4CFh"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Check the change in composition in this star"
      ],
      "metadata": {
        "id": "F4aN7b1X4L70"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "mQLSWLh_3udQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Creating an animation of the change in composition from start to end of the simulation"
      ],
      "metadata": {
        "id": "ynXoeE2z1JrU"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "5fnHGTi8GjOE"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "mAvtiNEf2HZj"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "* How does the composition change with time and in which layers of the star?\n",
        "* What is the biggest change induced by mass transfer?\n",
        "* How different is this evolution compared to a single star?\n",
        "\n"
      ],
      "metadata": {
        "id": "lTYllXxu3QUv"
      }
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "AlMRMAo0KUhJ"
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "54HE8N9X59jW"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "celltoolbar": "Metagegevens bewerken",
    "colab": {
      "provenance": []
    },
    "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.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}