/* read fracture state */ FS* fracture_state_read (BODY *bod) { FS *out = NULL, *item, *instance; char path [1024]; unsigned int id; int i, n, dofs; double *disp; #if HDF5 PBF *f, *g; snprintf (path, 1024, "%s/fracture", bod->dom->solfec->outpath); g = PBF_Read (path); do { double time; PBF_Time (g, &time); /* unused, but could be useful at some point */ for (f = g; f; f = f->next) { int numbod; PBF_Int2 (f, "numbod", &numbod, 1); while (numbod > 0) { PBF_Uint (f, &id, 1); PBF_Int (f, &dofs, 1); ERRMEM (disp = malloc (dofs * sizeof (double))); PBF_Double (f, disp, dofs); PBF_Int (f, &n, 1); for (i = 0, instance = NULL; i < n; i ++) { ERRMEM (item = MEM_CALLOC (sizeof (FS))); PBF_Double (f, &item->radius, 1); PBF_Double (f, item->point, 3); PBF_Double (f, item->force, 3); if (id == bod->id) { item->inext = instance; instance = item; if (i == (n-1)) { item->disp = disp; /* put displacements into first element of instance list */ item->next = out; out = item; } } else free (item); } if (!out || out->disp != disp) free (disp); /* not used */ numbod --; } } } while (PBF_Forward (g, 1)); PBF_Close (g); #else FILE *f; XDR x; snprintf (path, 1024, "%s/fracture.dat", bod->dom->solfec->outpath); f = fopen (path, "r"); /* TODO: read MPI mode data in case f == NULL but fractureRANK.dat exit */ if (f) { xdrstdio_create (&x, f, XDR_DECODE); while (! feof (f)) { if (xdr_u_int (&x, &id) == 0) break; ASSERT (xdr_int (&x, &dofs), ERR_FILE_READ); ERRMEM (disp = malloc (dofs * sizeof (double))); ASSERT (xdr_vector (&x, (char*)disp, dofs, sizeof (double), (xdrproc_t)xdr_double), ERR_FILE_READ); ASSERT (xdr_int (&x, &n), ERR_FILE_READ); for (i = 0, instance = NULL; i < n; i ++) { ERRMEM (item = MEM_CALLOC (sizeof (FS))); ASSERT (xdr_double (&x, &item->radius), ERR_FILE_READ); ASSERT (xdr_vector (&x, (char*)item->point, 3, sizeof (double), (xdrproc_t)xdr_double), ERR_FILE_READ); ASSERT (xdr_vector (&x, (char*)item->force, 3, sizeof (double), (xdrproc_t)xdr_double), ERR_FILE_READ); if (id == bod->id) { item->inext = instance; instance = item; if (i == (n-1)) { item->disp = disp; /* put displacements into first element of instance list */ item->next = out; out = item; } } else free (item); } if (!out || out->disp != disp) free (disp); /* not used */ } xdr_destroy (&x); fclose (f); } #endif return out; }
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; }