Note
Go to the end to download the full example code.
2.4.7 Custom noise
This example solves a diffusion equation with a custom noise.

0%| | 0/10.0 [00:00<?, ?it/s]
Initializing: 0%| | 0/10.0 [00:00<?, ?it/s]
0%| | 0/10.0 [00:04<?, ?it/s]
0%| | 0.01/10.0 [00:04<1:07:53, 407.76s/it]
3%|▎ | 0.31/10.0 [00:04<02:07, 13.16s/it]
80%|███████▉ | 7.97/10.0 [00:04<00:01, 1.94it/s]
80%|███████▉ | 7.97/10.0 [00:04<00:01, 1.93it/s]
100%|██████████| 10.0/10.0 [00:04<00:00, 2.42it/s]
100%|██████████| 10.0/10.0 [00:04<00:00, 2.42it/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) # noqa: NPY002
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 4.203 seconds)