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.

post step hook
  0%|          | 0/10000.0 [00:00<?, ?it/s]
Initializing:   0%|          | 0/10000.0 [00:00<?, ?it/s]
  0%|          | 0/10000.0 [00:00<?, ?it/s]
  0%|          | 0.2/10000.0 [00:10<143:53:27, 51.80s/it]
  0%|          | 0.3/10000.0 [00:10<95:55:47, 34.54s/it]
  0%|          | 3.2/10000.0 [00:10<8:59:38,  3.24s/it]
  1%|          | 52.5/10000.0 [00:10<32:54,  5.04it/s]
  3%|▎         | 263.4/10000.0 [00:10<06:33, 24.72it/s]
  7%|▋         | 697.2/10000.0 [00:11<02:28, 62.68it/s]
  7%|▋         | 697.2/10000.0 [00:11<02:32, 60.89it/s]
  7%|▋         | 697.2/10000.0 [00:11<02:32, 60.89it/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 11.563 seconds)