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


%clear all; close all;

%CONSTANTS (all in SI)
T_inf = 300;  %Ambient Temperature K
T_Sky = 200;  %Sky Radiation Temperature K
d = 30/1000;  %Max Board Diameter m
t = 5/1000;   %Board Thickness m
L=d;          %PVC half-length 
Rcont_R = 5e-2; %Thermal Resistance  R and Wall
Rcont_C = 5e-3; %Thermal Resistance  C and Wall
k = .375;         %Thermal conductivity of board
h = 10;         %convection coefficient
R = 2;          %Resistance
options = optimset('display','off');
T0 = [600 500 400];  %guess temperatures


%EXAMPLE CALL
curr = 1.8;
Rboard = d/k;  %d is distance between R and C

[TansT,HeatT] = CircuitTemperatures(curr,R,200,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
TansT
HeatT.Capacitor

TCdesired = 380;
[y, Heat] = MinBoardResisEq(Rboard,TCdesired,curr,R,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);

TCdesired_a = [310:2:380];
Power_a = [1:1:10];

[X_TC,Y_Power] = meshgrid(TCdesired_a,Power_a);
for i=1:size(X_TC,1)
    [i,size(X_TC,1)]
    for j=1:size(X_TC,2)
        current = sqrt(Y_Power(i,j)/R);
        [Z_Rboard(i,j)] = MinBoardResis(X_TC(i,j),current,R,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
    end
end

createfigureISO(X_TC,Z_Rboard,Y_Power,Power_a);


[C,h]= contour(X_TC,Z_Rboard,Y_Curr);ylim([0 0.2]);clabel(C,h);

for i=1:length(TCdesired_a);
    [Rboard_a(i)] = MinBoardResis(TCdesired_a(i),curr,R,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
    [Rboard_a2(i)] = MinBoardResis(TCdesired_a(i),1.4,R,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
end



figure;plot(TCdesired_a,Rboard_a,TCdesired_a,Rboard_a2);xlabel('Max Capacitor Temp (K)');ylabel('Min Thermal Resistance');
legend('1.8','1.4');grid on;ylim([0 .2]);  %text(0.06,475,['Current = ', num2str(curr), ' A']);




Tans = fsolve(@RLC_Thermal_Equations,T0,options,curr,R,Rboard,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
%extra call to get Heat Structure (not needed)
[y, Heat] = RLC_Thermal_Equations(Tans,curr,R,Rboard,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);

[TansT,HeatT] = CircuitTemperatures(curr,R,Rboard,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf)

%MAKES Temperature vs Power plot
%Rcont_R = 5e-2; %Thermal Resistance  R and Wall
%clear Power_a TR_a TC_a;
Rboard = d/k/(d*t);
Power_a = [0:0.2:8];
for i=1:length(Power_a)
    curr = sqrt(Power_a(i)/R);
    Tans = fsolve(@RLC_Thermal_Equations,T0,options,curr,R,Rboard,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
    TR_a(i) = Tans(1);
    TC_a(i) = Tans(2);
end
%TempPlot(X1, YMatrix1)
figure;plot(Power_a,TC_a,Power_a,TR_a,'LineWidth',3);xlabel('Power (W)');ylabel('Temperature (K)');
legend('T Capacitor','T Resistor');grid on;text(3,450,['R to C Thermal Resistance = ', num2str(Rboard), ' K/W'],'BackgroundColor',[1 1 1]);

%Makes temperature vs board thermal resistance plot
Rboard_a = [.004:0.002:0.1];
for i=1:length(Rboard_a)
    curr = 1.8;
    Rboard = Rboard_a(i); 
    Tans = fsolve(@RLC_Thermal_Equations,T0,options,curr,R,Rboard,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
    TR_ab(i) = Tans(1);
    TC_ab(i) = Tans(2);
end
figure;plot(Rboard_a,TC_ab,Rboard_a,TR_ab);xlabel('Thermal Resist (m*m*degK/W)');ylabel('T (K)');
legend('T Cap','T Resis');grid on; text(0.06,475,['Current = ', num2str(curr), ' A']);

%makes temperature vs length between R and C plot
length_a = [0.001:0.001:0.03];
for i=1:length(length_a)
    curr = 1.8;
    Rboard = length_a(i)/400/6.53e-7+22*2;%length_a(i)/k; 
    Tans = fsolve(@RLC_Thermal_Equations,T0,options,curr,R,Rboard,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf);
    TR_aL(i) = Tans(1);  % predicted tempreature of R
    TC_aL(i) = Tans(2);  % predicted temperature of C
    
    % Add one line to call cost function. Yu Long
    [Cost(i), kk(i)] = RLC_MfgCost_Equations(length_a(i),TR_aL(i),TC_aL(i)); %call manufacturing cost function by varying length and temperature. Yu Long
    % Originally, I think the conduction coeff kk should feedback to rerun the heat simulation to
    % predict the new temperature, but it looks like there will be an iteration,
    % so to simplify the problem and demo process, let's assume the k does not change with board material.
end
figure;plot(length_a,TC_aL);xlabel('Distance R to C (m)');ylabel('T Capacitor (K)');
grid on; text(0.06,475,['Current = ', num2str(curr), ' A']);%text(0.01,300,['Current = ', num2str(curr), ' A']);

% Add three line to plot the cost curves.
figure;plot(length_a,Cost);xlabel('Distance R to C (m)');ylabel('Cost ($)');  % Yu Long
%figure;plot(length_a,kk);xlabel('Distance R to C (m)');ylabel('heat conduction coeff (W/m^2)'); % Yu Long
figure; plot (TC_aL, Cost);xlabel('T Cap (K)');ylabel('Cost ($)'); % Yu Long