/* updating.h -- This file is part of Archimedes release 0.0.1.
   Archimedes is a simulator for Submicron 2D Silicon
   Devices. It implements the Monte Carlo method
   for the simulation of the semiclassical Boltzmann equation for both
   electrons and holes. It also includes the quantum effects by means 
   of effective potential method.

   Copyright (C) 2004 Jean Michel Sellier <sellier@dmi.unict.it>
 
   This program 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, or (at your option)
   any later version.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
   USA.  */


// ######################################################
// Created on 08 sep.2004, Siracusa, J.M.Sellier
// Last modif. : 07 Oct.2004, Siracusa, J.M.Sellier
// ######################################################

// Updating of the macroscopic variables
// Here we call all the subroutines we
// need for simulating the dynamics of the
// super-particles in the device.
// The solution is writted in the array named u2d.

void 
updating(int model)
{
 register int i,j;
 real maxi=0.,mini=0.;
// Computation of the electric field
// =================================
  Electric_Field();
// Monte Carlo Simulation
// ======================
  if(model==MCE || model==MCEH){
   EMC();
   Charge();
   if((int)(TF/DT)-(int)(TEMPO/DT)<MEDIA){
     media();
   }
   if((TEMPO+DT)>=TF) DT=TF-TEMPO;
   TEMPO+=DT;
  }
// Output on some usefull informations about the simulation
  printf("%5d   TIME = %10.4g  (picosec)\n",c,TEMPO*1.e12);
  if(MAXIMINI==1){
// Compute the maximum and minimum of various macroscopic variables
// Max and Min of Potential
    maxi=mini=0.;
    for(i=1;i<=nx+1;i++)
      for(j=1;j<=ny+1;j++){
        if(u2d[i][j][0]>=maxi) maxi=u2d[i][j][0];
        if(u2d[i][j][0]<=mini) mini=u2d[i][j][0];
      }
    printf("Max. Potential = %g\n",maxi);
    printf("Min. Potential = %g\n",mini);
// Max and Min of x-component of electric field
    maxi=mini=0.;
    for(i=1;i<=nx+1;i++)
      for(j=1;j<=ny+1;j++){
        if(E[i][j][0]>=maxi) maxi=E[i][j][0];
        if(E[i][j][0]<=mini) mini=E[i][j][0];
      }
    printf("Max. x-elec.field = %g\n",maxi);
    printf("Min. x-elec.field = %g\n",mini);
// Max and Min of y-component of electric field
    maxi=mini=0.;
    for(i=1;i<=nx+1;i++)
      for(j=1;j<=ny+1;j++){
        if(E[i][j][1]>=maxi) maxi=E[i][j][1];
        if(E[i][j][1]<=mini) mini=E[i][j][1];
      }
    printf("Max. y-elec.field = %g\n",maxi);
    printf("Min. y-elec.field = %g\n",mini);
// Max and Min of Density
    maxi=0.;
    mini=DDmax;
    for(i=1;i<=nx+1;i++)
      for(j=1;j<=ny+1;j++){
        if(u2d[i][j][1]>=maxi) maxi=u2d[i][j][1];
        if(u2d[i][j][1]<=mini) mini=u2d[i][j][1];
      }
    printf("Max. Density = %g\n",maxi);
    printf("Min. Density = %g\n",mini);
  }

// Here we save at each step if this option has been choosed
  if(SAVEALWAYS==1){
    SaveOutputFiles(File_Format,c);
    printf("Output number %d has been saved\n",c);
  }

  if(fabs(TEMPO-TF)/fabs(TF)<SMALL){
// Compute the various currents on the various defined contacts
    Compute_Currents();
    c=ITMAX+10;
  }
}

// ========================================
