function dipr_usd;
%2D energy-band and space illustrations and 
%animations provide an insight into the principles
%of P-N junction/diode operation.
%(Section 3.1 in Understanding Semiconductor Devices)

%   Sima Dimitrijev, September 27, 1999.
%   Copyright (c) 2000, Oxford University Press.

global con_vg_usd17; global con_vds_usd17; global con_r_usd17;
global con_start_usd17; global con_close_usd17; 
global con_br_usd17; global con_sl_usd17;

%SET THE DOPING COLOR MAP
a=([1:64].')*0; dopcolmap=[a a a];
dopcolmap(1,1)=1;
dopcolmap(1,2)=1;
dopcolmap(1,3)=1;
for ii=2:10; %the grey part
   dopcolmap(ii,1)=0.78*(10-ii)/8;
   dopcolmap(ii,2)=dopcolmap(ii,1);
   dopcolmap(ii,3)=dopcolmap(ii,1);
end   
for ii=11:27; %the dark green part
   dopcolmap(ii,2)=0.7*(ii-11)/16+0.3;
end   
for ii=28:35; %the light green part  
   dopcolmap(ii,2)=1;
   dopcolmap(ii,1)=(ii-27)/8;
   dopcolmap(ii,3)=(ii-27)/8;
end
for ii=36:39; %The white part in the middle
dopcolmap(ii,1)=1; dopcolmap(ii,2)=1; dopcolmap(ii,3)=1;   
end
for ii=40:47; %the light red part
   dopcolmap(ii,2)=1-(ii-40)/8;
   dopcolmap(ii,3)=1-(ii-40)/8;
   dopcolmap(ii,1)=1;
end   
for ii=48:64; %the dark red part
   dopcolmap(ii,1)=0.7*(64-ii)/16+0.3;
end   

%OPEN PARENT FIGURE
Hf=figure('NumberTitle','off','Name','Section 3.1: Diode Principles',...
   'Color',[0.8 0.8 0.8]);
set(gcf,'InvertHardCopy','off');

set(gca,'Visible','off','Xlim',[0 15],'Ylim',[0 7.5],'Position',[0.01 0.15 .75 .825]);
set(Hf,'Colormap',dopcolmap);

%READ THE INTRO TEXT              
introtext ={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
      'Section 3.1: Diode Principles' ;...
      ' ';...
      '3D and 2D energy-band and space diagrams and animations visualize ';...
      'P-N junction/diode operation. You can set the diode voltage and ';...
      'P-type/N-type doping at different values to see the energy and ';...
      'current effects.';...
      '  ';...
      'The RED surface shows the Ec in P-type areas, the GREEN surface ';...
      'shows the Ec in N-type areas, whereas the WHITE surface indicates the ';...
      'depletion layer (lighter the color, lower the carrier concentration). ';...
      '          ';...
      'It is very useful to relate the 3D view to the commonly used "x-y" and';...
      '"E-x" diagrams. There are demo rotations to enable you to make direct ';...
      'links between these views. '}; 

Ht_intro=text(0.15,0.55,char(introtext),'Units','normalized');
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.1 0.1 0.7 0.1],...
   'Style','pushbutton','String','CLICK HERE TO CONTINUE!','CallBack','con_start_usd17=1;');
con_start_usd17=0;
while con_start_usd17<1; 
   pause(0.2);  
   if ishandle(Hf)==0;
      return; 
   end;
end
con_start_usd17=0;
set(Ht_intro,'Visible','off');
set(Hp_cont,'Visible','off','Enable','off');
drawnow;

con_vg_usd17=0; con_vds_usd17=0; con_r_usd17=0; con_start_usd17=0; 
con_close_usd17=0; con_br_usd17=0; con_sl_usd17=0;

%SETTING THE CONTROL PANEL              
Hframe=uicontrol(Hf,'Units','normalized','Position',[0.77 0.01 0.22 0.98],...
   'Style','frame');
HframeVd=uicontrol(Hf,'Units','normalized','Position',[0.775 0.845 0.21 0.14],...
   'Style','frame','BackgroundColor','y');

%Vd
Ht_vdu=uicontrol(Hf,'Units','normalized','Position',[0.78 0.935 0.20 0.04],...
   'Style','text','String','Vd','BackgroundColor','y','FontWeight','bold');
Hsl_vd=uicontrol(Hf,'Units','normalized','Position',[0.78 0.885 0.20 0.05],...
   'Style','slider','min',-3,'max',0.8,'Value',0,'CallBack','con_vds_usd17=1;');
Ht_vdd=uicontrol(Hf,'Units','normalized','Position',[0.78 0.85 0.20 0.03],...
   'Style','text','String',[num2str(round(get(Hsl_vd,'Value')*10)/10) ' V'],...
   'BackgroundColor','y','ForegroundColor','k');
Vds=round(get(Hsl_vd,'Value')*10)/10;

%P-type doping
Ht_dpu=uicontrol(Hf,'Units','normalized','Position',[0.775 0.80 0.21 0.04],...
   'Style','text','String','P-TYPE DOPING','BackgroundColor',[0.6 0 0],...
   'ForegroundColor','w');
Hsl_dp=uicontrol(Hf,'Units','normalized','Position',[0.775 0.76 0.21 0.04],...
   'Style','slider','min',log10(2e15),'max',19,'Value',16,'CallBack','con_sl_usd17=1;');
Ht_dpd=uicontrol(Hf,'Units','normalized','Position',[0.775 0.72 0.21 0.04],...
   'Style','text','String',[num2str(round(10^(get(Hsl_dp,'Value')))) ' cm-3'],...
   'BackgroundColor',[0.6 0 0],'ForegroundColor','w');
Na=round(10^(get(Hsl_dp,'Value')));
%N-type doping
Ht_dnu=uicontrol(Hf,'Units','normalized','Position',[0.775 0.68 0.21 0.04],...
   'Style','text','String','N-TYPE DOPING','BackgroundColor',[0 0.5 0],...
   'ForegroundColor','w');
Hsl_dn=uicontrol(Hf,'Units','normalized','Position',[0.775 0.64 0.21 0.04],...
   'Style','slider','min',log10(2e15),'max',19,'Value',17,'CallBack','con_sl_usd17=2;');
Ht_dnd=uicontrol(Hf,'Units','normalized','Position',[0.775 0.60 0.21 0.04],...
   'Style','text','String',[num2str(round(10^(get(Hsl_dn,'Value')))) ' cm-3'],...
   'BackgroundColor',[0 .5 0],'ForegroundColor','w');
Nd=round(10^(get(Hsl_dn,'Value')));

%3D
%The radiobutton
Hr(1)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.54 0.20 0.03],...
   'Style','radio','String','3D (reset)','CallBack','con_r_usd17=1;','Value',1,...
   'ForegroundColor','b','FontWeight','bold');
%Azimut
Hsl_az=uicontrol(Hf,'Units','normalized','Position',[0.78 0.505 0.20 0.03],...
   'Style','slider','min',-45,'max',0,'Value',-20,'CallBack','con_r_usd17=4;');
Ht_azd=uicontrol(Hf,'Units','normalized','Position',[0.78 0.475 0.20 0.03],...
   'Style','text','String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
Az=round(get(Hsl_az,'Value'));
%Elevation
Hsl_el=uicontrol(Hf,'Units','normalized','Position',[0.78 0.43 0.20 0.03],...
   'Style','slider','min',1,'max',90,'Value',20,'CallBack','con_r_usd17=5;');
Ht_eld=uicontrol(Hf,'Units','normalized','Position',[0.78 0.40 0.20 0.03],...
   'Style','text','String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
El=round(get(Hsl_el,'Value'));
%2D
Hr(2)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.355 0.20 0.03],...
   'Style','radio','String','2D x - y diagram','CallBack','con_r_usd17=2;','Value',0,...
   'ForegroundColor','b','FontWeight','bold');
Hr(3)=uicontrol(Hf,'Units','normalized','Position',[0.78 0.31 0.20 0.03],...
   'Style','radio','String','2D E - x diagram','CallBack','con_r_usd17=3;','Value',0,...
   'ForegroundColor','b','FontWeight','bold');

%Demo rotations
Ht_start=uicontrol(Hf,'Units','normalized','Position',[0.78 0.255 0.20 0.03],...
   'Style','text','String','DEMO  ROTATIONS');
Hb_start1=uicontrol(Hf,'Units','normalized','Position',[0.78 0.20 0.20 0.05],...
   'Style','pushbutton','String','3D->x-y','CallBack','con_start_usd17=1;');
Hb_start2=uicontrol(Hf,'Units','normalized','Position',[0.78 0.14 0.20 0.05],...
   'Style','pushbutton','String','3D->E-x','CallBack','con_start_usd17=2;');

%CLOSE
Hb_close=uicontrol(Hf,'Units','normalized','Position',[0.78 0.02 0.20 0.10],...
   'Style','pushbutton','String','CLOSE','CallBack','con_close_usd17=1;');
%Brightness
Ht_bru=uicontrol(Hf,'Units','normalized','Position',[0.58 0.495 0.18 0.03],...
   'Style','text','String','brightness','BackgroundColor',[0.8 0.8 0.8]);
Hsl_br=uicontrol(Hf,'Units','normalized','Position',[0.58 0.46 0.18 0.03],...
   'Style','slider','min',13,'max',15,'Value',14.5,'CallBack','con_br_usd17=1;');
bright=get(Hsl_br,'Value');

%CONSTANTS
Vt=0.02586;
ni=1.02e10;
%epsox=3.9*8.85e-12;
epssi=11.8*8.85e-12;
q=1.6e-19;
Eg=1.12;
Isn=4e7;
Isp=4e7;
rs=0.005;
z2_max=log10(2e21); z2_min=log10(1e14);
shf=0.07;
kVt=8;
NN=200;
Ln=0.4;
Lp=0.4;

%THE GRID
Xmax=1; Xmin=-1;
Ymin=0; Ymax=1; 
%num_x=100*(Xmax-Xmin);
%num_y=3;
dx=(Xmax-Xmin)/200;
[xs,ys]=meshgrid(Xmin:dx:Xmax,Ymax:-Ymax/2:0);
[num_y num_x]=size(xs);

%INITIAL VALUES
%Diagram
phifn=-Vt*log(Nd/ni);
phifp=Vt*log(Na/ni);
Vbi=phifp-phifn;
Vd=0;
%if Vd>Vbi;...
wn=sqrt(2*epssi*(Vbi-Vd)/(q*Nd*1e6*(1+Nd/Na)))*1e6;
wp=sqrt(2*epssi*(Vbi-Vd)/(q*Na*1e6*(1+Na/Nd)))*1e6;
phin=Vbi-Vd-(0.5*q*Nd*1e6/epssi)*1e-12*(xs+wn).^2;
phip=(0.5*q*Na*1e6/epssi)*1e-12*(xs-wp).^2;
Ec=-(Vbi-Vd)*(xs<=-wn)-phin.*(xs>-wn).*(xs<0)-...
   phip.*(xs>=0).*(xs<wp)+phifp+Eg/2;
Ev=Ec-Eg;
Efn=Vd;
%Calculate the concentration
Ncarr=-ni*exp((Eg/2-Ec+Efn)/Vt).*(xs<0)+ni*exp((Eg/2+Ev)/Vt).*(xs>=0);
Ncarr=(abs(Ncarr)<2e10).*1e10+(abs(Ncarr)>=2e10).*Ncarr;

%I-V characteristics
Ifmax=15;
Vrmin=-4;
IS=(Isn/Nd+Isp/Na);
If=[0:Ifmax/100:Ifmax];
Vf=Vt*log(If/IS+1);
Vr=[Vrmin:abs(Vrmin/100):0];
Ir=IS*(exp(Vr/Vt)-1);

%SET THE GRAPHS

%Ir-Vr
Irmin=1.1*min(Ir);
Ndig=10^floor(log10(abs(Irmin)));
Irmin=Ndig*1*floor((Irmin/Ndig)/1);
Ha_irvr=axes('Xlim',[Vrmin -1e-3],'Ylim',[Irmin -1e-20],...
   'Position',[0.30 0.02 0.25 0.12],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic',...
   'XAxisLocation','top','YAxisLocation','right');
hold on;
%xlabel('V_{D} (V)'); %ylabel('I_{D} (mA)');
Hp_irvr=plot(Vr,Ir,'-k','LineWidth',2);
set(Hp_irvr,'Color',[0.55 0.55 0.55]);
Hp_irqvrq=plot(0,0,'.y','MarkerSize',15);
hold off;

%If-Vf
Vfmax=0.8;
Ha_ifvf=axes('Xlim',[1e-3 Vfmax],'Ylim',[1e-55 Ifmax],'Position',[0.55 0.14 0.20 0.27],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic');
hold on;
xlabel('      V_{D} (V)'); ylabel('I_{D} (mA)');
Hp_ifvf=plot(Vf,If,'-k','LineWidth',2);
set(Hp_ifvf,'Color',[0.55 0.55 0.55]);
Hp_ifqvfq=plot(0,0,'.y','MarkerSize',15);
hold off;

%2D GRAPH
z2=sign(Ncarr).*log10(abs(Ncarr));
   if ishandle(Hf)==0;
      return; 
   end;
Ha2=axes('Position',[0.05 0.15 0.65 0.8]);
Hsv=surface(xs,ys,(Ec-Eg),'EdgeColor','none','Visible','on');
hold on;
Hsc=surface(xs,ys,Ec,'EdgeColor','none','Visible','on');
set(Ha2,'Xlim',[Xmin Xmax],'Ylim',[0 Ymax],'Zlim',[-4.1 1.5],...
   'Color',[0.8 0.8 0.8],'FontAngle','italic','CLim',[1 64],...
   'View',[Az El],'Visible','off');
%ylabel('Lateral Distance (nm)'); xlabel('Depth (nm)');
set(Hsv,'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
Hp_efn=plot3([Xmin -wn],[0 0],[Efn Efn],'-.y');
Hp_efp=plot3([wp Xmax],[0 0],[0 0],'-.y');
Ht_lab(1)=text('String','E_{C}','Position',[Xmax 0 (Eg/2+phifp)],'Color','r',...
   'HorizontalAlignment','left','VerticalAlignment','bottom');
Ht_lab(2)=text('String','E_{F}','Position',[Xmax 0 0],'Color','y',...
   'HorizontalAlignment','left','VerticalAlignment','middle');
Ht_lab(3)=text('String','E_{V}','Position',[Xmax 0 -(Eg/2-phifp)],'Color','k',...
   'HorizontalAlignment','left','VerticalAlignment','top');

%The majority electrons
Ecn=Efn+0.5*Eg+phifn;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xeln=0; yeln=0; zeln=Ecn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1))+Efn;
   if zt>Ecn;
      zeln(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Neln=1;
   zeln(1)=Ecn+Vt*rand(1,1);
end   
[aa,Neln]=size(zeln);
xeln=rand(1,Neln)*(-wn-Xmin)+Xmin;
yeln=rand(1,Neln)*Ymax;
else; %The depletion layer is through
   Neln=1;
   xeln(1)=Xmin-1;
   yeln(1)=Ymax+1;
   zeln(1)=Ecn;
end   
Hp_eln=plot3(xeln,yeln,(zeln+shf),'.k');
set(Hp_eln,'MarkerSize',10,'Color',[0 0.6 0]);

%The minority electrons
Ecp=0.5*Eg+phifp;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xelp=0; yelp=0; zelp=Ecp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1));
   if zt>Ecp;
      zelp(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nelp=1;
   zelp(1)=Ecp+Vt*rand(1,1);
end   
[aa,Nelp]=size(zelp);
xelp=rand(1,Nelp)*(Xmax-wp)+wp;
yelp=rand(1,Nelp)*Ymax;
else; %the depletion layer is through
   Nelp=1;
   xelp(1)=Xmax+1;
   yelp(1)=Ymax+1;
   zelp=Ecp;
end;   
Hp_elp=plot3(xelp,yelp,(zelp+shf),'.k');
set(Hp_elp,'MarkerSize',10,'Color',[0 0.6 0]);

%The majority holes
Evp=Ecp-Eg;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xhop=0; yhop=0; zhop=Evp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1));
   if zt<Evp;
      zhop(ii)=zt;
   else
      ii=ii-1;
   end;
end
if ii<1; 
   Nhop=1;
   zhop(1)=Evp-Vt*rand(1,1);
end 
[aa,Nhop]=size(zhop);
xhop=rand(1,Nhop)*(Xmax-wp)+wp;
yhop=rand(1,Nhop)*Ymax;
else; %the depl. layer is through   
   Nhop=1;
   xhop(1)=Xmax+1;
   yhop(1)=Ymax+1;
   zhop=Evp;
end
Hp_hop=plot3(xhop,yhop,(zhop-shf),'.w');
set(Hp_hop,'MarkerSize',10,'Color',[1 0.95 0.95]);

%The minority holes
Evn=Ecn-Eg;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xhon=0; yhon=0; zhon=Evn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1))+Efn;
   if zt<Evn;
      zhon(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nhon=1;
   zhon(1)=Evn-Vt*rand(1,1);
end   
[aa,Nhon]=size(zhon);
xhon=rand(1,Nhon)*(-wn-Xmin)+Xmin;
yhon=rand(1,Nhon)*Ymax;
else; %The depletion layer is through
   Nhon=1;
   xhon(1)=Xmin-1;
   yhon(1)=Ymax+1;
   zhon(1)=Evn;
end   
Hp_hon=plot3(xhon,yhon,(zhon-shf),'.w');
set(Hp_hon,'MarkerSize',10,'Color',[1 0.95 0.95]);

%Is electron and trace
xise=wp; yise=Ymax/2; zise=Ecp;
xisetr=xise; yisetr=yise; zisetr=zise;
Htr_ise=plot3(xisetr,yisetr,zisetr,'-k');
set(Htr_ise,'EraseMode','xor','Color',[0 0.6 0],'Visible','off');
Hise=plot3(xise,yise,zise,'.k');
set(Hise,'MarkerSize',10,'EraseMode','xor','Color',[0 0.6 0],'Visible','off');
Htr_ish=plot3(xisetr,yisetr,zisetr,'-y');
set(Htr_ish,'EraseMode','xor','Color',[1 0.95 0.95],'Visible','off');
Hish=plot3(xise,yise,zise,'.y');
set(Hish,'MarkerSize',10,'EraseMode','xor','Color',[1 0.95 0.95],'Visible','off');
MAXTR=10;
for i=1:MAXTR;
   Hide(i)=plot3(xise,yise,zise,'.k');
   set(Hide(i),'EraseMode','xor','Visible','off');
   Htr_ide(i)=plot3(xisetr,yisetr,zisetr,'-k');
   set(Htr_ide(i),'EraseMode','xor','Color',[0 0.6 0],'Visible','off');
   Hidh(i)=plot3(xise,yise,zise,'.y');
   set(Hidh(i),'MarkerSize',10,'EraseMode','xor','Color',[1 0.95 0.95],'Visible','off');
   Htr_idh(i)=plot3(xisetr,yisetr,zisetr,'-y');
   set(Htr_idh(i),'EraseMode','xor','Color',[1 0.95 0.95],'Visible','off');
end 
hold off;

ISCEL=round(log10(Na)-12);
ISCHO=round(log10(Nd)-12);
i_ISCEL=0;
i_ISCHO=0;
%%%%%%%%%%%%%%%%%%%%%%
%%%THE MAIN LOOP
%%%%%%%%%%%%%%%%%%%%%%%
while con_close_usd17<1;
   
%BRIGHTNESS
%%%%%%%%%%%%%%%%%%%%%%%%
if con_br_usd17>0;
   bright=get(Hsl_br,'Value');
      if ishandle(Hf)==0;
         return; 
      end;
set(Hsv,'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
con_br_usd17=0;
end; %con_br_usd17>0;

 
%VOLTAGE IS CHANGED   
%%%%%%%%%%%%%%%%%%%%%%%%   
if con_vds_usd17>0;   
   Vd=round(get(Hsl_vd,'Value')*10)/10;
   Id=IS*(exp(Vd/Vt)-1);
if Id>14.5; 
   Vd=Vt*log(14.5/IS+1);
   Vd=round(Vd*1000)/1000; 
   set(Hsl_vd,'Value',Vd);
   Id=IS*(exp(Vd/Vt)-1);
end
set(Ht_vdd,'String',[num2str(Vd) ' V']);
wn=sqrt(2*epssi*(Vbi-Vd)/(q*Nd*1e6*(1+Nd/Na)))*1e6;
wp=sqrt(2*epssi*(Vbi-Vd)/(q*Na*1e6*(1+Na/Nd)))*1e6;
phin=Vbi-Vd-(0.5*q*Nd*1e6/epssi)*1e-12*(xs+wn).^2;
phip=(0.5*q*Na*1e6/epssi)*1e-12*(xs-wp).^2;
Ec=-(Vbi-Vd)*(xs<=-wn)-phin.*(xs>-wn).*(xs<0)-...
   phip.*(xs>=0).*(xs<wp)+phifp+Eg/2;
Ev=Ec-Eg;
Efn=Vd;
%Calculate the concentration
Ncarr=-ni*exp((Eg/2-Ec+Efn)/Vt).*(xs<0)+ni*exp((Eg/2+Ev)/Vt).*(xs>=0);
Ncarr=(abs(Ncarr)<2e10).*1e10+(abs(Ncarr)>=2e10).*Ncarr;
z2=sign(Ncarr).*log10(abs(Ncarr));
   if ishandle(Hf)==0;
      return; 
   end;
set(Hsv,'ZData',(Ec-Eg),...
   'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'ZData',Ec,...
   'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
set(Hp_efn,'XData',[Xmin -wn],'ZData',[Efn Efn]);
set(Hp_efp,'XData',[wp Xmax]);

%Electrons and holes
%The majority electrons
Ecn=Efn+0.5*Eg+phifn;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xeln=0; yeln=0; zeln=Ecn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1))+Efn;
   if zt>Ecn;
      zeln(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Neln=1;
   zeln(1)=Ecn+Vt*rand(1,1);
end   
[aa,Neln]=size(zeln);
xeln=rand(1,Neln)*(-wn-Xmin)+Xmin;
yeln=rand(1,Neln)*Ymax;
else; %The depletion layer is through
   Neln=1;
   xeln(1)=Xmin-1;
   yeln(1)=Ymax+1;
   zeln(1)=Ecn;
end   
set(Hp_eln,'XData',xeln,'YData',yeln,'ZData',(zeln+shf));

%The minority electrons
Ecp=0.5*Eg+phifp;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xelp=0; yelp=0; zelp=Ecp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1));
   if zt>Ecp;
      zelp(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nelp=1;
   zelp(1)=Ecp+Vt*rand(1,1);
end   
[aa,Nelp]=size(zelp);
xelp=rand(1,Nelp)*(Xmax-wp)+wp;
yelp=rand(1,Nelp)*Ymax;
else; %the depletion layer is through
   Nelp=1;
   xelp(1)=Xmax+1;
   yelp(1)=Ymax+1;
   zelp=Ecp;
end;   
if Vd>0;
xels=0; yels=0; zels=Ecp;
NNI=ceil(NN*(Ymax*Ln));
ii=0;
if NNI>=1;
   for i=1:NNI;
%The stored-charge electrons
      ii=ii+1;
      xt=(ii+rand(1,1)-0.5-5*floor(ii/5))*Ln+wp;
      zt=-kVt*Vt*log(rand(1,1))+Efn*(1-(xt-wp)/Ln);
      if zt>Ecp;
         zels(ii)=zt;
         xels(ii)=xt;
      else 
         ii=ii-1;
      end   
   end
   if ii>0; 
   [aa,Nels]=size(zels);
   yels=rand(1,Nels)*Ymax;
   xelp=[xelp xels];
   yelp=[yelp yels];
   zelp=[zelp zels];
   end
end;   
end; %if Vd>0
set(Hp_elp,'XData',xelp,'YData',yelp,'ZData',(zelp+shf));

%The majority holes
Evp=Ecp-Eg;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xhop=0; yhop=0; zhop=Evp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1));
   if zt<Evp;
      zhop(ii)=zt;
   else
      ii=ii-1;
   end;
end
if ii<1; 
   Nhop=1;
   zhop(1)=Evp-Vt*rand(1,1);
end 
[aa,Nhop]=size(zhop);
xhop=rand(1,Nhop)*(Xmax-wp)+wp;
yhop=rand(1,Nhop)*Ymax;
else; %the depl. layer is through   
   Nhop=1;
   xhop(1)=Xmax+1;
   yhop(1)=Ymax+1;
   zhop=Evp;
end
set(Hp_hop,'XData',xhop,'YData',yhop,'ZData',(zhop-shf));

%The minority holes
Evn=Ecn-Eg;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xhon=0; yhon=0; zhon=Evn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1))+Efn;
   if zt<Evn;
      zhon(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nhon=1;
   zhon(1)=Evn-Vt*rand(1,1);
end   
[aa,Nhon]=size(zhon);
xhon=rand(1,Nhon)*(-wn-Xmin)+Xmin;
yhon=rand(1,Nhon)*Ymax;
else; %The depletion layer is through
   Nhon=1;
   xhon(1)=Xmin-1;
   yhon(1)=Ymax+1;
   zhon(1)=Evn;
end   
%The stored-charge holes
if Vd>0;
xhos=0; yhos=0; zhos=Evn;
NNI=ceil(NN*(Ymax*Lp));
Nhos=ceil(0.5*NNI*exp(Evn/(kVt*Vt)));
if Nhos>=1;
   ii=1; LL=1;
   while ii<=Nhos;
      ll=1;
      while ((ll<=LL)*(ii<=Nhos));
      xhos(ii)=-(ll-1+0.5*rand(1,1))*Lp/5-wn;
      zhos(ii)=(kVt*Vt)*(1-(ll-1)/3)*log(rand(1,1))+Evn;
      ll=ll+1;
      ii=ii+1;
      end; %while (((ll<=5)+(ll<=ii))*(ii<=Nels);
      LL=LL+1; if LL>5; LL=5; end;
   end; %while ii<=Nhos
   yhos=rand(1,Nhos)*Ymax;
   xhon=[xhon xhos];
   yhon=[yhon yhos];
   zhon=[zhon zhos];
end;   
end; %if Vd>0
set(Hp_hon,'XData',xhon,'YData',yhon,'ZData',(zhon-shf));

if Vd==0;
   set([Hp_ifqvfq Hp_irqvrq],'XData',0,'YData',0,'Visible','on');
else
   if Vd>0; %FWD
      set(Hp_ifqvfq,'XData',Vd,'YData',Id,'Visible','on');
      set(Hp_irqvrq,'Visible','off');
   else  %REV
      set(Hp_irqvrq,'XData',Vd,'YData',Id,'Visible','on');
      set(Hp_ifqvfq,'Visible','off');
   end   
end   
con_vds_usd17=0;
end; %if con_vg_usd17>0

%DOPING IS CHANGED
%%%%%%%%%%%%%%%%%%%%%%%%%
if con_sl_usd17>0;
if con_sl_usd17>1;   
   Nd=10^(get(Hsl_dn,'Value'));
   set(Ht_dnd,'String',[num2str(round(10^(get(Hsl_dn,'Value')))) ' cm-3']);
   phifn=-Vt*log(Nd/ni);
else
   Na=10^(get(Hsl_dp,'Value'));
   set(Ht_dpd,'String',[num2str(round(10^(get(Hsl_dp,'Value')))) ' cm-3']);
   phifp=Vt*log(Na/ni);
end

IS=(Isn/Nd+Isp/Na);
Id=IS*(exp(Vd/Vt)-1);
if Id>14.5; 
   Vd=Vt*log(14.5/IS+1);
   Vd=round(Vd*1000)/1000; 
   set(Hsl_vd,'Value',Vd);
   set(Ht_vdd,'String',[num2str(Vd) ' V']);
   Id=IS*(exp(Vd/Vt)-1);
end

Vbi=phifp-phifn;
wn=sqrt(2*epssi*(Vbi-Vd)/(q*Nd*1e6*(1+Nd/Na)))*1e6;
wp=sqrt(2*epssi*(Vbi-Vd)/(q*Na*1e6*(1+Na/Nd)))*1e6;
phin=Vbi-Vd-(0.5*q*Nd*1e6/epssi)*1e-12*(xs+wn).^2;
phip=(0.5*q*Na*1e6/epssi)*1e-12*(xs-wp).^2;
Ec=-(Vbi-Vd)*(xs<=-wn)-phin.*(xs>-wn).*(xs<0)-...
   phip.*(xs>=0).*(xs<wp)+phifp+Eg/2;
Ev=Ec-Eg;
Efn=Vd;
%Calculate the concentration
Ncarr=-ni*exp((Eg/2-Ec+Efn)/Vt).*(xs<0)+ni*exp((Eg/2+Ev)/Vt).*(xs>=0);
Ncarr=(abs(Ncarr)<2e10).*1e10+(abs(Ncarr)>=2e10).*Ncarr;
z2=sign(Ncarr).*log10(abs(Ncarr));
   if ishandle(Hf)==0;
      return; 
   end;
set(Hsv,'ZData',(Ec-Eg),...
   'CData',((abs(z2)>bright).*((abs(z2)-bright)/(19.1-bright)*9+1)));
set(Hsc,'ZData',Ec,...
   'CData',((abs(z2)>bright).*(z2-sign(z2).*bright)*26.5/(z2_max-bright)+37.5));
set(Hp_efn,'XData',[Xmin -wn],'ZData',[Efn Efn]);
set(Hp_efp,'XData',[wp Xmax]);
set(Ht_lab(1),'Position',[Xmax 0 (Eg/2+phifp)]);   
%set(Ht_lab(2),'Position',[Xmax 0 0]);
set(Ht_lab(3),'Position',[Xmax 0 -(Eg/2-phifp)]);

%Electrons and holes
%The majority electrons
Ecn=Efn+0.5*Eg+phifn;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xeln=0; yeln=0; zeln=Ecn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1))+Efn;
   if zt>Ecn;
      zeln(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Neln=1;
   zeln(1)=Ecn+Vt*rand(1,1);
end   
[aa,Neln]=size(zeln);
xeln=rand(1,Neln)*(-wn-Xmin)+Xmin;
yeln=rand(1,Neln)*Ymax;
else; %The depletion layer is through
   Neln=1;
   xeln(1)=Xmin-1;
   yeln(1)=Ymax+1;
   zeln(1)=Ecn;
end   
set(Hp_eln,'XData',xeln,'YData',yeln,'ZData',(zeln+shf));

%The minority electrons
Ecp=0.5*Eg+phifp;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xelp=0; yelp=0; zelp=Ecp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1));
   if zt>Ecp;
      zelp(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nelp=1;
   zelp(1)=Ecp+Vt*rand(1,1);
end   
[aa,Nelp]=size(zelp);
xelp=rand(1,Nelp)*(Xmax-wp)+wp;
yelp=rand(1,Nelp)*Ymax;
else; %the depletion layer is through
   Nelp=1;
   xelp(1)=Xmax+1;
   yelp(1)=Ymax+1;
   zelp=Ecp;
end;   
%The stored-charge electrons
if Vd>0;
xels=0; yels=0; zels=Ecp;
NNI=ceil(NN*(Ymax*Ln));
Nels=ceil(0.75*NNI*exp(-(Ecp-Efn)/(kVt*Vt)));
if Nels>=1;
   ii=1; LL=1;
   while ii<=Nels;
      ll=1;
      while ((ll<=LL)*(ii<=Nels));
      xels(ii)=(ll-1+0.5*rand(1,1))*Ln/5+wp;
      zels(ii)=-(kVt*Vt)*(1-(ll-1)/3)*log(rand(1,1))+Ecp;
      ll=ll+1;
      ii=ii+1;
      end; %while (((ll<=5)+(ll<=ii))*(ii<=Nels);
      LL=LL+1; if LL>5; LL=5; end;
   end; %while ii<=Nels
   yels=rand(1,Nels)*Ymax;
   xelp=[xelp xels];
   yelp=[yelp yels];
   zelp=[zelp zels];
end;   
end; %if Vd>0
set(Hp_elp,'XData',xelp,'YData',yelp,'ZData',(zelp+shf));

%The majority holes
Evp=Ecp-Eg;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xhop=0; yhop=0; zhop=Evp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1));
   if zt<Evp;
      zhop(ii)=zt;
   else
      ii=ii-1;
   end;
end
if ii<1; 
   Nhop=1;
   zhop(1)=Evp-Vt*rand(1,1);
end 
[aa,Nhop]=size(zhop);
xhop=rand(1,Nhop)*(Xmax-wp)+wp;
yhop=rand(1,Nhop)*Ymax;
else; %the depl. layer is through   
   Nhop=1;
   xhop(1)=Xmax+1;
   yhop(1)=Ymax+1;
   zhop=Evp;
end
set(Hp_hop,'XData',xhop,'YData',yhop,'ZData',(zhop-shf));

%The minority holes
Evn=Ecn-Eg;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xhon=0; yhon=0; zhon=Evn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1))+Efn;
   if zt<Evn;
      zhon(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nhon=1;
   zhon(1)=Evn-Vt*rand(1,1);
end   
[aa,Nhon]=size(zhon);
xhon=rand(1,Nhon)*(-wn-Xmin)+Xmin;
yhon=rand(1,Nhon)*Ymax;
else; %The depletion layer is through
   Nhon=1;
   xhon(1)=Xmin-1;
   yhon(1)=Ymax+1;
   zhon(1)=Evn;
end   
%The stored-charge holes
if Vd>0;
xhos=0; yhos=0; zhos=Evn;
NNI=ceil(NN*(Ymax*Lp));
Nhos=ceil(0.75*NNI*exp(Evn/(kVt*Vt)));
if Nhos>=1;
   ii=1; LL=1;
   while ii<=Nhos;
      ll=1;
      while ((ll<=LL)*(ii<=Nhos));
      xhos(ii)=-(ll-1+0.5*rand(1,1))*Lp/5-wn;
      zhos(ii)=(kVt*Vt)*(1-(ll-1)/3)*log(rand(1,1))+Evn;
      ll=ll+1;
      ii=ii+1;
      end; %while (((ll<=5)+(ll<=ii))*(ii<=Nels);
      LL=LL+1; if LL>5; LL=5; end;
   end; %while ii<=Nhos
   yhos=rand(1,Nhos)*Ymax;
   xhon=[xhon xhos];
   yhon=[yhon yhos];
   zhon=[zhon zhos];
end;   
end; %if Vd>0
set(Hp_hon,'XData',xhon,'YData',yhon,'ZData',(zhon-shf));


If=[0:Ifmax/100:Ifmax];
Vf=Vt*log(If/IS+1);
Vr=[Vrmin:abs(Vrmin/100):0];
Ir=IS*(exp(Vr/Vt)-1);
Irmin=1.1*min(Ir);
Ndig=10^floor(log10(abs(Irmin)));
Irmin=Ndig*1*floor((Irmin/Ndig)/1);
set(Ha_irvr,'Ylim',[Irmin -1e-20]);
set(Hp_irvr,'Xdata',Vr,'YData',Ir);

set(Hp_ifvf,'Xdata',Vf,'YData',If);
if Vd==0;
   set([Hp_ifqvfq Hp_irqvrq],'XData',0,'YData',0);
else
   if Vd>0; %FWD
      set(Hp_ifqvfq,'XData',Vd,'YData',Id);
   else  %REV
      set(Hp_irqvrq,'XData',Vd,'YData',Id);
   end   
end   

ISCEL=round(log10(Na)-12);
ISCHO=round(log10(Nd)-12);
i_ISCEL=0;
i_ISCHO=0;
con_sl_usd17=0;
end; %if con_sl_usd17>0;

%3D/2D SELECTIONS
%%%%%%%%%%%%%%%%%%%%
if con_r_usd17>0;
   set(Hr,'Value',0);
   csw=con_r_usd17;
   switch csw;
   case 1;   
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(1),'Value',1);
      scf=1;
      wdf=0.8/scf;
      btf=0.95-wdf;
      Az=-20;
      El=20;
      set(Hsl_el,'Value',El);
      set(Hsl_az,'Value',Az);
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
      set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      set([Hp_efn Hp_efp],'Visible','on');
      set(Ht_lab,'Visible','on');

   case 2;   
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(2),'Value',1);
      scf=2;
      wdf=0.8/scf;
      btf=0.95-wdf;
      Az=0; 
      El=90;
      set(Hsl_el,'Value',El);
      set(Hsl_az,'Value',Az);
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
      set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      set([Hp_efn Hp_efp],'Visible','off');
      set(Ht_lab,'Visible','off');

   case 3;
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(3),'Value',1);
      scf=1;
      wdf=0.8/scf;
      btf=0.95-wdf;
      Az=0; 
      El=1;
      set(Hsl_el,'Value',El);
      set(Hsl_az,'Value',Az);
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
      set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      set([Hp_efn Hp_efp],'Visible','on');
      set(Ht_lab,'Visible','on');
      
   case 4;
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(1),'Value',1);
      Az=round(get(Hsl_az,'Value'));
      set(Ha2,'View',[Az El]);
      set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.']);
   
   case 5;
      El=round(get(Hsl_el,'Value'));
      if ishandle(Hf)==0;
         return; 
      end;
      set(Hr(1),'Value',1);
      if El>20;
         scf=1+(El-20)/70;
         wdf=0.8/scf;
         btf=0.95-wdf;
         set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      else;  
         scf=1;
         wdf=0.8/scf;
         btf=0.95-wdf;
         set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
      end;   
      set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.']);
      if El>85;
         set([Hp_efn Hp_efp],'Visible','off');
         set(Ht_lab,'Visible','off');
      else
         set([Hp_efn Hp_efp],'Visible','on');
         set(Ht_lab,'Visible','on');
      end   
   end
con_r_usd17=0;
end; %con_r_usd17>0;

%DEMO ROTATIONS
%%%%%%%%%%%%%%%%%
if con_start_usd17>0;
   dsw=con_start_usd17;
   set([Hsl_br Hsl_vd Hsl_dp Hsl_dn],'Enable','off');
   set(Hb_close,'Enable','off');
   set([Hp_hop Hp_hon],'EraseMode','normal');
   switch dsw;
   case 1; %3D -> x-y  
      Nstps=10;
      dAz=20/Nstps;
      dEl=(90-20)/Nstps;
      Az=-20;
      El=20;
      set([Hsl_el Hsl_az],'Enable','off');
      set([Ht_azd Ht_eld],'Visible','off');
      set(Hr,'Value',0,'Enable','off');
      set([Hb_start1 Hb_start2],'Enable','off');
      for i=1:(Nstps+1)
         Az=(i-1)*dAz-20;
         El=(i-1)*dEl+20;
         scf=1+(El-20)/70;
         wdf=0.8/scf;
         btf=0.95-wdf;
         if ishandle(Hf)==0;
            return; 
         end;
         set(Ha2,'Position',[0.05 btf 0.65 wdf],'View',[Az El]);
         if El>85;
            set([Hp_efn Hp_efp],'Visible','off');
            set(Ht_lab,'Visible','off');
         else
            set([Hp_efn Hp_efp],'Visible','on');
            set(Ht_lab,'Visible','on');
         end   
         drawnow;
         tic;
         while toc<0.3;
            pause(0.1);
         end 
         if ishandle(Hf)==0;
            return; 
         end;
      end  
         set(Hr,'Value',0);
         set(Hr(2),'Value',1);
         set(Hr,'Enable','on');
         set(Hsl_el,'Value',El,'Enable','on');
         set(Hsl_az,'Value',Az,'Enable','on');
         set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.'],'Visible','on');
         set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.'],'Visible','on');
         set([Hb_start1 Hb_start2],'Enable','on');

   case 2; %3D -> E-x
      Nstps=10;
      dAz=20/Nstps;
      dEl=(20-1)/Nstps;
      Az=-20;
      El=20;
      set([Hsl_el Hsl_az],'Enable','off');
      set([Ht_azd Ht_eld],'Visible','off');
      set(Hr,'Value',0,'Enable','off');
      set([Hb_start1 Hb_start2],'Enable','off');
      scf=1;
      wdf=0.8/scf;
      btf=0.95-wdf;
      set(Ha2,'Position',[0.05 btf 0.65 wdf]);
      set([Hp_efn Hp_efp],'Visible','on');
      set(Ht_lab,'Visible','on');
      for i=1:(Nstps+1)
         Az=(i-1)*dAz-20;
         El=-(i-1)*dEl+20;
         if ishandle(Hf)==0;
            return; 
         end;
         set(Ha2,'View',[Az El]);
         drawnow;
         tic;
         while toc<0.3;
            pause(0.1);
         end 
         if ishandle(Hf)==0;
            return; 
         end;
      end   
         set(Hr,'Value',0);
         set(Hr(3),'Value',1);
         set(Hr,'Enable','on');
         set(Hsl_el,'Value',El,'Enable','on');
         set(Hsl_az,'Value',Az,'Enable','on');
         set(Ht_eld,'String',['Elevation=' num2str(round(get(Hsl_el,'Value'))) ' deg.'],'Visible','on');
         set(Ht_azd,'String',['Azimuth=' num2str(round(get(Hsl_az,'Value'))) ' deg.'],'Visible','on');
         set([Hb_start1 Hb_start2],'Enable','on');

   end;
   set([Hsl_br Hsl_vd Hsl_dp Hsl_dn],'Enable','on');
   set(Hb_close,'Enable','on');
con_start_usd17=0;
end;   

%RESET ELECTRON/HOLE POSITIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if El>85; 
   set([Hp_hop Hp_hon],'EraseMode','xor');
else
   set([Hp_hop Hp_hon],'EraseMode','normal');
end 
%Electrons and holes
%The majority electrons
Ecn=Efn+0.5*Eg+phifn;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xeln=0; yeln=0; zeln=Ecn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1))+Efn;
   if zt>Ecn;
      zeln(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Neln=1;
   zeln(1)=Ecn+Vt*rand(1,1);
end   
[aa,Neln]=size(zeln);
xeln=rand(1,Neln)*(-wn-Xmin)+Xmin;
yeln=rand(1,Neln)*Ymax;
else; %The depletion layer is through
   Neln=1;
   xeln(1)=Xmin-1;
   yeln(1)=Ymax+1;
   zeln(1)=Ecn;
end   
set(Hp_eln,'XData',xeln,'YData',yeln,'ZData',(zeln+shf));

%The minority electrons
Ecp=0.5*Eg+phifp;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xelp=0; yelp=0; zelp=Ecp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=-kVt*Vt*log(rand(1,1));
   if zt>Ecp;
      zelp(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nelp=1;
   zelp(1)=Ecp+Vt*rand(1,1);
end   
[aa,Nelp]=size(zelp);
xelp=rand(1,Nelp)*(Xmax-wp)+wp;
yelp=rand(1,Nelp)*Ymax;
else; %the depletion layer is through
   Nelp=1;
   xelp(1)=Xmax+1;
   yelp(1)=Ymax+1;
   zelp=Ecp;
end;  
%The stored-charge electrons
if Vd>0;
xels=0; yels=0; zels=Ecp;
NNI=ceil(NN*(Ymax*Ln));
Nels=ceil(0.75*NNI*exp(-(Ecp-Efn)/(kVt*Vt)));
if Nels>=1;
   ii=1; LL=1;
   while ii<=Nels;
      ll=1;
      while ((ll<=LL)*(ii<=Nels));
      xels(ii)=(ll-1+0.5*rand(1,1))*Ln/5+wp;
      zels(ii)=-(kVt*Vt)*(1-(ll-1)/3)*log(rand(1,1))+Ecp;
      ll=ll+1;
      ii=ii+1;
      end; %while (((ll<=5)+(ll<=ii))*(ii<=Nels);
      LL=LL+1; if LL>5; LL=5; end;
   end; %while ii<=Nels
   yels=rand(1,Nels)*Ymax;
   xelp=[xelp xels];
   yelp=[yelp yels];
   zelp=[zelp zels];
end;   
end; %if Vd>0
set(Hp_elp,'XData',xelp,'YData',yelp,'ZData',(zelp+shf));

%The majority holes
Evp=Ecp-Eg;
NNI=ceil(NN*(Ymax*(Xmax-wp)));
xhop=0; yhop=0; zhop=Evp;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1));
   if zt<Evp;
      zhop(ii)=zt;
   else
      ii=ii-1;
   end;
end
if ii<1; 
   Nhop=1;
   zhop(1)=Evp-Vt*rand(1,1);
end 
[aa,Nhop]=size(zhop);
xhop=rand(1,Nhop)*(Xmax-wp)+wp;
yhop=rand(1,Nhop)*Ymax;
else; %the depl. layer is through   
   Nhop=1;
   xhop(1)=Xmax+1;
   yhop(1)=Ymax+1;
   zhop=Evp;
end
set(Hp_hop,'XData',xhop,'YData',yhop,'ZData',(zhop-shf));

%The minority holes
Evn=Ecn-Eg;
NNI=ceil(NN*(Ymax*(-wn-Xmin)));
xhon=0; yhon=0; zhon=Evn;
ii=0;
if NNI>=1;
for i=1:NNI;
   ii=ii+1;
   zt=kVt*Vt*log(rand(1,1))+Efn;
   if zt<Evn;
      zhon(ii)=zt;
   else 
      ii=ii-1;
   end   
end
if ii<1; 
   Nhon=1;
   zhon(1)=Evn-Vt*rand(1,1);
end   
[aa,Nhon]=size(zhon);
xhon=rand(1,Nhon)*(-wn-Xmin)+Xmin;
yhon=rand(1,Nhon)*Ymax;
else; %The depletion layer is through
   Nhon=1;
   xhon(1)=Xmin-1;
   yhon(1)=Ymax+1;
   zhon(1)=Evn;
end   
%The stored-charge holes
if Vd>0;
xhos=0; yhos=0; zhos=Evn;
NNI=ceil(NN*(Ymax*Lp));
Nhos=ceil(0.75*NNI*exp(Evn/(kVt*Vt)));
if Nhos>=1;
   ii=1; LL=1;
   while ii<=Nhos;
      ll=1;
      while ((ll<=LL)*(ii<=Nhos));
      xhos(ii)=-(ll-1+0.5*rand(1,1))*Lp/5-wn;
      zhos(ii)=(kVt*Vt)*(1-(ll-1)/3)*log(rand(1,1))+Evn;
      ll=ll+1;
      ii=ii+1;
      end; %while (((ll<=5)+(ll<=ii))*(ii<=Nels);
      LL=LL+1; if LL>5; LL=5; end;
   end; %while ii<=Nhos
   yhos=rand(1,Nhos)*Ymax;
   xhon=[xhon xhos];
   yhon=[yhon yhos];
   zhon=[zhon zhos];
end;   
end; %if Vd>0
set(Hp_hon,'XData',xhon,'YData',yhon,'ZData',(zhon-shf));

%%THE TRACES 
%%%%%%%%%%%%%%%%%%%%
%Setting
%%%%%%%%%%%%%%%%%%%%
%Is electron
i_ISCEL=i_ISCEL+1;
if i_ISCEL>ISCEL; i_ISCEL=1; end
if i_ISCEL>=ISCEL;
   set(Hise,'Visible','on');
   set(Htr_ise,'Visible','on');
   xise=wp+0.1*rand(1,1);
   yise=rand(1,1)*Ymax;
   decxise=rand(1,1)*0.2;
   zise=Ecp+decxise;
   xisetr=xise; yisetr=yise; zisetr=zise;
   dxise=(xise+wn+rand(1,1)*0.1)/5;
   if Vd==0;
      xide=-wn-0.1*rand(1,1);
      dxide=(wp+0.1*rand(1,1)-xide)/5;
      yide=rand(1,1)*Ymax;
      zide=Ecp+rand(1,1)*0.2;
      set(Hide(1),'Visible','on');
      set(Htr_ide(1),'Visible','on');
   end
end
%Is hole
i_ISCHO=i_ISCHO+1;
if i_ISCHO>ISCHO; i_ISCHO=1; end
if i_ISCHO>=ISCHO;
   if Az>-3;
   set(Hish,'Visible','on');
   set(Htr_ish,'Visible','on');
   end
   xish=-wn-0.1*rand(1,1);
   yish=rand(1,1)*Ymax;
   decxish=rand(1,1)*0.2;
   zish=Evn-decxish;
   xishtr=xish; yishtr=yish; zishtr=zish;
   dxish=(wp+rand(1,1)*0.1-xish)/5;
   if Vd==0;
      xidh=wp+0.1*rand(1,1);
      dxidh=(xidh+wn+0.1*rand(1,1))/5;
      yidh=rand(1,1)*Ymax;
      zidh=Evn-rand(1,1)*0.2;
      if Az>-3;
      set(Hidh(1),'Visible','on');
      set(Htr_idh(1),'Visible','on');
      end
   end
end

if Vd>0;
   Neltr=round(0.33*Nels+(1/Na)/(1/Na+1/Nd)*Id/3);
   if Neltr>10; Neltr=10; end
   Nhotr=round(0.33*Nhos+(1/Nd)/(1/Na+1/Nd)*Id/3);
   if Nhotr>10; Nhotr=10; end
   xide=0; dxide=0; yide=0; zide=0;
   if Neltr>=1;
   for i=1:Neltr;
      xide(i)=-wn-0.1*rand(1,1);
      dxide(i)=(wp+0.1*rand(1,1)-xide(i))/5;
      yide(i)=rand(1,1)*Ymax;
      zide(i)=Ecp+rand(1,1)*0.2;
      set(Hide(i),'Visible','on');
      set(Htr_ide(i),'Visible','on');
   end; 
   end
   xidh=0; dxidh=0; yidh=0; zidh=0;
   if Nhotr>=1;
   for i=1:Nhotr;
      xidh(i)=wp+0.1*rand(1,1);
      dxidh(i)=(xidh(i)+wn+0.1*rand(1,1))/5;
      yidh(i)=rand(1,1)*Ymax;
      zidh(i)=Evn-rand(1,1)*0.2;
      if Az>-3;
      set(Hidh(i),'Visible','on');
      set(Htr_idh(i),'Visible','on');
      end
   end;
   end
end   

%%The traces loop
%%%%%%%%%%%%%%%%%
for i=1:5;
%Is electron
if i_ISCEL>=ISCEL;
   [aa,in]=min(abs(xise-xs(1,:)));
   zise=Ec(1,in)+decxise;
   xisetr(2*i-1)=xise;
   zisetr(2*i-1)=zise;
   yisetr(2*i-1)=yise;
   xise=xise-dxise;
   xisetr(2*i)=xise;
   zisetr(2*i)=zise;
   yisetr(2*i)=yise;
   set(Hise,'XData',xise,'YData',yise,'ZData',(zise+shf));
   set(Htr_ise,'XData',xisetr,'YData',yisetr,'ZData',(zisetr+shf));
   if Vd==0;
      xiden=xide+i*dxide;
      set(Hide,'XData',xiden,'YData',yide,'ZData',(zide+shf));
      set(Htr_ide,'XData',[xide xiden],'YData',[yide yide],'ZData',[(zide+shf) (zide+shf)]);
   end   
end;   
%Is hole
if i_ISCHO>=ISCHO;
   [aa,in]=min(abs(xish-xs(1,:)));
   zish=Ev(1,in)-decxish;
   xishtr(2*i-1)=xish;
   zishtr(2*i-1)=zish;
   yishtr(2*i-1)=yish;
   xish=xish+dxish;
   xishtr(2*i)=xish;
   zishtr(2*i)=zish;
   yishtr(2*i)=yish;
   set(Hish,'XData',xish,'YData',yish,'ZData',(zish-shf));
   set(Htr_ish,'XData',xishtr,'YData',yishtr,'ZData',(zishtr-shf));
   if Vd==0;
      xidhn=xidh-i*dxidh;
      set(Hidh,'XData',xidhn,'YData',yidh,'ZData',(zidh-shf));
      set(Htr_idh,'XData',[xidh xidhn],'YData',[yidh yidh],'ZData',[(zidh-shf) (zidh-shf)]);
   end   
end;   

if Vd>0;
   xiden=0;
   if Neltr>=1;
   for j=1:Neltr;
      xiden(j)=xide(j)+i*dxide(j);
      set(Hide(j),'XData',xiden(j),'YData',yide(j),'ZData',(zide(j)+shf));
      set(Htr_ide(j),'XData',[xide(j) xiden(j)],'YData',[yide(j) yide(j)],'ZData',[(zide(j)+shf) (zide(j)+shf)]);
   end   
   end
   xidhn=0;
   if Nhotr>=1;
   for j=1:Nhotr;
      xidhn(j)=xidh(j)-i*dxidh(j);
      set(Hidh(j),'XData',xidhn(j),'YData',yidh(j),'ZData',(zidh(j)-shf));
      set(Htr_idh(j),'XData',[xidh(j) xidhn(j)],'YData',[yidh(j) yidh(j)],'ZData',[(zidh(j)-shf) (zidh(j)-shf)]);
   end   
   end
end   

if ishandle(Hf)==0;
   return; 
end;
tic;
while toc<0.2;
   pause(0.05);
end   
if ishandle(Hf)==0;
   return; 
end;

end; %for i=1:5;
set([Hise Hish],'Visible','off');
set([Htr_ise Htr_ish],'Visible','off');
set([Hide Hidh],'Visible','off');
set([Htr_ide Htr_idh],'Visible','off');

end; %while of main loop
close;

