Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolate yields using SciPy #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions src/intergalactic/elements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from bisect import bisect
import numpy as np
from scipy.interpolate import interp1d


class Expelled:
Expand All @@ -11,7 +13,9 @@ class Expelled:

def __init__(self, expelled_elements_filename="expelled_elements"):
self.mass_points = []
self.data_rows = []
self.by_mass = {}
self.interpolation_function = None
self.read_expelled_elements_file(expelled_elements_filename)

def read_expelled_elements_file(self, filename):
Expand All @@ -35,30 +39,19 @@ def read_expelled_elements_file(self, filename):
if data_row:
mass = data_row.pop(0) # the first column is the mass
self.mass_points.append(mass)
self.data_rows.append(data_row)
self.by_mass[mass] = dict(zip(self.elements_list, data_row))

expelled_data.close()
self.interpolation_function = interp1d(
np.array(self.mass_points),
np.matrix.transpose(np.array(self.data_rows)),
fill_value="extrapolate")

def for_mass(self, m):
"""
Interpolates expelled mass (per solar mass) for all elements for a given
stellar mass, using the data from the class' expelled_elements input file.

"""

index = bisect(self.mass_points, m)
if index == len(self.mass_points):
index -= 1

mass_prev = self.mass_points[index - 1]
mass_next = self.mass_points[index]
elements_prev = self.by_mass[mass_prev]
elements_next = self.by_mass[mass_next]
interpolations = {"mass": m}
p = (mass_next - m) / (mass_next - mass_prev)

for element in self.elements_list:
d = elements_next[element] - elements_prev[element]
interpolations[element] = (elements_next[element] - (p * d)) / m

return interpolations
return dict(zip(self.elements_list, self.interpolation_function(m) / m))