Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

read_ic.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <math.h>
00005 #include <string.h>
00006 #include <mpi.h>
00007 
00008 #include "allvars.h"
00009 #include "proto.h"
00010 
00011 
00031 void read_ic(char *fname)
00032 {
00033   int i, num_files, rest_files, ngroups, gr, filenr, masterTask, lastTask, groupMaster;
00034   double u_init;
00035   char buf[500];
00036 
00037 #ifndef ISOTHERM_EQS
00038   double molecular_weight;
00039 #endif
00040 #ifdef SFR
00041   double original_gas_mass, mass, masstot;
00042 #endif
00043 
00044   NumPart = 0;
00045   N_gas = 0;
00046   All.TotNumPart = 0;
00047 
00048   num_files = find_files(fname);
00049 
00050   rest_files = num_files;
00051 
00052   fill_Tab_IO_Labels();
00053 
00054   while(rest_files > NTask)
00055     {
00056       sprintf(buf, "%s.%d", fname, ThisTask + (rest_files - NTask));
00057       if(All.ICFormat == 3)
00058         sprintf(buf, "%s.%d.hdf5", fname, ThisTask + (rest_files - NTask));
00059 
00060       ngroups = NTask / All.NumFilesWrittenInParallel;
00061       if((NTask % All.NumFilesWrittenInParallel))
00062         ngroups++;
00063       groupMaster = (ThisTask / ngroups) * ngroups;
00064 
00065       for(gr = 0; gr < ngroups; gr++)
00066         {
00067           if(ThisTask == (groupMaster + gr))    /* ok, it's this processor's turn */
00068             read_file(buf, ThisTask, ThisTask);
00069           MPI_Barrier(MPI_COMM_WORLD);
00070         }
00071 
00072       rest_files -= NTask;
00073     }
00074 
00075 
00076   if(rest_files > 0)
00077     {
00078       distribute_file(rest_files, 0, 0, NTask - 1, &filenr, &masterTask, &lastTask);
00079 
00080       if(num_files > 1)
00081         {
00082           sprintf(buf, "%s.%d", fname, filenr);
00083           if(All.ICFormat == 3)
00084             sprintf(buf, "%s.%d.hdf5", fname, filenr);
00085         }
00086       else
00087         {
00088           sprintf(buf, "%s", fname);
00089           if(All.ICFormat == 3)
00090             sprintf(buf, "%s.hdf5", fname);
00091         }
00092 
00093       ngroups = rest_files / All.NumFilesWrittenInParallel;
00094       if((rest_files % All.NumFilesWrittenInParallel))
00095         ngroups++;
00096 
00097       for(gr = 0; gr < ngroups; gr++)
00098         {
00099           if((filenr / All.NumFilesWrittenInParallel) == gr)    /* ok, it's this processor's turn */
00100             read_file(buf, masterTask, lastTask);
00101           MPI_Barrier(MPI_COMM_WORLD);
00102         }
00103     }
00104 
00105 
00106   /* this makes sure that masses are initialized in the case that the mass-block
00107      is completely empty */
00108   for(i = 0; i < NumPart; i++)
00109     {
00110       if(All.MassTable[P[i].Type] != 0)
00111         P[i].Mass = All.MassTable[P[i].Type];
00112     }
00113 
00114   if(RestartFlag == 0)
00115     {
00116       if(All.InitGasTemp > 0)
00117         {
00118           u_init = (BOLTZMANN / PROTONMASS) * All.InitGasTemp;
00119           u_init *= All.UnitMass_in_g / All.UnitEnergy_in_cgs;  /* unit conversion */
00120 
00121 #ifdef ISOTHERM_EQS
00122           u_init *= 1.0;
00123 #else
00124           u_init *= (1.0 / GAMMA_MINUS1);
00125 
00126           if(All.InitGasTemp > 1.0e4)   /* assuming FULL ionization */
00127             molecular_weight = 4 / (8 - 5 * (1 - HYDROGEN_MASSFRAC));
00128           else                  /* assuming NEUTRAL GAS */
00129             molecular_weight = 4 / (1 + 3 * HYDROGEN_MASSFRAC);
00130 
00131           u_init /= molecular_weight;
00132 #endif
00133 
00134           for(i = 0; i < N_gas; i++)
00135             {
00136               if(SphP[i].Entropy == 0)
00137                 SphP[i].Entropy = u_init;
00138 
00139               /* Note: the coversion to entropy will be done in the function init(),
00140                  after the densities have been computed */
00141             }
00142         }
00143     }
00144 
00145   for(i = 0; i < N_gas; i++)
00146     SphP[i].Entropy = dmax(All.MinEgySpec, SphP[i].Entropy);
00147 
00148   MPI_Barrier(MPI_COMM_WORLD);
00149 
00150   if(ThisTask == 0)
00151     {
00152       printf("reading done.\n");
00153       fflush(stdout);
00154     }
00155 
00156   if(ThisTask == 0)
00157     {
00158       printf("Total number of particles :  %d%09d\n\n",
00159              (int) (All.TotNumPart / 1000000000), (int) (All.TotNumPart % 1000000000));
00160       fflush(stdout);
00161     }
00162 }
00163 
00164 
00168 void empty_read_buffer(enum iofields blocknr, int offset, int pc, int type)
00169 {
00170   int n, k;
00171   float *fp;
00172 
00173 #ifdef LONGIDS
00174   long long *ip;
00175 #else
00176   int *ip;
00177 #endif
00178 
00179   fp = CommBuffer;
00180   ip = CommBuffer;
00181 
00182   switch (blocknr)
00183     {
00184     case IO_POS:                /* positions */
00185       for(n = 0; n < pc; n++)
00186         for(k = 0; k < 3; k++)
00187           P[offset + n].Pos[k] = *fp++;
00188 
00189       for(n = 0; n < pc; n++)
00190         P[offset + n].Type = type;      /* initialize type here as well */
00191       break;
00192 
00193     case IO_VEL:                /* velocities */
00194       for(n = 0; n < pc; n++)
00195         for(k = 0; k < 3; k++)
00196           P[offset + n].Vel[k] = *fp++;
00197       break;
00198 
00199     case IO_ID:         /* particle ID */
00200       for(n = 0; n < pc; n++)
00201         P[offset + n].ID = *ip++;
00202       break;
00203 
00204     case IO_MASS:               /* particle mass */
00205       for(n = 0; n < pc; n++)
00206         P[offset + n].Mass = *fp++;
00207       break;
00208 
00209     case IO_U:                  /* temperature */
00210       for(n = 0; n < pc; n++)
00211         SphP[offset + n].Entropy = *fp++;
00212       break;
00213 
00214     case IO_RHO:                /* density */
00215       for(n = 0; n < pc; n++)
00216         SphP[offset + n].Density = *fp++;
00217       break;
00218 
00219 
00220     case IO_HSML:               /* SPH smoothing length */
00221       for(n = 0; n < pc; n++)
00222         SphP[offset + n].Hsml = *fp++;
00223       break;
00224 
00225 
00226 
00227 
00228       /* the other input fields (if present) are not needed to define the 
00229          initial conditions of the code */
00230 
00231     case IO_POT:
00232     case IO_ACCEL:
00233     case IO_DTENTR:
00234     case IO_TSTP:
00235       break;
00236     }
00237 }
00238 
00239 
00240 
00244 void read_file(char *fname, int readTask, int lastTask)
00245 {
00246   int blockmaxlen;
00247   int i, n_in_file, n_for_this_task, ntask, pc, offset = 0, task;
00248   int blksize1, blksize2;
00249   MPI_Status status;
00250   FILE *fd = 0;
00251   int nall;
00252   int type;
00253   char label[4];
00254   int nstart, bytes_per_blockelement, npart, nextblock, typelist[6];
00255   enum iofields blocknr;
00256 
00257 #ifdef HAVE_HDF5
00258   char buf[500];
00259   int rank, pcsum;
00260   hid_t hdf5_file, hdf5_grp[6], hdf5_dataspace_in_file;
00261   hid_t hdf5_datatype, hdf5_dataspace_in_memory, hdf5_dataset;
00262   hsize_t dims[2], count[2], start[2];
00263 #endif
00264 
00265 #define SKIP  {my_fread(&blksize1,sizeof(int),1,fd);}
00266 #define SKIP2  {my_fread(&blksize2,sizeof(int),1,fd);}
00267 
00268   if(ThisTask == readTask)
00269     {
00270       if(All.ICFormat == 1 || All.ICFormat == 2)
00271         {
00272           if(!(fd = fopen(fname, "r")))
00273             {
00274               printf("can't open file `%s' for reading initial conditions.\n", fname);
00275               endrun(123);
00276             }
00277 
00278           if(All.ICFormat == 2)
00279             {
00280               SKIP;
00281               my_fread(&label, sizeof(char), 4, fd);
00282               my_fread(&nextblock, sizeof(int), 1, fd);
00283               printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2], label[3],
00284                      nextblock);
00285               SKIP2;
00286             }
00287 
00288           SKIP;
00289           my_fread(&header, sizeof(header), 1, fd);
00290           SKIP2;
00291 
00292           if(blksize1 != 256 || blksize2 != 256)
00293             {
00294               printf("incorrect header format\n");
00295               fflush(stdout);
00296               endrun(890);
00297             }
00298         }
00299 
00300 
00301 #ifdef HAVE_HDF5
00302       if(All.ICFormat == 3)
00303         {
00304           read_header_attributes_in_hdf5(fname);
00305 
00306           hdf5_file = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
00307 
00308           for(type = 0; type < 6; type++)
00309             {
00310               if(header.npart[type] > 0)
00311                 {
00312                   sprintf(buf, "/PartType%d", type);
00313                   hdf5_grp[type] = H5Gopen(hdf5_file, buf);
00314                 }
00315             }
00316         }
00317 #endif
00318 
00319       for(task = readTask + 1; task <= lastTask; task++)
00320         MPI_Ssend(&header, sizeof(header), MPI_BYTE, task, TAG_HEADER, MPI_COMM_WORLD);
00321     }
00322   else
00323     MPI_Recv(&header, sizeof(header), MPI_BYTE, readTask, TAG_HEADER, MPI_COMM_WORLD, &status);
00324 
00325 
00326   if(All.TotNumPart == 0)
00327     {
00328       if(header.num_files <= 1)
00329         for(i = 0; i < 6; i++)
00330           header.npartTotal[i] = header.npart[i];
00331 
00332       All.TotN_gas = header.npartTotal[0] + (((long long) header.npartTotalHighWord[0]) << 32);
00333 
00334       for(i = 0, All.TotNumPart = 0; i < 6; i++)
00335         {
00336           All.TotNumPart += header.npartTotal[i];
00337           All.TotNumPart += (((long long) header.npartTotalHighWord[i]) << 32);
00338         }
00339 
00340 
00341       for(i = 0; i < 6; i++)
00342         All.MassTable[i] = header.mass[i];
00343 
00344       All.MaxPart = All.PartAllocFactor * (All.TotNumPart / NTask);     /* sets the maximum number of particles that may */
00345       All.MaxPartSph = All.PartAllocFactor * (All.TotN_gas / NTask);    /* sets the maximum number of particles that may 
00346                                                                            reside on a processor */
00347       allocate_memory();
00348 
00349       if(RestartFlag == 2)
00350         All.Time = All.TimeBegin = header.time;
00351     }
00352 
00353   if(ThisTask == readTask)
00354     {
00355       for(i = 0, n_in_file = 0; i < 6; i++)
00356         n_in_file += header.npart[i];
00357 
00358       printf("\nreading file `%s' on task=%d (contains %d particles.)\n"
00359              "distributing this file to tasks %d-%d\n"
00360              "Type 0 (gas):   %8d  (tot=%6d%09d) masstab=%g\n"
00361              "Type 1 (halo):  %8d  (tot=%6d%09d) masstab=%g\n"
00362              "Type 2 (disk):  %8d  (tot=%6d%09d) masstab=%g\n"
00363              "Type 3 (bulge): %8d  (tot=%6d%09d) masstab=%g\n"
00364              "Type 4 (stars): %8d  (tot=%6d%09d) masstab=%g\n"
00365              "Type 5 (bndry): %8d  (tot=%6d%09d) masstab=%g\n\n", fname, ThisTask, n_in_file, readTask,
00366              lastTask, header.npart[0], (int) (header.npartTotal[0] / 1000000000),
00367              (int) (header.npartTotal[0] % 1000000000), All.MassTable[0], header.npart[1],
00368              (int) (header.npartTotal[1] / 1000000000), (int) (header.npartTotal[1] % 1000000000),
00369              All.MassTable[1], header.npart[2], (int) (header.npartTotal[2] / 1000000000),
00370              (int) (header.npartTotal[2] % 1000000000), All.MassTable[2], header.npart[3],
00371              (int) (header.npartTotal[3] / 1000000000), (int) (header.npartTotal[3] % 1000000000),
00372              All.MassTable[3], header.npart[4], (int) (header.npartTotal[4] / 1000000000),
00373              (int) (header.npartTotal[4] % 1000000000), All.MassTable[4], header.npart[5],
00374              (int) (header.npartTotal[5] / 1000000000), (int) (header.npartTotal[5] % 1000000000),
00375              All.MassTable[5]);
00376       fflush(stdout);
00377     }
00378 
00379 
00380   ntask = lastTask - readTask + 1;
00381 
00382 
00383   /* to collect the gas particles all at the beginning (in case several
00384      snapshot files are read on the current CPU) we move the collisionless
00385      particles such that a gap of the right size is created */
00386 
00387   for(type = 0, nall = 0; type < 6; type++)
00388     {
00389       n_in_file = header.npart[type];
00390 
00391       n_for_this_task = n_in_file / ntask;
00392       if((ThisTask - readTask) < (n_in_file % ntask))
00393         n_for_this_task++;
00394 
00395       nall += n_for_this_task;
00396     }
00397 
00398   memmove(&P[N_gas + nall], &P[N_gas], (NumPart - N_gas) * sizeof(struct particle_data));
00399   nstart = N_gas;
00400 
00401 
00402 
00403   for(blocknr = 0; blocknr < IO_NBLOCKS; blocknr++)
00404     {
00405       if(blockpresent(blocknr))
00406         {
00407           if(RestartFlag == 0 && blocknr > IO_U)
00408             continue;           /* ignore all other blocks in initial conditions */
00409 
00410           bytes_per_blockelement = get_bytes_per_blockelement(blocknr);
00411 
00412           blockmaxlen = ((int) (All.BufferSize * 1024 * 1024)) / bytes_per_blockelement;
00413 
00414           npart = get_particles_in_block(blocknr, &typelist[0]);
00415 
00416           if(npart > 0)
00417             {
00418               if(ThisTask == readTask)
00419                 {
00420                   if(All.ICFormat == 2)
00421                     {
00422                       SKIP;
00423                       my_fread(&label, sizeof(char), 4, fd);
00424                       my_fread(&nextblock, sizeof(int), 1, fd);
00425                       printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2],
00426                              label[3], nextblock);
00427                       SKIP2;
00428 
00429                       if(strncmp(label, Tab_IO_Labels[blocknr], 4) != 0)
00430                         {
00431                           printf("incorrect block-structure!\n");
00432                           printf("expected '%c%c%c%c' but found '%c%c%c%c'\n",
00433                                  label[0], label[1], label[2], label[3],
00434                                  Tab_IO_Labels[blocknr][0], Tab_IO_Labels[blocknr][1],
00435                                  Tab_IO_Labels[blocknr][2], Tab_IO_Labels[blocknr][3]);
00436                           fflush(stdout);
00437                           endrun(1890);
00438                         }
00439                     }
00440 
00441                   if(All.ICFormat == 1 || All.ICFormat == 2)
00442                     SKIP;
00443                 }
00444 
00445               for(type = 0, offset = 0; type < 6; type++)
00446                 {
00447                   n_in_file = header.npart[type];
00448 #ifdef HAVE_HDF5
00449                   pcsum = 0;
00450 #endif
00451                   if(typelist[type] == 0)
00452                     {
00453                       n_for_this_task = n_in_file / ntask;
00454                       if((ThisTask - readTask) < (n_in_file % ntask))
00455                         n_for_this_task++;
00456 
00457                       offset += n_for_this_task;
00458                     }
00459                   else
00460                     {
00461                       for(task = readTask; task <= lastTask; task++)
00462                         {
00463                           n_for_this_task = n_in_file / ntask;
00464                           if((task - readTask) < (n_in_file % ntask))
00465                             n_for_this_task++;
00466 
00467                           if(task == ThisTask)
00468                             if(NumPart + n_for_this_task > All.MaxPart)
00469                               {
00470                                 printf("too many particles\n");
00471                                 endrun(1313);
00472                               }
00473 
00474 
00475                           do
00476                             {
00477                               pc = n_for_this_task;
00478 
00479                               if(pc > blockmaxlen)
00480                                 pc = blockmaxlen;
00481 
00482                               if(ThisTask == readTask)
00483                                 {
00484                                   if(All.ICFormat == 1 || All.ICFormat == 2)
00485                                     my_fread(CommBuffer, bytes_per_blockelement, pc, fd);
00486 #ifdef HAVE_HDF5
00487                                   if(All.ICFormat == 3)
00488                                     {
00489                                       get_dataset_name(blocknr, buf);
00490                                       hdf5_dataset = H5Dopen(hdf5_grp[type], buf);
00491 
00492                                       dims[0] = header.npart[type];
00493                                       dims[1] = get_values_per_blockelement(blocknr);
00494                                       if(dims[1] == 1)
00495                                         rank = 1;
00496                                       else
00497                                         rank = 2;
00498 
00499                                       hdf5_dataspace_in_file = H5Screate_simple(rank, dims, NULL);
00500 
00501                                       dims[0] = pc;
00502                                       hdf5_dataspace_in_memory = H5Screate_simple(rank, dims, NULL);
00503 
00504                                       start[0] = pcsum;
00505                                       start[1] = 0;
00506 
00507                                       count[0] = pc;
00508                                       count[1] = get_values_per_blockelement(blocknr);
00509                                       pcsum += pc;
00510 
00511                                       H5Sselect_hyperslab(hdf5_dataspace_in_file, H5S_SELECT_SET,
00512                                                           start, NULL, count, NULL);
00513 
00514                                       switch (get_datatype_in_block(blocknr))
00515                                         {
00516                                         case 0:
00517                                           hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT);
00518                                           break;
00519                                         case 1:
00520                                           hdf5_datatype = H5Tcopy(H5T_NATIVE_FLOAT);
00521                                           break;
00522                                         case 2:
00523                                           hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT64);
00524                                           break;
00525                                         }
00526 
00527                                       H5Dread(hdf5_dataset, hdf5_datatype, hdf5_dataspace_in_memory,
00528                                               hdf5_dataspace_in_file, H5P_DEFAULT, CommBuffer);
00529 
00530                                       H5Tclose(hdf5_datatype);
00531                                       H5Sclose(hdf5_dataspace_in_memory);
00532                                       H5Sclose(hdf5_dataspace_in_file);
00533                                       H5Dclose(hdf5_dataset);
00534                                     }
00535 #endif
00536                                 }
00537 
00538                               if(ThisTask == readTask && task != readTask)
00539                                 MPI_Ssend(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, task, TAG_PDATA,
00540                                           MPI_COMM_WORLD);
00541 
00542                               if(ThisTask != readTask && task == ThisTask)
00543                                 MPI_Recv(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, readTask,
00544                                          TAG_PDATA, MPI_COMM_WORLD, &status);
00545 
00546                               if(ThisTask == task)
00547                                 {
00548                                   empty_read_buffer(blocknr, nstart + offset, pc, type);
00549 
00550                                   offset += pc;
00551                                 }
00552 
00553                               n_for_this_task -= pc;
00554                             }
00555                           while(n_for_this_task > 0);
00556                         }
00557                     }
00558                 }
00559               if(ThisTask == readTask)
00560                 {
00561                   if(All.ICFormat == 1 || All.ICFormat == 2)
00562                     {
00563                       SKIP2;
00564                       if(blksize1 != blksize2)
00565                         {
00566                           printf("incorrect block-sizes detected!\n");
00567                           printf("Task=%d   blocknr=%d  blksize1=%d  blksize2=%d\n", ThisTask, blocknr,
00568                                  blksize1, blksize2);
00569                           fflush(stdout);
00570                           endrun(1889);
00571                         }
00572                     }
00573                 }
00574             }
00575         }
00576     }
00577 
00578 
00579   for(type = 0; type < 6; type++)
00580     {
00581       n_in_file = header.npart[type];
00582 
00583       n_for_this_task = n_in_file / ntask;
00584       if((ThisTask - readTask) < (n_in_file % ntask))
00585         n_for_this_task++;
00586 
00587       NumPart += n_for_this_task;
00588 
00589       if(type == 0)
00590         N_gas += n_for_this_task;
00591     }
00592 
00593   if(ThisTask == readTask)
00594     {
00595       if(All.ICFormat == 1 || All.ICFormat == 2)
00596         fclose(fd);
00597 #ifdef HAVE_HDF5
00598       if(All.ICFormat == 3)
00599         {
00600           for(type = 5; type >= 0; type--)
00601             if(header.npart[type] > 0)
00602               H5Gclose(hdf5_grp[type]);
00603           H5Fclose(hdf5_file);
00604         }
00605 #endif
00606     }
00607 }
00608 
00609 
00610 
00611 
00615 int find_files(char *fname)
00616 {
00617   FILE *fd;
00618   char buf[200], buf1[200];
00619   int dummy;
00620 
00621   sprintf(buf, "%s.%d", fname, 0);
00622   sprintf(buf1, "%s", fname);
00623 
00624   if(All.ICFormat == 3)
00625     {
00626       sprintf(buf, "%s.%d.hdf5", fname, 0);
00627       sprintf(buf1, "%s.hdf5", fname);
00628     }
00629 
00630 #ifndef  HAVE_HDF5
00631   if(All.ICFormat == 3)
00632     {
00633       if(ThisTask == 0)
00634         printf("Code wasn't compiled with HDF5 support enabled!\n");
00635       endrun(0);
00636     }
00637 #endif
00638 
00639   header.num_files = 0;
00640 
00641   if(ThisTask == 0)
00642     {
00643       if((fd = fopen(buf, "r")))
00644         {
00645           if(All.ICFormat == 1 || All.ICFormat == 2)
00646             {
00647               if(All.ICFormat == 2)
00648                 {
00649                   fread(&dummy, sizeof(dummy), 1, fd);
00650                   fread(&dummy, sizeof(dummy), 1, fd);
00651                   fread(&dummy, sizeof(dummy), 1, fd);
00652                   fread(&dummy, sizeof(dummy), 1, fd);
00653                 }
00654 
00655               fread(&dummy, sizeof(dummy), 1, fd);
00656               fread(&header, sizeof(header), 1, fd);
00657               fread(&dummy, sizeof(dummy), 1, fd);
00658             }
00659           fclose(fd);
00660 
00661 #ifdef HAVE_HDF5
00662           if(All.ICFormat == 3)
00663             read_header_attributes_in_hdf5(buf);
00664 #endif
00665         }
00666     }
00667 
00668   MPI_Bcast(&header, sizeof(header), MPI_BYTE, 0, MPI_COMM_WORLD);
00669 
00670   if(header.num_files > 0)
00671     return header.num_files;
00672 
00673   if(ThisTask == 0)
00674     {
00675       if((fd = fopen(buf1, "r")))
00676         {
00677           if(All.ICFormat == 1 || All.ICFormat == 2)
00678             {
00679               if(All.ICFormat == 2)
00680                 {
00681                   fread(&dummy, sizeof(dummy), 1, fd);
00682                   fread(&dummy, sizeof(dummy), 1, fd);
00683                   fread(&dummy, sizeof(dummy), 1, fd);
00684                   fread(&dummy, sizeof(dummy), 1, fd);
00685                 }
00686 
00687               fread(&dummy, sizeof(dummy), 1, fd);
00688               fread(&header, sizeof(header), 1, fd);
00689               fread(&dummy, sizeof(dummy), 1, fd);
00690             }
00691           fclose(fd);
00692 
00693 #ifdef HAVE_HDF5
00694           if(All.ICFormat == 3)
00695             read_header_attributes_in_hdf5(buf1);
00696 #endif
00697           header.num_files = 1;
00698         }
00699     }
00700 
00701   MPI_Bcast(&header, sizeof(header), MPI_BYTE, 0, MPI_COMM_WORLD);
00702 
00703   if(header.num_files > 0)
00704     return header.num_files;
00705 
00706   if(ThisTask == 0)
00707     {
00708       printf("\nCan't find initial conditions file.");
00709       printf("neither as '%s'\nnor as '%s'\n", buf, buf1);
00710       fflush(stdout);
00711     }
00712 
00713   endrun(0);
00714   return 0;
00715 }
00716 
00717 
00718 
00724 void distribute_file(int nfiles, int firstfile, int firsttask, int lasttask, int *filenr, int *master,
00725                      int *last)
00726 {
00727   int ntask, filesleft, filesright, tasksleft, tasksright;
00728 
00729   if(nfiles > 1)
00730     {
00731       ntask = lasttask - firsttask + 1;
00732 
00733       filesleft = (((double) (ntask / 2)) / ntask) * nfiles;
00734       if(filesleft <= 0)
00735         filesleft = 1;
00736       if(filesleft >= nfiles)
00737         filesleft = nfiles - 1;
00738 
00739       filesright = nfiles - filesleft;
00740 
00741       tasksleft = ntask / 2;
00742       tasksright = ntask - tasksleft;
00743 
00744       distribute_file(filesleft, firstfile, firsttask, firsttask + tasksleft - 1, filenr, master, last);
00745       distribute_file(filesright, firstfile + filesleft, firsttask + tasksleft, lasttask, filenr, master,
00746                       last);
00747     }
00748   else
00749     {
00750       if(ThisTask >= firsttask && ThisTask <= lasttask)
00751         {
00752           *filenr = firstfile;
00753           *master = firsttask;
00754           *last = lasttask;
00755         }
00756     }
00757 }
00758 
00759 
00763 #ifdef HAVE_HDF5
00764 void read_header_attributes_in_hdf5(char *fname)
00765 {
00766   hid_t hdf5_file, hdf5_headergrp, hdf5_attribute;
00767 
00768 
00769   hdf5_file = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
00770   hdf5_headergrp = H5Gopen(hdf5_file, "/Header");
00771 
00772 
00773   hdf5_attribute = H5Aopen_name(hdf5_headergrp, "NumPart_ThisFile");
00774   H5Aread(hdf5_attribute, H5T_NATIVE_INT, header.npart);
00775   H5Aclose(hdf5_attribute);
00776 
00777   hdf5_attribute = H5Aopen_name(hdf5_headergrp, "NumPart_Total");
00778   H5Aread(hdf5_attribute, H5T_NATIVE_UINT, header.npartTotal);
00779   H5Aclose(hdf5_attribute);
00780 
00781   hdf5_attribute = H5Aopen_name(hdf5_headergrp, "NumPart_Total_HighWord");
00782   H5Aread(hdf5_attribute, H5T_NATIVE_UINT, header.npartTotalHighWord);
00783   H5Aclose(hdf5_attribute);
00784 
00785 
00786   hdf5_attribute = H5Aopen_name(hdf5_headergrp, "MassTable");
00787   H5Aread(hdf5_attribute, H5T_NATIVE_DOUBLE, header.mass);
00788   H5Aclose(hdf5_attribute);
00789 
00790   hdf5_attribute = H5Aopen_name(hdf5_headergrp, "NumFilesPerSnapshot");
00791   H5Aread(hdf5_attribute, H5T_NATIVE_INT, &header.num_files);
00792   H5Aclose(hdf5_attribute);
00793 
00794   hdf5_attribute = H5Aopen_name(hdf5_headergrp, "Flag_Entropy_ICs");
00795   H5Aread(hdf5_attribute, H5T_NATIVE_INT, &header.flag_entropy_instead_u);
00796   H5Aclose(hdf5_attribute);
00797 
00798   H5Gclose(hdf5_headergrp);
00799   H5Fclose(hdf5_file);
00800 }
00801 #endif

Generated on Sun May 22 17:33:29 2005 for GADGET-2 by  doxygen 1.3.9.1