.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_gallery/advanced_pdes/pde_sir.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_advanced_pdes_pde_sir.py: 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: .. math:: \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 Here, :math:`D` is the diffusivity, :math:`\beta` the infection rate, and :math:`\gamma` the recovery rate. .. GENERATED FROM PYTHON SOURCE LINES 19-69 .. image-sg:: /examples_gallery/advanced_pdes/images/sphx_glr_pde_sir_001.png :alt: Time: 50, Susceptible, Infected, Recovered :srcset: /examples_gallery/advanced_pdes/images/sphx_glr_pde_sir_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/50.0 [00:00 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(interrupts=10, plot_args={"vmin": 0, "vmax": 1}) sol = eq.solve(state, t_range=50, dt=1e-2, tracker=["progress", tracker]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.549 seconds) .. _sphx_glr_download_examples_gallery_advanced_pdes_pde_sir.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pde_sir.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pde_sir.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: pde_sir.zip `