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


%this is an example script file for running several cases through heats
%without needed to call the mission file for each HEATS call

%% INPUT VALUES

MPNum = 2; %Choose the mission point number you would like to vary, this corresponds to the input file

%Choose the variables that you want to vary in this list (These would be
%the variables you want to assign some distribution
 Variable_Names = {  'Tank.Temp' ...
                     'GGFlow' ...
                     'ABFlow' ...
                     'EngActFlow' ...
                     'ECS.Heat' ...
                     'Hyd.Heat' ...
                     'Gen.Heat' ...
                     'EngOil.Heat' ...
                     'EngOil.Flow'};

%YOU SHOULD STORE THE DISTRIBUTIONS IN Variable_mat
%Set up a matrix 'Variable_Mat' where the columns correspond to "Variable_Names" and the rows are each a different value for the variable name                 
%As an example, row one will just be taken straight from the input file and
%row 2 will be have each variable 5% higher
Conditions = TMSMission(); %Read in the Input File seperately from HEATS
for i=1:length(Variable_Names)
   eval(['Variable_Mat(1,',INT2STR(i),')= Conditions{',INT2STR(MPNum),'}.',Variable_Names{i},';']);
end
Variable_Mat(2,:) = Variable_Mat(1,:) + Variable_Mat(1,:)*0.05; %make 5 percent higher


% LETS DO THE FIRST RUN TO SIZE THE PUMPS, SHOULD ONLY NEED TO DO THIS ONCE
% design fuel pumps
Conditions = TMSMission(); %Read in the Input File seperately from HEATS
TMSTemp.Init.Conditions = Conditions{17}; %17 is the sizing point
TMSTemp = HEATS('BaselineTMSConfigNoMissionFile','nocostgoalattain',TMSTemp.Init);
TMSDesign = TMSTemp{1};


%SUJIT, THIS IS THE FUNCTION YOU SHOULD CALL WITH THE MATRIX INPUT TO RUN
%ALL OF YOUR CASES
%CALL THIS ROUTINE TO RUN HEATS FOR EACH ROW IN Variable_Mat
TMS = TMSDemoMC(Variable_Mat,Variable_Names,MPNum,Conditions,TMSDesign)


%EVERYTHING YOU NEED SHOULD BE IN TMS
%YOU CAN FOLLOW THE EXAMPLE IN TMSEXPORTPM.M IF YOU WANT TO WRITE OUTPUT TO
%A CSV FILE

%The variable TMS{i}.Opt.DanFlag will contain 1 if the solution converged
%It is a good idea to check
fastplot(TMS,'Opt.DanFlag');



%% EVERYTHING BELOW RELATES TO HX SIZING, LETS SKIP THIS FOR NOW TO MAKE
%% THE CODE FASTER


% % 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
% % Conditions = TMSMission();
% % TMS.Init.Conditions = Conditions{17};
% % TMSTemp = HEATS('BaselineTMSConfig','nocostgoalattain',TMS.Init);
% % 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';
% % 
% % %CALL MISSION FILE SEPERATELY
% % 
% % 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)
% % for i=1:size(TMS,1);
% %     if TMS{i}.EngOil.Outlet.Temp > TMS{i}.Conditions.MaxFuelTemp
% % 
% % % 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);
% %         if(TMS{i}.Conditions.Mach > 0.3)
% % % size ram scoop area/mass required for air flow
% %             TMS{i}.RamScoop = InletRam(TMS{i}.RamScoop); 
% %             TMS{i}.RamScoop = InletRamWeight(TMS{i}.RamScoop);
% %         else            
% %             TMS{i}.RamScoop.Outlet = TMS{i}.RamScoop.Inlet;
% %             TMS{i}.RamScoop.Mass.Total = 0.0;
% %         end
% % 
% % % 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 
% %         if(TMS{i}.Conditions.Mach <= 0.3)
% %             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;
% %         else
% %             TMS{i}.Fan.Mass.Total = 0.0;
% %             TMS{i}.Fan.Pars.Power = 0.0;
% %         end
% %     else
% %         TMS{i}.RamScoop.Mass.Total = 0.0;
% %         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;
% % end
