/
shanchen_ex2.m
139 lines (112 loc) · 3.19 KB
/
shanchen_ex2.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
%% periodic flow my version
clear all;
clc;
% Macroscopic density and velocities
NX=64;
NY=64;
NPOP=9;
NSTEPS=500;
NOUTPUT=10;
% Parameters of the Shan-Chen model
rho_crit=log(2)
rho_liq=2.7
rho_gas=0.05
G=-6.0
% Initialization
rho=ones(NX,NY);
ux=zeros(NX,NY);
uy=zeros(NX,NY);
feq=zeros(NPOP);
f1=zeros(NPOP,NX,NY);
f2=zeros(NPOP,NX,NY);
forcex=zeros(NX,NY);
forcey=zeros(NY,NY);
% Parameters of the lattice
weights=[4/9 1/9 1/9 1/9 1/9 1/36 1/36 1/36 1/36];
cx=[0 1 0 -1 0 1 -1 -1 1];
cy=[0 0 1 0 -1 1 1 -1 -1];
omega=1.0;
for y=1:NY
for x=1:NX
if (y>=NY/2)
rho(x,y)=rho_gas;
else
rho(x,y)=rho_liq;
end
ux(x,y)=0;
uy(x,y)=0;
vx=ux(x,y);
vy=uy(x,y);
for k=1:NPOP
feq(k)=weights(k)*rho(x,y)*(1+3*(vx*cx(k)+vy*cy(k)) ...
+ 9/2*((cx(k)*cx(k)-1/3)*vx*vx+2*cx(k)*cy(k)*vx*vy+(cy(k)*cy(k)-1/3)*vy*vy));
f1(k,x,y)=feq(k);
f2(k,x,y)=feq(k);
end
end
end
counter_frame=1;
for counter=1:NSTEPS
% Calculation of the macroscopic quantities
for y=1:NY
for x=1:NX
dense=0;
vx=0;
vy=0;
for k=1:NPOP
dense=dense+f1(k,x,y);
vx=vx+cx(k)*f1(k,x,y);
vy=vy+cy(k)*f1(k,x,y);
end
rho(x,y)=dense;
ux(x,y)=vx/dense;
uy(x,y)=vy/dense;
end
end
% Calculation of the force
for y=1:NY
for x=1:NX
force_sum_x=0.0;
force_sum_y=0.0;
for k=2:NPOP
newx=1+mod(x-1+cx(k)+NX,NX);
newy=1+mod(y-1+cy(k)+NY,NY);
psi=1-exp(-rho(newx,newy));
force_sum_x=force_sum_x-G*weights(k)*psi*cx(k);
force_sum_y=force_sum_y-G*weights(k)*psi*cy(k);
end
forcex(x,y)=(1-exp(-rho(x,y)))*force_sum_x;
forcey(x,y)=(1-exp(-rho(x,y)))*force_sum_y;
end
end
% main loop
for y=1:NY
for x=1:NX
dense=rho(x,y);
vx=ux(x,y)+0.5*forcex(x,y)/dense;
vy=uy(x,y)+0.5*forcey(x,y)/dense;
for k=1:NPOP
feq(k)=weights(k)*dense*(1+3*(vx*cx(k)+vy*cy(k)) ...
+9/2*((cx(k)*cx(k)-1/3)*vx*vx+2*cx(k)*cy(k)*vx*vy+(cy(k)*cy(k)-1/3)*vy*vy));
forcepop=weights(k)*(1-0.5*omega)*((3*(cx(k)-vx)+9*cx(k)*(cx(k)*vx+cy(k)*vy))*forcex(x,y)...
+(3*(cy(k)-vy)+9*cy(k)*(cx(k)*vx+cy(k)*vy))*forcey(x,y));
newx=1+mod(x-1+cx(k)+NX,NX);
newy=1+mod(y-1+cy(k)+NY,NY);
f1(k,x,y)=f1(k,x,y)*(1-omega)+feq(k)*omega+forcepop;
f2(k,newx,newy)=f1(k,x,y);
end
end
end
f1=f2;
counter
if mod(counter,NOUTPUT)==0
imagesc(rho);
F(counter_frame) = getframe;
counter_frame=counter_frame+1;
end
end
movie(F,1)
disp('Rho_gas=')
disp(mean(mean(rho(NX/2-5:NX/2+5,(3*NY)/4-5:(3*NY)/4+5))))
disp('Rho_liq=')
disp(mean(mean(rho(NX/2-5:NX/2+5,NY/4-5:NY/4+5))))