-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdensity_2D.py
More file actions
55 lines (40 loc) · 1.4 KB
/
density_2D.py
File metadata and controls
55 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import MDAnalysis as mda
from MDAnalysis.analysis import density
import matplotlib.pyplot as plt
# Input file
file = "input.xyz"
# Create Universe object
u = mda.Universe(file)
def density_2D(u):
"""
Calculates and visualizes the 2D average density of water in a simulation box.
Args:
u (Universe): MDAnalysis Universe object.
Returns:
None
"""
# Define the maximum Z coordinate of the box
z_hi = "Enter maximum box Z value"
# Select oxygen atoms in the desired region (above z_hi)
ow = u.select_atoms("name O1 and prop {} > z".format(z_hi))
# Perform density analysis
dens = density.DensityAnalysis(ow, delta=3.5, padding=0)
dens.run()
# Get the density grid and convert it to SPC/E units
grid = dens.results.density.grid
dens.results.density.convert_density('SPC')
# Calculate the average density along the Z axis (top view)
avg = grid.mean(axis=-1)
# Create the figure and axes
fig, ax = plt.subplots(figsize=(10, 8))
# Create the heat map of the average density
im = ax.imshow(avg, interpolation="bicubic", cmap='jet', vmin=0, vmax=1)
# Add a colorbar
cbar = plt.colorbar(im)
cbar.set_label('Mean density of water over SPC/E literature value')
# Axis labels
plt.xlabel('X-axis ($\AA$)')
plt.ylabel('Y-axis ($\AA$)')
# Call the function to perform the analysis
density_2D(u)