%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 2.23 MATLAB help session notes - 2/14/07
%
% Matlab is a tool with several predefined functions that let you do
% all sorts of things, from calculating the sine of an angle to plotting 
% three-dimensional streamlines around a submarine.  You can read about 
% how to use each function by typing at the command prompt:
% >> help function_name
%
%                   * * * * * * * * * * * *
%
% This is a list of functions you may need to use in this class:                                          
% 
% general:             clear, close, for, if, set, get
%
% file input/output:   input, disp, textread, sprintf, 
%                      fopen, fprintf, fclose
%
% matrix manipulation: size, length, permute
%
% figures:             figure, plot, title, xlabel, ylabel, hold, 
%                      plot3, surf, surfl, view, shading, colormap, 
%                      subplot, axis
%
%                   * * * * * * * * * * * *
%
% Each function has its own syntax, and the help documentation tells you
% how to call it properly.
%
% You can also create your own functions, with your own syntax.  To learn
% how type at the command prompt:
% >> help function
%
% The variables within the body of the function are "local" variables; they
% are not saved in Matlab's workspace.  That is, any variables you define
% within a function are not available to use outside the funciton.  If you
% want to run a series of commands and have all of the results and
% variables "remembered" in the workspace, you can create a "script":
% >> help script
%
% The scripts and functions you write are simply text files (that you can
% make in Matlab's Editor window, Apple TextEdit, Microsoft Notepad, etc.), 
% but you save them with the file extemsion ".m" instead of ".txt".
%
%                   * * * * * * * * * * * *
%
% General programming guidelines:
%
% 1. Comment your work!!!  % Adding a comment telling what each line does
%                          % makes it infinitely easier to read your code 
%                          % later and remember how it works!!!  Matlab 
%                          % considers anything on a line after a % sign 
%                          % to be a comment.  
%
% 2. Organize your code:
%       2a. Use carriage returns:  Separate the part of code where you 
%                                  compute a variable from the part of code  
%                                  where you plot the answer.
%       2b. Use white space:       Matlab does not track whitespace, so add
%                                  an spaces to make similar equations 
%                                  align or comments legable. Example:

%             L_smooth(:,j)=Cl_smooth(:,j).*((1/2)*rho*V(:,j).^2*c);
%             L_bumpy(:,j)=Cl_bumpy(:,j).*((1/2)*rho*V(:,j).^2*c);
%
%               versus
% 
%             L_smooth(:,j) = Cl_smooth(:,j) .* ((1/2)*rho*V(:,j).^2*c);
%             L_bumpy (:,j) = Cl_bumpy (:,j) .* ((1/2)*rho*V(:,j).^2*c);
%
% 3. Use obvious varaible names: Is "t" time, thickness, torque,
%                                temperature, or what?
%
% 4. Keep notes on paper: For example, draw a picture of the geometry of
%                         a propeller in your notebook before  you compute  
%                         it in Matlab.  Your code is an implementation of
%                         the picture you drew and the equations you listed
%                         in your notes.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; close all;
set(0,'DefaultFigureWindowStyle','docked') 

cd ../propParams
dir
prop_name = input('Propeller geometry file [blade_geometry]: >>> ','s');

geometryFile = [prop_name '.dat'];

[r_R,P_D,rake,deg_skew,C_D,F0_C,T0_D] = textread(geometryFile,'%f%f%f%f%f%f%f','headerlines',1);
numSections = length(r_R);
disp(sprintf('Propeller geometry file %s has %d sections...',prop_name,numSections));

figure('Name','cross section profiles');
title('Cross section profiles');

for section = 1:numSections
    [x_sec_X x_sec_Y] = makeXsec(geometryFile,section,numPoints);

    if numSections < 13
        subplot (4,round(numSections/4),section);
        plot(x_sec_X, x_sec_Y,'LineWidth',2);
    end    
end

figure('Name','cross section profiles wrapped, skewed, and raked')
    subplot(1,2,1), title('front view of cross sections')
    hold on;
    for section=1:numSections
        hold on;
        plot3(localChord(section,:),localSpan(section,:),localHeight(section,:) );
    end
    
    subplot(1,2,2), title('end-on view of sections');
    hold on;
    for section=1:numSections
        plot3(localChord(section,:),localSpan(section,:),localHeight(section,:) );
    end
view(0,0)

        
%%%% Plot blades of a full propeller
figure('Name',sprintf('%d propeller blades',Z))
    hold on;
    Xsurf = permute(LocalChord,[2 1 3]);
    Ysurf = permute(LocalSpan,[2 1 3]);
    Zsurf = permute(localHeight,[2 1 3]);
    for blade=1:Z
        surfl(Xsurf(:,:,blade),Zsurf(:,:),Ysurf(:,:,blade))
    end
    shading interp
    colormap(copper);
    axis image
    title('view from front of propeller')
    view(180,0)

%%%% Output text files of each section that can be read in by a CAD program
fid = fopen([prop_name '-RhinoFull.txt'],'w');
    for j=1:length(localChord(section,:))
        fprintf(fid,'%f,%f,%f\n',Diam*localChord(section,j),...
            0.95*Diam*localSpan(section,j),Diam*localHeight(section,j) );
    end
fclose(fid);

disp('Use ReadCommandFile to generate the propeller.')

        