%superGrimace
%all alfas in DEGREES!
%by ascorrea, with help from Jacob Huffman and Omkar Shetty
%"filler" variable just makes it easier to switch between Ronald and Grimace

function superGrimace(paramfile, pranformfile, pranformConstraint)
testrange={'20'};

for testnumber=1:length(testrange)
prefix='FDR0';
name=strcat(prefix, testrange{testnumber});
paramfile=strcat(name, '.param');
pranformfile=strcat(name, '.plan', '');
fprintf('%s \n', name)


%LOCATIONS--------------------------------------------------------------]
rootLOCATION='/Users/anthonyscorrea/Documents/MATLAB/AE441 - Home Edition/Matlab Code';
airfoilLOCATION='/Users/anthonyscorrea/Documents/MATLAB/AE441 - Home Edition/Airfoils';
pranformLOCATION='/Users/anthonyscorrea/Documents/MATLAB/AE441 - Home Edition/Planforms';
pranformConstraintLOCATION='/Users/anthonyscorrea/Documents/MATLAB/AE441 - Home Edition/Planform Constraints';
paramLOCATION='/Users/anthonyscorrea/Documents/MATLAB/AE441 - Home Edition/Parameter Files';
outputsLOCATION='/Users/anthonyscorrea/Documents/MATLAB/AE441 - Home Edition/superGrimace Outputs';
%-----------------------------------------------------------------------]
spanwiseresolution=50;
chordwiseresolution=100;

cd(paramLOCATION);
[variablenames, variablevalues]=textread(paramfile, '%s %f');
cd(pranformLOCATION)
[spans, chords, offsets, airfoil]=textread(pranformfile, '%f %f %f %s');
cd(pranformConstraintLOCATION)
[spansCONST, chordsCONST, offsetsCONST, heightCONST]=textread(pranformConstraint, '%f %f %f %f');
cd(rootLOCATION)

for k = 1:length(variablenames)
eval([variablenames{k} '= variablevalues(k);']);
end

cd(airfoilLOCATION)

[surfplotY surfplotX thicknessplot]=findthicknessplot(airfoil, chords, spans, offsets, spanwiseresolution, chordwiseresolution);
[wakeplot]=findwakeplot(chords, spans, alt_cruise, M_cruise, laminarflow_cruise, fuse_width, spanwiseresolution, chordwiseresolution);
plotitle=regexprep(pranformfile, '.plan', '');

cd(outputsLOCATION)
% clf
% hold on
% surf(surfplotY, -surfplotX, thicknessplot, 'LineStyle', 'none')
% surf(-surfplotY,-surfplotX, thicknessplot, 'LineStyle', 'none')
% title(plotitle)
% axis off
% axis equal
% colorbar
% view(2)
% filename=regexprep(pranformfile, '.plan', '_surfacev1.tiff');
% print (gcf, '-dtiff', filename)
% clf
% hold on
% surf(surfplotY, surfplotX, thicknessplot, 'LineStyle', 'none')
% surf(-surfplotY, surfplotX, thicknessplot, 'LineStyle', 'none')
% axis equal
% axis off
% colorbar
% view(3)
% title(plotitle)
% filename=regexprep(pranformfile, '.plan', '_surfacev2.tiff');
% print (gcf, '-dtiff', filename)

clf
hold on
surf(surfplotY, -surfplotX, wakeplot, 'LineStyle', 'none')
surf(-surfplotY,-surfplotX, wakeplot, 'LineStyle', 'none')
title(plotitle)
axis off
axis equal
colorbar
view(2)
filename=regexprep(pranformfile, '.plan', '_BLthicknessv1.tiff');
print (gcf, '-dtiff', filename)

clf
hold on
surf(-surfplotY, -surfplotX, wakeplot, 'LineStyle', 'none')
surf(-surfplotY, -surfplotX, wakeplot, 'LineStyle', 'none')
axis off
axis([-80 80 -160 0 0 1])
colorbar
view(3)
title(plotitle)
filename=regexprep(pranformfile, '.plan', '_BLthicknessv2.tiff');
print (gcf, '-dtiff', filename)


axis equal
axis off
hold off

cd(rootLOCATION)
end
clc
fprintf('You are welcome\n')
end

function [plotLE plotTE]=plotpranform(spans, chords, offsets, n)
Chords = findChords(n, chords, spans);

%plot planform
for i = 1:(size(chords)-1)
    dist=0;
    for j = 1:n
        dist = (spans(i+1)-spans(i))./n;
        
        if i==1
            xplotLE(i,j+1)=spans(1)+dist*(j);
        else
            xplotLE(i,1)=xplotLE(i-1,end);
            xplotLE(i,j+1)=xplotLE(i-1,end)+dist*(j);
        end
        
        fitX(1)=spans(i);
        fitX(2)=spans(i+1);
        fitY(1)=offsets(i);
        fitY(2)=offsets(i+1);
        a = polyfit(fitX, fitY,1);
        yplotLE(i,j) = a(1)*xplotLE(i,j)+a(2);
        
        
        
    end
end


plotLE=zeros(1,2);
plotTE=zeros(1,2);

for i=1:(size(chords)-1)
    for j=1:n
        plotLE=[plotLE; xplotLE(i,j) -yplotLE(i,j)];
        plotTE=[plotTE; xplotLE(i,j) -(yplotLE(i,j)+Chords(i,j))];
    end
end
plotTE(1,:)=[];
plotLE(1,:)=[];
end
function [plotLE plotTE]=plotconstraint(spans, chords, offsets, n)

%plot constraint
for i = 1:(size(chords)-1)
    for j = 1:n
        dist = (spans(i+1)-spans(i))./n;
        if i==1
            xplotLE(i,j+1)=spans(1)+dist*(j);
        else
        xplotLE(i,1)=xplotLE(i-1,end);    
        xplotLE(i,j+1)=xplotLE(i-1,end)+dist*(j);
        end
        yplotLE(i,j) = offsets(i+1);
    end
end

plotLE=zeros(1,2);
plotTE=zeros(1,2);

for i=1:(size(chords)-1)
    for j=1:n
        plotLE=[plotLE; xplotLE(i,j) -yplotLE(i,j)];
        plotTE=[plotTE; xplotLE(i,j) (-yplotLE(i,j)-chords(i+1))];
    end
end

plotTE(1,:)=[];
plotLE(1,:)=[];
end
function plotThick=plotThick(spans, chords, offsets, airfoil, h, n)
n=n/10;
Chords = findChords(n, chords, spans);
plotThickLE=zeros(1,2);
plotThickTE=zeros(1,2);

for i = 1:(size(chords)-1)
    for j = 1:n
        
        Chords(i,j);
        dist = (spans(i+1)-spans(i))./n;
        
        if i==1
            xplotLE(i,j+1)=spans(1)+dist*(j);
        else
            xplotLE(i,1)=xplotLE(i-1,end);
            xplotLE(i,j+1)=xplotLE(i-1,end)+dist*(j);
        end
        
        fitX(1)=spans(i);
        fitX(2)=spans(i+1);
        fitY(1)=offsets(i);
        fitY(2)=offsets(i+1);
        a = polyfit(fitX, fitY,1);
        yplotLE(i,j) = a(1)*xplotLE(i,j)+a(2);
        
        chordloc=linspace(0, Chords(i,j));
        
        for k=1:length(chordloc)-1
            thicknessLE(k)=findthickness(airfoil{i}, Chords(i,j), chordloc(k));
            clc
            fprintf('%g:%u:%u:%u/%g:%u:%u:%u',h,i,j,k,h,(length(chords)-1),n,(length(chordloc)-1))
            if thicknessLE(k)>=h
                plotThickLE=[plotThickLE; xplotLE(i,j) -(chordloc(k)+yplotLE(i,j))];
                break
            end
        end
        
        for k=1:length(chordloc)-1
            thicknessTE(k)=findthickness(airfoil{i}, Chords(i,j), chordloc(length(chordloc)-k));
            if thicknessTE(k)>=h
                plotThickTE=[plotThickTE; xplotLE(i,j) -(chordloc(length(chordloc)-k)+yplotLE(i,j))];
                break
            end
        end
        
    end
    
end


plotLE=zeros(1,2);
plotTE=zeros(1,2);

for i=1:(size(chords)-1)
    for j=1:n
        plotLE=[plotLE; xplotLE(i,j) -yplotLE(i,j)];
        plotTE=[plotTE; xplotLE(i,j) -(yplotLE(i,j)+Chords(i,j))];
    end
end

plotTE(1,:)=[];
plotLE(1,:)=[];
plotThickLE(1,:)=[];
plotThickTE(1,:)=[];
plotThick=[plotThickLE; flipud(plotThickTE)];
end
function thickness=findthickness(airfoil, Chord, xLoc)
Airfoil = load(airfoil);
Airfoil = Airfoil*Chord;
% blah....goodnight
    
    AirfoilX=Airfoil(:,1);
    [min_diff, k]=min(abs(AirfoilX));
    
    for j=1:k
    AirfoilYupper(j)=Airfoil(j,2);
    AirfoilXupper(j)=Airfoil(j,1);
    end
    
    for j=1:length(Airfoil)-k
    AirfoilYlower(j)=Airfoil(length(Airfoil)-(j-1), 2);
    AirfoilXlower(j)=Airfoil(length(Airfoil)-(j-1), 1);
    end
    
    AirfoilYupper=fliplr(AirfoilYupper);
    AirfoilXupper=fliplr(AirfoilXupper);
    AirfoilYlower=fliplr(AirfoilYlower);
    AirfoilXlower=fliplr(AirfoilXlower);
    AirfoilYlower=[0 AirfoilYlower];
    AirfoilXlower=[0 AirfoilXlower];
    
    [min_diff, k]=min(abs(AirfoilX));
    
    [min_diff, kU]=min(abs(AirfoilXupper-xLoc));
    [min_diff, kL]=min(abs(AirfoilXlower-xLoc));
    
    if xLoc<AirfoilXupper(kU)
        xFitU(1)=AirfoilXupper(kU-1);
        xFitU(2)=AirfoilXupper(kU);
        yFitU(1)=AirfoilYupper(kU-1);
        yFitU(2)=AirfoilYupper(kU);
    else
        xFitU(1)=AirfoilXupper(kU);
        xFitU(2)=AirfoilXupper(kU+1);
        yFitU(1)=AirfoilYupper(kU);
        yFitU(2)=AirfoilYupper(kU+1);
    end
      
    if xLoc<AirfoilXlower(kL)
        xFitL(1)=AirfoilXlower(kL-1);
        xFitL(2)=AirfoilXlower(kL);
        yFitL(1)=AirfoilYlower(kL-1);
        yFitL(2)=AirfoilYlower(kL);
    else
        xFitL(1)=AirfoilXlower(kL);
        xFitL(2)=AirfoilXlower(kL+1);
        yFitL(1)=AirfoilYlower(kL);
        yFitL(2)=AirfoilYlower(kL+1);
    end
            aU = polyfit(xFitU, yFitU,1);
            aL = polyfit(xFitL, yFitL,1);
%             yplotLE(i,j) = a(1)*xplotLE(i,j)+a(2);
%     thickness =  AirfoilYupper(xLoc_indu)-AirfoilYlower(xLoc_indl);
    thickness =  (aU(1)*xLoc+aU(2))-(aL(1)*xLoc+aL(2));
end
function Chords = findChords( n, Chord, X )
for( i = 1:(length(Chord)-1) )
    Xtemp = linspace(X(i),X(i+1),n);
    Chords(i,:) = (Chord(i+1)-Chord(i))./(X(i+1)-X(i)).*(Xtemp-X(i))+Chord(i);
    
end
end
function [plotY plotX thicknessplot]=findthicknessplot(airfoil, chords, spans, offsets, n, reso)
    Chords = findChords(n, chords, spans);
    thicknessplot=zeros(reso-1,1);
    plotX=zeros(reso-1,1);
    plotY=zeros(reso-1,1);
    jcount=0;

    for i=1:(size(chords)-1)
        for j = 1:n
            %         wakecoord(j,:)=linspace(0, Chords(i,j), n);
            dist = (spans(i+1)-spans(i))./n;
            y(j)=spans(i)+j*dist;
            
            if i==1
                xplotLE(i,j+1)=spans(1)+dist*(j);
            else
                xplotLE(i,1)=xplotLE(i-1,end);
                xplotLE(i,j+1)=xplotLE(i-1,end)+dist*(j);
            end
            
            fitX(1)=spans(i);
            fitX(2)=spans(i+1);
            fitY(1)=offsets(i);
            fitY(2)=offsets(i+1);
            a = polyfit(fitX, fitY,1);
            yplotLE(i,j) = a(1)*xplotLE(i,j)+a(2);
            
            x=linspace(0, Chords(i,j),reso);
            
            for k = 1:length(x)-1
%                
%                 thickness(k,j)=findthickness(airfoil{i}, Chords(i,j), x(k));
            end
            wakeX(k,j)=x(k)+yplotLE(i,j);
            wakeY(k,j)=y(j);
            
            for k = 1:length(x)-1
                
                wakeX(k,j)=x(k)+yplotLE(i,j);
                wakeY(k,j)=y(j);
            end
            
            
            
%             thicknessplot=[thicknessplot, thickness];
            plotX=[plotX, wakeX];
            plotY=[plotY, wakeY];
        end
        fprintf('Thickness plot: %6.2f percent complete\n', 100*(i+.1*j)/((length(chords)-1)+ n*.1))
        
    end
    thicknessplot(:,1)=[ ];
    plotX(:,1)=[ ];
    plotY(:,1)=[ ];

    flipud(thicknessplot);
end
function [q v_fps dynvisc]=findconstants(alt, v_mach)
if alt < 36152
temp=59-.00356*alt;
press=2116*((temp+459.7)/518.6)^5.256;

elseif alt < 82345
    temp=-70;
    press=473.1*exp(1.73-.000048*alt);
    
else
    print('Program Not Valid for given altitude')
end

%slpcuft: clugs ber cubic foot
density_slpcuft=press/(1718*(temp+459.7));
v_fps=v_mach*sqrt(1.4*1718*(temp+459.7));
q=finddynpress(alt, v_mach);

S = 110.4;
beta = 1.458e-6;
temp_k=[(temp-32) / (1.8)] + 273 ;
viscosity_pascalseconds = (beta*temp_k^(1.5))/(temp_k+S);
%kgpmcu:kilograms per meter cubed
%msqpse:meters squared per second
%sqftpsec:square feet per second
density_kgpmcu=density_slpcuft*515.4;
dynamic_viscosity_msqpsec=viscosity_pascalseconds/density_kgpmcu;
dynamic_viscosity_sqftpsec=dynamic_viscosity_msqpsec*10.76;
dynvisc=dynamic_viscosity_sqftpsec;
density=density_slpcuft;
end
function [wakeplot]=findwakeplot(chords, spans, alt, v_mach, laminarflow, width, n, reso) 
[q v_fps dynvisc]=findconstants(alt, v_mach);
Chords = findChords(n, chords, spans);
wakeplot=zeros(reso,1);
width=width/2;
for i=1:1:(size(chords)-1)
        dist=(spans(i+1)-spans(i))/n;
    for j = 1:n
        if (spans(i)+dist*j)>width
            lam=laminarflow;
        else
            lam=laminarflow-.15;
        end
        x=linspace(0, Chords(i,j),reso);
        for k = 1:length(x) 
            if (k <= 100*lam)
                delta_x=( 5.2 * x(k) )*(v_fps*x(k)/dynvisc)^-0.5;
                wake(k,j)= delta_x;
            else %if turbulent
                x_tr=x(100*lam);
                Re_xtr = v_fps * x_tr / dynvisc;
                delta_xtr=(5.2*x_tr)*(v_fps*x(k)/dynvisc)^-(0.5);
                delta_l = ((delta_xtr*((v_fps)^0.2))/(0.37*((dynvisc)^0.2)))^(1/0.8);
                lt = x(k) - x_tr + delta_l;
                Re_lt = v_fps * lt / dynvisc;
                delta = 0.37 * lt /(Re_lt)^0.2;
                wake(k,j) = delta;
            end
        end
wakeplot=[wakeplot, wake];
    end
   
fprintf('Wake plot: %6.2f percent complete\n', 100*(i+.1*j)/((length(chords)-1)+ n*.1))
end
wakeplot(1,:)=[];
wakeplot(:,1)=[ ];
flipud(wakeplot);
end
function q=finddynpress(alt, v_mach)
if alt < 36152
temp=59-.00356*alt;
press=2116*((temp+459.7)/518.6)^5.256;

elseif alt < 82345
    temp=-70;
    press=473.1*exp(1.73-.000048*alt);
    
else
    print('Program Not Valid for given altitude')
end

%slpcuft: clugs ber cubic foot
density_slpcuft=press/(1718*(temp+459.7));
v_fps=v_mach*sqrt(1.4*1718*(temp+459.7));
q=.5*density_slpcuft*v_fps^2;
end
