-
Notifications
You must be signed in to change notification settings - Fork 40
/
d2q9-bgk.c
744 lines (634 loc) · 25.1 KB
/
d2q9-bgk.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
** Code to implement a d2q9-bgk lattice boltzmann scheme.
** 'd2' inidates a 2-dimensional grid, and
** 'q9' indicates 9 velocities per grid cell.
** 'bgk' refers to the Bhatnagar-Gross-Krook collision step.
**
** The 'speeds' in each cell are numbered as follows:
**
** 6 2 5
** \|/
** 3-0-1
** /|\
** 7 4 8
**
** A 2D grid:
**
** cols
** --- --- ---
** | D | E | F |
** rows --- --- ---
** | A | B | C |
** --- --- ---
**
** 'unwrapped' in row major order to give a 1D array:
**
** --- --- --- --- --- ---
** | A | B | C | D | E | F |
** --- --- --- --- --- ---
**
** Grid indicies are:
**
** ny
** ^ cols(ii)
** | ----- ----- -----
** | | ... | ... | etc |
** | ----- ----- -----
** rows(jj) | | 1,0 | 1,1 | 1,2 |
** | ----- ----- -----
** | | 0,0 | 0,1 | 0,2 |
** | ----- ----- -----
** ----------------------> nx
**
** Note the names of the input parameter and obstacle files
** are passed on the command line, e.g.:
**
** ./d2q9-bgk input.params obstacles.dat
**
** Be sure to adjust the grid dimensions in the parameter file
** if you choose a different obstacle file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#define NSPEEDS 9
#define FINALSTATEFILE "final_state.dat"
#define AVVELSFILE "av_vels.dat"
/* struct to hold the parameter values */
typedef struct
{
int nx; /* no. of cells in x-direction */
int ny; /* no. of cells in y-direction */
int maxIters; /* no. of iterations */
int reynolds_dim; /* dimension for Reynolds number */
float density; /* density per link */
float accel; /* density redistribution */
float omega; /* relaxation parameter */
} t_param;
/* struct to hold the 'speed' values */
typedef struct
{
float speeds[NSPEEDS];
} t_speed;
/*
** function prototypes
*/
/* load params, allocate memory, load obstacles & initialise fluid particle densities */
int initialise(const char* paramfile, const char* obstaclefile,
t_param* params, t_speed** cells_ptr, t_speed** tmp_cells_ptr,
int** obstacles_ptr, float** av_vels_ptr);
/*
** The main calculation methods.
** timestep calls, in order, the functions:
** accelerate_flow(), propagate(), rebound() & collision()
*/
int timestep(const t_param params, t_speed* cells, t_speed* tmp_cells, int* obstacles);
int accelerate_flow(const t_param params, t_speed* cells, int* obstacles);
int propagate(const t_param params, t_speed* cells, t_speed* tmp_cells);
int rebound(const t_param params, t_speed* cells, t_speed* tmp_cells, int* obstacles);
int collision(const t_param params, t_speed* cells, t_speed* tmp_cells, int* obstacles);
int write_values(const t_param params, t_speed* cells, int* obstacles, float* av_vels);
/* finalise, including freeing up allocated memory */
int finalise(const t_param* params, t_speed** cells_ptr, t_speed** tmp_cells_ptr,
int** obstacles_ptr, float** av_vels_ptr);
/* Sum all the densities in the grid.
** The total should remain constant from one timestep to the next. */
float total_density(const t_param params, t_speed* cells);
/* compute average velocity */
float av_velocity(const t_param params, t_speed* cells, int* obstacles);
/* calculate Reynolds number */
float calc_reynolds(const t_param params, t_speed* cells, int* obstacles);
/* utility functions */
void die(const char* message, const int line, const char* file);
void usage(const char* exe);
/*
** main program:
** initialise, timestep loop, finalise
*/
int main(int argc, char* argv[])
{
char* paramfile = NULL; /* name of the input parameter file */
char* obstaclefile = NULL; /* name of a the input obstacle file */
t_param params; /* struct to hold parameter values */
t_speed* cells = NULL; /* grid containing fluid densities */
t_speed* tmp_cells = NULL; /* scratch space */
int* obstacles = NULL; /* grid indicating which cells are blocked */
float* av_vels = NULL; /* a record of the av. velocity computed for each timestep */
struct timeval timstr; /* structure to hold elapsed time */
double tot_tic, tot_toc, init_tic, init_toc, comp_tic, comp_toc, col_tic, col_toc; /* floating point numbers to calculate elapsed wallclock time */
/* parse the command line */
if (argc != 3)
{
usage(argv[0]);
}
else
{
paramfile = argv[1];
obstaclefile = argv[2];
}
/* Total/init time starts here: initialise our data structures and load values from file */
gettimeofday(&timstr, NULL);
tot_tic = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
init_tic=tot_tic;
initialise(paramfile, obstaclefile, ¶ms, &cells, &tmp_cells, &obstacles, &av_vels);
/* Init time stops here, compute time starts*/
gettimeofday(&timstr, NULL);
init_toc = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
comp_tic=init_toc;
for (int tt = 0; tt < params.maxIters; tt++)
{
timestep(params, cells, tmp_cells, obstacles);
av_vels[tt] = av_velocity(params, cells, obstacles);
#ifdef DEBUG
printf("==timestep: %d==\n", tt);
printf("av velocity: %.12E\n", av_vels[tt]);
printf("tot density: %.12E\n", total_density(params, cells));
#endif
}
/* Compute time stops here, collate time starts*/
gettimeofday(&timstr, NULL);
comp_toc = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
col_tic=comp_toc;
// Collate data from ranks here
/* Total/collate time stops here.*/
gettimeofday(&timstr, NULL);
col_toc = timstr.tv_sec + (timstr.tv_usec / 1000000.0);
tot_toc = col_toc;
/* write final values and free memory */
printf("==done==\n");
printf("Reynolds number:\t\t%.12E\n", calc_reynolds(params, cells, obstacles));
printf("Elapsed Init time:\t\t\t%.6lf (s)\n", init_toc - init_tic);
printf("Elapsed Compute time:\t\t\t%.6lf (s)\n", comp_toc - comp_tic);
printf("Elapsed Collate time:\t\t\t%.6lf (s)\n", col_toc - col_tic);
printf("Elapsed Total time:\t\t\t%.6lf (s)\n", tot_toc - tot_tic);
write_values(params, cells, obstacles, av_vels);
finalise(¶ms, &cells, &tmp_cells, &obstacles, &av_vels);
return EXIT_SUCCESS;
}
int timestep(const t_param params, t_speed* cells, t_speed* tmp_cells, int* obstacles)
{
accelerate_flow(params, cells, obstacles);
propagate(params, cells, tmp_cells);
rebound(params, cells, tmp_cells, obstacles);
collision(params, cells, tmp_cells, obstacles);
return EXIT_SUCCESS;
}
int accelerate_flow(const t_param params, t_speed* cells, int* obstacles)
{
/* compute weighting factors */
float w1 = params.density * params.accel / 9.f;
float w2 = params.density * params.accel / 36.f;
/* modify the 2nd row of the grid */
int jj = params.ny - 2;
for (int ii = 0; ii < params.nx; ii++)
{
/* if the cell is not occupied and
** we don't send a negative density */
if (!obstacles[ii + jj*params.nx]
&& (cells[ii + jj*params.nx].speeds[3] - w1) > 0.f
&& (cells[ii + jj*params.nx].speeds[6] - w2) > 0.f
&& (cells[ii + jj*params.nx].speeds[7] - w2) > 0.f)
{
/* increase 'east-side' densities */
cells[ii + jj*params.nx].speeds[1] += w1;
cells[ii + jj*params.nx].speeds[5] += w2;
cells[ii + jj*params.nx].speeds[8] += w2;
/* decrease 'west-side' densities */
cells[ii + jj*params.nx].speeds[3] -= w1;
cells[ii + jj*params.nx].speeds[6] -= w2;
cells[ii + jj*params.nx].speeds[7] -= w2;
}
}
return EXIT_SUCCESS;
}
int propagate(const t_param params, t_speed* cells, t_speed* tmp_cells)
{
/* loop over _all_ cells */
for (int jj = 0; jj < params.ny; jj++)
{
for (int ii = 0; ii < params.nx; ii++)
{
/* determine indices of axis-direction neighbours
** respecting periodic boundary conditions (wrap around) */
int y_n = (jj + 1) % params.ny;
int x_e = (ii + 1) % params.nx;
int y_s = (jj == 0) ? (jj + params.ny - 1) : (jj - 1);
int x_w = (ii == 0) ? (ii + params.nx - 1) : (ii - 1);
/* propagate densities from neighbouring cells, following
** appropriate directions of travel and writing into
** scratch space grid */
tmp_cells[ii + jj*params.nx].speeds[0] = cells[ii + jj*params.nx].speeds[0]; /* central cell, no movement */
tmp_cells[ii + jj*params.nx].speeds[1] = cells[x_w + jj*params.nx].speeds[1]; /* east */
tmp_cells[ii + jj*params.nx].speeds[2] = cells[ii + y_s*params.nx].speeds[2]; /* north */
tmp_cells[ii + jj*params.nx].speeds[3] = cells[x_e + jj*params.nx].speeds[3]; /* west */
tmp_cells[ii + jj*params.nx].speeds[4] = cells[ii + y_n*params.nx].speeds[4]; /* south */
tmp_cells[ii + jj*params.nx].speeds[5] = cells[x_w + y_s*params.nx].speeds[5]; /* north-east */
tmp_cells[ii + jj*params.nx].speeds[6] = cells[x_e + y_s*params.nx].speeds[6]; /* north-west */
tmp_cells[ii + jj*params.nx].speeds[7] = cells[x_e + y_n*params.nx].speeds[7]; /* south-west */
tmp_cells[ii + jj*params.nx].speeds[8] = cells[x_w + y_n*params.nx].speeds[8]; /* south-east */
}
}
return EXIT_SUCCESS;
}
int rebound(const t_param params, t_speed* cells, t_speed* tmp_cells, int* obstacles)
{
/* loop over the cells in the grid */
for (int jj = 0; jj < params.ny; jj++)
{
for (int ii = 0; ii < params.nx; ii++)
{
/* if the cell contains an obstacle */
if (obstacles[jj*params.nx + ii])
{
/* called after propagate, so taking values from scratch space
** mirroring, and writing into main grid */
cells[ii + jj*params.nx].speeds[1] = tmp_cells[ii + jj*params.nx].speeds[3];
cells[ii + jj*params.nx].speeds[2] = tmp_cells[ii + jj*params.nx].speeds[4];
cells[ii + jj*params.nx].speeds[3] = tmp_cells[ii + jj*params.nx].speeds[1];
cells[ii + jj*params.nx].speeds[4] = tmp_cells[ii + jj*params.nx].speeds[2];
cells[ii + jj*params.nx].speeds[5] = tmp_cells[ii + jj*params.nx].speeds[7];
cells[ii + jj*params.nx].speeds[6] = tmp_cells[ii + jj*params.nx].speeds[8];
cells[ii + jj*params.nx].speeds[7] = tmp_cells[ii + jj*params.nx].speeds[5];
cells[ii + jj*params.nx].speeds[8] = tmp_cells[ii + jj*params.nx].speeds[6];
}
}
}
return EXIT_SUCCESS;
}
int collision(const t_param params, t_speed* cells, t_speed* tmp_cells, int* obstacles)
{
const float c_sq = 1.f / 3.f; /* square of speed of sound */
const float w0 = 4.f / 9.f; /* weighting factor */
const float w1 = 1.f / 9.f; /* weighting factor */
const float w2 = 1.f / 36.f; /* weighting factor */
/* loop over the cells in the grid
** NB the collision step is called after
** the propagate step and so values of interest
** are in the scratch-space grid */
for (int jj = 0; jj < params.ny; jj++)
{
for (int ii = 0; ii < params.nx; ii++)
{
/* don't consider occupied cells */
if (!obstacles[ii + jj*params.nx])
{
/* compute local density total */
float local_density = 0.f;
for (int kk = 0; kk < NSPEEDS; kk++)
{
local_density += tmp_cells[ii + jj*params.nx].speeds[kk];
}
/* compute x velocity component */
float u_x = (tmp_cells[ii + jj*params.nx].speeds[1]
+ tmp_cells[ii + jj*params.nx].speeds[5]
+ tmp_cells[ii + jj*params.nx].speeds[8]
- (tmp_cells[ii + jj*params.nx].speeds[3]
+ tmp_cells[ii + jj*params.nx].speeds[6]
+ tmp_cells[ii + jj*params.nx].speeds[7]))
/ local_density;
/* compute y velocity component */
float u_y = (tmp_cells[ii + jj*params.nx].speeds[2]
+ tmp_cells[ii + jj*params.nx].speeds[5]
+ tmp_cells[ii + jj*params.nx].speeds[6]
- (tmp_cells[ii + jj*params.nx].speeds[4]
+ tmp_cells[ii + jj*params.nx].speeds[7]
+ tmp_cells[ii + jj*params.nx].speeds[8]))
/ local_density;
/* velocity squared */
float u_sq = u_x * u_x + u_y * u_y;
/* directional velocity components */
float u[NSPEEDS];
u[1] = u_x; /* east */
u[2] = u_y; /* north */
u[3] = - u_x; /* west */
u[4] = - u_y; /* south */
u[5] = u_x + u_y; /* north-east */
u[6] = - u_x + u_y; /* north-west */
u[7] = - u_x - u_y; /* south-west */
u[8] = u_x - u_y; /* south-east */
/* equilibrium densities */
float d_equ[NSPEEDS];
/* zero velocity density: weight w0 */
d_equ[0] = w0 * local_density
* (1.f - u_sq / (2.f * c_sq));
/* axis speeds: weight w1 */
d_equ[1] = w1 * local_density * (1.f + u[1] / c_sq
+ (u[1] * u[1]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
d_equ[2] = w1 * local_density * (1.f + u[2] / c_sq
+ (u[2] * u[2]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
d_equ[3] = w1 * local_density * (1.f + u[3] / c_sq
+ (u[3] * u[3]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
d_equ[4] = w1 * local_density * (1.f + u[4] / c_sq
+ (u[4] * u[4]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
/* diagonal speeds: weight w2 */
d_equ[5] = w2 * local_density * (1.f + u[5] / c_sq
+ (u[5] * u[5]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
d_equ[6] = w2 * local_density * (1.f + u[6] / c_sq
+ (u[6] * u[6]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
d_equ[7] = w2 * local_density * (1.f + u[7] / c_sq
+ (u[7] * u[7]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
d_equ[8] = w2 * local_density * (1.f + u[8] / c_sq
+ (u[8] * u[8]) / (2.f * c_sq * c_sq)
- u_sq / (2.f * c_sq));
/* relaxation step */
for (int kk = 0; kk < NSPEEDS; kk++)
{
cells[ii + jj*params.nx].speeds[kk] = tmp_cells[ii + jj*params.nx].speeds[kk]
+ params.omega
* (d_equ[kk] - tmp_cells[ii + jj*params.nx].speeds[kk]);
}
}
}
}
return EXIT_SUCCESS;
}
float av_velocity(const t_param params, t_speed* cells, int* obstacles)
{
int tot_cells = 0; /* no. of cells used in calculation */
float tot_u; /* accumulated magnitudes of velocity for each cell */
/* initialise */
tot_u = 0.f;
/* loop over all non-blocked cells */
for (int jj = 0; jj < params.ny; jj++)
{
for (int ii = 0; ii < params.nx; ii++)
{
/* ignore occupied cells */
if (!obstacles[ii + jj*params.nx])
{
/* local density total */
float local_density = 0.f;
for (int kk = 0; kk < NSPEEDS; kk++)
{
local_density += cells[ii + jj*params.nx].speeds[kk];
}
/* x-component of velocity */
float u_x = (cells[ii + jj*params.nx].speeds[1]
+ cells[ii + jj*params.nx].speeds[5]
+ cells[ii + jj*params.nx].speeds[8]
- (cells[ii + jj*params.nx].speeds[3]
+ cells[ii + jj*params.nx].speeds[6]
+ cells[ii + jj*params.nx].speeds[7]))
/ local_density;
/* compute y velocity component */
float u_y = (cells[ii + jj*params.nx].speeds[2]
+ cells[ii + jj*params.nx].speeds[5]
+ cells[ii + jj*params.nx].speeds[6]
- (cells[ii + jj*params.nx].speeds[4]
+ cells[ii + jj*params.nx].speeds[7]
+ cells[ii + jj*params.nx].speeds[8]))
/ local_density;
/* accumulate the norm of x- and y- velocity components */
tot_u += sqrtf((u_x * u_x) + (u_y * u_y));
/* increase counter of inspected cells */
++tot_cells;
}
}
}
return tot_u / (float)tot_cells;
}
int initialise(const char* paramfile, const char* obstaclefile,
t_param* params, t_speed** cells_ptr, t_speed** tmp_cells_ptr,
int** obstacles_ptr, float** av_vels_ptr)
{
char message[1024]; /* message buffer */
FILE* fp; /* file pointer */
int xx, yy; /* generic array indices */
int blocked; /* indicates whether a cell is blocked by an obstacle */
int retval; /* to hold return value for checking */
/* open the parameter file */
fp = fopen(paramfile, "r");
if (fp == NULL)
{
sprintf(message, "could not open input parameter file: %s", paramfile);
die(message, __LINE__, __FILE__);
}
/* read in the parameter values */
retval = fscanf(fp, "%d\n", &(params->nx));
if (retval != 1) die("could not read param file: nx", __LINE__, __FILE__);
retval = fscanf(fp, "%d\n", &(params->ny));
if (retval != 1) die("could not read param file: ny", __LINE__, __FILE__);
retval = fscanf(fp, "%d\n", &(params->maxIters));
if (retval != 1) die("could not read param file: maxIters", __LINE__, __FILE__);
retval = fscanf(fp, "%d\n", &(params->reynolds_dim));
if (retval != 1) die("could not read param file: reynolds_dim", __LINE__, __FILE__);
retval = fscanf(fp, "%f\n", &(params->density));
if (retval != 1) die("could not read param file: density", __LINE__, __FILE__);
retval = fscanf(fp, "%f\n", &(params->accel));
if (retval != 1) die("could not read param file: accel", __LINE__, __FILE__);
retval = fscanf(fp, "%f\n", &(params->omega));
if (retval != 1) die("could not read param file: omega", __LINE__, __FILE__);
/* and close up the file */
fclose(fp);
/*
** Allocate memory.
**
** Remember C is pass-by-value, so we need to
** pass pointers into the initialise function.
**
** NB we are allocating a 1D array, so that the
** memory will be contiguous. We still want to
** index this memory as if it were a (row major
** ordered) 2D array, however. We will perform
** some arithmetic using the row and column
** coordinates, inside the square brackets, when
** we want to access elements of this array.
**
** Note also that we are using a structure to
** hold an array of 'speeds'. We will allocate
** a 1D array of these structs.
*/
/* main grid */
*cells_ptr = (t_speed*)malloc(sizeof(t_speed) * (params->ny * params->nx));
if (*cells_ptr == NULL) die("cannot allocate memory for cells", __LINE__, __FILE__);
/* 'helper' grid, used as scratch space */
*tmp_cells_ptr = (t_speed*)malloc(sizeof(t_speed) * (params->ny * params->nx));
if (*tmp_cells_ptr == NULL) die("cannot allocate memory for tmp_cells", __LINE__, __FILE__);
/* the map of obstacles */
*obstacles_ptr = malloc(sizeof(int) * (params->ny * params->nx));
if (*obstacles_ptr == NULL) die("cannot allocate column memory for obstacles", __LINE__, __FILE__);
/* initialise densities */
float w0 = params->density * 4.f / 9.f;
float w1 = params->density / 9.f;
float w2 = params->density / 36.f;
for (int jj = 0; jj < params->ny; jj++)
{
for (int ii = 0; ii < params->nx; ii++)
{
/* centre */
(*cells_ptr)[ii + jj*params->nx].speeds[0] = w0;
/* axis directions */
(*cells_ptr)[ii + jj*params->nx].speeds[1] = w1;
(*cells_ptr)[ii + jj*params->nx].speeds[2] = w1;
(*cells_ptr)[ii + jj*params->nx].speeds[3] = w1;
(*cells_ptr)[ii + jj*params->nx].speeds[4] = w1;
/* diagonals */
(*cells_ptr)[ii + jj*params->nx].speeds[5] = w2;
(*cells_ptr)[ii + jj*params->nx].speeds[6] = w2;
(*cells_ptr)[ii + jj*params->nx].speeds[7] = w2;
(*cells_ptr)[ii + jj*params->nx].speeds[8] = w2;
}
}
/* first set all cells in obstacle array to zero */
for (int jj = 0; jj < params->ny; jj++)
{
for (int ii = 0; ii < params->nx; ii++)
{
(*obstacles_ptr)[ii + jj*params->nx] = 0;
}
}
/* open the obstacle data file */
fp = fopen(obstaclefile, "r");
if (fp == NULL)
{
sprintf(message, "could not open input obstacles file: %s", obstaclefile);
die(message, __LINE__, __FILE__);
}
/* read-in the blocked cells list */
while ((retval = fscanf(fp, "%d %d %d\n", &xx, &yy, &blocked)) != EOF)
{
/* some checks */
if (retval != 3) die("expected 3 values per line in obstacle file", __LINE__, __FILE__);
if (xx < 0 || xx > params->nx - 1) die("obstacle x-coord out of range", __LINE__, __FILE__);
if (yy < 0 || yy > params->ny - 1) die("obstacle y-coord out of range", __LINE__, __FILE__);
if (blocked != 1) die("obstacle blocked value should be 1", __LINE__, __FILE__);
/* assign to array */
(*obstacles_ptr)[xx + yy*params->nx] = blocked;
}
/* and close the file */
fclose(fp);
/*
** allocate space to hold a record of the avarage velocities computed
** at each timestep
*/
*av_vels_ptr = (float*)malloc(sizeof(float) * params->maxIters);
return EXIT_SUCCESS;
}
int finalise(const t_param* params, t_speed** cells_ptr, t_speed** tmp_cells_ptr,
int** obstacles_ptr, float** av_vels_ptr)
{
/*
** free up allocated memory
*/
free(*cells_ptr);
*cells_ptr = NULL;
free(*tmp_cells_ptr);
*tmp_cells_ptr = NULL;
free(*obstacles_ptr);
*obstacles_ptr = NULL;
free(*av_vels_ptr);
*av_vels_ptr = NULL;
return EXIT_SUCCESS;
}
float calc_reynolds(const t_param params, t_speed* cells, int* obstacles)
{
const float viscosity = 1.f / 6.f * (2.f / params.omega - 1.f);
return av_velocity(params, cells, obstacles) * params.reynolds_dim / viscosity;
}
float total_density(const t_param params, t_speed* cells)
{
float total = 0.f; /* accumulator */
for (int jj = 0; jj < params.ny; jj++)
{
for (int ii = 0; ii < params.nx; ii++)
{
for (int kk = 0; kk < NSPEEDS; kk++)
{
total += cells[ii + jj*params.nx].speeds[kk];
}
}
}
return total;
}
int write_values(const t_param params, t_speed* cells, int* obstacles, float* av_vels)
{
FILE* fp; /* file pointer */
const float c_sq = 1.f / 3.f; /* sq. of speed of sound */
float local_density; /* per grid cell sum of densities */
float pressure; /* fluid pressure in grid cell */
float u_x; /* x-component of velocity in grid cell */
float u_y; /* y-component of velocity in grid cell */
float u; /* norm--root of summed squares--of u_x and u_y */
fp = fopen(FINALSTATEFILE, "w");
if (fp == NULL)
{
die("could not open file output file", __LINE__, __FILE__);
}
for (int jj = 0; jj < params.ny; jj++)
{
for (int ii = 0; ii < params.nx; ii++)
{
/* an occupied cell */
if (obstacles[ii + jj*params.nx])
{
u_x = u_y = u = 0.f;
pressure = params.density * c_sq;
}
/* no obstacle */
else
{
local_density = 0.f;
for (int kk = 0; kk < NSPEEDS; kk++)
{
local_density += cells[ii + jj*params.nx].speeds[kk];
}
/* compute x velocity component */
u_x = (cells[ii + jj*params.nx].speeds[1]
+ cells[ii + jj*params.nx].speeds[5]
+ cells[ii + jj*params.nx].speeds[8]
- (cells[ii + jj*params.nx].speeds[3]
+ cells[ii + jj*params.nx].speeds[6]
+ cells[ii + jj*params.nx].speeds[7]))
/ local_density;
/* compute y velocity component */
u_y = (cells[ii + jj*params.nx].speeds[2]
+ cells[ii + jj*params.nx].speeds[5]
+ cells[ii + jj*params.nx].speeds[6]
- (cells[ii + jj*params.nx].speeds[4]
+ cells[ii + jj*params.nx].speeds[7]
+ cells[ii + jj*params.nx].speeds[8]))
/ local_density;
/* compute norm of velocity */
u = sqrtf((u_x * u_x) + (u_y * u_y));
/* compute pressure */
pressure = local_density * c_sq;
}
/* write to file */
fprintf(fp, "%d %d %.12E %.12E %.12E %.12E %d\n", ii, jj, u_x, u_y, u, pressure, obstacles[ii + params.nx * jj]);
}
}
fclose(fp);
fp = fopen(AVVELSFILE, "w");
if (fp == NULL)
{
die("could not open file output file", __LINE__, __FILE__);
}
for (int ii = 0; ii < params.maxIters; ii++)
{
fprintf(fp, "%d:\t%.12E\n", ii, av_vels[ii]);
}
fclose(fp);
return EXIT_SUCCESS;
}
void die(const char* message, const int line, const char* file)
{
fprintf(stderr, "Error at line %d of file %s:\n", line, file);
fprintf(stderr, "%s\n", message);
fflush(stderr);
exit(EXIT_FAILURE);
}
void usage(const char* exe)
{
fprintf(stderr, "Usage: %s <paramfile> <obstaclefile>\n", exe);
exit(EXIT_FAILURE);
}