Note
Go to the end to download the full example code.
2.4.1 Post-step hook function
Demonstrate the simple hook function in PDE, which is called after
each time step and may modify the state and abort the simulation.

0%| | 0/10000.0 [00:00<?, ?it/s]
Initializing: 0%| | 0/10000.0 [00:00<?, ?it/s]
0%| | 0/10000.0 [00:08<?, ?it/s]
0%| | 0.1/10000.0 [00:11<332:11:35, 119.59s/it]
0%| | 0.2/10000.0 [00:11<166:05:56, 59.80s/it]
0%| | 0.3/10000.0 [00:11<110:43:59, 39.87s/it]
0%| | 11.0/10000.0 [00:11<3:01:01, 1.09s/it]
3%|▎ | 283.9/10000.0 [00:11<06:50, 23.66it/s]
3%|▎ | 283.9/10000.0 [00:12<06:53, 23.48it/s]
3%|▎ | 283.9/10000.0 [00:12<06:53, 23.48it/s]
from pde import PDE, ScalarField, UnitGrid
def post_step_hook(state_data, t):
"""Helper function called after every time step."""
state_data[24:40, 24:40] = 1 # set central region to given value
if t > 1e3:
raise StopIteration # abort simulation at given time
eq = PDE({"c": "laplace(c)"}, post_step_hook=post_step_hook)
state = ScalarField(UnitGrid([64, 64]))
result = eq.solve(state, dt=0.1, t_range=1e4)
result.plot()
Total running time of the script: (0 minutes 12.194 seconds)