-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline-average.py
133 lines (98 loc) · 4.02 KB
/
line-average.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/home/balazs/.conda/envs/mdanalysis/bin/python
import argparse
import numpy as np
import pyvista as pv
class CustomFormatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawTextHelpFormatter):
pass
def CheckAverageDirection(ax):
""" Check axis in 'x'/'y'.
"""
ax = ax.lower()
if ax not in ['x','y','radial']:
raise ValueError()
return ax
desc = """Calculate averages over the specified directions
The possible option are:
* 'x': Average along the x axis
* 'y': Average along the y axis
* 'radial': Radial average
"""
parser = argparse.ArgumentParser(description=desc, formatter_class=CustomFormatter)
parser.add_argument('mesh')
parser.add_argument('--direction', default='x', type=CheckAverageDirection, help="Direction for averaging")
parser.add_argument('--bins', default=50, type=int, help="Number of bins")
parser.add_argument('--name', default='mesh', type=str, help="Stem of the output names")
parser.add_argument('--property', default='msd', type=str, help="Property to be processed")
args = parser.parse_args()
mesh = pv.read(args.mesh)
lx = mesh.field_arrays['lx'][0]
ly = mesh.field_arrays['ly'][0]
# project onto the xy plane
mesh.project_points_to_plane(origin=(0,0,0), normal=(0,0,1),inplace=True)
# Calculate MSD for all lagtimes ------------------------------
if args.property == 'msd':
lagtimes = mesh.field_arrays['lagtimes']
# calculate the relevant positions
if args.direction == 'radial':
mesh.points[:,0] -= lx/2
mesh.points[:,1] -= ly/2
positions = np.linalg.norm(mesh.points,axis=1)
hist_range = [0,min(lx,ly)/2]
if args.direction == 'x':
positions = mesh.points[:,0]
hist_range = [0, lx]
if args.direction == 'y':
positions = mesh.points[:,1]
hist_range = [0, ly]
# Take the non-normalized values, and bin them
for dt in lagtimes:
dsum = mesh.point_arrays[f'dsum={dt}']
csum = mesh.point_arrays[f'csum={dt}']
# filter out NaN-s
mask = np.isnan(dsum)
dsum = dsum[~mask]
csum = csum[~mask]
p = positions[~mask]
# histogram
hist, edges = np.histogram(p, bins=args.bins, weights=dsum, range=hist_range)
norm, _ = np.histogram(p, bins=args.bins, weights=csum, range=hist_range)
values = np.divide(hist,norm)
centers = (edges[:-1] + edges[1:])/2
data = np.vstack((centers,values)).T
np.savetxt(f"{args.name}-{args.property}-{args.direction}-{dt}.dat", data)
# Calculate Mean or Gaussian curvature for all lagtimes ----------
elif args.property in ['Mean','Gaussian','Density']:
# calculate the relevant positions
# * use centers of the faces
points = mesh.cell_centers().points
if args.direction == 'radial':
points[:,0] -= lx/2
points[:,1] -= ly/2
positions = np.linalg.norm(points,axis=1)
hist_range = [0,min(lx,ly)/2]
if args.direction == 'x':
positions = points[:,0]
hist_range = [0, lx]
if args.direction == 'y':
positions = points[:,1]
hist_range = [0, ly]
if args.property in ['Mean','Gaussian']:
# covert point property to cell property
prop = mesh.ptc().cell_arrays[args.property]
else:
prop = mesh['Density']
area = mesh.compute_cell_sizes(length=False,area=True,volume=False).cell_arrays['Area']
# filter out NaN-s
mask = np.isnan(prop)
prop = prop[~mask]
area = area[~mask]
p = positions[~mask]
# area-weight the densities to particle numbers
prop = np.multiply(prop,area)
# area-weighted curvature values
hist, edges = np.histogram(p, bins=args.bins, weights=prop, range=hist_range)
norm, _ = np.histogram(p, bins=args.bins, weights=area, range=hist_range)
values = np.divide(hist,norm)
centers = (edges[:-1] + edges[1:])/2
data = np.vstack((centers,values)).T
np.savetxt(f"{args.name}-{args.property}-{args.direction}.dat", data)