/* fixed-node - diffusion Monte-Carlo method 1D */ /* coded by Ikeuchi Mitsuru */ /* ver 0.0.1 2005.06.14 */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class fixedNodeDMC1D extends Applet implements ActionListener, AdjustmentListener, Runnable { /* -------------------------------------- set global ------ */ Button bt_reset, bt_start, bt_stop; Scrollbar sc_kConstant, sc_deltaTau; Thread th = null; Dimension dim; Image imgOff; Graphics gOff; Random rand = new Random(); int sleepTime = 50; int resetSW = 0; int started = 1; double kConstant = 0.5; double xnode = 0.0; double deltaTau = 0.01; double sqrtdt = Math.sqrt(deltaTau); double hbar = 1.0; double t = 0.0; double dt = deltaTau; int NNr = 2000; int repStatus[] = new int[NNr]; double replica[][] = new double[NNr][3]; double eeRef0 = 0.0; double eeRef1 = eeRef0; double eeRef = eeRef0; int NN0 = 500; int NN1 = NN0; int NN00 = NN0; int NNb = 100; int box[] = new int[NNb]; double meanBox[] = new double[NNb]; double psi[] = new double[NNb]; double ffr[] = new double[NNb]; /* ----------------------------- applet control ------ */ public void init() { resize(630,320); setBackground(Color.white); dim = getSize(); imgOff = createImage(dim.width,dim.height); gOff = imgOff.getGraphics(); bt_reset= new Button("reset"); bt_reset.addActionListener(this); bt_start= new Button("start"); bt_start.addActionListener(this); bt_stop = new Button("stop"); bt_stop.addActionListener(this); sc_kConstant= new Scrollbar(Scrollbar.HORIZONTAL,50,10,0,210); sc_kConstant.addAdjustmentListener(this); sc_deltaTau= new Scrollbar(Scrollbar.HORIZONTAL,10,10,1,110); sc_deltaTau.addAdjustmentListener(this); setLayout(new BorderLayout()); Panel pnl = new Panel(); pnl.setLayout(new GridLayout(1,6,5,0)); pnl.add(bt_reset); pnl.add(bt_start); pnl.add(bt_stop); pnl.add(new Label(" ")); pnl.add(new Label(" ")); pnl.add(sc_kConstant); add(pnl,"North"); setInitialCondition(); repaint(); } public void start() { if (th == null) { th = new Thread(this); th.start(); } } public void stop() { if (th != null) { th.stop(); th = null; } } public void actionPerformed(ActionEvent ev){ if(ev.getSource() == bt_reset){ resetSW = 1; started = 0; } else if(ev.getSource() == bt_start){ started = 1; } else if(ev.getSource() == bt_stop){ started = 0; } } public void adjustmentValueChanged(AdjustmentEvent ev){ if (ev.getSource() == sc_kConstant) { kConstant = 0.01*(double)(sc_kConstant.getValue()); } else if (ev.getSource() == sc_deltaTau) { deltaTau = 0.001*(double)(sc_deltaTau.getValue()); dt = deltaTau; } } 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() { int gx,gy,gb; gx = 30; gy = 60; gOff.setColor(Color.white); gOff.fillRect(0,0,dim.width,dim.height); plotReplica(gx,gy,10); plotMeanBox(gx,gy,2.0); drawPotential(gx,gy,10.0); plotPsi(gx,gy, 500.0); gOff.setColor(Color.black); gOff.drawString("t="+(int)(t*100+0.5)/100.0+" ",630/6*0+10,40); gOff.drawString("k="+(int)(kConstant*100.0+0.5)/100.0+"",630/6*5+10,40); gb = 460; gOff.drawString("Nmax="+NNr+"",gb,100); gOff.drawString("d tau="+(float)(deltaTau)+"",gb,120); gOff.drawString("V(x)=k*x^2",gb,140); gOff.drawString("ER="+(float)(eeRef0)+"",gb,200); gOff.drawString("="+(float)(eeRef)+"",gb,220); gOff.drawString("N="+NN0+"",gb,240); gOff.drawString("E0 = 0.5au at k=0.5",gb,280); gOff.drawString("E1 = 1.5au at k=0.5",gb,300); } /* ----------------------------- plot methods ----------------- */ void drawPotential(int gx, int gy, double mag) { int ix; double x,sc,p1,p2; gOff.setColor(Color.red); sc = 5.0/200.0; for (ix=0; ix<400-1; ix++) { p1 = mag*potential(sc*(ix-200)); p2 = mag*potential(sc*(ix+1-200)); gOff.drawLine(gx+ix,gy+200-(int)(p1), gx+ix+1, gy+200-(int)(p2)); } } void plotReplica(int gx, int gy, int iy) { int i,ix; for (i=0; i0) { gOff.fillRect(gx+i*a-a/2,gy+200-iy,a,iy); } } } void plotMeanBox(int gx, int gy, double mag) { int i,iy,a; a = 400/NNb; gOff.setColor(Color.green); for (i=0; i0.5) { gOff.fillRect(gx+i*a-a/2,gy+200-iy,a,iy); } } } void plotPsi(int gx, int gy, double mag) { int ix,a; double x,sc,p1,p2; a = 400/NNb; gOff.setColor(Color.orange); for (ix=0; ixNNb-1) ix=NNb-1; return( ix ); } void setPsi() { int i,inode; double s,s2,sgn; inode = (int)(50.0+xnode*10.0+0.5); s2 = 0; for (i=0; i0.001) { ffr[i] = 0.0; } else { ffr[i] = (psi[i+1]-psi[i-1])/(2.0*h)/psi[i]; } } } /* ----------------------------- timeEvolution -------------- */ void timeEvolution() { int i; if (resetSW==1) { setInitialCondition(); resetSW = 0; } if (started==1) { timeStep(); } } void timeStep() { int i,ib; double xi,xid,q; t = t + dt; for (i=0; i3) mn = 3; if (mn==0) { repStatus[i] = 0; } for(im=1; im=0) { repStatus[ip] = 2; replica[ip][0] = replica[i][0]; } } int seekSpace(int istart) { int i,ip; ip = -1; for (i=1; i