/** applet No. 011 * * adsorption - molecular dynamics 2D * - Ar Lennard-Jones potential * * Created by Ikeuchi Mitsuru on July 09 2002. * Copyright (c) 2002-2007 Ikeuchi Mitsuru. All rights reserved. * * ver 0.0.1 2002.07.09 created * ver 0.0.2 2003.08.08 flicker noise reduced * ver 0.0.3 2004.02.04 improved pannel layout * ver 0.0.4 2004.06.05 improved velret code * ver 0.0.5 2007.07.21 display larger size and improved code * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class adsorption extends Applet implements ItemListener, ActionListener, AdjustmentListener, Runnable { // ------------------------------------ preset field ----------- Thread th = null; // for run()-paint() thread // for event Choice ch_tempCont, ch_view; Button bt_reset, bt_startStop; Scrollbar sc_surfaceEnergy, sc_temp; // for off-paint buffer Dimension dim; Image imgOff; Graphics gOff; // MD2D double t = 0.0; // time (s) double dt = 10.0*1.0e-15; // time division (s) double xMax = 6.0e-9; // x-Box size (m) double yMax = 6.0e-9; // y-Box size (m) double EE = 1.602e-19; double sgm = 3.40e-10; // L-J sigma for Ar (m) double eps = 1.67e-21; // L-J epsilon for Ar (J) double mas = 39.95*1.67e-27; // mass of Ar (kg) int tempMode = 2; // 0-adiabatic 1-scaling 2-wall cont double contTemp = 300.0; // control temperature (K) double plateSurfaceEnergy = 0.2*EE; double plateZone = 1.0*sgm; int resetSW = 0; // 1-reset int startSW = 1; // 0-stop 1-start int Nmt = 36; // number of molecules int NN = Nmt; double xx[] = new double[NN]; // i-th x-position double yy[] = new double[NN]; // i-th y-position double vx[] = new double[NN]; // i-th x-velocity double vy[] = new double[NN]; // i-th y-velocity double ffx[] = new double[NN]; // i-th x-force double ffy[] = new double[NN]; // i-th y-force // display int sleepTime = 30; // sleep time (ms) int dispWidth = 630; int dispHeight = 460; int dispMode = 0; double boxDispWidth = 320.0; double dispScale = (boxDispWidth/xMax); double viewScale = 1.0; // ------------------------------ applet main thread ----------- public void init() { resize(dispWidth,dispHeight); setBackground(Color.white); dim = getSize(); imgOff = createImage(dim.width,dim.height); gOff = imgOff.getGraphics(); ch_tempCont = new Choice(); ch_tempCont.add("adiabatic"); ch_tempCont.add("T scaling"); ch_tempCont.add("wall cont."); ch_tempCont.addItemListener(this); ch_tempCont.select("wall cont."); ch_view = new Choice(); ch_view.add("ball"); ch_view.add("line"); ch_view.add("velocity"); ch_view.addItemListener(this); ch_view.select("ball"); bt_reset = new Button("reset"); bt_reset.addActionListener(this); bt_startStop = new Button("start/stop"); bt_startStop.addActionListener(this); sc_surfaceEnergy = new Scrollbar(Scrollbar.HORIZONTAL,200,10,0,810); sc_surfaceEnergy.addAdjustmentListener(this); sc_temp = new Scrollbar(Scrollbar.HORIZONTAL,300,10,10,610); sc_temp.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(new Label(" ")); pnl.add(sc_surfaceEnergy); pnl.add(sc_temp); pnl.add(ch_view); add(pnl,"North"); setInitialCondition(); } public void start() { if (th == null) { th = new Thread(this); th.start(); } } public void stop() { if (th != null) th = null; } // ---------------------------------- event listener ----------- public void itemStateChanged(ItemEvent ev){ if (ev.getSource() == ch_tempCont) { tempMode = ch_tempCont.getSelectedIndex(); } else 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 (startSW==0) { startSW = 1; } else { startSW = 0; } } } public void adjustmentValueChanged(AdjustmentEvent ev){ if (ev.getSource() == sc_surfaceEnergy) { plateSurfaceEnergy = EE*0.001*(double)(sc_surfaceEnergy.getValue()); } else if (ev.getSource() == sc_temp) { contTemp = 1.0*(double)(sc_temp.getValue()); } } // ========================= run() - paint() loop ============== public void run() { while (th != null) { timeEvolution(); offPaint(); repaint(); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(imgOff,0,0,this); } // --------------------------------------- off-paint ----------- private void offPaint() { int gbx,gby; double sc; gOff.setColor(Color.white); gOff.fillRect(0,0,dim.width,dim.height); gbx = 30; gby = 90; sc = viewScale*dispScale; if (dispMode==0) { ballPlot(gbx,gby,sc); } else if (dispMode==1) { linePlot(gbx,gby,sc); } else if (dispMode==2) { velocityPlot(gbx,gby,sc, 0.05); } else if (dispMode==3) { ; } gOff.setColor(Color.black); gOff.drawString("molecule",10,40); if (startSW==0) { gOff.drawString("stopped",dispWidth/6*1+10,40); } else { gOff.drawString("started",dispWidth/6*1+10,40); } gOff.setColor(Color.black); gOff.drawString("adsorption E",dispWidth/6*3+10,40); gOff.setColor(Color.blue); gOff.drawString("="+(float)(plateSurfaceEnergy/EE)+" eV",dispWidth/6*3+10,60); gOff.setColor(Color.black); gOff.drawString("Tc="+contTemp+" K",dispWidth/6*4+10,40); gOff.setColor(Color.blue); gOff.drawString("T="+(int)(temperature()*10+0.5)/10.0+" K",dispWidth/6*4+10,60); gOff.setColor(Color.black); gOff.drawString("view",dispWidth/6*5+10,40); gOff.setColor(Color.blue); gOff.drawString("t="+(int)(t*1.0e13+0.5)/10.0+" ps",10,60); gOff.setColor(Color.black); gOff.drawString("Box="+(int)(xMax*1.0e10+0.5)/10.0+" x "+(int)(yMax*1.0e10+0.5)/10.0+" nm",120,60); } // ------------------------------------ plot methods ----------- private void ballPlot(int gbx, int gby, double sc) { int i, ix, iy, ir; drawBox(gbx, gby, sc); for (i=0; i1.2) { rr=1.2; }; } for (i=0; i xMax) { xx[i] = xMax; vx[i] = -rr*vx[i]; vy[i] = rr*vy[i]; } if (yy[i] < 0.0) { yy[i] = 0.0; vx[i] = rr*vx[i]; vy[i] = -rr*vy[i]; } if (yy[i] > yMax) { yy[i] = yMax; vx[i] = rr*vx[i]; vy[i] = -rr*vy[i]; } } } // --------------------------------------- utilities ----------- private double temperature() { int i; double s; s = 0.0; for (i=0; i