-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-cases-S-C.cpp
338 lines (331 loc) · 10.2 KB
/
bubble-cases-S-C.cpp
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath> // for fabs()
int const static n=9,mx=200,my=200; //number of latttice nodes
int c=2;//different cases
double f[n][mx][my],f2[n][mx][my],feq[n],rho[mx][my],w[n],ux[mx][my],uy[mx][my],x[mx],y[my];
const int cx[n]={0, 1, 0, -1, 0, 1, -1, -1, 1};
const int cy[n]={0, 0, 1, 0, -1, 1, 1, -1, -1};
double Fx[mx][my]; // x-component of Shan-Chen force
double Fy[mx][my]; // y-component of Shan-Chen force
double forcing[n];
double rho_l=1.95,rho_g=0.15;
double radius=mx/15;
double a=mx/10,b=my/5;
int i,j;
int dx=1,dy=1; //space and time step
double const alpha=0.4;
double omega=1.0/(3.*alpha+0.5);
int mstep=500; // The total number of time steps
int freq=10;
double rho0=1.0;//reference density
const double g = -4.7;//interaction strength between particles.
void result(std::string filename,int time)
{
std::ofstream out(filename);
out << "TITLE=\"LBM output\"" << std::endl;
out << "VARIABLES = \"X\", \"Y\", \"Ux\",\"Uy\",\"density\",\"f0\",\"f1\",\"f2\",\"f3\",\"f4\",\"f5\",\"f6\",\"f7\",\"f8\",\"feq0\",\"feq1\",\"feq2\",\"feq3\",\"feq4\",\"feq5\",\"feq6\",\"feq7\",\"feq8\"," << std::endl;
out << "ZONE T = \"fluid\", I=" << mx << ", J=" << my << ", F=POINT" << std::endl;
//out << "SOLUTIONTIME="<< time << std::endl;
for (unsigned j = 0; j < my; ++j)
for (unsigned i = 0; i < mx; ++i)
{
out << std::scientific << std::setprecision(5) << std::setw(15) << x[i];
out << std::scientific << std::setprecision(5) << std::setw(15) << y[j];
out << std::scientific << std::setprecision(5) << std::setw(15) << ux[i][j];
out << std::scientific << std::setprecision(5) << std::setw(15) << uy[i][j];
out << std::scientific << std::setprecision(5) << std::setw(15) << rho[i][j];
for(int k=0;k<9;k++)
{
out << std::scientific << std::setprecision(5) << std::setw(15) << f[k][i][j];
}
for(int k=0;k<9;k++)
{
out << std::scientific << std::setprecision(5) << std::setw(15) << feq[k];
}
out << std::endl;
}
out.close();
}
void ComputeDesnity(double ftem[n][mx][my])
{
for(i=0;i<mx;i++)
{
for(j=0;j<my;j++)
{
double ssum=0;
for(int k=0;k<9;k++)
{
ssum=ssum+ftem[k][i][j];
}
rho[i][j]=ssum;
}
}
}
double ComputeTotalDensity(int i,int j)
{
double dense = rho[i][j];
return dense;
}
double psi(double dense)
{
return rho0 * (1. - exp(-dense / rho0));
}
void ComputeSCForce()
{
for(int i=0;i<mx;i++)
{
for (int j=0;j<my;j++)
{
double fxtem=0;
double fytem=0;
for(int k=1;k<9;k++)
{
int i1=(i+cx[k]+mx)%mx;
int j1=(j+cy[k]+my)%my;
// std::cout<<"i1="<<i1<<std::endl;
// std::cout<<"j1="<<i1<<std::endl;
double dense= rho[i1][j1];
// std::cout<<"dense="<<dense<<std::endl;
double psinb=psi(dense);//psi(x+c*deltat)
// std::cout<<"psinb="<<psinb<<std::endl;
fxtem += w[k] * cx[k] * psinb;//psi(x+c*deltat)*w*c
fytem += w[k] * cy[k] * psinb;
// std::cout<<"fxtem="<<fxtem<<std::endl;
// std::cout<<"fytem="<<fytem<<std::endl;
}
double psiloc = psi(rho[i][j]);//psi(x)
fxtem *= (-g * psiloc);//g*psi(x)*psi(x+c*deltat)*w*c
fytem *= (-g * psiloc);
Fx[i][j] = fxtem;
Fy[i][j] = fytem;
}
}
}
void ComputeVelocity(double ftem[n][mx][my])
{
for(int i=0;i<mx;i++)
{
for(int j=0;j<my;j++)
{
ux[i][j] = 0.;
uy[i][j] = 0.;
for(int k=0;k<9;k++)
{
ux[i][j] += ftem[k][i][j]*cx[k];
uy[i][j] += ftem[k][i][j]*cy[k];
}
ux[i][j] += (0.5 * Fx[i][j]);
uy[i][j] += (0.5 * Fy[i][j]);
double dens = ComputeTotalDensity(i,j);
ux[i][j] /= dens;
uy[i][j] /= dens;
}
}
}
void ComputeEquilibrium(int i,int j)
{
double dens = rho[i][j];
double vx = ux[i][j];
double vy = uy[i][j];
double usq = vx * vx + vy * vy;
// std::cout<<"dens= "<<dens<<std::endl;
// std::cout<<"vx= "<<vx<<std::endl;
// std::cout<<"vy= "<<vy<<std::endl;
// std::cout<<"usq= "<<usq<<std::endl;
feq[0] = w[0] * dens * (1. - 1.5 * usq);
feq[1] = w[1] * dens * (1. + 3. * vx + 4.5 * vx * vx - 1.5 * usq);
feq[2] = w[2] * dens * (1. + 3. * vy + 4.5 * vy * vy - 1.5 * usq);
feq[3] = w[3] * dens * (1. - 3. * vx + 4.5 * vx * vx - 1.5 * usq);
feq[4] = w[4] * dens * (1. - 3. * vy + 4.5 * vy * vy - 1.5 * usq);
feq[5] = w[5] * dens * (1. + 3. * ( vx + vy) + 4.5 * ( vx + vy) * ( vx + vy) - 1.5 * usq);
feq[6] = w[6] * dens * (1. + 3. * (-vx + vy) + 4.5 * (-vx + vy) * (-vx + vy) - 1.5 * usq);
feq[7] = w[7] * dens * (1. + 3. * (-vx - vy) + 4.5 * ( vx + vy) * ( vx + vy) - 1.5 * usq);
feq[8] = w[8] * dens * (1. + 3. * ( vx - vy) + 4.5 * ( vx - vy) * ( vx - vy) - 1.5 * usq);
}
void ComputeGuoForce(int i,int j,int k)
{
double temp=cx[k] * ux[i][j] + cy[k] * uy[i][j];
forcing[k] = w[k] * (1. - 0.5 * omega) *
((3. * (cx[k] - ux[i][j]) + 9. * cx[k] * temp) * Fx[i][j] +
(3. * (cy[k] - uy[i][j]) + 9. * cy[k] * temp) * Fy[i][j]);
}
void Collision(double ftem[n][mx][my],double ftem2[n][mx][my])
{
for(int i=0;i<mx;i++)
{
for(int j=0;j<my;j++)
{
for (int k=0;k<9;k++)
{
ComputeEquilibrium(i,j);
ComputeGuoForce(i,j,k);
int i2 = (i + cx[k] + mx) % mx;
int j2 = (j + cy[k] + my) % my;
ftem2[k][i2][j2] = ftem[k][i][j] * (1. - omega) + feq[k] * omega + forcing[k] ;
}
}
}
}
void Streaming(double ftem[n][mx][my])
{
double f_hlp[9][mx][my];
for(int j=0;j<my;j++)
{
for (int i=0;i<mx;i++)
{
int y_n = (1+j)%my;
int x_e = (1+i)%mx;
int y_s = my - 1 - (my- j)%my;
int x_w = mx - 1 - (mx- i)%mx;
f_hlp[1][x_e][j] = ftem[1][i][j];
f_hlp[2][i][y_n] = ftem[2][i][j];
f_hlp[3][x_w][j] = ftem[3][i][j];
f_hlp[4][i][y_s] = ftem[4][i][j];
f_hlp[5][x_e][y_n] = ftem[5][i][j];
f_hlp[6][x_w][y_n] = ftem[6][i][j];
f_hlp[7][x_w][y_s] = ftem[7][i][j];
f_hlp[8][x_e][y_s] = ftem[8][i][j];
}
}
for(int j=0;j<my;j++)
{
for (int i=0;i<mx;i++)
{
for(int k=1;k<9;k++)
{
ftem[k][i][j]=f_hlp[k][i][j];
}
}
}
}
void BoundaryCondition(double ftem[n][mx][my])
{
for(int j=0;j<my;j++)
{
ftem[1][0][j]=ftem[3][0][j];//left
ftem[5][0][j]=ftem[7][0][j];
ftem[8][0][j]=ftem[6][0][j];
ftem[3][mx-1][j]=ftem[1][mx-1][j];//right
ftem[7][mx-1][j]=ftem[5][mx-1][j];
ftem[6][mx-1][j]=ftem[8][mx-1][j];
}
for (int i=0;i<mx;i++)
{
ftem[2][i][0]=ftem[4][i][0];//bottom
ftem[5][i][0]=ftem[7][i][0];
ftem[6][i][0]=ftem[8][i][0];
ftem[4][i][my-1]=ftem[2][i][my-1];//top
ftem[7][i][my-1]=ftem[5][i][my-1];
ftem[8][i][my-1]=ftem[6][i][my-1];
}
}
void initialize()
{
x[0] =0.0;
y[0] =0.0;
//coordinate
for (i=1;i<mx;i++)
{
x[i]=x[i-1]+dx;
}
for (j=1;j<my;j++)
{
y[j]=y[j-1]+dy;
}
std::cout<<"omega= "<<omega<<std::endl;
/*-------weight factor ------*/
w[0]=4./9;
for(i=1;i<5;i++)
{
w[i]=1./9;
}
for(i=5;i<9;i++)
{
w[i]=1./36;
}
/*---------weight factor ----------*/
/*initial condition--------------*/
for(int i=0;i<mx;i++)
{
for (int j=0;j<my;j++)
{
switch (c)
{
case 1:
{
if((x[i]-mx/2)*(x[i]-my/2)+(y[j]-mx/2)*(y[j]-my/2)<radius*radius)
{
rho[i][j]=rho_l;
}
else
rho[i][j]=rho_g;
break;
}
case 2:
{
if((x[i]-mx/2+radius-1)*(x[i]-mx/2+radius-1)+(y[j]-my/2)*(y[j]-my/2)<radius*radius
||(x[i]-mx/2-radius+1)*(x[i]-mx/2-radius+1)+(y[j]-my/2)*(y[j]-my/2)<radius*radius)
{
rho[i][j]=rho_g;
}
else
rho[i][j]=rho_l;
break;
}
case 3:
{
if((x[i]-mx/2)*(x[i]-my/2)/(a*a)+(y[j]-mx/2)*(y[j]-my/2)/(b*b)<1)
{
rho[i][j]=rho_l;
}
else
rho[i][j]=rho_g;
break;
}
}
}
}
//initial condition of distribution function
for(int j=0;j<my;j++)
{
for(int i=0;i<mx;i++)
{
for(int k=0;k<9;k++)
{
ux[i][j]=0;
uy[i][j]=0;
ComputeEquilibrium(i,j);
f[k][i][j]=feq[k];
f2[k][i][j]=feq[k];
}
}
}
/*initial condition--------------*/
}
int main()
{
//main loop
initialize();
int time=0;
std::string filename = std::string("animation") +std::to_string(time)+std::string(".dat");
result(filename,time);
for (time=1;time<mstep+1;++time)
{
ComputeDesnity(f);
ComputeSCForce();//only novelty which contribute to equilibrium velocity and macroscopic velocity
ComputeVelocity(f);
Collision(f,f2);//plus GuoForce
std::swap(f,f2);
//Streaming(f);
BoundaryCondition(f);
std::string filename = std::string("animation") +std::to_string(time)+std::string(".dat");
if(time%freq==0) result(filename,time);
if(time%freq==0) {
std::cout << "Iteration: " << time << " / " << mstep << " (" << 100.0*double(time)/double(mstep) << " %)" << std::endl;
}
}//main loop end
return 0;
}