{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run one binary on your laptop for debugging"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For debugging purposes you might want to run a single binary on your laptop to test a quick modification of the inlists, run_star_extras.f or run_binary_extras.f. Assume we want to run the same binary as in the previous tutorial. To use the POSYDON API to run a single binary, you can use the following command:\n",
    "\n",
    "```bash\n",
    "posydon-setup-grid --grid-type fixed --inifile HMS-HMS_yggdrasil.ini --submission-type shell\n",
    "```\n",
    "\n",
    "Similar to before, in our directorty we have:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "binary\t\t grid_test.csv\t\tlaptop.ipynb  star1\n",
      "grid_command.sh  HMS-HMS_yggdrasil.ini\tmk\t      star2\n"
     ]
    }
   ],
   "source": [
    "!ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now export the `SLURM_ARRAY_TASK_ID` enviroment variable corresponding to the csv index of the `grid_test.csv` we want to run, `0` in this case in the temrinal:\n",
    "\n",
    "```bash\n",
    "export $SLURM_ARRAY_TASK_ID=0\n",
    "```\n",
    "\n",
    "We can now  run the MESA binary simulation in the terminal with the following command:\n",
    "\n",
    "```bash\n",
    "chmod +x grid_command.sh\n",
    "./grid_command.sh\n",
    "``````\n",
    "\n",
    "The terminal will not display an output, the screen output is saved to an `out.txt` file in the simulation directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "binary\n",
      "grid_command.sh\n",
      "grid_test.csv\n",
      "HMS-HMS_yggdrasil.ini\n",
      "laptop.ipynb\n",
      "mk\n",
      "star1\n",
      "star2\n",
      "Zbase_0.0014_m1_30.0000_m2_21.0000_initial_z_1.4200e-03_initial_period_in_days_1.0000e+01_grid_index_0\n"
     ]
    }
   ],
   "source": [
    "!ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "binary_history_columns.list  inlist_grid_star1_binary_controls\n",
      "binary_history.data\t     inlist_grid_star2_binary_controls\n",
      "history_columns.list\t     LOGS1\n",
      "initial_star1.mod\t     LOGS2\n",
      "initial_star2.mod\t     out.txt\n",
      "inlist\t\t\t     profile_columns.list\n",
      "inlist_grid_points\t     tmp.hdf5\n"
     ]
    }
   ],
   "source": [
    "!ls Zbase_0.0014_m1_30.0000_m2_21.0000_initial_z_1.4200e-03_initial_period_in_days_1.0000e+01_grid_index_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      " read /srv/beegfs/scratch/shares/astro/posydon/simone/documentation/running_mesa/running_on_laptop/binary/inlist_project\n",
      " read inlist_grid_points\n",
      "                                         version_number       11701\n",
      " read inlist_grid_star1_binary_controls\n",
      " set_eos_PC_parameters\n",
      "                             mass_fraction_limit_for_PC    1.0000000000000000D-03\n",
      "                                       logRho1_PC_limit    2.9990000000000001D+00\n",
      "                                       logRho2_PC_limit    2.7999999999999998D+00\n",
      "                                     log_Gamma_all_HELM    1.0000000000000000D+00\n",
      "                                       log_Gamma_all_PC    1.3010299956000000D+00\n",
      "                                 PC_Gamma_start_crystal    1.5000000000000000D+02\n",
      "                                  PC_Gamma_full_crystal    1.7500000000000000D+02\n",
      "                                               PC_min_Z    9.9900000000000000D-01\n",
      " change rates preference to           2\n",
      "                                        set_initial_age    0.0000000000000000D+00\n",
      "                               set_initial_model_number           0\n",
      " change to \"approx21.net\"\n",
      " number of species          21\n",
      " new_rotation_flag T\n",
      "                                 new_surface_rotation_v           0    0.0000000000000000D+00    0.0000000000000000D+00\n",
      " net name approx21.net\n",
      " rotation_flag T\n",
      " species\n",
      "     1   neut    \n",
      "     2   h1      \n",
      "     3   prot    \n",
      "     4   he3     \n",
      "     5   he4     \n",
      "     6   c12     \n",
      "     7   n14     \n",
      "     8   o16     \n",
      "     9   ne20    \n",
      "    10   mg24    \n",
      "    11   si28    \n",
      "    12   s32     \n",
      "    13   ar36    \n",
      "    14   ca40    \n",
      "    15   ti44    \n",
      "    16   cr48    \n",
      "    17   cr56    \n",
      "    18   fe52    \n",
      "    19   fe54    \n",
      "    20   fe56    \n",
      "    21   ni56    \n",
      "\n",
      " kappa_file_prefix gs98\n",
      " kappa_lowT_prefix lowT_fa05_gs98\n",
      "                                        OMP_NUM_THREADS           4\n",
      "\n",
      "\n",
      "\n",
      "\n",
      " read inlist_grid_star2_binary_controls\n",
      " set_eos_PC_parameters\n",
      "                             mass_fraction_limit_for_PC    1.0000000000000000D-03\n",
      "                                       logRho1_PC_limit    2.9990000000000001D+00\n",
      "                                       logRho2_PC_limit    2.7999999999999998D+00\n",
      "                                     log_Gamma_all_HELM    1.0000000000000000D+00\n",
      "                                       log_Gamma_all_PC    1.3010299956000000D+00\n",
      "                                 PC_Gamma_start_crystal    1.5000000000000000D+02\n",
      "                                  PC_Gamma_full_crystal    1.7500000000000000D+02\n",
      "                                               PC_min_Z    9.9900000000000000D-01\n",
      " change rates preference to           2\n",
      "                                        set_initial_age    0.0000000000000000D+00\n",
      "                               set_initial_model_number           0\n",
      " change to \"approx21.net\"\n",
      " number of species          21\n",
      " new_rotation_flag T\n",
      "                                 new_surface_rotation_v           0    0.0000000000000000D+00    0.0000000000000000D+00\n",
      " net name approx21.net\n",
      " rotation_flag T\n",
      " species\n",
      "     1   neut    \n",
      "     2   h1      \n",
      "     3   prot    \n",
      "     4   he3     \n",
      "     5   he4     \n",
      "     6   c12     \n",
      "     7   n14     \n",
      "     8   o16     \n",
      "     9   ne20    \n",
      "    10   mg24    \n",
      "    11   si28    \n",
      "    12   s32     \n",
      "    13   ar36    \n",
      "    14   ca40    \n",
      "    15   ti44    \n",
      "    16   cr48    \n",
      "    17   cr56    \n",
      "    18   fe52    \n",
      "    19   fe54    \n",
      "    20   fe56    \n",
      "    21   ni56    \n",
      "\n",
      " kappa_file_prefix gs98\n",
      " kappa_lowT_prefix lowT_fa05_gs98\n",
      "                                        OMP_NUM_THREADS           4\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "                                                     m2    2.1000000000000000D+01\n",
      "                                                     m1    3.0000000000000000D+01\n",
      "                                 initial_period_in_days    1.0000000000000000D+01\n",
      "                             initial_separation_in_Rsun    7.2417496333000003D+01\n",
      "                                        jdot_multiplier    1.0000000000000000D+00\n",
      "                                                     fr    1.0000000000000000D-02\n",
      "\n",
      "\n",
      "\n",
      " The binary terminal output contains the following information\n",
      "\n",
      "      'step' is the number of steps since the start of the run,\n",
      "      'lg_dt' is log10 timestep in years,\n",
      "      'age_yr' is the simulated years since the start run,\n",
      "      'M1+M2' is the total mass of the system (Msun),\n",
      "      'M1' is the mass of the primary (Msun)\n",
      "      'M2' is the mass of the secondary (Msun)\n",
      "      'separ' is the semi-major axis of the orbit (Rsun),\n",
      "      'R1' is the radius of the primary (Rsun)\n",
      "      'R2' is the radius of the secondary (Rsun)\n",
      "      'Porb' is the orbital period (days),\n",
      "      'P1' is the rotation period of star 1 (days, zero if not modeling rotation),\n",
      "      'P2' is the rotation period of star 2 (days, zero if not modeling rotation),\n",
      "      'e' orbital eccentricity,\n",
      "      'dot_e' time derivative of e (1/yr),\n",
      "      'Eorb' orbital energy G*M1*M2/2*separation (ergs),\n",
      "      'M2/M1' mass ratio,\n",
      "      'vorb1' orbital velocity of star 1 (km/s),\n",
      "      'vorb2' orbital velocity of star 2 (km/s),\n",
      "      'pm_i' index of star evolved as point mass, zero if both stars are modeled,\n",
      "      'RL1' Roche lobe radius of star 1 (Rsun),\n",
      "      'Rl2' Roche lobe radius of star 2 (Rsun),\n",
      "      'donor_i' index of star taken as donor,\n",
      "      'RL_gap1' (R1-Rl1)/Rl1,\n",
      "      'RL_gap2' (R2-Rl2)/Rl2,\n",
      "      'dot_Mmt', mass transfer rate (Msun/yr),\n",
      "      'dot_M1', time derivative for the mass of star 1 (Msun/yr),\n",
      "      'dot_M2', time derivative for the mass of star 2 (Msun/yr),\n",
      "      'eff', mass transfer efficiency, computed as -dot_M2/dot_M1 (zero if dot_M1=0),\n",
      "      'dot_Medd', Eddington accretion rate (Msun/yr),\n",
      "      'L_acc', accretion luminosity when accreting to a point mass (ergs/s),\n",
      "      'Jorb', orbital angular momentum (g*cm^2/s)\n",
      "      'spin1', spin angular momentum of star 1 (g*cm^2/s),\n",
      "      'spin2', spin angular momentum of star 2 (g*cm^2/s),\n",
      "      'dot_J', time derivative of Jorb (g*cm^2/s^2),\n",
      "      'dot_Jgr', time derivative of Jorb due to gravitational waves (g*cm^2/s^2),\n",
      "      'dot_Jml', time derivative of Jorb due to mass loss (g*cm^2/s^2),\n",
      "      'dot_Jmb', time derivative of Jorb due to magnetic braking (g*cm^2/s^2),\n",
      "      'dot_Jls', time derivative of Jorb due to spin-orbit coupling (g*cm^2/s^2),\n",
      "      'rlo_iters', number of iterations for implicit calculation of mass transfer,\n",
      "\n",
      " All this and more can be saved in binary_history.data during the run.\n",
      " num_steps_to_relax_rotation          50\n",
      "        relax to omega: wanted-current, current, wanted           1    7.2722052166430393D-06    0.0000000000000000D+00    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           2    7.1239371671217664D-06    1.4826804952127298D-07    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           3    6.9775031537422630D-06    2.9470206290077610D-07    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           4    6.8325228134160023D-06    4.3968240322703724D-07    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           5    6.6880335934326623D-06    5.8417162321037739D-07    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           6    6.5429720944820478D-06    7.2923312216099155D-07    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           7    6.3960414376057951D-06    8.7616377903724423D-07    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           8    6.2481185101766360D-06    1.0240867064664032D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted           9    6.0988144371784416D-06    1.1733907794645978D-06    7.2722052166430393D-06\n",
      "         10   7.649681  4.541E+04   5.083637   5.083787  30.000000  30.000000   0.746765   0.000508   0.251100   0.747141  -6.159624   2598      0\n",
      "   3.021836   0.768039   0.749893 -23.011309   3.935137 -99.000000   0.000000   0.251795   0.000441   0.001420   0.251427   0.009963      3      0\n",
      " 2.3543E+03  16.667399   5.081908   1.622938   4.361599  -8.707435   0.000000   0.000004   0.000133   0.000071  1.432E-03  0.000E+00  max increase\n",
      "\n",
      "        relax to omega: wanted-current, current, wanted          10    5.9494923842667907D-06    1.3227128323762491D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          11    5.8047902383525235D-06    1.4674149782905158D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          12    5.6656472219130140D-06    1.6065579947300258D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          13    5.5253850379006039D-06    1.7468201787424357D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          14    5.3813998139082310D-06    1.8908054027348084D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          15    5.2360332812183832D-06    2.0361719354246558D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          16    5.0905569484589031D-06    2.1816482681841367D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          17    4.9451036922867509D-06    2.3271015243562889D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          18    4.7996593421142631D-06    2.4725458745287766D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          19    4.6542152662843934D-06    2.6179899503586464D-06    7.2722052166430393D-06\n",
      "         20   7.648797  4.525E+04   5.069089   5.069239  30.000000  30.000000   0.746765   0.000508   0.251100   0.747141  -6.164975   2613      0\n",
      "   6.032136   0.764405   0.746596 -23.057888   3.920548 -99.000000   0.000000   0.251795   0.000441   0.001420   0.251427   0.009955      2      0\n",
      " 2.1539E+06  16.663052   5.069239   1.607921   4.359365  -8.704639   0.000000   0.000004   0.000133   0.000071  1.432E-03  0.000E+00  max increase\n",
      "\n",
      "        relax to omega: wanted-current, current, wanted          20    4.5087711728839026D-06    2.7634340437591372D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          21    4.3633267819036798D-06    2.9088784347393599D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          22    4.2178823225008446D-06    3.0543228941421948D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          23    4.0724377789053992D-06    3.1997674377376405D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          24    3.9269931252400189D-06    3.3452120914030209D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          25    3.7815483288686108D-06    3.4906568877744285D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          26    3.6361034654341649D-06    3.6361017512088744D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          27    3.4906587737615971D-06    3.7815464428814422D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          28    3.3452144110082463D-06    3.9269908056347930D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          29    3.1997702456217982D-06    4.0724349710212411D-06    7.2722052166430393D-06\n",
      "         30   7.648797  4.525E+04   5.069078   5.069228  30.000000  30.000000   0.746765   0.000508   0.251100   0.747141  -6.164960   2610      0\n",
      "   9.042435   0.764409   0.746606 -23.057923   3.920536 -99.000000   0.000000   0.251795   0.000441   0.001420   0.251427   0.009955      2      0\n",
      " 2.2053E+09  16.663054   5.069228   1.607910   4.359335  -8.704658   0.000000   0.000004   0.000133   0.000071  1.432E-03  0.000E+00  max increase\n",
      "\n",
      "        relax to omega: wanted-current, current, wanted          30    3.0543261335993711D-06    4.2178790830436683D-06    7.2722052166430393D-06\n",
      "        relax to omega: wanted-current, current, wanted          31    2.9088820289801618D-06    4.3633231876628776D-06    7.2722052166430393D-06\n"
     ]
    }
   ],
   "source": [
    "!head -n 200 Zbase_0.0014_m1_30.0000_m2_21.0000_initial_z_1.4200e-03_initial_period_in_days_1.0000e+01_grid_index_0/out.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Congratulation, you now know how to harvast the power of POSYDON to run a grid of simulations on a supercomputer!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "posydon_env",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}