Ejemplo n.º 1
0
void twopassfft_scratch(multifile * infile, multifile * scratch,
                        long long nn, int isign)
{
   long long n1, n2, bb, fp, ii, jj, kk, kind, df;
   int move_size;
   unsigned char *move;
   rawtype *data, *dp;
   double tmp1, tmp2, wtemp, wpi, wpr, wi, wr, delta;

   if (nn < 2)
      return;

   /* Treat the input data as a n1 (rows) x n2 (cols) */
   /* matrix.  Make sure that n2 >= n1.               */

   n1 = good_factor(nn);
   if (n1 == 0) {
      printf("\nLength of FFT in twopassfft_scratch() must be factorable\n\n");
      exit(1);
   }
   n2 = nn / n1;
   bb = find_blocksize(n1, n2);
   if (bb == 0) {
      printf("\nCan't factor the FFT length in twopassfft_scratch()\n");
      printf("   into useful sizes.\n\n");
      exit(1);
   }

   data = gen_rawvect(bb * n2);
   move_size = (bb + n2) / 2;
   move = (unsigned char *) malloc(move_size);

   /* First do n2 transforms of length n1 by  */
   /* fetching size bb x n1 blocks in memory. */

   for (ii = 0; ii < n2; ii += bb) {

      /* Read a n1 (rows) x bb (cols) block of data */

      dp = data;
      fp = sizeof(rawtype) * ii;
      df = sizeof(rawtype) * n2;
      for (jj = 0; jj < n1; jj++) {
         fseek_multifile(infile, fp, SEEK_SET);
         fread_multifile(dp, sizeof(rawtype), bb, infile);
         dp += bb;              /* Data ptr */
         fp += df;              /* File ptr */
      }

      /* Transpose the n1 (rows) x bb (cols) block of data */

      transpose_fcomplex(data, n1, bb, move, move_size);

      /* Do bb transforms of length n1 */

      for (jj = 0; jj < bb; jj++)
         COMPLEXFFT(data + jj * n1, n1, isign);

      /* Multiply the matrix A(ii,jj) by exp(isign 2 pi i jj ii / nn). */
      /* Use recursion formulas from Numerical Recipes.                */

      for (jj = 0; jj < bb; jj++) {
         delta = isign * TWOPI * (ii + jj) / nn;
         wr = cos(delta);
         wi = sin(delta);
         wtemp = sin(0.5 * delta);
         wpr = -2.0 * wtemp * wtemp;
         wpi = wi;
         kind = jj * n1 + 1;
         for (kk = 1; kk < n1; kk++, kind++) {
            tmp1 = data[kind].r;
            tmp2 = data[kind].i;
            data[kind].r = tmp1 * wr - tmp2 * wi;
            data[kind].i = tmp2 * wr + tmp1 * wi;
            wtemp = wr;
            wr = wtemp * wpr - wi * wpi + wr;
            wi = wi * wpr + wtemp * wpi + wi;
         }
      }
      fwrite_multifile(data, sizeof(rawtype), bb * n1, scratch);
   }

   /* Now do n1 transforms of length n2 by fetching  */
   /* groups of size n2 (rows) x bb (cols) blocks.   */

   for (ii = 0; ii < n1; ii += bb) {

      /* Read two n2 (rows) x bb (cols) blocks from the file */

      dp = data;
      fp = sizeof(rawtype) * ii;
      df = sizeof(rawtype) * n1;
      for (jj = 0; jj < n2; jj++) {
         fseek_multifile(scratch, fp, SEEK_SET);
         fread_multifile(dp, sizeof(rawtype), bb, scratch);
         dp += bb;              /* Data ptr */
         fp += df;              /* File ptr */
      }

      /* Transpose the n2 (rows) x bb (cols) block of data */

      transpose_fcomplex(data, n2, bb, move, move_size);

      /* Do bb transforms of length n2 */

      for (jj = 0; jj < bb; jj++)
         COMPLEXFFT(data + jj * n2, n2, isign);

      /* Transpose the bb (rows) x n2 (cols) block of data */

      transpose_fcomplex(data, bb, n2, move, move_size);

      /* Scale the data if needed */

      if (isign == 1) {
         tmp1 = 1.0 / (double) nn;
         for (jj = 0; jj < n2 * bb; jj++) {
            data[jj].r *= tmp1;
            data[jj].i *= tmp1;
         }
      }


      /* Write n2 (rows) x bb (cols) blocks to the file  */

      dp = data;
      fp = sizeof(rawtype) * ii;
      df = sizeof(rawtype) * n1;
      for (jj = 0; jj < n2; jj++) {
         fseek_multifile(infile, fp, SEEK_SET);
         fwrite_multifile(dp, sizeof(rawtype), bb, infile);
         dp += bb;              /* Data ptr */
         fp += df;              /* File ptr */
      }
   }
   free(move);
   vect_free(data);
}
Ejemplo n.º 2
0
int main(int argc, char *argv[]){
  char    filename_properties[256];
  char    filename_profiles[256];
  char    filename_out_root[256];
  char    filename_out[256];
  char    filename_SSimPL[MAX_FILENAME_LENGTH];
  char    filename_halo_type[MAX_FILENAME_LENGTH];
  int     snap_number;
  int     snap_number_start;
  int     snap_number_stop;
  int     snap_number_step;

  SID_init(&argc,&argv,NULL,NULL);

  strcpy(filename_SSimPL,   argv[1]);
  strcpy(filename_halo_type,argv[2]);
  snap_number_start   =atoi(argv[3]);
  snap_number_stop    =atoi(argv[4]);
  snap_number_step    =atoi(argv[5]);
  strcpy(filename_out_root, argv[6]);

  int flag_use_profiles=FALSE;

  if(SID.I_am_Master){
    SID_log("Processing catalogs for snaps %d->%d...",SID_LOG_OPEN|SID_LOG_TIMER,snap_number_start,snap_number_stop);
    for(snap_number=snap_number_start;snap_number<=snap_number_stop;snap_number++){

         // Open halos
         char filename_halos[256];
         sprintf(filename_halos,"%s/halos/%s_%03d.catalog_groups",filename_SSimPL,filename_halo_type,snap_number);
         FILE *fp_halos=NULL;
         if((fp_halos=fopen(filename_halos,"r"))==NULL)
            SID_trap_error("Could not open halo file {%s} for reading.",ERROR_IO_OPEN,filename_halos);
         int n_groups_halos,group_offset_byte_size;
         fread_verify(&n_groups_halos,        sizeof(int),1,fp_halos);
         fread_verify(&group_offset_byte_size,sizeof(int),1,fp_halos);

         // Skip group sizes and offsets
         fseeko(fp_halos,(off_t)(n_groups_halos*(sizeof(int)+group_offset_byte_size)),SEEK_CUR);

         // Open catalogs
         char filename_cat_root[256];
         sprintf(filename_cat_root,"%s/catalogs/%s",filename_SSimPL,filename_halo_type);
         fp_catalog_info fp_catalog_groups;
         fp_catalog_info fp_catalog_subgroups;
         fopen_catalog(filename_cat_root,
                       snap_number,
                       READ_CATALOG_GROUPS|READ_CATALOG_PROPERTIES|READ_CATALOG_PROPERTIES,
                       &fp_catalog_groups);
         fopen_catalog(filename_cat_root,
                       snap_number,
                       READ_CATALOG_SUBGROUPS|READ_CATALOG_PROPERTIES|READ_CATALOG_PROPERTIES,
                       &fp_catalog_subgroups);

         // Open SO files if they're available
         fp_multifile_info fp_SO;
         int flag_use_SO=fopen_multifile("%s/catalogs/%s_%03d.catalog_groups_SO",sizeof(float),&fp_SO,filename_SSimPL,filename_halo_type,snap_number);
         if(flag_use_SO)
            SID_log("SO files present.",SID_LOG_COMMENT);

         // Sanity check
         if(n_groups_halos!=fp_catalog_groups.n_halos_total)
            SID_trap_error("Group counts in halo and catalog files don't match (ie. %d!=%d).",ERROR_LOGIC,n_groups_halos,fp_catalog_groups.n_halos_total);

         // Process halos
         SID_log("Processing snapshot #%03d...",SID_LOG_OPEN,snap_number);
         SID_log("(%d groups, %d subgroups)...",SID_LOG_CONTINUE,fp_catalog_groups.n_halos_total,fp_catalog_subgroups.n_halos_total);

         // Initialzie halo trend data structure
         halo_trend_info  halo_trend_data;
         char             filename_run[MAX_FILENAME_LENGTH];
         sprintf(filename_run,"%s/run/run.txt",filename_SSimPL);
         parameter_list_info *parameter_list=NULL;
         init_parameter_list(&parameter_list);
         add_parameter_to_list(parameter_list,"box_size",SID_DOUBLE,   PARAMETER_MODE_DEFAULT);
         add_parameter_to_list(parameter_list,"N_dark",  SID_SIZE_T,   PARAMETER_MODE_DEFAULT);
         add_parameter_to_list(parameter_list,"m_dark",  SID_DOUBLE,   PARAMETER_MODE_DEFAULT);
         read_gbpParam_file(filename_run,parameter_list);
         fetch_parameter_data(parameter_list,"box_size",&(halo_trend_data.box_size)); 
         fetch_parameter_data(parameter_list,"m_dark",  &(halo_trend_data.m_p)); 
         free_parameter_list(&parameter_list);
         char             filename_snaps[MAX_FILENAME_LENGTH];
         sprintf(filename_snaps,"%s/run/a_list.txt",filename_SSimPL);
         FILE *fp_snaps=fopen(filename_snaps,"r");
         size_t line_length=0;
         char  *line=NULL;
         halo_trend_data.n_snaps=count_lines_data(fp_snaps);
         halo_trend_data.z_list =(double *)SID_malloc(sizeof(double)*halo_trend_data.n_snaps);
         for (int i_snap=0;i_snap<halo_trend_data.n_snaps;i_snap++){
            double a_i;
            grab_next_line_data(fp_snaps,&line,&line_length);
            grab_double(line,1,&a_i);
            halo_trend_data.z_list[i_snap]=z_of_a(a_i);
         }
         SID_free(SID_FARG line);
         fclose(fp_snaps);

         // Initialize halo data structure
         halo_info        halo_data;
         halo_data.flag_use_profiles  = flag_use_profiles;
         halo_data.flag_use_SO        = flag_use_SO;
         halo_data.snapshot           = snap_number;
         halo_data.properties_group   =(halo_properties_info *)SID_malloc(sizeof(halo_properties_info));
         halo_data.properties_subgroup=(halo_properties_info *)SID_malloc(sizeof(halo_properties_info));
         halo_data.profiles_group     =(halo_profile_info    *)SID_malloc(sizeof(halo_profile_info));
         halo_data.profiles_subgroup  =(halo_profile_info    *)SID_malloc(sizeof(halo_profile_info));

         // Initialize trends
         trend_info *trend_M_FoF=NULL;
         init_trend(&trend_M_FoF,"SSFctn",&halo_trend_data,init_halo_trend_property_logM_FoF,free_halo_trend_property_logM_FoF,calc_halo_trend_property_index_logM_FoF);
         init_halo_trend_coordinate(&halo_trend_data,trend_M_FoF,"SSFctn");

         // Read halos and construct histograms
         for(int i_group=0,i_subgroup=0;i_group<fp_catalog_groups.n_halos_total;i_group++){
            int n_subgroups_group;
            // Read group catalog
            fread_catalog_file(&fp_catalog_groups,NULL,NULL,halo_data.properties_group,halo_data.profiles_group,i_group);
            // Read number of subgroups
            fread_verify(&n_subgroups_group,sizeof(int),1,fp_halos);
            // Read SO masses (if available)
            if(flag_use_SO)
               fread_multifile(&fp_SO,halo_data.SO_data_group,i_group);
            // Loop over subgroups
            halo_data.n_sub         =n_subgroups_group;
            halo_data.np_sub        =0;
            halo_data.np_sub_largest=0;
            for(int j_subgroup=0;j_subgroup<n_subgroups_group;i_subgroup++,j_subgroup++){
               // Read subgroup properties
               fread_catalog_file(&fp_catalog_subgroups,NULL,NULL,halo_data.properties_subgroup,halo_data.profiles_subgroup,i_subgroup);
               int np_i=halo_data.properties_subgroup->n_particles;
               halo_data.np_sub+=np_i;
               if(np_i>halo_data.np_sub_largest)
                  halo_data.np_sub_largest=np_i;
               // Add halo to subgroup trends
            }
            // Add halo to group trends
            add_item_to_trend(trend_M_FoF,GBP_ADD_ITEM_TO_TREND_DEFAULT,&halo_data);
         }

         // Write results
         char filename_out[MAX_FILENAME_LENGTH];
         sprintf(filename_out,"%s_%03d",filename_out_root,snap_number);
         write_trend_ascii(trend_M_FoF,filename_out);
         free_trend(&trend_M_FoF);

         // Clean-up
         SID_free(SID_FARG halo_trend_data.z_list);
         SID_free(SID_FARG halo_data.properties_group);
         SID_free(SID_FARG halo_data.properties_subgroup);
         SID_free(SID_FARG halo_data.profiles_group);
         SID_free(SID_FARG halo_data.profiles_subgroup);
         fclose(fp_halos);
         fclose_catalog(&fp_catalog_groups);
         fclose_catalog(&fp_catalog_subgroups);
         fclose_multifile(&fp_SO);
         SID_log("Done.",SID_LOG_CLOSE);
     }
     SID_log("Done.",SID_LOG_CLOSE);
  }  

  SID_exit(ERROR_NONE);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
   int ii, numfiles, filelen, datalen;
   multifile *mfile;
   float *data, *scratch;
   char **filenames;

   srand(3);
   if (argc < 4) {
      printf("usage:  test_multifiles numfiles filelen datalen\n");
      exit(0);
   }
   numfiles = atoi(argv[1]);
   filenames = (char **) malloc(sizeof(char *) * numfiles);
   filelen = atoi(argv[2]);
   for (ii = 0; ii < numfiles; ii++) {
      filenames[ii] = calloc(10, 1);
      sprintf(filenames[ii], "test%d.dat", ii);
   }

   /* Generate the data and scratch spaces */

   datalen = atoi(argv[3]);
   data = gen_fvect(datalen);
   scratch = gen_fvect(datalen);
   for (ii = 0; ii < datalen; ii++)
      data[ii] = (float) 2.0 *(rand() / ((double) RAND_MAX)) - 1.0;

   /* Write the data */

   printf("\nWrite test...\n");
   mfile = fopen_multifile(numfiles, filenames, "w", filelen);
   print_multifile(mfile, 1);
   fwrite_multifile(data, sizeof(float), datalen, mfile);
   print_multifile(mfile, 0);
   fclose_multifile(mfile);

   /* Read the data */

   printf("\nRead test...\n");
   mfile = fopen_multifile(numfiles, filenames, "r", 0);
   print_multifile(mfile, 1);
   fread_multifile(scratch, sizeof(float), datalen, mfile);
   print_multifile(mfile, 0);
   fclose_multifile(mfile);

   /* Compare data and scratch */

   for (ii = 0; ii < datalen; ii++) {
      if (scratch[ii] != data[ii])
         printf("Data point %d doesn't match:  %f, %f\n", ii, data[ii], scratch[ii]);
   }

   /* Seek to a random place in the file and check that data */

   printf("\nSeek test...\n");
   {
      long long byteindex;

      mfile = fopen_multifile(numfiles, filenames, "r", 0);
      print_multifile(mfile, 1);
      byteindex = mfile->filelens[0] + mfile->filelens[1]
          - sizeof(float) * 10;
      print_multifile(mfile, 0);
      fseek_multifile(mfile, byteindex, SEEK_SET);
      print_multifile(mfile, 0);
      fread_multifile(scratch + byteindex / sizeof(float), sizeof(float), 30, mfile);
      print_multifile(mfile, 0);
      fclose_multifile(mfile);
   }

   /* Compare data and scratch */

   for (ii = 0; ii < datalen; ii++) {
      if (scratch[ii] != data[ii])
         printf("Data point %d doesn't match:  %f, %f\n", ii, data[ii], scratch[ii]);
   }
   /* Free stuff up */

   vect_free(data);
   vect_free(scratch);
   for (ii = 0; ii < numfiles; ii++)
      free(filenames[ii]);
   free(filenames);
   exit(0);
}