
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class Magnet extends MagnetGUI{

  double mu0 =  (1.0e-3) * 4.0 * Math.PI;    /*  units are gauss m/A  */
  double criterion = 1.0e-6;

  public Magnet(){
    a = 1.0e-3;   /* grid spacing, in m  */
    Bscale = 5.0;   /*  scale, in gauss, for the display of B */
    Icell = 10.0;   /* current in A flowing out of /into one cell */
  }

  void solve(){
    double maxdiff = 1.0;
    int iteration = 0;
    double mu1, mu2, mu3, mu4, cellcurrent;
    do {
      maxdiff = 0.0;
      for (int n = 1; n <= 20; n++){
        for (int i = 1; i < Nx; i++){
          for (int j =1 ; j < Ny; j++){
	      /*  query to find the value of mu and current in each cell */
	    mu1 =  (State[i-1][j-1] == IronState) ?  mu : 1.0;
	    mu2 =  (State[i][j-1] == IronState) ?  mu : 1.0;
	    mu3 =  (State[i-1][j] == IronState) ?  mu : 1.0;
	    mu4 =  (State[i][j] == IronState) ?  mu : 1.0;
            cellcurrent = 0.0;
            if (State[i][j] == UpWireState)  cellcurrent = Icell;
            if (State[i][j] == DownWireState) cellcurrent = -Icell;
            double oldA = A[i][j];
            double newA = 0.0;
	        /* put something more sensible here */
            A[i][j] = newA;
            double delta = Math.abs(newA-oldA);
            if (delta > maxdiff) maxdiff = delta;
          }
        }
        iteration++;
    } 
    refreshPicture();
    Legend.write("max. diff : "+maxdiff+ "      "+iteration);
    if (timetostop) break;
    } while ( maxdiff > criterion);
  }
}
