import java.util.Random;

public class GasSimulation implements Runnable{

  public double beta=0.0;
  private final int N=25;
  public int flag=0;
  public Particle part[] = new Particle[N+1];
  public int[] g = new int[11];

  private Gas appletref;
    double     iwidth;
    double     jwidth;

       //Constructor initiates simulation with cold start

  GasSimulation(Gas ref){    //unsure about argument

    int    i,j,k;           // indicies
    int    int_iwidth;      // integer value of width of space
    int    int_jwidth;      // integer value of height of space
    double shift;

    appletref=ref;    
    int_iwidth=(int)(appletref.iborder+0.05);
    int_jwidth=(int)(appletref.jborder+0.05);
    shift=appletref.iborder/2;
    i=0;
    for (j=1; j<=5; j++){
      for (k=1; k<=5; k++){
        i++;
        System.out.println("i="+i);
        part[i]=new Particle();
        part[i].x=k*30+shift;
        part[i].y=j*30+shift;

      }
    }
  } 

  public void run(){

    int        oldPotential;         // interaction before change
    int        newPotential;         // interaction after change
    double     deltax;               // x component of distance between spins
    double     deltay;               // y component of distance between spins
    double     testx;
    double     testy;
    double     oldx=0;
    double     oldy=0;
    double     distance;             // distance between two particles
    double     diameter;
    double     sq_diam;              // square of diameter
    double     halfWidth;            // half the width of space
    double     halfHeight;           // half the height of space
    double     ex;                   // step to add to x position
    double     ey;                   // step to add to y position
    double     stepsize;
    int        i,j,k;                // indicies
    int        sum;                  // sum of interacting spins
    boolean    proceed=true;             
    Random     number = new Random();

    stepsize=(double)appletref.stepsize;
    iwidth=(double)appletref.iborder;      
    jwidth=(double)appletref.jborder;  
    diameter=(double)appletref.diameter;
    sq_diam=diameter*diameter;
    halfWidth=iwidth*0.5;
    halfHeight=jwidth*0.5;
      
    while (true){
      for(k=1; k<=10;k++){

        i=(int)Math.floor(number.nextDouble()*N)+1;  //whats floor?
        
        sum=0;
 
        for (j=1; j<=N; j++){
          if (j!=i){
            deltax=part[i].x-part[j].x;
            if (deltax<0.0)
              deltax=-deltax;
            if (deltax>halfWidth)
              deltax=iwidth-deltax;

            deltay=part[i].y-part[j].y;
            if(deltay<0.0)
              deltay=-deltay;
            if(deltay>halfHeight)
              deltay=jwidth-deltay;

            distance= (deltax*deltax)+(deltay*deltay);

            if (distance<=1200){
              sum+=1;
            }

          }
        }

        oldPotential=-sum;


        proceed=true;
        sum=0;

        ex=(2*number.nextDouble()-1)*stepsize;
        ey=(2*number.nextDouble()-1)*stepsize;

        testx=((part[i].x+ex)%iwidth);
        testy=((part[i].y+ey)%jwidth);

        for (j=1; j<=N; j++){
          if(j!=i){
            deltax=testx-part[j].x;
            if (deltax<0.0)
              deltax=-deltax;
            if (deltax>halfWidth)
              deltax=iwidth-deltax;
         
            deltay=testy-part[j].y;
            if(deltay<0.0)
              deltay=-deltay;
            if(deltay>halfHeight)
              deltay=jwidth-deltay;

            distance=(deltax*deltax)+(deltay*deltay);

            if (distance<=900){
              j=N;
              proceed=false;
            }
            else{
              if(distance<1200){
                sum+=1;
              }
            }
          }
        }

        if(proceed){
          newPotential=-sum;
          if(newPotential<=oldPotential){
            oldx=part[i].x;
            oldy=part[i].y;
            part[i].MovePart(iwidth,jwidth,ex,ey);
          }
          else
            if(number.nextDouble()<Math.exp(beta*(oldPotential-newPotential))){
              oldx=part[i].x;
              oldy=part[i].y;
              part[i].MovePart(iwidth,jwidth,ex,ey);
            }
        }



      }
      appletref.drawing.repaint();
      try{Thread.sleep(10);}
      catch(InterruptedException e){}
    }
  }

}                     






