{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "ed71de7e", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T14:29:28.462971Z", "iopub.status.busy": "2025-07-31T14:29:28.462722Z", "iopub.status.idle": "2025-07-31T14:29:28.877557Z", "shell.execute_reply": "2025-07-31T14:29:28.876987Z" }, "nbsphinx": "hidden" }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 2, "id": "fa859498", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T14:29:28.879503Z", "iopub.status.busy": "2025-07-31T14:29:28.879284Z", "iopub.status.idle": "2025-07-31T14:29:29.044105Z", "shell.execute_reply": "2025-07-31T14:29:29.043575Z" }, "nbsphinx": "hidden" }, "outputs": [ { "ename": "AttributeError", "evalue": "'ZMQInteractiveShell' object has no attribute 'magic'", "output_type": "error", "traceback": [ "\u001b[31m---------------------------------------------------------------------------\u001b[39m", "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", "\u001b[36mFile \u001b[39m\u001b[32m~/work/gala/gala/docs/tutorials/nb_setup:1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mget_ipython\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m.\u001b[49m\u001b[43mmagic\u001b[49m(\u001b[33m'\u001b[39m\u001b[33mconfig InlineBackend.figure_format = \u001b[39m\u001b[33m\"\u001b[39m\u001b[33mretina\u001b[39m\u001b[33m\"\u001b[39m\u001b[33m'\u001b[39m) \u001b[38;5;66;03m# noqa\u001b[39;00m\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mmatplotlib\u001b[39;00m\u001b[34;01m.\u001b[39;00m\u001b[34;01mpyplot\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mas\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mplt\u001b[39;00m\n\u001b[32m 5\u001b[39m plt.style.use(\u001b[33m\"\u001b[39m\u001b[33mdefault\u001b[39m\u001b[33m\"\u001b[39m)\n", "\u001b[31mAttributeError\u001b[39m: 'ZMQInteractiveShell' object has no attribute 'magic'" ] } ], "source": [ "%run nb_setup" ] }, { "cell_type": "markdown", "id": "394aaa0d", "metadata": {}, "source": [ "# Defining a Milky Way potential model" ] }, { "cell_type": "code", "execution_count": 3, "id": "4f932b92", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T14:29:29.045813Z", "iopub.status.busy": "2025-07-31T14:29:29.045650Z", "iopub.status.idle": "2025-07-31T14:29:29.808604Z", "shell.execute_reply": "2025-07-31T14:29:29.808034Z" } }, "outputs": [], "source": [ "import astropy.units as u\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "from astropy.io import ascii\n", "from scipy.optimize import leastsq\n", "\n", "import gala.potential as gp\n", "from gala.units import galactic\n" ] }, { "cell_type": "markdown", "id": "19d56fe4", "metadata": {}, "source": [ "## Introduction\n", "\n", "`gala` provides a simple and easy way to access and integrate orbits in an\n", "approximate mass model for the Milky Way. The parameters of the mass model are\n", "determined by least-squares fitting the enclosed mass profile of a pre-defined\n", "potential form to recent measurements compiled from the literature. These\n", "measurements are provided with the documentation of `gala` and are shown\n", "below. The radius units are kpc, and mass units are solar masses:" ] }, { "cell_type": "code", "execution_count": 4, "id": "071ec9f9", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T14:29:29.810631Z", "iopub.status.busy": "2025-07-31T14:29:29.810383Z", "iopub.status.idle": "2025-07-31T14:29:29.818600Z", "shell.execute_reply": "2025-07-31T14:29:29.818162Z" } }, "outputs": [], "source": [ "tbl = ascii.read(\"data/MW_mass_enclosed.csv\")" ] }, { "cell_type": "code", "execution_count": 5, "id": "3ffc325f", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T14:29:29.820169Z", "iopub.status.busy": "2025-07-31T14:29:29.819963Z", "iopub.status.idle": "2025-07-31T14:29:29.825826Z", "shell.execute_reply": "2025-07-31T14:29:29.825341Z" } }, "outputs": [ { "data": { "text/html": [ "
Table length=16\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
rMencMenc_err_negMenc_err_posref
float64float64float64float64str27
0.0130000000.010000000.010000000.0Feldmeier et al. (2014)
0.12800000000.0200000000.0200000000.0Launhardt et al. (2002)
8.189502860861.524294994562473.7977144858963492.608627Bovy et al. (2012)
8.3110417867208.190554475949382.6968844387023236.020782McMillan (2011)
8.4102421035406.9035616733918715.62994415468328224.531876Koposov et al. (2010)
19.0208023299175.3043844317988008.3810134833267089.920685Kuepper et al. (2015)
50.0539884832748.4897519995734543.31433268490735257.4718Wilkinson & Evans (1999)
50.0529886965173.187269997867269.65930238536752776.21277Sakamoto et al. (2003)
50.0399914690706.92847109976539940.5371172696676468.2511Smith et al. (2007)
50.0419910425325.726839991469076.96850638172735113.64386Deason et al. (2012)
60.0399914690957.518869985070910.6335464344945146.92987Xue et al. (2008)
80.0689852841359.0248299936018002.3314110361048549.1029Gnedin et al. (2010)
100.01399701417307.7747899808054059.3811831336271726.1903Watkins et al. (2010)
120.0539884832260.29584199957345314.72906123854764645.32489Battaglia et al. (2005)
150.0750000000000.0250000000000.0250000000000.0Deason et al. (2012)
200.0679854974257.2006409912558030.05396313652012195.06256Bhattacherjee et al. (2014)
" ], "text/plain": [ "\n", " r Menc ... Menc_err_pos ref \n", "float64 float64 ... float64 str27 \n", "------- ------------------ ... ------------------ ---------------------------\n", " 0.01 30000000.0 ... 10000000.0 Feldmeier et al. (2014)\n", " 0.12 800000000.0 ... 200000000.0 Launhardt et al. (2002)\n", " 8.1 89502860861.52429 ... 4858963492.608627 Bovy et al. (2012)\n", " 8.3 110417867208.19055 ... 4387023236.020782 McMillan (2011)\n", " 8.4 102421035406.90356 ... 15468328224.531876 Koposov et al. (2010)\n", " 19.0 208023299175.30438 ... 34833267089.920685 Kuepper et al. (2015)\n", " 50.0 539884832748.48975 ... 268490735257.4718 Wilkinson & Evans (1999)\n", " 50.0 529886965173.18726 ... 38536752776.21277 Sakamoto et al. (2003)\n", " 50.0 399914690706.92847 ... 72696676468.2511 Smith et al. (2007)\n", " 50.0 419910425325.7268 ... 38172735113.64386 Deason et al. (2012)\n", " 60.0 399914690957.5188 ... 64344945146.92987 Xue et al. (2008)\n", " 80.0 689852841359.0248 ... 110361048549.1029 Gnedin et al. (2010)\n", " 100.0 1399701417307.7747 ... 831336271726.1903 Watkins et al. (2010)\n", " 120.0 539884832260.29584 ... 123854764645.32489 Battaglia et al. (2005)\n", " 150.0 750000000000.0 ... 250000000000.0 Deason et al. (2012)\n", " 200.0 679854974257.2006 ... 313652012195.06256 Bhattacherjee et al. (2014)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tbl" ] }, { "cell_type": "markdown", "id": "be9d818a", "metadata": {}, "source": [ "Let's now plot the above data and uncertainties:" ] }, { "cell_type": "code", "execution_count": 6, "id": "f73b651e", "metadata": { "execution": { "iopub.execute_input": "2025-07-31T14:29:29.827347Z", "iopub.status.busy": "2025-07-31T14:29:29.827185Z", "iopub.status.idle": "2025-07-31T14:29:30.396292Z", "shell.execute_reply": "2025-07-31T14:29:30.395721Z" }, "lines_to_end_of_cell_marker": 2 }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "<>:21: SyntaxWarning: invalid escape sequence '\\o'\n", "<>:21: SyntaxWarning: invalid escape sequence '\\o'\n", "/tmp/ipykernel_3838/2928631988.py:21: SyntaxWarning: invalid escape sequence '\\o'\n", " ax.set_ylabel(\"$M(" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig, ax = plt.subplots(1, 1, figsize=(4, 4))\n", "\n", "ax.errorbar(\n", " tbl[\"r\"],\n", " tbl[\"Menc\"],\n", " yerr=(tbl[\"Menc_err_neg\"], tbl[\"Menc_err_pos\"]),\n", " marker=\"o\",\n", " markersize=2,\n", " color=\"k\",\n", " alpha=1.0,\n", " ecolor=\"#aaaaaa\",\n", " capthick=0,\n", " linestyle=\"none\",\n", " elinewidth=1.0,\n", ")\n", "\n", "ax.set_xlim(1e-3, 10**2.6)\n", "ax.set_ylim(7e6, 10**12.25)\n", "\n", "ax.set_xlabel(\"$r$ [kpc]\")\n", "ax.set_ylabel(\"$M(:27: SyntaxWarning: invalid escape sequence '\\o'\n", "<>:27: SyntaxWarning: invalid escape sequence '\\o'\n", "/tmp/ipykernel_3838/1286690888.py:27: SyntaxWarning: invalid escape sequence '\\o'\n", " ax.set_ylabel(\"$M(" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "xyz = np.zeros((3, 256))\n", "xyz[0] = np.logspace(-3, 3, 256)\n", "\n", "fig, ax = plt.subplots(1, 1, figsize=(4, 4))\n", "\n", "ax.errorbar(\n", " tbl[\"r\"],\n", " tbl[\"Menc\"],\n", " yerr=(tbl[\"Menc_err_neg\"], tbl[\"Menc_err_pos\"]),\n", " marker=\"o\",\n", " markersize=2,\n", " color=\"k\",\n", " alpha=1.0,\n", " ecolor=\"#aaaaaa\",\n", " capthick=0,\n", " linestyle=\"none\",\n", " elinewidth=1.0,\n", ")\n", "\n", "fit_menc = init_potential.mass_enclosed(xyz * u.kpc)\n", "ax.loglog(xyz[0], fit_menc.value, marker=\"\", color=\"#3182bd\", linewidth=2, alpha=0.7)\n", "\n", "ax.set_xlim(1e-3, 10**2.6)\n", "ax.set_ylim(7e6, 10**12.25)\n", "\n", "ax.set_xlabel(\"$r$ [kpc]\")\n", "ax.set_ylabel(\"$M(:27: SyntaxWarning: invalid escape sequence '\\o'\n", "<>:27: SyntaxWarning: invalid escape sequence '\\o'\n", "/tmp/ipykernel_3838/1667721107.py:27: SyntaxWarning: invalid escape sequence '\\o'\n", " ax.set_ylabel(\"$M(" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "xyz = np.zeros((3, 256))\n", "xyz[0] = np.logspace(-3, 3, 256)\n", "\n", "fig, ax = plt.subplots(1, 1, figsize=(4, 4))\n", "\n", "ax.errorbar(\n", " tbl[\"r\"],\n", " tbl[\"Menc\"],\n", " yerr=(tbl[\"Menc_err_neg\"], tbl[\"Menc_err_pos\"]),\n", " marker=\"o\",\n", " markersize=2,\n", " color=\"k\",\n", " alpha=1.0,\n", " ecolor=\"#aaaaaa\",\n", " capthick=0,\n", " linestyle=\"none\",\n", " elinewidth=1.0,\n", ")\n", "\n", "fit_menc = fit_potential.mass_enclosed(xyz * u.kpc)\n", "ax.loglog(xyz[0], fit_menc.value, marker=\"\", color=\"#3182bd\", linewidth=2, alpha=0.7)\n", "\n", "ax.set_xlim(1e-3, 10**2.6)\n", "ax.set_ylim(7e6, 10**12.25)\n", "\n", "ax.set_xlabel(\"$r$ [kpc]\")\n", "ax.set_ylabel(\"$M(" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "potential = MilkyWayPotential()\n", "potential" ] } ], "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.12.11" } }, "nbformat": 4, "nbformat_minor": 5 }