Grand canonical ensemble transition-matrix Monte Carlo

In this example, flat histogram methods are employed for a small macrostate range from 0 to 5 particles. To begin, the system is initialized with the minimum number of particles by setting Metropolis acceptance criteria with favorable conditions for adding particles. The Metropolis criteria are then replaced with the flat histogram criteria. At this point, typical analysis from the previous tutorials are added. In addition, we also add checkpoint files, criteria status, and average energy of a given macrostate. Finally, the simulation is run until the requested number of cycles of the flat histogram algorithm are complete.

A small macrostate range allows the simulation to run quickly with good sampling, and thus it is an ideal starting point to test the simulations. To begin, read the previous SRSW values from file for comparison.

[1]:
import pandas as pd
import numpy as np

srsw_file = "../test/data/stat150.csv"
fh=pd.read_csv(srsw_file)[:6]
fh['lnPI'] -= np.log(sum(np.exp(fh['lnPI'])))   # renormalize
fh['delta_ln_prob']=fh['lnPI'].diff()
fh
[1]:
N energy lnPI energystd lnPIstd delta_ln_prob
0 0 -2.312265e-10 -18.707570 6.689238e-10 0.037092 NaN
1 1 -6.057402e-04 -14.037373 6.709198e-10 0.036838 4.670197
2 2 -3.057422e-02 -10.050312 9.649147e-06 0.036964 3.987061
3 3 -8.992832e-02 -6.458921 1.387472e-04 0.037746 3.591391
4 4 -1.784571e-01 -3.145637 3.315245e-05 0.038097 3.313283
5 5 -2.961920e-01 -0.045677 1.348791e-05 0.038458 3.099960

Run a short flat-histogram simulation to compare with these values

[2]:
script="""
MonteCarlo
Configuration cubic_side_length 8 particle_type0 /feasst/particle/lj.fstprt
Potential Model LennardJones
Potential VisitModel LongRangeCorrections
ThermoParams beta {beta} chemical_potential0 -2.352321
FlatHistogram Macrostate MacrostateNumParticles width 1 min 0 max 5 \
              Bias TransitionMatrix min_sweeps 50
TrialTranslate weight 0.25 tunable_param 1
TrialTransfer weight 1 particle_type 0
Log trials_per_write 1e5 output_file lj.txt
Movie trials_per_write 1e5 output_file lj.xyz
CheckEnergy trials_per_update 1e5 tolerance 1e-8
Tune
CriteriaUpdater trials_per_update 1e5
CriteriaWriter trials_per_write 1e5 output_file lj_fh.txt
Energy output_file lj_en.txt trials_per_update 1 trials_per_write 1e5 multistate true
Checkpoint checkpoint_file checkpoint.fst num_hours 0.0001
Run until complete
""".format(beta=1./1.5)

def run(script):
    with open('script1.txt', 'w') as file: file.write(script)
    import subprocess
    syscode = subprocess.call("../../../build/bin/fst < script1.txt > script1.log", shell=True, executable='/bin/bash')
    with open('script1.log', 'r') as file: print(file.read(), '\n', 'exit:', syscode)
run(script)
# Usage: ./fst < file.txt
FEASST version 0.25.6
MonteCarlo
Configuration cubic_side_length 8 particle_type0 /home/user/feasst/particle/lj.fstprt
Potential Model LennardJones
Potential VisitModel LongRangeCorrections
ThermoParams beta 0.6666666666666666 chemical_potential0 -2.352321
FlatHistogram Bias TransitionMatrix Macrostate MacrostateNumParticles max 5 min 0 min_sweeps 50 width 1
TrialTranslate tunable_param 1 weight 0.25
TrialTransfer particle_type 0 weight 1
Log output_file lj.txt trials_per_write 1e5
Movie output_file lj.xyz trials_per_write 1e5
CheckEnergy tolerance 1e-8 trials_per_update 1e5
Tune
CriteriaUpdater trials_per_update 1e5
CriteriaWriter output_file lj_fh.txt trials_per_write 1e5
Energy multistate true output_file lj_en.txt trials_per_update 1 trials_per_write 1e5
Checkpoint checkpoint_file checkpoint.fst num_hours 0.0001
Run until complete
# initializing random number generator with seed: 1734453337

 exit: 0
[3]:
import pandas as pd

"""Compare the free energies and potential energies with the NIST SRSW
https://www.nist.gov/programs-projects/nist-standard-reference-simulation-website
https://mmlapps.nist.gov/srs/LJ_PURE/eostmmc.htm
"""
fh2=pd.read_csv('lj_fh.txt', comment='#')
en=pd.read_csv('lj_en.txt')
print(fh2[['delta_ln_prob', 'delta_ln_prob_stdev']])
print('energy', en[['average', 'block_stdev']])
for macro in range(1, len(fh2)):
    assert np.abs(fh["delta_ln_prob"][macro] - fh2["delta_ln_prob"][macro]) < 3*fh2["delta_ln_prob_stdev"][macro]
    assert np.abs(fh['energy'][macro] - en['average'][macro]) < 3*(fh["energystd"][macro]**2 + en['block_stdev'][macro]**2)**(1./2.)
   delta_ln_prob  delta_ln_prob_stdev
0            NaN             0.000000
1        4.67327             0.001854
2        3.98474             0.001643
3        3.58667             0.001978
4        3.31051             0.002104
5        3.10046             0.001927
energy         average   block_stdev
0  3.088249e-16  2.760036e-16
1 -6.057400e-04  7.702391e-10
2 -3.084554e-02  2.598340e-04
3 -8.976461e-02  4.648479e-04
4 -1.784771e-01  7.394942e-04
5 -2.983848e-01  1.408980e-03

A number of files should also have been created. If the flat histogram method is sampling perfectly, the simulation performs a random walk along the macrostate. For larger ranges of macrostates, or for more difficult sampling cases, monitoring the macrostate can help you determine what conditions are preventing convergence. For example, a plot of the macrostate as a function of the number of attempts may look like the following:

[4]:
pd.read_csv("lj.txt", header=0).dropna(axis='columns')
[4]:
volume num_particles_of_type0 beta state energy LennardJones LongRangeCorrections BondTwoBody BondThreeBody BondFourBody trial TrialTranslate tunable TrialAdd TrialRemove
0 512 5 0.666667 5 -8.727640e-02 -7.213290e-02 -1.514350e-02 0 0 0 100000 0.924077 2.8307 0.043735 0.043833
1 512 5 0.666667 5 -6.660980e-01 -6.509540e-01 -1.514350e-02 0 0 0 200000 0.938036 2.8307 0.381398 0.409809
2 512 3 0.666667 3 -5.451660e-03 1.998400e-15 -5.451660e-03 0 0 0 300000 0.944572 2.8307 0.523987 0.583411
3 512 5 0.666667 5 -1.531790e-01 -1.380350e-01 -1.514350e-02 0 0 0 400000 0.948249 2.8307 0.595659 0.675027
4 512 2 0.666667 2 -2.422960e-03 4.718450e-16 -2.422960e-03 0 0 0 500000 0.950444 2.8307 0.638244 0.731855
5 512 5 0.666667 5 -3.239200e-02 -1.724850e-02 -1.514350e-02 0 0 0 600000 0.952298 2.8307 0.667888 0.771003
6 512 0 0.666667 0 1.253450e-15 -2.688820e-15 -1.626300e-18 0 0 0 700000 0.953237 2.8307 0.688676 0.798975
7 512 2 0.666667 2 -2.422960e-03 3.219650e-15 -2.422960e-03 0 0 0 800000 0.953807 2.8307 0.704320 0.820270
8 512 0 0.666667 0 -7.241390e-16 -2.220450e-16 -1.626300e-18 0 0 0 900000 0.954429 2.8307 0.716680 0.837076
9 512 3 0.666667 3 -1.120650e-02 -5.754840e-03 -5.451660e-03 0 0 0 1000000 0.954842 2.8307 0.725920 0.850215
10 512 4 0.666667 4 -9.691840e-03 2.886580e-15 -9.691840e-03 0 0 0 1100000 0.955024 2.8307 0.733467 0.860974
11 512 1 0.666667 1 -6.057400e-04 -2.334940e-15 -6.057400e-04 0 0 0 1200000 0.955466 2.8307 0.739520 0.869937
12 512 5 0.666667 5 -8.527380e-02 -7.013030e-02 -1.514350e-02 0 0 0 1300000 0.955814 2.8307 0.745075 0.877721
13 512 2 0.666667 2 -9.756310e-03 -7.333350e-03 -2.422960e-03 0 0 0 1400000 0.956228 2.8307 0.749894 0.884502
14 512 3 0.666667 3 -5.662650e-02 -5.117490e-02 -5.451660e-03 0 0 0 1500000 0.956297 2.8307 0.754505 0.890294
15 512 4 0.666667 4 -3.814060e-02 -2.844880e-02 -9.691840e-03 0 0 0 1600000 0.956405 2.8307 0.758184 0.895346
16 512 2 0.666667 2 -2.422960e-03 7.389920e-16 -2.422960e-03 0 0 0 1700000 0.956616 2.8307 0.761639 0.899923
17 512 4 0.666667 4 -1.019770e-01 -9.228510e-02 -9.691840e-03 0 0 0 1800000 0.956707 2.8307 0.764923 0.904027
18 512 5 0.666667 5 -3.698630e-01 -3.547200e-01 -1.514350e-02 0 0 0 1900000 0.956772 2.8307 0.767428 0.907667
19 512 3 0.666667 3 -5.451660e-03 -6.106230e-16 -5.451660e-03 0 0 0 2000000 0.956990 2.8307 0.769773 0.910929
20 512 1 0.666667 1 -6.057400e-04 2.775560e-16 -6.057400e-04 0 0 0 2100000 0.957077 2.8307 0.771947 0.913836
21 512 2 0.666667 2 -2.422960e-03 9.835880e-16 -2.422960e-03 0 0 0 2200000 0.957141 2.8307 0.774049 0.916467
22 512 2 0.666667 2 -2.422960e-03 1.873500e-15 -2.422960e-03 0 0 0 2300000 0.957214 2.8307 0.775511 0.918833
23 512 3 0.666667 3 -3.700370e-02 -3.155200e-02 -5.451660e-03 0 0 0 2400000 0.957412 2.8307 0.777015 0.921124
24 512 1 0.666667 1 -6.057400e-04 4.551910e-15 -6.057400e-04 0 0 0 2500000 0.957494 2.8307 0.778547 0.923205
25 512 1 0.666667 1 -6.057400e-04 -1.627170e-15 -6.057400e-04 0 0 0 2600000 0.957551 2.8307 0.779935 0.925131
26 512 0 0.666667 0 1.338450e-15 2.600350e-15 -2.493660e-18 0 0 0 2700000 0.957652 2.8307 0.781218 0.926871
27 512 3 0.666667 3 -5.451660e-03 -2.584740e-16 -5.451660e-03 0 0 0 2800000 0.957703 2.8307 0.782535 0.928536
28 512 5 0.666667 5 -2.593190e-02 -1.078840e-02 -1.514350e-02 0 0 0 2900000 0.957743 2.8307 0.783582 0.930116
29 512 2 0.666667 2 -2.422960e-03 1.429410e-15 -2.422960e-03 0 0 0 3000000 0.957779 2.8307 0.784585 0.931510
30 512 4 0.666667 4 -4.505520e-02 -3.536330e-02 -9.691840e-03 0 0 0 3100000 0.957820 2.8307 0.785502 0.932881
31 512 2 0.666667 2 -9.667050e-03 -7.244090e-03 -2.422960e-03 0 0 0 3200000 0.957943 2.8307 0.786385 0.934185
32 512 3 0.666667 3 -1.568780e-02 -1.023620e-02 -5.451660e-03 0 0 0 3300000 0.958010 2.8307 0.787228 0.935371
33 512 0 0.666667 0 -2.267070e-16 -6.938890e-16 -1.626300e-18 0 0 0 3400000 0.958038 2.8307 0.787989 0.936441
34 512 1 0.666667 1 -6.057400e-04 -3.358420e-15 -6.057400e-04 0 0 0 3500000 0.958033 2.8307 0.788530 0.937483
35 512 4 0.666667 4 -4.098840e-02 -3.129660e-02 -9.691840e-03 0 0 0 3600000 0.958076 2.8307 0.789012 0.938482
36 512 3 0.666667 3 -8.501100e-02 -7.955940e-02 -5.451660e-03 0 0 0 3700000 0.958123 2.8307 0.789606 0.939364
37 512 5 0.666667 5 -1.837850e-01 -1.686410e-01 -1.514350e-02 0 0 0 3800000 0.958114 2.8307 0.790065 0.940242
38 512 2 0.666667 2 -2.422960e-03 -1.998400e-15 -2.422960e-03 0 0 0 3900000 0.958114 2.8307 0.790613 0.941117
39 512 3 0.666667 3 -2.511760e-02 -1.966590e-02 -5.451660e-03 0 0 0 4000000 0.958199 2.8307 0.791004 0.941927
40 512 5 0.666667 5 -3.783440e-02 -2.269090e-02 -1.514350e-02 0 0 0 4100000 0.958259 2.8307 0.791509 0.942696
41 512 1 0.666667 1 -6.057400e-04 2.664540e-15 -6.057400e-04 0 0 0 4200000 0.958252 2.8307 0.792036 0.943417
42 512 5 0.666667 5 -3.251350e-01 -3.099920e-01 -1.514350e-02 0 0 0 4300000 0.958328 2.8307 0.792618 0.944160
43 512 3 0.666667 3 -2.186770e-02 -1.641610e-02 -5.451660e-03 0 0 0 4400000 0.958334 2.8307 0.792951 0.944828
44 512 5 0.666667 5 -2.728280e-02 -1.213930e-02 -1.514350e-02 0 0 0 4500000 0.958397 2.8307 0.793361 0.945470
45 512 0 0.666667 0 -6.226570e-16 2.220450e-16 -1.626300e-18 0 0 0 4600000 0.958435 2.8307 0.793840 0.946083
46 512 3 0.666667 3 -5.451660e-03 4.288240e-15 -5.451660e-03 0 0 0 4700000 0.958476 2.8307 0.794319 0.946665
47 512 3 0.666667 3 -2.734430e-02 -2.189260e-02 -5.451660e-03 0 0 0 4800000 0.958556 2.8307 0.794731 0.947198
48 512 0 0.666667 0 1.319370e-15 5.287440e-15 -1.517880e-18 0 0 0 4900000 0.958586 2.8307 0.795058 0.947702
49 512 4 0.666667 4 -9.691840e-03 -4.052310e-15 -9.691840e-03 0 0 0 5000000 0.958608 2.8307 0.795381 0.948203
50 512 4 0.666667 4 -3.882760e-02 -2.913570e-02 -9.691840e-03 0 0 0 5100000 0.958685 2.8307 0.795777 0.948669
51 512 4 0.666667 4 -3.882760e-02 -2.913570e-02 -9.691840e-03 0 0 0 5100000 0.958685 2.8307 0.795777 0.948669

Did this tutorial work as expected? Did you find any inconsistencies or have any comments? Please contact us. Any feedback is appreciated!