Density planar tutorial#

To follow this tutorial, the data test files airwater of MAICoS are needed. You can obtain it by cloning MAICoS repository:

git clone git@gitlab.com:maicos-devel/maicos.git

The airwater data files are located in tests/data/airwater/. First, let us ignore unnecessary warnings:

[1]:
import warnings
warnings.filterwarnings("ignore")

First, import MAICoS, NumPy, MDAnalysis, and PyPlot:

[2]:
import maicos
import numpy as np
import MDAnalysis as mda
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In [2], line 1
----> 1 import maicos
      2 import numpy as np
      3 import MDAnalysis as mda

File ~/checkouts/readthedocs.org/user_builds/maicos/envs/v0.5/lib/python3.10/site-packages/maicos/__init__.py:36
     29 from .modules.density import DensityPlanar, DensityCylinder
     30 from .modules.epsilon import (
     31     EpsilonBulk,
     32     EpsilonPlanar,
     33     EpsilonCylinder,
     34     DielectricSpectrum
     35 )
---> 36 from .modules.structure import Saxs, Debye, Diporder
     37 from .modules.timeseries import DipoleAngle, KineticEnergy
     38 from .modules.transport import Velocity

File ~/checkouts/readthedocs.org/user_builds/maicos/envs/v0.5/lib/python3.10/site-packages/maicos/modules/structure.py:24
     18 from .. import tables
     19 from ..decorators import (
     20     make_whole,
     21     set_planar_class_doc,
     22     set_verbose_doc,
     23 )
---> 24 from ..lib import sfactor
     25 from ..utils import check_compound, savetxt
     27 logger = logging.getLogger(__name__)

ImportError: dynamic module does not define module export function (PyInit_sfactor)

and set a few parameters for plotting purpose:

[3]:
fontsize = 25
font = {'family': 'sans', 'color':  'black',
        'weight': 'normal', 'size': fontsize}
my_color_1 = np.array([0.090, 0.247, 0.560])
my_color_2 = np.array([0.235, 0.682, 0.639])
my_color_3 = np.array([1.000, 0.509, 0.333])
my_color_4 = np.array([0.588, 0.588, 0.588])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [3], line 4
      1 fontsize = 25
      2 font = {'family': 'sans', 'color':  'black',
      3         'weight': 'normal', 'size': fontsize}
----> 4 my_color_1 = np.array([0.090, 0.247, 0.560])
      5 my_color_2 = np.array([0.235, 0.682, 0.639])
      6 my_color_3 = np.array([1.000, 0.509, 0.333])

NameError: name 'np' is not defined

Define the path to the airwater data folder of MAICoS:

[4]:
datapath = "../../../../tests/data/airwater/"

The airwater system consists of a 2D slab with 352 water molecules in vacuum, where the two water/vacuum interfaces are normal to the axis \(z\) :

airwater.png

Create a universe using MDAnalysis and define a group containing the oxygen and the hydrogen atoms of the water molecules, as well as a group containing only the oxygen atoms, and a group containing only the hydrogen atoms:

[5]:
u = mda.Universe(datapath+'conf.gro',
                 datapath+'traj.trr')
group_H2O = u.select_atoms('type O or type H')
group_O = u.select_atoms('type O')
group_H = u.select_atoms('type H')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [5], line 1
----> 1 u = mda.Universe(datapath+'conf.gro',
      2                  datapath+'traj.trr')
      3 group_H2O = u.select_atoms('type O or type H')
      4 group_O = u.select_atoms('type O')

NameError: name 'mda' is not defined

Let us print a few information about the trajectory file:

[6]:
print(f"The number of water molecules is {group_O.n_atoms}")
timestep = np.round(u.trajectory.dt,2)
print(f"The time interval between the frames is {timestep} ps")
total_time = np.round(u.trajectory.totaltime,2)
print(f"The total simulation time is {total_time} ps")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [6], line 1
----> 1 print(f"The number of water molecules is {group_O.n_atoms}")
      2 timestep = np.round(u.trajectory.dt,2)
      3 print(f"The time interval between the frames is {timestep} ps")

NameError: name 'group_O' is not defined

Let us use the DensityPlanar class of MAICos using the group_H2O group:

[7]:
dplan = maicos.DensityPlanar(group_H2O)
dplan.run()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [7], line 1
----> 1 dplan = maicos.DensityPlanar(group_H2O)
      2 dplan.run()

NameError: name 'maicos' is not defined

Extract the coordinate and the density profile from the results attribute:

[8]:
zcoor = dplan.results['z']
dens = dplan.results['dens_mean']
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [8], line 1
----> 1 zcoor = dplan.results['z']
      2 dens = dplan.results['dens_mean']

NameError: name 'dplan' is not defined

By default the binwidth is 0.1 nanometers, the units are kg/m\(^3\), and the axis is \(z\). Plot the density profile using :

[9]:
fig = plt.figure(figsize=(13,6.5))
ax1 = plt.subplot(1, 1, 1)
plt.xlabel("z coordinate (nm)", fontdict=font)
plt.ylabel(r"density H2O (kg/m$^3$]", fontdict=font)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
ax1.plot(zcoor, dens, color=my_color_1, linewidth=4)
ax1.yaxis.offsetText.set_fontsize(20)
ax1.minorticks_on()
ax1.tick_params('both', length=10, width=2, which='major', direction='in')
ax1.tick_params('both', length=6, width=1.4, which='minor', direction='in')
ax1.xaxis.set_ticks_position('both')
ax1.yaxis.set_ticks_position('both')
ax1.spines["top"].set_linewidth(2)
ax1.spines["bottom"].set_linewidth(2)
ax1.spines["left"].set_linewidth(2)
ax1.spines["right"].set_linewidth(2)
ax1.yaxis.offsetText.set_fontsize(30)
minor_locator_y = AutoMinorLocator(2)
ax1.yaxis.set_minor_locator(minor_locator_y)
minor_locator_x = AutoMinorLocator(2)
ax1.xaxis.set_minor_locator(minor_locator_x)
ax1.tick_params(axis='x', pad=10)
plt.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [9], line 1
----> 1 fig = plt.figure(figsize=(13,6.5))
      2 ax1 = plt.subplot(1, 1, 1)
      3 plt.xlabel("z coordinate (nm)", fontdict=font)

NameError: name 'plt' is not defined

They are several options you can play with. To know the full list of options, have a look at the Inputs section in the documentation. For instance, you can increase the spacial resolution by reducing the binwidth:

[10]:
dplan_smaller_bin = maicos.DensityPlanar(group_H2O, binwidth = 0.05)
dplan_smaller_bin.run()
zcoor_smaller_bin = dplan_smaller_bin.results['z']
dens_smaller_bin = dplan_smaller_bin.results['dens_mean']
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [10], line 1
----> 1 dplan_smaller_bin = maicos.DensityPlanar(group_H2O, binwidth = 0.05)
      2 dplan_smaller_bin.run()
      3 zcoor_smaller_bin = dplan_smaller_bin.results['z']

NameError: name 'maicos' is not defined
[11]:
fig = plt.figure(figsize=(13,6.5))
ax1 = plt.subplot(1, 1, 1)
plt.xlabel(r"$z$ coordinate (nm)", fontdict=font)
plt.ylabel(r"density H2O (kg/m$^3$]", fontdict=font)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
ax1.plot(zcoor_smaller_bin, dens_smaller_bin, color=my_color_2, linewidth=4)
ax1.plot(zcoor, dens, color=my_color_1, linewidth=4)
ax1.yaxis.offsetText.set_fontsize(20)
ax1.minorticks_on()
ax1.tick_params('both', length=10, width=2, which='major', direction='in')
ax1.tick_params('both', length=6, width=1.4, which='minor', direction='in')
ax1.xaxis.set_ticks_position('both')
ax1.yaxis.set_ticks_position('both')
ax1.spines["top"].set_linewidth(2)
ax1.spines["bottom"].set_linewidth(2)
ax1.spines["left"].set_linewidth(2)
ax1.spines["right"].set_linewidth(2)
ax1.yaxis.offsetText.set_fontsize(30)
minor_locator_y = AutoMinorLocator(2)
ax1.yaxis.set_minor_locator(minor_locator_y)
minor_locator_x = AutoMinorLocator(2)
ax1.xaxis.set_minor_locator(minor_locator_x)
ax1.tick_params(axis='x', pad=10)
plt.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [11], line 1
----> 1 fig = plt.figure(figsize=(13,6.5))
      2 ax1 = plt.subplot(1, 1, 1)
      3 plt.xlabel(r"$z$ coordinate (nm)", fontdict=font)

NameError: name 'plt' is not defined

Note : less noisy profile can be obtained by running longer simulations.

MAICoS can deal with several groups at once:

[12]:
dplan_separate = maicos.DensityPlanar([group_O, group_H], binwidth = 0.05)
dplan_separate.run()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [12], line 1
----> 1 dplan_separate = maicos.DensityPlanar([group_O, group_H], binwidth = 0.05)
      2 dplan_separate.run()

NameError: name 'maicos' is not defined

In this case, respective results for each group are returned:

[13]:
zcoor_separate = dplan_smaller_bin.results['z']
dens_oxygen = dplan_separate.results['dens_mean'].T[0]
dens_hygrogen = dplan_separate.results['dens_mean'].T[1]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [13], line 1
----> 1 zcoor_separate = dplan_smaller_bin.results['z']
      2 dens_oxygen = dplan_separate.results['dens_mean'].T[0]
      3 dens_hygrogen = dplan_separate.results['dens_mean'].T[1]

NameError: name 'dplan_smaller_bin' is not defined
[14]:
fig = plt.figure(figsize=(13,6.5))
ax1 = plt.subplot(1, 1, 1)
plt.xlabel(r"$z$ coordinate (nm)", fontdict=font)
plt.ylabel(r"density H2O (kg/m$^3$]", fontdict=font)
plt.xticks(fontsize=fontsize)
plt.yticks(fontsize=fontsize)
ax1.plot(zcoor_separate, dens_hygrogen, color=my_color_4, linewidth=4)
ax1.plot(zcoor_separate, dens_oxygen, color=my_color_3, linewidth=4)
ax1.yaxis.offsetText.set_fontsize(20)
ax1.minorticks_on()
ax1.tick_params('both', length=10, width=2, which='major', direction='in')
ax1.tick_params('both', length=6, width=1.4, which='minor', direction='in')
ax1.xaxis.set_ticks_position('both')
ax1.yaxis.set_ticks_position('both')
ax1.spines["top"].set_linewidth(2)
ax1.spines["bottom"].set_linewidth(2)
ax1.spines["left"].set_linewidth(2)
ax1.spines["right"].set_linewidth(2)
ax1.yaxis.offsetText.set_fontsize(30)
minor_locator_y = AutoMinorLocator(2)
ax1.yaxis.set_minor_locator(minor_locator_y)
minor_locator_x = AutoMinorLocator(2)
ax1.xaxis.set_minor_locator(minor_locator_x)
ax1.tick_params(axis='x', pad=10)
plt.show()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In [14], line 1
----> 1 fig = plt.figure(figsize=(13,6.5))
      2 ax1 = plt.subplot(1, 1, 1)
      3 plt.xlabel(r"$z$ coordinate (nm)", fontdict=font)

NameError: name 'plt' is not defined