# Tutorial 2: A Simple Charged System, Part 1¶

## 1 Introduction¶

This tutorial introduces some of the basic features of ESPResSo for charged systems by constructing a simulation script for a simple salt crystal. In the subsequent task, we use a more realistic force-field for a NaCl crystal. Finally, we introduce constraints and 2D-Electrostatics to simulate a molten salt in a parallel plate capacitor. We assume that the reader is familiar with the basic concepts of Python and MD simulations. Compile espresso with the following features in your myconfig.hpp to be set throughout the whole tutorial:

#define EXTERNAL_FORCES
#define MASS
#define ELECTROSTATICS
#define LENNARD_JONES

## 2 Basic Set Up¶

The script for the tutorial can be found in your build directory at /doc/tutorials/02-charged_system/scripts/nacl.py. We start with importing numpy, pyplot, and the espressomd features and setting up all the relevant simulation parameters in one place:

In [1]:
from __future__ import print_function
from espressomd import System, electrostatics, features
import espressomd
import numpy
import matplotlib.pyplot as plt
plt.ion()

# Print enabled features
required_features = ["EXTERNAL_FORCES", "MASS", "ELECTROSTATICS", "LENNARD_JONES"]
espressomd.assert_features(required_features)
print(espressomd.features())

# System Parameters
n_part = 200
n_ionpairs = n_part/2
density = 0.5
time_step = 0.01
temp = 1.0
gamma = 1.0
l_bjerrum = 7.0

num_steps_equilibration = 1000
num_configs = 500
integ_steps_per_config = 1000

# Particle Parameters
types       = {"Anion":          0, "Cation": 1}
numbers     = {"Anion": n_ionpairs, "Cation": n_ionpairs}
charges     = {"Anion":       -1.0, "Cation": 1.0}
lj_sigmas   = {"Anion":        1.0, "Cation": 1.0}
lj_epsilons = {"Anion":        1.0, "Cation": 1.0}

WCA_cut = 2.**(1. / 6.)
lj_cuts     = {"Anion":  WCA_cut * lj_sigmas["Anion"],
"Cation": WCA_cut * lj_sigmas["Cation"]}

['ELECTROSTATICS', 'EXTERNAL_FORCES', 'FFTW', 'LATTICE', 'LB', 'LB_BOUNDARIES', 'LENNARD_JONES', 'MASS', 'P3M', 'ROTATION', 'ROTATIONAL_INERTIA', 'SWIMMER_REACTIONS', 'VIRTUAL_SITES', 'VIRTUAL_SITES_RELATIVE']


These variables do not change anything in the simulation engine, but are just standard Python variables. They are used to increase the readability and flexibility of the script. The box length is not a parameter of this simulation, it is calculated from the number of particles and the system density. This allows to change the parameters later easily, e. g. to simulate a bigger system. We use dictionaries for all particle related parameters, which is less error-prone and readable as we will see later when we actually need the values. The parameters here define a purely repulsive, equally sized, monovalent salt.

The simulation engine itself is modified by changing the espressomd.System() properties. We create an instance system and set the box length, periodicity and time step. The skin depth skin is a parameter for the link--cell system which tunes its performance, but shall not be discussed here. Further, we activate the Langevin thermostat for our NVT ensemble with temperature temp and friction coefficient gamma.

In [2]:
# Setup System
box_l = (n_part / density)**(1. / 3.)
system = System(box_l = [box_l, box_l, box_l])
system.seed=42
system.periodicity = [1, 1, 1]
system.time_step = time_step
system.cell_system.skin = 0.3
system.thermostat.set_langevin(kT=temp, gamma=gamma)


We now fill this simulation box with particles at random positions, using type and charge from our dictionaries. Using the length of the particle list system.part for the id, we make sure that our particles are numbered consecutively. The particle type is used to link non-bonded interactions to a certain group of particles.

In [3]:
for i in range(int(n_ionpairs)):
id=len(system.part),
type=types["Anion"],
pos=numpy.random.random(3) * box_l,
q=charges["Anion"])
for i in range(int(n_ionpairs)):
id=len(system.part),
type=types["Cation"],
pos=numpy.random.random(3) * box_l,
q=charges["Cation"])


Before we can really start the simulation, we have to specify the interactions between our particles. We already defined the Lennard-Jones parameters at the beginning, what is left is to specify the combination rule and to iterate over particle type pairs. For simplicity, we implement only the Lorentz-Berthelot rules. We pass our interaction pair to system.non_bonded_inter[\*,\*] and set the pre-calculated LJ parameters epsilon, sigma and cutoff. With shift="auto", we shift the interaction potential to the cutoff so that $U_\mathrm{LJ}(r_\mathrm{cutoff})=0$.

In [4]:
def combination_rule_epsilon(rule, eps1, eps2):
if rule=="Lorentz":
return (eps1*eps2)**0.5
else:
return ValueError("No combination rule defined")

def combination_rule_sigma(rule, sig1, sig2):
if rule=="Berthelot":
return (sig1+sig2)*0.5
else:
return ValueError("No combination rule defined")

# Lennard-Jones interactions parameters
for s in [["Anion", "Cation"], ["Anion", "Anion"], ["Cation", "Cation"]]:
lj_sig = combination_rule_sigma("Berthelot",lj_sigmas[s[0]], lj_sigmas[s[1]])
lj_cut = combination_rule_sigma("Berthelot", lj_cuts[s[0]], lj_cuts[s[1]])
lj_eps = combination_rule_epsilon("Lorentz", lj_epsilons[s[0]],lj_epsilons[s[1]])

system.non_bonded_inter[types[s[0]], types[s[1]]].lennard_jones.set_params(
epsilon=lj_eps, sigma=lj_sig, cutoff=lj_cut, shift="auto")


## 3 Equilibration¶

With randomly positioned particles, we most likely have huge overlap and the strong repulsion will cause the simulation to crash. The next step in our script therefore is a suitable LJ equilibration. This is known to be a tricky part of a simulation and several approaches exist to reduce the particle overlap. Here, we use a highly damped system (large gamma in the thermostat) and cap the forces of the LJ interaction. We use system.analysis.mindist to get the minimal distance between all particles pairs. This value is used to progressively increase the force capping. This results in a slow increase of the force capping at strong overlap. At the end, we reset our thermostat to the target values and deactivate the force cap by setting it to zero.

In [5]:
# Lennard Jones Equilibration
max_sigma = max(lj_sigmas.values())
min_dist = 0.0
cap = 10.0
# Warmup Helper: Cold, highly damped system
system.thermostat.set_langevin(kT=temp*0.1, gamma=gamma*50.0)

while min_dist < max_sigma:
#Warmup Helper: Cap max. force, increase slowly for overlapping particles
min_dist = system.analysis.min_dist([types["Anion"],types["Cation"]],[types["Anion"],types["Cation"]])
cap += min_dist
#print min_dist, cap
system.force_cap=cap
system.integrator.run(10)

# Don't forget to reset thermostat, timestep and force cap
system.thermostat.set_langevin(kT=temp, gamma=gamma)
system.force_cap=0


ESPResSo uses so-called actors for electrostatics, magnetostatics and hydrodynamics. This ensures that unphysical combinations of algorithms are avoided, for example simultaneous usage of two electrostatic interactions. Adding an actor to the system also activates the method and calls necessary initialization routines. Here, we define a P$^3$M object with parameters Bjerrum length and rms force error . This automatically starts a tuning function which tries to find optimal parameters for P$^3$M and prints them to the screen:

In [6]:
p3m = electrostatics.P3M(prefactor=l_bjerrum*temp,
accuracy=1e-3)

P3M tune parameters: Accuracy goal = 1.00000e-03 prefactor = 7.00000e+00
System: box_l = 7.36806e+00 # charged part = 200 Sum[q_i^2] = 2.00000e+02
mesh cao r_cut_iL     alpha_L      err          rs_err     ks_err     time [ms]
6    7   cao too large for this mesh
8    7   4.59284e-01 6.50911e+00 8.77128e-02 7.071e-04 8.771e-02 accuracy not achieved
8    7   4.59284e-01 6.50911e+00 8.77128e-02 7.071e-04 8.771e-02 accuracy not achieved
8    7   4.59284e-01 6.50911e+00 8.77128e-02 7.071e-04 8.771e-02 accuracy not achieved
10   7   4.59284e-01 6.50911e+00 1.39625e-02 7.071e-04 1.394e-02 accuracy not achieved
10   7   4.59284e-01 6.50911e+00 1.39625e-02 7.071e-04 1.394e-02 accuracy not achieved
10   7   4.59284e-01 6.50911e+00 1.39625e-02 7.071e-04 1.394e-02 accuracy not achieved
12   7   4.59284e-01 6.50911e+00 2.51196e-03 7.071e-04 2.410e-03 accuracy not achieved
12   7   4.59284e-01 6.50911e+00 2.51196e-03 7.071e-04 2.410e-03 accuracy not achieved
12   7   4.59284e-01 6.50911e+00 2.51196e-03 7.071e-04 2.410e-03 accuracy not achieved
14   7   4.44034e-01 6.73901e+00 9.96813e-04 7.071e-04 7.026e-04 1.43
14   6   4.59284e-01 6.50911e+00 1.30343e-03 7.071e-04 1.095e-03 accuracy not achieved
14   7   4.44034e-01 6.73901e+00 9.96813e-04 7.071e-04 7.026e-04 1.58
14   6   4.44034e-01 6.73901e+00 1.65807e-03 7.071e-04 1.500e-03 accuracy not achieved
16   7   3.92866e-01 7.64271e+00 9.88469e-04 7.071e-04 6.907e-04 1.42
16   6   4.26689e-01 7.02075e+00 9.95097e-04 7.071e-04 7.002e-04 1.35
16   5   4.44034e-01 6.73901e+00 1.71326e-03 7.071e-04 1.561e-03 accuracy not achieved
16   6   4.26689e-01 7.02075e+00 9.95097e-04 7.071e-04 7.002e-04 1.39
16   5   4.26689e-01 7.02075e+00 2.19825e-03 7.071e-04 2.081e-03 accuracy not achieved
16   7   3.92521e-01 7.64963e+00 9.93284e-04 7.071e-04 6.976e-04 1.72
16   6   4.26689e-01 7.02075e+00 9.95097e-04 7.071e-04 7.002e-04 1.43
16   5   4.26689e-01 7.02075e+00 2.19825e-03 7.071e-04 2.081e-03 accuracy not achieved
16   7   3.92521e-01 7.64963e+00 9.93284e-04 7.071e-04 6.976e-04 1.80
18   6   3.83353e-01 7.83768e+00 9.90888e-04 7.071e-04 6.942e-04 2.42
18   5   4.26689e-01 7.02075e+00 1.20437e-03 7.071e-04 9.749e-04 accuracy not achieved
18   7   3.51685e-01 8.56382e+00 9.96029e-04 7.071e-04 7.015e-04 2.45
18   6   3.82605e-01 7.85345e+00 9.99544e-04 7.071e-04 7.065e-04 2.27
18   5   3.83353e-01 7.83768e+00 2.20682e-03 7.071e-04 2.090e-03 accuracy not achieved
18   7   3.51906e-01 8.55828e+00 9.92553e-04 7.071e-04 6.965e-04 1.95
18   6   3.51906e-01 8.55828e+00 1.67651e-03 7.071e-04 1.520e-03 accuracy not achieved
18   7   3.51906e-01 8.55828e+00 9.92553e-04 7.071e-04 6.965e-04 1.79
18   6   3.51906e-01 8.55828e+00 1.67651e-03 7.071e-04 1.520e-03 accuracy not achieved
20   6   3.47783e-01 8.66258e+00 9.93918e-04 7.071e-04 6.985e-04 2.38
20   5   3.51906e-01 8.55828e+00 2.07358e-03 7.071e-04 1.949e-03 accuracy not achieved
20   7   3.18915e-01 9.46921e+00 9.95853e-04 7.071e-04 7.012e-04 2.64
20   6   3.47783e-01 8.66258e+00 9.93918e-04 7.071e-04 6.985e-04 2.19
20   5   3.47783e-01 8.66258e+00 2.23738e-03 7.071e-04 2.123e-03 accuracy not achieved
20   7   3.19253e-01 9.45890e+00 9.90030e-04 7.071e-04 6.929e-04 1.77
22   6   3.18630e-01 9.47792e+00 9.94477e-04 7.071e-04 6.993e-04 2.48
22   5   3.19253e-01 9.45890e+00 2.22894e-03 7.071e-04 2.114e-03 accuracy not achieved
22   7   2.91818e-01 1.03737e+01 9.97418e-04 7.071e-04 7.035e-04 2.90
22   6   3.18630e-01 9.47792e+00 9.94477e-04 7.071e-04 6.993e-04 2.31
22   5   3.18630e-01 9.47792e+00 2.25749e-03 7.071e-04 2.144e-03 accuracy not achieved
22   7   2.91870e-01 1.03718e+01 9.96422e-04 7.071e-04 7.020e-04 2.42
22   6   3.18630e-01 9.47792e+00 9.94477e-04 7.071e-04 6.993e-04 2.33
22   5   3.18630e-01 9.47792e+00 2.25749e-03 7.071e-04 2.144e-03 accuracy not achieved
22   7   2.91870e-01 1.03718e+01 9.96422e-04 7.071e-04 7.020e-04 2.41
24   6   2.94359e-01 1.02817e+01 9.91929e-04 7.071e-04 6.956e-04 3.23
24   5   3.18630e-01 9.47792e+00 1.41456e-03 7.071e-04 1.225e-03 accuracy not achieved
24   7   2.69466e-01 1.12585e+01 9.91213e-04 7.071e-04 6.946e-04 3.63
24   6   2.94359e-01 1.02817e+01 9.91929e-04 7.071e-04 6.956e-04 3.69
24   5   2.94359e-01 1.02817e+01 2.26572e-03 7.071e-04 2.153e-03 accuracy not achieved
24   7   2.69063e-01 1.12759e+01 9.99480e-04 7.071e-04 7.064e-04 3.78

resulting parameters:
16   16   16   6   4.26689e-01 7.02075e+00 9.95097e-04 1.35



Before the production part of the simulation, we do a quick temperature equilibration. For the output, we gather all energies with system.analysis.energy(), calculate the "current" temperature from the ideal part and print it to the screen along with the total and Coulomb energies. Note that for the ideal gas the temperature is given via $1/2 m \sqrt{\langle v^2 \rangle}=3/2 k_BT$, where $\langle \cdot \rangle$ denotes the ensemble average. Calculating some kind of "current temperature" via $T_\text{cur}=\frac{m}{3 k_B} \sqrt{ v^2 }$ you do not obtain the temperature in the system. Only when averaging the squared velocities first one would obtain the temperature for the ideal gas. $T$ is a fixed quantity and does not fluctuate in the canonical ensemble.

We integrate for a certain amount of steps with system.integrator.run(100).

In [7]:
# Temperature Equilibration
system.time = 0.0
for i in range(int(num_steps_equilibration/50)):
energy = system.analysis.energy()
temp_measured = energy['kinetic'] / ((3.0 / 2.0) * n_part)
print("t={0:.1f}, E_total={1:.2f}, E_coulomb={2:.2f},T={3:.4f}".format(system.time, energy['total'],
energy['coulomb'], temp_measured), end='\r')
system.integrator.run(200)

t=38.0, E_total=-517.91, E_coulomb=-869.73,T=0.9352


## 4 Running the Simulation¶

Now we can integrate the particle trajectories for a couple of time steps. Our integration loop basically looks like the equilibration:

In [8]:
# Integration
system.time = 0.0
for i in range(num_configs):
energy = system.analysis.energy()
temp_measured = energy['kinetic'] / ((3.0 / 2.0) * n_part)
print("t={0:.1f}, E_total={1:.2f}, E_coulomb={2:.2f}, T={3:.4f}".format(system.time, energy['total'],
energy['coulomb'], temp_measured), end='\r')
system.integrator.run(integ_steps_per_config)

# Internally append particle configuration
system.analysis.append()

t=4990.0, E_total=-467.30, E_coulomb=-845.54, T=1.0084


Additionally, we append all particle configurations in the core with system.analysis.append() for a very convenient analysis later on.

## 5 Analysis¶

Now, we want to calculate the averaged radial distribution functions $g_{++}(r)$ and $g_{+-}(r)$ with the rdf() command from system.analysis:

In [9]:
# Analysis
# Calculate the averaged rdfs
rdf_bins = 100
r_min  = 0.0
r_max  = system.box_l[0]/2.0
r,rdf_00 = system.analysis.rdf(rdf_type='<rdf>',
type_list_a=[types["Anion"]],
type_list_b=[types["Anion"]],
r_min=r_min,
r_max=r_max,
r_bins=rdf_bins)
r,rdf_01 = system.analysis.rdf(rdf_type='<rdf>',
type_list_a=[types["Anion"]],
type_list_b=[types["Cation"]],
r_min=r_min, r_max=r_max, r_bins=rdf_bins)


The shown rdf() commands return the radial distribution functions for equally and oppositely charged particles for specified radii and number of bins. In this case, we calculate the averaged rdf of the stored configurations, denoted by the chevrons in rdf_type='$<\mathrm{rdf}>$'. Using rdf_type='rdf' would simply calculate the rdf of the current particle configuration. The results are two NumPy arrays containing the $r$ and $g(r)$ values. We can then write the data into a file with standard python output routines.

In [10]:
with open('rdf.data', 'w') as rdf_fp:
for i in range(rdf_bins):
rdf_fp.write("%1.5e %1.5e %1.5e\n" %
(r[i], rdf_00[i], rdf_01[i]))


Finally we can plot the two radial distribution functions using pyplot.

In [11]:
# Plot the distribution functions
plt.figure(figsize=(10,6), dpi=80)
plt.plot(r[:],rdf_00[:], label='$g(r)_{++}$')
plt.plot(r[:],rdf_01[:], label='$g(r)_{+-}$')
plt.xlabel('$r$', fontsize=20)
plt.ylabel('$g(r)$', fontsize=20)
plt.legend(fontsize=20)
plt.show()


## 6 Task - Real Units¶

So far, the system has arbitrary units and is not connected to any real physical system. Simulate a proper NaCl crystal with the force field parameter taken from:

R. Fuentes-Azcatl and M. Barbosa, Sodium Chloride, NaCl/$\epsilon$ : New Force Field, J. Phys. Chem. B, 2016, 120(9), pp 2460-2470

Ion $q/\mathrm{e}$ $\sigma/\mathrm{\mathring{A}}$ $(\epsilon/\mathrm{k_B})/\mathrm{K}$ $m/\mathrm{u}$
Na +1 2.52 17.44 22.99
Cl -1 3.85 192.45 35.453

Use the following system parameters:

Parameter Value
Temperature $298\ \mathrm{K}$
Fiction Coeff. $10\ \mathrm{ps}^{-1}$
Density $1.5736\ \mathrm{ \mu \mathring{A}}^{-3}$
Bjerrum Length (298 K) $439.2\ \mathrm{\mathring{A}}$
Time Step $2\ \mathrm{fs}$

To make your life more easy, don't try to equilibrate randomly positioned particles, but set them up in a crystal structure close to equilibrium. If you do it right, you don't even need the Lennard-Jones equilibration. To speed things up, don't go further than 1000 particles and use a P$^3$M accuracy of $10^{-2}$. Your RDF should look like the plot in figure 2. When you get stuck, you can look at the solution script /doc/tutorials/02-charged_system/scripts/nacl_units.py (or nacl_units_vis.py with visualization).