%
% This File is an example of how to use DDGnlpoisson
% for equilibrium computations.
% 
% The device studied is an MOS capacitor 
% with 0 gate voltage
%


## This file is part of 
##
## SECS1D - A 1-D Drift--Diffusion Semiconductor Device Simulator
## -------------------------------------------------------------------
## Copyright (C) 2004-2007  Carlo de Falco
##
##
##
##  SECS1D is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  SECS1D is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with SECS1D; if not, write to the Free Software
##  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
##  USA

constants

%% device geometry
tbulk =  1.5e-6;
tox   =  90e-9;
L = tbulk + tox;
Nnodes = 300;
Nelements = Nnodes - 1;

x = linspace(0,L,Nnodes)';
sinodes = find(x<=tbulk);
Nsinodes = length(sinodes);
NelementsSi = Nsinodes-1;

Na=1e21; %% p-type doping density
D = - Na* ones(Nsinodes,1);

pp = Na ;
p = pp* ones(Nsinodes,1);
n = (ni^2)./p;

Fn = 0*n;
Fp = 0*n;

%% find threshold voltage
Vg = 0;
while((max(n)<Na)&(Vg<2))
    
    Vg = Vg + 0.01
    
    V = -Phims + Vg * ones(Nnodes,1);
    V(sinodes) = Fn + Vth*log(n/ni);
    
    % Scaling
    xs  = L;
    ns  = norm(D,inf);
    Din = D/ns;
    Vs  = Vth;
    xin   = x/xs;
    nin   = n/ns;
    pin   = p/ns;
    Vin   = V/Vs;
    Fnin  = (Fn - Vs * log(ni/ns))/Vs;
    Fpin  = (Fp + Vs * log(ni/ns))/Vs;
    
    l2    = (Vs*esio2)/(q*ns*xs^2)* ones(Nelements,1);
    l2(1:NelementsSi)    = (Vs*esi)/(q*ns*xs^2);
    
    % Solution of Nonlinear Poisson equation
    
    % Algorithm parameters
    toll  = 1e-10;
    maxit = 1000;
    verbose = 0;
    
    
    [V,nout,p,res,niter] = DDGnlpoisson (xin,sinodes,Vin,nin,...
        pin,Fnin,Fpin,Din,l2,toll,maxit,verbose);
    
    % Descaling
    n     = nout*ns;
    p     = p*ns;
    V     = V*Vs;
    
    
end

%!test
% MOS