/** applet No. 004 * * load test - tungsten (Morse potential) - molecular dynamics 2D * * Created by Ikeuchi Mitsuru on June 18 2002. * Copyright (c) 2002-2007 Ikeuchi Mitsuru. All rights reserved. * * ver 0.0.1 2002.06.18 created * ver 0.0.2 2002.07.03 version up * ver 0.0.3 2007.04.20 improved code * ver 0.0.4 2007.05.18 improved code * ver 0.0.5 2007.06.26 improved code * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class loadTest 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_load, sc_temp; // for off-paint buffer Dimension dim; Image imgOff; Graphics gOff; // MD2D double t = 0.0; // time (s) double dt = 5.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 mas = 183.85*1.661e-27; // mass of W (kg) double dMorse = 0.9906*1.6e-19; // Morse potential D for W (J) double aMorse = 1.4116*1.0e10; // Morse potential A for W double r0Morse = 3.032*1.0e-10; // Morse potential r0 for W (m) int tempMode = 2; // 0-adiabatic 1-scaling 2-wall cont double contTemp = 300.0; // control temperature (K) double atomLoad = 1.0e-9; int resetSW = 0; int startSW = 1; int Nmt = 12*16; // number of molecules int NN = Nmt; int kind[] = new int[NN]; // i-th kind 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 = 420; 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_load = new Scrollbar(Scrollbar.HORIZONTAL,100,10,0,210); sc_load.addAdjustmentListener(this); sc_temp = new Scrollbar(Scrollbar.HORIZONTAL,300,10,100,1010); 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(sc_load); pnl.add(ch_tempCont); 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_load) { atomLoad = 0.01*1.0e-9*(double)(sc_load.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 ----------- 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.blue); gOff.drawString("t="+(int)(t*1.0e13+0.5)/10.0+" ps",10,40); gOff.setColor(Color.black); if (startSW==0) { gOff.drawString("stopped",dispWidth/6*1+10,40); } else { gOff.drawString("started",dispWidth/6*1+10,40); } gOff.drawString("load ="+(int)(atomLoad*1.0e11)/100.0+" nN",dispWidth/6*2+10,40); gOff.drawString("T cont.",dispWidth/6*3+10,40); 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.drawString("Box="+(int)(xMax*1.0e10+0.5)/10.0+" x "+(int)(yMax*1.0e10+0.5)/10.0+" nm",10,60); } // ------------------------------------ plot methods ----------- void ballPlot(int gbx, int gby, double sc) { int i, ix, iy, ballSize; ballSize = (int)(0.9*r0Morse*sc); drawBox(gbx, gby, sc, ballSize); for (i=0; i0.66) col = 0.66; gOff.setColor(Color.getHSBColor((float)col,0.8f,0.8f )); gOff.drawLine(ix,iy, jx,jy); } } gOff.setColor(Color.blue); if (kind[i]==2) { gOff.setColor(Color.black); } gOff.fillOval(ix-ballSize/2,iy-ballSize/2, ballSize, ballSize); } } double rsquare(int i, int j) { double x,y; x = xx[i] - xx[j]; y = yy[i] - yy[j]; return x*x+y*y; } void velocityPlot(int gbx, int gby, double sc, double mag) { int i, ix, iy, ix2, iy2, ballSize; ballSize = (int)(0.9*r0Morse*sc); drawBox(gbx, gby, sc, ballSize); gOff.setColor(Color.blue); for (i=0; i=12*15) { kind[i] = 2; } } vAjustment(); } // ---------------------------------- time evolution ----------- void timeEvolution() { int i; if (resetSW==1) { setInitialCondition(); resetSW = 0; } if (startSW==1) { if (tempMode==1) { vAjustment(); } for (i=0; i<20; i++) { timeStep(); } } } void timeStep() { int i; double dtv2, a; dtv2 = dt/2.0; t = t + dt; 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 ----------- double temperature() { int i; double s; s = 0.0; for (i=0; i