! $Id: main.f,v 1.11 2000/11/21 16:31:08 crumleyj Exp $

c  "Abandon all hope ye who enter here"
c This code is a mess and hopefully I'll eventually get around to fixing
c some of its defiencies, but in case I don't these comments can be
c considered a minimal guide to some of the pitfalls in this code.
c 
c	1. Naming- This code has many bad naming practices. Many common
c	blocks had the same name as the subroutine they were a part of.
c	These problems I fixed, most often by repaeting the first
c	letter of the name (kontr -> kkontr, move -> mmove, etc.)
c	Also, some variable names are re-used in different subroutines
c	and common blocks.  These variables (in particular, qinit) are
c	DIFFERENT.  They have similar roles for different subroutines,
c	and they cannot be mixed.
c
c	2.  Common blocks - This code makes use of common block in 
c	ways that seem odd to me.  Many common blocks are only used
c	in a single subroutine.  I believe that they are used to ensure
c	that variable values are preserved between calls to the subroutines.
c
c	3.  Data statements - These were one of my biggest problems in
c	porting this code to the IBM SP supercomputer from the Crays. 
c	The SP is very picky about initializing common blocks with 
c	data statements.  It require that this type of initialization
c	is done with block data statements (see the code for examples).
c	I'm not sure how standard these blocks are - they might be a 
c	problem for future porting.  These data statements cannot be
c	replaced by simple x=0 type initializations, because the majority
c	of them are in subroutines that are called multiple times.  The
c	state of these variables is supposed to be preserved between calls
c	due to use of common blocks, so re-initialization each time a 
c	subroutine is called would break the program.
c
c	4. Commenting - The commenting in this code is sparse.  I should
c	fix this, but I may not get around to it.  Also, there are some
c	parts that I don't really understand well enough to comment.
c
c  There are probably more things I should add here.  Nevertheless, I
c  hope this helps.
c
c Jim Crumley, March 23, 1999


      PROGRAM es2

c***********************************************************************
c*                                                                     *
c*    PERIODIC 2D CODE FOR R. BERGMAN'S PROBLEM...no electrons         * 
c*                                                                     *
c***********************************************************************
c
c
c
      include 'maxpar.h'
      include 'param.h'
c
!      dimension x(maxlen), y(maxlen), v(nvmax,maxlen), np(nsmax)
! dimensions defined in param.h now 

      dimension srho(ngxmax,ngymax,nsmax), rho(ngxmax,ngymax),
     %      ex(ngxmax,ngymax),ey(ngxmax,ngymax),phi(ngxmax,ngymax),
     %      we(ngxmax2+1,ngymax2+1)
      common x,y,we
      character*(maxchar) filenames(num_files),
     % tfilenames(num_files)  ! drop this file later - for debugging
      
      integer name_lengths(num_files)
c      real(4) delta, ddtime(2)
      real delta, ddtime(2)
c
c
      it = 0
c          initialize timestep counter
c
c	write(*,*) 'entering setup'
      call setup(filenames, name_lengths,tfilenames)
c          set up file linkages
c
c	write(*,*) 'entering start'
      call start(x,y,v)
c          read input file, and initialize parameters and variables
      call load_particles(x, y, v)  ! pulled from start
c
c	write(*,*) 'entering fields'
      write(*,*) "Masses ", m, np
      call fields(x,y,v,srho,rho,ex,ey,phi,we,it)
      write(*,*) "after fields - np = ", np
c          compute electric field and potential
c
c	write (*,*) 'entering output'
      call output(x,y,v,srho,rho,ex,ey,phi,we,it,filenames,
     %    name_lengths)

c          output any and all diagnostics
c
c*    begin timestep loop
c
   11 continue

	!delta=dtime_(ddtime)
C dtime gets the time since last call. ddtime(1) is the user time since the
C last call of dtime, dtime(2) is the system time since the last call to
C dtime.  delta is the total elapsed CPU time since the last call to dtime.
      
	write(*,*) ' ', it, ddtime(1), delta
      call move(x,y,v,ex,ey)
c          advance the particles one timestep
c
c          advance circuit one timestep
c
      it = it + 1
c          increment timestep counter
c
c
c	write(*,*) "entering fields ", x(486)
      call fields(x,y,v,srho,rho,ex,ey,phi,we,it)
c          compute electric field and potential
c
c        write(*,*) "entering output ", x(486)
      call output(x,y,v,srho,rho,ex,ey,phi,we,it,filenames,
     %    name_lengths)

c          output any and all diagnostics
c
      if(it.lt.nt)goto 11
c          check for end of run
c
c      write(*,*) 'entering finish'
      call finish(filenames,name_lengths)
c should be filenames, not tfilenames above, eventually
c          print final timing information, and close files
c
      end
c
c
