You are on page 1of 17

% v_cell[num_cells+1];

% exp_phi_n[num_cells+1];
% exp_phi_p[num_cells+1];
% diff_v[num_cells];
% E[num_cells];
% up[num_cells+layers -1];
% un[num_cells+layers -1];
% um[num_cells+layers -1];
% v_middle[num_cells];
% Jn[num_cells+2];
% Jp[num_cells+2];
% J[num_cells+2];
% p[num_cells+1];
% n[num_cells+1];
% phi_p_interface[layers - 1];
% phi_n_interface[layers - 1];

function f0=main()
%// check whether the length of the device is equal to the sum of the
individual layers
layers = 4;
len_full=120e-9;
len_prec=1e-11;
length = 0.0;
for i=1:(layers)
len = [10e-9, 40e-9, 30e-9, 40e-9];
length =length + len(i);
end

if abs(length-len_full) > len_prec


printf ('\nlen_full is not equal to sum of lengths of individual
layers');
return;
end

%// calculating the position of the organic-organic


interface
%// used in continuity equations
rel_factor_j = 1.0;
j_temp=0.0;
num_cells=(1000 + layers - 1);
sum_length = 0.0;
for i=1:(layers-1)
sum_length =sum_length + len(i);
cells_int(i) = (num_cells * sum_length / len_full) + 1;
end

%// assuming the band levels to be straight the


following can be said about the OLED

for i=1:(layers-1)
ea=[3.6, 2.4, 3.19, 2.4];
eg=[2, 3.1, 2.38, 3.1];
phi_p_interface(i) = (ea(i+1)+eg(i+1)) - (ea(i)+eg(i)); %//
interface barrier for holes at O-O interface
phi_n_interface(i) = ea(i+1) - ea(i); %//interface
barrier for electrons at O-O interface
end

%// normalized value of intrinsic carrier density


n_0=1e27;
len_full=120e-9; %length of the device in meters
epsi_r=3;
epsi_0=8.854e-12;
q=1.6e-19; %charge of electron
kb_ev=8.61738e-5; %Boltzmann constant(in ev)
kb=1.38066e-23; %Boltzmann constant(in Joule)
temp=300; %Temperature(in K)

len_norm=len_full;
v_n = (kb*temp)/q;
E_n = v_n/len_norm;
cd_n = epsi_r*epsi_0*E_n/(q*len_norm);

for i=1:(layers)
uN = [1e-4, 6.9e-6,1.51e-7,1.51e-7]; %mobility of
electrons in layers
uP = [1e-3, 2.6e-4, 9.16e-10,9.16e-10]; %mobility of
holes in layers
u_n=uP(1);
ni(i) = (n_0/cd_n)*exp(-eg(i)/(2*kb_ev*temp));
uP_n(i) = uP(i)/u_n;
uN_n(i) = uN(i)/u_n;
end

%// normalized value of deltaV

del_v(layers-1) = 0.0;
for i=(layers-2):-1:1
del_v(i) = del_v(i+1) + ((ea(i+1) - ea(i)) + ((eg(i+1) - eg(i))/2))/
(kb_ev*temp);
end

%// calculating parameters used for calculating value of current at O-O


interface

for i=1:(layers-1)
JpintA(i) = exp (-phi_p_interface(i)/(kb_ev*temp));
JpintB(i) = (1200e3*300*300/n_0)*exp (+phi_p_interface(i)/(kb_ev*temp))
/ (q*E_n);
JnintA(i) = exp (-phi_n_interface(i)/(kb_ev*temp));
JnintB(i) = (1200e3*300*300/n_0)*exp (+phi_n_interface(i)/(kb_ev*temp))
/ (q*E_n);
end

%//file handling

% FILE *fp1, *fp2, *fp3, *fp4, *fp5, *fp6, *fp7, *fp8, *fp9, *fp10, *fp11,
*fp12 ,*fp13, *fpfile(3);
name = 'problem8HTL';
savedir = 'temp';
initdir = 'temp';
mkdir('D:/Simulation');
char(path);
fprintf(path, '%s%s%s', 'D:/Simulation/', name, '/');
char temppath;
fprintf('temppath', '%s%s', path, 'temp/');
mkdir('path');
mkdir('temppath');

% fp1 = fopen('V_dir.txt','w');
% fp2 = fopen('Edir.txt','w');
% fp3 = fopen('ndir.txt','w');
% fp4 = fopen('pdir.txt','w');
% fp5 = fopen('Xdir.txt','w');
% fp6 = fopen('exp_fn_dir.txt','w');
% fp7 = fopen('exp_fp_dir.txt','w');
% fp8 = fopen('Jdir.txt','w');
% fp9 = fopen('Jndir.txt','w');
% fp10 = fopen('Jpdir.txt','w');
% fp11 = fopen('updir.txt','w');
% fp12 = fopen('undir.txt','w');
% fp13 = fopen('plotdir.txt','w');
% fpfile(1) = fopen('file2.txt','w');
% fpfile(0) = fopen('file1.txt','w');

sprintf('%s%s%s', path,savedir,'/E.txt');
sprintf('%s%s%s', path,savedir,'/n.txt');
sprintf('%s%s%s', path,savedir,'/p.txt');
sprintf('%s%s%s', path,savedir,'/X.txt');
sprintf('%s%s%s', path,initdir,'/exp_fn.txt');
sprintf('%s%s%s', path,initdir,'/exp_fp.txt');
sprintf('%s%s%s', path,initdir,'/V.txt');
sprintf('%s%s%s', path,initdir,'/X.txt');
sprintf('exp_fn_dir_in','%s%s%s', path,savedir,'/exp_fn.txt');
sprintf('exp_fp_dir_in','%s%s%s', path,savedir,'/exp_fp.txt');
sprintf('V_dir_in','%s%s%s', path,savedir,'/V.txt');
sprintf('%s%s%s', path,savedir,'/J.txt');
sprintf('%s%s%s', path,savedir,'/Jn.txt');
sprintf('%s%s%s', path,savedir,'/Jp.txt');
sprintf('%s%s%s', path,savedir,'/up.txt');
sprintf('%s%s%s', path,savedir,'/un.txt');
sprintf('%s%s%s', path,savedir,'/plot.txt');
sprintf('%s%s%s', path,savedir,'/effi.txt');
sprintf('%s%s', path,'/IV_char.txt');
sprintf('%s%s', path,'/device_par.txt');

v_app = 0;
reading = 1;
incr_step = 0.1;
v_start=2.5;
v_final=15;
max_vol = v_final;
vapp = v_start;
for r=1:(v_app<max_vol)
j_temp = 0.0;
v_app = vapp + incr_step*r;

% %// some file settings


%
% fprintf(file2,'%s%s%f%s', path,'/device_char_',v_app,'.txt');

%// file settings done

% // initial guess

initialize()

%// normalized cathoda and anode voltages

wfa=5.05; %work function of anode(in eV)


wfc=3.0; %work function of cathode(in eV)
v_c = (ea(layers-1) + (eg(layers-1)/2) - wfc)/v_n;
v_a = (ea(layers-1) + (eg(layers-1)/2) - wfa)/v_n + v_app/v_n;

%// boundary condition for voltage

v_cell(1) = v_a;
v_cell(num_cells) = v_c;

count_iterations = 0;
iteration_max = 1500;
exp_phi_p=[num_cells+1];
exp_phi_n=[num_cells+1];
for j=0:iteration_max
count_iterations=count_iterations+1;
end
tol_v=1e-6;

solve_poisson () %v_cell, exp_phi_p, exp_phi_n, tol_v)

field ()

%// intermediate variables for the calculation of injection current

S0pn = S0 * up(num_cells+layers-2); %// recombination velocity of


holes at zero field in ETL
S0pp = S0 * up(0); %// recombination velocity of
holes at zero field in HTL
S0nn = S0 * un(num_cells+layers-2); %// recombination velocity of
electrons at zero field in ETL
S0np = S0 * un(0); %// recombination velocity of
electrons at zero field in HTL
fp = rc_n * E(0); %// reduced field for ETL
fn = rc_n * E(num_cells-1); %// reduced field for ETL
if (fp>0)
psip = (1/fp) + sqrt(1/fp) - (1/fp)*sqrt(1+2*sqrt(fp));
spp = (S0pp/4) * (pow(psip,-2) - fp);

else
psip = (-1/fp) + sqrt(-1/fp) - (-1/fp)*sqrt(1+2*sqrt(-fp));
snp = (S0np/4) * (pow(psip,-2) + fp);
end

if (fn>0)
psin = (1/fn) + sqrt(1/fn) - (1/fn)*sqrt(1+2*sqrt(fn));
snn = (S0nn/4) * (pow(psin,-2) - fn);

else
psin = (-1/fn) + sqrt(-1/fn) - (-1/fn)*sqrt(1+2*sqrt(-fn));
spn = (S0pn/4) * (pow(psin,-2) + fn);
end

%// calculating the intermediate variables for O-O interface currents

for l=0:(layers-2)
inter(l) = exp(E(cells_int(l)-1) *(del_x/2));
apoo(l) = inter(l) * JpintA(l) * ni(l) * exp(del_v(l));
bpoo(l) = (1/inter(l)) * ni(l+1) * (exp(del_v(l+1)));
kpoo(l) = ( (up(cells_int(l)-1+l) * u_n * E(cells_int(l)-
1)/JpintB(l)) + (up(cells_int(l)-1+l)/up(cells_int(l)+l)) * (1 - (bpoo(l) /
ni(l+1)) * (1/exp(del_v(l+1)))) - ((1 - inter(l)) * JpintA(l)) );

anoo(l) = inter(l) * JnintA(l) * ni(l+1) * (1/exp(del_v(l+1)));


bnoo(l) = (1/inter(l)) * ni(l) * (1/exp(del_v(l)));
knoo(l) = ( (un(cells_int(l)+l) * u_n * E(cells_int(l)-1) /
JnintB(l)) + (un(cells_int(l)+l)/un(cells_int(l)-1+l)) * (1 - (bnoo(l) *
exp(del_v(l)) / ni(l))) - ((1 - inter(l)) * JnintA(l)) );
end

%// solve continuity equations

solve_continuity_n ()

solve_continuity_p ()

%// calculate current


%// 'difference' is a variable used inside calculate_current, and it
stores the difference between the
%// current in previous iteration and this iteration.

difference = 0.0;

calculate_current ()
calculate_carrier ()

if (difference > J(0) * curr_prec1)


if (j < iteration1)

end
if (j < iteration2)
rel_factor_j = 0.7;

end
if (j < iteration3)
rel_factor_j = 0.5;

end
else
return;
end

if (fabs(j_temp - J(0)*J_n) < J(0)*J_n*curr_prec2)


printf ('\n %i.\tVoltage: %e,\tIterations: %i', r+1, v_app, j);
return;
end

j_tmp = j_temp; % // this value stored here in j_tmp helps to


print the value of current in case, the number of iterations exceed
if (difference < J(0) * curr_prec1)
j_temp = J(0)*J_n;
end

%// count_iterations stores the value of no. of iterations

%// store the values to files


%//if (((reading-1)*vol_step - r*incr_step) < incr_step/2){

reading=reading+1;

if (count_iterations >= iteration3)


printf ('\n voltage = %e, many iterations , possible
nonconvergence', v_app);
% continue;
end

fp1 = fopen('V_dir.txt','w');
fp2 = fopen('Edir.txt','w');
fp3 = fopen('ndir.txt','w');
fp4 = fopen('pdir.txt','w');
fp5 = fopen('Xdir.txt','w');
fp6 = fopen('exp_fn_dir.txt','w');
fp7 = fopen('exp_fp_dir.txt','w');
fp8 = fopen('Jdir.txt','w');
fp9 = fopen('Jndir.txt','w');
fp10 = fopen('Jpdir.txt','w');
fp11 = fopen('updir.txt','w');
fp12 = fopen('undir.txt','w');
fp13 = fopen('plotdir.txt','w');
fpfile(1) = fopen('file2.txt','w');
fpfile(0) = fopen('file1.txt','w');

%// print all the other data on file

for i=1:num_cells
fprintf (fp1, '%i\t%e\n', i, v_cell(i)*v_n);
fprintf (fp3, '%i\t%e\n', i, n(i)*cd_n);
fprintf (fp4, '%i\t%e\t%e\n', i, p(i)*cd_n, (up(i)
+un(i))*p(i)*n(i));
fprintf (fp6, '%i\t%e\n', i, ((exp_phi_n(i))));
fprintf (fp7, '%i\t%e\n', i, ((exp_phi_p(i))));
fprintf (fp11, '%i\t%e\n', i, up(i));
fprintf (fp12, '%i\t%e\n', i, un(i));
fprintf (fp9, '%i\t%e\n', i, Jn(i)*J_n);
fprintf (fp10, '%i\t%e\n', i, Jp(i)*J_n);
fprintf (fp8, '%i\t%e\n', i, (J(i))*J_n);
if (i ~= num_cells)

fprintf (fp2, '%i\t%e\n', i, E(i)*E_n);


fprintf (fp13, '%i\t%e\t%e\t%e\t%e\t%e\t%e\t%e\n', i,
v_cell(i)*v_n, E(i)*E_n, Jn(i)*J_n, Jp(i)*J_n, n(i)*cd_n, p(i)*cd_n, (up(i)
+un(i))*p(i)*n(i));
end
end

fprintf (fp10, '%i\t%e\n', num_cells+1, Jp(num_cells+1)*J_n);


fprintf (fp9, '%i\t%e\n', num_cells+1, Jn(num_cells+1)*J_n);
fprintf (fp8, '%i\t%e\n', num_cells+1, (J(num_cells+1))*J_n);

%// printing in file to store all values together


%//for (int i=0; i<2; i++){
if (v_app == vapp) %// || (i == 1)){
fpfile(2) = fopen(file3,'w');
%//no. of layers

fprintf (fpfile(2), '\nLayers\t\t\t\t%i', layers);

%//length of device

fprintf (fpfile(2), '\nLength of device\t\t%e', len_full);


fprintf (fpfile(2), '\nLength of layers\t\t%e', len(0));

for j=1:(layers-1)
fprintf (fpfile(2), '\t%e', len(j));
end
%//mobilities at zero field

fprintf (fpfile(2), '\nuP\t\t\t\t%e', uP(0));


for j=1:(layers-1)
fprintf (fpfile(2), '\t%e', uP(j));
end
fprintf (fpfile(2), '\nuN\t\t\t\t%e', uN(0));
for j=1:(layers-1)
fprintf (fpfile(2), '\t%e', uN(j));
end

%//gammap, gamman(factor through which mobility depends on


%electric field)

fprintf (fpfile(2), '\ngammap\t\t\t\t%e', gammap(0));


for j=1:(layers-1)
fprintf (fpfile(2), '\t%e', gammap(j));
end
fprintf (fpfile(2), '\ngamman\t\t\t\t%e', gamman(0));
for j=1:(layers-1)
fprintf (fpfile(2), '\t%e', gamman(j));
end

%//electron affinity and band gap

fprintf (fpfile(2), '\nElectron Affinity\t\t%e', ea(0));


for j=1:(layers-1)
fprintf (fpfile(2), '\t%e', ea(j));
end
fprintf (fpfile(2), '\nBand Gap\t\t\t%e', eg(0));
for j=1:(layers-1)
fprintf (fpfile(2), '\t%e',eg(j));
end

%//epsilon_r

fprintf (fpfile(2), '\nDielectric Permittivity\t\t%e', epsi_r);

%//intrinsic carrier concentration

fprintf (fpfile(2), '\nIntrinsic carrier concentration\t%e',


n_0);

%//work function

fprintf (fpfile(2), '\nWork function of anode(in eV)\t%e',


wfa);
fprintf (fpfile(2), '\nWork function of cathode(in eV)\t%e',
wfc);
end

% // print I-V characteristics onto file


% //if (vapp == v_app){
% // fprintf (fpfile(0), '\n\nVoltage\tCurrent');
% //}
if (vapp == v_app)
fprintf (fpfile(0), '%e\t%e', v_app, J(50)*J_n); % //instead of
J(50) we could have used any value, as J is constant throughout the device

else
fprintf (fpfile(0), '\n%e\t%e', v_app, J(50)*J_n);
end

% //applied voltage
% // fprintf (fpfile(1), '\n\n\nApplied voltage\t\t\t%e', v_app);
% // printing the rest of the values

org_int = 0;

%//fprintf (fpfile(1), '\n\nPosition1 \tJn \tJp \tJ


\tPosition2 \te mobility \thole mobility\tPosition3 \tElectric
field\tPosition4 \tV \te density \thole
density\tRecombination');

for i=0:(num_cells+layers-1)
if (i==0)
fprintf (fpfile(1), '%e\t%e\t%e\t%e\t%s\t%s\t%s\t%s\t%s\t%e\t
%e\t%e\t%e\t%e', i*delx, Jn(i)*J_n, Jp(i)*J_n, J(i)*J_n, ' ',' ',' ', ' ','
', i*delx, v_cell(i)*v_n, n(i)*cd_n, p(i)*cd_n, (q/(epsi_r*epsi_0))*(un(i) +
up(i))*u_n*n(i)*cd_n*p(i)*cd_n);
continue;
end
if (i<num_cells+1)
fprintf (fpfile(1), '\n%e\t%e\t%e\t%e\t%e\t%e\t%e\t%e\t%e\t
%e\t%e\t%e\t%e\t%e', delx/2+(i-1)*delx, Jn(i)*J_n, Jp(i)*J_n, J(i)*J_n,
delx/2+(i-1)*delx, un(i-1+org_int)*u_n, up(i-1+org_int)*u_n, delx/2+(i-
1)*delx, E(i-1)*E_n, i*delx, v_cell(i)*v_n, n(i)*cd_n, p(i)*cd_n, (q/
(epsi_r*epsi_0))*(un(i+org_int) + up(i+org_int))*u_n*n(i)*cd_n*p(i)*cd_n);
end
if ((org_int < layers-1) && (i == cells_int(org_int)))
org_int=org_int+1;
fprintf (fpfile(1), '\n%s\t%s\t%s\t%s\t%e\t%e\t%e\t%s\t%s\t
%s\t%s\t%s\t%s\t%s', ' ',' ',' ',' ', delx/2+(i-1)*delx, un(i-1+org_int)*u_n,
up(i-1+org_int)*u_n, ' ',' ', ' ',' ',' ',' ',' ');
end

if (i==num_cells+1)
fprintf (fpfile(1), '\n%e\t%e\t%e\t%e\t%s\t%s\t%s\t%s\t%s\t
%s\t%s\t%s\t%s\t%s', (i-1)*delx, Jn(i)*J_n, Jp(i)*J_n, J(i)*J_n, ' ',' ',' ',
' ',' ', ' ',' ',' ',' ',' ');
end
end

fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
fclose(fp5);
fclose(fp6);
fclose(fp7);
fclose(fp8);
fclose(fp9);
fclose(fp10);
fclose(fp11);
fclose(fp12);
fclose(fp13);
fclose(fpfile(1));

fclose(fpfile(0));
printf('\nPress any key to exit...');

return ;

end

function f1 = initialize()
layers=4;
num_cells=(1000+layers-1);
v_start=2.5;
vapp=v_start;
if v_app == vapp
for i=1:(num_cells+1)
exp_phi_n(i) = 1.0;
exp_phi_p(i) = 1.0;
v_cell(i) = 0.0;
end
end

% else
% fp1 = fopen ('exp_fn_dir_in', 'r');
% fp2 = fopen ('exp_fp_dir_in', 'r');
% fp3 = fopen ('V_dir_in', 'r');
% end
% for i=1:(num_cells+1)
% fscanf (fp1,'%e %e', c, d);
% exp_phi_n(i) = d;
% fscanf(fp2,'%e %e', c, d);
% exp_phi_p(i) = d;
% fscanf(fp3,'%e %e', c, d);
% v_cell(i) = (d/v_n);
% end
% fclose(fp1);
% fclose(fp2);
% fclose(fp3);

end

function f2=solve_poisson( v_cell, exp_phi_p, exp_phi_n, tol_v )


x1 = 1;
tol_v = 1e-6;
error = 100;
delx=(len_full/num_cells) ;
del_x = delx/len_full;
exp_phi_n(i) = 1.0;
exp_phi_p(i) = 1.0;
v_cell(i) = 0.0;

while (error > tol_v)


x1 = 1;
for i=1:num_cells
if (i==num_cells-1)

a(i-2) = 1;
b(i-1) = -v_cell(i+1) - v_cell(i-1) + 2*v_cell(i) -
(del_x*del_x)*ni(layers -1)*((exp_phi_p(i)/exp(v_cell(i))) -
(exp_phi_n(i)*exp(v_cell(i))));
d(i-1) = -(2.0 + (del_x*del_x)*ni(layers-
1)*((exp_phi_p(i)/exp(v_cell(i))) + (exp_phi_n(i)*exp(v_cell(i)))));
end
if (i==1)
c(i) = 1;
d(i) = -(2.0+
(del_x*del_x)*ni(1)*(((exp_phi_p(i)*exp(del_v(x1+1)))/exp(v_cell(i))) +
(exp_phi_n(i)*exp(v_cell(i))/exp(del_v(x1+1)))));
b(i) = -v_cell(i+1) - v_cell(i) + 2*v_cell(i) -
(del_x*del_x)*ni(1)*((exp_phi_p(i)*exp(del_v(x1+1))/exp(v_cell(i))) -
(exp_phi_n(i)*exp(v_cell(i))/exp(del_v(x1+1))));
end
for q1=1:(layers-1)
if i==cells_int(q1)
x1=q1+1;
end
end
if ((i~=1) & (i~=num_cells))
a(i-1) = 1;
c(i-1) = 1;
b(i-1) = -v_cell(i+1) - v_cell(i-1) + 2*v_cell(i-1) -
(del_x*del_x)*ni(x1)*((exp_phi_p(i-1)*exp(del_v(x1))/exp(v_cell(i-1))) -
(exp_phi_n(i-1)*exp(v_cell(i-1))/exp(del_v(x1))));
d(i-1) = -(2.0 + (del_x*del_x)*ni(x1)*(((exp_phi_p(i-
1)*exp(del_v(x1)))/exp(v_cell(i-1))) + (exp_phi_n(i-1)*exp(v_cell(i-
1))/exp(del_v(x1)))));
end
end

for i=1:(num_cells-1)
d(i) = d(i) - (a(i-1)/d(i-1))*c(i-1);
b(i) = b(i) - (a(i-1)/d(i-1))*b(i-1);
end

diff_v(num_cells-1) = (b(num_cells-2)/d(num_cells-2));
v_cell(num_cells-1) = v_cell(num_cells-1) +
rel_factor_v*diff_v(num_cells-1);
error = fabs(diff_v(num_cells-1));

for i=(num_cells-2):-1:0
diff_v(i) = (b(i-1) - c(i-1)*diff_v(i+1))/d(i-1);
v_cell(i) = v_cell(i) + rel_factor_v*diff_v(i);
if (fabs(diff_v(i)) > error)
error = fabs(diff_v(i));
end
end
end
end

function f3= field( v_cell, E, up, un )


g=0;
h=0;

for i=0:num_cells
E(i) = -(v_cell(i+1)-v_cell(i))/(del_x);
end

for i=0:(num_cells + layers - 1)


up(i) = uP_n(g)*exp(gammap(g)*pow(E_n,.5) * sqrt(fabs(E(i-g))));
un(i) = uN_n(g)*exp(gamman(g)*pow(E_n,.5) * sqrt(fabs(E(i-g))));

um(i) = un(i) +up(i);


for q5=0:(layers-1)
if (i==cells_int(q5)-1+q5)
g =q5+1;
end
end
end
end

function f4=solve_continuity_p( exp_phi_n, exp_phi_p, v_cell, E, up, um


)
x=0;
r1=0;

for i=0:num_cells
v_middle(i) = (v_cell(i)+v_cell(i+1))/2;
end

% temp1;
%temp2;

for i=0:num_cells
r1 =0;
if (i==0)
cp(0) = up(0)*ni(0)*exp(del_v(0))*(1/exp(v_middle(0)));
if (E(0)>0)
dp(0) = - ni(0)*exp(del_v(0))*spp*exp(-v_cell(0))*del_x - cp(0) -
del_x*gamma_rec*um(0)*ni(0)*ni(0)*exp_phi_n(0)/2;
bp(0) = - del_x*Jp0_cons*up(0)*exp(sqrt(fp));
end
else
dp(0) = - ni(0)*exp(del_v(0))*S0pp*exp(-v_cell(0))*del_x - cp(0)
- del_x*gamma_rec*um(0)*ni(0)*ni(0)*exp_phi_n(0)/2;
bp(0) = - del_x*Jp0_cons*up(0);
end
end

for y1=0:(layers-1)
if (i==(cells_int(y1)-1))
ap(i-1) = up(i+(y1-1))*ni(y1)*exp(del_v(y1))*(1/exp(v_middle(i-
1)));
bp(i) = 0;
cp(i) = del_x*(up(i+y1)*E(i)/kpoo(y1))*(bpoo(y1))*exp(-
v_cell(i+1));
dp(i) = - (up(i+y1)*E(i)/kpoo(y1))*apoo(y1)*exp(-v_cell(i))*del_x
- ap(i-1) - del_x*gamma*um(i+(y1-1))*ni(y1)*ni(y1)*exp_phi_n(i);

x=x+1;
r1=1;
end
if (i == cells_int(y1))
x=x-1;
ap(i-1) = (up(i+(y1-1))*E(i-1)/kpoo(y1))*apoo(y1)*exp(-v_cell(i-
1))*del_x;
bp(i) = 0;
cp(i) = up(i+y1+1)*ni(y1+1)*exp(del_v(y1+1))*(1/exp(v_middle(i)));
dp(i) = - del_x*(up(i+(y1-1))*E(i-1)/kpoo(y1))*bpoo(y1)*exp(-
v_cell(i)) - cp(i) - del_x*gamma*um(i+y1+1)*ni(y1+1)*ni(y1+1)*exp_phi_n(i);
r1 =1;
x=x+1;
end
end

if (i==num_cells)
ap(i-1) = up(num_cells+layers -2)*ni(layers -1)*(1/exp(v_middle(i-
1)));
if (E(i-1)<0)
bp(i) = - del_x*JpN_cons*up(num_cells+layers -2)*exp(sqrt(-fn));
dp(i) = - del_x*spn*ni(layers -1)*exp(-v_cell(i)) - ap(i-1) -
del_x*gamma_rec*um(num_cells+layers -2)*ni(layers -1)*ni(layers
-1)*exp_phi_n(i)/2;
end
else
bp(i) = - del_x*JpN_cons*up(num_cells+layers -2);
dp(i) = - del_x*S0pn*ni(layers -1)*exp(-v_cell(i)) - ap(i-1) -
del_x*gamma_rec*um(num_cells+layers -2)*ni(layers -1)*ni(layers
-1)*exp_phi_n(i)/2;
end

if ((i~=0) & (i~=num_cells) & (r1==0))


ap(i-1) = up(i+(x-1))*ni(x)*(1/exp(v_middle(i-1)))*exp(del_v(x));
bp(i) = 0;
cp(i) = up(i+x)*ni(x)*(1/exp(v_middle(i)))*exp(del_v(x));
dp(i) = - ap(i-1) - cp(i) -
del_x*gamma_rec*ni(x)*ni(x)*um(i+x)*exp_phi_n(i);
end
for i=1:num_cells
dp(i) = dp(i) - (ap(i-1)/dp(i-1))*cp(i-1);
bp(i) = bp(i) - (ap(i-1)/dp(i-1))*bp(i-1);
end

temp1 = bp(num_cells)/dp(num_cells);
temp2 = exp_phi_p(num_cells) + rel_factor_j*(temp1 -
exp_phi_p(num_cells));

exp_phi_p(num_cells) = temp2;

for i=(num_cells-1):-1:0
temp1 = (bp(i) - cp(i)*exp_phi_p(i+1))/dp(i);
temp2 = exp_phi_p(i) + rel_factor_j*(temp1 - exp_phi_p(i));
exp_phi_p(i) = temp2;
end
end

function f5=solve_continuity_n( exp_phi_n, exp_phi_p, v_cell, E, un, um


)
x1=0;
r2=0;
% temp1;
% temp2;

for i=0:num_cells
v_middle(i) = (v_cell(i)+v_cell(i+1))/2;
end

for i=0:num_cells
r2 =0;
if (i==0)
cn(0) = un(0)*ni(0)*(1/exp(del_v(0)))*exp(v_middle(0));
if (E(0)>0)
dn(0) = - ni(0)*(1/exp(del_v(0))*S0np*exp(v_cell(0))*del_x) -
cn(0) - del_x*gamma_rec*um(0)*ni(0)*ni(0)*exp_phi_p(0)/2;
bn(0) = -del_x*Jn0_cons*un(0);
end
else
dn(0) = - ni(0)*(1/exp(del_v(0)))*snp*exp(v_cell(0))*del_x -
cn(0) - del_x*gamma_rec*um(0)*ni(0)*ni(0)*exp_phi_p(0)/2;
bn(0) = -del_x*Jn0_cons*un(0)*exp(sqrt(-fp));
end
end

for y2=0:(layers-1)
if (i==cells_int(y2)-1)
an(i-1) = un(i+(x1-1))*ni(y2)*(1/exp(del_v(y2)))*exp(v_middle(i-
1));
bn(i) = 0;
cn(i) =
del_x*(un(i+x1+1)*E(i)/knoo(y2))*anoo(y2)*exp(v_cell(i+1));
dn(i) = -
(un(i+x1+1)*E(i)/knoo(y2))*bnoo(y2)*exp(v_cell(i))*del_x - an(i-1) -
del_x*gamma*um(i+(x1-1))*ni(y2)*ni(y2)*exp_phi_p(i);

r2 =1;
x1=x1+1;
end
if (i==cells_int(y2))
x1=x1-1;
an(i-1) = un(i+x1)*(E(i-1)/knoo(y2))*bnoo(y2)*exp(v_cell(i-
1))*del_x;
bn(i) = 0;
cn(i) = un(i+x1+1)*ni(y2+1)*exp(v_middle(i))*(1/exp(del_v(y2+1)));
dn(i) = - del_x*(un(i+x1)*E(i-
1)/knoo(y2))*anoo(y2)*exp(v_cell(i)) - cn(i) -
del_x*gamma*um(i+x1+1)*ni(y2+1)*ni(y2+1)*exp_phi_p(i);

r2=1;
x1=x1+1;
end
end

if (i==num_cells)
an(i-1) = un(num_cells+layers-2)*ni(layers -1)*exp(v_middle(i-1));
if (E(i-1)<0)
bn(i) = - del_x*JnN_cons*un(num_cells+layers -2);
dn(i) = - del_x*S0nn*ni(layers -1)*exp(v_cell(i)) - an(i-1) -
del_x*gamma_rec*um(num_cells+layers -2)*ni(layers -1)*ni(layers
-1)*exp_phi_p(i)/2;
end
else
bn(i) = - del_x*JnN_cons*un(num_cells+layers -2)*exp(sqrt(fn));
dn(i) = - del_x*snn*ni(layers -1)*exp(v_cell(i)) - an(i-1) -
del_x*gamma_rec*um(num_cells+layers -2)*ni(layers -1)*ni(layers
-1)*exp_phi_p(i)/2;
end

if ((i~=0) & (i~=num_cells) & (r2==0))


an(i-1) = un(i+(x1-1))*ni(x1)*exp(v_middle(i-1))*(1/exp(del_v(x1)));
bn(i) = 0;
cn(i) = un(i+x1)*ni(x1)*exp(v_middle(i))*(1/exp(del_v(x1)));
dn(i) = - an(i-1) - cn(i) -
del_x*gamma_rec*ni(x1)*ni(x1)*um(i+x1)*exp_phi_p(i);
end

ii = 0;
for i=1:num_cells
ii = num_cells - i;
dn(ii) = dn(ii) - (cn(ii)/dn(ii+1))*an(ii);
bn(ii) = bn(ii) - (cn(ii)/dn(ii+1))*bn(ii+1);
end

temp1 = bn(0)/dn(0);
temp2 = exp_phi_n(0) + rel_factor_j*(temp1 - exp_phi_n(0));
exp_phi_n(0) = temp2;

for i=(num_cells-1):-1:0
ii = num_cells - i;
temp1 = (bn(ii) - an(ii-1)*exp_phi_n(ii-1))/dn(ii);
temp2 = exp_phi_n(ii) + rel_factor_j*(temp1 - exp_phi_n(ii));

exp_phi_n(ii) = temp2;
end
end

function f6=calculate_current( exp_phi_n, exp_phi_p, v_cell, E, un, up )


% int r3;
% temp7;
g=0;
for i=0:(num_cells+1)
r3 =0;
if (i==0)
if (E(0)>0)
Jp(i) = Jp0_cons*up(0)*exp(sqrt(fp)) - spp*ni(0)*exp(-
v_cell(0))*exp(del_v(0))*exp_phi_p(0);
Jn(i) = S0np*ni(0)*(1/exp(del_v(0)))*exp(v_cell(0))*exp_phi_n(0)
- Jn0_cons*un(0);
end
else
Jp(i) = Jp0_cons*up(0) - S0pp*ni(0)*exp(-
v_cell(0))*exp(del_v(0))*exp_phi_p(0);
Jn(i) = snp*ni(0)*(1/exp(del_v(0)))*exp(v_cell(0))*exp_phi_n(0) -
Jn0_cons*un(0)*exp(sqrt(-fp));%%CHANGED Snp FOR S0np
end
end
if (i==num_cells+1)
if (E(num_cells-1)<0)
Jp(i) = spn*ni(layers -1)*exp(-v_cell(i-1))*exp_phi_p(i-1) -
JpN_cons*up(num_cells+layers -2)*exp(sqrt(-fn));
Jn(i) = JnN_cons*un(num_cells+layers -2) - S0nn*ni(layers
-1)*exp(v_cell(i-1))*exp_phi_n(i-1);
end
else
Jp(i) = S0pn*ni(layers -1)*exp(-v_cell(i-1))*exp_phi_p(i-1) -
JpN_cons*up(num_cells+layers -2);
Jn(i) = JnN_cons*un(num_cells+layers -2)*exp(sqrt(fn)) -
snn*ni(layers -1)*exp(v_cell(i-1))*exp_phi_n(i-1);
end

for h=0:(layers-1)
if (i==cells_int(h))
Jn(i) = (un(i+g)*E(i-
1)/knoo(h))*(anoo(h)*exp(v_cell(i))*exp_phi_n(i) - bnoo(h)*exp(v_cell(i-
1))*exp_phi_n(i-1));
Jp(i) = (up(i+g-1)*E(i-1)/kpoo(h))*(apoo(h)*exp(-v_cell(i-
1))*exp_phi_p(i-1)- bpoo(h)*exp(-v_cell(i))*exp_phi_p(i));
g=g+1;
r3=1;
end
end
if ((i~=0) & (i~=num_cells+1) & (r3==0))
Jn(i) = un(i+(g-1))*ni(g)*exp(v_middle(i-
1))*(1/exp(del_v(g)))*(exp_phi_n(i)-exp_phi_n(i-1))/del_x;
Jp(i) = -up(i+(g-1))*ni(g)*(1/exp(v_middle(i-
1)))*exp(del_v(g))*(exp_phi_p(i)-exp_phi_p(i-1))/del_x;
end

J(i) = Jn(i) + Jp(i);

if (i~=0)
difference =difference + fabs(J(i)-temp7);
end

temp7 = J(i);
end

function f7=calculate_carrier( exp_phi_n, exp_phi_p, v_cell )


b =0;

for i=0:num_cells
for p1=0:(layers-1)
if (i==cells_int(p1))
b=b+1;
end
end

n(i) = ni(b)*exp(v_cell(i))*(1/exp(del_v(b)))*exp_phi_n(i);
p(i) = ni(b)*exp(-v_cell(i))*exp(del_v(b))*exp_phi_p(i);
end
end
end