From 3ee358cad14345820866edba1d3a44c14a33ea05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 30 Sep 2020 11:33:31 +0200 Subject: [PATCH 1/2] use scipy interp1d to interpolate yields --- src/intergalactic/elements.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/intergalactic/elements.py b/src/intergalactic/elements.py index e6e429f..2f0cbec 100644 --- a/src/intergalactic/elements.py +++ b/src/intergalactic/elements.py @@ -1,4 +1,6 @@ from bisect import bisect +import numpy as np +from scipy.interpolate import interp1d class Expelled: @@ -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): @@ -35,9 +39,14 @@ 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): """ @@ -45,7 +54,14 @@ def for_mass(self, m): stellar mass, using the data from the class' expelled_elements input file. """ + return dict(zip(self.elements_list, self.interpolation_function(m) / m)) + def for_mass_old(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 From 6c1ea4a03074f78b8ab75d96b0fbf77b9cf364d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Wed, 30 Sep 2020 11:34:23 +0200 Subject: [PATCH 2/2] remove old interpolation function --- src/intergalactic/elements.py | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/src/intergalactic/elements.py b/src/intergalactic/elements.py index 2f0cbec..e2f941b 100644 --- a/src/intergalactic/elements.py +++ b/src/intergalactic/elements.py @@ -55,26 +55,3 @@ def for_mass(self, m): """ return dict(zip(self.elements_list, self.interpolation_function(m) / m)) - - def for_mass_old(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