% Code solves the 1-D N.S. equations with constant Cp, and transport coeffs (mu, lambda)
% 8th order derivatives on a periodic domain
% 4th order R.K. advancement
% properties for air at 300 K.  

clc;
clear;

global ngrd ldom cv Rgas M Tcnd mu nvar Lperiodic rho v u e P T qflx tau xgrd dx beta;

%------------ CHANGE THESE FOR CASE SETUP

ngrd = 501;         % number of grid points
ldom = 0.01;        % domain length (m)
tEnd = 2.7E-4;      % run time (s)

dt   = 8.80E-8;     % timestep size (s)   (increase to 8.9)
beta = 1.0;          % pressure gradient scaling factor

%dt   = 1.20E-6;     % timestep size (s)      (increase to 1.3)
%beta = 0.005;        % pressure gradient scaling factor

modD = 40;          % number of outputs (plot) during solution
Vinit= 0;           % initial domain velocity

%------------

cv   = 718.96;      % (J/kg*K)
Rgas = 8314.47215;  % (J/kmol*K)   
M    = 29;          % (kg/kmol)
mu   = 0.00001846;  % (kg/m*s)
Tcnd = mu*(Rgas/M+cv)/1.0;      % (J/m*K*s)

nvar = 3;           % number of variables (rho, rho*v, rho*e)
Lperiodic = 1;      % 1 for periodic


%-------------

xgrd = linspace(0,ldom,ngrd); 
xgrd = xgrd(1:end-1);       % for periodic
xgrd = xgrd';
ngrd = ngrd-1;              % for periodic
dx   = xgrd(2)-xgrd(1);

v    = zeros(ngrd,1);       % velocity
P    = zeros(ngrd,1);       % pressure
T    = zeros(ngrd,1);       % temperature 
rho  = zeros(ngrd,1);       % density
u    = zeros(ngrd,1);       % internal energy
e    = zeros(ngrd,1);       % total energy
qflx = zeros(ngrd,1);       % heat flux
tau  = zeros(ngrd,1);       % stress tensor

q    = zeros(ngrd,nvar);    % solution vector: rho, rho*v, rho*etot
kq   = zeros(ngrd,nvar,4);  % R.K. Solver vector

%------------- Initialize the domain and the solution vectors

initField;
data = [xgrd v0 P0 T0];
save -ascii x_v_P_T_init.dat data;

%------------- Advance the solution

nt = floor(tEnd/dt);
dt = tEnd/nt;
t  = 0;

for it=1:nt

    kq(:,:,1) = rhsf( q );
    kq(:,:,2) = rhsf( q + 0.5*dt*kq(:,:,1) );
    kq(:,:,3) = rhsf( q + 0.5*dt*kq(:,:,2) );
    kq(:,:,4) = rhsf( q +     dt*kq(:,:,3) );

    q = q + dt/6*( kq(:,:,1) + 2*kq(:,:,2) + 2*kq(:,:,3) + kq(:,:,4) );

    t = t+dt;

    if(mod(it,floor(nt/modD))==0)
        [t tEnd]*1000
        setPrimitiveVarsFromQ;
        subplot(3,1,1); plot(xgrd,v,'-'); xlabel('x (m)'); ylabel('v (m/s)');
%            axis([0 ldom min(v0)-0.01*min(v0) max(v0)+0.01*max(v0)]);
%        ylim([-0.01 0.01]);
        subplot(3,1,2); plot(xgrd,P-101325,'-'); xlabel('x (m)'); ylabel('P (Pa)');
        ylim([-5 5]);
%        subplot(3,1,2); plot(xgrd,rho,'-'); xlabel('x (m)'); ylabel('rho (kg/m3)');
%        ylim([1 1.2]);
%            axis([0 ldom min(P0)-0.00007*min(P0) max(P0)+0.00007*max(P0)]);
        subplot(3,1,3); plot(xgrd,T,'-'); xlabel('x (m)'); ylabel('T (K)');
            axis([0 ldom min(T0)-0.01*min(T0) max(T0)+0.01*max(T0)]);
        %pause(0.05);
        pause(1.05);
    end

end

%setPrimitiveVarsFromQ;
%subplot(3,1,1); plot(xgrd,v,'-'); xlabel('x (m)'); ylabel('v (m/s)');
%%    axis([0 ldom min(v0)-0.01*min(v0) max(v0)+0.01*max(v0)]);
%subplot(3,1,2); plot(xgrd,P,'-'); xlabel('x (m)'); ylabel('P (Pa)');
%    axis([0 ldom min(P0)-0.00007*min(P0) max(P0)+0.00007*max(P0)]);
%subplot(3,1,3); plot(xgrd,T,'-'); xlabel('x (m)'); ylabel('T (K)');
%    axis([0 ldom min(T0)-0.01*min(T0) max(T0)+0.01*max(T0)]);

data = [xgrd v P T];
save -ascii x_v_P_T.out data;









 
