Skip to content

Commit

Permalink
Fixed overflow due to dt being too high
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankTiger committed Jun 6, 2022
1 parent b32dd4f commit c3e06e6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 25 deletions.
34 changes: 13 additions & 21 deletions SEIRD_math_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ fn seird_se(_t: f64, y: &Array1<f64>, c: &SeirdArgs) -> Array1<f64> {
let ratio = y[0] / (y[0] + y[1]);
let vac_s: f64;
let vac_e: f64;
let mut corr = 0.0;

if diff_v != 0.0 && ratio < 1.0 {
if diff_v != 0.0 && ratio <= 1.0 {
vac_s = diff_v * ratio;
vac_e = diff_v * (1.0 - ratio);
} else {
Expand All @@ -50,17 +49,8 @@ fn seird_se(_t: f64, y: &Array1<f64>, c: &SeirdArgs) -> Array1<f64> {
diff_v = 0.0;
}

if y[0] - c.beta * c.sigma * y[0] * c.sum_contact - vac_s < 0.0 {
corr = y[0];
}

dydt[0] = -c.beta * c.sigma * y[0] * c.sum_contact - vac_s - corr;

if y[1] * c.epsilon + vac_e > y[1] + c.beta * c.sigma * y[0] * c.sum_contact {
corr = y[1];
}

dydt[1] = c.beta * c.sigma * y[0] * c.sum_contact - c.epsilon * y[1] - vac_e - corr;
dydt[0] = -c.beta * c.sigma * y[0] * c.sum_contact - vac_s;
dydt[1] = c.beta * c.sigma * y[0] * c.sum_contact - c.epsilon * y[1] - vac_e;
dydt[2] = c.epsilon * c.f_s * y[1] - (c.gamma_s + c.delta) * y[2];
dydt[3] = c.epsilon * (1. - c.f_s) * y[1] - c.gamma_a * y[3];
dydt[4] = c.gamma_s * y[2] + c.gamma_a * y[3] + diff_v;
Expand Down Expand Up @@ -148,9 +138,10 @@ fn seird_math(_py: Python, m: &PyModule) -> PyResult<()> {
let vac_param: f64;

if vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][1]
<= time_points[t] as f64
&& time_points[t] as f64
<= vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][2]
<= time_points[t]
&& time_points[t]
< vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][2]
+ 1.0
{
vac_param = vac_params["eff"][0]
* vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][0];
Expand All @@ -167,7 +158,7 @@ fn seird_math(_py: Python, m: &PyModule) -> PyResult<()> {
gamma_a: gamma_a[i],
delta: delta[i],
sum_contact: sum_contact,
vac_params: vac_param * dt,
vac_params: vac_param,
};

let ct = time_points[t];
Expand All @@ -194,9 +185,10 @@ fn seird_math(_py: Python, m: &PyModule) -> PyResult<()> {
let vac_param: f64;

if vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][1]
<= time_points[t] as f64
&& time_points[t] as f64
<= vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][2]
<= time_points[t]
&& time_points[t]
< vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][2]
+ 1.0
{
vac_param = vac_params["eff"][0]
* vac_params[&("age_grp_".to_string() + &(i + 1).to_string())][0];
Expand All @@ -213,7 +205,7 @@ fn seird_math(_py: Python, m: &PyModule) -> PyResult<()> {
gamma_a: gamma_a[i],
delta: delta[i],
sum_contact: sum_contact,
vac_params: vac_param * dt,
vac_params: vac_param,
};

let ct = time_points[t];
Expand Down
2 changes: 1 addition & 1 deletion model.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ t, y = seird_math.solve_seird(
sweden_contact_matrix,
dict(),
False,
1,
0.1,
)

# Create a figure from the solution
Expand Down
2 changes: 1 addition & 1 deletion utils/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create_updated_fig_SEIRD(
coeff = params["-PARAMTAB-"]
contact = params["-CONTACTTAB-"]
t, y = seird_math.solve_seird(
(0, t_1), y0.astype(np.float64), coeff, contact, params_vac, vac_E, 1
(0, t_1), y0.astype(np.float64), coeff, contact, params_vac, vac_E, 0.1
)

fig = plots.plot_SEIRD(t, y, screen_size)
Expand Down
1 change: 0 additions & 1 deletion utils/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def layout(screen_size):
settings.vac_font = (r"Helvetica", 8)
settings.stat_font = (r"Helvetica", 9)
settings.vac_scrollable = True
print(settings.canvas_size)

duration_text = sg.Text("Duration in days:", size=settings.text_size, expand_x=True)
duration_input = sg.InputText(
Expand Down
2 changes: 1 addition & 1 deletion utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def plot_SEIRD(t, y, screen_size):
ax[i, j].set_ylabel("Population")

plt.text(
0.7,
0.13,
0.97,
"© M. Urban, J. Jodłowska, J. Balbus, K. Kubica",
fontsize=10,
Expand Down

0 comments on commit c3e06e6

Please sign in to comment.