Skip to content

Commit

Permalink
Fix py27 integer division bug
Browse files Browse the repository at this point in the history
  • Loading branch information
andysim committed Oct 3, 2017
1 parent 3302524 commit 6882e01
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
72 changes: 50 additions & 22 deletions Tutorials/12_MD/12a_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib notebook\n",
Expand Down Expand Up @@ -39,7 +41,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def showpotential(ax, sigma=0.25, epsilon=0.05):\n",
Expand Down Expand Up @@ -72,7 +76,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Evaluate this cell, then play with the sliders to see the effect of the well\n",
Expand All @@ -94,7 +100,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"rval = 0.5\n",
Expand Down Expand Up @@ -143,12 +151,14 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"atoms_per_dim = 5\n",
"boxlength = 2*atoms_per_dim # atoms spaced by 2nm in each dimension\n",
"cutoff = boxlength/2 - 1e-8 # Longest cutoff possible\n",
"boxlength = 2.0*atoms_per_dim # atoms spaced by 2nm in each dimension\n",
"cutoff = boxlength/2.0 - 1e-8 # Longest cutoff possible\n",
"epsilon = 0.25 # kJ/mol\n",
"sigma = 0.2 # nm\n",
"\n",
Expand All @@ -170,7 +180,7 @@
"coords = np.array([np.repeat(spacing, atoms_per_dim), np.tile(spacing, atoms_per_dim)]).T\n",
"# Move the atoms very slightly away from their regular grid positions\n",
"np.random.seed(0)\n",
"coords += (np.random.rand(natoms,2)-0.5)/50\n",
"coords += (np.random.rand(natoms,2)-0.5)*0.02\n",
"\n",
"\n",
"def apply_periodicity(dR):\n",
Expand Down Expand Up @@ -288,7 +298,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"Rgas = 0.0083144621 # Molar gas constant in kJ/(mol K)\n",
Expand Down Expand Up @@ -322,7 +334,7 @@
" self.boxinv = 1.0/self.boxlength\n",
" self.minv = 1.0/self.mass\n",
" \n",
" if(cutoff >= boxlength/2):\n",
" if(cutoff >= boxlength/2.0):\n",
" raise ValidationError(\"The cutoff must be less than boxlength/2\")\n",
"\n",
" np.random.seed(0)\n",
Expand All @@ -348,8 +360,8 @@
" self.kline, = self.enerplot.plot(0, 0, 'bo', ms=0.5, label='kinetic')\n",
" self.vline, = self.enerplot.plot(0, 0, 'ro', ms=0.5, label='potential')\n",
" self.eline, = self.enerplot.plot(0, 0, 'ko', ms=0.5, label='total')\n",
" self.coordplot.set_xlim(-self.boxlength/2, self.boxlength/2)\n",
" self.coordplot.set_ylim(-self.boxlength/2, self.boxlength/2)\n",
" self.coordplot.set_xlim(-self.boxlength/2.0, self.boxlength/2.0)\n",
" self.coordplot.set_ylim(-self.boxlength/2.0, self.boxlength/2.0)\n",
" self.coordplot.set_aspect('equal')\n",
" self.coordplot.get_xaxis().set_ticks([])\n",
" self.coordplot.get_yaxis().set_ticks([])\n",
Expand Down Expand Up @@ -395,8 +407,8 @@
" def init_coords(self):\n",
" \"\"\" Set up the initial simulation coordinates on a regular grid, perturbed slightly. \"\"\"\n",
" # Initial coordinates; regular lattice\n",
" spacing = np.linspace(-self.boxlength/2, self.boxlength/2, self.atoms_per_dim, endpoint=False)\n",
" spacing += self.boxlength/(2*self.atoms_per_dim)\n",
" spacing = np.linspace(-self.boxlength/2.0, self.boxlength/2.0, self.atoms_per_dim, endpoint=False)\n",
" spacing += self.boxlength/(2.0*self.atoms_per_dim)\n",
" # Center the array\n",
" self.coords = np.array([np.repeat(spacing, self.atoms_per_dim), np.tile(spacing, self.atoms_per_dim)]).T\n",
" # Move the atoms very slightly away from their regular grid positions\n",
Expand Down Expand Up @@ -488,7 +500,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sim = MDSimulation(sigma=0.27, epsilon=0.3, mass=2, temp=300, dt=0.005, atoms_per_dim=5, boxlength=3,\n",
Expand All @@ -512,7 +526,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sim = MDSimulation(sigma=0.27, epsilon=0.3, mass=2, temp=300, dt=0.005, atoms_per_dim=5, boxlength=3, cutoff=1.2, nsteps=500)\n",
Expand All @@ -535,7 +551,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def showpotential(ax, sigma=0.25, epsilon=0.11, cutoff=0.8):\n",
Expand Down Expand Up @@ -567,7 +585,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Evaluate this cell, then play with the sliders to see the effect of the well\n",
Expand All @@ -590,7 +610,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def showpotential(ax, sigma=0.25, epsilon=0.11, cutoff=0.8, window=0.2):\n",
Expand Down Expand Up @@ -629,7 +651,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Evaluate this cell, then play with the sliders to see the effect of the well\n",
Expand All @@ -653,7 +677,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class MDSimulationSwitch(MDSimulation):\n",
Expand Down Expand Up @@ -713,7 +739,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"sim = MDSimulationSwitch(sigma=0.27, epsilon=0.3, mass=2, temp=300, dt=0.005, atoms_per_dim=5,\n",
Expand Down
20 changes: 10 additions & 10 deletions Tutorials/12_MD/12b_ewald.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@
"coulomb = 1/(4*np.pi*epsilon0)\n",
"boxlength = 0.8 #nm\n",
"# Initial coordinates; regular lattice\n",
"spacing = np.linspace(-boxlength/2, boxlength/2, atoms_per_dim, endpoint=False)\n",
"spacing += boxlength/(2*atoms_per_dim)\n",
"spacing = np.linspace(-boxlength/2.0, boxlength/2.0, atoms_per_dim, endpoint=False)\n",
"spacing += boxlength/(2.0*atoms_per_dim)\n",
"# Center the array\n",
"coords = np.array(list(itertools.product(spacing, spacing, spacing)))\n",
"# Move the atoms very slightly away from their regular grid positions\n",
"np.random.seed(0)\n",
"coords += 0.2*(np.random.rand(natoms,3)-0.5)\n",
"coords[coords>boxlength/2] = boxlength/2\n",
"coords[coords<-boxlength/2] = -boxlength/2\n",
"coords[coords>boxlength/2.0] = boxlength/2.0\n",
"coords[coords<-boxlength/2.0] = -boxlength/2.0\n",
"charges = np.zeros(len(coords))\n",
"if 1:\n",
" # Generate a large dipole\n",
Expand All @@ -193,9 +193,9 @@
"ax.scatter(poscrd[:,0], poscrd[:,1], poscrd[:,2], 'bo')\n",
"ax.scatter(negcrd[:,0], negcrd[:,1], negcrd[:,2], 'ro')\n",
"ax.scatter(neucrd[:,0], neucrd[:,1], neucrd[:,2], 'ko')\n",
"ax.set_xlim(-boxlength/2, boxlength/2)\n",
"ax.set_ylim(-boxlength/2, boxlength/2)\n",
"ax.set_zlim(-boxlength/2, boxlength/2)\n",
"ax.set_xlim(-boxlength/2.0, boxlength/2.0)\n",
"ax.set_ylim(-boxlength/2.0, boxlength/2.0)\n",
"ax.set_zlim(-boxlength/2.0, boxlength/2.0)\n",
"ax.set_xticks([])\n",
"ax.set_yticks([])\n",
"ax.set_zticks([])\n",
Expand Down Expand Up @@ -584,7 +584,7 @@
"plt.show()\n",
"\n",
"# Confirm that all high alpha values have the same total energy\n",
"assert np.allclose(Uvals[1,3], Uvals[2:,3])"
"assert np.allclose(Uvals[2,3], Uvals[3:,3])"
]
},
{
Expand Down Expand Up @@ -662,7 +662,7 @@
" # that this is independent of particle positions and can therefore be formed\n",
" # at the start of the simulation and cached for reuse in each time step.\n",
" pi2_a2 = np.pi**2/alpha**2 if alpha != 0.0 else 1e50\n",
" mvals = shiftm / boxlength\n",
" mvals = shiftm / float(boxlength)\n",
" splinemod = 1/dftmod(splineorder, mdim)\n",
" splinemods = np.einsum('x,y,z->xyz', splinemod, splinemod, splinemod)\n",
" m = np.array(list(itertools.product(mvals, mvals, mvals)))\n",
Expand Down Expand Up @@ -735,7 +735,7 @@
"plt.show()\n",
"\n",
"# Confirm that all high alpha values have the same total energy\n",
"assert np.allclose(Uvals[1,3], Uvals[2:,3], atol=5)"
"assert np.allclose(Uvals[2,3], Uvals[3:,3], atol=5)"
]
},
{
Expand Down

0 comments on commit 6882e01

Please sign in to comment.