// 22 fev 2004
// mars
//v2 juillet 04
// pyl@ccr.jussieu.fr
// www.lmm.jussieu.fr/~lagree/lagree.html
// libre de copie
// 
//javac AppletMascaret.java;open HelloMascaret.html
import java.awt.event.*;
import java.awt.*; 
import java.applet.*;
import java.io.*;                      // Import input-output Library 



public class AppletMascaret  extends java.applet.Applet  
 implements Runnable 
    {
	Thread runstring;
	private Image offScreenImage;
	int appWidth, appHeight ;
	int tt;
	double Lx=50,dx,dt,nu,mu,eps,t=0;
	double h[];
	
	double ho[];
	int n=500;  //333
  static 	PrintWriter q;
	
	
	public static void main(String[] argv) throws IOException, 
					 FileNotFoundException  
	 {
 q = new PrintWriter( new FileOutputStream("h.dat"), true); 
	 }
	
        public void init()  {
	int i;
	appWidth = getSize().width;
	appHeight = getSize().height;
	offScreenImage=createImage(appWidth,appHeight);
	tt=1;
	h =new double[n+1];
	ho=new double[n+1];
	dx=Lx/n;
//	dt=.000001;
	dt=Double.parseDouble(getParameter ("dt"));
//	nu =  .000001*dx*dx;
	nu=Double.parseDouble(getParameter ("nu"));
	eps=Double.parseDouble(getParameter ("eps"));	
	mu=Double.parseDouble(getParameter ("mu"));	
	System.out.println("pyl@ccr.jussieu.fr ");
	System.out.println("www.lmm.jussieu.fr/~lagree"); 
	System.out.println(" mars 2004 ");
	System.out.println(" dt =" + dt); 
	System.out.println(" dx =" + dx);
	System.out.println(" nu =" + nu);
	System.out.println(" mu =" + mu);
	System.out.println(" eps=" + eps);
	 
//pour faire un autre cas changer le commentaire	
	for(i=0;i<=n;i++) 
	  h[i] = 0.5*(1.-(2./Math.PI)*(Math.atan(0.3*(i-(2/2*n)/5.)*dx)));
	  //h[i] = 0.1+ 0.5* Math.exp( -(0.3*(i-(2*n)/5.)*dx)*(0.3*(i-(2*n)/5.)*dx));
	    
	// h[i] = 1 - i*1./n; 
	//for(i=0;i<(2*n)/3;i++) h[i] = 1;
	//for(i=(2*n)/3;i<n;i++) h[i] = 0;
	for(i=0;i<=n;i++) {ho[i]=h[i];/* System.out.println(ho[i])*/;}
	 System.out.println("hauteur de h[0]="+h[0]+" � h["+n+"]="+h[n]);
	 System.out.println("observation de l'avancee du ressaut");
	 System.out.println("puis formation du mascaret.         ");
         }

/*  indispensable */
   public void start()
      {
	if(runstring==null);
	{
	 runstring=new Thread(this);
	 runstring.start();
	}
      }

      public void stop()
      {
	 if(runstring != null){
	   runstring.stop();
	   runstring=null;
	 }
      }
      
    public void run()
     {
       while(true){
	 repaint();
	 try{ Thread.sleep(300);
	 }
	 catch(InterruptedException e){ }
       }
     }

    public void update(Graphics g)
     { //To avoid flicker
       Graphics offScreenGraphics=offScreenImage.getGraphics();
       offScreenGraphics.setColor(getBackground());
       offScreenGraphics.fillRect(0,0,size().width,size().height);
       offScreenGraphics.setColor(g.getColor());
       paint(offScreenGraphics);
       g.drawImage(offScreenImage,0,0,this);
     }

/* fin du debut */ 
    
public double  ff(double x) {
	 return  Math.sin(2*(x-tt));}    
    
public void nouvel1(){
int i;
     for ( i = 2 ; i < n-1 ; i++) {
       ho[i] = h[i] - dt*(0+eps*h[i])*(h[i+1]-h[i-1])/2/dx
                    +  dt* nu * (h[i+1]-2*h[i]+h[i-1]) /dx/dx
                    - mu*dt*(h[i+2]-2*h[i+1]+ 2*h[i-1]-h[i-2])/dx/dx/dx/2
          ; }
 for ( i = 1 ; i < n ; i++) {
       h[i] = ho[i]; }
			
}   
public void nouvel(){
int i,ic;
double hn,e;
double mub,nub,epsb;
mub=mu*dt/dx/dx/dx/2;
nub=dt*(nu+3*dx)/dx/dx;
epsb=dt*eps/2/dx;
ic=0;
//System.out.println(" AA " + n + "  " + mub + " " + nub);
//Resolution par Gauss Seidel
  do{ic=ic+1;e=0;
     for ( i = 2 ; i < n-1 ; i++) {
       hn = (h[i]   + nub * (ho[i+1]+ho[i-1])  
	                - (0+epsb )*(ho[i+1]*ho[i+1]/2-ho[i-1]*ho[i-1]/2)/dx                
                    - mub*(ho[i+2]-2*ho[i+1]+ 2*ho[i-1]-ho[i-2]))/( 1 + 2 * nub)
          ;
	   e+=(hn-ho[i])*(hn-ho[i]);
	   ho[i]=hn;
	   }
	//****************************************************************
         hn  = (4.*ho[n-1]-ho[n-2])/3.; //condition en x=1
    //****************************************************************
    //    e+=(hn-ho[i])*(hn-ho[i]);
		ho[n]= hn;//ho[n-1]; 
	}while(Math.sqrt(e)>.0000001);
	// System.out.println(" e =" + Math.sqrt(e) + "ic = " + ic); 
 for ( i = 1 ; i < n ; i++) {
       h[i] = ho[i]; }
       	   
	 		  
}   	 
public void dro(Graphics g, double x1,double y1,double x2,double y2){
  int i1,i2,j1,j2;
  int rr,gg,bb;
  i1 = (int)(appWidth*x1/Lx);
  i2 = (int)(appWidth*x2/Lx);
  j1 = (int)(appHeight/2 - (appHeight/4.)*y1);
  j2 = (int)(appHeight/2 - (appHeight/4.)*y2);
  rr=Math.min((128*(int)(100*(y2)))/100,128);
  rr =0;
  gg=0;
  bb=250;//Math.max(0,255-rr);
  g.setColor(new Color(rr,gg, bb));
  g.drawLine(i1,j1,i2,j2);
}

public void nextpaint(Graphics g, int tt) {
    int i,jj;
    double x,y,xm1,ym1;

    for ( jj = 0 ; jj < 1000 ; jj++) {   
     t=t+dt;
	 nouvel();
     }
		  
		  
    
    
       xm1=0;
       ym1=h[0];       
        for ( i = 1 ; i < n ; i++) {
	x=i*dx;
	y= h[i];
        
	dro(g,xm1, ym1, x,y);
	    xm1=x;
	    ym1=y;
	  // q.println(x+" "+y); 
        } 
	//dro(g,n*dx/2, -.95, n*dx/2,h[n/2]-.05);
	dro(g,1, -.95,1,1-.1);
	dro(g,0, -1,n*dx,-1);
	
    g.setColor(new Color(255,0,0));
	g.drawString("h(x,t="+(((int)(t*100))/100.)+")",20,30);
	g.drawString("--->",20,50);
	g.drawString("--->",20,110);
	g.drawString("--->",20,70);
	g.drawString("--->",20,90);
	g.drawString("Mascaret! " ,100,10);
     }
    
    
    public void paint(Graphics g){

      nextpaint(g,tt);
      tt=tt+1;
	//  System.out.println(" tt =" + tt); 
	

   }
    

}

// javac AppletMascaret.java
// 
// 
// 
//pour memoire:    fichier HelloWorldApp.java
//class HelloWorldApp {
//    public static void main (String args[]) {
//        System.out.println("Hello World!");
//    }
//}
//
//
////Fp = F0 + h F1 + h^2 F2/2 + h^3 F3/6 + h^4 F4 ;
// Fpp = Fp /. (h -> 2h);
// Fppp = Fp /. (h -> 3h);
// Fm = Fp /. (h -> -h);
// Fmm = Fp /. (h -> -2h);
// Fmmm = Fp /. (h -> -3h);
// {Simplify[Fpp - 2 Fp + 2 Fm - Fmm]/2/h^3, 
//  Simplify[Fppp - 3 Fp + 3 Fm - Fmmm]/8/h^3}