/* read new bodies data */ static void read_new_bodies (DOM *dom, PBF *bf) { #if HDF5 double time, start, end; PBF_Time (bf, &time); /* back up time frame from outside of this function */ PBF_Limits (bf, &start, &end); PBF_Seek (bf, start); do { for (PBF *f = bf; f; f = f->next) { int ipos = 0, ints; int dpos = 0, doubles; double *d; int *i; int k, n; BODY *bod; if (PBF_Has_Group (f, "NEWBOD") == 0) continue; /* don't try to read if there are no new bodies stored */ PBF_Push (f, "NEWBOD"); PBF_Int2 (f, "count", &n, 1); PBF_Int2 (f, "ints", &ints, 1); ERRMEM (i = malloc (sizeof (int [ints]))); PBF_Int2 (f, "i", i, ints); PBF_Int2 (f, "doubles", &doubles, 1); ERRMEM (d = malloc (sizeof (double [doubles]))); PBF_Double2 (f, "d", d, doubles); for (k = 0; k < n; k ++) { bod = BODY_Unpack (dom->solfec, &dpos, d, doubles, &ipos, i, ints); if (!MAP_Find (dom->allbodies, (void*) (long) bod->id, NULL)) { MAP_Insert (&dom->mapmem, &dom->allbodies, (void*) (long) bod->id, bod, NULL); /* all bodies from all times */ } else BODY_Destroy (bod); /* FIXME: bodies created in input files at time > 0; FIXME: perhaps there is no need of moving GLV to the fist lng_RUN call, FIXME: but rather bodies created in Python should not be put into the 'dom->newb' set; FIXME: this way, as now, all Python created bodies will be anyway read at time 0 */ } free (d); free (i); PBF_Pop (f); } } while (PBF_Forward (bf, 1)); dom->allbodiesread = 1; /* mark as read */ PBF_Seek (bf, time); /* seek to the backed up time again */ #else char *path, *ext; FILE *file; int m, n; XDR xdr; if (dom->solfec->verbose) printf ("Reading all bodies ...\n"); dom->allbodiesread = 1; /* mark as read */ path = SOLFEC_Alloc_File_Name (dom->solfec, 16); ext = path + strlen (path); for (m = 0; bf; bf = bf->next) m ++; /* count input files */ for (n = 0; n < m; n ++) { if (n || m > 1) { sprintf (ext, ".bod.%d", n); if (!(file = fopen (path, "r"))) continue; /* no new bodies for this rank */ } else /* n == 0 && m == 1 */ { sprintf (ext, ".bod.%d", n); if (!(file = fopen (path, "r"))) /* either prallel with "mpirun -np 1" */ { sprintf (ext, ".bod"); if (!(file = fopen (path, "r"))) continue; /* or serial */ } } xdrstdio_create (&xdr, file, XDR_DECODE); int ipos, ints, *i, dpos, doubles; double *d; BODY *bod; for (;;) { if (xdr_int (&xdr, &doubles)) { ERRMEM (d = malloc (sizeof (double [doubles]))); if (xdr_vector (&xdr, (char*)d, doubles, sizeof (double), (xdrproc_t)xdr_double)) { if (xdr_int (&xdr, &ints)) { ERRMEM (i = malloc (sizeof (int [ints]))); if (xdr_vector (&xdr, (char*)i, ints, sizeof (int), (xdrproc_t)xdr_int)) { ipos = dpos = 0; bod = BODY_Unpack (dom->solfec, &dpos, d, doubles, &ipos, i, ints); if (!MAP_Find (dom->allbodies, (void*) (long) bod->id, NULL)) { MAP_Insert (&dom->mapmem, &dom->allbodies, (void*) (long) bod->id, bod, NULL); } else BODY_Destroy (bod); /* FIXME: bodies created in input files at time > 0; FIXME: perhaps there is no need of moving GLV to the fist lng_RUN call, FIXME: but rather bodies created in Python should not be put into the 'dom->newb' set; FIXME: this way, as now, all Python created bodies will be anyway read at time 0 */ free (d); free (i); } else { free (d); free (i); break; } } else { free (d); break; } } else { free (d); break; } } else break; } xdr_destroy (&xdr); fclose (file); } free (path); #endif }
int main (int argc, char **argv) { char *s, str [512]; int n, nmax; double d; PBF *bf; if (argc == 1) { printf ("pbftest [NUMBER OF SAMPLES] [COMPRESSION FLAG]\n"); return 0; } if (argc >= 2 && isdigit (argv [1][0])) nmax = atoi (argv [1]); else nmax = 10; bf = PBF_Write ("pbftest.state"); if (argc >= 3) { if (atoi (argv [2]) == 1) { bf->compression = PBF_ON; /* enable compression */ } } for (n = 1; n <= nmax; n ++) write (bf, n); PBF_Close (bf); bf = PBF_Read ("pbftest.state"); for (n = 1; n <= nmax; n ++) { if (!read (bf, n)) { fprintf (stderr, "FAILED (reading all)\n"); return 1; } PBF_Forward (bf, 1); } /* read every third double */ PBF_Seek (bf, 0); for (n = 1; n <= nmax; n += 3) { PBF_Label (bf, "DOUBLE"); PBF_Double (bf, &d, 1); if (d != (double) n) { fprintf (stderr, "FAILED (forward reading every 3rd labeled double)\n"); return 1; } PBF_Forward (bf, 3); } /* read every fifth string */ PBF_Seek (bf, 0); for (n = 1; n <= nmax; n += 5) { PBF_Label (bf, "STRING"); s = NULL; PBF_String (bf, &s); sprintf (str, "CURRENT NUMBER IS %d", n); if (strcmp (s, str) != 0) { fprintf (stderr, "FAILED (forward reading every 5th labeled string)\n"); return 1; } PBF_Forward (bf, 5); free (s); } /* do the same backwards */ PBF_Seek (bf, nmax); for (n = nmax; n >= 1; n -= 3) { PBF_Label (bf, "DOUBLE"); PBF_Double (bf, &d, 1); if (d != (double) n) { fprintf (stderr, "FAILED (backward reading every 3rd labeled double)\n"); return 1; } PBF_Backward (bf, 3); } PBF_Seek (bf, nmax); for (n = nmax; n >= 1; n -= 5) { PBF_Label (bf, "STRING"); s = NULL; PBF_String (bf, &s); sprintf (str, "CURRENT NUMBER IS %d", n); if (strcmp (s, str) != 0) { fprintf (stderr, "FAILED (forward reading every 5th labeled string)\n"); return 1; } PBF_Backward (bf, 5); free (s); } PBF_Close (bf); printf ("PASSED\n"); return 0; }