1 CE 530 Molecular Simulation Lecture 9 Monte Carlo Simulation David A. Kofke Department of Chemical Engineering SUNY Buffalo kofke@eng.buffalo.edu 2 Review We want to apply Monte Carlo simulation to evaluate the configuration integrals arising in statistical mechanics U 1 N! dr N U (r N ) e U ( r N ) p(rN) ZN Importance-sampling Monte Carlo is the only viable approach • unweighted sum of U with configurations generated according to U / ZN distribution e Markov processes can be used to generate configurations according to the desired distribution p(rN). • Given a desired limiting distribution, we construct single-step transition probabilities that yield this distribution for large samples • Construction of transition probabilities is aided by the use of detailed balance: p ip ij p jp ji • The Metropolis recipe is the most commonly used method in molecular simulation for constructing the transition probabilities 3 Monte Carlo Simulation State k MC techniques applied to molecular simulation Almost always involves a Markov process • move to a new configuration from an existing one according to a well-defined transition probability Simulation procedure • generate a new “trial” configuration by making a perturbation to the present configuration • accept the new configuration based on the ratio of the probabilities for the new and old configurations, according to the Metropolis algorithm • if the trial is rejected, the present configuration is taken as the next one in the Markov chain • repeat this many times, accumulating sums for averages e U e new U old State k+1 4 Trial Moves A great variety of trial moves can be made Basic selection of trial moves is dictated by choice of ensemble • almost all MC is performed at constant T no need to ensure trial holds energy fixed • must ensure relevant elements of ensemble are sampled all ensembles have molecule displacement, rotation; atom displacement isobaric ensembles have trials that change the volume grand-canonical ensembles have trials that insert/delete a molecule Significant increase in efficiency of algorithm can be achieved by the introduction of clever trial moves • reptation, crankshaft moves for polymers • multi-molecule movements of associating molecules • many more 5 General Form of Algorithm Monte Carlo Move Entire Simulation New configuration Initialization Select type of trial move Reset block sums “cycle” or “sweep” each type of move has fixed probability of being selected New configuration “block” moves per cycle Move each atom once (on average) Add to block sum cycles per block Compute block average blocks per simulation Compute final results 100’s or 1000’s of cycles Independent “measurement” Perform selected trial move Decide to accept trial configuration, or keep original 6 Monte Carlo in the Molecular Simulation API User's Perspective on the Molecular Simulation API Simulation Space Controller IntegratorMC Meter Phase Species Boundary Configuration Potential Display Device MCMove IntegratorMC (examine code) • performs selection of type of move to conduct at each trial MCMove (examine code) • methods to implement the Monte Carlo trial and make acceptance decision • different subclasses perform different types of moves • usually several of these associated with IntegratorMC 7 Displacement Trial Move 1. Specification Gives new configuration of same volume and number of molecules Basic trial: • 8 Displacement Trial Move 1. Specification Gives new configuration of same volume and number of molecules Basic trial: • displace a randomly selected atom to a point chosen with uniform probability inside a cubic volume of edge 2d centered on the current position of the atom Select an atom at random 9 Displacement Trial Move 1. Specification Gives new configuration of same volume and number of molecules Basic trial: • displace a randomly selected atom to a point chosen with uniform probability inside a cubic volume of edge 2d centered on the current position of the atom 2d Consider a region about it 10 Displacement Trial Move 1. Specification Gives new configuration of same volume and number of molecules Basic trial: • displace a randomly selected atom to a point chosen with uniform probability inside a cubic volume of edge 2d centered on the current position of the atom Consider a region about it 11 Displacement Trial Move 1. Specification Gives new configuration of same volume and number of molecules Basic trial: • displace a randomly selected atom to a point chosen with uniform probability inside a cubic volume of edge 2d centered on the current position of the atom Move atom to point chosen uniformly in region 12 Displacement Trial Move 1. Specification Gives new configuration of same volume and number of molecules Basic trial: • displace a randomly selected atom to a point chosen with uniform probability inside a cubic volume of edge 2d centered on the current position of the atom Consider acceptance of new configuration ? 13 Displacement Trial Move 1. Specification Gives new configuration of same volume and number of molecules Basic trial: • displace a randomly selected atom to a point chosen with uniform probability inside a cubic volume of edge 2d centered on the current position of the atom Limiting probability distribution • canonical ensemble p (r N )dr N 1 U (r N ) N e dr ZN Examine underlying transition probabilities to formulate acceptance criterion ? • for this trial move, probability ratios are the same in other common ensembles, so the algorithm described here pertains to them as well 14 Displacement Trial Move 2. Analysis of Transition Probabilities Detailed specification of trial move and transition probabilities Event [reverse event] Probability [reverse probability] Select molecule k [select molecule k] Move to rnew [move back to rold] Accept move [accept move] Forward-step transition probability 1 1 min(1, ) N v 1/N [1/N] v = (2d)d 1/v [1/v] min(1,) [min(1,1/)] Reverse-step transition probability 1 1 min(1, 1 ) N v is formulated to satisfy detailed balance 15 Displacement Trial Move 3. Analysis of Detailed Balance Forward-step transition probability Reverse-step transition probability 1 1 min(1, ) N v 1 1 min(1, 1 ) N v Detailed balance pi pij = pj pji Limiting p (r N )dr N 1 e U (r N ) dr N ZN distribution 16 Displacement Trial Move 3. Analysis of Detailed Balance Forward-step transition probability Reverse-step transition probability 1 1 min(1, ) N v 1 1 min(1, 1 ) N v Detailed balance pi e U dr N ZN old pij = pj pji U dr N 1 1 e N v min(1, ) ZN new 1 1 1 ) min(1, N v Limiting p (r N )dr N 1 e U (r N ) dr N ZN distribution 17 Displacement Trial Move 3. Analysis of Detailed Balance Forward-step transition probability Reverse-step transition probability 1 1 min(1, ) N v 1 1 min(1, 1 ) N v Detailed balance pi e U dr N ZN old pij = pj pji U dr N 1 1 e N v min(1, ) ZN new e U old e e U 1 1 1 ) min(1, N v new (U new U old ) Acceptance probability Displacement Trial Move 4a. Examination of Java Code public void thisTrial(Phase phase) { double uOld, uNew; if(phase.atomCount==0) {return;} 18 //no atoms to move int i = (int)(rand.nextDouble()*phase.atomCount); //pick a random number from 1 to N Atom a = phase.firstAtom(); for(int j=i; --j>=0; ) {a = a.nextAtom();} //get ith atom in list uOld = phase.potentialEnergy.currentValue(a); //calculate its contribution to the energy a.displaceWithin(stepSize); //move it within a local volume phase.boundary().centralImage(a.coordinate.position()); //apply PBC uNew = phase.potentialEnergy.currentValue(a); //calculate its new contribution to energy if(uNew < uOld) { //accept if energy decreased nAccept++; return; } if(uNew >= Double.MAX_VALUE || //reject if energy is huge or doesn’t pass test Math.exp(-(uNew-uOld)/parentIntegrator.temperature) < rand.nextDouble()) { a.replace(); //...put it back in its original position return; } nAccept++; //if reached here, move is accepted } Have a look at a simple MC simulation applet 19 Displacement Trial Move 4b. Examination of Java Code Atom methods public final void displaceWithin(double d) { workVector.setRandomCube(); displaceBy(d,workVector);} public final void displaceBy(double d, Space.Vector u) { rLast.E(r); translateBy(d,u);} public final void translateBy(double d, Space.Vector u) {r.PEa1Tv1(d,u);} public final void replace() {r.E(rLast);} Space.Vector methods public void setRandomCube() { x = random.nextDouble() - 0.5; y = random.nextDouble() - 0.5; } public void E(Vector u) {x = u.x; y = u.y;} public void PEa1Tv1(double a1, Vector u) { x += a1*u.x; y += a1*u.y;} 20 Displacement Trial Move 5. Tuning Size of step is adjusted to reach a target rate of acceptance of displacement trials • typical target is 50% • for hard potentials target may be lower (rejection is efficient) Large step leads to less acceptance but bigger moves Small step leads to less movement but more acceptance 21 Volume-change Trial Move 1. Specification Gives new configuration of different volume and same N and sN Basic trial: • 22 Volume-change Trial Move 1. Specification Gives new configuration of different volume and same N and sN Basic trial: • increase or decrease the total system volume by some amount within ±dV, scaling all molecule centers-of-mass in proportion to the linear scaling of the volume +dV dV Select a random value for volume change 23 Volume-change Trial Move 1. Specification Gives new configuration of different volume and same N and sN Basic trial: • increase or decrease the total system volume by some amount within ±dV, scaling all molecule centers-of-mass in proportion to the linear scaling of the volume Perturb the total system volume 24 Volume-change Trial Move 1. Specification Gives new configuration of different volume and same N and sN Basic trial: • increase or decrease the total system volume by some amount within ±dV, scaling all molecule centers-of-mass in proportion to the linear scaling of the volume Scale all positions in proportion 25 Volume-change Trial Move 1. Specification Gives new configuration of different volume and same N and sN Basic trial: • increase or decrease the total system volume by some amount within ±dV, scaling all molecule centers-of-mass in proportion to the linear scaling of the volume Consider acceptance of new configuration ? 26 Volume-change Trial Move 1. Specification Gives new configuration of different volume and same N and sN Basic trial: • increase or decrease the total system volume by some amount within ±dV, scaling all molecule centers-of-mass in proportion to the linear scaling of the volume Limiting probability distribution • isothermal-isobaric ensemble Examine underlying transition probabilities to formulate acceptance criterion p (Vs) N ) 1 U (Vs) N ) PV N N e V ds dV Remember how volumescaling was used in derivation of virial formula 27 Volume-change Trial Move 2. Analysis of Transition Probabilities Detailed specification of trial move and transition probabilities Event [reverse event] Select Vnew [select Vold] Accept move [accept move] Probability [reverse probability] 1/(2 dV) [1/(2 dV)] Min(1,) [Min(1,1/)] Forward-step transition probability Reverse-step transition probability 1 min(1, ) 2d V 1 min(1, 1 ) 2d V is formulated to satisfy detailed balance 28 Volume-change Trial Move 3. Analysis of Detailed Balance Forward-step transition probability Reverse-step transition probability 1 min(1, ) 2d V 1 min(1, 1 ) 2d V Detailed balance pi Limiting distribution pij = p (Vs) N ) pj pji 1 U (Vs) N ) PV N N e V ds dV 29 Volume-change Trial Move 3. Analysis of Detailed Balance Forward-step transition probability Reverse-step transition probability 1 min(1, ) 2d V 1 min(1, 1 ) 2d V Detailed balance pi (U old + PV old ) old V e N ) N pij = pj pji (U new + PV new ) new V 1 min(1, ) e 2d V N Limiting distribution p (Vs) N ) ) N 1 min(1, 1 ) 2d V 1 U (Vs) N ) PV N N e V ds dV 30 Volume-change Trial Move 3. Analysis of Detailed Balance Forward-step transition probability Reverse-step transition probability 1 min(1, ) 2d V 1 min(1, 1 ) 2d V Detailed balance pi (U old + PV old ) old V e e ) N pij = pj pji (U new + PV new ) new V 1 min(1, ) e 2d V (U old + PV old ) V ) old N e (U new + PV new ) exp (U + PV ) + N ln(V new / V old ) ) N 1 min(1, 1 ) 2d V V ) new N Acceptance probability 31 Volume-change Trial Move 4. Alternative Formulation Step in ln(V) instead of V • larger steps at larger volumes, smaller steps at smaller volumes 1 U (Vs) N ) PV N +1 N Limiting N p (Vs) e V ds d ln V distribution ) Trial move d lnV V new V old e ) Acceptance probability min(1,) exp (U + PV ) + ( N + 1)ln(V new / V old ) ln V )new ln V )old + d ln V ) 32 Volume-change Trial Move 5. Examination of Java Code public void thisTrial(Phase phase) { double hOld, hNew, vOld, vNew; vOld = phase.volume(); hOld = phase.potentialEnergy.currentValue() //current value of the enthalpy + pressure*vOld*Constants.PV2T; //PV2T is a conversion factor double vScale = (2.*rand.nextDouble()-1.)*stepSize; //choose step size vNew = vOld * Math.exp(vScale); //Step in ln(V) double rScale = Math.exp(vScale/(double)Simulation.D); //evaluate linear scaling phase.inflate(rScale); //scale all center-of-mass coordinates hNew = phase.potentialEnergy.currentValue() //new value of the enthalpy + pressure*vNew*Constants.PV2T; if(hNew >= Double.MAX_VALUE || //decide acceptance Math.exp(-(hNew-hOld)/parentIntegrator.temperature+(phase.moleculeCount+1)*vScale) < rand.nextDouble()) { //reject; put coordinates back to original positions phase.inflate(1.0/rScale); } nAccept++; //accept } Have a look at a simple NPT MC simulation applet 33 Summary Monte Carlo simulation is the application of MC integration to molecular simulation Trial moves made in MC simulation depend on governing ensemble • many trial moves are possible to sample the same ensemble Careful examination of underlying transition matrix and limiting distribution give acceptance probabilities • particle displacement • volume change Next up: molecular dynamics