2.4.7 Custom noise

This example solves a diffusion equation with a custom noise.

custom noise
  0%|          | 0/10.0 [00:00<?, ?it/s]
Initializing:   0%|          | 0/10.0 [00:00<?, ?it/s]
  0%|          | 0/10.0 [00:03<?, ?it/s]
  0%|          | 0.01/10.0 [00:03<1:01:53, 371.70s/it]
  3%|▎         | 0.3/10.0 [00:03<02:00, 12.40s/it]
 67%|██████▋   | 6.67/10.0 [00:03<00:01,  1.77it/s]
 67%|██████▋   | 6.67/10.0 [00:03<00:01,  1.76it/s]
100%|██████████| 10.0/10.0 [00:03<00:00,  2.64it/s]
100%|██████████| 10.0/10.0 [00:03<00:00,  2.64it/s]

import numpy as np

from pde import DiffusionPDE, ScalarField, UnitGrid


class DiffusionCustomNoisePDE(DiffusionPDE):
    """Diffusion PDE with custom noise implementation."""

    def noise_realization(self, state, t):
        """Numpy implementation of spatially dependent noise."""
        noise_field = ScalarField.random_uniform(state.grid, -self.noise, self.noise)
        return state.grid.cell_coords[..., 0] * noise_field

    def make_noise_realization(self, state, backend):
        """Compilable implementation of spatially dependent noise."""
        noise = float(self.noise)
        x_values = state.grid.cell_coords[..., 0]

        def noise_realization(state_data, t):
            return x_values * np.random.uniform(-noise, noise, size=state_data.shape)

        return noise_realization


eq = DiffusionCustomNoisePDE(diffusivity=0.1, noise=0.1)  # define the pde
state = ScalarField.random_uniform(UnitGrid([64, 64]))  # generate initial condition
result = eq.solve(state, t_range=10, dt=0.01)
result.plot()

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