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 />
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 />
24-08-2024
31-08-2024
07-09-2024
21-09-2024
28-09-2024
05-10-2024
02-11-2024
09-11-2024
16-11-2024
23-11-2024
30-11-2024
07-12-2024
14-12-2024
11-01-2025
18-01-2025
25-01-2025
01-02-2025
08-02-2025
01-03-2025
08-03-2025
15-03-2025
22-03-2025
29-03-2025
05-04-2025
03-05-2025
10-05-2025
17-05-2025
24-05-2025