{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Computing mass and charge density on each axis\n",
"\n",
"Here we compute the mass and charge density of water along the three cartesian axes of a fixed-volume unit cell (i.e. from a simulation in the NVT ensemble). \n",
"\n",
"**Last updated:** December 2022 with MDAnalysis 2.4.0-dev0\n",
"\n",
"**Minimum version of MDAnalysis:** 0.17.0\n",
"\n",
"**Packages required:**\n",
" \n",
"* MDAnalysis (Michaud-Agrawal *et al.*, 2011, Gowers *et al.*, 2016)\n",
"* MDAnalysisTests\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:12.926693Z",
"iopub.status.busy": "2021-05-19T06:03:12.926156Z",
"iopub.status.idle": "2021-05-19T06:03:14.201615Z",
"shell.execute_reply": "2021-05-19T06:03:14.202150Z"
},
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.498830621Z",
"start_time": "2023-06-09T12:02:38.566620541Z"
}
},
"outputs": [],
"source": [
"import MDAnalysis as mda\n",
"from MDAnalysis.tests.datafiles import waterPSF, waterDCD\n",
"from MDAnalysis.analysis import lineardensity as lin\n",
"\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Loading files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The test files we are working with are a cube of water."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.208174Z",
"iopub.status.busy": "2021-05-19T06:03:14.207100Z",
"iopub.status.idle": "2021-05-19T06:03:14.365047Z",
"shell.execute_reply": "2021-05-19T06:03:14.365692Z"
},
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.721579406Z",
"start_time": "2023-06-09T12:02:39.539285035Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/pbarletta/mambaforge/envs/guide/lib/python3.9/site-packages/MDAnalysis/coordinates/DCD.py:165: DeprecationWarning: DCDReader currently makes independent timesteps by copying self.ts while other readers update self.ts inplace. This behavior will be changed in 3.0 to be the same as other readers. Read more at https://github.com/MDAnalysis/mdanalysis/issues/3889 to learn if this change in behavior might affect you.\n",
" warnings.warn(\"DCDReader currently makes independent timesteps\"\n"
]
}
],
"source": [
"u = mda.Universe(waterPSF, waterDCD)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`MDAnalysis.analysis.lineardensity.LinearDensity` ([API docs](https://docs.mdanalysis.org/stable/documentation_pages/analysis/lineardensity.html#MDAnalysis.analysis.lineardensity.LinearDensity)) will partition each of your axes into bins of user-specified `binsize` (in angstrom), and give the average mass density and average charge density of your atom group selection. \n",
"\n",
"This analysis is only suitable for a trajectory with a fixed box size. While passing a trajectory with a variable box size will not raise an error, `LinearDensity` will not account for changing dimensions. It will only evaluate the density of your atoms in the bins created from the trajectory frame when the class is first initialised.\n",
"\n",
"Below, we iterate through the trajectory to verify that its box dimensions remain constant."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.370749Z",
"iopub.status.busy": "2021-05-19T06:03:14.369941Z",
"iopub.status.idle": "2021-05-19T06:03:14.376456Z",
"shell.execute_reply": "2021-05-19T06:03:14.376999Z"
},
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.729113515Z",
"start_time": "2023-06-09T12:02:39.720089223Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n",
"[50. 50. 50. 90. 90. 90.]\n"
]
}
],
"source": [
"for ts in u.trajectory:\n",
" print(ts.dimensions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can choose to compute the density of individual atoms, residues, segments, or fragments (groups of bonded atoms with no bonds to any atom outside the group). By default, the grouping is for `atoms`. "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.381930Z",
"iopub.status.busy": "2021-05-19T06:03:14.381082Z",
"iopub.status.idle": "2021-05-19T06:03:14.399906Z",
"shell.execute_reply": "2021-05-19T06:03:14.400301Z"
},
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.792023484Z",
"start_time": "2023-06-09T12:02:39.729952761Z"
}
},
"outputs": [],
"source": [
"density = lin.LinearDensity(u.atoms,\n",
" grouping='atoms').run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The results of the analysis are in `density.results`."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.407350Z",
"iopub.status.busy": "2021-05-19T06:03:14.406373Z",
"iopub.status.idle": "2021-05-19T06:03:14.409688Z",
"shell.execute_reply": "2021-05-19T06:03:14.410098Z"
},
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.809039922Z",
"start_time": "2023-06-09T12:02:39.763907644Z"
}
},
"outputs": [
{
"data": {
"text/plain": "200"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"density.nbins"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.415895Z",
"iopub.status.busy": "2021-05-19T06:03:14.415329Z",
"iopub.status.idle": "2021-05-19T06:03:14.417659Z",
"shell.execute_reply": "2021-05-19T06:03:14.418186Z"
},
"tags": [
"nbval-ignore-output"
],
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.809831995Z",
"start_time": "2023-06-09T12:02:39.807626584Z"
}
},
"outputs": [
{
"data": {
"text/plain": "array([0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0.00053562, 0.00080344, 0.00876945, 0.03507781, 0.00107125,\n 0.00348155, 0.00241031, 0.02791523, 0.04277601, 0.0175389 ,\n 0.00160687, 0.00133906, 0.00026781, 0. , 0.00107125,\n 0.00107125, 0.00053562, 0. , 0.03400656, 0.0196814 ,\n 0.02339659, 0.0135559 , 0.00026781, 0.00107125, 0.00107125,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ])"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"density.results['x']['mass_density']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.422349Z",
"iopub.status.busy": "2021-05-19T06:03:14.421606Z",
"iopub.status.idle": "2021-05-19T06:03:14.424303Z",
"shell.execute_reply": "2021-05-19T06:03:14.424709Z"
},
"tags": [
"nbval-ignore-output"
],
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.810506364Z",
"start_time": "2023-06-09T12:02:39.808001286Z"
}
},
"outputs": [
{
"data": {
"text/plain": "KeysView({'dim': 0, 'slice_volume': 625.0, 'mass_density': array([0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0.00053562, 0.00080344, 0.00876945, 0.03507781, 0.00107125,\n 0.00348155, 0.00241031, 0.02791523, 0.04277601, 0.0175389 ,\n 0.00160687, 0.00133906, 0.00026781, 0. , 0.00107125,\n 0.00107125, 0.00053562, 0. , 0.03400656, 0.0196814 ,\n 0.02339659, 0.0135559 , 0.00026781, 0.00107125, 0.00107125,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ]), 'mass_density_stddev': array([0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0.00107125, 0.00122727, 0.01688797, 0.01691979, 0.00177646,\n 0.00241031, 0.00279604, 0.02179554, 0.02689655, 0.02096112,\n 0.001312 , 0.00133906, 0.00080344, 0. , 0.001312 ,\n 0.001312 , 0.00107125, 0. , 0.01700328, 0.03402765,\n 0.02131476, 0.01957657, 0.00080344, 0.001312 , 0.001312 ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ]), 'charge_density': array([ 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0.00022158, 0.00033237, -0.00033237, -0.00132949, 0.00044316,\n 0.00144029, 0.00099712, -0.00033237, -0.00210503, -0.00066475,\n 0.00066475, 0.00055396, 0.00011079, 0. , 0.00044316,\n 0.00044316, 0.00022158, 0. , -0.00177266, 0.00022158,\n -0.00022158, -0.00033237, 0.00011079, 0.00044316, 0.00044316,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ]), 'charge_density_stddev': array([0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0.00044316, 0.00050771, 0.00099712, 0.00108553, 0.00073491,\n 0.00099712, 0.00115669, 0.00111344, 0.00144029, 0.00112985,\n 0.00054276, 0.00055396, 0.00033237, 0. , 0.00054276,\n 0.00054276, 0.00044316, 0. , 0.00088633, 0.0018406 ,\n 0.00129204, 0.00111344, 0.00033237, 0.00054276, 0.00054276,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ,\n 0. , 0. , 0. , 0. , 0. ]), 'hist_bin_edges': array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ,\n 2.25, 2.5 , 2.75, 3. , 3.25, 3.5 , 3.75, 4. , 4.25,\n 4.5 , 4.75, 5. , 5.25, 5.5 , 5.75, 6. , 6.25, 6.5 ,\n 6.75, 7. , 7.25, 7.5 , 7.75, 8. , 8.25, 8.5 , 8.75,\n 9. , 9.25, 9.5 , 9.75, 10. , 10.25, 10.5 , 10.75, 11. ,\n 11.25, 11.5 , 11.75, 12. , 12.25, 12.5 , 12.75, 13. , 13.25,\n 13.5 , 13.75, 14. , 14.25, 14.5 , 14.75, 15. , 15.25, 15.5 ,\n 15.75, 16. , 16.25, 16.5 , 16.75, 17. , 17.25, 17.5 , 17.75,\n 18. , 18.25, 18.5 , 18.75, 19. , 19.25, 19.5 , 19.75, 20. ,\n 20.25, 20.5 , 20.75, 21. , 21.25, 21.5 , 21.75, 22. , 22.25,\n 22.5 , 22.75, 23. , 23.25, 23.5 , 23.75, 24. , 24.25, 24.5 ,\n 24.75, 25. , 25.25, 25.5 , 25.75, 26. , 26.25, 26.5 , 26.75,\n 27. , 27.25, 27.5 , 27.75, 28. , 28.25, 28.5 , 28.75, 29. ,\n 29.25, 29.5 , 29.75, 30. , 30.25, 30.5 , 30.75, 31. , 31.25,\n 31.5 , 31.75, 32. , 32.25, 32.5 , 32.75, 33. , 33.25, 33.5 ,\n 33.75, 34. , 34.25, 34.5 , 34.75, 35. , 35.25, 35.5 , 35.75,\n 36. , 36.25, 36.5 , 36.75, 37. , 37.25, 37.5 , 37.75, 38. ,\n 38.25, 38.5 , 38.75, 39. , 39.25, 39.5 , 39.75, 40. , 40.25,\n 40.5 , 40.75, 41. , 41.25, 41.5 , 41.75, 42. , 42.25, 42.5 ,\n 42.75, 43. , 43.25, 43.5 , 43.75, 44. , 44.25, 44.5 , 44.75,\n 45. , 45.25, 45.5 , 45.75, 46. , 46.25, 46.5 , 46.75, 47. ,\n 47.25, 47.5 , 47.75, 48. , 48.25, 48.5 , 48.75, 49. , 49.25,\n 49.5 , 49.75, 50. ], dtype=float32)})"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"density.results['x'].keys()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.428405Z",
"iopub.status.busy": "2021-05-19T06:03:14.427650Z",
"iopub.status.idle": "2021-05-19T06:03:14.430358Z",
"shell.execute_reply": "2021-05-19T06:03:14.430951Z"
},
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.810902504Z",
"start_time": "2023-06-09T12:02:39.808816049Z"
}
},
"outputs": [
{
"data": {
"text/plain": "1"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"density.results['y']['dim']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2021-05-19T06:03:14.447065Z",
"iopub.status.busy": "2021-05-19T06:03:14.444367Z",
"iopub.status.idle": "2021-05-19T06:03:14.550228Z",
"shell.execute_reply": "2021-05-19T06:03:14.550713Z"
},
"ExecuteTime": {
"end_time": "2023-06-09T12:02:39.944318607Z",
"start_time": "2023-06-09T12:02:39.809409947Z"
}
},
"outputs": [
{
"data": {
"text/plain": "[]"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": "