%FINAL PROJECT #9--------------COMPUTATION OF LINEARIZED EULER EQUATIONS
%Lance Simms 12/2005
%
%u=velocity in x direction      v=velocity in y direction
%Flow is flowing in from left.  Assumed to be uniform rho=1, u=M_inf, v=0
%Linearization about p=1, u=M_inf, v=0
%
%Thin airfoil present having shape given by
%   ywall=Tx(1-x)/2     0<x<1
%   ywall=0             x<0 x>1
%
%Euler Equations are written as 
%
%   DQ       DQ     DQ
%   --  +  A -- + B --  = 0
%   Dt       Dx     Dy
%
%  where:     |     0         1      0   |      |  rho  |
%         A = | -M_inf^2   2 M_inf   0   |, Q = | rho u |
%             |     0         0    M_inf |      | rho v |
%
%             |     0         0      1    |
%         B = |     0         0     M_inf |
%             |     1         0      0    |
%  Inputs:
%         M_inf :  Free Stream Mach number
%         nx    :  Number of points on airfoil (x=0,1)
%         cfl   :  dt = min(dx,dy)*cfl/(M_inf+1 + 1)
%                     where Spectral radius of A is (M_inf+1) and B is 1
%         nmax  : Number of time steps to go
%         nout  : Output frequency of plot of cp  (0 for no plots)
%         meth  : 1: Explicit Euler  / 2: RK4 / 3: Implicit Euler
%         order : 0 : central 2nd / 1 : 1st order upwind / 2 : 2nd order upwind
%         ichar : 0 : simple bc / 1 : char bc / -1 simpler bc
%
%
clear;

%MACH NUMBER

M_inf=0.8;
%M_inf=input('Enter M_inf');

%Grid Generation
xmin=-1.0;          xmax=3.0;
ymin=0.0;           ymax=2.0;
nx=50;
%nx=input('Enter number of points on body');
dx=1.0/(nx-1);      dy=dx; 
x=[xmin:dx:xmax]';  y=[ymin:dy:ymax]';
jmax=size(x,1);     kmax=size(y,1);

%Generate meshgrid having x values as rows and y values as columns
[X,Y]=meshgrid(x,y);

jle=nx; jte=2*nx-1;
fprintf('Leading edge point= %g trailing edge point = %g \n', jle,jte);
fprintf('X of Leading edge point =%g    X of trailing edge point = %g \n', x(jle),x(jte));

%Surface Definition: tau is the thickness
tau=0.5;   
%tau=input ('tau);
s=[jle:jte];
ys=zeros(jmax,1);   ys(s)=tau*0.5*x(s).*(1.0-x(s));

%Try to figure out indices of airfoil
foil=0;
for xind=1:length(x)
    newfoil=find(y<ys(xind));
    foil=[foil,xind*length(y)+newfoil'];
end
foil=foil(2:length(foil));

%Initialze elements of Q
Q(:,:,1)=ones(jmax,kmax,1);         %rho
Q(:,:,2)=M_inf*ones(jmax,kmax,1);   %rho u
Q(:,:,3)=zeros(jmax,kmax,1);        %rho v

%Jacobian Matrices
A=zeros(3,3);                                       B=zeros(3,3);

A(1,:)=[0,          1,           0    ];            B(1,:)=[0,   0,   1    ];
A(2,:)=[-M_inf^2+1  2.0*M_inf,   0    ];            B(2,:)=[0,   0,   M_inf];
A(3,:)=[0,          0,           M_inf];            B(3,:)=[1,   0,   0    ];

%Thin airfoil BC v=M_inf D(ys)/Dx
Q(s,1,3)=M_inf*(ys(s+1)-ys(s-1))*0.5/dx;

%Indices
J=[1:jmax];         K=[1:kmax];
JM=[2:jmax-1];      KM=[2:kmax-1];

%INPUTS
nmax=100;
%nmax=input('Enter nmax ');
nmax_tot=nmax;

cfl=0.5;
%cfl=input('Enter cfl');

meth=0;
%meth=input('Enter 0 for Euler Implicit AF, 1 for Euler Explicit:  ');

order=1;    
epse=0.0;
epse=Enter('2nd order diss coef epse   ',epse);

Eig_t=2.0+M_inf;

dt=min(dx,dy)*cfl/Eig_t;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%Eventually placed in separate function

R = zeros(size(Q));
Qx_c(JM,KM,:)=0.5*(Q(JM+1,KM,:)-Q(JM-1,KM,:))/dx;
Qy_c(JM,KM,:)=0.5*(Q(JM,KM+1,:)-Q(JM,KM-1,:))/dy;

for m=1:3
    R(JM,KM,m)=0.0;
    for n=1:3
        R(JM,KM,m)=R(JM,KM,m)+...
        A(m,n)*Qx_c(JM,KM,n)+B(m,n)*Qy_c(JM,KM,n);
    end
end

R(JM,KM,:)=-R(JM,KM,:);

ncount=0;
cpu0=cputime;

shg;
clf;
set (gcf,'doublebuffer','on');
set (gcf,'Units','normalized');
set (gcf,'Position',[0,0,.8,.8]);
for nstep=1:nmax_tot
    ncount=ncount+1;
    
    R=rhs_cen(Q,JM,KM,dx,dy,A,B,epse);
    
    Q(JM,KM,:) = Q(JM,KM,:) + dt*R(JM,KM,:);
    Q=bc8(Q,JM,KM,jmax,kmax,M_inf);
    %contourf(X,Y,Q(:,:,1)',50);
    %xlabel('rho');
    PlotQ=Q(:,:,1)';
    PlotQ(foil)=0;
    subplot(2,1,1);
    imagesc(x,y,flipud(PlotQ));
    subplot(2,1,2);
    quiver(X,Y,Q(:,:,2)',Q(:,:,3)');
    %imagesc(x,y(kmax:-1:1),Q(:,:,1)');
    pause(0.05);
    drawnow;    
end
