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:00<?, ?it/s]
3%|▎ | 0.29/10.0 [00:00<00:04, 2.20it/s]
11%|█ | 1.07/10.0 [00:00<00:01, 6.53it/s]
54%|█████▍ | 5.43/10.0 [00:00<00:00, 15.86it/s]
54%|█████▍ | 5.43/10.0 [00:00<00:00, 10.32it/s]
100%|██████████| 10.0/10.0 [00:00<00:00, 19.01it/s]
100%|██████████| 10.0/10.0 [00:00<00:00, 19.00it/s]
import numpy as np
from numba import jit
from pde import DiffusionPDE, ScalarField, UnitGrid
class DiffusionCustomNoisePDE(DiffusionPDE):
"""Diffusion PDE with custom noise implementations."""
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_numba(self, state):
"""Numba implementation of spatially-dependent noise."""
noise = float(self.noise)
x_values = state.grid.cell_coords[..., 0]
@jit
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 0.603 seconds)