#include <stdio.h>
#include <stdlib.h>
+#include <math.h>
#include <unistd.h>
#include "../shared.h"
return;
}
-/* write an 8-bit grayscale, binary PPM image of the lattice */
-void write_image(d2q9_block_t lattice[CORES_Y][CORES_X], int iter)
+/* write an 8-bit grayscale, binary PPM image of the particle density */
+void write_density(d2q9_block_t lattice[CORES_Y][CORES_X], int iter)
{
char name[32]; snprintf(name, 32, "./tmp/i%06d.ppm", iter);
/* open image file and write header */
FILE *file = fopen(name, "wb");
if(!file) {
- perror("write_image/fopen");
+ perror("write_density/fopen");
return;
}
fprintf(file, "P5\n%d %d\n%d\n",
return;
}
+/* write an 8-bit grayscale, binary PPM image of the particle velocity */
+void write_velocity(d2q9_block_t lattice[CORES_Y][CORES_X], int iter)
+{
+ char name[32]; snprintf(name, 32, "./tmp/i%06d.ppm", iter);
+
+ /* open image file and write header */
+ FILE *file = fopen(name, "wb");
+ if(!file) {
+ perror("write_velocity/fopen");
+ return;
+ }
+ fprintf(file, "P5\n%d %d\n%d\n",
+ CORES_X*BLOCKS_X, CORES_Y*BLOCKS_Y, 255);
+
+
+ /* calculate all velocities and remember min/max */
+ FLOAT min = 1000, max = 0;
+ FLOAT us[CORES_Y][BLOCKS_Y][CORES_X][BLOCKS_X];
+ for(int cy = 0; cy < CORES_Y; cy++) {
+ for(int y = 0; y < BLOCKS_Y; y++) {
+ for(int cx = 0; cx < CORES_X; cx++) {
+ for(int x = 0; x < BLOCKS_X; x++) {
+ FLOAT rho = (
+ lattice[cy][cx][y][x][0] +
+ lattice[cy][cx][y][x][1] +
+ lattice[cy][cx][y][x][2] +
+ lattice[cy][cx][y][x][3] +
+ lattice[cy][cx][y][x][4] +
+ lattice[cy][cx][y][x][5] +
+ lattice[cy][cx][y][x][6] +
+ lattice[cy][cx][y][x][7] +
+ lattice[cy][cx][y][x][8]
+ );
+
+ FLOAT ux = (
+ lattice[cy][cx][y][x][5] +
+ lattice[cy][cx][y][x][6] +
+ lattice[cy][cx][y][x][7] -
+ lattice[cy][cx][y][x][1] -
+ lattice[cy][cx][y][x][2] -
+ lattice[cy][cx][y][x][3]
+ ) / rho;
+
+ FLOAT uy = (
+ lattice[cy][cx][y][x][1] +
+ lattice[cy][cx][y][x][7] +
+ lattice[cy][cx][y][x][8] -
+ lattice[cy][cx][y][x][3] -
+ lattice[cy][cx][y][x][4] -
+ lattice[cy][cx][y][x][5]
+ ) / rho;
+
+ FLOAT u = sqrtf(ux*ux + uy*uy);
+ if(u < min) min = u;
+ if(u > max) max = u;
+ us[cy][y][cx][x] = u;
+ }
+ }
+ }
+ }
+
+ /* scale values and write them to the image */
+ for(int cy = 0; cy < CORES_Y; cy++) {
+ for(int y = 0; y < BLOCKS_Y; y++) {
+ for(int cx = 0; cx < CORES_X; cx++) {
+ for(int x = 0; x < BLOCKS_X; x++) {
+ unsigned char gray;
+ gray = (255. * (us[cy][y][cx][x]-min) / (max-min));
+ fwrite(&gray, 1, 1, file);
+ }
+ }
+ }
+ }
+
+ /* close the file and chown if run with sudo */
+ fclose(file);
+ fixsudo(name);
+
+ return;
+}
+
/* convert image files to animated gif ./tmp/anim.gif */
void write_animation(void)
{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <math.h>
#include <errno.h>
#include <unistd.h>
/* helper functions */
void fixsudo(const char *filename);
void write_populations(d2q9_block_t lattice[CORES_Y][CORES_X], int iter);
-void write_image(d2q9_block_t lattice[CORES_Y][CORES_X], int iter);
void write_animation(void);
+void write_density (d2q9_block_t lattice[CORES_Y][CORES_X], int iter);
+void write_velocity (d2q9_block_t lattice[CORES_Y][CORES_X], int iter);
void write_timers(uint32_t timers[CORES_Y][CORES_X][TIMERS], uint32_t iter);
/* globals */
/* write data */
//write_populations(shm.lattice, shm.iteration);
- write_image(shm.lattice, shm.iteration);
+ write_density(shm.lattice, shm.iteration);
write_timers(shm.timers, shm.iteration);
}
/* ================================================================ */