Algorithmes ? Programmation ?
Ils sont absolument partout dans votre quotidien.
Venez découvrir comment...
Lien de pré-inscription
Contact mail
EPFL IC Cours Turing
BC 246 (Bâtiment BC)
Station 14
CH-1015 Lausanne
<span style="color:red">import numpy as np</span><br /> <br /> import matplotlib.pyplot as pl<br /> <br /> <br /> <br /> # ============ define relevant functions =============<br /> <br /> <br /> <br /> # an efficient function to compute a mean over neighboring cells<br /> <br /> def apply_laplacian(mat):<br /> <br /> """This function applies a discretized Laplacian<br /> <br /> in periodic boundary conditions to a matrix<br /> <br /> For more information see<br /> <br /> https://en.wikipedia.org/wiki/Discrete_Laplace_operator#Implementation_via_operator_discretization<br /> <br /> """<br /> <br /> <br /> <br /> # the cell appears 4 times in the formula to compute<br /> <br /> # the total difference<br /> <br /> neigh_mat = -4*mat.copy()<br /> <br /> <br /> <br /> # Each direct neighbor on the lattice is counted in<br /> <br /> # the discrete difference formula<br /> <br /> neighbors = [<br /> <br /> ( 1.0, (-1, 0) ),<br /> <br /> ( 1.0, ( 0,-1) ),<br /> <br /> ( 1.0, ( 0, 1) ),<br /> <br /> ( 1.0, ( 1, 0) ),<br /> <br /> ]<br /> <br /> <br /> <br /> # shift matrix according to demanded neighbors<br /> <br /> # and add to this cell with corresponding weight<br /> <br /> for weight, neigh in neighbors:<br /> <br /> neigh_mat += weight * np.roll(mat, neigh, (0,1))<br /> <br /> <br /> <br /> return neigh_mat<br /> <br /> <br /> <br /> # Define the update formula for chemicals A and B<br /> <br /> def update(A, B, DA, DB, f, k, delta_t):<br /> <br /> """Apply the Gray-Scott update formula"""<br /> <br /> <br /> <br /> # compute the diffusion part of the update<br /> <br /> diff_A = DA * apply_laplacian(A)<br /> <br /> diff_B = DB * apply_laplacian(B)<br /> <br /> <br /> <br /> # Apply chemical reaction<br /> <br /> reaction = A*B**2<br /> <br /> diff_A -= reaction<br /> <br /> diff_B += reaction<br /> <br /> <br /> <br /> # Apply birth/death<br /> <br /> diff_A += f * (1-A)<br /> <br /> diff_B -= (k+f) * B<br /> <br /> <br /> <br /> A += diff_A * delta_t<br /> <br /> B += diff_B * delta_t<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def get_initial_A_and_B(N, random_influence = 0.2):<br /> <br /> """get the initial chemical concentrations"""<br /> <br /> <br /> <br /> # get initial homogeneous concentrations<br /> <br /> A = (1-random_influence) * np.ones((N,N))<br /> <br /> B = np.zeros((N,N))<br /> <br /> <br /> <br /> # put some noise on there<br /> <br /> A += random_influence * np.random.random((N,N))<br /> <br /> B += random_influence * np.random.random((N,N))<br /> <br /> <br /> <br /> # get center and radius for initial disturbance<br /> <br /> N2, r = N//2, 50<br /> <br /> <br /> <br /> # apply initial disturbance<br /> <br /> A[N2-r:N2+r, N2-r:N2+r] = 0.50<br /> <br /> B[N2-r:N2+r, N2-r:N2+r] = 0.25<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def draw(A, B):<br /> <br /> """return the matplotlib artists for animation"""<br /> <br /> fig, ax = pl.subplots(1,2,figsize=(5.65,3))<br /> <br /> imA = ax[0].imshow(A, animated=True,vmin=0,cmap='Greys')<br /> <br /> imB = ax[1].imshow(B, animated=True,vmax=1,cmap='Greys')<br /> <br /> ax[0].axis('off')<br /> <br /> ax[1].axis('off')<br /> <br /> ax[0].set_title('A')<br /> <br /> ax[1].set_title('B')<br /> <br /> <br /> <br /> return fig, imA, imB<br /> <br /> <br /> <br /> # =========== define model parameters ==========<br /> <br /> <br /> <br /> # update in time<br /> <br /> delta_t = 1.0<br /> <br /> <br /> <br /> # Diffusion coefficients<br /> <br /> DA = 0.16<br /> <br /> DB = 0.08<br /> <br /> <br /> <br /> # define birth/death rates<br /> <br /> f = 0.060<br /> <br /> k = 0.062<br /> <br /> <br /> <br /> # grid size<br /> <br /> N = 200<br /> <br /> <br /> <br /> # intialize the chemical concentrations<br /> <br /> A, B = get_initial_A_and_B(N)<br /> <br /> <br /> <br /> N_simulation_steps = 10000<br /> <br /> for step in range(N_simulation_steps):<br /> <br /> A, B = update(A, B, DA, DB, f, k, delta_t)<br /> <br /> <br /> <br /> draw(A, B)<br /> <br /> <br /> <br /> # show the result<br /> <br /> pl.show()<br /> <br />
import numpy as np<br /> <br /> import matplotlib.pyplot as pl<br /> <br /> <br /> <br /> # ============ define relevant functions =============<br /> <br /> <br /> <br /> # an efficient function to compute a mean over neighboring cells<br /> <br /> def apply_laplacian(mat):<br /> <br /> """This function applies a discretized Laplacian<br /> <br /> in periodic boundary conditions to a matrix<br /> <br /> For more information see<br /> <br /> https://en.wikipedia.org/wiki/Discrete_Laplace_operator#Implementation_via_operator_discretization<br /> <br /> """<br /> <br /> <br /> <br /> # the cell appears 4 times in the formula to compute<br /> <br /> # the total difference<br /> <br /> neigh_mat = -4*mat.copy()<br /> <br /> <br /> <br /> # Each direct neighbor on the lattice is counted in<br /> <br /> # the discrete difference formula<br /> <br /> neighbors = [<br /> <br /> ( 1.0, (-1, 0) ),<br /> <br /> ( 1.0, ( 0,-1) ),<br /> <br /> ( 1.0, ( 0, 1) ),<br /> <br /> ( 1.0, ( 1, 0) ),<br /> <br /> ]<br /> <br /> <br /> <br /> # shift matrix according to demanded neighbors<br /> <br /> # and add to this cell with corresponding weight<br /> <br /> for weight, neigh in neighbors:<br /> <br /> neigh_mat += weight * np.roll(mat, neigh, (0,1))<br /> <br /> <br /> <br /> return neigh_mat<br /> <br /> <br /> <br /> # Define the update formula for chemicals A and B<br /> <br /> def update(A, B, DA, DB, f, k, delta_t):<br /> <br /> """Apply the Gray-Scott update formula"""<br /> <br /> <br /> <br /> # compute the diffusion part of the update<br /> <br /> diff_A = DA * apply_laplacian(A)<br /> <br /> diff_B = DB * apply_laplacian(B)<br /> <br /> <br /> <br /> # Apply chemical reaction<br /> <br /> reaction = A*B**2<br /> <br /> diff_A -= reaction<br /> <br /> diff_B += reaction<br /> <br /> <br /> <br /> # Apply birth/death<br /> <br /> diff_A += f * (1-A)<br /> <br /> diff_B -= (k+f) * B<br /> <br /> <br /> <br /> A += diff_A * delta_t<br /> <br /> B += diff_B * delta_t<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def get_initial_A_and_B(N, random_influence = 0.2):<br /> <br /> """get the initial chemical concentrations"""<br /> <br /> <br /> <br /> # get initial homogeneous concentrations<br /> <br /> A = (1-random_influence) * np.ones((N,N))<br /> <br /> B = np.zeros((N,N))<br /> <br /> <br /> <br /> # put some noise on there<br /> <br /> A += random_influence * np.random.random((N,N))<br /> <br /> B += random_influence * np.random.random((N,N))<br /> <br /> <br /> <br /> # get center and radius for initial disturbance<br /> <br /> N2, r = N//2, 50<br /> <br /> <br /> <br /> # apply initial disturbance<br /> <br /> A[N2-r:N2+r, N2-r:N2+r] = 0.50<br /> <br /> B[N2-r:N2+r, N2-r:N2+r] = 0.25<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def draw(A, B):<br /> <br /> """return the matplotlib artists for animation"""<br /> <br /> fig, ax = pl.subplots(1,2,figsize=(5.65,3))<br /> <br /> imA = ax[0].imshow(A, animated=True,vmin=0,cmap='Greys')<br /> <br /> imB = ax[1].imshow(B, animated=True,vmax=1,cmap='Greys')<br /> <br /> ax[0].axis('off')<br /> <br /> ax[1].axis('off')<br /> <br /> ax[0].set_title('A')<br /> <br /> ax[1].set_title('B')<br /> <br /> <br /> <br /> return fig, imA, imB<br /> <br /> <br /> <br /> # =========== define model parameters ==========<br /> <br /> <br /> <br /> # update in time<br /> <br /> delta_t = 1.0<br /> <br /> <br /> <br /> # Diffusion coefficients<br /> <br /> DA = 0.16<br /> <br /> DB = 0.08<br /> <br /> <br /> <br /> # define birth/death rates<br /> <br /> f = 0.060<br /> <br /> k = 0.062<br /> <br /> <br /> <br /> # grid size<br /> <br /> N = 200<br /> <br /> <br /> <br /> # intialize the chemical concentrations<br /> <br /> A, B = get_initial_A_and_B(N)<br /> <br /> <br /> <br /> N_simulation_steps = 10000<br /> <br /> for step in range(N_simulation_steps):<br /> <br /> A, B = update(A, B, DA, DB, f, k, delta_t)<br /> <br /> <br /> <br /> draw(A, B)<br /> <br /> <br /> <br /> # show the result<br /> <br /> pl.show()<br /> <br />
import numpy as np<br /> <br /> import matplotlib.pyplot as pl<br /> <br /> <br /> <br /> # ============ define relevant functions =============<br /> <br /> <br /> <br /> # an efficient function to compute a mean over neighboring cells<br /> <br /> def apply_laplacian(mat):<br /> <br /> """This function applies a discretized Laplacian<br /> <br /> in periodic boundary conditions to a matrix<br /> <br /> For more information see<br /> <br /> https://en.wikipedia.org/wiki/Discrete_Laplace_operator#Implementation_via_operator_discretization<br /> <br /> """<br /> <br /> <br /> <br /> # the cell appears 4 times in the formula to compute<br /> <br /> # the total difference<br /> <br /> neigh_mat = -4*mat.copy()<br /> <br /> <br /> <br /> # Each direct neighbor on the lattice is counted in<br /> <br /> # the discrete difference formula<br /> <br /> neighbors = [<br /> <br /> ( 1.0, (-1, 0) ),<br /> <br /> ( 1.0, ( 0,-1) ),<br /> <br /> ( 1.0, ( 0, 1) ),<br /> <br /> ( 1.0, ( 1, 0) ),<br /> <br /> ]<br /> <br /> <br /> <br /> # shift matrix according to demanded neighbors<br /> <br /> # and add to this cell with corresponding weight<br /> <br /> for weight, neigh in neighbors:<br /> <br /> neigh_mat += weight * np.roll(mat, neigh, (0,1))<br /> <br /> <br /> <br /> return neigh_mat<br /> <br /> <br /> <br /> # Define the update formula for chemicals A and B<br /> <br /> def update(A, B, DA, DB, f, k, delta_t):<br /> <br /> """Apply the Gray-Scott update formula"""<br /> <br /> <br /> <br /> # compute the diffusion part of the update<br /> <br /> diff_A = DA * apply_laplacian(A)<br /> <br /> diff_B = DB * apply_laplacian(B)<br /> <br /> <br /> <br /> # Apply chemical reaction<br /> <br /> reaction = A*B**2<br /> <br /> diff_A -= reaction<br /> <br /> diff_B += reaction<br /> <br /> <br /> <br /> # Apply birth/death<br /> <br /> diff_A += f * (1-A)<br /> <br /> diff_B -= (k+f) * B<br /> <br /> <br /> <br /> A += diff_A * delta_t<br /> <br /> B += diff_B * delta_t<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def get_initial_A_and_B(N, random_influence = 0.2):<br /> <br /> """get the initial chemical concentrations"""<br /> <br /> <br /> <br /> # get initial homogeneous concentrations<br /> <br /> A = (1-random_influence) * np.ones((N,N))<br /> <br /> B = np.zeros((N,N))<br /> <br /> <br /> <br /> # put some noise on there<br /> <br /> A += random_influence * np.random.random((N,N))<br /> <br /> B += random_influence * np.random.random((N,N))<br /> <br /> <br /> <br /> # get center and radius for initial disturbance<br /> <br /> N2, r = N//2, 50<br /> <br /> <br /> <br /> # apply initial disturbance<br /> <br /> A[N2-r:N2+r, N2-r:N2+r] = 0.50<br /> <br /> B[N2-r:N2+r, N2-r:N2+r] = 0.25<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def draw(A, B):<br /> <br /> """return the matplotlib artists for animation"""<br /> <br /> fig, ax = pl.subplots(1,2,figsize=(5.65,3))<br /> <br /> imA = ax[0].imshow(A, animated=True,vmin=0,cmap='Greys')<br /> <br /> imB = ax[1].imshow(B, animated=True,vmax=1,cmap='Greys')<br /> <br /> ax[0].axis('off')<br /> <br /> ax[1].axis('off')<br /> <br /> ax[0].set_title('A')<br /> <br /> ax[1].set_title('B')<br /> <br /> <br /> <br /> return fig, imA, imB<br /> <br /> <br /> <br /> # =========== define model parameters ==========<br /> <br /> <br /> <br /> # update in time<br /> <br /> delta_t = 1.0<br /> <br /> <br /> <br /> # Diffusion coefficients<br /> <br /> DA = 0.16<br /> <br /> DB = 0.08<br /> <br /> <br /> <br /> # define birth/death rates<br /> <br /> f = 0.060<br /> <br /> k = 0.062<br /> <br /> <br /> <br /> # grid size<br /> <br /> N = 200<br /> <br /> <br /> <br /> # intialize the chemical concentrations<br /> <br /> A, B = get_initial_A_and_B(N)<br /> <br /> <br /> <br /> N_simulation_steps = 10000<br /> <br /> for step in range(N_simulation_steps):<br /> <br /> A, B = update(A, B, DA, DB, f, k, delta_t)<br /> <br /> <br /> <br /> draw(A, B)<br /> <br /> <br /> <br /> # show the result<br /> <br /> pl.show()<br /> <br />
import numpy as np<br /> <br /> import matplotlib.pyplot as pl<br /> <br /> <br /> <br /> # ============ define relevant functions =============<br /> <br /> <br /> <br /> # an efficient function to compute a mean over neighboring cells<br /> <br /> def apply_laplacian(mat):<br /> <br /> """This function applies a discretized Laplacian<br /> <br /> in periodic boundary conditions to a matrix<br /> <br /> For more information see<br /> <br /> https://en.wikipedia.org/wiki/Discrete_Laplace_operator#Implementation_via_operator_discretization<br /> <br /> """<br /> <br /> <br /> <br /> # the cell appears 4 times in the formula to compute<br /> <br /> # the total difference<br /> <br /> neigh_mat = -4*mat.copy()<br /> <br /> <br /> <br /> # Each direct neighbor on the lattice is counted in<br /> <br /> # the discrete difference formula<br /> <br /> neighbors = [<br /> <br /> ( 1.0, (-1, 0) ),<br /> <br /> ( 1.0, ( 0,-1) ),<br /> <br /> ( 1.0, ( 0, 1) ),<br /> <br /> ( 1.0, ( 1, 0) ),<br /> <br /> ]<br /> <br /> <br /> <br /> # shift matrix according to demanded neighbors<br /> <br /> # and add to this cell with corresponding weight<br /> <br /> for weight, neigh in neighbors:<br /> <br /> neigh_mat += weight * np.roll(mat, neigh, (0,1))<br /> <br /> <br /> <br /> return neigh_mat<br /> <br /> <br /> <br /> # Define the update formula for chemicals A and B<br /> <br /> def update(A, B, DA, DB, f, k, delta_t):<br /> <br /> """Apply the Gray-Scott update formula"""<br /> <br /> <br /> <br /> # compute the diffusion part of the update<br /> <br /> diff_A = DA * apply_laplacian(A)<br /> <br /> diff_B = DB * apply_laplacian(B)<br /> <br /> <br /> <br /> # Apply chemical reaction<br /> <br /> reaction = A*B**2<br /> <br /> diff_A -= reaction<br /> <br /> diff_B += reaction<br /> <br /> <br /> <br /> # Apply birth/death<br /> <br /> diff_A += f * (1-A)<br /> <br /> diff_B -= (k+f) * B<br /> <br /> <br /> <br /> A += diff_A * delta_t<br /> <br /> B += diff_B * delta_t<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def get_initial_A_and_B(N, random_influence = 0.2):<br /> <br /> """get the initial chemical concentrations"""<br /> <br /> <br /> <br /> # get initial homogeneous concentrations<br /> <br /> A = (1-random_influence) * np.ones((N,N))<br /> <br /> B = np.zeros((N,N))<br /> <br /> <br /> <br /> # put some noise on there<br /> <br /> A += random_influence * np.random.random((N,N))<br /> <br /> B += random_influence * np.random.random((N,N))<br /> <br /> <br /> <br /> # get center and radius for initial disturbance<br /> <br /> N2, r = N//2, 50<br /> <br /> <br /> <br /> # apply initial disturbance<br /> <br /> A[N2-r:N2+r, N2-r:N2+r] = 0.50<br /> <br /> B[N2-r:N2+r, N2-r:N2+r] = 0.25<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def draw(A, B):<br /> <br /> """return the matplotlib artists for animation"""<br /> <br /> fig, ax = pl.subplots(1,2,figsize=(5.65,3))<br /> <br /> imA = ax[0].imshow(A, animated=True,vmin=0,cmap='Greys')<br /> <br /> imB = ax[1].imshow(B, animated=True,vmax=1,cmap='Greys')<br /> <br /> ax[0].axis('off')<br /> <br /> ax[1].axis('off')<br /> <br /> ax[0].set_title('A')<br /> <br /> ax[1].set_title('B')<br /> <br /> <br /> <br /> return fig, imA, imB<br /> <br /> <br /> <br /> # =========== define model parameters ==========<br /> <br /> <br /> <br /> # update in time<br /> <br /> delta_t = 1.0<br /> <br /> <br /> <br /> # Diffusion coefficients<br /> <br /> DA = 0.16<br /> <br /> DB = 0.08<br /> <br /> <br /> <br /> # define birth/death rates<br /> <br /> f = 0.060<br /> <br /> k = 0.062<br /> <br /> <br /> <br /> # grid size<br /> <br /> N = 200<br /> <br /> <br /> <br /> # intialize the chemical concentrations<br /> <br /> A, B = get_initial_A_and_B(N)<br /> <br /> <br /> <br /> N_simulation_steps = 10000<br /> <br /> for step in range(N_simulation_steps):<br /> <br /> A, B = update(A, B, DA, DB, f, k, delta_t)<br /> <br /> <br /> <br /> draw(A, B)<br /> <br /> <br /> <br /> # show the result<br /> <br /> pl.show()<br /> <br />
import numpy as np<br /> <br /> import matplotlib.pyplot as pl<br /> <br /> <br /> <br /> # ============ define relevant functions =============<br /> <br /> <br /> <br /> # an efficient function to compute a mean over neighboring cells<br /> <br /> def apply_laplacian(mat):<br /> <br /> """This function applies a discretized Laplacian<br /> <br /> in periodic boundary conditions to a matrix<br /> <br /> For more information see<br /> <br /> https://en.wikipedia.org/wiki/Discrete_Laplace_operator#Implementation_via_operator_discretization<br /> <br /> """<br /> <br /> <br /> <br /> # the cell appears 4 times in the formula to compute<br /> <br /> # the total difference<br /> <br /> neigh_mat = -4*mat.copy()<br /> <br /> <br /> <br /> # Each direct neighbor on the lattice is counted in<br /> <br /> # the discrete difference formula<br /> <br /> neighbors = [<br /> <br /> ( 1.0, (-1, 0) ),<br /> <br /> ( 1.0, ( 0,-1) ),<br /> <br /> ( 1.0, ( 0, 1) ),<br /> <br /> ( 1.0, ( 1, 0) ),<br /> <br /> ]<br /> <br /> <br /> <br /> # shift matrix according to demanded neighbors<br /> <br /> # and add to this cell with corresponding weight<br /> <br /> for weight, neigh in neighbors:<br /> <br /> neigh_mat += weight * np.roll(mat, neigh, (0,1))<br /> <br /> <br /> <br /> return neigh_mat<br /> <br /> <br /> <br /> # Define the update formula for chemicals A and B<br /> <br /> def update(A, B, DA, DB, f, k, delta_t):<br /> <br /> """Apply the Gray-Scott update formula"""<br /> <br /> <br /> <br /> # compute the diffusion part of the update<br /> <br /> diff_A = DA * apply_laplacian(A)<br /> <br /> diff_B = DB * apply_laplacian(B)<br /> <br /> <br /> <br /> # Apply chemical reaction<br /> <br /> reaction = A*B**2<br /> <br /> diff_A -= reaction<br /> <br /> diff_B += reaction<br /> <br /> <br /> <br /> # Apply birth/death<br /> <br /> diff_A += f * (1-A)<br /> <br /> diff_B -= (k+f) * B<br /> <br /> <br /> <br /> A += diff_A * delta_t<br /> <br /> B += diff_B * delta_t<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def get_initial_A_and_B(N, random_influence = 0.2):<br /> <br /> """get the initial chemical concentrations"""<br /> <br /> <br /> <br /> # get initial homogeneous concentrations<br /> <br /> A = (1-random_influence) * np.ones((N,N))<br /> <br /> B = np.zeros((N,N))<br /> <br /> <br /> <br /> # put some noise on there<br /> <br /> A += random_influence * np.random.random((N,N))<br /> <br /> B += random_influence * np.random.random((N,N))<br /> <br /> <br /> <br /> # get center and radius for initial disturbance<br /> <br /> N2, r = N//2, 50<br /> <br /> <br /> <br /> # apply initial disturbance<br /> <br /> A[N2-r:N2+r, N2-r:N2+r] = 0.50<br /> <br /> B[N2-r:N2+r, N2-r:N2+r] = 0.25<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def draw(A, B):<br /> <br /> """return the matplotlib artists for animation"""<br /> <br /> fig, ax = pl.subplots(1,2,figsize=(5.65,3))<br /> <br /> imA = ax[0].imshow(A, animated=True,vmin=0,cmap='Greys')<br /> <br /> imB = ax[1].imshow(B, animated=True,vmax=1,cmap='Greys')<br /> <br /> ax[0].axis('off')<br /> <br /> ax[1].axis('off')<br /> <br /> ax[0].set_title('A')<br /> <br /> ax[1].set_title('B')<br /> <br /> <br /> <br /> return fig, imA, imB<br /> <br /> <br /> <br /> # =========== define model parameters ==========<br /> <br /> <br /> <br /> # update in time<br /> <br /> delta_t = 1.0<br /> <br /> <br /> <br /> # Diffusion coefficients<br /> <br /> DA = 0.16<br /> <br /> DB = 0.08<br /> <br /> <br /> <br /> # define birth/death rates<br /> <br /> f = 0.060<br /> <br /> k = 0.062<br /> <br /> <br /> <br /> # grid size<br /> <br /> N = 200<br /> <br /> <br /> <br /> # intialize the chemical concentrations<br /> <br /> A, B = get_initial_A_and_B(N)<br /> <br /> <br /> <br /> N_simulation_steps = 10000<br /> <br /> for step in range(N_simulation_steps):<br /> <br /> A, B = update(A, B, DA, DB, f, k, delta_t)<br /> <br /> <br /> <br /> draw(A, B)<br /> <br /> <br /> <br /> # show the result<br /> <br /> pl.show()<br /> <br />
import numpy as np<br /> <br /> import matplotlib.pyplot as pl<br /> <br /> <br /> <br /> # ============ define relevant functions =============<br /> <br /> <br /> <br /> # an efficient function to compute a mean over neighboring cells<br /> <br /> def apply_laplacian(mat):<br /> <br /> """This function applies a discretized Laplacian<br /> <br /> in periodic boundary conditions to a matrix<br /> <br /> For more information see<br /> <br /> https://en.wikipedia.org/wiki/Discrete_Laplace_operator#Implementation_via_operator_discretization<br /> <br /> """<br /> <br /> <br /> <br /> # the cell appears 4 times in the formula to compute<br /> <br /> # the total difference<br /> <br /> neigh_mat = -4*mat.copy()<br /> <br /> <br /> <br /> # Each direct neighbor on the lattice is counted in<br /> <br /> # the discrete difference formula<br /> <br /> neighbors = [<br /> <br /> ( 1.0, (-1, 0) ),<br /> <br /> ( 1.0, ( 0,-1) ),<br /> <br /> ( 1.0, ( 0, 1) ),<br /> <br /> ( 1.0, ( 1, 0) ),<br /> <br /> ]<br /> <br /> <br /> <br /> # shift matrix according to demanded neighbors<br /> <br /> # and add to this cell with corresponding weight<br /> <br /> for weight, neigh in neighbors:<br /> <br /> neigh_mat += weight * np.roll(mat, neigh, (0,1))<br /> <br /> <br /> <br /> return neigh_mat<br /> <br /> <br /> <br /> # Define the update formula for chemicals A and B<br /> <br /> def update(A, B, DA, DB, f, k, delta_t):<br /> <br /> """Apply the Gray-Scott update formula"""<br /> <br /> <br /> <br /> # compute the diffusion part of the update<br /> <br /> diff_A = DA * apply_laplacian(A)<br /> <br /> diff_B = DB * apply_laplacian(B)<br /> <br /> <br /> <br /> # Apply chemical reaction<br /> <br /> reaction = A*B**2<br /> <br /> diff_A -= reaction<br /> <br /> diff_B += reaction<br /> <br /> <br /> <br /> # Apply birth/death<br /> <br /> diff_A += f * (1-A)<br /> <br /> diff_B -= (k+f) * B<br /> <br /> <br /> <br /> A += diff_A * delta_t<br /> <br /> B += diff_B * delta_t<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def get_initial_A_and_B(N, random_influence = 0.2):<br /> <br /> """get the initial chemical concentrations"""<br /> <br /> <br /> <br /> # get initial homogeneous concentrations<br /> <br /> A = (1-random_influence) * np.ones((N,N))<br /> <br /> B = np.zeros((N,N))<br /> <br /> <br /> <br /> # put some noise on there<br /> <br /> A += random_influence * np.random.random((N,N))<br /> <br /> B += random_influence * np.random.random((N,N))<br /> <br /> <br /> <br /> # get center and radius for initial disturbance<br /> <br /> N2, r = N//2, 50<br /> <br /> <br /> <br /> # apply initial disturbance<br /> <br /> A[N2-r:N2+r, N2-r:N2+r] = 0.50<br /> <br /> B[N2-r:N2+r, N2-r:N2+r] = 0.25<br /> <br /> <br /> <br /> return A, B<br /> <br /> <br /> <br /> def draw(A, B):<br /> <br /> """return the matplotlib artists for animation"""<br /> <br /> fig, ax = pl.subplots(1,2,figsize=(5.65,3))<br /> <br /> imA = ax[0].imshow(A, animated=True,vmin=0,cmap='Greys')<br /> <br /> imB = ax[1].imshow(B, animated=True,vmax=1,cmap='Greys')<br /> <br /> ax[0].axis('off')<br /> <br /> ax[1].axis('off')<br /> <br /> ax[0].set_title('A')<br /> <br /> ax[1].set_title('B')<br /> <br /> <br /> <br /> return fig, imA, imB<br /> <br /> <br /> <br /> # =========== define model parameters ==========<br /> <br /> <br /> <br /> # update in time<br /> <br /> delta_t = 1.0<br /> <br /> <br /> <br /> # Diffusion coefficients<br /> <br /> DA = 0.16<br /> <br /> DB = 0.08<br /> <br /> <br /> <br /> # define birth/death rates<br /> <br /> f = 0.060<br /> <br /> k = 0.062<br /> <br /> <br /> <br /> # grid size<br /> <br /> N = 200<br /> <br /> <br /> <br /> # intialize the chemical concentrations<br /> <br /> A, B = get_initial_A_and_B(N)<br /> <br /> <br /> <br /> N_simulation_steps = 10000<br /> <br /> for step in range(N_simulation_steps):<br /> <br /> A, B = update(A, B, DA, DB, f, k, delta_t)<br /> <br /> <br /> <br /> draw(A, B)<br /> <br /> <br /> <br /> # show the result<br /> <br /> pl.show()<br /> <br />
26-08-2023
02-09-2023
09-09-2023
23-09-2023
30-09-2023
07-10-2023
04-11-2023
11-11-2023
18-11-2023
25-11-2023
02-12-2023
09-12-2023
16-12-2023
13-01-2024
20-01-2024
27-01-2024
03-02-2024
24-02-2024
02-03-2024
09-03-2024
16-03-2024
23-03-2024
20-04-2024
27-04-2024
04-05-2024
25-05-2024
01-06-2024
08-06-2024
15-06-2024
22-06-2024