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


% Fixed environmental conditions by mission point
Conditions = TMSMission();

% Variational Parameters for Converged Design
%                       initial guess       min                         max
% EngActTempOut         400.00              TMS.Conditions.Tank.Temp    2 * TMS.Conditions.Tank.Temp
% ActPumpRecircTemp     400.00              TMS.Conditions.Tank.Temp    2 * TMS.Conditions.Tank.Temp
% ActPumpRecircFlow     0.5                 0.01                        1
% RTTFuelFlow           0.2                 0.0                         2.0

%
% size system pumps
%

Sys.Opt.Model = 'size';
% set design pressure rise
Sys.ABPump.Pars.DeltaP = ConvertUnits('psia', 1000.0, 'import');
Sys.MFPump.Pars.DeltaP = ConvertUnits('psia', 1000.0, 'import');

% set variation parameter initial guesses
% these two variational parameters have to be iterated on to drive the equality constraints to convergence
Sys.Param.EngActTempOut = 400.0;
Sys.Param.ActPumpRecircTemp = 400.0;
% these are not variational parameters for the sizing case
Sys.Param.ActPumpRecircFlow = 0.0;    % no recirc flow in pump sizing mode
Sys.Param.RTTFuelFlow = 0.0;          % no RTT in pump sizing mode

% select design mission point to run and run mission point
MPNum = 17;
Sys.Conditions = Conditions{MPNum};
Sys = BaselineTMS(Sys);
% the two equality constrains returned in the Sys structure are
% Sys.Opt.EqConstr(1) and Sys.Opt.EqConstr(2)

% save design solution in TMSDesign
TMSDesign = Sys;

%
% run performance points with RTT off to determine which points require RTT
% (those with engine oil hex outlet temperature > max allowable)
%

Sys = TMSDesign;
Sys.Opt.Model = 'performanceRTToff';

% set variation parameter initial guesses 
% these are the three varational parameters in RTToff mode
Sys.Param.EngActTempOut = 400.0;
Sys.Param.ActPumpRecircTemp = 400.0;
Sys.Param.ActPumpRecircFlow = 0.5;
% this is not a variational parameter for the RTToff mode
Sys.Param.RTTFuelFlow = 0.0;          % no RTT in RTT off mode

% select design mission point to run and run mission point
MPNum = 1;
Sys.Conditions = Conditions{MPNum};
Sys = BaselineTMS(Sys);
% the three equality constrains returned in the Sys structure are
% Sys.Opt.EqConstr(1), Sys.Opt.EqConstr(2)and Sys.ActPump.Bnd.Res

% save current solution in TMS
TMS{MPNum} = Sys;
% 
% % 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';
%         Sys = TMS{i};
% 
% % set variation parameter initial guesses 
% % these are the four varational parameters in RTTon mode
%         Sys.Param.EngActTempOut = Sys.EngAct.Outlet.Temp;
%         Sys.Param.ActPumpRecircTemp = Sys.ActPump.Outlet.Temp;
%         Sys.Param.ActPumpRecircFlow = Sys.Merge2.Inlet{2}.Mdot;
%         Sys.Param.RTTFuelFlow = 0.2;
% 
% % select design mission point to run and run mission point   
%         Sys.Conditions = Conditions{i};
%         Sys = BaselineTMS(Sys);
% % the four equality constrains returned in the Sys structure are
% % Sys.Opt.EqConstr(1), Sys.Opt.EqConstr(2), Sys.ActPump.Bnd.Res and Sys.Opt.EqConstr(3)
% 
% % save current solution in TMS
%         TMS{i} = Sys;        
%     end
% end






