/** applet No. 1044 * * harmonic oscillator - path-integral quantum Monte-Carlo method 1D * - multi thread PIMC :: display - asynchronous * * Created by Ikeuchi Mitsuru on October 22 2006. * Copyright (c) 2006-2007 Ikeuchi Mitsuru. All rights reserved. * * ver 0.0.1 2006.10.22 created * ver 0.0.2 2006.12.04 improved code * ver 0.0.3 2007.05.26 improved code * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class harmonicsMtPIMC1D extends Applet implements ItemListener, ActionListener, AdjustmentListener, Runnable { // ------------------------------------ preset field ----------- Thread th = null; // for run()-paint() thread PathIntegralMC pimc = null; Choice ch_view; Button bt_reset, bt_startStop, bt_step; Scrollbar sc_nPath, sc_kConst; Dimension dim; Image imgOff; Graphics gOff; int ithPath = 0; int dispMode = 2; int sleepTime = 30; int thCount = 0; // ------------------------------ applet main thread ----------- public void init() { resize(630,320); setBackground(Color.white); dim = getSize(); imgOff = createImage(dim.width,dim.height); gOff = imgOff.getGraphics(); ch_view = new Choice(); ch_view.add("density"); ch_view.add("wave"); ch_view.add("path"); ch_view.addItemListener(this); ch_view.select("path"); 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); sc_nPath = new Scrollbar(Scrollbar.HORIZONTAL,100,10,1,210); sc_nPath.addAdjustmentListener(this); sc_kConst = new Scrollbar(Scrollbar.HORIZONTAL,50,10,50,210); sc_kConst.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(bt_step); pnl.add(sc_nPath); pnl.add(sc_kConst); pnl.add(ch_view); add(pnl,"North"); } public void start() { if (pimc == null) { pimc = new PathIntegralMC(); pimc.start(); } if (th == null) { th = new Thread(this); th.start(); } } public void stop() { if (pimc == null) pimc = null; if (th == null) th = null; } // ---------------------------------- event listener ----------- public void itemStateChanged(ItemEvent ev){ if (ev.getSource() == ch_view) { dispMode = ch_view.getSelectedIndex(); } } public void actionPerformed(ActionEvent ev){ if(ev.getSource() == bt_reset){ pimc.reset(); thCount = 0; } else if (ev.getSource() == bt_startStop){ if (pimc.getStartSW()==0) { pimc.setStartSW(1); } else { pimc.setStartSW(0); } } else if (ev.getSource() == bt_step){ if (pimc.getStartSW()==0) pimc.setStepSW(); } } public void adjustmentValueChanged(AdjustmentEvent ev){ if (ev.getSource() == sc_nPath) { pimc.setnMax( sc_nPath.getValue() ); } else if (ev.getSource() == sc_kConst) { pimc.setkConstant( 0.01*(double)(sc_kConst.getValue()) ); } } // ========================= run() - paint() loop ============== public void run() { while (th != null) { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } thCount += 1; offPaint(); repaint(); } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(imgOff,0,0,this); } // ---------------------------------------- offPaint ----------- void offPaint() { int gbx,gby; double sc,dispScale,viewScale; gOff.setColor(Color.white); gOff.fillRect(0,0,dim.width,dim.height); gbx = 30; gby = 60; dispScale = (250.0/(10.0e-9)); viewScale = 1.0; sc = viewScale*dispScale; if (dispMode==0) { densityPlot(gbx,gby, 5000.0); potentialPlot(gbx, gby); } else if (dispMode==1) { wavePlot(gbx,gby,500.0); potentialPlot(gbx, gby); } else if (dispMode==2) { wavePlot(gbx,gby,500.0); potentialPlot(gbx, gby); pathPlot(gbx, gby, ithPath); } else if (dispMode==3) { ; } gOff.setColor(Color.black); gOff.drawString("PIMC="+pimc.getCount()+" ",10,40); if (pimc.getStartSW()==0) { gOff.drawString("stopped",630/6*1+10,40); } else { gOff.drawString("started",630/6*1+10,40); } gOff.drawString("Npath="+pimc.getnMax()+"",630/6*3+10,40); gOff.drawString("k="+pimc.getkConstant()+"",630/6*4+10,40); gOff.drawString("V(x) = k x^2",630/6*4+10,60); gOff.drawString("view",630/6*5+10,40); gOff.drawString("norm ="+(float)pimc.getNorm()+"",400,100); gOff.drawString(" ="+(float)pimc.getEnergy()+"",400,120); gOff.drawString("virial energy ="+(float)pimc.getMeanVirialEnergy()+"",400,140); gOff.drawString("loop count",400,200); gOff.drawString("thread disp ="+thCount+"",400,220); gOff.drawString("thread pimc ="+pimc.getCount()+"",400,240); gOff.drawString("disp sleep = 30 ms",400,260); } // ------------------------------------ plot methods ----------- void densityPlot(int gbx, int gby, double mag) { int ix, iy, dmax; gOff.setColor(Color.getHSBColor(0.66f,0.3f,0.9f )); dmax = pimc.getdMax(); for(ix=0; ixxMax ); dEnergy = ( (xx[jp]-xtrial)*(xx[jp]-xtrial)+(xtrial-xx[jm])*(xtrial-xx[jm]) -(xx[jp]-xx[j])*(xx[jp]-xx[j]) -(xx[j]-xx[jm])*(xx[j]-xx[jm]) )/dt2 + potential(xtrial) - potential(xx[j]); if ( Math.exp(-dEnergy)>Math.random() ) { xx[j] = xtrial; return j; } return -1; } private double potential(double x) { return ( kConstant*x*x ); } // set density private void setDensity() { int i,j,ix,nm; double x,ei; nm = nMax*mMax; ei = 1.0/(count+1.0); if (count>10000) ei=0.0001; for (ix=0; ix10000) ei=0.0001; meanVirialEnergy = (1.0-ei)*meanVirialEnergy + ei*virialEnergy(); } private double virialEnergy() { int i; double s; s = 0.0; for (i=0; i,dR) * * Integrate(,dR0) * = Integrate(,dR0...dR_M-1 ) * * time evolution in short time: * Integrate(,dR0) * ~ normalized exp(-dtau V(R)) exp(-(R-R')^2/2dtau) * * * Integrate(,dR0) * ~ normalized Integrate( exp(-dtau*sum(Pm,{m,0,M-1}))) * ( where Pm = {(R_m+1-Rm)/dtau }^2 + V(Rm) ) * * * method * * path - {x[i] ,i=0,..,j,..,M-1} * - sampling path (Metropolis Algorithm) * * (1) trial move at j * select j at random: * trial move x[j] + xtrial, xtrial <-- a*random[-1,1] * * (2) energy change : dEnergy * dEnergy = { (x[j+1]-xtrial)^2 + (xtrial-x[j-1])^2 * - (x[j+1]-x[j])^2 - (x[j]-x[j-1])^2 }/(2dtau^2) * + {V(xtrial)-V(x[j]) } * * (3) move or not * if exp(-dEnergy)>random[0,1] * then move x[j] <-- xtrial * * goto (1) * */