/*
 * NewJApplet.java
 *
 * Created on January 18, 2008, 7:16 PM
 */
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;



/**
 *
 * @author  root
 */
public class NewJApplet extends javax.swing.JApplet
implements ActionListener{
    
    Timer timer;
    double[][] u,du,checksum;
    double[] alpha,beta,rs,xsol,u_c,gam_c;
    double x,y,z,x1,x2,y3,y2,dumax,ddu,csmax,DELTAT;
    int count,iloop,iter,i,j,k,n,color,window_w,w_h;
    int selector,ivalues;
    double a0,a1,gam1,duij;
    int nmax=100;
    
    double DELTAX=0.1;
    double DELTAXSQ=0.01;
    
       
      public void actionPerformed(ActionEvent e) {
          jPanel1.repaint();
          timer.restart();
          
      }
     
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                     DELTAT=0.001;
                    initialize();
                     jTextField1.setText(""+DELTAT);
                     selector=0;
                     ivalues=0;
                   
                   
                    
                    
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        } jTextField1.setText(""+DELTAT);
        timer = new Timer(10, this);
        timer.setInitialDelay(1);
        timer.start(); 
    }
   public void initialize()
{
    

   alpha = new double[nmax];
   beta= new double[nmax];
   rs = new double[nmax];
   xsol = new double[nmax];
   u_c = new double[nmax];
   gam_c = new double[nmax];

    u = new double[nmax][nmax];
    du = new double[nmax][nmax];
    checksum = new double [nmax][nmax];
    
    for(i=0;i<nmax;i++) for(j=0;j<nmax;j++)
    { u[i][j]=0.0; du[i][j]=0.0;}
    
    u[nmax/2][nmax/2]=5.0;
    count=0;
    window_w=3;
   

    
    
}
    
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jPanel1 = new myPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 441, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 409, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jButton1.setText("restart");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel2.setText("Timestep");

        jTextField1.setText("jTextField1");

        jLabel3.setText("max log change");

        jLabel1.setText("jLabel1");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "explicit method", "implicit method", "crank-nicolson" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "T peak", "source" }));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(90, 90, 90)
                        .addComponent(jButton1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(76, 76, 76)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(44, 44, 44))
        );

        jLabel4.setText("Diffusion");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(89, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        initialize();
        DELTAT=Double.parseDouble(jTextField1.getText());
    }//GEN-LAST:event_jButton1MouseClicked

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        selector=(int)jComboBox1.getSelectedIndex();
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        ivalues=(int)jComboBox2.getSelectedIndex();
         // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

   

public class myPanel extends javax.swing.JPanel{

    double a0r,a1r;
    
    
    void tred()

     /* solves the system of equations

        a1* x[i-1]+a0*x[i]+ a1*x[i+1] =rs[i]

     */         

{
  double bet;
  int ic,jc;

  bet=a0;
  u_c[0]=rs[0]/bet;


  for(jc=1;jc<nmax;jc++)
    {  gam_c[jc]=a1/bet;
    bet=a0-a1*gam_c[jc];
    u_c[jc]=(rs[jc]-a1*u_c[jc-1])/bet;


 }

  for(jc=(nmax-2);jc>=0;jc--)
    u_c[jc]-=gam_c[jc+1]*u_c[jc+1];


  for(jc=0;jc<nmax;jc++) xsol[jc]=u_c[jc];
  
  

}


    void cn()
    {
       count++;
       dumax=0.0;



for(j=1;j<nmax-1;j++)
  {


    /* Koeffizienten des Gleichungssystems */

    a0=1.0+DELTAT/DELTAXSQ;
    a1=-0.5*DELTAT/DELTAXSQ;

    a0r=1.0-DELTAT/DELTAXSQ;
    a1r=-a1;

    rs[0]=a0r*u[0][j]+a1r*u[1][j];
    rs[nmax-1]=a1r*u[nmax-2][j]+a0r*u[nmax-1][j];
    for(i=1;i<nmax-1;i++) rs[i]=a1r*u[i-1][j]+a0r*u[i][j]+a1r*u[i+1][j];

    tred();



    for(i=0;i<nmax;i++)
      {  duij=Math.abs(xsol[i]-u[i][j]);
           
               if((ivalues==0)|| (i!=nmax/2)||(j!=nmax/2) )
               if(duij>dumax)dumax=duij;   }
    
    for(i=0;i<nmax;i++) u[i][j]=xsol[i];


  }


/* jetzt in j-Richtung */

for(i=1;i<nmax-1;i++)
  {

    /* Koeffizienten des Gleichungssystems */

    a0=1.0+DELTAT/DELTAXSQ;
    a1=-0.5*DELTAT/DELTAXSQ;

    a0r=1.0-DELTAT/DELTAXSQ;
    a1r=-a1;

    rs[0]=a0r*u[i][0]+a1r*u[i][1];
    rs[nmax-1]=a1r*u[i][nmax-2]+a0r*u[i][nmax-1];
    for(j=1;j<nmax-1;j++) rs[j]=a1r*u[i][j-1]+a0r*u[i][j]+a1r*u[i][j+1];

    tred();

    for(j=1;j<nmax-1;j++)
      {           duij=Math.abs(xsol[j]-u[i][j]);
           
               if((ivalues==0)|| (i!=nmax/2)||(j!=nmax/2)) 
               if(duij>dumax)dumax=duij;   }


    for(j=0;j<nmax;j++) u[i][j]=xsol[j];

if(ivalues==1) u[nmax/2][nmax/2]=5.0;


  }

    }
void implicit()
{


count++;

dumax=0.0;

/*  i- direction */

for(j=1;j<nmax-1;j++)
  {

    /* Koeffizienten des Gleichungssystems */

    a0=1.0+2.0*DELTAT/DELTAXSQ;
    a1=-DELTAT/DELTAXSQ;

    alpha[nmax-1]=0;
    beta[nmax-1]=u[nmax-1][j];


    for(i=nmax-2;i>=0;i--)
      {gam1=-1.0/(a0+a1*alpha[i+1]);
      alpha[i]=gam1*a1;
      beta[i]=gam1*(a1*beta[i+1]-u[i+1][j]);
      }

    for(i=1;i<nmax-1;i++)
      {  duij=u[i][j];
         u[i][j]=alpha[i-1]*u[i-1][j]+beta[i-1];
         duij=Math.abs(duij-u[i][j]);
           if((ivalues==0)|| (i!=nmax/2)||(j!=nmax/2)  )
               if(duij>dumax)dumax=duij;   }



  }


/* jetzt in j-Richtung */

for(i=1;i<nmax-1;i++)
  {

    /* Koeffizienten des Gleichungssystems */

    a0=1.0+2.0*DELTAT/DELTAXSQ;
    a1=-DELTAT/DELTAXSQ;

    alpha[nmax-1]=0;
    beta[nmax-1]=u[nmax-1][j];


    for(j=nmax-2;j>=0;j--)
      {gam1=-1.0/(a0+a1*alpha[j+1]);
      alpha[j]=gam1*a1;
      beta[j]=gam1*(a1*beta[j+1]-u[i][j+1]);
      }

    for(j=1;j<nmax-1;j++)
      {  duij=u[i][j];
         u[i][j]=alpha[j-1]*u[i][j-1]+beta[j-1];
         duij=Math.abs(duij-u[i][j]);
          
               if((ivalues==0)|| (i!=nmax/2)||(j!=nmax/2) )
               if(duij>dumax)dumax=duij;   }

  }

if(ivalues==1) u[nmax/2][nmax/2]=5.0;

}
    public void explicit()
    {
  count++;
  
     for(i=1;i<nmax -1;i++) for(j=1;j<nmax -1;j++)
{

  { du[i][j]=(u[i+1][j]+u[i-1][j]+u[i][j+1]+u[i][j-1]-4*u[i][j])*0.25
             *DELTAT/DELTAXSQ;}
}
dumax=0.0; /* maximale aenderung als Konvergenzkriterium */
csmax=0.0;

for(i=0;i<nmax;i++) for(j=0;j<nmax;j++)
checksum[i][j]=0.0;


for(i=1;i<nmax-1;i++) for(j=1;j<nmax-1;j++)
  {
   
     {
   ddu=Math.abs(du[i][j]); 
   u[i][j]= u[i][j]+du[i][j];
   
       if((ivalues==0)|| (i!=nmax/2)||(j!=nmax/2) )
   {
   if(ddu>dumax) {dumax=ddu; }}
   
   
     }
   }

if(ivalues==1) u[nmax/2][nmax/2]=5.0;

    }

    public void paintComponent(Graphics g)
{        int ix,iy,ix1,iy1,ix2,iy2,i2;
    
super.paintComponent(g);  


     jLabel1.setText(""+Math.log10(dumax));
     

w_h= 400;
if (selector==0) explicit();
if (selector==1) implicit();
if (selector==2) cn ();

for(i=0;i<nmax;i++)
{ for(j=0;j<nmax-1;j++)
  { color=(u[i][j]>0 ? 1:2);
    x1=j+i/2; y3=i+u[i][j]*30;
    x2=j+1+i/2; y2=i+u[i][j+1]*30;
    ix1=(int)(x1*window_w);
    iy1=w_h-(int)(y3*window_w);
    ix2=(int)(x2*window_w);
    iy2=w_h-(int)(y2*window_w);
  
 if(color==1) g.setColor(Color.black);
 if(color==2) g.setColor(Color.red);
    
 g.drawLine(ix1,iy1,ix2,iy2);
  }
  }
} 

}
    
    }



