% 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 System = BaselineTMS(System)
% Baseline TMS System Model
% Features:
%   Simple fuel system model

% CVS Version control line. DO NOT EDIT!
System.Opt.Rev.BaselineTMS = '$Revision: 1.0 $';

% set ambient air conditions
System.Ambient.Pars.Alt           = System.Conditions.Altitude;
System.Ambient.Pars.Mode          = 'ISA';
System.Ambient.Pars.DayType       = System.Conditions.DayType;
System.Ambient                    = PropAtmosphere(System.Ambient);

% initialize all input and loop starting streams
% the input streams are

% fuel tank
System.FuelTank.Outlet            = System.Conditions.Tank;
System.FuelTank.Outlet.Mdot       = System.Conditions.GGFlow + System.Conditions.ABFlow + System.Param.RTTFuelFlow;
System.FuelTank.Outlet            = TransportProps(System.FuelTank.Outlet);

% merge 1
System.Merge1.Inlet{2}.Fluid      = System.Conditions.Tank.Fluid;
System.Merge1.Inlet{2}.Temp       = System.Param.EngActTempOut;
System.Merge1.Inlet{2}.Mdot       = System.Conditions.EngActFlow;

% merge 2
System.Merge2.Inlet{2}.Fluid      = System.Conditions.Tank.Fluid;
System.Merge2.Inlet{2}.Temp       = System.Param.ActPumpRecircTemp;
System.Merge2.Inlet{2}.Mdot       = System.Param.ActPumpRecircFlow;

% model loop
% ecs heat load out
System.ECS.Inlet                  = System.FuelTank.Outlet;
System.ECS.Pars.Q                 = System.Conditions.ECS.Heat;
System.ECS.Pars.DeltaP            = System.Conditions.ECS.DeltaP;
System.ECS                        = HeatLoad(System.ECS);

% aircraft actuation heat load out
System.Hyd.Inlet                  = System.ECS.Outlet;
System.Hyd.Pars.Q                 = System.Conditions.Hyd.Heat;
System.Hyd.Pars.DeltaP            = System.Conditions.Hyd.DeltaP;
System.Hyd                        = HeatLoad(System.Hyd);

% generator heat load out
System.Gen.Inlet                  = System.Hyd.Outlet;
System.Gen.Pars.Q                 = System.Conditions.Gen.Heat;
System.Gen.Pars.DeltaP            = System.Conditions.Gen.DeltaP;
System.Gen                        = HeatLoad(System.Gen);

% merge 1
System.Merge1.Inlet{1}            = System.Gen.Outlet;
System.Merge1.Inlet{2}.Press      = System.Merge1.Inlet{1}.Press;
System.Merge1                     = Mixer(System.Merge1);

% afterburner split
System.ABSplit.Inlet              = System.Merge1.Outlet;
System.ABSplit.Outlet{1}.Mdot     = System.Conditions.ABFlow;
System.ABSplit.Outlet{2}.Mdot     = System.ABSplit.Inlet.Mdot - System.ABSplit.Outlet{1}.Mdot;
System.ABSplit                    = Splitter(System.ABSplit);

% Afterburner pump
System.ABPump.Inlet               = System.ABSplit.Outlet{1};
System.ABPump.Pars.GearRatio      = System.Conditions.ABPumpGearRatio;
System.ABPump.Pars.N              = System.Conditions.N2*System.ABPump.Pars.GearRatio;
System.ABPump.Pars.Mode           = System.Opt.Model;
if (strncmp(System.Opt.Model,'performance',11))
    System.ABPump.Pars.Mode       = 'performance';
end
if (System.ABPump.Inlet.Mdot == 0)
    System.ABPump.Outlet          = System.ABPump.Inlet;
else
    System.ABPump                 = PumpRadial(System.ABPump);
end

% Main Fuel Pump
System.MFPump.Inlet               = System.ABSplit.Outlet{2};
System.MFPump.Pars.GearRatio      = System.Conditions.MFPumpGearRatio;
System.MFPump.Pars.N              = System.Conditions.N2*System.MFPump.Pars.GearRatio;
System.MFPump.Pars.Mode           = System.Opt.Model;
if (strncmp(System.Opt.Model,'performance',11))
    System.MFPump.Pars.Mode       = 'performance';
end
System.MFPump                     = PumpRadial(System.MFPump);

% merge 2
System.Merge2.Inlet{1}            = System.MFPump.Outlet;
System.Merge2.Inlet{2}.Press      = System.Merge2.Inlet{1}.Press;
System.Merge2                     = Mixer(System.Merge2);

% actuation pump split
System.ActSplit.Inlet             = System.Merge2.Outlet;
System.ActSplit.Outlet{1}.Mdot    = System.Conditions.EngActFlow + System.Param.ActPumpRecircFlow;
System.ActSplit.Outlet{2}.Mdot    = System.ActSplit.Inlet.Mdot - System.ActSplit.Outlet{1}.Mdot;
System.ActSplit                   = Splitter(System.ActSplit);

% actuation pump
System.ActPump.Inlet              = System.ActSplit.Outlet{1};
System.ActPump.Pars.DeltaP        = System.Conditions.ActPumpDeltaP;
if (strcmp(System.Opt.Model,'size'))
    System.ActPump.Pars.Mode      = 'size';    
    System.ActPump                = PumpGear(System.ActPump);    
    System.ActPump.Pars.GearRatio = System.ActPump.Pars.N/System.Conditions.N2;
end
if (strncmp(System.Opt.Model,'performance',11))
    System.ActPump.Pars.Mode      = 'performance';     
    System.ActPump.Pars.N         = System.Conditions.N2*System.ActPump.Pars.GearRatio;  
    System.ActPump                = PumpGear(System.ActPump);   
end

% engine actuation
System.EngAct.Inlet               = System.ActPump.Outlet;
System.EngAct.Inlet.Mdot          = System.Conditions.EngActFlow;
System.EngAct.Pars.DeltaP         = System.EngAct.Inlet.Press - System.Merge1.Inlet{2}.Press;
System.EngAct.Pars.Q              = 0.0;
System.EngAct                     = HeatLoad(System.EngAct);

% engine oil
System.EngOil.Inlet               = System.ActSplit.Outlet{2};
System.EngOil.Pars.Q              = System.Conditions.EngOil.Heat;
System.EngOil.Pars.DeltaP         = System.Conditions.EngOil.DeltaP;
System.EngOil                     = HeatLoad(System.EngOil);

% MFTV
System.MFTV.Inlet                 = System.EngOil.Outlet;
System.MFTV.Outlet{1}.Mdot        = System.Param.RTTFuelFlow;
System.MFTV.Outlet{2}.Mdot        = System.Conditions.GGFlow;
System.MFTV                       = Splitter(System.MFTV);

% determine RTT fuel cooler heat
System.RTTCooler.Inlet            = System.MFTV.Outlet{1};
System.RTTCooler.Outlet           = System.FuelTank.Outlet;
System.RTTCooler.Outlet.Mdot      = System.RTTCooler.Inlet.Mdot;
System.RTTCooler.Pars.Q           = System.RTTCooler.Inlet.Mdot*(System.RTTCooler.Inlet.Enthalpy-System.RTTCooler.Outlet.Enthalpy);

% ram scoop
System.RamScoop.Inlet             = System.Ambient;
System.RamScoop.Pars.Mach         = System.Conditions.Mach;
System.RamScoop.Pars.Type         = 'Scoop';
System.RamScoop.Pars.Mode         = 'Size';
System.RamScoop.Inlet.Mdot        = 1; % set flow rate equal to 1 just to get ram scoop outlet conditions
if(System.Conditions.Mach > 0.3)
    System.RamScoop               = InletRam(System.RamScoop);
else
    System.RamScoop.Outlet        = System.RamScoop.Inlet;
end
System.RamScoop.Outlet            = TransportProps(System.RamScoop.Outlet);

%************************************************************************************************
% Equaltiy Constraints 

j = 1;
% loop closings
System.Opt.Names.EqConstr{j} = 'Engine Act Loop Temperature';
System.Opt.EqConstr(j) = abs(System.Param.EngActTempOut-System.EngAct.Outlet.Temp)/50; j=j+1;
System.Opt.Names.EqConstr{j} = 'Act Pump Recirc Loop Temperature';
System.Opt.EqConstr(j) = abs(System.Param.ActPumpRecircTemp-System.ActPump.Outlet.Temp)/50; j=j+1;

if(strcmp(System.Opt.Model,'performanceRTTon'))
% gas generator temp
    System.Opt.Names.EqConstr{j} = 'Gas Generator Temperature';
    System.Opt.EqConstr(j) = abs(System.MFTV.Outlet{2}.Temp-System.Conditions.MaxFuelTemp)/50; j=j+1;
end

% Inequality constraints

% Cost Functions

% Bounds
%*********************************************************************
% CVS Version Control Log Data. DO NOT EDIT BELOW THIS LINE!
% $Log: BaselineTMS.m,v $