Note
Go to the end to download the full example code.
2.2.7 Klein-Gordon equation
This example solves the Klein-Gordon equation in one dimension, showing the effect of the mass term on wave propagation. With mass=0 the equation reduces to the standard wave equation; increasing the mass introduces dispersion.

0%| | 0/50.0 [00:00<?, ?it/s]
Initializing: 0%| | 0/50.0 [00:00<?, ?it/s]
0%| | 0/50.0 [00:02<?, ?it/s]
0%| | 0.01/50.0 [00:02<4:01:55, 290.37s/it]
1%| | 0.39/50.0 [00:02<06:09, 7.45s/it]
63%|██████▎ | 31.58/50.0 [00:02<00:01, 10.87it/s]
63%|██████▎ | 31.58/50.0 [00:02<00:01, 10.86it/s]
100%|██████████| 50.0/50.0 [00:02<00:00, 17.20it/s]
100%|██████████| 50.0/50.0 [00:02<00:00, 17.19it/s]
from pde import KleinGordonPDE, ScalarField, UnitGrid
grid = UnitGrid([128]) # generate grid
u = ScalarField.from_expression(grid, "exp(-((x - 32) / 5) ** 2)")
eq = KleinGordonPDE(speed=1, mass=0.5) # define the pde
state = eq.get_initial_condition(u)
result = eq.solve(state, t_range=50, dt=0.01)
result[0].plot()
Total running time of the script: (0 minutes 3.024 seconds)