% 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.
% 


RTTCoolerPinchTemp = 100/1.8;
RTTCoolerColdSideDP = ConvertUnits('psia', 0.5, 'import');
RTTCoolerHotSideDP = ConvertUnits('psia', 10.0, 'import');

optimoptionsHX = optimset('TolFun', 1e-4,'TolCon', 1e-3,'MaxFunEvals', 350, 'Display','iter');
OptionsHX = SetHEATS('plot','off','save', 'off','ScottBounds','on');

% design fuel pumps
TMS = HEATS('BaselineTMSConfig','nocostgoalattain',{'D3'});
TMSDesign = TMS{17};

% run performance points with RTT off to determine which points require RTT
% (those with engine oil hex outlet temperature > max allowable)
TMSDesign.Opt.Model = 'performanceRTToff';
TMS = HEATS('BaselineTMSConfig','nocostgoalattain',TMSDesign);

% run performance points again that require RTT
for i=1:size(TMS,1);
    if TMS{i}.EngOil.Outlet.Temp > TMS{i}.Conditions.MaxFuelTemp
        TMS{i}.Opt.Model = 'performanceRTTon';
        TMSRTT = HEATS('BaselineTMSConfig','nocostgoalattain',TMS, {TMS{i}.Conditions.MissionPoint});
        TMS{i} = TMSRTT{i};
    end
end

% size ram circuit components (ram scoop, fan, fuel cooler) at hot day ground condition
i = 15;

% determine ram fuel cooler flow rate
    TCout = TMS{i}.RTTCooler.Inlet.Temp - RTTCoolerPinchTemp;
    PCout = TMS{i}.RamScoop.Outlet.Press - RTTCoolerColdSideDP;
    Dum = TransportProps(PCout, TCout, TMS{i}.RamScoop.Outlet.Fluid); HCout = Dum.Enthalpy;
    TMS{i}.RamScoop.Inlet.Mdot = TMS{i}.RTTCooler.Pars.Q/(HCout - TMS{i}.RamScoop.Outlet.Enthalpy);
    TMS{i}.RamScoop.Outlet = TMS{i}.RamScoop.Inlet;

% fuel cooler sizing
% cold side conditons
    RTTCoolerHX.Coldside.Inlet = TMS{i}.RamScoop.Outlet;
    RTTCoolerHX.Coldside.Pars.DeltaP = RTTCoolerColdSideDP;
        
% hot side conditions
    RTTCoolerHX.Hotside.Inlet = TMS{i}.RTTCooler.Inlet;
    RTTCoolerHX.Hotside.Pars.DeltaP = RTTCoolerHotSideDP;
        
% define required configuration parameters
    RTTCoolerHX.Hotside.Geom.Name = '1/9-24.12'     ;
    RTTCoolerHX.Coldside.Geom.Name = '1/8-15.2'   ;
        
    RTTCoolerHX.Pars.Material.Name = 'Aluminum'     ;
    RTTCoolerHX.Pars.Type = 'PlateFin'              ;
    RTTCoolerHX.Pars.Mode = 'Hex'                   ;
    RTTCoolerHX.Pars.FlowConfig = 'Cross Unmixed'   ;
    RTTCoolerHX.Pars.ReBounds = 'on'                ;
    RTTCoolerHX = SurfaceParams(RTTCoolerHX,'.Hotside');
    RTTCoolerHX = SurfaceParams(RTTCoolerHX,'.Coldside');
        
% specify desired target type and pressure drops
    RTTCoolerHX.Pars.TargetType = 'hot side outlet';
    RTTCoolerHX.Pars.Target = TMS{i}.RTTCooler.Outlet.Temp;
    RTTCoolerHX.Pars.MaxDeltaPH = RTTCoolerHotSideDP;
    RTTCoolerHX.Pars.MaxDeltaPC = RTTCoolerColdSideDP;

% set initial guesses and bounds
    RTTCoolerHX.Opt.Names.X{1} = ['Hotside.Geom.Length'] ;
    RTTCoolerHX.Opt.Names.X{2} = ['Coldside.Geom.Length'];
    RTTCoolerHX.Opt.Names.X{3} = ['Geom.Height'];
    RTTCoolerHX.Opt.X0(1) = [ 0.1 ]; % Hotside Lenght initial guesse.
    RTTCoolerHX.Opt.X0(2) = [ 0.1] ; % Coldside Lenght initial guesse.
    RTTCoolerHX.Opt.X0(3) = [  0.25]; % Height initial guesse.

    RTTCoolerHX.Opt.Bounds(1,:) = [0.05  2];  %Hotside Length
    RTTCoolerHX.Opt.Bounds(2,:) = [0.05  2];  %Coldside Length
    RTTCoolerHX.Opt.Bounds(3,:) = [0.05  10];  %Height

    RTTCoolerHX.WidgetOpt.X0 = RTTCoolerHX.Opt.X0;
    RTTCoolerHX.WidgetOpt.Bounds = RTTCoolerHX.Opt.Bounds;

    HX = HEATS('WidgetSizeHexConfig', 'mincon', RTTCoolerHX, OptionsHX, optimoptionsHX);
    TMS{i}.RTTCoolerHX = HX{1};
        
% fan sizing and performance 
    TMS{i}.Fan.Inlet = TMS{i}.RTTCoolerHX.Coldside.Outlet;
    TMS{i}.Fan.Pars.PR = (TMS{i}.Fan.Inlet.Press + 2 * RTTCoolerColdSideDP)/TMS{i}.Fan.Inlet.Press;
    TMS{i}.Fan.Pars.Eta = 0.7;
    TMS{i}.Fan = SimpleFan(TMS{i}.Fan,'Simple');
    TMS{i}.Fan.Mass.Total = 0.25*TMS{i}.Fan.Pars.Power/1000;
    
for i=1: size(TMS,1);
    TMS{i}.RamScoop.Mass.Total = 0.0;
    if i ~= 15  
        TMS{i}.RTTCoolerHX.Mass.Total = 0.0;
        TMS{i}.Fan.Mass.Total = 0.0;
        TMS{i}.Fan.Pars.Power = 0.0;
    end
    TMS{i}.Sys.PowerShaft = TMS{i}.MFPump.Pars.Power + TMS{i}.ABPump.Pars.Power + TMS{i}.ActPump.Pars.Power;
    TMS{i}.Sys.PowerElec = TMS{i}.Fan.Pars.Power;
    TMS{i}.Sys.Mass = TMS{i}.MFPump.Mass.Total + ...
                      TMS{i}.ABPump.Mass.Total + ...
                      TMS{i}.ActPump.Mass.Total + ...
                      TMS{i}.RamScoop.Mass.Total + ...
                      TMS{i}.Fan.Mass.Total + ...
                      TMS{i}.RTTCoolerHX.Mass.Total;
    TMS{i}.Sys.PenaltyP = (TMS{i}.Sys.PowerShaft/1000+TMS{i}.Sys.PowerElec/1000)/100 + TMS{i}.Sys.Mass/200;
    TMS{i}.Sys.PenaltyC = 0.1*(TMS{i}.MFPump.Pars.Power + TMS{i}.ABPump.Pars.Power)+0.075*TMS{i}.ActPump.Pars.Power + ...
                          0.2*TMS{i}.Fan.Pars.Power + ...
                          500*TMS{i}.RTTCoolerHX.Mass.Total;
end