Skip to content

Commit

Permalink
MAINT: tweak the frustrated Ising example notebook, show multiprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
ev-br committed Jul 8, 2021
1 parent 0cbd1fc commit d569a28
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ jobs:
# also check that the 2D triangular Ising example runs
pip install jupyter
ipython -c"%run examples/frustrated_2d_ising.ipynb"
ipython -c"%run frustrated_2d_ising.ipynb"
99 changes: 60 additions & 39 deletions examples/frustrated_2d_ising.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "488bd36c",
"id": "b92b380b",
"metadata": {},
"source": [
"# Simulate a frustrated 2D Ising model on the triangular lattice. \n",
Expand All @@ -19,7 +19,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "601e580b",
"id": "0bee3da3",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -29,7 +29,7 @@
{
"cell_type": "code",
"execution_count": 11,
"id": "23f5fd9b",
"id": "da0196e3",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -178,7 +178,7 @@
{
"cell_type": "code",
"execution_count": 9,
"id": "a6cf11a6",
"id": "5628ca52",
"metadata": {},
"outputs": [
{
Expand All @@ -197,53 +197,74 @@
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "788c0558",
"cell_type": "markdown",
"id": "2a2d9104",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0. 1. 0. 1. 1. -0.1 0. 0. 0. 0. 0. 0. 1. 0. 0. -0.1]\n",
" [ 1. 0. 1. 0. 0. 1. -0.1 0. 0. 0. 0. 0. -0.1 1. 0. 0. ]\n",
" [ 0. 1. 0. 1. 0. 0. 1. -0.1 0. 0. 0. 0. 0. -0.1 1. 0. ]\n",
" [ 1. 0. 1. 0. -0.1 0. 0. 1. 0. 0. 0. 0. 0. 0. -0.1 1. ]\n",
" [ 1. 0. 0. -0.1 0. 1. 0. 1. 1. -0.1 0. 0. 0. 0. 0. 0. ]\n",
" [-0.1 1. 0. 0. 1. 0. 1. 0. 0. 1. -0.1 0. 0. 0. 0. 0. ]\n",
" [ 0. -0.1 1. 0. 0. 1. 0. 1. 0. 0. 1. -0.1 0. 0. 0. 0. ]\n",
" [ 0. 0. -0.1 1. 1. 0. 1. 0. -0.1 0. 0. 1. 0. 0. 0. 0. ]\n",
" [ 0. 0. 0. 0. 1. 0. 0. -0.1 0. 1. 0. 1. 1. -0.1 0. 0. ]\n",
" [ 0. 0. 0. 0. -0.1 1. 0. 0. 1. 0. 1. 0. 0. 1. -0.1 0. ]\n",
" [ 0. 0. 0. 0. 0. -0.1 1. 0. 0. 1. 0. 1. 0. 0. 1. -0.1]\n",
" [ 0. 0. 0. 0. 0. 0. -0.1 1. 1. 0. 1. 0. -0.1 0. 0. 1. ]\n",
" [ 1. -0.1 0. 0. 0. 0. 0. 0. 1. 0. 0. -0.1 0. 1. 0. 1. ]\n",
" [ 0. 1. -0.1 0. 0. 0. 0. 0. -0.1 1. 0. 0. 1. 0. 1. 0. ]\n",
" [ 0. 0. 1. -0.1 0. 0. 0. 0. 0. -0.1 1. 0. 0. 1. 0. 1. ]\n",
" [-0.1 0. 0. 1. 0. 0. 0. 0. 0. 0. -0.1 1. 1. 0. 1. 0. ]]\n"
]
}
],
"source": [
"L=4\n",
"Js = np.zeros((L**2, L**2), dtype=float)\n",
"## Use multiprocessing for running several simulations in parallel\n",
"\n",
"get_J(Js, 1.0, -0.1, L, L)\n",
"(this can be streamlined, of course)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "181e48c5",
"metadata": {},
"outputs": [],
"source": [
"from multiprocessing import Pool"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "94b126b6",
"metadata": {},
"outputs": [],
"source": [
"param = [\n",
" # L, T, J, Jd, num_sweeps, seed\n",
" (4, 1.0, 1, -0.1, 100, 0),\n",
" (4, 1.5, 1, -0.1, 100, 0),\n",
" (4, 2.0, 1, -0.1, 100, 0),\n",
"]\n",
"\n",
"with np.printoptions(linewidth=120, precision=2):\n",
" print(Js)"
"with Pool(processes=2) as pool:\n",
" res = pool.starmap(simulate, param)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "57db2306",
"execution_count": 42,
"id": "0d5a40b7",
"metadata": {},
"outputs": [],
"source": [
"raise ValueError"
"res_sequential = [simulate(*par) for par in param ]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "990cbb4f",
"metadata": {},
"outputs": [],
"source": [
"from numpy.testing import assert_allclose\n",
"\n",
"assert_allclose(res,\n",
" res_sequential,\n",
" atol=1e-14)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fed870b8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit d569a28

Please sign in to comment.