.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_gallery/advanced_pdes/pde_brusselator_class.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_brusselator_class.py: Brusselator - Using custom class ================================ This example implements the `Brusselator `_ with spatial coupling, .. math:: \partial_t u &= D_0 \nabla^2 u + a - (1 + b) u + v u^2 \\ \partial_t v &= D_1 \nabla^2 v + b u - v u^2 Here, :math:`D_0` and :math:`D_1` are the respective diffusivity and the parameters :math:`a` and :math:`b` are related to reaction rates. Note that the PDE can also be implemented using the :class:`~pde.pdes.pde.PDE` class; see :doc:`the example <../simple_pdes/pde_brusselator_expression>`. However, that implementation is less flexible and might be more difficult to extend later. .. GENERATED FROM PYTHON SOURCE LINES 20-78 .. image-sg:: /examples_gallery/advanced_pdes/images/sphx_glr_pde_brusselator_class_001.png :alt: Time: 20 :srcset: /examples_gallery/advanced_pdes/images/sphx_glr_pde_brusselator_class_001.png :class: sphx-glr-single-img .. code-block:: Python import numpy as np from pde import FieldCollection, PDEBase, PlotTracker, ScalarField, UnitGrid class BrusselatorPDE(PDEBase): """Brusselator with diffusive mobility.""" def __init__(self, a=1, b=3, diffusivity=None, bc="auto_periodic_neumann"): super().__init__() self.a = a self.b = b self.diffusivity = [1, 0.1] if diffusivity is None else diffusivity self.bc = bc # boundary condition def get_initial_state(self, grid): """Prepare a useful initial state.""" u = ScalarField(grid, self.a, label="Field $u$") v = self.b / self.a + 0.1 * ScalarField.random_normal(grid, label="Field $v$") return FieldCollection([u, v]) def evolution_rate(self, state, t=0): """Pure python implementation of the PDE.""" u, v = state rhs = state.copy() d0, d1 = self.diffusivity rhs[0] = d0 * u.laplace(self.bc) + self.a - (self.b + 1) * u + u**2 * v rhs[1] = d1 * v.laplace(self.bc) + self.b * u - u**2 * v return rhs def make_evolution_rate(self, state, backend): """Compilable implementation of the PDE.""" d0, d1 = self.diffusivity a, b = self.a, self.b laplace = state.grid.make_operator( "laplace", bc=self.bc, backend=backend, dtype=state.dtype ) def pde_rhs(state_data, t): u = state_data[0] v = state_data[1] rate_u = d0 * laplace(u) + a - (1 + b) * u + v * u**2 rate_v = d1 * laplace(v) + b * u - v * u**2 return np.stack((rate_u, rate_v)) return pde_rhs # initialize state grid = UnitGrid([64, 64]) eq = BrusselatorPDE(diffusivity=[1, 0.1]) state = eq.get_initial_state(grid) # simulate the pde tracker = PlotTracker(interrupts=1, plot_args={"kind": "merged", "vmin": 0, "vmax": 5}) sol = eq.solve(state, t_range=20, dt=1e-3, tracker=tracker) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.496 seconds) .. _sphx_glr_download_examples_gallery_advanced_pdes_pde_brusselator_class.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pde_brusselator_class.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pde_brusselator_class.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: pde_brusselator_class.zip `