-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.m
212 lines (185 loc) · 6.9 KB
/
run.m
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This file runs the calculation for contact angle of a liquid droplet
% on a solid surface using the method of Khalkhali et al. In this method
% contact angle is calculated through convex hull triangulation (see
% the referenced Publication for more details)
%
% Authors:
% Mohammad Khalkhali
% Nasser Kazemi
% (Sep 2016)
%
% References:
% Khalkhali et al. J. Chem. Phys. (2017)
%
% Functions:
% read_LAMMPS_traj: reads a LAMMPS trajectory file and returns the
% positions of centre of masses of water molecules.
% hit_and_count: runs hin and count algorithm to identify points in liquid
% droplet from thos in the gas phase.
% fine_percision: applied the fine precision droplet identification process
% to remove near-droplet gas molecules.
% contact_angle: calculates the contact angles along the contact line using
% the convex hul triangulation. This function also returns the area of the
% base of the droplet and the area of corresponding triangles used to
% calculate each contact angle value.
% weighted_distribution: calculates weighted histogram of contact angles
% according to the area of correcponding triangles
%
% This program is a free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published
% by the Free Software Foundation, either version 3 of the License, or
% any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details: http://www.gnu.org/licenses/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear all;
fclose ('all');
close('all');
trajName = '../Graphite_Water(3nm)/Graphite_Water.lammpstrj';
% This format_spec is needed when reading lammps trajectories in
% read_LAMMPS_traj function. There are 5 entries at each line of our
% trajectories
format_spec = '%f %f %f %f %f';
StartStep = 4990000;
EndStep = 5000000;
step = 1000;
% Parameters of hit-and-count function (refer to the publication for details)
binsize=2;
numer_atom_in_bin=5;
% Parameters of fine_percision function (refer to the publication for details)
% fine presicion step of identifying droplet limits is usually redundant
% (hit and count step is sufficient).
fine_percision_check = 0; % calls fine_percision function if is 1
delta_R = 5;
R_step = 1;
% Draws graphs corresponding to each step for the first time step. It is
% recommended to check if parameters droplet identification process are
% ajusted properly.
graphcheck = 0;
% Triangles with atleast one vertex position lower that z_min*max(z) are
% contributing in the contact angle calculation.
z_min = 0.08;
% This parameter is used to recognize triangles contributing to the area of
% the base of the droplet.
precision = 1.0;
Distribution=[];
Weight=[];
BaseA = 0;
for tt = StartStep:step:EndStep
fprintf('Current time step = %d\r', tt);
Filename = sprintf('%s.%d',trajName,tt); %trajectory file name
% Reading box dimentions to calculates centre of mass of water
% molecules correctly: some water molecules may pass the boundary.
% Since it is a NVT simulation we just need to read box dimentions
% once.
if (tt == StartStep)
fileID = fopen(Filename,'r');
if (fileID < 0)
fprintf('can not open %s file',Filename);
break;
end
%reading box sizes
header = textscan(fileID, '%s',9,'delimiter', '\n');
data = textscan(header{1}{6},'%f %f');
Box_x = data{2}-data{1};
data = textscan(header{1}{7},'%f %f');
Box_y = data{2}-data{1};
data = textscan(header{1}{8},'%f %f');
Box_z = data{2}-data{1};
fclose(fileID);
end
[fileID,x_org,y_org,z_org] = read_LAMMPS_traj(Filename,Box_x,...
Box_y,Box_z,format_spec);
if (fileID < 0)
fprintf('can not open %s file',Filename);
break;
end
% Drawing the droplet before applying hit-and-run
if (tt == StartStep && graphcheck == 1)
figure;
scatter3(x_org,y_org,z_org,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0 .25 .25]);
view(45,45)
title('original');
size1 = size(x_org,1);
axis tight
end
% Applying hit and count method to remove outliers
[x_final,y_final,z_final] = hit_and_count(x_org,...
y_org,z_org,binsize,numer_atom_in_bin);
if (tt == StartStep && graphcheck == 1)
figure;
scatter3(x_final,y_final,z_final,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0 .25 .25]);
view(45,45)
size2 = size(x_final,1);
str = sprintf('After hit-and-count (%d points were removed)', ...
size1-size2);
title(str);
axis tight
end
% Applying fine precission method to remove near droplet outliers
if (fine_percision_check == 1)
[x_final,y_final,z_final] = fine_percision(x_final,y_final,...
z_final,R_step,delta_R);
end
if (tt == StartStep && graphcheck == 1)
figure;
scatter3(x_final,y_final,z_final,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0 .25 .25]);
view(45,45)
size3 = size(x_final,1);
str = sprintf('After hit-and-count and fine-percision (%d points were removed)', ...
size2-size3);
title(str);
axis tight
end
% Calculating contact angle distribution and interfacial area using convex hull
[K,V,angle,BaseA_temp] = contact_angle(x_final,y_final,z_final,...
z_min,precision);
if (tt == StartStep && graphcheck == 1)
figure;
hold on;
trisurf(K,x_final,y_final,z_final,'facecolor','r','facealpha',0.5)
scatter3(x_final,y_final,z_final,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0 .75 .75], ...
'MarkerFaceAlpha',0.5);
view(45,45)
axis tight
end
BaseA = BaseA + BaseA_temp;
Distribution = cat(2,Distribution,angle(1,:));
Weight = cat(2,Weight,angle(2,:));
end
fprintf('\n');
% Calculating weighted histogram of angular values
w = weighted_distribution(Distribution,Weight);
BaseA = 0.5*BaseA/((EndStep-StartStep)/step+1);
angle = 0:180;
s0=w;
s1=conv(w,hanning(30),'same');
theta_ave = sum(angle.*w)/sum(w);
% drawing angular distribution
% drawing angular distribution
figure;
plot(0:180,100*s1/sum(s1));
xlabel('Contact Angle (degree)');
ylabel('Probability (%)');
str(1) = {'Khalkhali et al. meathod'};
str(2) = {sprintf('\\theta_{ave}=%f',theta_ave)};
title(str);
file(:,1)=0:180;
file(:,2)=s0;
file(:,3)=s1;
output = sprintf('../Results/MK.txt');
dlmwrite(output,file,'delimiter','\t');
% toc