From 6882e0156801f45cbc43f20aea015f79304c8d0a Mon Sep 17 00:00:00 2001 From: Andy Simmonett Date: Tue, 3 Oct 2017 17:10:25 -0600 Subject: [PATCH] Fix py27 integer division bug --- Tutorials/12_MD/12a_basics.ipynb | 72 ++++++++++++++++++++++---------- Tutorials/12_MD/12b_ewald.ipynb | 20 ++++----- 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/Tutorials/12_MD/12a_basics.ipynb b/Tutorials/12_MD/12a_basics.ipynb index dc7f6c93..a1c6cea4 100644 --- a/Tutorials/12_MD/12a_basics.ipynb +++ b/Tutorials/12_MD/12a_basics.ipynb @@ -3,7 +3,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "%matplotlib notebook\n", @@ -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", @@ -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", @@ -94,7 +100,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "rval = 0.5\n", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -653,7 +677,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "class MDSimulationSwitch(MDSimulation):\n", @@ -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", diff --git a/Tutorials/12_MD/12b_ewald.ipynb b/Tutorials/12_MD/12b_ewald.ipynb index b87655de..5dab578f 100644 --- a/Tutorials/12_MD/12b_ewald.ipynb +++ b/Tutorials/12_MD/12b_ewald.ipynb @@ -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", @@ -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", @@ -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])" ] }, { @@ -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", @@ -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)" ] }, {