You are on page 1of 21

Batch - 8

Department of Mechanical Engineering


MVGR College of Engineering(A)

Computational Fluid Dynamics(CFD)

Project-1 Evaluation

Comparative Analysis of 2D Transient Heat


Conduction using different Discretization Methods

Reg. No: 20331A03E2

Component of Marks
Assessment
CFD Modeling (10)

Development of CFD code (10)

Report (10)

Total Marks

Signature of Internal Examiner Signature of External Examiner


Batch – 8

ABSTRACT

Transient heat conduction is a type of heat transfer in which the temperature of the
medium is not constant with time. This contrasts with steady-state heat conduction, in which
the temperature of the medium does not change with time. Transient heat conduction is used
in a variety of applications, including building energy modelling, solar thermal systems,
industrial processes, medical devices, and fire safety. In this analysis, the Jacobian iterative
method is used to solve the discretized governing equations of transient heat conduction. Three
discretization methods are used: Crank-Nicolson, FTCS (forward time centre space), and fully
implicit. The methods are compared for accuracy. The 2D wall is discretized into a finite
number of nodes, and the grid sizes are varied while the boundary conditions are kept constant,
including the isothermal condition at the walls. The absolute value is used to validate the results
with previously obtained (ref.01) solutions. The results show that the Crank-Nicolson method is
the most accurate method for this problem. The fully implicit method is the least accurate
method, but it is the most computationally efficient. This study provides a better understanding
of the transient heat conduction problem and the different methods that can be used to solve it.
The results can be used to improve the design and performance of systems that involve heat
transfer.

Abbreviations:

T-Top Temperature of top of wall.


T-Bottom Temperature of bottom of wall.
T-Left Temperature of left of wall
T-Right Temperature of right of wall
T Temperature distribution within the wall.
t Time.
k Thermal conductivity (constant).
∇ Gradient operator (del operator), which takes the spatial derivatives.

Time derivative of temperature T.

Partial derivative of temperature T with respect to x.

Partial derivative of temperature T with respect to y.

Keywords: Heat transfer coefficient, Transient temperature, Thermal conductivity, Transient heat
conduction equation.

2
Batch – 8

1. INTRODUCTION

Computational Fluid Dynamics (CFD) is a numerical simulation technique used to study the
behavior of fluids and heat transfer in various engineering and scientific applications. One of
the important aspects of CFD is the analysis of heat conduction, which involves the study of
heat transfer through solid materials. In this introduction, we will focus on the CFD analysis of
heat conduction through a 2D wall. A 2D wall refers to a two-dimensional surface that has
length and width but negligible thickness in comparison to its other dimensions. This
simplification is often valid when studying heat transfer through thin materials, such as plates
or walls. The governing equation for heat conduction in a 2D wall is the 2D heat conduction
equation, also known as the 2D Laplace equation or the 2D steady-state heat equation. In
Cartesian coordinates (x, y). Applications of CFD analysis of heat conduction in 2D walls are
abundant in various fields, including building thermal analysis, electronics cooling, material
processing, and aerospace heat shield design.
Applications: CFD (Computational Fluid Dynamics) analysis of 2D steady-state heat
conduction has a wide range of applications in various engineering and scientific fields. Some
of the key applications include: Electronics Cooling: In the electronics industry, CFD is used
to analyze heat conduction in electronic components and systems. It helps design effective
cooling solutions to prevent overheating and ensure optimal performance and reliability of
electronic devices. Building and HVAC Systems: CFD is applied to study heat conduction in
buildings and HVAC (Heating, Ventilation, and Air Conditioning) systems. It aids in
optimizing energy consumption, thermal comfort, and indoor air quality. Thermal Barrier
Coatings: CFD is used to analyze the heat conduction in thermal barrier coatings, which are
applied to high-temperature components like gas turbine blades to protect them from extreme
heat. Nuclear Reactor Analysis: CFD is employed in nuclear engineering to simulate and
analyze the steady-state heat conduction within nuclear reactor cores. This helps in ensuring
the safe and efficient operation of nuclear power plants. Metal Casting and Welding: CFD is
applied in the casting and welding processes to analyze the heat conduction and solidification
of molten metal, helping to optimize casting and welding parameters and reduce defects. Heat
Exchanger Design: CFD is used to design and optimize heat exchangers, which are essential
components in various industrial processes, such as in refrigeration, air conditioning, and
chemical processing. Aerospace Applications: CFD is utilized in the aerospace industry to
study heat conduction in components like aircraft wings, engine components, and heat shields
during re-entry in space missions. Automotive Engineering: CFD plays a significant role in the
automotive industry by analyzing heat conduction in engine components, exhaust systems, and

3
Batch – 8

cooling systems to enhance engine efficiency and reduce emissions. Biomedical Applications:
In biomedical engineering, CFD is used to study heat conduction in biological tissues and to
design medical devices like prosthetics and implants. Solar Energy: CFD is applied to study the
steady-state heat conduction in solar panels and concentrated solar power systems, aiding in the
design and optimization of solar energy technologies. These are just a few examples of the
numerous applications of CFD 2D steady-state heat conduction analysis. The versatility of CFD
makes it a powerful tool in understanding and optimizing heat transfer phenomena in various
engineering and scientific disciplines.
CFD (Computational Fluid Dynamics) analysis of 2D steady-state heat conduction is an
important area in engineering and physics. There are numerous research papers, books, and
academic articles that cover this topic. Here are some key references to literature in this field:
"Introduction to Heat Transfer" by Frank P. Incropera and David P. DeWitt: This classic
textbook covers the fundamentals of heat transfer, including steady-state conduction in 2D. It
provides a strong foundation for understanding the principles of heat transfer and the
mathematical techniques used in CFD analysis. "Numerical Heat Transfer and Fluid Flow" by
Suhas V. Patankar: This book focuses on the numerical methods used for solving heat
conduction problems. It includes finite difference methods, finite element methods, and other
numerical techniques applied to heat transfer and fluid flow. "Computational Heat Transfer" by
Tuncer Cebeci and Peter Bradshaw: This book provides an in-depth understanding of
computational methods applied to heat transfer problems, including steady-state conduction in
2D. It covers both theoretical aspects and practical applications. Research papers from
academic journals: Many researchers have published papers on CFD analysis of 2D steady-
state heat conduction in various engineering and scientific domains. Some key journals include:
 International Journal of Heat and Mass Transfer
 Numerical Heat Transfer, Part B: Fundamentals
 Journal of Computational Physics
 International Journal for Numerical Methods in Engineering
Academic Conferences: Proceedings from conferences such as ASME Heat Transfer
Conference, AIAA Aerospace Sciences Meeting, and others often include papers and
presentations on the latest research in heat conduction using CFD techniques. Open-source
CFD software documentation: OpenFOAM and other open-source CFD software packages
often provide documentation and tutorials related to heat conduction simulations, including 2D
steady-state cases. When searching for research papers, you can use academic databases such
as Google Scholar, IEEE Xplore, or ScienceDirect to find specific articles related to your
interest in CFD analysis of 2D steady-state heat conduction applications.

4
Batch – 8

The scope of CFD (Computational Fluid Dynamics) analysis of 2D steady-state heat conduction
applications is quite extensive and holds significant importance in various fields of engineering
and research. Some key aspects of the scope include: Engineering Design and Optimization:
CFD analysis allows engineers to simulate and analyze heat conduction in various components
and systems, aiding in the design and optimization of thermal management solutions. It helps
identify hotspots, temperature distributions, and areas of potential concern, leading to improved
designs and efficient heat transfer systems. Energy Efficiency and Sustainability: With the
growing emphasis on energy conservation and sustainability, CFD analysis of heat conduction
plays a crucial role in optimizing energy consumption, improving thermal efficiency, and
reducing greenhouse gas emissions in industries like HVAC, electronics, and power generation.
Material Selection and Performance: CFD can assist in the evaluation of material properties
and their impact on heat conduction. Engineers can use CFD simulations to select materials
with appropriate thermal conductivities for specific applications, ensuring better performance
and reliability. Safety and Reliability: Understanding heat conduction patterns is vital for
ensuring the safety and reliability of various systems. For instance, in nuclear reactors,
aerospace components, and industrial equipment, CFD analysis helps predict temperature
distributions and avoid potential failures due to overheating. Process Optimization: CFD
analysis enables process optimization in various manufacturing industries, such as metal
casting, welding, and chemical processes. By studying heat conduction during these processes,
engineers can enhance productivity, reduce defects, and optimize resource utilization.
Microelectronics and Nanotechnology: With the ongoing trend of miniaturization in electronics
and advancements in nanotechnology, heat dissipation becomes critical. CFD analysis helps
design efficient cooling solutions for microchips, integrated circuits, and nanoscale devices.
Environmental Impact Assessment: CFD simulations aid in assessing the environmental impact
of projects and processes by analyzing heat conduction in natural bodies like lakes, rivers, and
the atmosphere. This knowledge is valuable for understanding climate change and
environmental sustainability. Medical Applications: In the medical field, CFD is used to study
heat conduction in biological tissues during therapies like hyperthermia and cryotherapy. It
assists in the design of medical devices for improved patient outcomes. Education and
Research: CFD analysis of heat conduction provides a valuable tool for researchers and
educators to study fundamental heat transfer phenomena and explore new methodologies and
applications. Overall, the scope of CFD analysis of 2D steady-state heat conduction is vast,
encompassing a wide range of industries and research domains. Its applications contribute to
improved design, energy efficiency, safety, and environmental sustainability in various
engineering and scientific disciplines. As computational capabilities continue to advance, CFD

5
Batch – 8

is expected to play an increasingly significant role in tackling complex heat transfer challenges
in the future. Ref: 4-Simscale Blog.

2. Materials and Methods


2.1 Physical model and mathematical formulation
The computational domain 2D transient heat conduction scenario. considering a two-
dimensional domain in which heat is being transferred over time. The physical model can be
represented using the heat conduction equation, also known as the heat diffusion equation. Here
taking the wall with all three sides of similar temperature and fourth side of one temperature.
Considering a 2D domain with coordinates (x, y) and a temperature distribution T(x, y, t) at
each point in the domain, where t is time. To solve the heat conduction equation, we need to
specify initial conditions and boundary conditions. The initial condition is the temperature
distribution at time t = 0, and the boundary conditions describe the temperature at the edges of
the domain. Assumptions: 2 Dimensional, Transient, Temperatures dependent of Time. This
equation states that the sum of the second partial derivatives of temperature with respect to x
and y is equal to zero, indicating that there is no net heat generation or loss within the domain
in the steady-state condition. To solve this equation using CFD methods, it is often transformed
into a discretized form suitable for numerical simulation. Commonly, finite difference methods,
finite element methods, or finite volume methods are employed to solve the discretized form of
the 2D Transient heat conduction equation on a computational grid representing the 2D domain.
Physical Model:

T-Top

T-Bottom
Fig.2.1- Schematic of the Physical model

6
Batch – 8

2.2 Governing Equation:


In CFD analysis of 2D Transient heat conduction, the governing equation is the Transient
heat conduction equation, also known as the Laplace's equation for heat conduction.
Ref.2-Textbook by Steven C. Chapra on Numerical methods for engineers.

The 2D Transient heat conduction equation can be expressed as:

+ = …Eq1

2.3 Boundary Conditions


Boundary temperatures:
T_left = 348k
T_right = 323k
T_top = 373k
T_bottom = 273k

3. Numerical procedure
Numerical procedures for solving 2D transient heat conduction problems involve discretizing the partial
differential equation governing heat transfer over a 2D domain and iteratively solving the resulting
system of equations to approximate the temperature distribution over time. One common approach is
the finite difference method. Here's a step-by-step write-up of the numerical procedure for solving a 2D
transient heat conduction problem using finite differences: Define the problem geometry, initial
conditions, and boundary conditions. Specify the material properties, thermal diffusivity (α), and time
step (ΔΔt). Discretization: Discretize the 2D domain into a grid with nodes representing spatial
locations. Choose appropriate grid spacing (Δx and Δy). Discretize the time domain into time steps.
Update the simulation time (t) at each time step (n) as t=n⋅Δt. Ref.2-Textbook by Steven C. Chapra on Numerical
methods for engineers.

Finite Difference Approximation: Apply the finite difference approximation to the heat
conduction equation to calculate the temperature at the next time step (t + Δt) based on the temperature
at the current time step (t) and the boundary conditions. The most common method is the explicit forward-
time central-space (FTCS) method, which approximates both spatial and temporal derivatives:

𝑇 − 𝑇 𝑇 , − 2𝑇 + 𝑇 , 𝑇, − 2𝑇 + 𝑇 , …Eq2
= 𝛼 +
∆𝑡 ∆𝑥 ∆𝑦

7
Batch – 8

Crank Nicolson Method: The Crank–Nicolson method is a finite difference method used for
numerically solving the heat equation and similar partial differential equations. It is a second-
order method in time. It is implicit in time, can be written as an implicit Runge–Kutta method,
and it is numerically stable.

𝑻𝒏+𝟏 𝒏
𝒊+𝟏,𝒋 + 𝑻𝒊+𝟏,𝒋 𝟐𝑻𝒏+𝟏 𝒏
𝒊,𝒋 + 𝟐𝑻𝒊,𝒋 𝑻𝒏−𝟏 𝒏
𝒊−𝟏,𝒋 + 𝑻𝒊−𝟏,𝒋
𝑇 − 𝑇 − +
𝟐 𝟐 𝟐
= 𝜶
∆𝑡 ∆𝒙 𝟐
…Eq3

𝑻𝒏+𝟏 𝒏
𝒊,𝒋+𝟏 + 𝑻𝒊,𝒋+𝟏 𝟐𝑻𝒏+𝟏 𝒏
𝒊,𝒋 + 𝟐𝑻𝒊,𝒋 𝑻𝒏−𝟏 𝒏
𝒊,𝒋−𝟏 + 𝑻𝒊,𝒋−𝟏
− +
𝟐 𝟐 𝟐
+
∆𝒚𝟐

Fully Implicit Method:


The fully implicit method is a numerical technique used in Computational Fluid Dynamics (CFD) to
solve the governing equations for fluid flow. Unlike explicit methods, which require small time steps for
stability, the fully implicit method can handle larger time steps and is unconditionally stable.
This method involves solving a system of algebraic equations at each time step, considering all terms,
including the time derivatives, implicitly. It is computationally more intensive compared to explicit
methods but allows for greater accuracy and stability, especially for problems with strong shocks or
complex physics.

𝑇 − 𝑇 𝑇 , − 2𝑇 + 𝑇 , 𝑇, − 2𝑇 + 𝑇,
= 𝛼 + …Eq4
∆𝑡 ∆𝑥 ∆𝑦

Boundary Conditions Apply the appropriate boundary conditions to update the boundary nodes at each
time step. Time Loop Iterate through time steps, updating the temperature values at each node based on
the finite difference approximation and boundary conditions. Visualization and Analysis After reaching
the desired simulation time, visualize and analyze the temperature distribution over time. You can create
contour plots or animations to observe the evolution of temperature. Convergence and Stability Check
for numerical stability and convergence of the solution. Adjust the time step or grid spacing if necessary
to ensure stability. Refinement and Optimization Consider using more advanced numerical techniques,
such as implicit methods (e.g., Crank-Nicolson) for improved stability or higher-order finite difference
schemes for accuracy. Ref.2-Textbook by Steven C. Chapra on Numerical methods for engineers.

8
Batch – 8

3. SOLUTION METHODOLOGY:
A python code is generated based on the discretized governing equation and discretized boundary
conditions. The generated python code is run on Google Co-laboratory platform with a maximum of
4,00,000 iterations. In this problem to verify the solution generated by the code, Jacobian iterative
method is preferred to numerically solve this heat conduction equation by discretizing the domain into a
grid or mesh. Generally, different methods are in available to do it. This is the one method among them.
Analytically, it is difficult to calculate the temperatures at different positions with respect to the time -
intervals. To make it possible, a python code (ref.5-G. Nervadof) is prepared to perform this entire solution
methodology. This method considered as an iterative algorithm which is used for determining the
solutions for the system of linear equations in numerical linear algebra.

Input Parameters

Boundary temperatures & Initialization


T [0, :], T [-1,:], T[:, 0], T[:, -1]

For loop (Max iterations)

Temperature Calculation
Iter +1

Iteration > Max iteration

For iterations
<
Desired one

Temperatures Plotted

Fig.3- Indicates the Step-by-step Flow process of execution of the python code

9
Batch – 8

4. Results and discussion


4.1 Grid Independence Study:
Grid independence is a concept in numerical simulations, particularly in computational fluid dynamics
(CFD) and other numerical methods used to solve partial differential equations (PDEs). It refers to the
property of a numerical solution where the results become insensitive to changes in the grid resolution
(i.e., the number of grid points or cell size) while maintaining the same underlying physical problem. In
numerical simulations, the domain of interest is discretized into a grid or mesh of points, and the PDEs
governing the physical phenomena are solved at these grid points. However, it is essential to ensure that
the results obtained are not heavily dependent on the particular grid used but are instead representative
of the true behaviors of the physical system being modelled. Grid independence is typically assessed by
running the simulation with different grid resolutions and comparing the results. The goal is to determine
if the computed solution converges to a consistent and stable solution as the grid is refined (i.e., as the
grid becomes finer with smaller cell sizes) By running the python code for this problem for different
GRID sizes i.e,8×8, 10×10, 12×12, 14×14, 18×18, 20×20, 22×22 with fixed temperatures on 4 sides as
shown in Fig 4.1 the grid independency is reached for below grid sizes. Here the Grid independence
analysis was performed on above mentioned different grids for one method like Fully implicit method

Fig.4.1- Indicates the grid indication of


2d Wall

Fig.4.1- Indicates the grid independence in Fully implicit method at different grids.

10
Batch – 8

Here after the analysis of grid independence, observed from fig 4.1 that taking the grids as above
mentioned. The temperature became constant after a particular point and from there, there is no
temperature change even though the grid size is varied. So taking the 20×20 grid for the validation step
where steady state is attained.

4.2 VALIDATION:
Here the solution graph from the Previously done work ref.1-by shrey shah is taken as the
reference to compare with the present work. Here in reference work author used the different Solution
methods Implicit schema, Jacobi method, Gauss seidel method and compared its iteration time of each
method. So, from this journal although the methods used in present work and research journal are
different but the solution will be same in all the methods, so the present work is done by using the
different Numerical Discretization techniques like FTCS, Crank Nicolson & Fully implicit Method.
Finally, the solution graphs should be same and the Transient temperature state attains the Steady State
for the 2D wall. A rectangular steel plate of 750mm x 500mm with Top side having 600K, while the
Bottom side have 900K temperature and left and right-side having temperatures 400K & 800K is
considered in this research work. The objective is to analyze the interior point temperature (T1,T2,T3
&T4) analytically and numerically and comparing the results, from the journal solution linear curve
graph of Gauss Seidel method Vs the Linear curve graph fig. 4.3.3 extracted from the present work,
According to the theoretical analysis in journal they took the highest temperature so the steady state was
attained at less time step whereas in the present work the considered temperature was less so here in
present work to reach the steady state it took some more time compared to the Journal, except that the
grid sizes and Temperature variation with time was taken at Central Node are taken same as the journal
to Validate the present work with less error. Hence plotted the graph fig.4.2 of reference work to the
present work using temperature variation with time at Vertical Centre line.

1000
Comparison Graph of present & Reference work
900
Temperature at Vertical central line (K)

800
700
600
500
400
300
200
100
0
0 2 4 6 8 10 12 14 16 18 20
Y coordinates of vertical central line
present work Reference work - ref-01

Fig.4.2 indicates the Linear curve graph plotted by comparing the reference work to the present work stating the temperature variation with
time at Vertical Centre line.

11
Batch – 8

Table.4.2: Indicates the Validation of present work with reference work with percentage error

Y Reference work Present Work %error


0 900 900 0

0.1 839.98 840.17 -0.02


0.2 786.32 785.73 0.075
0.3 740.37 739.86 0.068
0.4 703.44 703.14 0.042
0.5 674.19 674.59 -0.059
0.6 651.13 652.61 -0.227
0.7 636.12 635.54 0.09
0.8 622.13 621.91 0.035
0.9 610.4 610.43 -0.004
1.0 600 600 0

12
Batch – 8

4.3 RESULTS AFTER RUNNING THE CODE


Fully Implicit

Fig.4.3.1 shows the grid temperature distribution Fig.4.3.2 shows the 2D temperature distribution
of Fully Implicit Method of Fully Implicit Method

Fig.4.3.3 indicates the graph that the 2d Wall attained steady state condition from
Transient State in Fully Implicit Method

Here using the python code, the above results are obtained and those different plots created using different plot
commands, this process was repeated for several nodes i.e., 8x8, 10x10, 11x11, 12x12, 14x14, 16x16, 18x18, 20x20,
22x22 up to 4,00,000 iterations and the linear curve graph was plotted using the canter node temperatures (K)
with respect to no. of iterations

13
Batch – 8

FTCS Method

Fig.4.3.4 shows the grid temperature Fig.4.3.5 shows the 2D Contour plot
distribution of FTCS Method temperature distribution of FTCS Method

Fig.4.3.6 indicates the graph that the 2d Wall attained steady state condition from
Transient State in FTCS Method
Similarly, as briefed in the above step here same procedure is followed but the method of numerical
discretization is changed in the python code in iteration loop, here used one is FTCS method and
remaining process is same and graph are plotted as followed in Fully implicit method.

14
Batch – 8

Crank Nicolson Method

Fig.4.3.7 shows the grid temperature Fig.4.3.8 shows the 2D temperature distribution of
distribution of Crank Nicolson Method Crank Nicolson Method

Fig.4.3.9 indicates the graph that the 2d Wall attained steady state condition from Transient
State in Crank Nicolson Method

Similarly, as briefed in the above step here same procedure is followed but the method of numerical
discretization is changed in the python code in iteration loop, here used one is Crank Nicolson method
and remaining process is same for grid sizes and graph are plotted as followed in Fully implicit method

15
Batch – 8

Comparison of 3 different Discretization Solvers


Crank Nicolson

No. of Iterations Fully implicit

FTCS

Discretization Methods

5.CONCLUSION
In this project, 2-D transient heat conduction equation is solved. In this analysis, the Jacobian
iterative method is used to solve the discretized governing equations of transient heat conduction. The
2D wall is discretized into a finite number of nodes, and the grid sizes are varied while the boundary
conditions are kept constant, including the isothermal condition at the walls. PDE’s are converted into
algebraic equations by using numerical methods like FTCS, Crank Nicolson, Fully Implicit method.
Three discretization methods are used: Crank-Nicolson, FTCS (forward time center space), and fully
implicit. The methods are compared for accuracy. Mathematical modelling and Numerical discretization
was done using three different methods are FTCS, Fully Implicit, Crank Nicolson method. For different
Grids Sizes like 8x8, 10x10, 12x12, 14x14, 18x18, 20x20, 22x22. After making the Boundary conditions.
The developed equations are used to obtain temperature distribution over a specific period of time at a
central node. Then the solution methodology was performed using the Jacobian Iterative method for
different grid sizes. Then Grid independence study was done using different grid sizes and proved that
the temperature distribution over the Wall is independent of the grid size. From the analysis of 2D
transient heat conduction the temperature remained constant although the grid sizes are varied. The
absolute value is used to validate the results with previously obtained solutions. Here comes the
validation step where the present work is done is compared with the previously done work by shrey shah
and calculated the %error represented using the graph with present work vs reference work ref.no-1.
Finally shown that a 2d wall attained a Steady state condition from the Transient State.

16
Batch – 8

REFERENCES
01. https://skill-lync.com/student-projects/Solving-the-2-D-steady-and-unsteady-heat-conduction-equation-using-
finite-difference-explicit-and-implicit-iteration

02. Textbook of “Numerical Methods for Engineers “by Steven C. chapra & Raymond P.
Canale.

03.https://skill-lync.com/student-projects/week-5-1-mid-term-project-solving-the-steady-and-unsteady-2d-heat-
conduction-problem

04. https://www.simscale.com/blog/what-everybody-ought-to-know-about-cfd/

05.https://levelup.gitconnected.com/solving-2d-heat-equation-numerically-using-python-
3334004aa01a

**APPENDIX**
Fully implicit
#fully implicit
import numpy as np
import matplotlib.pyplot as plt

# Parameters
Lx = 1 # Length of the domain in the x-direction
Ly = 1 # Length of the domain in the y-direction
nx = 11 # Number of grid points in the x-direction
ny = 11 # Number of grid points in the x-direction
nt = 100 # Number of grid points in the t-direction
dx = Lx / (nx-1) # Grid spacing in the x-direction
dy = Ly / (ny-1) # Grid spacing in the x-direction
dt = 0.01 # Grid spacing in the t-direction

# Boundary temperatures
T_left = 348
T_right = 323
T_top = 373
T_bottom = 273

# Initialization

17
Batch – 8

X, Y = np.meshgrid(np.linspace(0, Lx, nx), np.linspace(0, Ly, ny))


T = np.zeros((ny, nx))
T[0, :] = T_bottom
T[-1, :] = T_top
T[:, 0] = T_left
T[:, -1] = T_right

# Parameter
alpha = 0.0001
r = alpha*dt/(dx**2)
s = alpha*dt/(dy**2)

max_iter = 400000
tolerance = 1e-10

file_path='temperature_data_cn_9x9.txt'
file=open(file_path,'w')

# Jacobi iterative method

for iteration in range(max_iter):


T_new = T.copy()
for j in range(1, ny-1):
for i in range(1, nx-1):
T_new[i,j] = T[i,j]+r*(T_new[i+1,j]-2*T_new[i,j]+T_new[i-1,j])+s*(T_new[i,j+1]-2*T_new[i,j]+T_new[i,j-1])
T_new[0, :] = T_bottom
T_new[-1, :] = T_top
T_new[:, 0] = T_left
T_new[:, -1] = T_right
T = T_new

file.write("# Position (m) Temperature (k)\n")


for i in range(nx):
for j in range(ny):
file.write(f" {T[i,j]: .2f},\n")
file.close()

# Plotting the temperature distribution as contours


plt.contourf(X, Y, T, cmap='viridis')
plt.colorbar(label='Temperature (K)')
plt.xlabel('X (m)')
plt.ylabel('Y (m)')
plt.title('Temperature Contours in a 2D Domain')

18
Batch – 8

plt.show()

FTCS Method
#ftcs
import numpy as np
import matplotlib.pyplot as plt

# Parameters
Lx = 1 # Length of the domain in the x-direction
Ly = 1 # Length of the domain in the y-direction
nx = 11 # Number of grid points in the x-direction
ny = 11 # Number of grid points in the x-direction
nt = 100 # Number of grid points in the t-direction
dx = Lx / (nx-1) # Grid spacing in the x-direction
dy = Ly / (ny-1) # Grid spacing in the x-direction
dt = 0.01 # Grid spacing in the t-direction

# Boundary temperatures
T_left = 348
T_right = 323
T_top = 373
T_bottom = 273
# Initialization
X, Y = np.meshgrid(np.linspace(0, Lx, nx), np.linspace(0, Ly, ny))
T = np.zeros((ny, nx))
T[0, :] = T_bottom
T[-1, :] = T_top
T[:, 0] = T_left
T[:, -1] = T_right

# Parameter
alpha = 0.0001
r = alpha*dt/(dx**2)
s = alpha*dt/(dy**2)

max_iter = 400000
tolerance = 1e-10

file_path='temperature_data_cn_9x9.txt'
file=open(file_path,'w')

# Jacobi iterative method

19
Batch – 8

for iteration in range(max_iter):


T_new = T.copy()
for j in range(1, ny-1):
for i in range(1, nx-1):
T_new[i,j] = T[i,j]+r*(T[i+1,j]-2*T[i,j]+T[i-1,j])+s*(T[i,j+1]-2*T[i,j]+T[i,j-1])
T_new[0, :] = T_bottom
T_new[-1, :] = T_top
T_new[:, 0] = T_left
T_new[:, -1] = T_right
T = T_new

file.write("# Position (m) Temperature (k)\n")


for i in range(nx):
for j in range(ny):
file.write(f"{T[i,j]: .2f},\n")
file.close()

# Plotting the temperature distribution as contours


plt.contourf(X, Y, T, cmap='viridis')
plt.colorbar(label='Temperature (K)')
plt.xlabel('X (m)')
plt.ylabel('Y (m)')
plt.title('Temperature Contours in a 2D Domain')
plt.show()

Crank Nicolson Method


#crank nicolson
import numpy as np
import matplotlib.pyplot as plt

# Parameters
Lx = 1 # Length of the domain in the x-direction
Ly = 1 # Length of the domain in the y-direction
nx = 11 # Number of grid points in the x-direction
ny = 11 # Number of grid points in the x-direction
dx = Lx / (nx-1) # Grid spacing in the x-direction
dy = Ly / (ny-1) # Grid spacing in the x-direction
dt = 0.01 # Time step

# Boundary temperatures
T_left = 348

20
Batch – 8

T_right = 323
T_top = 373
T_bottom = 273

# Initialization
X, Y = np.meshgrid(np.linspace(0, Lx, nx), np.linspace(0, Ly, ny))
T = np.zeros((ny, nx))
T[0, :] = T_bottom
T[-1, :] = T_top
T[:, 0] = T_left
T[:, -1] = T_right

# Parameter
alpha = 0.0001
r = alpha*dt/(dx**2)
s = alpha*dt/(dy**2)

max_iter = 400000
tolerance = 1e-10

for iteration in range(max_iter):


T_new = T.copy()
for j in range(1, ny-1):
for i in range(1, nx-1):
T_new[i,j] = T[i,j] + r * (((T_new[i+1,j]+T[i+1,j])/2) - 2*((T_new[i,j]+T[i,j])/2) + ((T_new[i-1,j]+T[i-
1,j])/2)) + \
s * (((T_new[i,j+1]+T[i,j+1])/2) - 2*((T_new[i,j]+T[i,j])/2) + ((T_new[i,j-1]+T[i,j-1])/2))
T_new[0, :] = T_bottom
T_new[-1, :] = T_top
T_new[:, 0] = T_left
T_new[:, -1] = T_right
T = T_new

# Plotting the temperature distribution as contours


plt.contourf(X, Y, T, cmap='viridis')
plt.colorbar(label='Temperature (K)')
plt.xlabel('X (m)')
plt.ylabel('Y (m)')
plt.title('Temperature Contours in a 2D Domain')
plt.show()

21

You might also like