def sample(C_si, C_rel): # Imports import sir_ddft import numpy as np from numpy import pi, sqrt from dispy.dispynode import dispynode_logger # Constants NUM_THREADS = 2 DT = 0.1 MIN_TIME = 10 TIMEOUT = 400 THRESHOLD = 1e-4 # Use model parameters from original publication C_sd = C_si / C_rel L = 10 N = 512 # Powers of 2 are favorable for the internal FFT dx = L / N params = sir_ddft.SIRParameters(1.0, 0.1) diff_params = sir_ddft.SIRDiffusionParameters(0.01, 0.01, 0.01) ddft_params = sir_ddft.SIRDDFTParameters(1.0, 1.0, 1.0, C_sd, 100, C_si, 100) x = np.linspace(0, L, N, endpoint=False) + dx / 2 x, y = np.meshgrid(x, x) S = lambda x, y: np.exp(-1 / (L * 2.0 * L / 50.0) * ((x - L / 2)**2 + (y - L / 2)**2)) norm_fac = N**2 / np.sum(S(x, y)) * sqrt(pi) / 5 S = lambda x, y, S=S: S(x, y) * norm_fac I = lambda x, y: 0.001 * S(x, y) total_SIR = np.sum(S(x, y) + I(x, y)) * dx * dx # Total population # Create initial state and solver grid = sir_ddft.Grid2D.new_equidistant(0, L, 0, L, N, N) state = sir_ddft.SIRStateSpatial2D( grid, lambda x, y: [S(x, y), I(x, y), 0.0]) solver = sir_ddft.SIRDDFT2DSolver(params, diff_params, ddft_params, state, NUM_THREADS) t = [] S = [] I = [] R = [] def store_result(result): t.append(result["time"]) S.append(np.sum(result["S"]) * dx * dx) I.append(np.sum(result["I"]) * dx * dx) R.append(np.sum(result["R"]) * dx * dx) store_result(solver.get_result()) while t[-1] < MIN_TIME or (I[-1] / total_SIR > THRESHOLD and t[-1] < TIMEOUT): solver.add_time(DT) solver.integrate() store_result(solver.get_result()) dispynode_logger.info(t[-1]) return (t, S, I, R)
import numpy as np from numpy import pi, sqrt import os, os.path import sys sys.path.append(os.path.abspath(os.path.join(__file__, "../../../target/release"))) import sir_ddft from common_2d import run_sim L=10 N=512 params = sir_ddft.SIRParameters(1.0,0.1) diff_params = sir_ddft.SIRDiffusionParameters(0.01,0.01,0.01) ddft_params = sir_ddft.SIRDDFTParameters(1.0,1.0,1.0,-10,100,-30,100) grid = sir_ddft.Grid2D.new_equidistant(0,L,0,L,N,N) S = lambda x,y: np.exp(-1/(L*2.0*L/50.0)*((x-L/2)**2 + (y-L/2)**2)) * 2.832 I = lambda x,y: 0.001*S(x,y) state = sir_ddft.SIRStateSpatial2D(grid, lambda x,y: [S(x,y),I(x,y),0.0]) solver = sir_ddft.SIRDDFT2DSolver(params, diff_params, ddft_params, state, 6) run_sim(solver, 0.1, 200, "SIR DDFT model")