/* harmonic oscillator - steepest descent 2D */ /* coded by Ikeuchi Mitsuru */ /* ver 0.0.1 2004.08.06 */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class harmonicOscillatorSD2D extends Applet implements ItemListener, ActionListener, AdjustmentListener, Runnable { /* -------------------------------------- set global ------ */ Choice cvw; Button bt_reset, bt_start, bt_stop; Scrollbar spx,spy; Thread th = null; Dimension dim; Image imgOff; Graphics gOff; int sleepTime = 30; int dispMode = 3; int started = 1; double kx0 = 2.0; double ky0 = 2.0; double t = 0.0; double dx = 8.0/64.0; double dy = 8.0/64.0; double dt = 2.0*dx*dx; int NNx = 64+1; int NNy = 64+1; int NN = NNx+1; /* max(NNx,NNy)+1 */ double vv[][] = new double[NNx][NNy]; double sdEnergy[] = new double[20]; double sdState[][][] = new double[20][NNx][NNy]; double wrk[][] = new double[NNx][NNy]; /* ----------------------------- applet control ------ */ public void init() { resize(630,320); setBackground(Color.white); dim = getSize(); imgOff = createImage(dim.width,dim.height); gOff = imgOff.getGraphics(); cvw = new Choice(); cvw.addItem("density"); cvw.addItem("psi"); cvw.addItemListener(this); cvw.select("density"); 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); spx= new Scrollbar(Scrollbar.HORIZONTAL,200,10,50,210); spx.addAdjustmentListener(this); spy= new Scrollbar(Scrollbar.HORIZONTAL,200,10,50,210); spy.addAdjustmentListener(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(new Label(" ")); pnl.add(spx); pnl.add(spy); 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() == cvw){ dispMode = cvw.getSelectedIndex(); } } public void actionPerformed(ActionEvent ev){ if(ev.getSource() == bt_reset){ t = 0.0; setInitialCondition(); started = 0; } else if(ev.getSource() == bt_start){ started = 1; } else if(ev.getSource() == bt_stop){ started = 0; } } public void adjustmentValueChanged(AdjustmentEvent ev){ if (ev.getSource() == spx) { kx0 = 0.01*(double)(spx.getValue()); setPotential(); } else if (ev.getSource() == spy) { ky0 = 0.01*(double)(spy.getValue()); setPotential(); } } 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 i,gbx,gby; gOff.setColor(Color.white); gOff.fillRect(0,0,dim.width,dim.height); for (i=0; i<10; i++) { gbx = 10+120*(i%5); gby = 50+130*(i/5); densPlot2D50(sdState[i], gbx, gby); gOff.drawString("E["+i+"]="+(float)(sdEnergy[i])+" ",gbx+10,gby+130); } gOff.setColor(Color.black); gOff.drawString("steepest descent method",30,45); gOff.setColor(Color.blue); gOff.drawString("kx="+(int)(kx0*100.0+0.5)/100.0+" ",630/6*4+10,40); gOff.drawString("ky="+(int)(ky0*100.0+0.5)/100.0+" ",630/6*5+10,40); } /*------------------------------ plot methods --------------*/ void densPlot2D50(double psi[][], int gbx, int gby) { int i,j; double ph2; for (i=7; i<57; i++) { for (j=7; j<57; j++) { ph2 = 2.0*(psi[i][j]*psi[i][j])+0.0004*vv[i][j]; if (ph2>0.9) { ph2 = 0.9; } if (ph2>=0.01) { gOff.setColor(Color.getHSBColor( (float)(0.15+0.0004*vv[i][j]), 0.95f, (float)(ph2+0.05))); gOff.fillRect(gbx+i*2,gby+j*2, 2, 2); } } } } /*-------------------------- set initial condition ----------*/ void setInitialCondition() { setPotential(); initSD(10); } /* ----------------------------- set potential -------------- */ void setPotential() { int i,j; double x,y,x0,y0; x0 = 4.0; y0 = 4.0; for (i=1;i