% Delivered to the Government in Accordance with Contract FA8650-10-C-7080
% 
%  (c) 2011 United Technologies Corp.
% Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
%  
% Permission is hereby granted, free of charge, to any person obtaining a copy of this data, including any software or models in source or binary form, as well as any drawings, specifications, and documentation (collectively "the Data"), to deal in the Data without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data is furnished to do so, subject to the following conditions:
%  
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Data.
%  
% THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE USE OR OTHER DEALINGS IN THE DATA.
% 

function [out,output,in,params,vars_keep,discrete_vars_names,discrete_vars] = pump_model(I_c,A_c)

% Inputs and Outputs for pump
% For the discrete inputs: Only the type has been considered.

% for the discrete inputs
fluid = {'LinearFuel'};%,'IdealAir'};
type = {'radial','gear'};



% names of the different uncontrollable variables
params{1} = 'mdot';
params{2} = 'T_i';
params{3} = 'P_i';
params{4} = 'Delta P';
params{5} = 'N';

vars_keep = [];
% this loop finds which variables to keep
for i = 1:size(I_c,2),   
    vars_keep = [vars_keep find(strcmp(I_c{i},params))];
    if isempty(vars_keep),
        error('check component selection')
    end
end

% Default input ranges:
default.lower = [0.2 100 1e6 3e6 5000];
default.upper = [2 300 3e6 6e6 10000];

% for continuous inputs

for i = 1:size(params,2),
    if ismember(i,vars_keep),
        j = find(vars_keep(:)==i);
        in{i} = A_c.lower(j):((A_c.upper(j)-A_c.lower(j))/5):A_c.upper(j); 
       
    else
        in{i} = default.lower(i):((default.upper(i)-default.lower(i))/5):default.upper(i); % temp_inlet
    end
end


% Running the detailed pump model

for i = 1:length(in{1}),
    for j = 1:length(in{2}),
        for k = 1:length(in{3}),
            for l = 1:length(in{4}),
                for m = 1:length(in{5}),
                    Pump.Inlet.Mdot = in{1}(i);
                    Pump.Inlet.Temp = in{2}(j);
                    Pump.Inlet.Press = in{3}(k); 
                    Pump.Pars.Mode = 'Size';
                    Pump.Pars.DeltaP = in{4}(l);

                    for fl = 1:size(fluid,2),
                        Pump.Inlet.Fluid = DefineFluid(fluid{fl});

                        PumpOut{size(type,2)*(fl-1)+1} = PumpGear(Pump); % first type
                        Pump.Pars.N = in{5}(m);
                        PumpOut{size(type,2)*fl} = PumpRadial(Pump); % second type
                    end
                    
                    % outputs
                    
                    for x = 1:size(PumpOut,2),
                        mdotO{x}(i,j,k,l,m) = PumpOut{x}.Outlet.Mdot;
                        T_o{x}(i,j,k,l,m) = PumpOut{x}.Outlet.Temp;
                        P_o{x}(i,j,k,l,m) = PumpOut{x}.Outlet.Press;
                        eff{x}(i,j,k,l,m) = PumpOut{x}.Pars.Eta; 
                        pwr{x}(i,j,k,l,m) = PumpOut{x}.Pars.Power;

                        PumpOut{x} = PumpWeight(PumpOut{x});
                        W{x}(i,j,k,l,m) = PumpOut{x}.Mass.Total;
                    end
                end
            end
        
        end
    end
end

% output variables

out{1} = mdotO;
out{2} = T_o;
out{3} = P_o;
out{4} = W;
out{5} = eff;
out{6} = pwr;

%output names
output = {'mdotO','T_o','P_o','W','eff','pwr'};

% discrete variables
discrete_vars_names = {'type'};
discrete_vars = {type};
