/** applet No. t04 * * molecular dynamics 2D * Ar atom - Lennard-Jones 12-6 potential * - multi thread MD2D :: display - asynchronous * * Created by Ikeuchi Mitsuru on July 28 2007. * Copyright (c) 2007-2008 Ikeuchi Mitsuru. All rights reserved. * * ver 0.0.1 2007.07.28 created * ver 0.0.2 2008.06.21 added setGUIPannel() * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class multiThreadMD2D extends Applet implements ItemListener, ActionListener, AdjustmentListener, Runnable { // ------------------------------------ preset field ----------- Thread th = null; // for run()-paint() thread Thread th_md = null; // for MD2D() thread MD2D md = new MD2D(); // for event private Choice ch_tempCont, ch_view; private Button bt_reset, bt_startStop, bt_step; private Scrollbar sc_temp; // for off-paint buffer private Dimension dim; private Image imgOff; private Graphics gOff; // display private int sleepTime = 30; // sleep time (ms) private int dispWidth = 630; private int dispHeight = 460; private int dispMode = 0; private double boxDispWidth = 320.0; private 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(); setGUIPannel(); } public void start() { if (th == null) { th = new Thread(this); th.start(); } if (th_md == null) { th_md = new Thread(md); th_md.start(); } } public void stop() { if (th != null) th = null; if (th_md != null) th_md = null; } // ===================================== GUI control =========== private void setGUIPannel() { setChoice(); setButton(); setScrollbar(); 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(ch_tempCont); pnl.add(sc_temp); pnl.add(ch_view); add(pnl,"North"); } // choice private void setChoice() { 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"); } public void itemStateChanged(ItemEvent ev){ if (ev.getSource() == ch_tempCont) { md.setTempMode( ch_tempCont.getSelectedIndex() ); } else if (ev.getSource() == ch_view) { dispMode = ch_view.getSelectedIndex(); } } // button private void setButton() { bt_reset = new Button("reset"); bt_reset.addActionListener(this); bt_startStop = new Button("start/stop"); bt_startStop.addActionListener(this); bt_step = new Button("step"); bt_step.addActionListener(this); } public void actionPerformed(ActionEvent ev){ if (ev.getSource() == bt_reset){ md.reset(); } else if (ev.getSource() == bt_startStop){ if (md.getStartSW()==0) { md.setStartSW(1); } else { md.setStartSW(0); } } else if (ev.getSource() == bt_step){ if (md.getStartSW()==0) { md.setStepSW(); } } } // scrollbar private void setScrollbar() { sc_temp = new Scrollbar(Scrollbar.HORIZONTAL,300,10,10,610); sc_temp.addAdjustmentListener(this); } public void adjustmentValueChanged(AdjustmentEvent ev){ if (ev.getSource() == sc_temp) { md.setContTemp( 1.0*(double)(sc_temp.getValue()) ); } } // ========================= run() - paint() loop ============== public void run() { while (th != null) { 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 dispScale,sc; if (md==null) return; dispScale = (boxDispWidth/md.getXMax()); 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.blue); gOff.drawString("t="+(int)(md.getTime()*1.0e13+0.5)/10.0+" ps",10,40); gOff.setColor(Color.black); if (md.getStartSW()==0) { gOff.drawString("stopped",dispWidth/6*1+10,40); } else { gOff.drawString("started",dispWidth/6*1+10,40); } gOff.drawString("T cont.",dispWidth/6*3+10,40); gOff.drawString("Tc="+md.getContTemp()+" K",dispWidth/6*4+10,40); gOff.setColor(Color.blue); gOff.drawString("T="+(int)(md.getTemperature()*10+0.5)/10.0+" K",dispWidth/6*4+10,60); gOff.setColor(Color.black); gOff.drawString("view",dispWidth/6*5+10,40); gOff.drawString("Box="+(int)(md.getXMax()*1.0e10+0.5)/10.0+" x "+(int)(md.getYMax()*1.0e10+0.5)/10.0+" nm",10,60); } // ------------------------------------ plot methods ----------- private void ballPlot(int gbx, int gby, double sc) { int i, ix, iy, ballSize, Nmt; Nmt = md.getNmt(); ballSize = (int)(md.getDia()*sc); drawBox(gbx, gby, sc, ballSize); gOff.setColor(Color.blue); for (i=0; i0) { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } } } } // --------------------------- set initial condition ----------- private void setInitialCondition() { int i; double s; t = 0.0; s = xMax/8.0; 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]; } } } // ----------------------------------------- utility ----------- private double temperature() { int i; double s; s = 0.0; for (i=0; i