% 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 [y, Heat] = RLC_Thermal_Equations(T,curr,R,Rboard,Rcont_R,Rcont_C,d,t,L,h,T_Sky,T_inf)
sigma = 5.67e-8; %stefen-boltzman constant

T_R = T(1);
T_C = T(2);
T_Wall = T(3);

r=d/2;
Aconv = (0.5*pi*r^2)/2; %divide areas in half
Acontact = pi*d*t/2;
A_R_C = 1; %Talking about total Resistance d*t; %area for conduction between Resis and Capac

A_R = Aconv;  A_C = Aconv; %upper surface area of Resis and Capac nodes
A_Wall = pi*d*L*2;  %wall area above circuit board 2x since wall on both sides

r1 = sqrt((A_R)*2/pi);
%calculate view factors
Ri = r1/L; Rj = r/L; S = 1 + (1 + Rj^2)/Ri^2;
Fij = 0.5*(S-sqrt(S^2-4*(r1/r)^2));

F_R_Sky = Fij*2; %2x since open on both sides 
F_R_Wall = 1-Fij;

F_C_Sky = Fij*2;
F_C_Wall = 1-Fij;

F_Wall_R = A_R/A_Wall*F_R_Wall;  %reciprocity
F_Wall_C = A_C/A_Wall*F_C_Wall;
F_Wall_Sky = 1-F_Wall_R-F_Wall_C;

%ENERGY BALANCE FOR RESISTOR
Heating = -curr^2*R;  %- heat entering the system
R_conv = 2*h*A_R*(T_R-T_inf); %2 for both sides of board 
R_rad_wall = sigma*A_R*F_R_Wall*(T_R^4 - T_Wall^4);
R_rad_sky = sigma*A_R*F_R_Sky * (T_R^4 - T_Sky^4);
R_cond_wall = (Acontact/Rcont_R)*(T_R-T_Wall);
R_cond_C = (A_R_C/Rboard)*(T_R-T_C);
y(1) = Heating + R_conv + R_rad_wall + R_rad_sky + R_cond_wall + R_cond_C; 

%Energy Balance for Capacitor
C_conv = 2*h*A_C*(T_C-T_inf); 
C_rad_wall = sigma*A_C*F_C_Wall*(T_C^4 - T_Wall^4);
C_rad_sky = sigma*A_C*F_C_Sky * (T_C^4 - T_Sky^4);
C_cond_wall = (Acontact/Rcont_C)*(T_C-T_Wall);
C_cond_R = (A_R_C/Rboard)*(T_C-T_R);
y(2)=C_conv + C_rad_wall + C_rad_sky + C_cond_wall + C_cond_R; 

%Energy Balance for the Wall
Wall_conv = 2*h*A_Wall*(T_Wall-T_inf); %2 for both sides of pvc tube
Wall_rad_R = sigma*A_Wall*F_Wall_R*(T_Wall^4 - T_R^4);
Wall_rad_C = sigma*A_Wall*F_Wall_C*(T_Wall^4 - T_C^4);
Wall_rad_Sky_Inside = sigma*A_Wall*F_Wall_Sky * (T_Wall^4 - T_Sky^4);
Wall_rad_Sky_Outside = sigma*A_Wall*1*(T_Wall^4 - T_Sky^4); %F Wall-Sky = 1
Wall_cond_R=(Acontact/Rcont_R)*(T_Wall-T_R);
Wall_cond_C=(Acontact/Rcont_C)*(T_Wall-T_C);
y(3) = Wall_conv + Wall_rad_R + Wall_rad_C + Wall_rad_Sky_Inside + Wall_rad_Sky_Outside + Wall_cond_R + Wall_cond_C;

Heat.Resistor.Heating = Heating;
Heat.Resistor.Convection = R_conv;
Heat.Resistor.RadiationWall = R_rad_wall;
Heat.Resistor.RadiationSky = R_rad_sky;
Heat.Resistor.ConductionWall = R_cond_wall;
Heat.Resistor.ConductionCapacitor = R_cond_C;

Heat.Capacitor.Convection = C_conv;
Heat.Capacitor.RadiationWall = C_rad_wall;
Heat.Capacitor.RadiationSky = C_rad_sky;
Heat.Capacitor.ConductionWall = C_cond_wall;
Heat.Capacitor.ConductionResistor = C_cond_R;

Heat.Wall.Convection = Wall_conv;
Heat.Wall.RadiationInside = Wall_rad_R + Wall_rad_C;
Heat.Wall.RadiationSky = Wall_rad_Sky_Inside + Wall_rad_Sky_Outside;
Heat.Wall.ConductionR = Wall_cond_R;
Heat.Wall.ConductionC = Wall_cond_C;
