0% found this document useful (0 votes)
5 views12 pages

Labreport

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Labreport

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

#Lab 3

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 of Diffusion

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 of Diffusion

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.

Finite Difference Method

The finite difference method is a numerical technique used to approximate solutions to


differential equations by discretizing them in space and 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

Time is divided into discrete steps of size Δt\Delta tΔt:

Δ𝑡 = 𝑑𝑡

Using the forward difference approximation for the time derivative:

∂𝐶 𝐶𝑖𝑛+1 − 𝐶𝑖𝑛

∂𝑡 Δ𝑡

Combining the Approximations

Substituting these approximations into Fick's Second Law, we get,

𝐶𝑖𝑛+1 − 𝐶𝑖𝑛 𝑛
𝐶𝑖−1 − 2𝐶𝑖𝑛 + 𝐶𝑖+1
𝑛
=𝐷
Δ𝑡 (Δ𝑥)2

Rearranging to solve for 𝐶𝑖𝑛+1 :


𝐷Δ𝑡
𝐶𝑖𝑛+1 = 𝐶𝑖𝑛 + (𝐶 𝑛 − 2𝐶𝑖𝑛 + 𝐶𝑖+1
𝑛
)
(Δ𝑥)2 𝑖−1

This equation is used to update the concentration at each time step.

Initial and Boundary Conditions

For the simulation, we define the initial concentration distribution and boundary
conditions:

 Initial condition: Concentration is 1 in the middle half of the domain and 0


elsewhere.
 Boundary conditions: Assume zero flux at the boundaries, meaning the
concentration gradient at the boundaries is zero.

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

# Initial concentration distribution


C = np.zeros(nx)
C[int(nx/4):int(3*nx/4)] = 1 #Initial condition
# Time-stepping loop
for n in range(nt):
C_new = C.copy()
for i in range(1, nx-1):
C_new[i] = C[i] + D * dt / dx**2 * (C[i+1] - 2*C[i] + C[i-1])
C = C_new
# Plotting the results
plt.plot(np.linspace(0, length, nx), C)
plt.xlabel('Position')
plt.ylabel('Concentration')
plt.title('1D Mass Transfer')
plt.show()

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.

Finite Difference Method

The finite difference method is a numerical technique used to approximate solutions


to differential equations by discretizing them in space and time.

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.

Initial and Boundary Conditions

For the simulation, we define the initial temperature distribution and boundary
conditions:

 Initial condition: The entire rod is initially at 20°C.


 Boundary conditions: The temperature at both ends of the rod is held constant
at 100°C.

Source Code:
import numpy as np
import matplotlib.pyplot as plt

# Defining our problem


a = 110 #thermal conductivity
length = 50 #length of of rod,mm
time = 4 #total simulation time,seconds
nodes = 20 #number of nodes

# 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

# Visualizing with a plot


fig, axis = plt.subplots()
pcm = axis.pcolormesh([u], cmap=plt.cm.jet, vmin=0, vmax=100)
plt.colorbar(pcm, ax=axis)
axis.set_ylim([-2, 3])

# Simulating
counter = 0
while counter < time :
w = u.copy()

for i in range(1, nodes - 1):


u[i] = dt * a * (w[i - 1] - 2 * w[i] + w[i + 1]) / dx ** 2 + w[i]
counter += dt
print("t: {:.3f} [s], Average temperature: {:.2f}
Celcius".format(counter,np.average(u)))
# Updating the plot
pcm.set_array([u])
axis.set_title("Distribution at t: {:.3f} [s].".format(counter))
plt.pause(0.01)
plt.show()
Conclusion:
Heat conduction in a 1D rod using the finite difference method was simulated.
#Lab2.1
Calculation of Gas Pressure Using the Ideal Gas Law with Unit Conversion
Theory:
The ideal gas law is the equation of state of a hypothetical ideal gas. It assumes that the
gas behaves ideally, meaning that interactions between gas molecules are negligible,
and the volume of the gas molecules themselves is much smaller than the volume of
the container. It describes the relationship between the pressure, volume, temperature,
and the amount of a gas. It is expressed as:

PV= nRT

Where;

o P is the pressure of the gas.


o V is the volume occupied by the gas.
o n is the number of moles of the gas.
o R is the universal gas constant.
o T is the absolute temperature in Kelvin.

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;

 𝑃 is the pressure of the gas,


 𝑉𝑚 is the molar volume,
 𝑅 is the universal gas constant,
 𝑇 is the temperature.
For real gases, deviations from ideal behavior are significant, and these deviations can
be described using equations of state. One widely used equation of state is the Peng-
Robinson equation of state, which is especially useful for calculating the properties of
gases and liquids under a range of conditions.
The Peng-Robinson equation of state is given by:
𝑅𝑇 𝑎𝛼
𝑃= −
𝑉𝑚 − 𝑏 (𝑉𝑚 ^2 + 2𝑏𝑉𝑚 − 𝑏^2)
𝑅 2 𝑇𝑐2
𝑎 = 0.457235
𝑃𝑐
𝑅𝑇𝑐
𝑏=
𝑃𝑐

Where:

 𝑇𝑐 is the critical temperature,


 𝑃𝑐 is the critical pressure,
 𝑅 is the universal gas constant.
The temperature-dependent factor 𝛼 is calculated as:
2
𝛼 = (1 + 𝑘 ⋅ (1 − √𝑇𝑟 ))

Where;
𝑇
 𝑇𝑟 is the reduced temperature (𝑇𝑟 = 𝑇 ),
𝑐

 𝑘 is a parameter dependent on the acentric factor (𝜔).


𝑘 =0.37464 + 1.54226ω − 0.26922ω^2
The Peng-Robinson state of equation can be rearranged into a cubic equation in terms
of the compressibility factor, 𝑍 :

𝑍 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

from scipy.optimize import fsolve

#constants

TR = 273.15

TC = -82.59 + TR

PC = 45.99

omega = 0.011

k = 0.37464+ 1.54226*omega - 0.26922*omega**2

R= 8.31472e-5

#calculate parameters

b= R*TC/PC

P = 65.0

T = 298.0

Tr = T/TC

a = 0.457235 * R**2 *TC**2/PC

alpha = (1+k*(1-np.sqrt(Tr)))**2
A= a*alpha*P/(R**2/T**2)

B= b*P/(R*T)

#solve for compressibility factor using fsolve

def equation(z):

return z**3 - (1-B)*z**2+(A-2 * B-3* B**2)*z-(A*B - B**2 - B**3)

f, = fsolve(equation, 0.8)

print("Compressibility factor of methane is ", round(f,2))

Results:
Compressibility factor of methane is 0.9.
Conclusion:
The compressibility factor of Methane was calculated using the Peng-Robinson
Equation of State.

You might also like