{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a544d28c-c2e1-4c6a-b55b-8caec440743f",
   "metadata": {},
   "source": [
    "# Running individual systems with binarycpython\n",
    "This notebook will show you how to run single systems and analyze their results.\n",
    "\n",
    "It can be useful to have some functions to quickly run a single system to e.g. inspect what evolutionary steps a specific system goes through, to plot the mass loss evolution of a single star, etc. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd5d9ec7-5791-45f1-afbd-225947e2a583",
   "metadata": {},
   "source": [
    "## Single system with run_wrapper\n",
    "The simplest method to run a single system is to use the run_system wrapper. This function deals with setting up the argument string, makes sure all the required parameters are included and handles setting and cleaning up the custom logging functionality (see notebook_custom_logging).\n",
    "\n",
    "As arguments to this function we can add any of the parameters that binary_c itself actually knows, as well as:\n",
    "- custom_logging_code: string containing a print statement that binary_c can use to print information\n",
    "- log_filename: path of the logfile that binary_c generates\n",
    "- parse_function: function that handles parsing the output of binary-c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e32dcdee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from binarycpython.utils.functions import temp_dir\n",
    "TMP_DIR = temp_dir(\"notebooks\", \"notebook_individual_systems\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "425efed3-d8e3-432d-829e-41d8ebe05162",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binarycpython.utils.run_system_wrapper import run_system\n",
    "# help(run_system) # Uncomment to see the docstring"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b2abab48-433d-4936-8434-14804c52c9f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SINGLE_STAR_LIFETIME 1 12461.9\n",
      "\n"
     ]
    }
   ],
   "source": [
    "output = run_system(M_1=1)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f127a5e4-dc01-4472-9130-8a943c92e8a7",
   "metadata": {},
   "source": [
    "Lets try adding a log filename now:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "029fc3f2-f09a-49af-a32b-248505738f2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      TIME      M1       M2   K1  K2           SEP      PER    ECC  R1/ROL1 R2/ROL2  TYPE RANDOM_SEED=17089 RANDOM_COUNT=0\n",
      "     0.0000    1.000    0.000  1  15            -1       -1  -1.00   0.000   0.000  \"INITIAL \"\n",
      " 11003.1302    1.000    0.000  2  15            -1       -1  -1.00   0.000   0.000  \"OFF_MS\"\n",
      " 11003.1302    1.000    0.000  2  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
      " 11582.2424    1.000    0.000  3  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
      " 12325.1085    0.817    0.000  4  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
      " 12457.1301    0.783    0.000  5  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
      " 12460.9983    0.716    0.000  6  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
      " 12460.9983    0.716    0.000  6  15            -1       -1  -1.00   0.000   0.000  \"shrinkAGB\"\n",
      " 12461.9458    0.518    0.000 11  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
      " 15000.0000    0.518    0.000 11  15            -1       -1  -1.00   0.000   0.000  \"MAX_TIME\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "log_filename = os.path.join(TMP_DIR, 'test_logfile.txt')\n",
    "output = run_system(M_1=1, log_filename=log_filename, api_log_filename_prefix=TMP_DIR)\n",
    "with open(log_filename, 'r') as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "606670f2-3e0a-43c7-a885-006b92fac9d2",
   "metadata": {},
   "source": [
    "To get more useful output we can include a custom_logging snippet (see notebook_custom_logging):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e6a23b55-ca42-440d-83ac-e76a24a83a67",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EXAMPLE_MASSLOSS             0.000000000000e+00 1 1 1\n",
      "EXAMPLE_MASSLOSS             0.000000000000e+00 1 1 1\n",
      "EXAMPLE_MASSLOSS             1.000000000000e-06 1 1 1\n",
      "EXAMPLE_MASSLOSS             2.000000000000e-06 1 1 1\n"
     ]
    }
   ],
   "source": [
    "from binarycpython.utils.custom_logging_functions import binary_c_log_code\n",
    "\n",
    "# Create the print statement\n",
    "custom_logging_print_statement = \"\"\"\n",
    "Printf(\"EXAMPLE_MASSLOSS %30.12e %g %g %d\\\\n\",\n",
    "    // \n",
    "    stardata->model.time, // 1\n",
    "    stardata->star[0].mass, //2\n",
    "    stardata->common.zero_age.mass[0], //4\n",
    "\n",
    "    stardata->star[0].stellar_type //5\n",
    ");\n",
    "\"\"\"\n",
    "\n",
    "# Generate entire shared lib code around logging lines\n",
    "custom_logging_code = binary_c_log_code(custom_logging_print_statement)\n",
    "\n",
    "output = run_system(M_1=1, custom_logging_code=custom_logging_code, api_log_filename_prefix=TMP_DIR)\n",
    "print('\\n'.join(output.splitlines()[:4]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c885143-db79-4fed-b4c4-0bd846e24f7d",
   "metadata": {},
   "source": [
    "Now we have some actual output, it is time to create a parse_function which parses the output. Adding a parse_function to the run_system will make run_system run the output of binary_c through the parse_function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3822721f-217a-495b-962e-d57137b9e290",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['time', 'mass', 'initial_mass', 'stellar_type'], [0.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 1.0]]\n"
     ]
    }
   ],
   "source": [
    "def parse_function(output):\n",
    "    \"\"\"\n",
    "    Example function to parse the output of binary_c\n",
    "    \"\"\"\n",
    "\n",
    "    # \n",
    "    column_names = ['time', 'mass', 'initial_mass', 'stellar_type']\n",
    "    value_lines = [column_names]\n",
    "    \n",
    "    # Loop over output\n",
    "    for line in output.splitlines():\n",
    "        \n",
    "        # Select the lines starting with the header we chose\n",
    "        if line.startswith(\"EXAMPLE_MASSLOSS\"):\n",
    "        \n",
    "        # Split the output and fetch the data\n",
    "            split_line = line.split()\n",
    "            values = [float(el) for el in split_line[1:]]\n",
    "            value_lines.append(values)\n",
    "\n",
    "    return value_lines\n",
    "\n",
    "# Catch output\n",
    "output = run_system(M_1=1, custom_logging_code=custom_logging_code, parse_function=parse_function)\n",
    "print(output[:3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a551f07f-2eff-4425-9375-267579a581b3",
   "metadata": {},
   "source": [
    "This output can now be turned into e.g. an Numpy array or Pandas dataframe (my favorite: makes querying the data very easy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "654a07ed-2a88-46ff-9da0-b7759580f9f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0        time      mass initial_mass stellar_type\n",
      "1           0         1            1            1\n",
      "2           0         1            1            1\n",
      "3       1e-06         1            1            1\n",
      "4       2e-06         1            1            1\n",
      "5       3e-06         1            1            1\n",
      "...       ...       ...          ...          ...\n",
      "1617  12461.8  0.546683            1            6\n",
      "1618  12461.9  0.517749            1           11\n",
      "1619  13461.9  0.517749            1           11\n",
      "1620  14461.9  0.517749            1           11\n",
      "1621    15000  0.517749            1           11\n",
      "\n",
      "[1621 rows x 4 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Load data into dataframe\n",
    "example_df = pd.DataFrame(output)\n",
    "\n",
    "# Fix column headers\n",
    "example_df.columns = example_df.iloc[0]\n",
    "example_df = example_df.drop(example_df.index[0])\n",
    "\n",
    "print(example_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "325c2ce6-f9a1-46b7-937f-84040e1252cf",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Single system via population object\n",
    "When setting up your population object (see notebook_population), and configuring all the parameters, it is possible to run a single system using that same configuration. It will use the parse_function if set, and running a single system is a good method to test if everything works accordingly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4a98ffca-1b72-4bb8-8df1-3bf3187d882f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binarycpython.utils.grid import Population\n",
    "# help(Population) # Uncomment to see the docstring"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e2c2ef0-3db2-46a6-8c85-9b6cf720eb6a",
   "metadata": {},
   "source": [
    "First, let's try this without any custom logging or parsing functionality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bff1cc2e-6b32-4ba0-879f-879ffbabd223",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adding: api_log_filename_prefix=/tmp/binary_c_python/notebooks/notebook_individual_systems to BSE_options\n",
      "adding: M_1=10 to BSE_options\n",
      "Creating and loading custom logging functionality\n",
      "Running binary_c M_1 10 api_log_filename_prefix /tmp/binary_c_python/notebooks/notebook_individual_systems\n",
      "Cleaning up the custom logging stuff. type: single\n",
      "SINGLE_STAR_LIFETIME 10 28.4838\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# Create the population object\n",
    "example_pop = Population()\n",
    "\n",
    "# Set some parameters\n",
    "example_pop.set(\n",
    "    verbosity=1,\n",
    "    api_log_filename_prefix=TMP_DIR\n",
    ")\n",
    "example_pop.set(\n",
    "    M_1=10\n",
    ")\n",
    "\n",
    "# get output and print\n",
    "output = example_pop.evolve_single()\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae01fa35-f8b1-4a40-bfb2-b9e872cae0e7",
   "metadata": {},
   "source": [
    "Now lets add some actual output with the custom logging"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dd748bab-b57e-4129-8350-9ea11fa179d0",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adding: C_logging_code=\n",
      "Printf(\"EXAMPLE_MASSLOSS %30.12e %g %g %g %d\\n\",\n",
      "    // \n",
      "    stardata->model.time, // 1\n",
      "    stardata->star[0].mass, //2\n",
      "    stardata->previous_stardata->star[0].mass, //3\n",
      "    stardata->common.zero_age.mass[0], //4\n",
      "\n",
      "    stardata->star[0].stellar_type //5\n",
      ");\n",
      " to grid_options\n",
      "Creating and loading custom logging functionality\n",
      "Running binary_c M_1 10 api_log_filename_prefix /tmp/binary_c_python/notebooks/notebook_individual_systems\n",
      "Cleaning up the custom logging stuff. type: single\n",
      "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_8967553693ac4e11a49c42d4eef773e8.so\n",
      "EXAMPLE_MASSLOSS             0.000000000000e+00 10 0 10 1\n",
      "EXAMPLE_MASSLOSS             0.000000000000e+00 10 10 10 1\n",
      "EXAMPLE_MASSLOSS             1.000000000000e-06 10 10 10 1\n",
      "EXAMPLE_MASSLOSS             2.000000000000e-06 10 10 10 1\n"
     ]
    }
   ],
   "source": [
    "custom_logging_print_statement = \"\"\"\n",
    "Printf(\"EXAMPLE_MASSLOSS %30.12e %g %g %g %d\\\\n\",\n",
    "    // \n",
    "    stardata->model.time, // 1\n",
    "    stardata->star[0].mass, //2\n",
    "    stardata->previous_stardata->star[0].mass, //3\n",
    "    stardata->common.zero_age.mass[0], //4\n",
    "\n",
    "    stardata->star[0].stellar_type //5\n",
    ");\n",
    "\"\"\"   \n",
    "\n",
    "example_pop.set(C_logging_code=custom_logging_print_statement)\n",
    "\n",
    "# get output and print\n",
    "output = example_pop.evolve_single()\n",
    "print('\\n'.join(output.splitlines()[:4]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "588a7d9e-9d64-4b3b-8907-656b905286e8",
   "metadata": {},
   "source": [
    "Lastly we can add a parse_function to handle parsing the output again. \n",
    "\n",
    "Because the parse_function will now be part of the population object, it can access information of the object. We need to make a new parse function that is fit for an object: we the arguments now need to be (self, output). Returning the data is useful when running evolve_single(), but won't be used in a population evolution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fec39154-cce6-438c-8c2c-509d76b00f34",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import numpy as np\n",
    "\n",
    "def object_parse_function(self, output):\n",
    "    \"\"\"\n",
    "    Example parse function that can be added to the population object\n",
    "    \"\"\"\n",
    "\n",
    "    # We can access object instance information now. \n",
    "    # In this way we can store the results in a file for example. \n",
    "    output_file = os.path.join(self.custom_options['output_dir'], 'example_output.json')\n",
    "    \n",
    "    # \n",
    "    column_names = ['time', 'mass', 'initial_mass', 'stellar_type']\n",
    "    value_lines = [column_names]\n",
    "    \n",
    "    # Loop over output\n",
    "    for line in output.splitlines():\n",
    "        \n",
    "        # Select the lines starting with the header we chose\n",
    "        if line.startswith(\"EXAMPLE_MASSLOSS\"):\n",
    "        \n",
    "        # Split the output and fetch the data\n",
    "            split_line = line.split()\n",
    "            values = [float(el) for el in split_line[1:]]\n",
    "            value_lines.append(values)\n",
    "\n",
    "    # Turn into an array\n",
    "    values_array = np.array(value_lines[1:])\n",
    "    \n",
    "    # make dict and fill\n",
    "    output_dict = {}\n",
    "    for i in range(len(column_names)):\n",
    "        output_dict[column_names[i]] = list(values_array[:,i])\n",
    "\n",
    "    # Write to file\n",
    "    with open(output_file, 'w') as f:\n",
    "        f.write(json.dumps(output_dict, indent=4))\n",
    "        \n",
    "    # Return something anyway\n",
    "    return value_lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "57347512-fd4a-434b-b13c-5e6dbd3ac415",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adding: parse_function=<function object_parse_function at 0x7fb4d41ebbf8> to grid_options\n",
      "<<<< Warning: Key does not match previously known parameter:                     adding: output_dir=/tmp/binary_c_python/notebooks/notebook_individual_systems to custom_options >>>>\n",
      "adding: api_log_filename_prefix=/tmp/binary_c_python/notebooks/notebook_individual_systems to BSE_options\n",
      "Creating and loading custom logging functionality\n",
      "Running binary_c M_1 10 api_log_filename_prefix /tmp/binary_c_python/notebooks/notebook_individual_systems\n",
      "Cleaning up the custom logging stuff. type: single\n",
      "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_5d7779e8190e4b79b10c7e6a44cb0e7e.so\n",
      "[['time', 'mass', 'initial_mass', 'stellar_type'], [0.0, 10.0, 0.0, 10.0, 1.0], [0.0, 10.0, 10.0, 10.0, 1.0], [1e-06, 10.0, 10.0, 10.0, 1.0]]\n",
      "dict_keys(['time', 'mass', 'initial_mass', 'stellar_type'])\n"
     ]
    }
   ],
   "source": [
    "example_pop.set(\n",
    "    parse_function=object_parse_function,\n",
    "    output_dir=TMP_DIR,\n",
    "    api_log_filename_prefix=TMP_DIR\n",
    ")\n",
    "output = example_pop.evolve_single()\n",
    "print(output[:4])\n",
    "\n",
    "# Example of loading the data that was written\n",
    "with open(os.path.join(example_pop.custom_options['output_dir'], 'example_output.json')) as f:\n",
    "    written_data = json.loads(f.read())\n",
    "\n",
    "print(written_data.keys())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddc06da3-fc68-4c6f-8067-14ea862b964d",
   "metadata": {},
   "source": [
    "## Single system via API functionality\n",
    "It is possible to construct your own functionality to run a single system by directly calling the API function to run a system. Under the hood all the other functions and wrappers actually use this API.\n",
    "\n",
    "There are less failsafes for this method, so this make sure the input is correct and binary_c knows all the arguments you pass in.\n",
    "\n",
    "for more details on this API function see `notebook_api_functions`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56886792-d379-4eac-b0d4-54508edb39c7",
   "metadata": {},
   "source": [
    "First we must construct the argument string that we pass to binary_c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ec48125c-6bf5-48f4-9357-8261800b5d8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SINGLE_STAR_LIFETIME 15 14.9947\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# For a binary system we need to pass in these arguments\n",
    "M_1 = 15.0  # Msun\n",
    "M_2 = 14.0  # Msun\n",
    "separation = 0  # 0 = ignored, use period\n",
    "orbital_period = 4530.0  # days\n",
    "eccentricity = 0.0\n",
    "metallicity = 0.02\n",
    "max_evolution_time = 15000  # Myr. You need to include this argument.\n",
    "api_log_filename_prefix = TMP_DIR\n",
    "\n",
    "# Here we set up the argument string that is passed to the bindings\n",
    "argstring = \"\"\"\n",
    "binary_c M_1 {M_1} M_2 {M_2} separation {separation} orbital_period {orbital_period} eccentricity {eccentricity} metallicity {metallicity} max_evolution_time {max_evolution_time} api_log_filename_prefix {api_log_filename_prefix}\n",
    "\"\"\".format(\n",
    "    M_1=M_1,\n",
    "    M_2=M_2,\n",
    "    separation=separation,\n",
    "    orbital_period=orbital_period,\n",
    "    eccentricity=eccentricity,\n",
    "    metallicity=metallicity,\n",
    "    max_evolution_time=max_evolution_time,\n",
    "    api_log_filename_prefix=TMP_DIR\n",
    ").strip()\n",
    "\n",
    "from binarycpython import _binary_c_bindings\n",
    "\n",
    "output = _binary_c_bindings.run_system(argstring)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55c8ea24-0fc0-452c-8121-1e7667433479",
   "metadata": {},
   "source": [
    "As we can see above, the output is rather empty. But if SINGLE_STAR_LIFETIME is printed we know we caught the output correctly. To get actual output we should have timesteps printed in the `log_every_timestep.c` in binary_c, or add some custom_logging (see notebook_custom_logging). "
   ]
  }
 ],
 "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.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
