Labreport
Labreport
Write a Python program to simulate 1D mass transfer using the finite difference
method. The program should:
1. Define the following parameters:
Length of the domain (length = 1.0)
Number of spatial points (nx = 10)
Diffusion coefficient (D = 0.01)
Time step size (dt = 0.01)
Number of time steps (nt = 100)
2. Initialize the concentration distribution such that the concentration is 1 in the middle
half of the domain and 0 elsewhere.
3. Implement a time-stepping loop to update the concentration distribution using the
finite difference method.
4. Plot the final concentration distribution along the length of the domain.
Include comments in your code to explain each step.
Theory:
Mass transfer in a medium involves the movement of particles from regions of high
concentration to regions of low concentration. This process is described by Fick's laws
of diffusion, which form the foundation for understanding and modeling diffusion
processes.
Fick's First Law describes the flux of particles due to diffusion. It states that the
diffusion flux is proportional to the negative gradient of the concentration:
∂C
J=−D ∂x
Where;
J is the diffusion flux (amount of substance per unit area per unit time),
D is the diffusion coefficient,
C is the concentration,
∂C
is the concentration gradient.
∂x
This law applies to steady-state diffusion, where the concentration gradient does not
change over time.
Fick's Second Law extends the first law to account for time-dependent changes in
concentration. It is derived from the first law and the principle of mass conservation. In
one dimension, it is expressed as:
∂C ∂2 𝐶
= ∂𝑥 2
∂t
Where;
∂C
is the rate of change of concentration with time,
∂t
∂2 𝐶
is the second spatial derivative of concentration.
∂𝑥 2
This partial differential equation (PDE) is fundamental for modeling transient diffusion
processes, where the concentration gradient and flux change over time.
Spatial Discretization
The spatial domain is divided into nx points with a uniform spacing Δx:
𝑙𝑒𝑛𝑔𝑡ℎ
Δx= 𝑛𝑥−1
Let Cin represents the concentration at the i-th spatial point and n-th time step. The
second spatial derivative can be approximated using the central difference formula:
𝑛
∂2 𝐶 𝐶𝑖−1 − 2𝐶𝑖𝑛 + 𝐶𝑖+1
𝑛
≈
∂𝑥 2 (Δ𝑥)2
Temporal Discretization
Δ𝑡 = 𝑑𝑡
∂𝐶 𝐶𝑖𝑛+1 − 𝐶𝑖𝑛
≈
∂𝑡 Δ𝑡
𝐶𝑖𝑛+1 − 𝐶𝑖𝑛 𝑛
𝐶𝑖−1 − 2𝐶𝑖𝑛 + 𝐶𝑖+1
𝑛
=𝐷
Δ𝑡 (Δ𝑥)2
For the simulation, we define the initial concentration distribution and boundary
conditions:
Source Code:
import numpy as np
import matplotlib.pyplot as plt
# Defining Parameters
length = 1.0 #Length of the domain
nx = 10 #Number of spatial points
dx = length / (nx - 1) #Spatial step size
D = 0.01 #Diffusion coefficient
dt = 0.01 #Time step size
nt = 100 #Number of time steps
Conclusion:
1D mass transfer using the finite difference method was simulated.
#Lab 4
Write a Python program to simulate heat conduction in a 1D rod using the finite
difference method. The program should:
1. Define the following parameters:
Thermal diffusivity (a = 110)
Length of the rod (length = 50 mm)
Total simulation time (time = 4 seconds)
Number of spatial nodes (nodes = 20)
2. Initialize the temperature distribution such that the entire rod is initially at 20°C.
3. Set the boundary conditions such that the temperature at both ends of the rod is
100°C.
4. Calculate the spatial step size (dx) and the time step size (dt) using the given
formulas.
5. Implement a time-stepping loop to update the temperature distribution using the finite
difference method.
6. Visualize the temperature distribution using a color plot that updates in real-time as
the simulation progresses.
Theory:
Heat conduction in a medium involves the transfer of thermal energy from regions of
high temperature to regions of low temperature. This process is described by the heat
equation, which in one dimension (1D) can be expressed as:
∂T ∂2 𝑇
= a ∂𝑥 2
∂t
Where;
T is the temperature.
t is time.
x is the spatial coordinate.
a is the thermal diffusivity of the material.
Spatial Discretization
The spatial domain (length of the rod) is divided into nodes points with a uniform
spacing Δx:
𝑙𝑒𝑛𝑔𝑡ℎ
Δx= 𝑛𝑜𝑑𝑒𝑠−1
Let 𝑇𝑖𝑛 represent the temperature at the 𝑖-th spatial point and 𝑛-th time step. The
second spatial derivative can be approximated using the central difference formula:
𝑛
∂2 𝑇 𝑇𝑖−1 − 2𝑇𝑖𝑛 + 𝑇𝑖+1
𝑛
≈
∂𝑥 2 (Δ𝑥)2
Temporal Discretization
Time is also divided into discrete steps of size Δ𝑡 :
Δ𝑥 2
Δ𝑡 =
2𝑎
Using the forward difference approximation for the time derivative:
∂𝑇 𝑇𝑖𝑛+1 − 𝑇𝑖𝑛
≈
∂𝑡 Δ𝑡
Combining the Approximations
Substituting these approximations into the heat equation, we get:
𝑇𝑖𝑛+1 − 𝑇𝑖𝑛 𝑛
𝑇𝑖−1 − 2𝑇𝑖𝑛 + 𝑇𝑖+1
𝑛
=𝑎
Δ𝑡 (Δ𝑥)2
Rearranging to solve for 𝑇𝑖𝑛+1 :
𝑎Δ𝑡
𝑇𝑖𝑛+1 = 𝑇𝑖𝑛 + (𝑇 𝑛 − 2𝑇𝑖𝑛 + 𝑇𝑖+1
𝑛
)
(Δ𝑥)2 𝑖−1
This equation is used to update the temperature at each time step.
For the simulation, we define the initial temperature distribution and boundary
conditions:
Source Code:
import numpy as np
import matplotlib.pyplot as plt
# Initialization
dx = length / nodes
dt = 0.5 * dx**2 / a
t_nodes = int(time/dt)
u = np.zeros(nodes) + 20 # Plate is initially at 20 degree C
# Boundary Conditions
u[0] = 100
u[-1] = 100
# Simulating
counter = 0
while counter < time :
w = u.copy()
PV= nRT
Where;
Source Code:
from pint import UnitRegistry
ur= UnitRegistry()
T= 284*ur.kelvin
Rgas = 8.314*ur.joule/ur.kelvin/ur.mole
n= 2* ur.mole
V= 22.4*ur.liter
P= n*Rgas*T/V
print('pressure is : ')
print(P.to(ur.psi))
Result:
pressure is :
30.576752269898606 pound_force_per_square_inch
Conclusion:
The gas pressure was calculated using Ideal Gas Law with unit conversion.
#Lab 2.2
Calculation of the Compressibility Factor of Methane Using the Peng-Robinson
Equation of State.
Theory:
The compressibility factor (Z) is a measure of how much the behavior of a real gas
deviates from the ideal gas law. It is defined as:
𝑃𝑉𝑚
𝑍=
𝑅𝑇
Where;
Where:
Where;
𝑇
𝑇𝑟 is the reduced temperature (𝑇𝑟 = 𝑇 ),
𝑐
𝑍 3 − (1 − 𝐵)𝑍 2 + (𝐴 − 2𝐵 − 3𝐵 2 )𝑍 − (A𝐵 − 𝐵 2 − 𝐵 3 ) = 0
Where;
𝑎𝛼𝑃
𝐴=
𝑅2𝑇 2
𝑏𝑃
𝐵=
𝑅𝑇
By solving this cubic equation for 𝑍 using numerical methods (such as the fsolve
function from the scipy.optimize library), we can determine the compressibility factor
of methane, which provides insights into its non-ideal behavior at given conditions.
Source Code:
import numpy as np
#constants
TR = 273.15
TC = -82.59 + TR
PC = 45.99
omega = 0.011
R= 8.31472e-5
#calculate parameters
b= R*TC/PC
P = 65.0
T = 298.0
Tr = T/TC
alpha = (1+k*(1-np.sqrt(Tr)))**2
A= a*alpha*P/(R**2/T**2)
B= b*P/(R*T)
def equation(z):
f, = fsolve(equation, 0.8)
Results:
Compressibility factor of methane is 0.9.
Conclusion:
The compressibility factor of Methane was calculated using the Peng-Robinson
Equation of State.