/* steepest descent - time dependent Kohn-Sham 2D */ /* coded by Ikeuchi Mitsuru */ /* ver 0.0.1 2004.11.20 */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class steepestDescentTDKS2D extends Applet implements MouseListener, MouseMotionListener, ItemListener, ActionListener, AdjustmentListener, Runnable { /* -------------------------------------- set global ------ */ Choice che,cvw; Button bt_reset, bt_start, bt_stop, bt_loss; Scrollbar spx,spy; Thread th = null; Dimension dim; Image imgOff; Graphics gOff; int sleepTime = 30; int dispMode = 0; int dispObject = 0; int started = 1; int lossFlag = 0; double px1 = 2.0; double py1 = 0.0; int dgX, dgY, dgXb,dgYb; double viewTheta = -15.0*3.14/180.0; double viewFai = -72.0*3.14/180.0; double dtheta = 0.0*3.14/180.0; double pai = 3.1415926536; double cosTh = Math.cos(viewTheta); double sinTh = Math.sin(viewTheta); double cosFi = Math.cos(viewFai); double sinFi = Math.sin(viewFai); double t = 0.0; double dx = 1.0/4.0; double dy = 1.0/4.0; double dt = 1.0*dx*dx; double lz = 8.0; int NNx = 64+1; int NNy = 64+1; int NN = NNx+1; /* max(NNx,NNy)+1 */ int NNe = 2; int spin[] = { 1,1,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0 }; double energy[] = new double[NNe]; double psi[][][][] = new double[NNe][NNx][NNy][2]; double wrk[][][] = new double[NNx][NNy][2]; double vv[][] = new double[NNx][NNy]; double vvext[][] = new double[NNx][NNy]; double vvh[][] = new double[NNx][NNy]; double vvx[][] = new double[NNx][NNy]; double vvc[][] = new double[NNx][NNy]; double rho[][] = new double[NNx][NNy]; double aaRe[] = new double[NN]; double aaIm[] = new double[NN]; double bbRe[] = new double[NN]; double bbIm[] = new double[NN]; double bRe[] = new double[NN]; double bIm[] = new double[NN]; double uRe[] = new double[NN]; double uIm[] = new double[NN]; int xpts[] = new int[NN]; int ypts[] = new int[NN]; double icol[] = new double[NN]; /* ----------------------------- applet control ------ */ public void init() { resize(630,300); setBackground(Color.white); dim = getSize(); imgOff = createImage(dim.width,dim.height); gOff = imgOff.getGraphics(); che = new Choice(); che.addItem("0"); che.addItem("1"); che.addItemListener(this); cvw = new Choice(); cvw.addItem("rho"); cvw.addItem("grid-i"); cvw.addItem("dens-i"); cvw.addItem("phase-i"); cvw.addItem("Vext"); cvw.addItem("VH x100"); cvw.addItem("Vx x100"); cvw.addItem("Vc x100"); cvw.addItem("Veff"); cvw.addItemListener(this); bt_reset= new Button("reset"); bt_reset.addActionListener(this); bt_start= new Button("start"); bt_start.addActionListener(this); bt_stop = new Button("stop"); bt_stop.addActionListener(this); bt_loss = new Button("loss"); bt_loss.addActionListener(this); spx= new Scrollbar(Scrollbar.HORIZONTAL,70,10,0,110); spx.addAdjustmentListener(this); spy= new Scrollbar(Scrollbar.HORIZONTAL,50,10,0,110); spy.addAdjustmentListener(this); addMouseListener(this); addMouseMotionListener(this); setLayout(new BorderLayout()); Panel pnl = new Panel(); pnl.setLayout(new GridLayout(1,6,5,0)); pnl.add(bt_reset); pnl.add(bt_start); pnl.add(bt_stop); pnl.add(che); pnl.add(cvw); pnl.add(bt_loss); add(pnl,"North"); setInitialCondition(); } public void start() { if (th == null) { th = new Thread(this); th.start(); } } public void stop() { if (th != null) { th.stop(); th = null; } } public void itemStateChanged(ItemEvent ev){ if (ev.getSource() == che){ dispObject = che.getSelectedIndex(); } else if (ev.getSource() == cvw){ dispMode = cvw.getSelectedIndex(); } } public void actionPerformed(ActionEvent ev){ if(ev.getSource() == bt_reset){ started = -1; } else if(ev.getSource() == bt_start){ started = 1; } else if(ev.getSource() == bt_stop){ started = 0; } else if(ev.getSource() == bt_loss){ if (lossFlag==0) { lossFlag = 1; } else { lossFlag = 0; } } } public void adjustmentValueChanged(AdjustmentEvent ev){ if (ev.getSource() == spx) { px1 = 0.1*(double)(spx.getValue())-5.0; } else if (ev.getSource() == spy) { py1 = 0.1*(double)(spy.getValue())-5.0; } } public void mousePressed(MouseEvent ev){ } public void mouseReleased(MouseEvent ev){ dgXb = 0; dgYb = 0; dgX = 0; dgY = 0; } public void mouseClicked(MouseEvent ev){ } public void mouseEntered(MouseEvent ev){ } public void mouseExited (MouseEvent ev){ } public void mouseMoved (MouseEvent ev){ } public void mouseDragged(MouseEvent ev){ dgXb = dgX; dgYb = dgY; dgX=ev.getX(); dgY=ev.getY(); if (dgXb==0 && dgYb==0) { dgXb = dgX; dgYb = dgY; } viewTheta += 0.5*3.14/180.0*(dgX-dgXb); viewFai += 0.5*3.14/180.0*(dgY-dgYb); cosTh = Math.cos(viewTheta); sinTh = Math.sin(viewTheta); cosFi = Math.cos(viewFai); sinFi = Math.sin(viewFai); } public void run() { while (th != null) { try { timeEvolution(); offPaint(); repaint(); Thread.sleep(sleepTime); } catch (InterruptedException e) { } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(imgOff,0,0,this); } /* ----------------------------- offPaint -------------------- */ void offPaint() { int ie, gb, gbx,gby; double ev0,ek0,ev1,ek1; gOff.setColor(Color.white); gOff.fillRect(0,0,dim.width,dim.height); ev0 = meanPotential(psi[0]); ek0 = meanKinetic(psi[0]); ev1 = meanPotential(psi[1]); ek1 = meanKinetic(psi[1]); if (dispMode==0) { gridPlot(rho,800.0, 0.2,0); } else if (dispMode==1) { gridPsiPlot(dispObject,100.0, 0.2); } else if (dispMode==2) { densPlot(psi[dispObject],20.0, 0); } else if (dispMode==3) { densPlot(psi[dispObject],20.0, 1); } else if (dispMode==4) { gridPlot(vvext,0.2, 0.0,1); } else if (dispMode==5) { gridPlot(vvh,20.0, 0.0,1); } else if (dispMode==6) { gridPlot(vvx,20.0, 0.0,1); } else if (dispMode==7) { gridPlot(vvc,20.0, 0.0,1); } else if (dispMode==8) { gridPlot(vv,0.2, 0.0,1); } gOff.setColor(Color.black); gOff.drawString("object",630/6*3+10,40); gOff.drawString("view",630/6*4+10,40); if (lossFlag==1) { gOff.drawString("loss=ON",630/6*5+10,40); } else { gOff.drawString("loss=OFF",630/6*5+10,40); } gOff.setColor(Color.blue); gOff.drawString("t="+(int)(t*2418.9+0.5)/100.0+" as",10,40); gb = 330; gOff.setColor(Color.blue); gOff.drawString("Lx="+(int)(NNx*dx+0.5)+", Ly="+(int)(NNy*dy+0.5)+" au",gb,60); for (ie=0;ie="+(float)(meanPosX(psi[ie]))+" ",gb,gby+20); gOff.drawString("="+(float)(meanPosY(psi[ie]))+" ",gb,gby+40); } for (ie=0; ie|="+(float)(innerNorm(psi[ie],psi[dispObject]))+" ",gb,260+15*ie); } } /*------------------------------- plot methods ------------*/ void gridPsiPlot(int ie, double phWeight, double vvWeight) { int i,j,gbx,gby; int cx,cy,cz; double z,px,py,pz,sc; double ph2; cx = NNx/2; cy = NNy/2; cz = 0; sc = 4.0; gbx = 30+(int)(cx/sc); gby = 50+(int)(cy/sc); for(j=0; j0.002) { icol[i] = 0.15; } else if (vv[i][j]>0.0) { icol[i] = 0.33; } else { icol[i] = 0.66; } } for (i=0; i0.002) { icol[j] = 0.15; } else if (vv[i][j]>0.0) { icol[j] = 0.33; } else { icol[j] = 0.66; } } for (j=0; j0.01) { if (ph2>1.0) { ph2 = 1.0; } if (mode==0) { gOff.setColor(Color.getHSBColor(0.15f, 0.95f, (float)(ph2))); } else if (mode==1) { gOff.setColor(Color.getHSBColor((float)(0.5+0.5*ph[i][j][0]/Math.sqrt(ph2/weight)), 0.9f, (float)(ph2))); } gOff.fillRect(gbx+mag*i,gby+mag*j, mag, mag); } } } } void gridPlot(double fn[][],double phWeight, double vvWeight,int mode) { int i,j,gbx,gby; int cx,cy,cz; double z,px,py,pz,sc; double ph2; cx = NNx/2; cy = NNy/2; cz = 0; sc = 4.0; gbx = 30+(int)(cx/sc); gby = 50+(int)(cy/sc); for(j=0; j0.001) { icol[i] = 0.15; } else if (vv[i][j]>0.0) { icol[i] = 0.33; } else { icol[i] = 0.66; } } for (i=0; i0.001) { icol[j] = 0.15; } else if (vv[i][j]>0.0) { icol[j] = 0.33; } else { icol[j] = 0.66; } } for (j=0; j=1.0) { ec = -0.1423/(1.0+1.0529*Math.sqrt(r)+0.3334*r); } else { ec = -0.0480-0.0116*r+(0.0311+0.0020*r)*Math.log(r); } return ( ec ); } /* ----------------------------- time step -------------- */ void kxStep(double ph[][][],double deltat) { int i,j; double a,aaAb,auAb; a = 4.0*dx*dx/deltat; for (j=1; j=1; i--) { ph[i][j][0] -= ph[i+1][j][0]*uRe[i] - ph[i+1][j][1]*uIm[i]; ph[i][j][1] -= ph[i+1][j][0]*uIm[i] + ph[i+1][j][1]*uRe[i]; } } } void kyStep(double ph[][][],double deltat) { int i,j; double a,aaAb,auAb; a = 4.0*dx*dx/deltat; for (i=1; i=1; j--) { ph[i][j][0] -= ph[i][j+1][0]*uRe[j] - ph[i][j+1][1]*uIm[j]; ph[i][j][1] -= ph[i][j+1][0]*uIm[j] + ph[i][j+1][1]*uRe[j]; } } } void phaseStep(double ph[][][],double deltat) { int i,j; double th,cs,sn,phr,phi; for (i=1; i