/
serial.c
647 lines (521 loc) · 17.7 KB
/
serial.c
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
////////////////////////////////////////////////////////////////////////////////
// Crude 2D Lattice Boltzmann Demo program
// C version
// Graham Pullan - Oct 2008
//
// f6 f2 f5
// \ | /
// \ | /
// \|/
// f3---|--- f1
// /|\
// / | \ and f0 for the rest (zero) velocity
// / | \
// f7 f4 f8
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <glew.h>
#include <GLUT/glut.h>
#define I2D(ni,i,j) (((ni)*(j)) + i)
////////////////////////////////////////////////////////////////////////////////
// OpenGL pixel buffer object and texture //
GLuint gl_PBO, gl_Tex;
// arrays //
float *f0,*f1,*f2,*f3,*f4,*f5,*f6,*f7,*f8;
float *tmpf0,*tmpf1,*tmpf2,*tmpf3,*tmpf4,*tmpf5,*tmpf6,*tmpf7,*tmpf8;
float *cmap,*plotvar;
int *solid;
unsigned int *cmap_rgba, *plot_rgba; //rgba arrays for plotting
// scalars //
float tau,faceq1,faceq2,faceq3;
float vxin, roout;
float width, height;
int ni,nj;
int ncol;
int ipos_old,jpos_old, draw_solid_flag;
////////////////////////////////////////////////////////////////////////////////
//
// OpenGL function prototypes
//
void display(void);
void resize(int w, int h);
void mouse(int button, int state, int x, int y);
void mouse_motion(int x, int y);
void shutdown(void);
//
// Lattice Boltzmann function prototypes
//
void stream(void);
void collide(void);
void solid_BC(void);
void per_BC(void);
void in_BC(void);
void ex_BC_crude(void);
void apply_BCs(void);
unsigned int get_col(float min, float max, float val);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
int array_size_2d,totpoints,i;
float rcol,gcol,bcol;
FILE *fp_col;
// The following parameters are usually read from a file, but
// hard code them for the demo:
ni=320;
nj=112;
vxin=0.04;
roout=1.0;
tau=0.51;
// End of parameter list
// Write parameters to screen
printf ("ni = %d\n", ni);
printf ("nj = %d\n", nj);
printf ("vxin = %f\n", vxin);
printf ("roout = %f\n", roout);
printf ("tau = %f\n", tau);
totpoints=ni*nj;
array_size_2d=ni*nj*sizeof(float);
// Allocate memory for arrays
f0 = malloc(array_size_2d);
f1 = malloc(array_size_2d);
f2 = malloc(array_size_2d);
f3 = malloc(array_size_2d);
f4 = malloc(array_size_2d);
f5 = malloc(array_size_2d);
f6 = malloc(array_size_2d);
f7 = malloc(array_size_2d);
f8 = malloc(array_size_2d);
tmpf0 = malloc(array_size_2d);
tmpf1 = malloc(array_size_2d);
tmpf2 = malloc(array_size_2d);
tmpf3 = malloc(array_size_2d);
tmpf4 = malloc(array_size_2d);
tmpf5 = malloc(array_size_2d);
tmpf6 = malloc(array_size_2d);
tmpf7 = malloc(array_size_2d);
tmpf8 = malloc(array_size_2d);
plotvar = malloc(array_size_2d);
plot_rgba = malloc(ni*nj*sizeof(unsigned int));
solid = malloc(ni*nj*sizeof(int));
//
// Some factors used to calculate the f_equilibrium values
//
faceq1 = 4.f/9.f;
faceq2 = 1.f/9.f;
faceq3 = 1.f/36.f;
//
// Initialise f's by setting them to the f_equilibirum values assuming
// that the whole domain is at velocity vx=vxin vy=0 and density ro=roout
//
for (i=0; i<totpoints; i++) {
f0[i] = faceq1 * roout * (1.f - 1.5f*vxin*vxin);
f1[i] = faceq2 * roout * (1.f + 3.f*vxin + 4.5f*vxin*vxin - 1.5f*vxin*vxin);
f2[i] = faceq2 * roout * (1.f - 1.5f*vxin*vxin);
f3[i] = faceq2 * roout * (1.f - 3.f*vxin + 4.5f*vxin*vxin - 1.5f*vxin*vxin);
f4[i] = faceq2 * roout * (1.f - 1.5f*vxin*vxin);
f5[i] = faceq3 * roout * (1.f + 3.f*vxin + 4.5f*vxin*vxin - 1.5f*vxin*vxin);
f6[i] = faceq3 * roout * (1.f - 3.f*vxin + 4.5f*vxin*vxin - 1.5f*vxin*vxin);
f7[i] = faceq3 * roout * (1.f - 3.f*vxin + 4.5f*vxin*vxin - 1.5f*vxin*vxin);
f8[i] = faceq3 * roout * (1.f + 3.f*vxin + 4.5f*vxin*vxin - 1.5f*vxin*vxin);
plotvar[i] = vxin;
solid[i] = 1;
}
//
// Read in colourmap data for OpenGL display
//
fp_col = fopen("cmap.dat","r");
if (fp_col==NULL) {
printf("Error: can't open cmap.dat \n");
return 1;
}
// allocate memory for colourmap (stored as a linear array of int's)
fscanf (fp_col, "%d", &ncol);
cmap_rgba = (unsigned int *)malloc(ncol*sizeof(unsigned int));
// read colourmap and store as int's
for (i=0;i<ncol;i++){
fscanf(fp_col, "%f%f%f", &rcol, &gcol, &bcol);
cmap_rgba[i]=((int)(255.0f) << 24) | // convert colourmap to int
((int)(bcol * 255.0f) << 16) |
((int)(gcol * 255.0f) << 8) |
((int)(rcol * 255.0f) << 0);
}
fclose(fp_col);
//
// Iinitialise OpenGL display - use glut
//
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(ni, nj); // Window of ni x nj pixels
glutInitWindowPosition(50, 50); // position
glutCreateWindow("2D LB"); // title
// Check for OpenGL extension support
printf("Loading extensions: %s\n", glewGetErrorString(glewInit()));
if(!glewIsSupported(
"GL_VERSION_2_0 "
"GL_ARB_pixel_buffer_object "
"GL_EXT_framebuffer_object "
)){
fprintf(stderr, "ERROR: Support for necessary OpenGL extensions missing.");
fflush(stderr);
return;
}
// Set up view
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,ni,0.,nj, -200.0, 200.0);
// Create texture which we use to display the result and bind to gl_Tex
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &gl_Tex); // Generate 2D texture
glBindTexture(GL_TEXTURE_2D, gl_Tex); // bind to gl_Tex
// texture properties:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, ni, nj, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
// Create pixel buffer object and bind to gl_PBO. We store the data we want to
// plot in memory on the graphics card - in a "pixel buffer". We can then
// copy this to the texture defined above and send it to the screen
glGenBuffers(1, &gl_PBO);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, gl_PBO);
printf("Buffer created.\n");
// Set the call-back functions and start the glut loop
printf("Starting GLUT main loop...\n");
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutIdleFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(mouse_motion);
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void stream(void)
// Move the f values one grid spacing in the directions that they are pointing
// i.e. f1 is copied one location to the right, etc.
{
int i,j,im1,ip1,jm1,jp1,i0;
// Initially the f's are moved to temporary arrays
for (j=0; j<nj; j++) {
jm1=j-1;
jp1=j+1;
if (j==0) jm1=0;
if (j==(nj-1)) jp1=nj-1;
for (i=1; i<ni; i++) {
i0 = I2D(ni,i,j);
im1 = i-1;
ip1 = i+1;
if (i==0) im1=0;
if (i==(ni-1)) ip1=ni-1;
tmpf1[i0] = f1[I2D(ni,im1,j)];
tmpf2[i0] = f2[I2D(ni,i,jm1)];
tmpf3[i0] = f3[I2D(ni,ip1,j)];
tmpf4[i0] = f4[I2D(ni,i,jp1)];
tmpf5[i0] = f5[I2D(ni,im1,jm1)];
tmpf6[i0] = f6[I2D(ni,ip1,jm1)];
tmpf7[i0] = f7[I2D(ni,ip1,jp1)];
tmpf8[i0] = f8[I2D(ni,im1,jp1)];
}
}
// Now the temporary arrays are copied to the main f arrays
for (j=0; j<nj; j++) {
for (i=1; i<ni; i++) {
i0 = I2D(ni,i,j);
f1[i0] = tmpf1[i0];
f2[i0] = tmpf2[i0];
f3[i0] = tmpf3[i0];
f4[i0] = tmpf4[i0];
f5[i0] = tmpf5[i0];
f6[i0] = tmpf6[i0];
f7[i0] = tmpf7[i0];
f8[i0] = tmpf8[i0];
}
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void collide(void)
// Collisions between the particles are modeled here. We use the very simplest
// model which assumes the f's change toward the local equlibrium value (based
// on density and velocity at that point) over a fixed timescale, tau
{
int i,j,i0;
float ro, rovx, rovy, vx, vy, v_sq_term;
float f0eq, f1eq, f2eq, f3eq, f4eq, f5eq, f6eq, f7eq, f8eq;
float rtau, rtau1;
// Some useful constants
rtau = 1.f/tau;
rtau1 = 1.f - rtau;
for (j=0; j<nj; j++) {
for (i=0; i<ni; i++) {
i0 = I2D(ni,i,j);
// Do the summations needed to evaluate the density and components of velocity
ro = f0[i0] + f1[i0] + f2[i0] + f3[i0] + f4[i0] + f5[i0] + f6[i0] + f7[i0] + f8[i0];
rovx = f1[i0] - f3[i0] + f5[i0] - f6[i0] - f7[i0] + f8[i0];
rovy = f2[i0] - f4[i0] + f5[i0] + f6[i0] - f7[i0] - f8[i0];
vx = rovx/ro;
vy = rovy/ro;
// Also load the velocity magnitude into plotvar - this is what we will
// display using OpenGL later
plotvar[i0] = sqrt(vx*vx + vy*vy);
v_sq_term = 1.5f*(vx*vx + vy*vy);
// Evaluate the local equilibrium f values in all directions
f0eq = ro * faceq1 * (1.f - v_sq_term);
f1eq = ro * faceq2 * (1.f + 3.f*vx + 4.5f*vx*vx - v_sq_term);
f2eq = ro * faceq2 * (1.f + 3.f*vy + 4.5f*vy*vy - v_sq_term);
f3eq = ro * faceq2 * (1.f - 3.f*vx + 4.5f*vx*vx - v_sq_term);
f4eq = ro * faceq2 * (1.f - 3.f*vy + 4.5f*vy*vy - v_sq_term);
f5eq = ro * faceq3 * (1.f + 3.f*(vx + vy) + 4.5f*(vx + vy)*(vx + vy) - v_sq_term);
f6eq = ro * faceq3 * (1.f + 3.f*(-vx + vy) + 4.5f*(-vx + vy)*(-vx + vy) - v_sq_term);
f7eq = ro * faceq3 * (1.f + 3.f*(-vx - vy) + 4.5f*(-vx - vy)*(-vx - vy) - v_sq_term);
f8eq = ro * faceq3 * (1.f + 3.f*(vx - vy) + 4.5f*(vx - vy)*(vx - vy) - v_sq_term);
// Simulate collisions by "relaxing" toward the local equilibrium
f0[i0] = rtau1 * f0[i0] + rtau * f0eq;
f1[i0] = rtau1 * f1[i0] + rtau * f1eq;
f2[i0] = rtau1 * f2[i0] + rtau * f2eq;
f3[i0] = rtau1 * f3[i0] + rtau * f3eq;
f4[i0] = rtau1 * f4[i0] + rtau * f4eq;
f5[i0] = rtau1 * f5[i0] + rtau * f5eq;
f6[i0] = rtau1 * f6[i0] + rtau * f6eq;
f7[i0] = rtau1 * f7[i0] + rtau * f7eq;
f8[i0] = rtau1 * f8[i0] + rtau * f8eq;
}
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void solid_BC(void)
// This is the boundary condition for a solid node. All the f's are reversed -
// this is known as "bounce-back"
{
int i,j,i0;
float f1old,f2old,f3old,f4old,f5old,f6old,f7old,f8old;
for (j=0;j<nj;j++){
for (i=0;i<ni;i++){
i0=I2D(ni,i,j);
if (solid[i0]==0) {
f1old = f1[i0];
f2old = f2[i0];
f3old = f3[i0];
f4old = f4[i0];
f5old = f5[i0];
f6old = f6[i0];
f7old = f7[i0];
f8old = f8[i0];
f1[i0] = f3old;
f2[i0] = f4old;
f3[i0] = f1old;
f4[i0] = f2old;
f5[i0] = f7old;
f6[i0] = f8old;
f7[i0] = f5old;
f8[i0] = f6old;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void per_BC(void)
// All the f's leaving the bottom of the domain (j=0) enter at the top (j=nj-1),
// and vice-verse
{
int i0,i1,i;
for (i=0; i<ni; i++){
i0 = I2D(ni,i,0);
i1 = I2D(ni,i,nj-1);
f2[i0] = f2[i1];
f5[i0] = f5[i1];
f6[i0] = f6[i1];
f4[i1] = f4[i0];
f7[i1] = f7[i0];
f8[i1] = f8[i0];
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void in_BC(void)
// This inlet BC is extremely crude but is very stable
// We set the incoming f values to the equilibirum values assuming:
// ro=roout; vx=vxin; vy=0
{
int i0, j;
float f1new, f5new, f8new, vx_term;
vx_term = 1.f + 3.f*vxin +3.f*vxin*vxin;
f1new = roout * faceq2 * vx_term;
f5new = roout * faceq3 * vx_term;
f8new = f5new;
for (j=0; j<nj; j++){
i0 = I2D(ni,0,j);
f1[i0] = f1new;
f5[i0] = f5new;
f8[i0] = f8new;
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void ex_BC_crude(void)
// This is the very simplest (and crudest) exit BC. All the f values pointing
// into the domain at the exit (ni-1) are set equal to those one node into
// the domain (ni-2)
{
int i0, i1, j;
for (j=0; j<nj; j++){
i0 = I2D(ni,ni-1,j);
i1 = i0 - 1;
f3[i0] = f3[i1];
f6[i0] = f6[i1];
f7[i0] = f7[i1];
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void apply_BCs(void)
// Just calls the individual BC functions
{
per_BC();
solid_BC();
in_BC();
ex_BC_crude();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void display(void)
// This function is called automatically, over and over again, by GLUT
{
int i,j,ip1,jp1,i0,icol,i1,i2,i3,i4,isol;
float minvar,maxvar,frac;
// set upper and lower limits for plotting
minvar=0.0;
maxvar=0.2;
// do one Lattice Boltzmann step: stream, BC, collide:
stream();
apply_BCs();
collide();
// convert the plotvar array into an array of colors to plot
// if the mesh point is solid, make it black
for (j=0;j<nj;j++){
for (i=0;i<ni;i++){
i0=I2D(ni,i,j);
frac=(plotvar[i0]-minvar)/(maxvar-minvar);
icol=frac*ncol;
isol=(int)solid[i0];
plot_rgba[i0] = isol*cmap_rgba[icol];
}
}
// Fill the pixel buffer with the plot_rgba array
glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB,ni*nj*sizeof(unsigned int),
(void **)plot_rgba,GL_STREAM_COPY);
// Copy the pixel buffer to the texture, ready to display
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,ni,nj,GL_RGBA,GL_UNSIGNED_BYTE,0);
// Render one quad to the screen and colour it using our texture
// i.e. plot our plotvar data to the screen
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex3f (0.0, 0.0, 0.0);
glTexCoord2f (1.0, 0.0);
glVertex3f (ni, 0.0, 0.0);
glTexCoord2f (1.0, 1.0);
glVertex3f (ni, nj, 0.0);
glTexCoord2f (0.0, 1.0);
glVertex3f (0.0, nj, 0.0);
glEnd();
glutSwapBuffers();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void resize(int w, int h)
// GLUT resize callback to allow us to change the window size
{
width = w;
height = h;
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0., ni, 0., nj, -200. ,200.);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void mouse(int button, int state, int x, int y)
// GLUT mouse callback. Left button draws the solid, right button removes solid
{
float xx,yy;
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) {
draw_solid_flag = 0;
xx=x;
yy=y;
ipos_old=xx/width*ni;
jpos_old=(height-yy)/height*nj;
}
if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN)) {
draw_solid_flag = 1;
xx=x;
yy=y;
ipos_old=xx/width*ni;
jpos_old=(height-yy)/height*nj;
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void mouse_motion(int x, int y)
// GLUT call back for when the mouse is moving
// This sets the solid array to draw_solid_flag as set in the mouse callback
// It will draw a staircase line if we move more than one pixel since the
// last callback - that makes the coding a bit cumbersome:
{
float xx,yy,frac;
int ipos,jpos,i,j,i1,i2,j1,j2, jlast, jnext;
xx=x;
yy=y;
ipos=(int)(xx/width*(float)ni);
jpos=(int)((height-yy)/height*(float)nj);
if (ipos <= ipos_old){
i1 = ipos;
i2 = ipos_old;
j1 = jpos;
j2 = jpos_old;
}
else {
i1 = ipos_old;
i2 = ipos;
j1 = jpos_old;
j2 = jpos;
}
jlast=j1;
for (i=i1;i<=i2;i++){
if (i1 != i2) {
frac=(float)(i-i1)/(float)(i2-i1);
jnext=(int)(frac*(j2-j1))+j1;
}
else {
jnext=j2;
}
if (jnext >= jlast) {
solid[I2D(ni,i,jlast)]=draw_solid_flag;
for (j=jlast; j<=jnext; j++){
solid[I2D(ni,i,j)]=draw_solid_flag;
}
}
else {
solid[I2D(ni,i,jlast)]=draw_solid_flag;
for (j=jnext; j<=jlast; j++){
solid[I2D(ni,i,j)]=draw_solid_flag;
}
}
jlast = jnext;
}
ipos_old=ipos;
jpos_old=jpos;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////