⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lbio.c

📁 基于格子Boltzmann方法开源可视化软件的源代码 具有很高的实用价值。对学习LBM方法及其软件开发非常游泳
💻 C
📖 第 1 页 / 共 5 页
字号:
  int    **map;  char   red_val,          green_val,          blue_val,          val;  double min_rho, max_rho;#if SAY_HI  printf("rho2bmp() -- Hi!\n");#endif /* SAY_HI */  frame = time/lattice->param.FrameRate;  sprintf( filename, "%dx%d.bmp", lattice->param.LX, lattice->param.LY);  if( !( in = fopen( filename, "r")))  {    printf("rho2bmp() -- Error opening file \"%s\".\n", filename);    exit(1);  }  // n = fread( void *BUF, size_t SIZE, size_t COUNT, FILE *FP);  n = fread( &bmfh, sizeof(struct bitmap_file_header), 1, in );  if( strncmp(bmfh.bfType,"BM",2))  {    printf("ERROR: Can't process this file type.  Exiting!\n");    printf("\n");    exit(1);  }  n = fread( &bmih, sizeof(struct bitmap_info_header), 1, in );  int_ptr = (int*)bmih.biCompression;  if( *int_ptr != 0)  {    printf("ERROR: Can't handle compression.  Exiting!\n");    printf("\n");    exit(1);  }  width_ptr = (int*)bmih.biWidth;  height_ptr = (int*)bmih.biHeight;  bitcount_ptr = (short int*)bmih.biBitCount;  // Read palette entries, if applicable.  if( *bitcount_ptr < 24)  {    n = (double)pow(2.,(double)*bitcount_ptr); // Num palette entries.    for( i=0; i<n; i++)    {      k = fread( &rgb, sizeof(struct rgb_quad), 1, in );      if( k!=1)      {        printf("Error reading palette entry %d.  Exiting!\n", i);        exit(1);      }    }  }  fclose(in);  // Bytes per row of the bitmap.  bytes_per_row =     ((int)ceil(( (((double)(*width_ptr))*((double)(*bitcount_ptr)))/8.)));  // Bitmaps pad rows to preserve 4-byte boundaries.  // The length of a row in the file will be bytes_per_row + pad .  pad = ((4) - bytes_per_row%4)%4;  map = (int**)malloc( (lattice->param.LY)*sizeof(int*));  for( j=0; j<(lattice->param.LY); j++)  {    map[j] = (int*)malloc( (lattice->param.LX)*sizeof(int));  }#if 0  //memset( &(map[0][0]), -1, (lattice->param.LX)*(lattice->param.LY)*sizeof(int));  memset( &(map[0][0]), -1, 29*sizeof(int));#else  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      map[j][i] = -1;    }  }#endif#if 0  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      printf(" %d", map[j][i]);    }    printf("\n");  }  printf("\n");#endif  for( n=0; n<lattice->NumNodes; n++)  {    map[lattice->node[n].j][lattice->node[n].i] = lattice->node[n].n;  }#if 0  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      printf(" %d", map[j][i]);    }    printf("\n");  }  printf("\n");#endif  compute_min_rho( lattice, &min_rho);  compute_max_rho( lattice, &max_rho);  sprintf( filename, "rho%dx%d_frame%04d.bmp",       lattice->param.LX,       lattice->param.LY,       frame);  o = fopen( filename, "w+");  fwrite( &bmfh, sizeof(struct bitmap_file_header), 1, o );  fwrite( &bmih, sizeof(struct bitmap_info_header), 1, o );  //for( j=lattice->param.LY-1; j>=0; j--)  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      if(    map[j][i] != INACTIVE_NODE           //&& !( lattice->bc[ map[j][i]].bc_type & BC_SOLID_NODE)          && lattice->bc[ map[j][i]].bc_type == /*FLUID_NODE*/0        )      {        red_val = (char)0;        green_val = (char)0;        red_val = (char)round( 255.*(lattice->macro_vars[ map[j][i]].rho[0] - min_rho)/(max_rho-min_rho));        blue_val = (char)255-red_val;      } /* if( map[j][i] != INACTIVE_NODE) */      else // map[j][i] == INACTIVE_NODE      {#if SOLID_COLOR_IS_CHECKERBOARD        // Checkerboard pattern over the solids and boundary conditions.        if( (i+j)%2)        {          red_val   = (char)200;          green_val = (char)200;          blue_val  = (char)200;          val       = (char)200;        }        else        {          red_val   = (char)184;          green_val = (char)184;          blue_val  = (char)184;          val       = (char)184;        }#else /* !( SOLID_COLOR_IS_CHECKERBOARD) */#if SOLID_COLOR_IS_BLACK        red_val   = (char)0;        green_val = (char)0;        blue_val  = (char)0;        val       = (char)0;#else /* !( SOLID_COLOR_IS_BLACK) */        red_val   = (char)255;        green_val = (char)255;        blue_val  = (char)255;        val       = (char)255;#endif /* SOLID_COLOR_IS_BLACK */#endif /* SOLID_COLOR_IS_CHECKERBOARD */      } /* if( map[j][i] != INACTIVE_NODE) else *///printf("blue_val( %d, %d) = %d\n", i, j, (int)blue_val);      if( fwrite( &blue_val, 1, 1, o) != 1) { printf("BOOM!\n"); exit(1);}      //printf("BING %d %d\n", i, j);      if( fwrite( &green_val, 1, 1, o) != 1) { printf("BOOM!\n"); exit(1);}      //printf("BING %d %d\n", i, j);      if( fwrite( &red_val, 1, 1, o) != 1) { printf("BOOM!\n"); exit(1);}      //printf("BING %d %d\n", i, j);         } /* for( i=0; i<lattice->param.LY; i++) */    // Pad for 4-byte boundaries.    val = (char)0;    for( i=0; i<pad; i++)    {      if( fwrite( &val, 1, 1, o) != 1) { printf("BOOM!\n"); exit(1);}    }  } /* for( j=0; j<lattice->param.LY; j++) */  fclose(o);#if VERBOSITY_LEVEL > 0  printf("rho2bmp() -- Wrote file \"%s\".\n", filename);#endif /* VERBOSITY_LEVEL > 0 */#if SAY_HI  printf("rho2bmp() -- Bye!\n");  printf("\n");#endif /* SAY_HI */} /* rho2bmp( lattice_ptr lattice, int time) */// void u2bmp( char *filename, int time)//##############################################################################//// U 2 B M P //void u2bmp( lattice_ptr lattice, int time){  FILE   *in,          *o_u,         *o_ux,         *o_uy;  int    i, j,          n, m;  int    pad,          bytes_per_row;  int    frame;  char   k;  char   b;  struct bitmap_file_header bmfh;  struct bitmap_info_header bmih;  struct rgb_quad rgb;  int    *int_ptr;  short  int *short_int_ptr;  int    *width_ptr;  int    *height_ptr;  short  int *bitcount_ptr;  char   filename[1024];  int    **map;  char   red_val,          green_val,          blue_val,          val;  double max_u[2], maxu;  double u_x, u_y, u;#if SAY_HI  printf("u2bmp() -- Hi!\n");#endif /* SAY_HI */  frame = time/lattice->param.FrameRate;  sprintf( filename, "%dx%d.bmp", lattice->param.LX, lattice->param.LY);  if( !( in = fopen( filename, "r")))  {    printf("u2bmp() -- Error opening file \"%s\".\n", filename);    exit(1);  }  // n = fread( void *BUF, size_t SIZE, size_t COUNT, FILE *FP);  n = fread( &bmfh, sizeof(struct bitmap_file_header), 1, in );  if( strncmp(bmfh.bfType,"BM",2))  {    printf("ERROR: Can't process this file type.  Exiting!\n");    printf("\n");    exit(1);  }  n = fread( &bmih, sizeof(struct bitmap_info_header), 1, in );  int_ptr = (int*)bmih.biCompression;  if( *int_ptr != 0)  {    printf("ERROR: Can't handle compression.  Exiting!\n");    printf("\n");    exit(1);  }  width_ptr = (int*)bmih.biWidth;  height_ptr = (int*)bmih.biHeight;  bitcount_ptr = (short int*)bmih.biBitCount;  // Read palette entries, if applicable.  if( *bitcount_ptr < 24)  {    n = (double)pow(2.,(double)*bitcount_ptr); // Num palette entries.    for( i=0; i<n; i++)    {      k = fread( &rgb, sizeof(struct rgb_quad), 1, in );      if( k!=1)      {        printf("Error reading palette entry %d.  Exiting!\n", i);        exit(1);      }    }  }  fclose(in);  // Bytes per row of the bitmap.  bytes_per_row =     ((int)ceil(( (((double)(*width_ptr))*((double)(*bitcount_ptr)))/8.)));  // Bitmaps pad rows to preserve 4-byte boundaries.  // The length of a row in the file will be bytes_per_row + pad .  pad = ((4) - bytes_per_row%4)%4;  // Create a map from (i,j) space onto n index space.  This could be a   // bad idea in terms of memory usage.  But the purpose is to create a   // bitmap file.  If the domain is small enough to view as a BMP, then  // it is small enough to manage in a 2D array like this...  map = (int**)malloc( (lattice->param.LY)*sizeof(int*));  for( j=0; j<(lattice->param.LY); j++)  {    map[j] = (int*)malloc( (lattice->param.LX)*sizeof(int));  }#if 0  //memset( &(map[0][0]), -1, (lattice->param.LX)*(lattice->param.LY)*sizeof(int));  memset( &(map[0][0]), -1, 29*sizeof(int));#else  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      map[j][i] = -1;    }  }#endif#if 0  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      printf(" %d", map[j][i]);    }    printf("\n");  }  printf("\n");#endif  for( n=0; n<lattice->NumNodes; n++)  {    map[lattice->node[n].j][lattice->node[n].i] = lattice->node[n].n;  }#if 0  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      printf(" %d", map[j][i]);    }    printf("\n");  }  printf("\n");#endif  compute_max_u( lattice, max_u);  sprintf( filename, "u%dx%d_frame%04d.bmp",       lattice->param.LX,       lattice->param.LY,       frame);  o_u = fopen( filename, "w+");  sprintf( filename, "u_x%dx%d_frame%04d.bmp",       lattice->param.LX,       lattice->param.LY,       frame);  o_ux = fopen( filename, "w+");  sprintf( filename, "u_y%dx%d_frame%04d.bmp",       lattice->param.LX,       lattice->param.LY,       frame);  o_uy = fopen( filename, "w+");  fwrite( &bmfh, sizeof(struct bitmap_file_header), 1, o_u );  fwrite( &bmih, sizeof(struct bitmap_info_header), 1, o_u );  fwrite( &bmfh, sizeof(struct bitmap_file_header), 1, o_ux );  fwrite( &bmih, sizeof(struct bitmap_info_header), 1, o_ux );  fwrite( &bmfh, sizeof(struct bitmap_file_header), 1, o_uy);  fwrite( &bmih, sizeof(struct bitmap_info_header), 1, o_uy);  //for( j=lattice->param.LY-1; j>=0; j--)  for( j=0; j<lattice->param.LY; j++)  {    for( i=0; i<lattice->param.LX; i++)    {      if(    map[j][i] != INACTIVE_NODE           //&& !( lattice->bc[ map[j][i]].bc_type & BC_SOLID_NODE)          && lattice->bc[ map[j][i]].bc_type == /*FLUID_NODE*/0        )      {#if 1        blue_val  = (char)0;        green_val = (char)0;        red_val   = (char)0;        u_x = (lattice->macro_vars[ map[j][i]].u[0]);        u_y = (lattice->macro_vars[ map[j][i]].u[1]);        u = sqrt(u_x*u_x + u_y*u_y);        maxu = sqrt( max_u[0]*max_u[0] + max_u[1]*max_u[1]);        if( lattice->bc[ map[j][i]].bc_type & BC_SOLID_NODE)        {          blue_val  = (char)round( 128.*fabs(u_x)/max_u[0]);          green_val = (char)round( 128.*fabs(u_y)/max_u[1]);        } /* if( lattice->bc[ map[j][i]].bc_type & BC_SOLID_NODE) */        else // !( lattice->bc[ map[j][i]].bc_type & BC_SOLID_NODE)        {          blue_val  = (char)round(  255.*fabs(u_x)/max_u[0]);          green_val = (char)round(  255.*fabs(u_y)/max_u[1]);          red_val    = (char)round( 128.*fabs(u)/maxu);        } /* if( lattice->bc[ map[j][i]].bc_type & BC_SOLID_NODE) else */#else        blue_val  = (char)255;        green_val = (char)255;        red_val   = (char)255;        u = sqrt(u_x*u_x + u_y*u_y);        maxu = sqrt( max_u[0]*max_u[0] + max_u[1]*max_u[1]);        //if( fabs(u) > .1*maxu)        //{          green_val  = (char)round( 255.-255.*fabs(u)/maxu);          red_val    = (char)round( 255.-255.*fabs(u)/maxu);          blue_val    = (char)round( 255.-255.*fabs(u)/maxu);        //}        //else        //{        //  green_val  = (char)0;        //  red_val    = (char)0;        //  blue_val    = (char)0;        //}#endif        val = (char)0;      } /* if( map[j][i] != INACTIVE_NODE) */      else // map[j][i] == INACTIVE_NODE      {#if SOLID_COLOR_IS_CHECKERBOARD        // Checkerboard pattern over the solids and boundary conditions.        if( (i+j)%2)        {          red_val   = (char)200;          green_val = (char)200;          blue_val  = (char)200;          val       = (char)200;        }        else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -