How Can I Optimize My 2D Cylindrical Heat Equation Program?

In summary, if you want to make your program run faster for n = 400, you can try using vectorized operations, optimizing your code, and implementing parallelization techniques.
  • #1
range.rover
16
0
Hello friends, I am new for numerical methods and programming. i have been trying to devolop a program in 2D poisson heat equation in cylinder (r,angle) by finite difference method
2u/∂r2 + 1/r * ∂u/∂r + 1/r2 * ∂2u/∂θ2 = Q(u,θ)discritized equation :-

ui+1,j − 2uij + ui−1,j/(∆r)2 + 1/ri * ui+1,j − ui−1,j/2∆r +1/r2i * ui,j+1−2uij +ui,j−1/(∆θ)2 = Q(u,θ)

i have been trying to develop this program, can anybody help me in making it to run faster for n = 400;

clear all
close all
clc
tic
n = 5; % mesh size
s = n-2;
r = linspace(1,5,n)-1;

angle =((1:1:n)-1)/(n)*2*pi;

m = (s*n); %number of nodesdr = r(3)-r(2); % distance between radius

dangle = angle(3)- angle(2); % change of angle between radius

Q = 20*dr^2; % heat sourse

% right hand side Matrix

rhs = zeros(m+1,1);
rhs( : ) = Q;
bou = ones(m+1,1);

% boundary values
bou( : ) = 0;
tic
for i = n-1:n-2:m+1
rhs(i,1) = rhs(i,1) + bou(i);
end
toc% evaluating the co effitiants of tridiagonal sparse matrix

tic
for i = 2:n-1
a(i) = 2*(1 + ((dr^2)/(r(i)*dangle)^2));
b(i) = 1+(1/(2*r(i)));
c(i) = 1-(1/(2*r(i)));
d(i) = dr^2/(r(i)^2*dangle^2);
end
toc
% building up the matrix for A.x = B

f = c(2);
I = speye(n);
a = (a(2:end))';
b = [0;b(2:n-2)'];
p = [c(3:end)';0];
% d = (d(2:end))';
T = spdiags([p -1*a b],[-1 0 1],n-2,n-2);
oops = full(T);
FirstM = kron(I,T);
% z = full(FirstM);
d = (d(2:end));
e = repmat(d,1,n-1);
tic
for i = 1: (m-s)
FirstM(i,(i+s)) = e(i);

FirstM((i+s),i) = e(i);
end
toc
tic
for i = 1: s
FirstM((m-s+i),i) = e(i);
FirstM(i,(m-s+i)) = e(i);
end
toc
% z = full(FirstM);

FirstM(1,m+1) = 0;
FirstM(m+1,1) = 0;
tic
for i = 1:n-2:m+1
FirstM(i,m+1) = f;
FirstM(m+1,i) = 1;
end
toc
FirstM(m+1,m+1) = -4;
z = full(FirstM);

% solving A*x = B for solution
Tsol = abs(mldivide(FirstM,rhs));
toc
T1soln = Tsol(1:m,1);
Tsoln = reshape(Tsol,n-1,n-1);
% Tsoln = Tsoln';% Tsoln = reshape(T1soln,n,s);
% angle = angle(1,2:n);
% r = r(1,2:n);
[x,y] = meshgrid(r,angle);
[xs,ys] = pol2cart(x,y);
surf(xs,ys,Tsoln,'EdgeColor','none').
 
Last edited:
Physics news on Phys.org
  • #2
I can suggest some methods to help you make the program run faster. Firstly, you should try to use vectorized operations instead of using for loops whenever possible. Secondly, you can try to optimize your code by avoiding unnecessary computations or data accesses. Finally, you can try to use parallelization techniques such as OpenMP or MPI to make your program run faster.
 

FAQ: How Can I Optimize My 2D Cylindrical Heat Equation Program?

What is the 2D cylindrical heat equation?

The 2D cylindrical heat equation is a mathematical model used to describe the transfer of heat in a cylindrical object, such as a pipe or a rod. It takes into account the temperature distribution in both the radial and axial directions.

What are the variables involved in the 2D cylindrical heat equation?

The variables involved in the 2D cylindrical heat equation are temperature (T), time (t), radial distance (r), and axial distance (z). These variables are used to describe the temperature distribution at any point in the cylindrical object over time.

What are the boundary conditions for the 2D cylindrical heat equation?

The boundary conditions for the 2D cylindrical heat equation depend on the specific problem being solved. However, in general, they include the initial temperature distribution, the temperature at the boundaries of the cylindrical object, and any external sources or sinks of heat.

What is the importance of the 2D cylindrical heat equation?

The 2D cylindrical heat equation is an important tool for understanding heat transfer in cylindrical objects, which has many practical applications in fields such as engineering, physics, and materials science. It allows for the prediction and control of temperature distribution, which is crucial for the design and operation of various systems and processes.

How is the 2D cylindrical heat equation solved?

The 2D cylindrical heat equation is typically solved using numerical methods, such as finite difference or finite element methods. These methods involve discretizing the problem into smaller parts and using iterative calculations to approximate the temperature distribution over time. Advanced techniques, such as analytical solutions, can also be used for simplified or idealized cases.

Back
Top