#!/usr/bin/env python3
# ########################################################################
# This file is part of PYSWIFT.
# Copyright (c) 2019 Loic Hausammann (loic.hausammann@epfl.ch)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ########################################################################

from pyswiftsim import libstellar
import pyswiftsim

import numpy as np
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)


N = 100
solMass_in_cgs = 1.989e33


if __name__ == "__main__":

    with pyswiftsim.ParameterFile() as filename:

        parser = pyswiftsim.parseYamlFile(filename)
        imf = parser["GEARInitialMassFunction"]
        mass_limits = imf["mass_limits_msun"]
        mass_min = float(mass_limits[0])
        mass_max = float(mass_limits[1])

        mass = np.linspace(mass_min, mass_max, N, dtype=np.float32)

        units = parser["InternalUnitSystem"]
        unit_mass = units["UnitMass_in_cgs"]
        # change the units
        mass *= solMass_in_cgs / unit_mass

        print("Computing initial mass function...")

        imf = libstellar.initialMassFunction(filename, mass)

        imf *= solMass_in_cgs / unit_mass

        print("IMF obtained: {}".format(imf))