/* applet No. 995 * * adatom on metal * - Metropolis sampling - Monte-Carlo simulation * * Created by Ikeuchi Mitsuru on August 12 2006. * Copyright (c) 2006 Ikeuchi Mitsuru. All rights reserved. * * ver 0.0.1 2006.08.12 created * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class adatomMetropolisMCS extends Applet implements ItemListener, ActionListener, AdjustmentListener, Runnable { /* -------------------------------------- set global ------ */ Choice ch_view; Button bt_reset, bt_startStop; Scrollbar sc_nn, sc_qqn, sc_kT; Thread th = null; Dimension dim; Image imgOff; Graphics gOff; int dispWidth = 630; int dispHeight = 420; int sleepTime = 50; double t = 0.0; double dt = 1.0; int resetSW = 0; int started = 1; int dispMode = 0; double qqAdsorption = 0.0; double qqNeighbor = -0.3; double kT = 0.1; int NNx = 32; int NNy = 32; int NNa = 300; int lattice[][] = new int[NNx][NNy]; int adatom[][] = new int[600][2]; /* ---------------------------------- applet control -------- */ public void init() { resize(dispWidth,dispHeight); setBackground(Color.white); dim = getSize(); imgOff = createImage(dim.width,dim.height); gOff = imgOff.getGraphics(); ch_view = new Choice(); ch_view.addItem("0 yellow"); ch_view.addItem("1 magenta"); ch_view.addItem("2 green"); ch_view.addItemListener(this); ch_view.select("1 magenta"); bt_reset= new Button("reset"); bt_reset.addActionListener(this); bt_startStop= new Button("start/stop"); bt_startStop.addActionListener(this); sc_nn= new Scrollbar(Scrollbar.HORIZONTAL,300,10,1,610); sc_nn.addAdjustmentListener(this); sc_qqn= new Scrollbar(Scrollbar.HORIZONTAL,300,10,0,1010); sc_qqn.addAdjustmentListener(this); sc_kT= new Scrollbar(Scrollbar.HORIZONTAL,100,10,1,510); sc_kT.addAdjustmentListener(this); setLayout(new BorderLayout()); Panel pnl = new Panel(); pnl.setLayout(new GridLayout(1,6,5,0)); //pnl.add(new Label(" ")); pnl.add(bt_reset); pnl.add(bt_startStop); pnl.add(sc_nn); pnl.add(new Label(" ")); pnl.add(sc_qqn); pnl.add(sc_kT); 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() == ch_view) { dispMode = ch_view.getSelectedIndex(); } } public void actionPerformed(ActionEvent ev){ if(ev.getSource() == bt_reset){ resetSW = 1; } else if (ev.getSource() == bt_startStop){ if (started==0) { started = 1; } else { started = 0; } } } public void adjustmentValueChanged(AdjustmentEvent ev){ if (ev.getSource() == sc_nn) { NNa = sc_nn.getValue(); resetSW = 1; } else if (ev.getSource() == sc_qqn) { qqNeighbor = -0.001*(double)(sc_qqn.getValue()); } else if (ev.getSource() == sc_kT) { kT = 0.001*(double)(sc_kT.getValue()); } } 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() { gOff.setColor(Color.white); gOff.fillRect(0,0,dim.width,dim.height); if (dispMode==0) { dispLattice(30, 80, 10); } else if (dispMode==1) { ; } else if (dispMode==2) { ; } gOff.setColor(Color.black); gOff.drawString("sample="+(int)(t+0.5)+" ",10,40); if (started==0) { gOff.drawString("stopped",dispWidth/6*1+10,40); } else { gOff.drawString("started",dispWidth/6*1+10,40); } gOff.drawString("N="+NNa+"",dispWidth/6*2+10,40); gOff.drawString("qq="+qqNeighbor+"",dispWidth/6*4+10,40); gOff.drawString("kT="+(int)(kT*1000+0.5)/1000.0+"",dispWidth/6*5+10,40); gOff.setColor(Color.black); gOff.drawString("E = Sum(qq,{if neighbor})",30,60); gOff.setColor(Color.blue); gOff.drawString("Etotal="+(float)totalEnergy()+"",400,60); } /*-------------------------- dispLattice --------- */ void dispLattice(int gx, int gy, int span) { int i,j,ir; double col; ir = span-2; for(i=0;i=0) { col = 0.1; } else { col = 0.66; } gOff.setColor(Color.getHSBColor((float)col,0.4f,0.8f)); gOff.fillOval(gx+span*i-ir/2,gy+span*j-ir/2, ir, ir); } } } /*-------------------------- statistics ------------- */ double totalEnergy() { int ia; double s; s = 0.0; for(ia=0;ia=0) n += 1; } } return(n); } /*-------------------------- set initial condition --------- */ void setInitialCondition() { int ia; t = 0.0; initLattice(); for(ia=0;ia= 0); adatom[ia][0] = i; adatom[ia][1] = j; lattice[i][j] = ia; } /* ----------------------------- timeEvolution -------------- */ void timeEvolution() { if (resetSW==1) { setInitialCondition(); resetSW = 0; started = 1; } if (started==1) { timeStep(); } } void timeStep() { int ia; t = t + dt; for(ia=0;ia=0) return; lattice[ia][ja] = -1; e0 = energy(ia, ja); e1 = energy(i, j); de = e1 - e0; if ( Math.exp(-de/kT)>Math.random() ) { adatom[iatom][0] = i; adatom[iatom][1] = j; lattice[i][j] = iatom; } else { adatom[iatom][0] = ia; adatom[iatom][1] = ja; lattice[ia][ja] = iatom; } } double energy(int i, int j) { int ip,im,jp,jm; double qq; ip = (i+1)%NNx; im = (i+NNx-1)%NNx; jp = (j+1)%NNy; jm = (j+NNy-1)%NNy; qq = qqAdsorption; if (lattice[ip][j]>=0) qq += qqNeighbor; if (lattice[im][j]>=0) qq += qqNeighbor; if (lattice[i][jp]>=0) qq += qqNeighbor; if (lattice[i][jm]>=0) qq += qqNeighbor; return( qq ); } /* ----------------------------- end of applet -------------- */ }