%
% runmodel - application script for modelling the microscope preamplifier.
%	It calls equations.m to solve the system of linear equations to get
%	the linearized response of the circuit to various inputs, and prints
%	a standard set of response figures returned by the model.  It also
%	checks the model for saturation or cut-off conditions anywhere in
%	the circuit, and identifies places where large pulses are clipped.
%
% author: richard.t.jones at uconn.edu
% version: 10-4-2012
%

varnames;

%=== List of the unknowns in the model of the single-channel amp
% VR18C1 = voltage on VBias side of the SiPM
% VR19C2 = voltage on ground side of the SiPM
% VR1R20 = voltage on junction of R20 and R1
% VQ0c = voltage on collector of Q0
% VQ0e = voltage on emitter of Q0
% VQ0b = voltage on base of Q0
% Jc0 = current on collector of Q0
% Je0 = current on emitter of Q0
% Jb0 = current on base of Q0
% VQ1c = voltage on collector of Q1 [duplicate]
% --- = ground on emitter of Q1
% VQ1b = voltage on base of Q1
% Jc1 = current on collector of Q1
% Je1 = current on emitter of Q1
% Jb1 = current on base of Q1
% --- = ground on collector of Q2
% VQ2e = voltage on emitter of Q2
% VQ1c = voltage on base of Q2 [duplicate]
% Jc2 = current on collector of Q2
% Je2 = current on emitter of Q2
% Jb2 = current on base of Q2
% Vcc = voltage on collector of Q3
% Vba = voltage on emitter of Q3
% VQ1c = voltage on base of Q3 [duplicate]
% Jc3 = current on collector of Q3
% Je3 = current on emitter of Q3
% Jb3 = current on base of Q3
% VR7C6 = voltage on junction of R7 and C6
% I18 = current on resistor R18
% H1 = current on capacitor C1
% HS = current on the SiPM itself
% I19 = current on resistor R19
% H2 = current on capacitor C2
% IBi = current on resistor RBi
% HBi = current on capacitor CBi
% I6 = current on resistor R6
% H3 = current on capacitor C3
% I1 = current on resistor R1
% H4 = current on capacitor C4
% I3 = current on resistor R3
% I2 = current on resistor R2
% H5 = current on capacitor C5
% Vt = single-channel output voltage
% It = current on single-channel output
% VPa = filtered 5V power plane for single-channel section
% I20 = current on resistor R20
% I4 = current on resistor R4
% I5 = current on resistor R5
% I7 = current on resistor R7
% I8 = current on resistor R8
% HPa = current on capacitor C20/C21
% IPa = current on resistor R27
%
%=== List of the unknowns in the model of the summed-channel amp
% VR10C8 = voltage on junction of R10 and C8
% VQ4c = voltage on collector of Q4 [duplicate]
% VQ4e = voltage on emitter of Q4
% VQ4b = voltage on base of Q4
% Jc4 = current on collector of Q4
% Je4 = current on emitter of Q4
% Jb4 = current on base of Q4
% VPs = voltage on collector of Q8 [duplicate]
% VQ8e = voltage on emitter of Q8
% VQ4c = voltage on base of Q8 [duplicate]
% Jc8 = current on collector of Q8
% Je8 = current on emitter of Q8
% Jb8 = current on base of Q8
% VQ5c = voltage on collector of Q5
% VQ5e = voltage on emitter of Q5
% VQ5b = voltage on base of Q5
% Jc6 = current on collector of Q5
% Je6 = current on emitter of Q5
% Jb6 = current on base of Q5
% --- = ground on collector of Q6
% VQ6e = voltage on emitter of Q6 [duplicate]
% VQ6b = voltage on base of Q6
% Jc7 = current on collector of Q6
% Je7 = current on emitter of Q6
% Jb7 = current on base of Q6
% --- = ground on collector of Q7
% VQ7e = voltage on emitter of Q7
% VQ6e = voltage on base of Q7 [duplicate]
% Jc8 = current on collector of Q7
% Je8 = current on emitter of Q7
% Jb8 = current on base of Q7
% I9 = current on resistor R9
% H7 = current on capacitor C7
% IBi1 = current on resistor RBi1
% HBi1 = current on capacitor CBi1
% I11 = current on resistor R11
% H8 = current on capacitor C8
% I10 = current on resistor R10
% HE = current on capacitor CE
% IF = current on resistor RF
% I17 = current on resistor R17
% I14 = current on resistor R14
% H10 = current on capacitor C10
% ID = current on resistor RD
% H9 = current on capacitor C9
% Vs = summed-channel output voltage
% Is = current on summed-channel output
% VPs = filtered 5V power plane for summed-channel section
% I12 = current on resistor R12
% I13 = current on resistor R13
% IC = current on resistor RC
% I15 = current on resistor R15
% I16 = current on resistor R16
% HPs = current on capacitor C24/25
% IPs = current on R29
%
% these are the external inputs
% constant = 99;
% VBias = 100;
% Vpower = 101;
% VBin = 102;
% VBout = 103;
% Iin = 104;

check_transistor('Q0',[iVQ0e iJe0; iVQ0b iJb0; iVQ0c iJc0],'PNP');
check_transistor('Q1',[iVQ1e iJe1; iVQ1b iJb1; iVQ1c iJc1],'NPN');
check_transistor('Q2',[iVQ2e iJe2; iVQ2b iJb2; iVQ2c iJc2],'PNP');
check_transistor('Q3',[iVQ3e iJe3; iVQ3b iJb3; iVQ3c iJc3],'PNP');
check_transistor('Q4',[iVQ4e iJe4; iVQ4b iJb4; iVQ4c iJc4],'PNP');
check_transistor('Q8',[iVQ8e iJe8; iVQ8b iJb8; iVQ8c iJc8],'NPN');
check_transistor('Q5',[iVQ5e iJe5; iVQ5b iJb5; iVQ5c iJc5],'NPN');
check_transistor('Q6',[iVQ6e iJe6; iVQ6b iJb6; iVQ6c iJc6],'PNP');
check_transistor('Q7',[iVQ7e iJe7; iVQ7b iJb7; iVQ7c iJc7],'PNP');

gsingle1MHz = equations('Vt','Iin',1e6);
gsingle10MHz = equations('Vt','Iin',10e6);
gsingle100MHz = equations('Vt','Iin',100e6);
fprintf('single channel gain @ 1MHz, 10MHz, 100MHz = %f, %f, %f\n',...
        gsingle1MHz,gsingle10MHz,gsingle100MHz);
gsum1MHz = equations('Vs','Iin',1e6);
gsum10MHz = equations('Vs','Iin',10e6);
gsum100MHz = equations('Vs','Iin',100e6);
fprintf('summed channel gain @ 1MHz, 10MHz, 100MHz = %f, %f, %f\n',...
        gsum1MHz,gsum10MHz,gsum100MHz);
Iperchan = equations('IPa','constant',1)+equations('IPs','constant',1)/5;
fprintf('total power per channel %f W\n',Iperchan*5.02);
