diff --git a/doc/tutorials/electrodes/electrodes_part1.ipynb b/doc/tutorials/electrodes/electrodes_part1.ipynb index 06146bdf41..21e2f8861a 100644 --- a/doc/tutorials/electrodes/electrodes_part1.ipynb +++ b/doc/tutorials/electrodes/electrodes_part1.ipynb @@ -49,18 +49,17 @@ "## Theoretical Background \n", "\n", "The normal component of electric field across a surface dividing two dielectric\n", - "media yields a discontinuity, which can be expressed in terms of a finite\n", - "surface charge density \n", + "media in presence of a surface charge density $\\sigma$ is discontinuous and follows as\n", "$(\\epsilon_1\\vec{E}_1 - \\epsilon_2\\vec{E}_2).\\hat{n}=-\\sigma(\\vec{r})$.\n", "This expression describes the jump in the electric field across the material\n", "interface going from a dielectric medium $\\epsilon_1$ to another one,\n", "$\\epsilon_2$.\n", "\n", "While in the case of non-polarizable materials ($\\epsilon_1 = \\epsilon_2 = 1$),\n", - "this jump is only related to surface charges and dielectric contrast and the\n", + "this jump is only related to surface charges and the\n", "potential is continuous across the interface, for polarizable materials also the\n", "polarization field $\\vec{P}$ will give a contribution. \n", - "In order to solve the problem in presence of a jump of the dielectric constant\n", + "In order to solve for the electric field in presence of a jump of the dielectric constant\n", "across an interface, one must know the electric fields on both sides. \n", "\n", "Another approach is to replace this two domain problem by an equivalent one\n", @@ -70,18 +69,19 @@ "With this well known \"method of image charges\", it is sufficient to know the\n", "electric field on one side of the interface. \n", "**ESPResSo** provides the \"Induced Charge Calculation with fast Coulomb Solvers\"\n", - "*(ICC $\\star$) algorithm [1] which employs a numerical\n", - "*scheme for solving the boundary integrals and the induced charge. \n", + "(ICC $\\star$) algorithm [1] which employs a numerical scheme for solving the boundary integrals and the induced charge. \n", "\n", - "*Note*: Apart from ICC $\\star$, **ESPResSo** offers the \"Electrostatic layer\n", + "*Note*: Apart from ICC $\\star$ that solves for image charges spatially resolved, **ESPResSo** offers the \"Electrostatic layer\n", "correction with image charges\" (ELC-IC) method [3], for\n", - "planar 2D+h partially periodic systems with dielectric interfaces.\n", + "planar 2D+h partially periodic systems with dielectric interfaces that accounts for laterally averaged surface charge.\n", "The tutorial on *Basic simulation of electrodes in ESPResSo part II*\n", "addresses this in detail.\n", "\n", + "### Green's function for charges in a dielectric slab\n", + "\n", "The Green's function for two point charges in a dielectric slab can be solved\n", "analytically [2].\n", - "In the metallic limit the dielectric contrast is\n", + "In the metallic limit ($\\epsilon_2 \\to\\infty$) the dielectric contrast is\n", "$$ \\Delta = \\frac{\\epsilon_1 - \\epsilon_2} {\\epsilon_1 + \\epsilon_2} = -1 .$$\n", "If the ions are placed in the center of the slab of width $w$ and a distance $r$\n", "away from each other, the Green's function accounting for all image charges\n", @@ -114,7 +114,7 @@ "These systems usually exhibit a confinement along one ($z$) direction, where the\n", "confining boundary or interface imposes a dielectric discontinuity, while the\n", "other $x$-$y$ directions are treated periodic. \n", - "To such a partially periodic system, we combine the efficient scaling behavior\n", + "To investigate such a partially periodic system, we combine the efficient scaling behavior\n", "of three-dimensional mesh-based solvers (typically\n", "$\\mathcal{O}(N \\log N)$ for P3M) with the Electrostatic Layer Correction (ELC)\n", "[3].\n", @@ -137,10 +137,10 @@ "pillbox, which will give the induced surface charge in this infinitesimal\n", "segment as (Gauss law):\n", "$$q_{ind} = \\frac{1}{4\\pi} \\oint\\, dA\\, \\cdot \\epsilon\\nabla \\phi = \\frac{A}{4\\pi}(\\epsilon_1\\vec{E}_1 \\cdot \\hat{n}-\\epsilon_2\\vec{E}_2 \\cdot\\hat{n})$$\n", - "- The electric field at the closest proximity of the interface, $\\vec{E}_{1/2}$,\n", + "- The electric field in region 1 at the closest proximity of the interface, $\\vec{E}_{1}$,\n", "can be written as a sum of electric field contributions from the surface charge\n", "$\\sigma$ and the external electric field $\\vec{E}$:\n", - "$$ \\vec{E}_{1/2} =\\vec{E} + 2\\pi/\\epsilon_1\\sigma\\hat{n} $$\n", + "$$ \\vec{E}_{1} =\\vec{E} + 2\\pi/\\epsilon_1\\sigma\\hat{n} $$\n", "- Combining this with the previous expression, the induced charge can be written in terms of the dielectric mismatch $\\Delta$ and the electric field as:\n", "$$\\sigma = \\frac{\\epsilon_1}{2\\pi} \\frac{\\epsilon_1-\\epsilon_2}{\\epsilon_1+\\epsilon_2}\\vec{E} \\cdot \\hat{n} =: \\frac{\\epsilon_1}{2\\pi} \\Delta \\, \\vec{E} \\cdot \\hat{n}$$\n", "\n", @@ -148,7 +148,7 @@ "The basic idea of the ICC $^\\star$ formalism now is to employ a discretization\n", "of the surface by means of spatially fixed ICC particles.\n", "The charge of each ICC particle is not fixed but rather iterated using the\n", - "expressions for $\\vec{E}_{1/2}$ and $\\sigma$ above until a self-consistent\n", + "expressions for $\\vec{E}_{1}$ and $\\sigma$ above until a self-consistent\n", "solution is found." ] }, @@ -172,17 +172,15 @@ "metadata": {}, "outputs": [], "source": [ - "import espressomd\n", + "import matplotlib.pyplot as plt\n", + "import tqdm\n", "import numpy as np\n", - "import espressomd.electrostatics\n", - "import espressomd.electrostatic_extensions\n", - "from espressomd.interactions import *\n", "\n", + "import espressomd\n", "espressomd.assert_features(['ELECTROSTATICS'])\n", "\n", - "import matplotlib.pyplot as plt\n", - "from scipy.special import *\n", - "from tqdm import tqdm" + "import espressomd.electrostatics\n", + "import espressomd.electrostatic_extensions" ] }, { @@ -192,13 +190,12 @@ "We need to define the system dimensions and some physical parameters related to\n", "length, time and energy scales of our system.\n", "All physical parameters are defined in reduced units of length ($\\sigma=1$;\n", - "Particle size), mass ($m=1$; Particle mass), time ($t=0.01 \\tau$) and\n", + "Particle size), mass ($m=1$; Particle mass), arbitrary time (we do not consider particle dynamics) and\n", "elementary charge ($e=1$).\n", "\n", "Another important length scale is the Bjerrum Length, which is the length at\n", "which the electrostatic energy between two elementary charges is comparable to\n", - "the thermal energy $k_\\mathrm{B}T$ and thus defines the energy scale in our\n", - "system.\n", + "the thermal energy $k_\\mathrm{B}T$.\n", "It is defined as\n", "$$\\ell_\\mathrm{B}=\\frac{1}{4\\pi\\epsilon_0\\epsilon_r k_\\mathrm{B}T}.$$ \n", "In our case, if we choose the ion size ($\\sigma$) in simulations to represent a\n", @@ -213,48 +210,67 @@ "metadata": {}, "outputs": [], "source": [ - "#***************************************************\n", - "# System Setup\n", - "#***************************************************\n", - "\n", "# Box dimensions\n", "# To construct a narrow slit Lz << (Lx , Ly)\n", "box_l_x = 100.\n", "box_l_y = 100.\n", "box_l_z = 5.\n", "\n", - "# ICC* with ELC: 2D electrostatics\n", + "# Additional space for ELC\n", "ELC_GAP = 6*box_l_z\n", "\n", "system = espressomd.System(box_l=[box_l_x, box_l_y, box_l_z+ELC_GAP])\n", "\n", - "# System Time\n", "system.time_step = 0.01\n", "system.cell_system.skin = 0.4\n", "\n", "# Elementary charge \n", - "q = np.array([1.0]) \n", - "\n", - "# Interaction Parameters for P3M with ELC\n", + "q = 1.0 \n", "\n", + "# Interaction parameters for P3M with ELC\n", "BJERRUM_LENGTH = 2.0 # Electrostatic prefactor passed to P3M ; prefactor=lB KBT/e2 \n", "ACCURACY = 1e-7 # P3M force accuracy \n", - "CHECK_ACCURACY = 1e-7 # maximim pairwise error in ELC\n", - "\n", - "#Lennard-Jones Parameters\n", - "\n", + "MAX_PW_ERROR = 1e-7 # maximum pairwise error in ELC\n", + "ICC_EPSILON_DOMAIN = 1. # epsilon inside the slit\n", + "ICC_EPSILON_WALLS = 1e5 # epsilon outside the slit. Very large to mimic metal\n", + "ICC_CONVERGENCE = 1e-3 # ICC numeric/performance parameters\n", + "ICC_RELAXATION = 0.95\n", + "ICC_MAX_ITERATIONS = 1000\n", + "\n", + "# Lennard-Jones parameters\n", "LJ_SIGMA = 1.0\n", "LJ_EPSILON = 1.0 \n", "\n", - "#Particle parameters\n", + "# Particle parameters\n", + "TYPES = {\"Cation\": 0, \"Anion\": 1 ,\"Electrodes\": 2}\n", + "charges = {\"Cation\": q, \"Anion\": -q }\n", "\n", - "types = {\"Cation\": 0, \"Anion\": 1 ,\"Electrodes\": 2}\n", - "charges = {\"Cation\": q[0], \"Anion\": -q[0] }\n", - "\n", - "p1=system.part.add(pos=[box_l_x/4.0, box_l_y/2.0, box_l_z/2.0], q=charges[\"Cation\"])\n", - "print(f\"Cation placed at position: {p1.pos}\")\n", - "p2=system.part.add(pos=[3.0*box_l_x/4.0, box_l_y/2.0, box_l_z/2.0], q=charges[\"Anion\"])\n", - "print(f\"Anion placed at position: {p2.pos}\")\n" + "# Test particles to measure forces\n", + "p1=system.part.add(pos=[box_l_x/4.0, box_l_y/2.0, box_l_z/2.0], q=charges[\"Cation\"], fix=3*[True])\n", + "p2=system.part.add(pos=[3.0*box_l_x/4.0, box_l_y/2.0, box_l_z/2.0], q=charges[\"Anion\"], fix=3*[True])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Setup of electrostatic interactions\n", + "First, we define our 3D electrostatics solver (P3M)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "p3m = espressomd.electrostatics.P3M(\n", + " prefactor=BJERRUM_LENGTH,\n", + " accuracy=ACCURACY,\n", + " check_neutrality = False,\n", + " mesh = [100,100,150],\n", + " cao = 5\n", + " )" ] }, { @@ -264,105 +280,153 @@ "solution2_first": true }, "source": [ - "### Task:\n", - "Set up electrostatics interactions between the ion pairs using P3M and ICC $\\star$. \n", - "#### Hints:\n", - "- First instantiate a P3M object (using\n", - " [*espressomd.electrostatics.P3M(...)*](https://espressomd.github.io/doc/espressomd.html#espressomd.electrostatics.P3M))\n", - " as the 3D Coulomb solver for the system.\n", - "- Since, we are working on a 2D+h partially periodic system, we use ELC in\n", - " combination with P3M. For this purpose, instantiate ELC (using\n", - " [*espressomd.electrostatics.ELC(...)*](https://espressomd.github.io/doc/espressomd.html#espressomd.electrostatics.ELC))\n", - " with p3m as actor, which calls necessary initialization routines. \n", - "- Now we account for dielectric interface by adding ICC $^\\star$.\n", - " To this end, first create an ICC object using\n", - " [*espressomd.electrostatic_extensions.ICC(...)*](https://espressomd.github.io/doc/espressomd.html#espressomd.electrostatic_extensions.ICC).\n", - "- Setting up ICC $^\\star$ requires specifying a fixed number of ICC particles\n", - " and their initial charges.\n", - " Since the ICC particles are normal ESPResSo particles, they need to be added\n", - " at the interfaces using\n", - " [*system.part.add(..)*](https://espressomd.github.io/doc/espressomd.html?#module-espressomd.particle_data).\n", - "\n", - "*Note* - Refer to section\n", - "[**Dielectric interfaces with the ICC algorithm**](https://espressomd.github.io/doc/electrostatics.html#dielectric-interfaces-with-the-icc-star-algorithm)\n", - "in the **ESPResSo** documentation for the basics of an ICC $^\\star$ setup.\n" + "### Task\n", + "\n", + "* Set up [ELC](https://espressomd.github.io/doc/espressomd.html#espressomd.electrostatics.ELC) with ``p3m`` as its actor." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "solution2": "hidden" + }, + "source": [ + "```python \n", + "elc = espressomd.electrostatics.ELC(actor=p3m, gap_size=ELC_GAP, maxPWerror=MAX_PW_ERROR)\n", + "```" ] }, { "cell_type": "code", "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, we set up the ICC particles on both electrodes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ICC_PARTCL_NUMBER_DENSITY = 1\n", + "icc_partcls_bottom = []\n", + "icc_partcls_top = []" + ] + }, + { + "cell_type": "markdown", "metadata": { "solution2": "hidden", "solution2_first": true }, + "source": [ + "### TASK\n", + "\n", + "* Using the (area) density of ICC particles defined in the cell above, calculate the x/y positions of the particles for a uniform, quadratic grid. \n", + "* Add fixed particles on the electrodes. Make sure to use the correct ``type``. Give the top (bottom) plate a total charge of 1 (-1). \n", + "* Store the created particles in lists ``icc_partcls_bottom``, ``icc_partcls_top``." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "solution2": "hidden" + }, + "source": [ + "```python \n", + "line_density = np.sqrt(ICC_PARTCL_NUMBER_DENSITY)\n", + "xs = np.linspace(0, system.box_l[0], num=int(round(system.box_l[0] * line_density)), endpoint=False)\n", + "ys = np.linspace(0, system.box_l[1], num=int(round(system.box_l[1] * line_density)), endpoint=False)\n", + "n_partcls_each_electrode = len(xs) * len(ys)\n", + "\n", + "# Bottom electrode\n", + "for x in xs:\n", + " for y in ys:\n", + " icc_partcls_bottom.append(system.part.add(pos=[x, y, 0.], q=-1/n_partcls_each_electrode,\n", + " type=TYPES[\"Electrodes\"], fix=3*[True]))\n", + "# Top electrode\n", + "for x in xs:\n", + " for y in ys:\n", + " icc_partcls_top.append(system.part.add(pos=[x, y, box_l_z], q=1/n_partcls_each_electrode,\n", + " type=TYPES[\"Electrodes\"], fix=3*[True]))\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "solution2": "hidden", + "solution2_first": true + }, "source": [ - "p3m = espressomd.electrostatics.P3M(\n", - " prefactor=BJERRUM_LENGTH,\n", - " accuracy=ACCURACY,\n", - " check_neutrality = False,\n", - " mesh = [100,100,150],\n", - " cao = 5\n", - " )\n", - "\n", - "elc = espressomd.electrostatics.ELC(actor=p3m, gap_size=ELC_GAP, maxPWerror=CHECK_ACCURACY)\n", - "\n", - "# Set the ICC line density and calculate the number of\n", - "# ICC particles according to the box size\n", - "nicc = 100 # linear density\n", - "nicc_per_electrode = nicc**2 # surface density\n", - "nicc_tot = 2 * nicc_per_electrode\n", - "iccArea = box_l_x * box_l_y / nicc_per_electrode\n", - "lx = box_l_x / nicc\n", - "ly = box_l_y / nicc\n", - "\n", - "# Lists to collect required parameters\n", - "iccNormals = []\n", - "iccAreas = []\n", - "iccSigmas = []\n", - "iccEpsilons = []\n", - "\n", - "# Add the fixed ICC particles:\n", - "\n", - "# Bottom electrode (normal [0, 0, 1])\n", - "for xi in range(nicc):\n", - " for yi in range(nicc):\n", - " system.part.add(pos=[lx * xi, ly * yi, 0.], q=-0.0001,\n", - " type=types[\"Electrodes\"], fix=[True, True, True])\n", - "iccNormals.extend([0, 0, 1] * nicc_per_electrode)\n", - "\n", - "# Top electrode (normal [0, 0, -1])\n", - "for xi in range(nicc):\n", - " for yi in range(nicc):\n", - " system.part.add(pos=[lx * xi, ly * yi, box_l_z], q=0.0001,\n", - " type=types[\"Electrodes\"], fix=[True, True, True])\n", - "iccNormals.extend([0, 0, -1] * nicc_per_electrode)\n", + "### Task\n", + "\n", + "* Set ``elc`` as ``system.electrostatics.solver``\n", + "* Create an [ICC object]((https://espressomd.github.io/doc/espressomd.html#espressomd.electrostatic_extensions.ICC) and set it as ``system.electrostatics.extension``\n", + "\n", + "### Hints\n", + "\n", + "* ICC variables are defined in the second code cell from the top.\n", + "* Make sure to not mark our test particles ``p1`` and ``p2`` (with ids 0 and 1) as ICC particles." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "solution2": "hidden" + }, + "source": [ + "```python\n", + "system.electrostatics.solver = elc\n", + "\n", + "n_icc_partcls = len(icc_partcls_top) + len(icc_partcls_bottom)\n", "\n", "# Common area, sigma and metallic epsilon\n", - "iccAreas.extend([iccArea] * nicc_tot)\n", - "iccSigmas.extend([0] * nicc_tot)\n", - "iccEpsilons.extend([100000] * nicc_tot)\n", + "icc_areas= 1/ICC_PARTCL_NUMBER_DENSITY * np.ones(n_icc_partcls)\n", + "icc_sigmas= np.zeros(n_icc_partcls)\n", + "icc_epsilons = ICC_EPSILON_WALLS * np.ones(n_icc_partcls)\n", "\n", - "iccNormals = np.array(iccNormals, dtype=float).reshape(nicc_tot, 3)\n", + "icc_normals = np.array([[0,0,1] for _ in range(n_icc_partcls//2)] + [[0,0,-1] for _ in range(n_icc_partcls//2)])\n", "\n", "icc = espressomd.electrostatic_extensions.ICC(\n", - " first_id=2,\n", - " n_icc=nicc_tot,\n", - " convergence=1e-3,\n", - " relaxation=0.95,\n", + " first_id=min(system.part.select(type=TYPES[\"Electrodes\"]).id),\n", + " n_icc=n_icc_partcls,\n", + " convergence=ICC_CONVERGENCE,\n", + " relaxation=ICC_RELAXATION,\n", " ext_field=[0, 0, 0],\n", - " max_iterations=1000,\n", - " eps_out=1.0,\n", - " normals=iccNormals,\n", - " areas=np.array(iccAreas, dtype=float),\n", - " sigmas=np.array(iccSigmas, dtype=float),\n", - " epsilons=np.array(iccEpsilons, dtype=float)\n", + " max_iterations=ICC_MAX_ITERATIONS,\n", + " eps_out=ICC_EPSILON_DOMAIN,\n", + " normals=icc_normals,\n", + " areas=icc_areas,\n", + " sigmas=icc_sigmas,\n", + " epsilons=icc_epsilons\n", " )\n", - "\n", - "system.electrostatics.solver = elc\n", - "system.electrostatics.extension = icc" + "system.electrostatics.extension = icc\n", + "```" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -378,18 +442,22 @@ "source": [ "r = np.logspace(0,box_l_z/4.,10) \n", "elc_forces_axial = np.empty((len(r), 2))\n", + "n_icc_per_electrode = len(icc_partcls_top)\n", + "\n", + "p1.pos = [0, box_l_y/2.0, box_l_z/2.0]\n", "\n", - "for i, x in enumerate(tqdm(r)):\n", - " p1.pos = [0, box_l_y/2.0, box_l_z/2.0]\n", + "for i, x in enumerate(tqdm.tqdm(r)): \n", " p2.pos = [x, box_l_y/2.0, box_l_z/2.0]\n", "\n", " system.integrator.run(0)\n", " elc_forces_axial[i, 0] = p1.f[0]\n", " elc_forces_axial[i, 1] = p2.f[0]\n", " \n", - " # reset ICC charges to ensure charge neutrality check passes\n", - " system.part.by_ids(range(2,2+nicc_per_electrode)).q = np.array([-0.0001]*nicc_per_electrode)\n", - " system.part.by_ids(range(2+nicc_per_electrode,2+2*nicc_per_electrode)).q = np.array([0.0001]*nicc_per_electrode) " + " # reset ICC charges to ensure charge neutrality \n", + " for part in icc_partcls_top:\n", + " part.q = 1/n_icc_per_electrode\n", + " for part in icc_partcls_bottom:\n", + " part.q = -1/n_icc_per_electrode" ] }, { @@ -416,11 +484,11 @@ " return (-1)**n * x/(x**2+n**2)**(3./2)\n", " \n", " def do_sum(x):\n", - " max = int(1e3)\n", - " sum = 0\n", - " for n in range(-max+1,max+1):\n", - " sum += summand(x,n)\n", - " return sum\n", + " limit = int(1e3)\n", + " sum_ = 0\n", + " for n in range(-limit+1,limit+1):\n", + " sum_ += summand(x,n)\n", + " return sum_\n", "\n", " F = do_sum(x) * prefactor\n", " return F\n", @@ -478,7 +546,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -492,7 +560,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/doc/tutorials/electrodes/electrodes_part2.ipynb b/doc/tutorials/electrodes/electrodes_part2.ipynb index a22fadbbc4..61a0e18b4c 100644 --- a/doc/tutorials/electrodes/electrodes_part2.ipynb +++ b/doc/tutorials/electrodes/electrodes_part2.ipynb @@ -2,10 +2,7 @@ "cells": [ { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "# Basic simulation of electrodes in ESPResSo part II:\n", "# Electrolyte capacitor and Poisson-Boltzmann theory" @@ -13,10 +10,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "## Prerequisites\n", "\n", @@ -34,10 +28,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "## Introduction\n", "\n", @@ -48,7 +39,7 @@ "placed between two electrodes and thus gets polarized upon application of an\n", "external potential formic an electric double layer at the interfaces\n", "[1].\n", - "Electric double-layer capacitors (EDLCs) can be constructed of electrodes of\n", + "Electric double-layer capacitors (EDLCs) can be constructed from electrodes of\n", "various geometries and materials where energy is stored by potential driven\n", "adsorption of counterions on the surface of the electrodes and forming the\n", "double layer. \n", @@ -56,13 +47,13 @@ "energy per volume.\n", "\n", "In this tutorial we are going to investigate the ionic layer formation between\n", - "two conducting dielectric walls in presence of an applied voltage **ESPResSo**'s\n", + "two conducting dielectric walls in presence of an applied voltage using **ESPResSo**'s\n", "\"Electrostatic layer correction with image charges\" (ELC-IC) method\n", "[2].\n", "We employ a primitive model of an aqueous salt solution, where the solvent is\n", - "treated implicitly as homogeneous dielectric background.\n", + "treated implicitly as a homogeneous dielectric background.\n", "Thus, within the limits of a mean-field treatment and for not too small slit\n", - "widths, we can compare our findings against with the analytical Gouy-Chapmann\n", + "widths, we can compare our findings with the analytical Gouy-Chapmann\n", "solution for a single charged plane since additivity is assumed to hold if the\n", "potential of both walls is screened sufficiently.\n", "While this mean-field formalism properly describes the behavior of Coulomb\n", @@ -76,18 +67,15 @@ "\n", "The inclusion of dielectric inhomogeneities appearing at the conducting\n", "interfaces demands to take into account image effects that involve the full\n", - "solution of the Poisson equation on the fly.\n", - "This is dealt in a computational cost-effective way using the ELC-IC method to\n", - "treat the image charge effect in the presence of 2D dielectric bounding\n", + "solution of the Poisson equation.\n", + "This is dealt with in a computationally cost-effective way using the ELC-IC method to\n", + "treat the image charge effects in the presence of 2D dielectric bounding\n", "interfaces. " ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "## Theoretical Background \n", "\n", @@ -101,14 +89,17 @@ "Gouy [3] and Chapman [4] derived in the\n", "early 20th century the analytic solution for the case of a single planar wall\n", "withing the mean-field approximation of the Poisson Boltzmann (PB) equation. \n", + "We will use it to describe our two-electrode system, which is justified if the electrodes are \n", + "so far apart that one surface does not influence the ion distribution in front of the other surface.\n", + "\n", "In the case of a monovalent electrolyte, double integrating the PB equation and\n", "employing the corresponding boundary conditions for the charged plane located at\n", "$z=0$ yields the electrostatic potential:\n", - "$$\\psi(z) = -2\\ln\\left[\n", - " \\frac{1-\\tanh(\\psi_\\mathrm{s}/4)e^{-\\kappa_\\mathrm{D} z}}\n", - " {1+\\tanh(\\psi_\\mathrm{s}/4)e^{-\\kappa_\\mathrm{D}z}} \\right].$$ \n", - "Here, $\\psi_\\mathrm{s}=\\psi(z=0)=$ const is the surface potential considering\n", - "that $\\psi(z\\rightarrow \\infty)=0$ and\n", + "$$\\phi(z) = -2\\ln\\left[\n", + " \\frac{1-\\tanh(\\phi_\\mathrm{s}/4)e^{-\\kappa_\\mathrm{D} z}}\n", + " {1+\\tanh(\\phi_\\mathrm{s}/4)e^{-\\kappa_\\mathrm{D}z}} \\right].$$ \n", + "Here, $\\phi_\\mathrm{s}=\\phi(z=0)=$ const is the surface potential such\n", + "that $\\phi(z\\rightarrow \\infty)=0$.\n", "$\\kappa_\\mathrm{D} = \\lambda_\\mathrm{D}^{-1}$ is the inverse Debye screening length given by\n", "$$ \\lambda_\\mathrm{D} = \\left(\\frac{\\epsilon \\, k_{\\mathrm B} T}{\\sum_{j = 1}^N n_j^0 \\, q_j^2}\\right)^{1/2}, $$\n", "where $n_j^{(0)}$ and $q_j$ are the equilibrium number density and charge of the\n", @@ -123,9 +114,9 @@ "$$n_{\\pm}(z)=n_\\pm^{(0)}\\left(\\frac{1\\pm\\gamma e^{-\\kappa_\\mathrm{D}z}}\n", " {1\\mp\\gamma e^{-\\kappa_\\mathrm{D}z}} \\right)^2$$\n", "Here, $\\gamma$ is associated with the surface potential as\n", - "$\\psi_\\mathrm{s}=-4\\tanh^{-1}(\\gamma)$.\n", + "$\\phi_\\mathrm{s}=-4\\tanh^{-1}(\\gamma)$.\n", "At large z, where the potential decays to zero, the ionic profiles tend to their\n", - "bulk (reservoir) densities, $n_\\pm(\\infty) = n_\\pm^{(0)}$\n", + "bulk (reservoir) densities, $n_\\pm(z\\to\\infty) = n_\\pm^{(0)}$\n", "\n", "The relation between the surface potential and the surface charge induced on the\n", "electrode is given by the Grahame Equation [5] :\n", @@ -137,10 +128,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "## ELC-IC for 2D+h periodic systems with dielectric interfaces\n", "\n", @@ -151,11 +139,11 @@ "scaling behavior of three-dimensional mesh-based solvers (typically\n", "$\\mathcal{O}(N \\log N)$ for P3M) with the Electrostatic Layer Correction (ELC)\n", "[1].\n", - "The latter accounts for the contributions from the periodic images in the\n", - "constrained direction and its numerical cost grows linear with the number of\n", + "The latter removes the contributions from the periodic images in the\n", + "non-periodic direction and its numerical cost grows linear with the number of\n", "point charges $N$, hence the performance overall depends on the underlying $3-D$\n", "Coulomb solver.\n", - "Furthermore, ELC can be extended straightforward to implement metallic boundary\n", + "Furthermore, ELC can be extended straightforwardly to metallic boundary\n", "conditions (or any other dielectric contrast) by using the method of image charges,\n", "which is referred to as the “Electrostatic Layer Correction with Image Charges”\n", "(ELC-IC) approach used in this tutorial.\n", @@ -165,7 +153,7 @@ "The total potential drop $\\Delta \\phi$ across the simulated system is readily\n", "obtained from the ion distribution and integrating twice over the one\n", "dimensional Poisson's equation:\n", - "$$-\\epsilon_{0}\\epsilon_{r}\\phi_\\mathrm{ind}(z)=\\iint_{0}^{z}\\rho(z^{'})(z-z^{'})dz^{'} $$\n", + "$$-\\epsilon_{0}\\epsilon_{r}\\phi_\\mathrm{ind}(z)=\\iint_{0}^{z}\\rho(z^{\\prime})(z-z^{\\prime})dz^{\\prime} $$\n", "Here, the subscript 'ind' indicates that this is the potential due to the\n", "induced inhomogeneous charge distribution.\n", "In order to set up a constant potential difference $\\Delta \\phi$, a homogeneous\n", @@ -181,13 +169,13 @@ "Here, $L$ denotes the lateral system size, $L_z$ the distance between the plates\n", "and $P_z = \\sum_i q_i z_i$ is the total dipole moment in $z$-direction due to\n", "the charges $q_i$.\n", - "Then, to maintain $\\Delta phi$, a force $F_z^\\mathrm{bat} = q E_z^\\mathrm{(bat)}$\n", + "Then, to maintain $\\Delta \\phi$, a force $F_z^\\mathrm{bat} = q E_z^\\mathrm{(bat)}$\n", "is applied on all charges.\n", - "Since ELC already $P_z$, the constant potential correction requires no\n", + "Since ELC already calculates $P_z$, the constant potential correction requires no\n", "additional computational effort.\n", "\n", "*Note*: Apart from ELC-IC, **ESPResSo** also provides the ICC$\\star$ method\n", - "[2] which employs ab iterative numerical scheme with\n", + "[2] which employs ab iterative numerical scheme with\n", "discretized surface particles to solve the boundary integrals at the dieletcric\n", "interface.\n", "The tutorial on *Basic simulation of electrodes in ESPResSo part I* addresses this\n", @@ -196,10 +184,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "## 1. System setup \n", "\n", @@ -209,44 +194,37 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ - "import espressomd\n", "import numpy as np\n", - "rng = np.random.default_rng(42)\n", + "import matplotlib.pyplot as plt\n", + "from scipy import constants as const\n", + "from tqdm import tqdm\n", + "from scipy.stats import sem\n", + "\n", + "import espressomd\n", + "espressomd.assert_features(['WCA', 'ELECTROSTATICS'])\n", "\n", "import espressomd.electrostatics\n", "import espressomd.electrostatic_extensions\n", - "from espressomd.interactions import *\n", "import espressomd.observables\n", "import espressomd.accumulators\n", "from espressomd import shapes\n", "\n", - "espressomd.assert_features(['WCA', 'ELECTROSTATICS'])\n", - "\n", - "import matplotlib.pyplot as plt\n", - "from scipy import constants as const\n", - "from tqdm import tqdm\n", - "from scipy.stats import sem" + "rng = np.random.default_rng(42)" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "We need to define system dimensions and some physical parameters related to\n", "length, time and energy scales of our system.\n", "As discussed in previous tutorials, all physical parameters are defined in terms\n", "of a length $\\sigma$, mass $m$ and time $t$ and unit of charge $q$.\n", "Since we are not explicitly interested in the dynamics of the system, we set the\n", - "mass to $m=1$ (Particle mass) and time $t=0.01 \\tau$.\n", + "mass to $m=1$ (Particle mass).\n", "For convenience, we choose the elementary charge as fundamental unit ($q=1e$)\n", "and $\\sigma = 1 \\,\\mathrm{nm}$.\n", "\n", @@ -256,43 +234,34 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "# water at room temperature\n", - "EPSILON_R = 78.4 # Relative dielectric constant of the water\n", + "EPSILON_R = 78.4 # Relative dielectric constant of water\n", "TEMPERATURE = 300.0 # Temperature in Kelvin\n", "BJERRUM_LENGTH = const.elementary_charge**2 / (4*np.pi*const.epsilon_0*EPSILON_R*const.Boltzmann*TEMPERATURE) / const.nano\n", "# BERRUM_LENGTH of water at room temperature is 0.71 nm; electrostatic prefactor passed to P3M KBT/e2 \n", "\n", - "#Lennard-Jones Parameters\n", - "LJ_SIGMA = 0.3 # Particle size nanometers, not point-like\n", + "#Lennard-Jones parameters\n", + "LJ_SIGMA = 0.3 # Particle size nanometers\n", "LJ_EPSILON = 1.0\n", - "HS_ION_SIZE = 2**(1/6) * LJ_SIGMA\n", "\n", - "CONCENTRATION = 1e-2 # desired concentration 10 mmol/l\n", + "CONCENTRATION = 1e-2 # desired concentration in mol/l\n", "DISTANCE = 10 # 10 Debye lengths\n", "N_IONPAIRS = 500\n", "\n", "POTENTIAL_DIFF = 5.0\n", "\n", "# Elementary charge \n", - "q = np.array([1.0]) \n", + "q = 1.0 \n", "types = {\"Cation\": 0, \"Anion\": 1 ,\"Electrodes\": 2}\n", - "charges = {\"Cation\": q[0], \"Anion\": -q[0] }" + "charges = {\"Cation\": q, \"Anion\": -q }" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "solution2": "hidden", - "solution2_first": true - }, + "metadata": {}, "source": [ "### 1.1 Setting up the box dimensions and create system\n", "\n", @@ -313,35 +282,42 @@ "\n", "Note that in order to obtain results that we can interpret easily, we explicitly\n", "set a unit system using nanometers as length-scale above.\n", - "The corresponding ion size (`HS_ION_SIZE`) of about 0.33 nm is a\n", + "The corresponding ion size of about 0.3 nm is a\n", "typical value for a simple salt; this, however, is in sharp contrast to the\n", "mean-field assumption of point-like ions.\n", "The latter are not easily studied within Molecular Dynamics simulations due to\n", "the required small time steps and are better suited for Monte-Carlo type\n", "simulations.\n", "We instead focus here on analyzing deviations from PB theory due to the finite\n", - "ion size.\n", + "ion size." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "solution2": "hidden", + "solution2_first": true + }, + "source": [ + "### Task\n", "\n", - "The first task now is to write a function \n", + "* write a function \n", "`get_box_dimension(concentration, distance, n_ionpairs=N_IONPAIRS)`\n", "that returns the lateral and normal box lengths `box_l_xy` and `box_l_z` (in\n", "nanometers) for the given parameters.\n", "\n", "**Hint:** To account for the finite ion size and the wall interaction it is\n", "useful to define the effective separation $d^\\prime = d-2\\sigma$, such that the\n", - "concentration is $\\rho = N/(A*d^\\prime)$.\n" + "concentration is $\\rho = N/(A*d^\\prime)$." ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden" }, - "outputs": [], "source": [ + "```python\n", "def get_box_dimension(concentration, distance, n_ionpairs=N_IONPAIRS):\n", " \"\"\" For a given number of particles, determine the lateral area of the box\n", " to match the desired concentration \"\"\"\n", @@ -352,29 +328,24 @@ " l_z = distance * debye_length\n", " \n", " box_volume = n_ionpairs / rho\n", - " area = box_volume / (l_z - 2*HS_ION_SIZE) # account for finite ion size in density calculation\n", + " area = box_volume / (l_z - 2*LJ_SIGMA) # account for finite ion size in density calculation\n", " l_xy = np.sqrt(area)\n", "\n", - " return l_xy, l_z" + " return l_xy, l_z\n", + "```" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "box_l_xy, box_l_z = get_box_dimension(CONCENTRATION,DISTANCE,N_IONPAIRS)\n", @@ -386,10 +357,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "We now can create the **ESPResSo** system.\n", "\n", @@ -406,10 +374,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "ELC_GAP = 6*box_l_z\n", @@ -419,23 +384,26 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true, - "solution2": "hidden", - "solution2_first": true - }, + "metadata": {}, "source": [ "### 1.2 Set up the double-layer capacitor\n", "\n", "We now set up an electrolyte solution made of monovalent cations and anions\n", "between two metallic electrodes at constant potential. \n", "\n", - "#### 1.2.1 Electrode walls \n", - "\n", - "First, we add two wall constraints at $z=0$ and $z=L_z$ to stop particles from\n", + "#### 1.2.1 Electrode walls " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "solution2": "hidden", + "solution2_first": true + }, + "source": [ + "### Task\n", + "* add two wall constraints at $z=0$ and $z=L_z$ to stop particles from\n", "crossing the boundaries and model the electrodes.\n", - "\n", "Refer to \n", "[espressomd.constraints.ShapeBasedConstraint](https://espressomd.github.io/doc/espressomd.html#espressomd.constraints.ShapeBasedConstraint)\n", "and its\n", @@ -445,15 +413,12 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden" }, - "outputs": [], "source": [ + "```python \n", "# Bottom wall, normal pointing in the +z direction \n", "floor = espressomd.shapes.Wall(normal=[0, 0, 1])\n", "c1 = system.constraints.add(\n", @@ -463,47 +428,42 @@ "ceil = espressomd.shapes.Wall(normal=[0, 0, -1],\n", " dist=-box_l_z) \n", "c2 = system.constraints.add(\n", - " particle_type=types[\"Electrodes\"], penetrable=False, shape=ceil)" + " particle_type=types[\"Electrodes\"], penetrable=False, shape=ceil)\n", + "```" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden", "solution2_first": true }, "source": [ "#### 1.2.2 Add particles for the ions\n", "\n", - "Now, place the ion pairs at random positions between the electrodes.\n", + "### Task\n", + "\n", + "* place ion pairs at random positions between the electrodes.\n", "\n", "Note, that unfavorable overlap can be avoided by placing the particles in the\n", "interval $[\\sigma, d-\\sigma]$ in the $z$-direction only." ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden" }, - "outputs": [], "source": [ - "offset=HS_ION_SIZE # avoid unfavorable overlap at close to the walls\n", + "```python\n", + "offset=LJ_SIGMA # avoid unfavorable overlap at close to the walls\n", "Init_part_btw_z1=0+offset \n", "Init_part_btw_z2=box_l_z-offset\n", "ion_pos=np.empty((3),dtype=float)\n", @@ -518,69 +478,59 @@ " ion_pos[0] = rng.random(1) * system.box_l[0]\n", " ion_pos[1] = rng.random(1) * system.box_l[1]\n", " ion_pos[2] = rng.random(1) * (Init_part_btw_z2-Init_part_btw_z1) + Init_part_btw_z1\n", - " system.part.add(pos=ion_pos, type=types[\"Anion\"] , q=charges[\"Anion\"])" + " system.part.add(pos=ion_pos, type=types[\"Anion\"] , q=charges[\"Anion\"])\n", + "```" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden", "solution2_first": true }, "source": [ "#### 1.2.3 Add interactions:\n", - "For excluded volume interactions, we add a WCA potential. \n", + "\n", + "### Task\n", + "\n", + "* For excluded volume interactions, add a WCA potential. \n", "\n", "Refer to the documentation to set up the\n", "[WCA interaction](https://espressomd.github.io/doc/espressomd.html#espressomd.interactions.WCAInteraction) \n", "under [Non-bonded](https://espressomd.github.io/doc/inter_non-bonded.html)\n", - "section.\n", - "\n", - "Add the corresponding interaction parameters to the system." + "section." ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden" }, - "outputs": [], "source": [ - "for key, val in types.items():\n", - " for key1, val1 in types.items():\n", - " system.non_bonded_inter[val, val1].wca.set_params(epsilon=LJ_EPSILON, sigma=LJ_SIGMA)" + "```python\n", + "for val in types.values():\n", + " for val1 in types.values():\n", + " system.non_bonded_inter[val, val1].wca.set_params(epsilon=LJ_EPSILON, sigma=LJ_SIGMA)\n", + "```" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden", "solution2_first": true }, @@ -598,20 +548,19 @@ "This function will take care of tuning the P3M and ELC parameters.\n", "For our purposes, an accuracy of $10^{-3}$ is sufficient.\n", "\n", - "Write a function `setup_electrostatic_solver(potential_diff)` that\n", + "### Task\n", + "\n", + "* Write a function `setup_electrostatic_solver(potential_diff)` that\n", "returns the ELC instance." ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden" }, - "outputs": [], "source": [ + "```python\n", "def setup_electrostatic_solver(potential_diff):\n", "\n", " delta_mid_top = -1.0 #(Fully metallic case both -1) \n", @@ -632,25 +581,20 @@ " delta_mid_bot=delta_mid_bot,\n", " delta_mid_top=delta_mid_top)\n", " \n", - " return elc" + " return elc\n", + "```" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "Now add the solver to the system:" ] @@ -658,10 +602,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "system.electrostatics.solver = setup_electrostatic_solver(POTENTIAL_DIFF)" @@ -669,10 +610,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "## 2. Equilibration\n", "\n", @@ -694,44 +632,27 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ - "# Relax the overlaps with steepest descent\n", - "\n", "system.integrator.set_steepest_descent(f_max=10, gamma=50.0,\n", " max_displacement=0.02)\n", "system.integrator.run(250)\n", - "system.integrator.set_vv() # Switch bach to Velocity Verlet \n", - "\n", - "# Add thermostat \n", - "thermostat_seed = np.random.randint(np.random.randint(1000000))\n", - "system.thermostat.set_langevin(kT=1.0, gamma=0.1, seed=thermostat_seed)" + "system.integrator.set_vv()\n", + "system.thermostat.set_langevin(kT=1.0, gamma=0.1, seed=42)" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "## Equilibrate the ion distribution\n", - "\n", - "Convergence after $t\\sim25$ time units, possible to run up to $t=100$ here...\n", - "This is a total of 10.000 time steps (~1 minute)." + "## Equilibrate the ion distribution" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "# Equlibration parameters\n", @@ -754,10 +675,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "# Plot the convergence of the total energy\n", @@ -770,11 +688,16 @@ "plt.show()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Convergence after $t\\sim50$ time units." + ] + }, { "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden", "solution2_first": true }, @@ -790,7 +713,9 @@ "The time average is obtained through a\n", "[espressomd.accumulators.MeanVarianceCalculator](espressomd.accumulators.MeanVarianceCalculator).\n", "\n", - "Write a function `setup_densityprofile_accumulators(bin_width)` that returns the\n", + "### Task\n", + "\n", + "* Write a function `setup_densityprofile_accumulators(bin_width)` that returns the\n", "`bin_centers` and the accumulators for both ion species in the $z$-range $[0,d]$.\n", "Since we are not estimating errors in this tutorial, the choice of `delta_N` is\n", "rather arbitrary and does not affect the results. In practice, a typical value is\n", @@ -798,34 +723,20 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { - "deletable": true, - "editable": true, "solution2": "hidden" }, - "outputs": [], "source": [ + "```python \n", "def setup_densityprofile_accumulators(bin_width):\n", + " cations = system.part.select(type=types[\"Cation\"]) \n", + " anions = system.part.select(type=types[\"Anion\"])\n", "\n", - " Ion_id=[]\n", - " Cations = system.part.select(type=types[\"Cation\"])\n", - " Cations_id=[]\n", - " for i in Cations:\n", - " Cations_id.append(i.id)\n", - " Ion_id.append(i.id)\n", - " \n", - " Anions = system.part.select(type=types[\"Anion\"])\n", - " Anions_id=[]\n", - " for i in Anions:\n", - " Anions_id.append(i.id)\n", - " Ion_id.append(i.id)\n", " \n", " n_z_bins = int(np.round((system.box_l[2] - ELC_GAP) / bin_width))\n", " \n", - " # Accumulator 1 : observable::Density_Profile\n", - " density_profile_cation = espressomd.observables.DensityProfile(ids=Cations_id,\n", + " density_profile_cation = espressomd.observables.DensityProfile(ids=cations.id,\n", " n_x_bins=1,\n", " n_y_bins=1,\n", " n_z_bins=n_z_bins,\n", @@ -836,10 +747,11 @@ " max_y=system.box_l[1],\n", " max_z=system.box_l[2] - ELC_GAP)\n", " \n", - " density_accumulator_cation = espressomd.accumulators.MeanVarianceCalculator(obs=density_profile_cation, delta_N=20)\n", + " density_accumulator_cation = espressomd.accumulators.MeanVarianceCalculator(\n", + " obs=density_profile_cation, delta_N=20)\n", " \n", " \n", - " density_profile_anion = espressomd.observables.DensityProfile(ids=Anions_id,\n", + " density_profile_anion = espressomd.observables.DensityProfile(ids=anions.id,\n", " n_x_bins=1,\n", " n_y_bins=1,\n", " n_z_bins=n_z_bins,\n", @@ -850,41 +762,35 @@ " max_y=system.box_l[1],\n", " max_z=system.box_l[2] - ELC_GAP)\n", " \n", - " density_accumulator_anion = espressomd.accumulators.MeanVarianceCalculator(obs=density_profile_anion, delta_N=20)\n", + " density_accumulator_anion = espressomd.accumulators.MeanVarianceCalculator(\n", + " obs=density_profile_anion, delta_N=20)\n", "\n", " zs = density_profile_anion.bin_centers()[0, 0, :, 2]\n", " \n", - " return zs, density_accumulator_cation, density_accumulator_anion" + " return zs, density_accumulator_cation, density_accumulator_anion\n", + "```" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ - "zs, density_accumulator_cation, density_accumulator_anion = setup_densityprofile_accumulators(bin_width = DEBYE_LENGTH/10.)" + "zs, density_accumulator_cation, density_accumulator_anion = setup_densityprofile_accumulators(\n", + " bin_width = DEBYE_LENGTH/10.)" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "### 3.2 Run the simulation\n", "\n", @@ -894,10 +800,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "N_SAMPLES_PROD = 10\n", @@ -921,24 +824,18 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "### Compare to analytical prediction\n", "\n", - "Since we assume pair-wise additivity, the total ion density follows from\n", + "Since we assume additivity, the total ion density follows from\n", "$$ \\rho (z) = \\rho_+(z) - \\rho_+ (d-z) + \\rho_-(z) - \\rho_-(d-z) .$$" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "def gouy_chapman_potential(x, debye_length, phi_0):\n", @@ -954,10 +851,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(10, 6))\n", @@ -965,14 +859,14 @@ "plt.plot(zs, anion_profile_mean, color='r', label='anion')\n", "plt.plot(zs, cation_profile_mean + anion_profile_mean, color='k', label='total')\n", "\n", - "x = np.linspace(HS_ION_SIZE, box_l_z-HS_ION_SIZE, 100)\n", + "x = np.linspace(LJ_SIGMA, box_l_z-LJ_SIGMA, 100)\n", "plt.plot(x, (gouy_chapman_density(x, CONCENTRATION, DEBYE_LENGTH,-POTENTIAL_DIFF/2.) \\\n", - " + gouy_chapman_density(box_l_z-HS_ION_SIZE-x, CONCENTRATION, DEBYE_LENGTH,POTENTIAL_DIFF/2.))/2., color='r', ls='--')\n", - "plt.plot(x, (gouy_chapman_density(box_l_z-HS_ION_SIZE-x, CONCENTRATION, DEBYE_LENGTH,-POTENTIAL_DIFF/2.) \\\n", + " + gouy_chapman_density(box_l_z-LJ_SIGMA-x, CONCENTRATION, DEBYE_LENGTH,POTENTIAL_DIFF/2.))/2., color='r', ls='--')\n", + "plt.plot(x, (gouy_chapman_density(box_l_z-LJ_SIGMA-x, CONCENTRATION, DEBYE_LENGTH,-POTENTIAL_DIFF/2.) \\\n", " + gouy_chapman_density(x, CONCENTRATION, DEBYE_LENGTH,POTENTIAL_DIFF/2.) )/2., color='b', ls='--')\n", "plt.plot(x, (gouy_chapman_density(x, CONCENTRATION, DEBYE_LENGTH,-POTENTIAL_DIFF/2.) \\\n", - " + gouy_chapman_density(box_l_z-HS_ION_SIZE-x, CONCENTRATION, DEBYE_LENGTH,POTENTIAL_DIFF/2.))/2 \\\n", - " + (gouy_chapman_density(box_l_z-HS_ION_SIZE-x, CONCENTRATION, DEBYE_LENGTH,-POTENTIAL_DIFF/2.) \\\n", + " + gouy_chapman_density(box_l_z-LJ_SIGMA-x, CONCENTRATION, DEBYE_LENGTH,POTENTIAL_DIFF/2.))/2 \\\n", + " + (gouy_chapman_density(box_l_z-LJ_SIGMA-x, CONCENTRATION, DEBYE_LENGTH,-POTENTIAL_DIFF/2.) \\\n", " + gouy_chapman_density(x, CONCENTRATION, DEBYE_LENGTH,POTENTIAL_DIFF/2.) )/2., color='k', ls='--', lw=2)\n", "\n", "plt.legend()\n", @@ -983,10 +877,14 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, + "source": [ + "We see good agreement between our simulation and the meanfield solution of Guy and Chapman. Low density and reasonably low potential make the assumptions of the analytical approach justified." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, "source": [ "We now check how well the surface charge agrees with Graham equation.\n", "To this end we calculate \n", @@ -996,15 +894,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ - "#Test sigma with Graham equation\n", - "sigma_left = np.sum((cation_profile_mean-anion_profile_mean)[:int(len(zs)/2.)]) * (zs[1]-zs[0])\n", - "sigma_right = np.sum((+cation_profile_mean-anion_profile_mean)[int(len(zs)/2.):]) * (zs[1]-zs[0])\n", + "sigma_left = np.sum((cation_profile_mean-anion_profile_mean)[:len(zs)//2]) * (zs[1]-zs[0])\n", + "sigma_right = np.sum((cation_profile_mean-anion_profile_mean)[len(zs)//2:]) * (zs[1]-zs[0])\n", "\n", "def graham_sigma(phi):\n", " return np.sinh(phi/4.) * np.sqrt(2*rho/(np.pi*BJERRUM_LENGTH))\n", @@ -1016,10 +910,7 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "The electric field is readily obtained from the integral \n", "$$E(z) = \\int_0^{z} \\frac{1}{\\epsilon_0 \\epsilon_r} \\rho(z^\\prime) \\,\\mathrm{d}z^\\prime .$$" @@ -1028,10 +919,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "# plot the electric field\n", @@ -1055,21 +943,22 @@ }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ - "And the electric potential from $\\phi(z) = \\int_0^z -E(z^\\prime)\\,\\mathrm{d}z^\\prime$." + "We see that the electric field reduces to 0 in the middle of the channel, justifying the assumption that the two electrodes are far enough apart to not influence each other." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The electric potential can be calculated from $\\phi(z) = \\int_0^z -E(z^\\prime)\\,\\mathrm{d}z^\\prime$." ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "# plot the elecrostatic potential\n", @@ -1093,44 +982,35 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ - "measured_potential_difference = -(phi[-1]+phi[0])\n", + "measured_potential_difference = -(phi[-1]-phi[0])\n", "print('applied voltage', POTENTIAL_DIFF, 'measured voltage', measured_potential_difference,\n", " 'relative deviation:', 1-measured_potential_difference/POTENTIAL_DIFF)" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "source": [ "## 4. Differential capacitance\n", "\n", - "With the above knowledge, we can now easily assess the \n", - "differential capacitance of the system, i.e. change the applied voltage\n", - "difference and determine the corresponding surface charge density." + "With the above knowledge, we can now assess the \n", + "differential capacitance of the system, by changing the applied voltage\n", + "difference and determining the corresponding surface charge density." ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "sigma_vs_phi = []\n", "MIN_PHI = 0.5\n", - "MAX_PHI = 14\n", - "N_PHI = 10\n", + "MAX_PHI = 10\n", + "N_PHI = 7\n", "N_SAMPLES_EQUIL_CAP = 5\n", "N_SAMPLES_CAP = 5\n", "\n", @@ -1172,15 +1052,12 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, "outputs": [], "source": [ "f, ax = plt.subplots(figsize=(10, 6))\n", "sigma_vs_phi = np.array(sigma_vs_phi)\n", - "x = np.linspace(0,sigma_vs_phi[:,0].max()*1.3)\n", + "x = np.linspace(0,sigma_vs_phi[:,0].max())\n", "phi_SI = sigma_vs_phi[:,0] / (const.elementary_charge / (const.Boltzmann * TEMPERATURE))\n", "plt.errorbar(-sigma_vs_phi[:,1]*const.elementary_charge/const.nano**2, phi_SI, xerr=sigma_vs_phi[:,2]*const.elementary_charge/const.nano**2, fmt='o',label='Sim')\n", "plt.plot(graham_sigma(x)*const.elementary_charge/const.nano**2,\n", @@ -1188,17 +1065,22 @@ "x = np.linspace(0,ax.get_ylim()[1])\n", "plt.plot(EPSILON_R*const.epsilon_0*x/2/(DEBYE_LENGTH*const.nano),x, label='linear PB', ls='--')\n", "plt.xlabel(r'$\\sigma\\,\\mathrm{[C/m^2]}$')\n", - "plt.ylabel(r'$\\Psi_0\\,\\mathrm{[V]}$')\n", + "plt.ylabel(r'$\\phi_\\mathrm{s}\\,\\mathrm{[V]}$')\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", - "metadata": { - "deletable": true, - "editable": true - }, + "metadata": {}, + "source": [ + "For small potential drops, one observes the expected Poisson-Boltzmann behavior. It also agrees with the linearized solution $\\sigma(\\phi_\\mathrm{s}) = \\varepsilon_r\\varepsilon_0 \\frac{\\phi_\\mathrm{s}}{2 \\lambda_\\mathrm{D}}$.\n", + "However, we observe already for potentials $\\sim 0.1\\,\\mathrm{V} = 4\\,k_\\mathrm{B}T / e$ a significant deviations which can be attributed to the fact that our ions are of finite size and thus the surface charge saturates." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, "source": [ "## References\n", "\n", @@ -1218,7 +1100,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -1232,7 +1114,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/testsuite/scripts/tutorials/test_electrodes_1.py b/testsuite/scripts/tutorials/test_electrodes_1.py index 6724cf8afe..08419519e6 100644 --- a/testsuite/scripts/tutorials/test_electrodes_1.py +++ b/testsuite/scripts/tutorials/test_electrodes_1.py @@ -28,12 +28,13 @@ class Tutorial(ut.TestCase): def test_force(self): + # exclude largest distance (small force -> large relative error) msg = 'The force for particle 1 should agree with the analytical expression.' - np.testing.assert_allclose(tutorial.elc_forces_axial[:, 0], - tutorial.analytic_force_centered(tutorial.r / tutorial.BJERRUM_LENGTH, tutorial.box_l_z), rtol=1, err_msg=msg) + np.testing.assert_allclose(tutorial.elc_forces_axial[:-1, 0], + tutorial.analytic_force_centered(tutorial.r[:-1], tutorial.box_l_z), rtol=1e-1, err_msg=msg) msg = 'The force for particle 2 should agree with the analytical expression.' - np.testing.assert_allclose(-tutorial.elc_forces_axial[:, 1], - tutorial.analytic_force_centered(tutorial.r / tutorial.BJERRUM_LENGTH, tutorial.box_l_z), rtol=1, err_msg=msg) + np.testing.assert_allclose(-tutorial.elc_forces_axial[:-1, 1], + tutorial.analytic_force_centered(tutorial.r[:-1], tutorial.box_l_z), rtol=1e-1, err_msg=msg) if __name__ == "__main__": diff --git a/testsuite/scripts/tutorials/test_electrodes_2.py b/testsuite/scripts/tutorials/test_electrodes_2.py index 355f69e4b0..e83393a40f 100644 --- a/testsuite/scripts/tutorials/test_electrodes_2.py +++ b/testsuite/scripts/tutorials/test_electrodes_2.py @@ -34,10 +34,10 @@ class Tutorial(ut.TestCase): def test_potential_difference(self): # Test that the applied potential difference equals the one from - # integrating Poisson equation (within 30 %) + # integrating Poisson equation msg = 'The potential difference is not equal to the one from integrating Poisson equation.' self.assertAlmostEqual( - tutorial.measured_potential_difference / tutorial.POTENTIAL_DIFF, 1, delta=0.1, msg=msg) + tutorial.measured_potential_difference / tutorial.POTENTIAL_DIFF, 1, delta=0.25, msg=msg) def test_charge_profile(self): # Roughly test the profile, deviations are expected!! @@ -45,7 +45,7 @@ def test_charge_profile(self): tutorial.cation_profile_mean + tutorial.anion_profile_mean) analytic = (tutorial.gouy_chapman_density(tutorial.zs, tutorial.CONCENTRATION, tutorial.DEBYE_LENGTH, -tutorial.POTENTIAL_DIFF / 2.) - + tutorial.gouy_chapman_density(tutorial.box_l_z - tutorial.HS_ION_SIZE - tutorial.zs, tutorial.CONCENTRATION, tutorial.DEBYE_LENGTH, tutorial.POTENTIAL_DIFF / 2.)) / 2. + + tutorial.gouy_chapman_density(tutorial.box_l_z - tutorial.LJ_SIGMA - tutorial.zs, tutorial.CONCENTRATION, tutorial.DEBYE_LENGTH, tutorial.POTENTIAL_DIFF / 2.)) / 2. msg = 'The density profile is not sufficiently equal to PB theory.' np.testing.assert_allclose( charge_profile, @@ -56,7 +56,7 @@ def test_charge_profile(self): def test_capacitance(self): # For low potentials the capacitance should be in line with Graham/DH - # equilibration limiting (2.5 minutes total) + # equilibration performance limiting graham = -tutorial.sigma_vs_phi[:, 0] / ( constants.elementary_charge / (constants.Boltzmann * tutorial.TEMPERATURE)) msg = 'The capacitance at low potentials should be in line with Graham/DH.'