def demo_SIR(): """Test case using an SIR model""" def f(u, t): S, I, R = u return [-beta * S * I, beta * S * I - gamma * I, gamma * I] beta = 10. / (40 * 8 * 24) gamma = 3. / (15 * 24) # 48 h dt = 48.0 # Simulate for D days D = 30 # Corresponding no of hours N_t = int(D * 24 / dt) # End time T = dt * N_t U_0 = [50, 1, 0] u_old, t_old = ode_FE(f, U_0, dt, T) S_old = u_old[:, 0] I_old = u_old[:, 1] R_old = u_old[:, 2] k = 1 one_more = True while one_more == True: dt_k = 2**(-k) * dt u_new, t_new = ode_FE(f, U_0, dt_k, T) S_new = u_new[:, 0] I_new = u_new[:, 1] R_new = u_new[:, 2] plt.plot(t_old, S_old, 'b-', t_new, S_new, 'b--',\ t_old, I_old, 'r-', t_new, I_new, 'r--',\ t_old, R_old, 'g-', t_new, R_new, 'g--') plt.xlabel('hours') plt.ylabel('S (blue), I (red), R (green)') #plt.savefig("SIR_dt_" + str(dt_k) + ".png") plt.title("dt = " + str(dt_k)) pp.savefig() plt.clf() print("Finest timestep was: ", dt_k) answer = input('Do one more with finer dt (y/n)? ') if answer == 'y': S_old = S_new.copy() R_old = R_new.copy() I_old = I_new.copy() t_old = t_new.copy() k += 1 else: one_more = False
def test_diffusion_exact_linear(): global L, beta, dx, L = 1.5 beta = 0.5 N = 40 x = linspace(0, L, N+1) dx = x[1] - x[0] u = zeros(N+1) U_0 = zeros(N+1) U_0[0] = s(0) U_0[1:] = u_exact(x[1:], 0) dt = dx**2/(2*beta) print 'stability limit:', dt u, t = ode_FE(rhs, U_0, dt, T=1.2)
def test_diffusion_exact_linear(): global beta, dx, L, x # needed in rhs L = 1.5 beta = 0.5 N = 4 x = linspace(0, L, N+1) dx = x[1] - x[0] u = zeros(N+1) U_0 = zeros(N+1) U_0[0] = s(0) U_0[1:] = u_exact(x[1:], 0) dt = 0.1 print dt u, t = ode_FE(rhs, U_0, dt, T=1.2) tol = 1E-12 for i in range(0, u.shape[0]): diff = abs(u_exact(x, t[i]) - u[i,:]).max() assert diff < tol, 'diff=%.16g' % diff print 'diff=%g at t=%g' % (diff, t[i])
def test_diffusion_exact_linear(): global beta, dx, L, x # needed in rhs L = 1.5 beta = 0.5 N = 4 x = np.linspace(0, L, N+1) dx = x[1] - x[0] u = np.zeros(N+1) U_0 = np.zeros(N+1) U_0[0] = s(0) U_0[1:] = u_exact(x[1:], 0) dt = 0.1 print(dt) u, t = ode_FE(rhs, U_0, dt, T=1.2) tol = 1E-12 for i in range(0, u.shape[0]): diff = np.abs(u_exact(x, t[i]) - u[i,:]).max() assert diff < tol, 'diff={:.16g}'.format(diff) print('diff={:g} at t={:g}'.format(diff, t[i]))
L = 0.5 beta = 8.2E-5 N = 40 x = np.linspace(0, L, N + 1) dx = x[1] - x[0] u = np.zeros(N + 1) U_0 = np.zeros(N + 1) U_0[0] = s(0) U_0[1:] = 283 dt = dx**2 / (2 * beta) print('stability limit:', dt) #dt = 0.00034375 from ode_system_FE import ode_FE u, t = ode_FE(rhs, U_0, dt, T=1 * 60 * 60) plt.ion() y = u[0, :] lines = plt.plot(x, y) plt.axis([x[0], x[-1], 273, s(0) + 10]) plt.xlabel('x') plt.ylabel('u(x,t)') counter = 0 # Plot each of the first 100 frames, then increase speed by 10x change_speed = 100 for i in range(0, u.shape[0]): print(t[i]) plot = True if i <= change_speed else i % 10 == 0 lines[0].set_ydata(u[i, :]) if i > change_speed:
L = 1 beta = 1 N = 40 x = linspace(0, L, N+1) dx = x[1] - x[0] u = zeros(N+1) U_0 = zeros(N+1) U_0[0] = s(0) U_0[1:] = 0 dt = dx**2/(2*beta) print 'stability limit:', dt t0 = time.clock() from ode_system_FE import ode_FE u, t = ode_FE(rhs, U_0, dt, T=1.2) t1 = time.clock() print 'CPU time: %.1fs' % (t1 - t0) # Make movie import os os.system('rm tmp_*.png') import matplotlib.pyplot as plt plt.ion() y = u[0,:] lines = plt.plot(x, y) plt.axis([x[0], x[-1], -0.1, 1.2*s(0)]) plt.xlabel('x') plt.ylabel('u(x,t)') counter = 0 # Plot each of the first 100 frames, then increase speed by 10x
beta = 8.2E-5 N = 40 x = linspace(0, L, N+1) dx = x[1] - x[0] u = zeros(N+1) U_0 = zeros(N+1) U_0[0] = s(0) U_0[1:] = 283 dt = dx**2/(2*beta) print 'stability limit:', dt #dt = 0.00034375 t0 = time.clock() from ode_system_FE import ode_FE u, t = ode_FE(rhs, U_0, dt, T=1*60*60) t1 = time.clock() print 'CPU time: %.1fs' % (t1 - t0) # Make movie import os os.system('rm tmp_*.png') import matplotlib.pyplot as plt plt.ion() y = u[0,:] lines = plt.plot(x, y) plt.axis([x[0], x[-1], 273, s(0)+10]) plt.xlabel('x') plt.ylabel('u(x,t)') counter = 0 # Plot each of the first 100 frames, then increase speed by 10x