CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

MacCormack's solution of Shock tube problem

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 13, 2018, 06:22
Default MacCormack's solution of Shock tube problem
  #1
New Member
 
thecampusgirl
Join Date: Jan 2018
Posts: 11
Rep Power: 8
mathgra is on a distinguished road
Hello Im working on numerical solution of Sod Shock tube problem. I have written matlab code using Lax Friedrichs. Now I wanna write matlab code with MacCormack two step scheme.

My code is working but atfer some steps I got NaN values so I can not obtain my figures. If you can look my code and give any idea for solving this problem I will be thankful. And density start at 0.5 instead of 1 I dont understand why this happen.

Here the code that I write in MATLAB ;

clear all;
close all;
clc;

nx = 101;

% Initialization
r = zeros(nx,1);
u = zeros(nx,1);
E = zeros(nx,1);
p = zeros(nx,1);

x = zeros(nx,1);
Q = zeros(nx,3);
F = zeros(nx,3);
Qnew = zeros(nx,3);
c = zeros(nx,1);

% Parameters
gamma = 1.4;
CFL = 0.68;
tMax = 0.2;
dx = 1/(nx-1);

% Creating mesh

for i=1:nx
x(i) = (i-1)*dx;
end


% Initial condition
for i=1:nx
if (x(i) > 0.5)
p(i) = 0.1;
r(i) = 0.125;
u(i) = 0.0;

elseif (x(i) <= 0.5 )
p(i) = 1.0;
r(i) = 1.0;
u(i) = 0.0;

end
E(i) = p(i)./(gamma-1) + 0.5*r(i)*(u(i).^2);
c(i) = sqrt(gamma*p(i)/r(i));
end


% Q_new will be the solution vector

Qnew(:,1)=r;
Qnew(:,2)=r.*u;
Qnew(:,3)=E;

t=0.d0;
% Time loop
while (t<tMax)
dt = CFL*dx./(max(max(c+sqrt(u.*u))));
for i=1:nx

% Flux calculation
Q(i,1) = r(i);
Q(i,2) = r(i).*u(i);
Q(i,3) = E(i);


F(i,1) = r(i).*u(i);
F(i,2) = r(i).*u(i).^2 + p(i);
F(i,3) = u(i).*(E(i) + p(i));
end




for i=2:nx-1

% predictor step
Qstar(i,1) = Q(i,1) - (dt/dx)*(F(i+1,1) - F(i,1)) ;
Qstar(i,2) = Q(i,2) - (dt/dx)*(F(i+1,2) - F(i,2)) ;
Qstar(i,3) = Q(i,3) - (dt/dx)*(F(i+1,3) - F(i,3)) ;

% Corrector step

ustar(i) = Qstar(i,2) / Qstar(i,1);
pstar(i) = (gamma-1)*( Qstar(i,3) - 0.5*(Qstar(i,2).^2/Qstar(i,1)) );

Fstar(i,1) = Qstar(i,1).*ustar(i);
Fstar(i,2) = Qstar(i,1).*(ustar(i).^2) + pstar(i);
Fstar(i,3) = ustar(i).*( Qstar(i,3) + pstar(i) );

Qnew(i,1) = 0.5* (Qnew(i,1) + Qstar(i,1) - (dt/dx) * (Fstar(i,1) - Fstar(i-1,1)));

Qnew(i,2) = 0.5* (Qnew(i,2) + Qstar(i,2) - (dt/dx) * (Fstar(i,2) - Fstar(i-1,2)));

Qnew(i,3) = 0.5* (Qnew(i,3) + Qstar(i,3) - (dt/dx) * (Fstar(i,3) - Fstar(i-1,3)));


end


% Boundary condition

Qnew(1,: ) = Qnew(2,: );
Qnew(nx,: ) = Qnew(nx-1,: );

% Primitive variables
r = Qnew(:,1);
u = Qnew(:,2)./r;
E = Qnew(:,3);
p = (gamma-1)*(E-0.5*r.*(u.*u));

t=t+dt; % Advance in time

end

plot(x,r);
figure(1)
xlabel('x');
title('Density');

Last edited by mathgra; August 13, 2018 at 10:05.
mathgra is offline   Reply With Quote

Old   August 14, 2018, 08:54
Default
  #2
Senior Member
 
Join Date: Oct 2011
Posts: 242
Rep Power: 16
naffrancois is on a distinguished road
I think I can see one coding mistake. In the corrector step you use Fstar(i-1,: ). As the components of Fstar are defined in a loop from 2 to nx, Fstar(1,: ) is never computed.

Second comment, probably not critical here, in general it is better to update the speed of sound inside the temporal loop for dt computation
naffrancois is offline   Reply With Quote

Old   August 14, 2018, 09:06
Default
  #3
New Member
 
thecampusgirl
Join Date: Jan 2018
Posts: 11
Rep Power: 8
mathgra is on a distinguished road
Quote:
Originally Posted by naffrancois View Post
I think I can see one coding mistake. In the corrector step you use Fstar(i-1,: ). As the components of Fstar are defined in a loop from 2 to nx, Fstar(1,: ) is never computed.

Second comment, probably not critical here, in general it is better to update the speed of sound inside the temporal loop for dt computation

Oh you are right, Thanks a lot I added Fstar at boundary as

Fstar(1,: ) = Fstar(2,: );
Fstar(nx,: ) = Fstar(nx-1,: );

but density still start with 0.8 its really interesting
mathgra is offline   Reply With Quote

Old   August 14, 2018, 09:24
Default
  #4
New Member
 
thecampusgirl
Join Date: Jan 2018
Posts: 11
Rep Power: 8
mathgra is on a distinguished road
Quote:
Originally Posted by naffrancois View Post
I think I can see one coding mistake. In the corrector step you use Fstar(i-1,: ). As the components of Fstar are defined in a loop from 2 to nx, Fstar(1,: ) is never computed.

Second comment, probably not critical here, in general it is better to update the speed of sound inside the temporal loop for dt computation
I did it

thank you so much for advise the place that I put Fstar boundary was wrong.

Now it works Im so much thankful
mathgra is offline   Reply With Quote

Old   August 14, 2018, 09:27
Default
  #5
Senior Member
 
Join Date: Oct 2011
Posts: 242
Rep Power: 16
naffrancois is on a distinguished road
All right well done!
naffrancois is offline   Reply With Quote

Old   April 12, 2020, 15:07
Default
  #6
New Member
 
Volkan
Join Date: Nov 2018
Posts: 5
Rep Power: 7
volkanyildirim48 is on a distinguished road
Hi mathgra,


I am trying to use your code to have a basic understanding of the mccormick solution, but i keep having error "Undefined function or variable 'Fstar'.
"


I don't know what I am doing wrong.


Thank you
volkanyildirim48 is offline   Reply With Quote

Old   January 28, 2023, 21:10
Default
  #7
New Member
 
Join Date: Nov 2012
Posts: 4
Rep Power: 13
black key is on a distinguished road
Please share your updated code for Maccormack. I am also working on the same for understanding.
black key is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
1D Shock Tube problem RamShivam Main CFD Forum 3 June 13, 2018 09:37
Sod Shock Tube solution using Nodal DGFEM 2D CNS Solver Quasar_89 Main CFD Forum 0 July 30, 2016 08:22
Analytical solution for the Shock Tube Farouk Main CFD Forum 0 July 12, 2013 08:16
ghost fluid method (shock tube problem) Amir Main CFD Forum 0 March 1, 2009 06:16
ENO Scheme in a Shock tube problem Shiv Main CFD Forum 4 January 29, 2007 15:31


All times are GMT -4. The time now is 01:18.