Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Python-Programs-for-Nonlinear-Dynamics/SIRWave.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
164 lines (128 sloc)
3.68 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat March 21 2020 | |
@author: nolte | |
D. D. Nolte, Introduction to Modern Dynamics: Chaos, Networks, Space and Time, 2nd ed. (Oxford,2019) | |
""" | |
import numpy as np | |
from scipy import integrate | |
from matplotlib import pyplot as plt | |
plt.close('all') | |
print(' ') | |
print('SIR.py') | |
def solve_flow(param,max_time=1000.0): | |
def flow_deriv(x_y_z_w,tspan): | |
In, Sn, Iq, Sq = x_y_z_w | |
Inp = -mu*In + beta*knn*In*Sn + beta*knq*Iq*Sn | |
Snp = -beta*knn*In*Sn - beta*knq*Iq*Sn | |
Iqp = -mu*Iq + beta*kqn*In*Sq + beta*kqq*Iq*Sq | |
Sqp = -beta*kqn*In*Sq - beta*kqq*Iq*Sq | |
return [Inp, Snp, Iqp, Sqp] | |
x0 = [In0, Sn0, Iq0, Sq0] | |
# Solve for the trajectories | |
t = np.linspace(tlo, thi, thi-tlo) | |
x_t = integrate.odeint(flow_deriv, x0, t) | |
return t, x_t | |
beta = 0.03 # infection rate | |
dill = 6 # mean days infectious | |
mu = 1/dill # decay rate | |
fnq = 0.2 # fraction not quarantining | |
fq = 1-fnq # fraction quarantining | |
P = 330 # Population of the US in millions | |
mr = 0.001 # Mortality rate | |
dq = 90 # Days of lock-down (this is the key parameter) | |
# During quarantine | |
knn = 50 # Average connections per day for non-compliant group among themselves | |
kqq = 0 # Connections among compliant group | |
knq = 0 # Effect of compliaht group on non-compliant | |
kqn = 5 # Effect of non-clmpliant group on compliant | |
initfrac = 0.0001 # Initial conditions: | |
In0 = initfrac*fnq # infected non-compliant | |
Sn0 = (1-initfrac)*fnq # susceptible non-compliant | |
Iq0 = initfrac*fq # infected compliant | |
Sq0 = (1-initfrac)*fq # susceptivle compliant | |
tlo = 0 | |
thi = dq | |
param = (mu, beta, knn, knq, kqn, kqq) # flow parameters | |
t1, y1 = solve_flow(param) | |
In1 = y1[:,0] | |
Sn1 = y1[:,1] | |
Rn1 = fnq - In1 - Sn1 | |
Iq1 = y1[:,2] | |
Sq1 = y1[:,3] | |
Rq1 = fq - Iq1 - Sq1 | |
# Lift the quarantine | |
knn = 50 | |
kqq = 5 | |
knq = 20 | |
kqn = 15 | |
fin1 = len(t1) | |
In0 = In1[fin1-1] | |
Sn0 = Sn1[fin1-1] | |
Iq0 = Iq1[fin1-1] | |
Sq0 = Sq1[fin1-1] | |
tlo = fin1 | |
thi = fin1 + 365-dq | |
param = (mu, beta, knn, knq, kqn, kqq) | |
t2, y2 = solve_flow(param) | |
In2 = y2[:,0] | |
Sn2 = y2[:,1] | |
Rn2 = fnq - In2 - Sn2 | |
Iq2 = y2[:,2] | |
Sq2 = y2[:,3] | |
Rq2 = fq - Iq2 - Sq2 | |
fin2 = len(t2) | |
t = np.zeros(shape=(fin1+fin2,)) | |
In = np.zeros(shape=(fin1+fin2,)) | |
Sn = np.zeros(shape=(fin1+fin2,)) | |
Rn = np.zeros(shape=(fin1+fin2,)) | |
Iq = np.zeros(shape=(fin1+fin2,)) | |
Sq = np.zeros(shape=(fin1+fin2,)) | |
Rq = np.zeros(shape=(fin1+fin2,)) | |
t[0:fin1] = t1 | |
In[0:fin1] = In1 | |
Sn[0:fin1] = Sn1 | |
Rn[0:fin1] = Rn1 | |
Iq[0:fin1] = Iq1 | |
Sq[0:fin1] = Sq1 | |
Rq[0:fin1] = Rq1 | |
t[fin1:fin1+fin2] = t2 | |
In[fin1:fin1+fin2] = In2 | |
Sn[fin1:fin1+fin2] = Sn2 | |
Rn[fin1:fin1+fin2] = Rn2 | |
Iq[fin1:fin1+fin2] = Iq2 | |
Sq[fin1:fin1+fin2] = Sq2 | |
Rq[fin1:fin1+fin2] = Rq2 | |
plt.figure(1) | |
lines = plt.semilogy(t,In,t,Iq,t,(In+Iq)) | |
plt.ylim([0.0001,.1]) | |
plt.xlim([0,thi]) | |
plt.legend(('Non-compliant','Compliant','Total')) | |
#plt.setp(lines, linewidth=0.5) | |
plt.xlabel('Days') | |
plt.ylabel('Infected (Millions)') | |
plt.title('Infection Dynamics for COVID-19 in US') | |
plt.show() | |
plt.figure(2) | |
lines = plt.semilogy(t,Rn*P*mr,t,Rq*P*mr) | |
plt.ylim([0.001,1]) | |
plt.xlim([0,thi]) | |
plt.legend(('Non-compliant','Compliant')) | |
plt.setp(lines, linewidth=0.5) | |
plt.xlabel('Days') | |
plt.ylabel('Deaths (Millions)') | |
plt.title('Total Deaths for COVID-19 in US') | |
plt.show() | |
D = P*mr*(Rn[fin1+fin2-1] + Rq[fin1+fin2-1]) | |
print('Deaths = ',D) | |
plt.figure(3) | |
lines = plt.semilogy(t,In/fnq,t,Iq/fq) | |
plt.ylim([0.0001,.1]) | |
plt.xlim([0,thi]) | |
plt.legend(('Non-compliant','Compliant')) | |
plt.setp(lines, linewidth=0.5) | |
plt.xlabel('Days') | |
plt.ylabel('Fraction of Sub-Population') | |
plt.title('Population Dynamics for COVID-19 in US') | |
plt.show() | |