if norm > 1: s /= norm i /= norm s.label = 'Susceptible' i.label = 'Infected' # create recovered field r = ScalarField(s.grid, data=1 - s - i, label='Recovered') return FieldCollection([s, i, r]) def evolution_rate(self, state, t=0): s, i, r = state diff = self.diffusivity ds_dt = diff * s.laplace(self.bc) - self.beta * i * s di_dt = diff * i.laplace(self.bc) + self.beta * i * s - self.gamma * i dr_dt = diff * r.laplace(self.bc) + self.gamma * i return FieldCollection([ds_dt, di_dt, dr_dt]) eq = SIRPDE(beta=2, gamma=0.1) # initialize state grid = UnitGrid([32, 32]) s = ScalarField(grid, 1) i = ScalarField(grid, 0) i.data[0, 0] = 1 state = eq.get_state(s, i) # simulate the pde tracker = PlotTracker(interval=10, plot_args={'vmin': 0, 'vmax': 1}) sol = eq.solve(state, t_range=50, dt=1e-2, tracker=['progress', tracker])
s /= norm i /= norm s.label = "Susceptible" i.label = "Infected" # create recovered field r = ScalarField(s.grid, data=1 - s - i, label="Recovered") return FieldCollection([s, i, r]) def evolution_rate(self, state, t=0): s, i, r = state diff = self.diffusivity ds_dt = diff * s.laplace(self.bc) - self.beta * i * s di_dt = diff * i.laplace(self.bc) + self.beta * i * s - self.gamma * i dr_dt = diff * r.laplace(self.bc) + self.gamma * i return FieldCollection([ds_dt, di_dt, dr_dt]) eq = SIRPDE(beta=2, gamma=0.1) # initialize state grid = UnitGrid([32, 32]) s = ScalarField(grid, 1) i = ScalarField(grid, 0) i.data[0, 0] = 1 state = eq.get_state(s, i) # simulate the pde tracker = PlotTracker(interval=10, plot_args={"vmin": 0, "vmax": 1}) sol = eq.solve(state, t_range=50, dt=1e-2, tracker=["progress", tracker])
#!/usr/bin/env python3 from pde import (DiffusionPDE, UnitGrid, ScalarField, MemoryStorage, PlotTracker, PrintTracker, RealtimeIntervals) eq = DiffusionPDE() # define the pde grid = UnitGrid([16, 16]) # generate grid state = ScalarField.random_uniform(grid, 0.2, 0.3) # generate initial condition storage = MemoryStorage() trackers = [ 'progress', # show progress bar during simulation 'steady_state', # abort when steady state is reached storage.tracker(interval=1), # store data every simulation time unit PlotTracker(show=True), # show images during simulation # print some output every 5 real seconds: PrintTracker(interval=RealtimeIntervals(duration=5)) ] eq.solve(state, 10, dt=0.1, tracker=trackers) storage[0].plot(show=True)
""" Create a movie live =================== This example shows how to create a movie while running the simulation. Making movies requires that `ffmpeg` is installed in a standard location. """ from pde import DiffusionPDE, PlotTracker, ScalarField, UnitGrid grid = UnitGrid([16, 16]) # generate grid state = ScalarField.random_uniform(grid, 0.2, 0.3) # generate initial condition tracker = PlotTracker(movie="diffusion.mov") # create movie tracker eq = DiffusionPDE() # define the physics eq.solve(state, t_range=2, dt=0.005, tracker=tracker)