function [ DeltaT,Voc,Uoc,effteg,Riteg,Iload,Uload,tcond,Pelec,Pr,Pa ] = tegmoduleSiGe( Th,Tc,n )
%   The Thermoelectric Generator (TEG) function "tegmoduleSiGe" enables to simulate
%   the behavior of a thermoelectric generator module made of a selected material.
%   This function contains general fundamental laws about TEG, however,
%   some equations and parameters are specific to the chosen material.  

% 3 OPTIONS OF THERMOELECTRIC MATERIALS:
%   1) Option 1 - Bi2Te3 
%           Temperature range 200 to 500K
%   2) Option 2 - PbTe/TAGS
%           Temperature range 300 to 700K
%   3) Option 3 - Si0.8Ge0.2
%           Temperature range 500k to 1000K for optimal efficiency
%   This script can be easily adapted for other thermoelectric materials.

% Definition of input parameters
Th; %temperature at the "hot" sink (K)
Tc; %temperature at the "cold" sink (K)
n; %number of TC in assembled in serie

Tm=(Th+Tc)/2; %Define the mean temperature (K)

%   OPTION 1 - Bi2Te3

Rint=9.75e-3; %internal resistance of one thermocouple (Ohm)
S=372.2e-6; %Seebeck coefficient (V/K)
ZT=0.86; %Figure of Merit at a given FOM temperature (dimensionless)
FOMtemp=293.15; %temperature at which the FOM is given (K)

%   OPTION 2 - PbTe/TAGS

% Rint=11.4e-3; %internal resistance of one thermocouple (Ohm)
% S=280e-6; %Seebeck coefficient (V/K)
% ZT=0.85; %Figure of Merit a FOM temperature (dimensionless)
% FOMtemp=505; %temperature at which the FOM is given (K)

%   OPTION 3 - Si0.8Ge0.2
% 
% hleg = 0.0049; %leg height (m)
% Aleg = 0.0025*0.0025; %Cross section of a leg (m2)
% 
% TC_internal_resistance = @(T) (-4.37e-14*T^3 + 7.86e-11*T^2 - 1.96e-8*T + 2.54e-5 + 6.51e-12*T^2 + 9.75e-9*T + 7.4e-6)*hleg/Aleg;
% Rint = TC_internal_resistance(Tm); %internal resistance of the TC (Ohm)
% 
% seebeck_SiGe = @(x) -2.00e-10*x^2 + 6.39e-7*x + 1.06e-4;    %SEEBECK coefficient function for SiGe (V/K)
% S = seebeck_SiGe(Tm);
% 
% FOM = @(Th) 4.2857e-7*Th^2 + 7.5886e-4*Th - 0.17197;        %Figure Of Merit
% ZT = FOM(Th);  %Figure of Merit at given temperature of hot side (dimensionless)

%   TEST DAVID's MATERIAL (properties to be confirmed before any
%   publication)

% hleg = 0.0049; %leg height (m)
% Aleg = 0.0025*0.0025; %Cross section of a leg (m2)
% TC_internal_resistance = @(T) (1/(100*(0.0097*T^2 - 6.8502*T +1678)))*hleg/Aleg; %Material from David
% Rint = TC_internal_resistance(Tm) %internal resistance of the TC (Ohm)
% seebeck_David = @(x) 0.000002*(-0.0012*x^2 + 0.878*x + 50.013); david
% S = seebeck_David(Tm);
% FOM = @(Th)-1e-5*Th^2 + 0.011*Th - 1.6559; David
% ZT = FOM(Th);  %Figure of Merit at given temperature of hot side (dimensionless)

%COMPUTATIONS

%temperature difference between the hot and the cold side of the thermoelectric module (K)
DeltaT=Th-Tc; 

%TC Open circuit voltage (V)
Voc=DeltaT*S;

%TEG Open circuit voltage (V)
Uoc=n*Voc;

%TEG internal resistance (Ohm)
Riteg=n*Rint;

%TEG load resistance which matches Riteg to maximize power output
Rlteg = Riteg;

%load current (A)
Iload=Uoc/(Riteg+Rlteg);

%Load voltage (V)
Uload=Uoc-Iload*Riteg;

%Figure of Merit (K-1)
Z=ZT/Th;

%thermal conductance in TEG (W/K)
tcond=(S*S)/(Rint*Z);

% POWER computations
Pelec = Uload*Iload; %Electrical power delivered by by the TEG module (W)
Pr = n*(0.5*Rint*Iload*Iload+S*Iload*Tc+tcond*DeltaT); %rejected power at the cold side of the TEG module (W)
Pa = n*(-0.5*Rint*Iload*Iload+S*Iload*Th+tcond*DeltaT); %absorbed power at the hot side of the TEG module (W)

%Compute the efficiency of energy conversion by the thermoelectric module
% The total efficiency is the product of the "carnot efficiency" times the
% "efficiency of the thermoelectric material"

%Maximum conversion efficiency of the TEG Module (dimensionless)
%EFFICIENCY
carnoteff=(Th-Tc)/Th; %Carnot efficiency
A=(1+Z*Tm)^0.5; %intermediate calculation
effteg=carnoteff*((A-1)/(A+Tc/Th)); % maximum conversion efficiency of the TEG module (dimensionless)

end

