Skip to content

Tutorial 1: using the TOV code

Author: Alexandra C. Semposki

Welcome to our tutorials! Here you can learn how to use our TOV solver for your own EOS codes, by using the classic SLy4 EOS as a test EOS. Below we start by importing the relevant functions and packages.

# import the relevant packages
import numpy as np 
import matplotlib.pyplot as plt 

# import the slmemulator package
from slmemulator import TOV
# run the help function to see what the TOVsolver does
help(TOV)
Help on class TOV in module slmemulator.TOV_class:

class TOV(builtins.object)
 |  TOV(
 |      eos_filepath=None,
 |      tidal=False,
 |      solver='RK4',
 |      solve_ivp_kwargs=None,
 |      sol_pts=4000
 |  )
 |
 |  Methods defined here:
 |
 |  RK2(self, f, x0, t0, te, N)
 |      A simple RK2 solver using the Heun's method.
 |      This is a low-fidelity solver.
 |
 |      Example:
 |          tov.RK2(f=func, x0=1., t0=1., te=10., N=100)
 |
 |      Parameters:
 |          f (func): A Python function for the ODE(s) to be solved.
 |              Able to solve N coupled ODEs.
 |
 |          x0 (float): Guess for the function(s) to be solved.
 |
 |          t0 (float): Initial point of the grid.
 |
 |          te (float): End point of the grid.
 |
 |          N (int): The number of steps to take in the range (te-t0).
 |
 |      Returns:
 |          times (array): The grid of solution steps.
 |
 |          solution (array): The solutions of each function
 |              at each point in the grid.
 |
 |  RK4(self, f, x0, t0, te, N)
 |      A simple RK4 solver to avoid overhead of
 |      calculating with solve_ivp or any other
 |      adaptive step-size function.
 |
 |      Example:
 |          tov.RK4(f=func, x0=1., t0=1., te=10., N=100)
 |
 |      Parameters:
 |          f (func): A Python function for the ODE(s) to be solved.
 |              Able to solve N coupled ODEs.
 |
 |          x0 (float): Guess for the function(s) to be solved.
 |
 |          t0 (float): Initial point of the grid.
 |
 |          te (float): End point of the grid.
 |
 |          N (int): The number of steps to take in the range (te-t0).
 |
 |      Returns:
 |          times (array): The grid of solution steps.
 |
 |          solution (array): The solutions of each function
 |              at each point in the grid.
 |
 |  __init__(
 |      self,
 |      eos_filepath=None,
 |      tidal=False,
 |      solver='RK4',
 |      solve_ivp_kwargs=None,
 |      sol_pts=4000
 |  )
 |      Class to calculate the Tolman-Oppenheimer-Volkoff equations,
 |      including options for the tidal deformability and moment of
 |      inertia using RK4. Also includes uncertainty quantification
 |      techniques through the highest posterior density interval (HPD
 |      or HDI) calculation. Able to accept one EOS from a single curve
 |      or draws from an EOS, such as from a Gaussian Process.
 |
 |      Example:
 |          tov = TOVSolver(eos_filepath='path/to/eos', tidal=True, moment=True)
 |
 |      Parameters:
 |          eos_filepath (str): The path to the EOS data table to be used.
 |
 |          tidal (bool): Whether to calculate tidal deformability or not.
 |              Default is False.
 |
 |          moment (bool): Whether to calculate moment of inertia or not.
 |              Default is False.
 |
 |      Returns:
 |          None.
 |
 |  canonical_NS_radius(self)
 |      Calculation of the radius of a 1.4 M_sol neutron star.
 |
 |      Example:
 |          tov.canonical_NS_radius()
 |
 |      Parameters:
 |          None.
 |
 |      Returns:
 |          rad_14 (array): The array of values of the radius
 |              for each EOS used.
 |
 |  central_dens(self, pres_arr=None)
 |      Calculation to determine the central density of the star
 |      at the maximum mass and radius determined from the tov_routine().
 |
 |      Example:
 |          tov.central_dens()
 |
 |      Parameters:
 |          pres_arr (array): An optional pressure array to use for
 |              calculating central densities at places other
 |              than the absolute TOV maximum mass of each curve.
 |              Default is None, and code will use absolute TOV
 |              maximum mass central pressure class array.
 |
 |      Returns:
 |          c_dens (array): The array of central densities for
 |              each EOS used.
 |
 |  euler(self, f, x0, t0, te, N)
 |      A simple forward euler solver to avoid overhead of
 |      calculating with solve_ivp or any other
 |      adaptive step-size function.
 |      This is a low fidelity solver!
 |
 |      Example:
 |          tov.euler(f=func, x0=1., t0=1., te=10., N=100)
 |
 |      Parameters:
 |          f (func): A Python function for the ODE(s) to be solved.
 |              Able to solve N coupled ODEs.
 |
 |          x0 (float): Guess for the function(s) to be solved.
 |
 |          t0 (float): Initial point of the grid.
 |
 |          te (float): End point of the grid.
 |
 |          N (int): The number of steps to take in the range (te-t0).
 |
 |      Returns:
 |          times (array): The grid of solution steps.
 |
 |          solution (array): The solutions of each function
 |              at each point in the grid.
 |
 |  f_x(self, x, mass, pres, eps)
 |      A function in the tidal deformability calculation.
 |
 |      Example:
 |          tov.f_x(x=0.2, mass=1.06, pres=2.34, eps=6.0)
 |
 |      Parameters:
 |          x (float): The current gridpoint in scaled radius.
 |
 |          mass (float): The current mass.
 |
 |          pres (float): The current pressure from the EOS.
 |
 |          eps (float): The current energy density from the EOS.
 |
 |      Returns:
 |          The value of F(x) at the current radius.
 |
 |  max_arrays(self)
 |      Returns the max arrays needed for the interval calculation.
 |
 |      Parameters:
 |          None.
 |
 |      Returns:
 |          self.max_radius_arr (array): Maximum radius array.
 |          self.max_pres_arr (array): Maximum central pressure array.
 |          self.max_mass_arr (array): Maximum mass array.
 |
 |  q_x(self, x, mass, pres, eps, cs2)
 |      A function in the calculation of the tidal deformability.
 |
 |      Example:
 |          tov.q_x(x=0.1, mass=2.0, pres=1.0, eps=3.0, cs2=0.33)
 |
 |      Parameters:
 |          x (float): The current gridpoint in scaled radius.
 |
 |          mass (float): The current mass.
 |
 |          pres (float): The current pressure from the EOS.
 |
 |          eps (float): The current energy density from the EOS.
 |
 |          cs2 (float): The current speed of sound from the EOS.
 |
 |      Returns:
 |          The value of Q(x) at the current radius.
 |
 |  tidal_def(self, yR, mass, radius)
 |      The calculation of the tidal deformability, Lambda, and
 |      the tidal Love number, k2. This function is calculated after
 |      the RK4 routine has been completed.
 |
 |      Example:
 |          tov.tidal_def(yR=np.array, mass=np.array, radius=np.array)
 |
 |      Parameters:
 |          yR (float): The array of y at the maximum radii points.
 |
 |          mass (float): The array of mass at the maximum radii.
 |
 |          radius (float): The maximum radii array.
 |
 |      Returns:
 |          tidal_deform (array): The tidal deformability solved at
 |              each point in the maximum radius.
 |
 |          k2 (array): The value of the Love number calculated at the
 |              compactness M/R and the value of y at maximum radius.
 |
 |  tov_equations_scaled(self, x, y0)
 |      The Tolman-Oppenheimer-Volkoff equations in scaled format, to be
 |      solved with the RK4 routine. If selected, the tidal deformability
 |      and moment of inertia will be included and solved
 |      simultaneously.
 |
 |      Example:
 |          tov.tov_equations_scaled(x=0.2,
 |              y0=[m_init, p_init])
 |
 |      Parameters:
 |          x (float): A point in the scaled radius grid.
 |
 |          y0 (list): The list of initial guesses for each function
 |              solved.
 |
 |      Returns:
 |          The solutions, in array format, of each function to be
 |              solved.
 |
 |  tov_routine(self, verbose=False, write_to_file=False)
 |      The TOV routine to solve each set of coupled ODEs and to output
 |      the quantities needed to display the M-R curve, as well as the
 |      tidal deformability and moment of inertia if desired.
 |
 |      Example:
 |          tov.tov_routine(verbose=True, write_to_file=True)
 |
 |      Parameters:
 |          verbose (bool): Whether to plot quantities and display
 |              the full maximum mass array. Default is False.
 |
 |          write_to_file (bool): Choice to write the TOV results to
 |              a file located in a folder of the user's choice.
 |              Default is False.
 |
 |      Returns:
 |          self.total_radius (array): The array of total maximum
 |              radius values.
 |
 |          self.total_pres_central (array): The array of total
 |              central pressure values.
 |
 |          self.total_max_mass (array): The array of total
 |              maximum mass values.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object


As you can see above, this class has multiple high-fidelity solvers implemented. The best one (so far) is the RK4 solver. You can supply the name of it in the solver argument of the class.

Using TOV solver

To use the TOV solver, one can use the EOSs provided in the EOS_Data folder or generate their own EOS. In this example we will use APR EOS.

from slmemulator.config import get_paths
paths = get_paths()
eos_data_dir = paths['eos_data_dir']

# Create an instance of the TOV solver
tov = TOV(eos_data_dir / 'apr_eos.table',tidal=True)
results = tov.tov_routine()
# plot the results
plt.figure(figsize=(10, 6))
plt.plot(results[0], results[2], label='HF')
plt.xlabel('Radius (km)')
plt.ylabel('Mass (M_sun)')
plt.title('TOV Solution')
plt.legend()
# plt.grid()
plt.show()
Woo it worked!
Max mass:  2.192818228236291 Radius:  9.967329639499997 Central pressure:  1032.2286804996406

No description has been provided for this image

Plot tidal deformability: k_2

# plot the results
plt.figure(figsize=(10, 6))
plt.plot(results[0], results[3], label='HF')
plt.xlabel('Radius (km)')
plt.ylabel(r'$k_2$')
plt.title('TOV Solution')
plt.legend()
# plt.grid()
plt.show()
No description has been provided for this image

Tabular Equations of state

We provide a list of tabular equations of state, some of which have been obtained from the CompOSE database. They can be listed as below:

from slmemulator.config import get_paths
import os
paths = get_paths()
eos_data_dir = paths['eos_data_dir']
os.listdir(eos_data_dir)
['BFH_eos.table',
 'BL_eos.table',
 'DS_CMF_eos.table',
 'EOS_Quarkyonia.dat',
 'FSUGarnetNStarEOSA.txt',
 'Gibbs_EOS.dat',
 'Gibbs_MS_Bag_MFT.dat',
 'MFT_ns6p.dat',
 'MS_EOS.dat',
 'MS_fullEOS.dat',
 'Max_EOS.dat',
 'NJL_EOS.dat',
 'Polytrope_1.0_gamma.dat',
 'Polytrope_K_1.0_Gam_1.5.dat',
 'apr_eos.table',
 'delta_eos.table',
 'eos_tov_20n0.dat',
 'eos_tov_40n0.dat',
 'sortedDD2YDelta.dat',
 'sortedDSeos.dat',
 'sorted_Sly4.dat',
 'EOS_Quarkyonia_0.12_380.00.dat',
 'EOS_Quarkyonia_0.20_380.00.dat',
 'EOS_Quarkyonia_0.30_380.00.dat']