2.24. Custom PDE class: SIR model

This example implements a spatially coupled SIR model with the following dynamics for the density of susceptible, infected, and recovered individuals:

\[\begin{split}\partial_t s &= D \nabla^2 s - \beta is \\ \partial_t i &= D \nabla^2 i + \beta is - \gamma i \\ \partial_t r &= D \nabla^2 r + \gamma i\end{split}\]

Here, \(D\) is the diffusivity, \(\beta\) the infection rate, and \(\gamma\) the recovery rate.

Time: 50, Susceptible, Infected, Recovered

Out:

  0%|          | 0/50.0 [00:00<?, ?it/s]
Initializing:   0%|          | 0/50.0 [00:00<?, ?it/s]
  0%|          | 0/50.0 [00:00<?, ?it/s]
  0%|          | 0.02/50.0 [00:03<2:28:08, 177.84s/it]
  0%|          | 0.03/50.0 [00:03<1:38:46, 118.60s/it]
  1%|          | 0.3/50.0 [00:03<09:52, 11.93s/it]
  4%|4         | 2.2/50.0 [00:03<01:20,  1.69s/it]
 15%|#4        | 7.37/50.0 [00:04<00:23,  1.81it/s]
 32%|###1      | 15.99/50.0 [00:04<00:10,  3.30it/s]
 52%|#####1    | 25.81/50.0 [00:05<00:05,  4.54it/s]
 73%|#######3  | 36.5/50.0 [00:06<00:02,  5.53it/s]
 95%|#########5| 47.71/50.0 [00:07<00:00,  6.33it/s]
 95%|#########5| 47.71/50.0 [00:07<00:00,  6.07it/s]
100%|##########| 50.0/50.0 [00:07<00:00,  6.36it/s]
100%|##########| 50.0/50.0 [00:07<00:00,  6.36it/s]

from pde import FieldCollection, PDEBase, PlotTracker, ScalarField, UnitGrid


class SIRPDE(PDEBase):
    """SIR-model with diffusive mobility"""

    def __init__(
        self, beta=0.3, gamma=0.9, diffusivity=0.1, bc="auto_periodic_neumann"
    ):
        self.beta = beta  # transmission rate
        self.gamma = gamma  # recovery rate
        self.diffusivity = diffusivity  # spatial mobility
        self.bc = bc  # boundary condition

    def get_state(self, s, i):
        """generate a suitable initial state"""
        norm = (s + i).data.max()  # maximal density
        if norm > 1:
            s /= norm
            i /= norm
        s.label = "Susceptible"
        i.label = "Infected"

        # create recovered field
        r = ScalarField(s.grid, data=1 - s - i, label="Recovered")
        return FieldCollection([s, i, r])

    def evolution_rate(self, state, t=0):
        s, i, r = state
        diff = self.diffusivity
        ds_dt = diff * s.laplace(self.bc) - self.beta * i * s
        di_dt = diff * i.laplace(self.bc) + self.beta * i * s - self.gamma * i
        dr_dt = diff * r.laplace(self.bc) + self.gamma * i
        return FieldCollection([ds_dt, di_dt, dr_dt])


eq = SIRPDE(beta=2, gamma=0.1)

# initialize state
grid = UnitGrid([32, 32])
s = ScalarField(grid, 1)
i = ScalarField(grid, 0)
i.data[0, 0] = 1
state = eq.get_state(s, i)

# simulate the pde
tracker = PlotTracker(interval=10, plot_args={"vmin": 0, "vmax": 1})
sol = eq.solve(state, t_range=50, dt=1e-2, tracker=["progress", tracker])

Total running time of the script: ( 0 minutes 8.009 seconds)

Gallery generated by Sphinx-Gallery