Merge branch 'pelson/improved_concentration_calculation' into 'master'

Simplify (and fix) the concentration calculation.

See merge request cara/cara!7
This commit is contained in:
Philip James Elson 2020-10-26 19:25:41 +00:00
commit 55cf9820fa
2 changed files with 12 additions and 5 deletions

View file

@ -266,6 +266,13 @@ class Model:
return k + self.virus.decay_constant + self.ventilation.air_exchange(self.room, time)
def collect_time_state_changes(self):
"""
All time dependent entities on this model must provide information about
the times at which their state changes.
"""
@functools.lru_cache()
def concentration(self, time: float) -> float:
t = time
@ -280,10 +287,10 @@ class Model:
elif t0 < t <= t1:
# Concentration while infected present.
init_concentration = self.concentration(t0)
time_present = t - t0
fac = np.exp(-IVRR * time_present)
return ((ER + Ni) / (IVRR * V)) * (1 - fac) + init_concentration * fac
fac = np.exp(IVRR * (t0 - t))
return ((ER * Ni) / (IVRR * V)) * (1 - fac) + init_concentration * fac
else:
# Concentration while infected not present.
end_concentration = self.concentration(t1)
return (end_concentration + ((np.exp(IVRR * t1) - 1) * end_concentration)) * np.exp(-IVRR * t)
fac = np.exp(IVRR * (t1 - t))
return end_concentration * fac

View file

@ -64,7 +64,7 @@ def test_r0(baseline_model):
concentrations = [baseline_model.concentration(t) for t in ts]
npt.assert_allclose(
concentrations,
[0.000000e+00, 2.909211e-01, 1.273836e-04, 2.909210e-01, 5.577662e-08],
[0.000000e+00, 2.891970e-01, 1.266287e-04, 2.891969e-01, 5.544607e-08],
rtol=1e-5
)