Project

Compare fastest steady state simulation vs fastest transient simulation 

Objective-To compare fastest steady state simulation vs fastest transient simulation

Steady State Matlab code:

%main program
% steady state 2D heat conduction
close all
clear all
clc
nx=20;
ny=20;
tol=1e-4;
% iterative_solver=1   (steady state jacobi)
% iterative_solver=2    (steady state gauss seidal)
% iterative_solver=3     (steady state SOR)

iterative_solver=input('mention the solver number?');

if iterative_solver==1 

steady_state_jacobi(nx,ny,tol);

elseif iterative_solver==2

steady_state_gs(nx,ny,tol);

elseif iterative_solver==3
    
steady_state_SOR(nx,ny,tol);

end
function steady_state_jacobi(nx,ny,tol)
tic
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
dy=y(2)-y(1);
T=100*ones(nx,ny);
T(:,1)=400;
T(:,nx)=800;
T(1,:)=900;
T(ny,:)=600;
Told=T;
k1=0.5*(dy^2/(dy^2+dx^2));
k2=0.5*(dx^2/(dy^2+dx^2));

error=9e9;

    jacobi=1;
    while(error>tol)
      for j=2:ny-1  
    for i=2:nx-1
      T(i,j)=k1*(Told(i-1,j)+Told(i+1,j))+k2*(Told(i,j-1)+Told(i,j+1));
    end
      end
    error=max(max(abs(Told-T)));
    Told=T;
    jacobi=jacobi+1;
    steady_jacobi_time=toc
    contourf(x,y,T);
    [C,h]=contourf(x,y,T);
    clabel(C,h);
  
    xlabel('X axis')
    ylabel('Y axis')
    title_text=sprintf("jacobi iteration number=%d ,time of simulation=%gs",jacobi,steady_jacobi_time);
    title(title_text);
    colorbar
    
      pause(0.03)
    
    end
    
    
    
end
function steady_state_gs(nx,ny,tol)
tic
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
dy=y(2)-y(1);
T=100*ones(nx,ny);
T(:,1)=400;
T(:,nx)=800;
T(1,:)=900;
T(ny,:)=600;
Told=T;
k1=0.5*(dy^2/(dy^2+dx^2));
k2=0.5*(dx^2/(dy^2+dx^2));

error=9e9;

    gs_iter=1;
    while(error>tol)
      for j=2:ny-1  
    for i=2:nx-1
      T(i,j)=k1*(T(i-1,j)+Told(i+1,j))+k2*(T(i,j-1)+Told(i,j+1));
    end
      end
    error=max(max(abs(Told-T)));
    Told=T;
    gs_iter=gs_iter+1;
    steady_gs_time=toc
     contourf(x,y,T);
   
    [C,h]=contourf(x,y,T);
        clabel(C,h);
          
    xlabel('X axis')
    ylabel('Y axis')
    title_text=sprintf("Gauss iteration number=%d ,time of simulation=%gs",gs_iter,steady_gs_time);
    title(title_text);
    colorbar
    pause(0.03)
    
    
    end
    
      
end
function steady_state_SOR(nx,ny,tol)
tic
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
dy=y(2)-y(1);
T=100*ones(nx,ny);
T(:,1)=400;
T(:,nx)=800;
T(1,:)=900;
T(ny,:)=600;
Told=T;
k1=0.5*(dy^2/(dy^2+dx^2));
k2=0.5*(dx^2/(dy^2+dx^2));

error=9e9;
w=1.5;

    SOR_iter=1;
    while(error>tol)
      for j=2:ny-1  
    for i=2:nx-1
      Tgauss(i,j)=k1*(T(i-1,j)+Told(i+1,j))+k2*(T(i,j-1)+Told(i,j+1));
      T(i,j)=(1-w)*Told(i,j)+w*Tgauss(i,j);
    end
      end
    error=max(max(abs(Told-T)));
    Told=T;
    SOR_iter=SOR_iter+1;
    steady_sor_time=toc
    contourf(x,y,T);
   
    [C,h]=contourf(x,y,T);
        clabel(C,h);
          
    xlabel('X axis')
    ylabel('Y axis')
    title_text=sprintf("SOR iteration number=%d ,time of simulation=%gs",SOR_iter,steady_sor_time);
    title(title_text);
    colorbar
      pause(0.03)
    
    
    end
    
end

Output:

Steady state simulation using jacobi

Steady state simulation using Gauss Seidal

Steady state simulation using jacobi SOR

Inference-For steady state analysis of 2D heat equation 20 grid points were taken.

Steady state jacobi- No. of iterations-858 Time of simulation -172.34 s

Steady state Gauss seidal- No. of iterations-456 Time of simulation -93.8 s

Steady state SOR- No. of iterations-160 Time of simulation -34.6 s

From the results it was seen that Jacobi iterative solver took the highest time to converge to the solution followed by gauss seidal and SOR.

SOR iterative method was almost 6 times faster than jacobi method.This is mainly due to introduction of ralaxation factor(`w`). If `w<1`it is called under-relaxation and `w>1` is called over-relaxation.

The relaxation factor used was 1.5(over-relaxation) in the simulation.However w>1.5 increased the no of iteration and below 1.5 also increased the no of iterations.So optimum w should be used for faster convergence.

Transient Matlab code:

% main function
% Transient 2D heat conduction
close all
clear all
clc
nx=20;
ny=20;
tol=1e-4;
dt=0.0004;
% solver=1  (Transient explicit scheme)
% solver=2  (Transient Implicit scheme using jacobi)    
% solver=3  (Transient Implicit scheme using gauss siedal) 
% solver=4  (Transient Implicit scheme using SOR) 
 
solver=input('Mention solver number?');
if solver==1
    
transient_conduction_explicit(nx,ny,dt)

elseif solver==2 

transient_jacobi(nx,ny,tol,dt);

elseif solver==3

transient_gs(nx,ny,tol,dt);

elseif solver==4
    
transient_sor(nx,ny,tol,dt);

end
%2D transient conduction explicit
function transient_conduction_explicit(nx,ny,dt)
tic
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
dy=y(2)-y(1);
T=100*ones(nx,ny);
T(:,1)=400;
T(:,nx)=800;
T(1,:)=900;
T(ny,:)=600;
Told=T;
alpha=1;
 t=0.2;
nt = t/dt;
k1=(alpha*dt)/dx^2;
k2=(alpha*dt)/dy^2;
iterative_solver=1;
error=9e9;

time=0;
    for k=1:nt
          for j=2:ny-1  
    for i=2:nx-1
      T(i,j)=(1-2*k1-2*k2)*Told(i,j)+k1*(Told(i+1,j)+Told(i-1,j))+k2*(Told(i,j-1)+Told(i,j+1));
    end
      end
 time=time+dt;
   Told=T;
   explicit=toc
  contourf(x,y,T)
    [C,h]=contourf(x,y,T);
    clabel(C,h)
  
    xlabel('X axis')
    ylabel('Y axis')
     title_text=sprintf("explicit\n total time=%gs\n time of simulation=%gs",time,explicit);
    title(title_text);
    colorbar
      pause(0.03) 
    
    end
end
    
    
  
%2D transient heat conduction implicit

function transient_jacobi(nx,ny,tol,dt)
tic
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
dy=y(2)-y(1);
T=300*ones(nx,ny);
T(:,1)=400;
T(:,nx)=800;
T(1,:)=900;
T(ny,:)=600;
Told=T;
Tprev=T;
alpha=1;
t=0.2;
nt =t/dt;

k1=(alpha*dt)/dx^2;
k2=(alpha*dt)/dy^2;
term1=(1+2*k1+2*k2)^-1;

% error=9e9;

w=1.1;

%jacobi

     jacobi_iter=1;
time=0;

    for k=1:nt
       error=9e9;
    while(error>tol)
      for j=2:ny-1  
    for i=2:nx-1
      T(i,j)=term1*Tprev(i,j)+k1*term1*(Told(i+1,j)+Told(i-1,j))+k2*term1*(Told(i,j-1)+Told(i,j+1));
    end
      end
    error=max(max(abs(Told-T)));
    Told=T;
  jacobi_iter=jacobi_iter+1;

  
    
    end
   Tprev=T;
    time=time+dt;
    time_of_simulation=toc
   contourf(x,y,T);
     [C,h]=contourf(x,y,T);
    clabel(C,h)
   
    xlabel('X axis')
    ylabel('Y')
    title_text=sprintf('total time=%gs\n no of iterations jacobi=%d\n time of simulation=%gs',time, jacobi_iter,time_of_simulation);
    title(title_text);
    colorbar
      pause(0.03) 
   
    end
end
%Gauss Seidal
function transient_gs(nx,ny,tol,dt)
tic
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
dy=y(2)-y(1);
T=300*ones(nx,ny);
T(:,1)=400;
T(:,nx)=800;
T(1,:)=900;
T(ny,:)=600;
Told=T;
Tprev=T;
alpha=1;
t=0.2;
nt =t/dt;
k1=(alpha*dt)/dx^2;
k2=(alpha*dt)/dy^2;
term1=(1+2*k1+2*k2)^-1;
w=1.1;
     gs_iter=1;
time=0;

    for k=1:nt
       error=9e9;
    while(error>tol)
      for j=2:ny-1  
    for i=2:nx-1
      T(i,j)=term1*Tprev(i,j)+k1*term1*(T(i+1,j)+Told(i-1,j))+k2*term1*(T(i,j-1)+Told(i,j+1));
    end
      end
    error=max(max(abs(Told-T)));
    Told=T;
  gs_iter=gs_iter+1;

         end
   Tprev=T;
    time=time+dt;
    time_of_gs=toc
   contourf(x,y,T);
     [C,h]=contourf(x,y,T);
    clabel(C,h)
   
    xlabel('X axis')
    ylabel('Y')
    title_text=sprintf('total time=%gs\n and no of iterations gauss seidal=%d\n time of simulation=%gs',time, gs_iter,time_of_gs);
    title(title_text);
    colorbar
      pause(0.03) 
   
    end
  
      end
%SOR
function transient_sor(nx,ny,tol,dt)
tic
x=linspace(0,1,nx);
dx=x(2)-x(1);
y=linspace(0,1,ny);
dy=y(2)-y(1);
T=300*ones(nx,ny);
T(:,1)=400;
T(:,nx)=800;
T(1,:)=900;
T(ny,:)=600;
Told=T;
Tprev=T;
alpha=1;
t=0.2;
nt =t/dt;
 k1=(alpha*dt)/dx^2;
k2=(alpha*dt)/dy^2;
term1=(1+2*k1+2*k2)^-1;
w=1.1;
     SOR_iter=1;
time=0;

    for k=1:nt
       error=9e9;
    while(error>tol)
      for j=2:ny-1  
    for i=2:nx-1
         Tgauss(i,j)=term1*(Tprev(i,j)+k1*(T(i+1,j)+Told(i-1,j))+k2*(T(i,j-1)+Told(i,j+1)));
      T(i,j)=(1-w)*Tprev(i,j)+w*Tgauss(i,j);
    end
      end
    error=max(max(abs(Told-T)));
    Told=T;
  SOR_iter=SOR_iter+1;

      end
   Tprev=T;
    time=time+dt;
    time_of_sor =toc
   contourf(x,y,T);
     [C,h]=contourf(x,y,T);
    clabel(C,h)
       xlabel('X axis')
    ylabel('Y')
    title_text=sprintf('total time=%gs\n no of iterations sor=%d\n time of simulation=%gs',time, SOR_iter,time_of_sor);
    title(title_text);
    colorbar
      pause(0.03) 
   
    end
end

 

Output:

Transient simulation using explicit scheme

Transient simulation using Jacobi(Implicit)

Transient simulation using Gauss Seidal(Implicit)

Transient simulation using SOR(Implicit)

Inference-For transient analysis of 2D heat equation 20 grid points were taken and simulation was run for 0.2 seconds.Time step dt was taken to be 0.0004.

Transient analysis explicit- Time of simulation =103.3 s

Transient analysis jacobi- No. of iterations-4867 Time of simulation =128.9 s

Transient analysis Gauss seidal- No. of iterations-4255 Time of simulation =115.1 s

Transient analysis SOR- No. of iterations-4486 Time of simulation =117.7 s

So the explicit method was the fastest as in explicit scheme we did a time marching solution and there was no convergence criteria.Jacobi took the highest time to converge followed br SOR and Gauss Seidal.

SOR converged faster for under relaxed condition.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.