Skip to content

Commit

Permalink
Add tolerance and avoid log(0) warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoya committed Oct 7, 2024
1 parent c9a9eb0 commit 6a64e99
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 26 deletions.
9 changes: 4 additions & 5 deletions src/pst/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def wrapper_stellar_mass_formed(*args):
quenching_time = getattr(args[0], "quenching_time", 20.0 << u.Gyr)
stellar_mass = stellar_mass_formed(*args)
final_mass = stellar_mass_formed(args[0], quenching_time)
stellar_mass[args[1] > quenching_time] = final_mass
return stellar_mass
return np.where(args[1] < quenching_time, stellar_mass, final_mass)
return wrapper_stellar_mass_formed


Expand Down Expand Up @@ -407,9 +406,9 @@ def __init__(self, **kwargs):

@u.quantity_input
def stellar_mass_formed(self, times: u.Quantity):
z = - np.log(times / self.t0) / self.scale
m = 0.5 * (1 - special.erf(z / SQRT_2))
m[times <= 0] = 0
z = - np.log(times[times > 0] / self.t0) / self.scale
m = np.zeros(times.shape)
m[times > 0] = 0.5 * (1 - special.erf(z / SQRT_2))
return m * self.mass_norm

@u.quantity_input
Expand Down
2 changes: 1 addition & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_lognormal_zpowerlaw(self):
self.assertEqual(mass[0], 0.0)
self.assertTrue(np.isclose(mass[-1], 1.0 * u.Msun, rtol=1e-4))
self.assertEqual(metals[0], 0.0)
#self.assertTrue(np.isclose(metals[-1], 0.02))
self.assertTrue(np.isclose(metals[-1], 0.02, rtol=1e-4))

def test_tabular(self):
low_res_time = np.linspace(0, 13.7, 10) * u.Gyr
Expand Down
20 changes: 0 additions & 20 deletions tutorials/models/analytical.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,6 @@
"ssp_weights = model.interpolate_ssp_masses(ssp, t_obs=today)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d91db156-54a7-4ef2-8a0f-9316bc8e2322",
"metadata": {},
"outputs": [],
"source": [
"model.ism_metallicity(2*u.Gyr)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -260,16 +250,6 @@
"ax.plot(today-cosmic_time, model.ism_metallicity(cosmic_time), 'k-', alpha=.5)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d035a95-dcae-4a49-bb37-2c172dfe6f06",
"metadata": {},
"outputs": [],
"source": [
"np.nanmax(ssp_weights)"
]
},
{
"cell_type": "markdown",
"id": "f1e6080b",
Expand Down

0 comments on commit 6a64e99

Please sign in to comment.