import numpy as np import matplotlib.pyplot as plt Définition des paramètres du modèle D…
- User
import numpy as np import matplotlib.pyplot as plt
Définition des paramètres du modèle
D = 1.0 # Coefficient de diffusion L = 10.0 # Longueur du milieu poreux dx = 0.1 # Pas d'espace dt = 0.01 # Pas de temps N = int(L/dx + 1) # Nombre de points d'espace M = 500 # Nombre de pas de temps
Conditions initiales
C = np.zeros(N) C[int(N/2)] = 1.0 # Profil de concentration initial
Boucle de calcul
for k in range(M): C[1:-1] += Ddt/dx**2(C[2:] - 2*C[1:-1] + C[:-2]) # Equation de diffusion C[0] = C[1] # Condition de frontièreC[-1] = C[-2] # Condition de frontière
Affichage du résultat
x = np.linspace(0, L, N) plt.plot(x, C) plt.xlabel('Distance') plt.ylabel('Concentration') plt.show()
- Assistant
The given code simulates the diffusion process of a concentration profile within a one-dimensional porous medium. The model uses the finite difference method to solve the partial differential equation for the diffusion process.
Here is what each part of the code does:
- Definition of model parameters:
- D represents the diffusion coefficient that characterizes the rate of the diffusion process.
- L is the length of the porous medium.
- dx represents the spatial grid size.
- dt is the time step.
- N is the number of spatial grid points, calculated by dividing the length (L) by the grid size (dx) and adding 1.
- M denotes the number of time steps for which the model runs.
- Setting initial conditions:
- A concentration array C is created, with an initial concentration profile having a spike in the center of the porous medium.
- Calculation loop:
- The code iterates through the time steps, calculating the concentration profile at each time step using the diffusion equation and the finite difference method.
- The boundary conditions at both ends of the medium are ensured by forcing the concentration at the first and last grid points to be equal to the second and second-to-last grid points, respectively.
- Displaying the result:
- The spatial grid is defined by creating an array x that spans from 0 to L with N points.
- A plot of the concentration profile at the end of the simulation is created and displayed, with the x-axis representing the distance and the y-axis representing the concentration.
In summary, the code models the diffusion process in a one-dimensional porous medium and displays the concentration profile after a certain number of time steps.