-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parallel Temper fix and example (#333)
This fixes several problems with random number states that would cause MPI deadlocks. Support for fluctuating particles is temporarily disabled. Test notebook added for running an 1D system with and without tempering. Still needs to be added as a test target. Partial refactoring of ParallelTempering but still plenty of things to improve in terms of safety and clarity. * Add temperature scale to 2D example energy * Resurrect parallel tempering move * Add missing macro guard for MPI * Update mass centers * Add particle update function to Space * Add unit test to Space::updateParticle() * Adds `SpaceFactory::makeWater()` used in the unittest. * Change order of volume/particle update * Rearrange updateParticles test case * Fix hexagonal prism failing test * Add 'temper' to test suite w. statistical test
- Loading branch information
Showing
20 changed files
with
607 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Parallel Tempering\n", | ||
"\n", | ||
"This is a simple example of how to use parallel tempering in Faunus. The example is a particle moving in one dimension\n", | ||
"and exposed to an osciallating potential. The example is taken from the book of Frenkel and Smit for parallel tempering." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline\n", | ||
"import matplotlib\n", | ||
"import matplotlib.cm as cm\n", | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import jinja2, json, yaml, sys\n", | ||
"from math import log, fabs, pi, cos, sin\n", | ||
"from scipy.stats import ks_2samp" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"number_of_replicas = 6\n", | ||
"scale_array = np.geomspace(1, 0.1, number_of_replicas)\n", | ||
"temper = True # run with parallel tempering or not?\n", | ||
"\n", | ||
"# generate input files from the template file; one for each replica\n", | ||
"with open(\"temper.yml\") as template_file:\n", | ||
" template = jinja2.Template(template_file.read())\n", | ||
" for replica, scale in enumerate(scale_array):\n", | ||
" contents = template.render(scale=scale, micro=20000)\n", | ||
" yml = yaml.safe_load(contents) # parse contents as YAML\n", | ||
" if not temper: \n", | ||
" del yml[\"moves\"][1]\n", | ||
" file = open(\"mpi{}.input.json\".format(replica), 'w')\n", | ||
" json.dump(yml, file, indent=4) # save JSON file\n", | ||
" file.close()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Run simulation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%bash\n", | ||
"if [[ -z \"${FAUNUS_EXECUTABLE}\" ]]; then\n", | ||
" mpirun -np 6 faunus -i input.json\n", | ||
"else\n", | ||
" echo \"Seems we're running CTest - use Faunus target from CMake\"\n", | ||
" \"${MPIEXEC}\" -np 6 \"${FAUNUS_EXECUTABLE}\" -i input.json --nobar\n", | ||
"fi" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Plot sampled histogram against expected result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def energy(x):\n", | ||
" s = 1 + sin(2*pi*x)\n", | ||
" if x>=-2.00 and x<=-1.25: return 1*s\n", | ||
" if x>=-1.25 and x<=-0.25: return 2*s\n", | ||
" if x>=-0.25 and x<= 0.75: return 3*s\n", | ||
" if x>= 0.75 and x<= 1.75: return 4*s\n", | ||
" if x>= 1.75 and x<= 2.00: return 5*s\n", | ||
" return 10000000\n", | ||
"\n", | ||
"u_vec = np.vectorize(energy)\n", | ||
"offset = 1e10 # used to offset pmf to match above energy function\n", | ||
"for replica in range(number_of_replicas):\n", | ||
" if (replica==0):\n", | ||
" x = np.loadtxt(\"mpi{}.x.dat.gz\".format(replica), usecols=[1])\n", | ||
" hist, bins = np.histogram(x, bins=150)\n", | ||
" x = (bins[:-1] + bins[1:]) / 2\n", | ||
" pmf = -np.log(hist)\n", | ||
" if (pmf.min() < offset):\n", | ||
" offset = pmf.min();\n", | ||
" plt.plot(x, pmf, label='{}'.format(replica))\n", | ||
" \n", | ||
"plt.legend(loc=0, frameon=False, title='replica')\n", | ||
"plt.plot(x, u_vec(x) + offset, 'k--', alpha=0.6)\n", | ||
"plt.xlabel(r'x')\n", | ||
"plt.ylabel(r'PMF ($k_BT$)');" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Statistical test of output against expected result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"statistic, pvalue = ks_2samp(pmf, u_vec(x)+offset)\n", | ||
"if (pvalue < 0.95):\n", | ||
" sys.exit(1)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Run with: | ||
# | ||
# $ yason.py penalty.yml | mpirun --np 4 --stdin all faunus | ||
# | ||
temperature: 300 | ||
random: { seed: fixed } | ||
mcloop: { macro: 10, micro: {{micro}} } | ||
geometry: {type: cuboid, length: [4,4,4]} | ||
atomlist: | ||
- A: {dp: 0.1} | ||
moleculelist: | ||
- mygroup: {atoms: [A], atomic: true, insdir: [1,1,0]} | ||
insertmolecules: | ||
- mygroup: {N: 1} | ||
energy: | ||
- example2d: {scale: {{scale}}, 2D: false } # this is hard-coded 2d potential, see Frenkel+Smit | ||
moves: | ||
- transrot: {molecule: mygroup, dir: [1,1,0], repeat: 10} | ||
- temper: {format: xyz} | ||
analysis: | ||
- reactioncoordinate: {type: atom, property: x, file: x.dat.gz, index: 0, nstep: 1} | ||
- savestate: {file: state.json} | ||
- sanity: {nstep: 1000} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.