host: split write_image for density/velocity, -lm
authorSebastian <git@sraa.de>
Mon, 25 Aug 2014 16:23:44 +0000 (16:23 +0000)
committerSebastian <git@sraa.de>
Mon, 25 Aug 2014 16:23:44 +0000 (16:23 +0000)
split write_image() into write_density() and
write_velocity().
velocity calculations needs square root, so include
<math.h> and link against libm.

d2q9/Makefile
d2q9/hsrc/data.c
d2q9/hsrc/main.c

index f4f22ed633ad4d84df37ef0e0fe95634938785b5..28db1cead0d1be7fe9324608d885dd0c0eaa1e35 100644 (file)
@@ -3,7 +3,7 @@
 # host toolchain
 HCC    = gcc
 HCFLAGS        = -O2 -std=c99 -I$(EPIPHANY_HOME)/tools/host/include -Wall
-HLFLAGS        = -L$(EPIPHANY_HOME)/tools/host/lib -le-hal
+HLFLAGS        = -lm -L$(EPIPHANY_HOME)/tools/host/lib -le-hal
 ECHO   = /bin/echo -e
 
 # target toolchain
index 487768d2bb5bbea04e7c2c8f89063f176fceca0e..8e709a19b3afd81b4b6367cb8f334da7a7ebe2b6 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 #include <unistd.h>
 
 #include "../shared.h"
@@ -54,15 +55,15 @@ void write_populations(d2q9_block_t lattice[CORES_Y][CORES_X], int iter)
        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",
@@ -107,6 +108,87 @@ void write_image(d2q9_block_t lattice[CORES_Y][CORES_X], int iter)
        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)
 {
index 48ec00a23ac200fe306ea72e538fde5854430c64..712feb19914a53026148ebb985f7189247b17f83 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include <errno.h>
 #include <unistd.h>
 
@@ -15,8 +16,9 @@
 /* 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 */
@@ -92,7 +94,7 @@ int main()
 
                /* 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);
        }
        /* ================================================================ */