• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/mg/vgrid.c

Go to the documentation of this file.
00001 
00049 #include "apbscfg.h"
00050 #include "apbs/vgrid.h"
00051 
00052 VEMBED(rcsid="$Id: vgrid.c 1608 2010-10-01 18:39:30Z sobolevnrm $")
00053 
00054 #if !defined(VINLINE_VGRID)
00055     VPUBLIC unsigned long int Vgrid_memChk(Vgrid *thee) { 
00056         return Vmem_bytes(thee->mem); 
00057     }
00058 #endif
00059 #define IJK(i,j,k)  (((k)*(nx)*(ny))+((j)*(nx))+(i))
00060 
00061 VPRIVATE char *MCwhiteChars = " =,;\t\n";
00062 VPRIVATE char *MCcommChars  = "#%";
00063 VPRIVATE double Vcompare;
00064 VPRIVATE char Vprecision[26];
00065 
00066 /* ///////////////////////////////////////////////////////////////////////////
00067 // Routine:  Vgrid_ctor
00068 // Author:   Nathan Baker
00070 VPUBLIC Vgrid* Vgrid_ctor(int nx, int ny, int nz,
00071                   double hx, double hy, double hzed,
00072                   double xmin, double ymin, double zmin,
00073                   double *data) {
00074 
00075     Vgrid *thee = VNULL;
00076 
00077     thee = Vmem_malloc(VNULL, 1, sizeof(Vgrid));
00078     VASSERT(thee != VNULL);
00079     VASSERT(Vgrid_ctor2(thee, nx, ny, nz, hx, hy, hzed,
00080                   xmin, ymin, zmin, data));
00081 
00082     return thee;
00083 }
00084 
00085 /* ///////////////////////////////////////////////////////////////////////////
00086 // Routine:  Vgrid_ctor2
00087 // Author:   Nathan Baker
00089 VPUBLIC int Vgrid_ctor2(Vgrid *thee, int nx, int ny, int nz,
00090                   double hx, double hy, double hzed,
00091                   double xmin, double ymin, double zmin,
00092                   double *data) {
00093 
00094     if (thee == VNULL) return 0;
00095     thee->nx = nx;
00096     thee->ny = ny;
00097     thee->nz = nz;
00098     thee->hx = hx;
00099     thee->hy = hy;
00100     thee->hzed = hzed;
00101     thee->xmin = xmin;
00102     thee->xmax = xmin + (nx-1)*hx;
00103     thee->ymin = ymin;
00104     thee->ymax = ymin + (ny-1)*hy;
00105     thee->zmin = zmin;
00106     thee->zmax = zmin + (nz-1)*hzed;
00107     if (data == VNULL) {
00108         thee->ctordata = 0;
00109         thee->readdata = 0;
00110     } else {
00111         thee->ctordata = 1;
00112         thee->readdata = 0;
00113         thee->data = data;
00114     }
00115 
00116     thee->mem = Vmem_ctor("APBS:VGRID");
00117 
00118     Vcompare = pow(10,-1*(VGRID_DIGITS - 2));
00119     sprintf(Vprecision,"%%12.%de %%12.%de %%12.%de", VGRID_DIGITS,
00120             VGRID_DIGITS, VGRID_DIGITS);
00121 
00122     return 1;
00123 }
00124 
00125 /* ///////////////////////////////////////////////////////////////////////////
00126 // Routine:  Vgrid_dtor
00127 // Author:   Nathan Baker
00129 VPUBLIC void Vgrid_dtor(Vgrid **thee) {
00130 
00131     if ((*thee) != VNULL) {
00132         Vgrid_dtor2(*thee);
00133         Vmem_free(VNULL, 1, sizeof(Vgrid), (void **)thee);
00134         (*thee) = VNULL;
00135     }
00136 }
00137 
00138 /* ///////////////////////////////////////////////////////////////////////////
00139 // Routine:  Vgrid_dtor2
00140 // Author:   Nathan Baker
00142 VPUBLIC void Vgrid_dtor2(Vgrid *thee) { 
00143 
00144     if (thee->readdata) {
00145         Vmem_free(thee->mem, (thee->nx*thee->ny*thee->nz), sizeof(double),
00146           (void **)&(thee->data));
00147     }
00148     Vmem_dtor(&(thee->mem));
00149  
00150 }
00151 
00152 /* ///////////////////////////////////////////////////////////////////////////
00153 // Routine:  Vgrid_value
00154 // Author:   Nathan Baker
00156 VPUBLIC int Vgrid_value(Vgrid *thee, double pt[3], double *value) {
00157 
00158     int nx, ny, nz, ihi, jhi, khi, ilo, jlo, klo;
00159     double hx, hy, hzed, xmin, ymin, zmin, ifloat, jfloat, kfloat;
00160     double xmax, ymax, zmax;
00161     double u, dx, dy, dz;
00162 
00163     if (thee == VNULL) {
00164         Vnm_print(2, "Vgrid_value:  Error -- got VNULL thee!\n");
00165         VASSERT(0);
00166     }
00167     if (!(thee->ctordata || thee->readdata)) {
00168         Vnm_print(2, "Vgrid_value:  Error -- no data available!\n");
00169         VASSERT(0);
00170     }
00171 
00172     nx = thee->nx;
00173     ny = thee->ny;
00174     nz = thee->nz;
00175     hx = thee->hx;
00176     hy = thee->hy;
00177     hzed = thee->hzed;
00178     xmin = thee->xmin;
00179     ymin = thee->ymin;
00180     zmin = thee->zmin;
00181     xmax = thee->xmax;
00182     ymax = thee->ymax;
00183     zmax = thee->zmax;
00184 
00185     u = 0;
00186    
00187     ifloat = (pt[0] - xmin)/hx;
00188     jfloat = (pt[1] - ymin)/hy;
00189     kfloat = (pt[2] - zmin)/hzed;
00190 
00191     ihi = (int)ceil(ifloat);
00192     jhi = (int)ceil(jfloat);
00193     khi = (int)ceil(kfloat);
00194     ilo = (int)floor(ifloat);
00195     jlo = (int)floor(jfloat);
00196     klo = (int)floor(kfloat);
00197  if (VABS(pt[0] - xmin) < Vcompare) ilo = 0;
00198  if (VABS(pt[1] - ymin) < Vcompare) jlo = 0;
00199  if (VABS(pt[2] - zmin) < Vcompare) klo = 0;
00200  if (VABS(pt[0] - xmax) < Vcompare) ihi = nx-1;
00201  if (VABS(pt[1] - ymax) < Vcompare) jhi = ny-1;
00202  if (VABS(pt[2] - zmax) < Vcompare) khi = nz-1;
00203  
00204     /* See if we're on the mesh */
00205     if ((ihi<nx) && (jhi<ny) && (khi<nz) &&
00206         (ilo>=0) && (jlo>=0) && (klo>=0)) {
00207 
00208         dx = ifloat - (double)(ilo);
00209         dy = jfloat - (double)(jlo);
00210         dz = kfloat - (double)(klo);
00211         u = dx      *dy      *dz      *(thee->data[IJK(ihi,jhi,khi)])
00212           + dx      *(1.0-dy)*dz      *(thee->data[IJK(ihi,jlo,khi)])
00213           + dx      *dy      *(1.0-dz)*(thee->data[IJK(ihi,jhi,klo)])
00214           + dx      *(1.0-dy)*(1.0-dz)*(thee->data[IJK(ihi,jlo,klo)])
00215           + (1.0-dx)*dy      *dz      *(thee->data[IJK(ilo,jhi,khi)])
00216           + (1.0-dx)*(1.0-dy)*dz      *(thee->data[IJK(ilo,jlo,khi)])
00217           + (1.0-dx)*dy      *(1.0-dz)*(thee->data[IJK(ilo,jhi,klo)])
00218           + (1.0-dx)*(1.0-dy)*(1.0-dz)*(thee->data[IJK(ilo,jlo,klo)]);
00219 
00220         *value = u;
00221 
00222         if (isnan(u)) {
00223             Vnm_print(2, "Vgrid_value:  Got NaN!\n");
00224             Vnm_print(2, "Vgrid_value:  (x, y, z) = (%4.3f, %4.3f, %4.3f)\n", 
00225                     pt[0], pt[1], pt[2]);
00226             Vnm_print(2, "Vgrid_value:  (ihi, jhi, khi) = (%d, %d, %d)\n", 
00227                     ihi, jhi, khi);
00228             Vnm_print(2, "Vgrid_value:  (ilo, jlo, klo) = (%d, %d, %d)\n", 
00229                     ilo, jlo, klo);
00230             Vnm_print(2, "Vgrid_value:  (nx, ny, nz) = (%d, %d, %d)\n", 
00231                     nx, ny, nz);
00232             Vnm_print(2, "Vgrid_value:  (dx, dy, dz) = (%4.3f, %4.3f, %4.3f)\n", 
00233                     dx, dy, dz);
00234             Vnm_print(2, "Vgrid_value:  data[IJK(ihi,jhi,khi)] = %g\n", 
00235                     thee->data[IJK(ihi,jhi,khi)]);
00236             Vnm_print(2, "Vgrid_value:  data[IJK(ihi,jlo,khi)] = %g\n", 
00237                     thee->data[IJK(ihi,jlo,khi)]);
00238             Vnm_print(2, "Vgrid_value:  data[IJK(ihi,jhi,klo)] = %g\n", 
00239                     thee->data[IJK(ihi,jhi,klo)]);
00240             Vnm_print(2, "Vgrid_value:  data[IJK(ihi,jlo,klo)] = %g\n", 
00241                     thee->data[IJK(ihi,jlo,klo)]);
00242             Vnm_print(2, "Vgrid_value:  data[IJK(ilo,jhi,khi)] = %g\n", 
00243                     thee->data[IJK(ilo,jhi,khi)]);
00244             Vnm_print(2, "Vgrid_value:  data[IJK(ilo,jlo,khi)] = %g\n", 
00245                     thee->data[IJK(ilo,jlo,khi)]);
00246             Vnm_print(2, "Vgrid_value:  data[IJK(ilo,jhi,klo)] = %g\n", 
00247                     thee->data[IJK(ilo,jhi,klo)]);
00248             Vnm_print(2, "Vgrid_value:  data[IJK(ilo,jlo,klo)] = %g\n", 
00249                     thee->data[IJK(ilo,jlo,klo)]);
00250         }
00251         return 1;
00252 
00253     } else {
00254 
00255         *value = 0;
00256         return 0;
00257 
00258     }
00259 
00260     return 0;
00261 
00262 }
00263 
00264 /* ///////////////////////////////////////////////////////////////////////////
00265 // Routine:  Vgrid_curvature
00266 //
00267 //   Notes:  cflag=0 ==> Reduced Maximal Curvature
00268 //           cflag=1 ==> Mean Curvature (Laplace)
00269 //           cflag=2 ==> Gauss Curvature
00270 //           cflag=3 ==> True Maximal Curvature
00271 //
00272 // Authors:  Stephen Bond and Nathan Baker
00274 VPUBLIC int Vgrid_curvature(Vgrid *thee, double pt[3], int cflag, 
00275   double *value) {
00276 
00277     double hx, hy, hzed, curv;
00278     double dxx, dyy, dzz;
00279     double uleft, umid, uright, testpt[3];
00280 
00281     if (thee == VNULL) {
00282         Vnm_print(2, "Vgrid_curvature:  Error -- got VNULL thee!\n");
00283         VASSERT(0);
00284     }
00285     if (!(thee->ctordata || thee->readdata)) {
00286         Vnm_print(2, "Vgrid_curvature:  Error -- no data available!\n");
00287         VASSERT(0);
00288     }
00289 
00290     hx = thee->hx;
00291     hy = thee->hy;
00292     hzed = thee->hzed;
00293 
00294     curv = 0.0;
00295 
00296     testpt[0] = pt[0];
00297     testpt[1] = pt[1];
00298     testpt[2] = pt[2];
00299 
00300     /* Compute 2nd derivative in the x-direction */
00301     VJMPERR1(Vgrid_value( thee, testpt, &umid));
00302     testpt[0] = pt[0] - hx;
00303     VJMPERR1(Vgrid_value( thee, testpt, &uleft));
00304     testpt[0] = pt[0] + hx;
00305     VJMPERR1(Vgrid_value( thee, testpt, &uright));
00306     testpt[0] = pt[0];
00307 
00308     dxx = (uright - 2*umid + uleft)/(hx*hx);
00309 
00310     /* Compute 2nd derivative in the y-direction */
00311     VJMPERR1(Vgrid_value( thee, testpt, &umid));
00312     testpt[1] = pt[1] - hy;
00313     VJMPERR1(Vgrid_value( thee, testpt, &uleft));
00314     testpt[1] = pt[1] + hy;
00315     VJMPERR1(Vgrid_value( thee, testpt, &uright));
00316     testpt[1] = pt[1];
00317 
00318     dyy = (uright - 2*umid + uleft)/(hy*hy);
00319 
00320     /* Compute 2nd derivative in the z-direction */
00321     VJMPERR1(Vgrid_value( thee, testpt, &umid));
00322     testpt[2] = pt[2] - hzed;
00323     VJMPERR1(Vgrid_value( thee, testpt, &uleft));
00324     testpt[2] = pt[2] + hzed;
00325     VJMPERR1(Vgrid_value( thee, testpt, &uright));
00326 
00327     dzz = (uright - 2*umid + uleft)/(hzed*hzed);
00328 
00329 
00330     if ( cflag == 0 ) {
00331         curv = fabs(dxx);
00332         curv = ( curv > fabs(dyy) ) ? curv : fabs(dyy);
00333         curv = ( curv > fabs(dzz) ) ? curv : fabs(dzz);
00334     } else if ( cflag == 1 ) {
00335         curv = (dxx + dyy + dzz)/3.0;
00336     } else {
00337         Vnm_print(2, "Vgrid_curvature:  support for cflag = %d not available!\n", cflag);
00338         VASSERT( 0 ); /* Feature Not Coded Yet! */
00339     }
00340 
00341     *value = curv;
00342     return 1;
00343 
00344     VERROR1:
00345         return 0; 
00346 
00347 }
00348 
00349 /* ///////////////////////////////////////////////////////////////////////////
00350 // Routine:  Vgrid_gradient
00351 //
00352 // Authors:  Nathan Baker and Stephen Bond
00354 VPUBLIC int Vgrid_gradient(Vgrid *thee, double pt[3], double grad[3]) {
00355 
00356     double hx, hy, hzed;
00357     double uleft, umid, uright, testpt[3];
00358     int haveleft, haveright;
00359 
00360     if (thee == VNULL) {
00361         Vnm_print(2, "Vgrid_gradient:  Error -- got VNULL thee!\n");
00362         VASSERT(0);
00363     }
00364     if (!(thee->ctordata || thee->readdata)) {
00365         Vnm_print(2, "Vgrid_gradient:  Error -- no data available!\n");
00366         VASSERT(0);
00367     }
00368 
00369     hx = thee->hx;
00370     hy = thee->hy;
00371     hzed = thee->hzed;
00372 
00373     /* Compute derivative in the x-direction */
00374     testpt[0] = pt[0];
00375     testpt[1] = pt[1];
00376     testpt[2] = pt[2];
00377     VJMPERR1( Vgrid_value( thee, testpt, &umid));
00378     testpt[0] = pt[0] - hx;
00379     if (Vgrid_value( thee, testpt, &uleft)) haveleft = 1;
00380     else haveleft = 0;
00381     testpt[0] = pt[0] + hx;
00382     if (Vgrid_value( thee, testpt, &uright)) haveright = 1;
00383     else haveright = 0;
00384     if (haveright && haveleft) grad[0] = (uright - uleft)/(2*hx);
00385     else if (haveright) grad[0] = (uright - umid)/hx;
00386     else if (haveleft) grad[0] = (umid - uleft)/hx;
00387     else VJMPERR1(0);
00388 
00389     /* Compute derivative in the y-direction */
00390     testpt[0] = pt[0];
00391     testpt[1] = pt[1];
00392     testpt[2] = pt[2];
00393     VJMPERR1(Vgrid_value(thee, testpt, &umid));
00394     testpt[1] = pt[1] - hy;
00395     if (Vgrid_value( thee, testpt, &uleft)) haveleft = 1;
00396     else haveleft = 0;
00397     testpt[1] = pt[1] + hy;
00398     if (Vgrid_value( thee, testpt, &uright)) haveright = 1;
00399     else haveright = 0;
00400     if (haveright && haveleft) grad[1] = (uright - uleft)/(2*hy);
00401     else if (haveright) grad[1] = (uright - umid)/hy;
00402     else if (haveleft) grad[1] = (umid - uleft)/hy;
00403     else VJMPERR1(0);
00404 
00405     /* Compute derivative in the z-direction */
00406     testpt[0] = pt[0];
00407     testpt[1] = pt[1];
00408     testpt[2] = pt[2];
00409     VJMPERR1(Vgrid_value(thee, testpt, &umid));
00410     testpt[2] = pt[2] - hzed;
00411     if (Vgrid_value( thee, testpt, &uleft)) haveleft = 1;
00412     else haveleft = 0;
00413     testpt[2] = pt[2] + hzed;
00414     if (Vgrid_value( thee, testpt, &uright)) haveright = 1;
00415     else haveright = 0;
00416     if (haveright && haveleft) grad[2] = (uright - uleft)/(2*hzed);
00417     else if (haveright) grad[2] = (uright - umid)/hzed;
00418     else if (haveleft) grad[2] = (umid - uleft)/hzed;
00419     else VJMPERR1(0);
00420 
00421     return 1;
00422 
00423     VERROR1:
00424         return 0; 
00425 
00426 }
00427 
00428 /* ///////////////////////////////////////////////////////////////////////////
00429  // Routine:  Vgrid_readGZ
00430  //
00431  // Author:   David Gohara
00433 #ifdef HAVE_ZLIB
00434 #define off_t long
00435 #include "../../contrib/zlib/zlib.h"
00436 #endif
00437 VPUBLIC int Vgrid_readGZ(Vgrid *thee, const char *fname) {
00438 
00439 #ifdef HAVE_ZLIB
00440  int i, j, k, q, itmp, u, header;
00441  int length, incr;
00442  double * temp;
00443  double dtmp1, dtmp2, dtmp3;
00444  gzFile infile;
00445  char line[VMAX_ARGLEN];
00446 
00447  header = 0;
00448  
00449  /* Check to see if the existing data is null and, if not, clear it out */
00450     if (thee->data != VNULL) {
00451         Vnm_print(1, "Vgrid_readDX:  destroying existing data!\n");
00452   Vmem_free(thee->mem, (thee->nx*thee->ny*thee->nz), sizeof(double),
00453       (void **)&(thee->data)); }
00454     thee->readdata = 1;
00455     thee->ctordata = 0;
00456  
00457  infile = gzopen(fname, "rb");
00458  if (infile == Z_NULL) {
00459         Vnm_print(2, "Vgrid_writeDX:  Problem opening compressed file %s\n",
00460       fname);
00461         return VRC_FAILURE;
00462     }
00463  
00464  thee->hx = 0.0;
00465  thee->hy = 0.0;
00466  thee->hzed = 0.0;
00467  
00468  //read data here
00469  while (header < 7) {
00470   if(gzgets(infile, line, VMAX_ARGLEN) == Z_NULL){
00471    return VRC_FAILURE;
00472   }
00473   if(strncmp(line, "#", 1) == 0) continue;
00474   if(line[0] == '\n') continue; 
00475   
00476   switch (header) {
00477    case 0:
00478     sscanf(line, "object 1 class gridpositions counts %d %d %d",
00479         &(thee->nx),&(thee->ny),&(thee->nz));
00480     break;
00481    case 1:
00482     sscanf(line, "origin %lf %lf %lf",
00483         &(thee->xmin),&(thee->ymin),&(thee->zmin));
00484     break;
00485    case 2:
00486    case 3:
00487    case 4:
00488     sscanf(line, "delta %lf %lf %lf",&dtmp1,&dtmp2,&dtmp3);
00489     thee->hx += dtmp1;
00490     thee->hy += dtmp2;
00491     thee->hzed += dtmp3;
00492     break;
00493    default:
00494     break;
00495   }
00496   
00497   header++;
00498  }
00499  
00500  /* Allocate space for the data */
00501     Vnm_print(0, "Vgrid_readGZ:  allocating %d x %d x %d doubles for storage\n",
00502      thee->nx, thee->ny, thee->nz);
00503     thee->data = VNULL;
00504     thee->data = Vmem_malloc(thee->mem, (thee->nx)*(thee->ny)*(thee->nz), 
00505         sizeof(double));
00506     if (thee->data == VNULL) {
00507         Vnm_print(2, "Vgrid_readGZ:  Unable to allocate space for data!\n");
00508         return 0;
00509     }
00510  
00511  /* Allocate a temporary buffer to store the compressed 
00512   * data into (column major order). Add 2 to ensure the buffer is
00513   * big enough to take extra data on the final read loop.
00514   */
00515  temp = (double *)malloc(thee->nx*thee->ny*thee->nz*sizeof(double) + 2);
00516  for(i=0;i<thee->nx*thee->ny*thee->nz;i+=3){
00517   gzgets(infile, line, length);
00518   sscanf(line, "%lf %lf %lf",&temp[i],&temp[i+1],&temp[i+2]);
00519  }
00520  
00521  /* Now move the data to row major order */
00522  incr = 0;
00523  for (i=0; i<thee->nx; i++) {
00524   for (j=0; j<thee->ny; j++) {
00525    for (k=0; k<thee->nz; k++) {
00526     u = k*(thee->nx)*(thee->ny)+j*(thee->nx)+i;
00527     (thee->data)[u] = temp[incr++];
00528    }
00529   }
00530  }
00531  
00532  /* calculate grid maxima */
00533  thee->xmax = thee->xmin + (thee->nx-1)*thee->hx;
00534  thee->ymax = thee->ymin + (thee->ny-1)*thee->hy;
00535  thee->zmax = thee->zmin + (thee->nz-1)*thee->hzed;
00536  
00537  /* Close off the socket */
00538  gzclose(infile);
00539  free(temp);
00540 #else
00541  
00542  Vnm_print(0, "WARNING\n");
00543  Vnm_print(0, "Vgrid_readGZ:  gzip read/write support is disabled in this build\n");
00544  Vnm_print(0, "Vgrid_readGZ:  configure and compile without the --disable-zlib flag.\n");
00545  Vnm_print(0, "WARNING\n");
00546 #endif 
00547  return VRC_SUCCESS;
00548 } 
00549 
00550 /* ///////////////////////////////////////////////////////////////////////////
00551 // Routine:  Vgrid_readDX
00552 //
00553 // Author:   Nathan Baker
00555 VPUBLIC int Vgrid_readDX(Vgrid *thee, const char *iodev, const char *iofmt,
00556   const char *thost, const char *fname) {
00557 
00558     int i, j, k, itmp, u;
00559     double dtmp;
00560     char tok[VMAX_BUFSIZE];
00561     Vio *sock;
00562 
00563     /* Check to see if the existing data is null and, if not, clear it out */
00564     if (thee->data != VNULL) {
00565         Vnm_print(1, "Vgrid_readDX:  destroying existing data!\n");
00566  Vmem_free(thee->mem, (thee->nx*thee->ny*thee->nz), sizeof(double),
00567           (void **)&(thee->data)); }
00568     thee->readdata = 1;
00569     thee->ctordata = 0;
00570 
00571     /* Set up the virtual socket */
00572     sock = Vio_ctor(iodev,iofmt,thost,fname,"r");
00573     if (sock == VNULL) {
00574         Vnm_print(2, "Vgrid_readDX: Problem opening virtual socket %s\n",
00575           fname);
00576         return 0;
00577     }
00578     if (Vio_accept(sock, 0) < 0) {
00579         Vnm_print(2, "Vgrid_readDX: Problem accepting virtual socket %s\n",
00580           fname);
00581         return 0;
00582     }
00583 
00584     Vio_setWhiteChars(sock, MCwhiteChars);
00585     Vio_setCommChars(sock, MCcommChars);
00586 
00587     /* Read in the DX regular positions */
00588     /* Get "object" */
00589     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00590     VJMPERR1(!strcmp(tok, "object"));
00591     /* Get "1" */
00592     VJMPERR2(1 == Vio_scanf(sock, "%d", &itmp));
00593     /* Get "class" */
00594     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00595     VJMPERR1(!strcmp(tok, "class"));
00596     /* Get "gridpositions" */
00597     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00598     VJMPERR1(!strcmp(tok, "gridpositions"));
00599     /* Get "counts" */
00600     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00601     VJMPERR1(!strcmp(tok, "counts"));
00602     /* Get nx */
00603     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00604     VJMPERR1(1 == sscanf(tok, "%d", &(thee->nx)));
00605     /* Get ny */
00606     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00607     VJMPERR1(1 == sscanf(tok, "%d", &(thee->ny)));
00608     /* Get nz */
00609     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00610     VJMPERR1(1 == sscanf(tok, "%d", &(thee->nz)));
00611     Vnm_print(0, "Vgrid_readDX:  Grid dimensions %d x %d x %d grid\n", 
00612      thee->nx, thee->ny, thee->nz);
00613     /* Get "origin" */
00614     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00615     VJMPERR1(!strcmp(tok, "origin"));
00616     /* Get xmin */
00617     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00618     VJMPERR1(1 == sscanf(tok, "%lf", &(thee->xmin)));
00619     /* Get ymin */
00620     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00621     VJMPERR1(1 == sscanf(tok, "%lf", &(thee->ymin)));
00622     /* Get zmin */
00623     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00624     VJMPERR1(1 == sscanf(tok, "%lf", &(thee->zmin)));
00625     Vnm_print(0, "Vgrid_readDX:  Grid origin = (%g, %g, %g)\n",
00626       thee->xmin, thee->ymin, thee->zmin);
00627     /* Get "delta" */
00628     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00629     VJMPERR1(!strcmp(tok, "delta"));
00630     /* Get hx */
00631     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00632     VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hx)));
00633     /* Get 0.0 */
00634     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00635     VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00636     VJMPERR1(dtmp == 0.0);
00637     /* Get 0.0 */
00638     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00639     VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00640     VJMPERR1(dtmp == 0.0);
00641     /* Get "delta" */
00642     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00643     VJMPERR1(!strcmp(tok, "delta"));
00644     /* Get 0.0 */
00645     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00646     VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00647     VJMPERR1(dtmp == 0.0);
00648     /* Get hy */
00649     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00650     VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hy)));
00651     /* Get 0.0 */
00652     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00653     VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00654     VJMPERR1(dtmp == 0.0);
00655     /* Get "delta" */
00656     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00657     VJMPERR1(!strcmp(tok, "delta"));
00658     /* Get 0.0 */
00659     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00660     VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00661     VJMPERR1(dtmp == 0.0);
00662     /* Get 0.0 */
00663     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00664     VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00665     VJMPERR1(dtmp == 0.0);
00666     /* Get hz */
00667     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00668     VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hzed)));
00669     Vnm_print(0, "Vgrid_readDX:  Grid spacings = (%g, %g, %g)\n",
00670       thee->hx, thee->hy, thee->hzed);
00671     /* Get "object" */
00672     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00673     VJMPERR1(!strcmp(tok, "object"));
00674     /* Get "2" */
00675     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00676     /* Get "class" */
00677     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00678     VJMPERR1(!strcmp(tok, "class"));
00679     /* Get "gridconnections" */
00680     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00681     VJMPERR1(!strcmp(tok, "gridconnections"));
00682     /* Get "counts" */
00683     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00684     VJMPERR1(!strcmp(tok, "counts"));
00685     /* Get the dimensions again */
00686     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00687     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00688     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00689     /* Get "object" */
00690     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00691     VJMPERR1(!strcmp(tok, "object"));
00692     /* Get # */
00693     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00694     /* Get "class" */
00695     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00696     VJMPERR1(!strcmp(tok, "class"));
00697     /* Get "array" */
00698     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00699     VJMPERR1(!strcmp(tok, "array"));
00700     /* Get "type" */
00701     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00702     VJMPERR1(!strcmp(tok, "type"));
00703     /* Get "double" */
00704     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00705     VJMPERR1(!strcmp(tok, "double"));
00706     /* Get "rank" */
00707     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00708     VJMPERR1(!strcmp(tok, "rank"));
00709     /* Get # */
00710     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00711     /* Get "items" */
00712     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00713     VJMPERR1(!strcmp(tok, "items"));
00714     /* Get # */
00715     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00716     VJMPERR1(1 == sscanf(tok, "%d", &itmp));
00717     VJMPERR1(((thee->nx)*(thee->ny)*(thee->nz)) == itmp);
00718     /* Get "data" */
00719     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00720     VJMPERR1(!strcmp(tok, "data"));
00721     /* Get "follows" */
00722     VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00723     VJMPERR1(!strcmp(tok, "follows"));
00724 
00725     /* Allocate space for the data */
00726     Vnm_print(0, "Vgrid_readDX:  allocating %d x %d x %d doubles for storage\n",
00727       thee->nx, thee->ny, thee->nz);
00728     thee->data = VNULL;
00729     thee->data = Vmem_malloc(thee->mem, (thee->nx)*(thee->ny)*(thee->nz), 
00730       sizeof(double));
00731     if (thee->data == VNULL) {
00732         Vnm_print(2, "Vgrid_readDX:  Unable to allocate space for data!\n");
00733         return 0;
00734     }
00735                      
00736     for (i=0; i<thee->nx; i++) {
00737         for (j=0; j<thee->ny; j++) {
00738             for (k=0; k<thee->nz; k++) {
00739                 u = k*(thee->nx)*(thee->ny)+j*(thee->nx)+i;
00740                 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00741                 VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00742                 (thee->data)[u] = dtmp;
00743             }
00744         }
00745     }
00746   
00747     /* calculate grid maxima */
00748     thee->xmax = thee->xmin + (thee->nx-1)*thee->hx;
00749     thee->ymax = thee->ymin + (thee->ny-1)*thee->hy;
00750     thee->zmax = thee->zmin + (thee->nz-1)*thee->hzed;
00751 
00752     /* Close off the socket */
00753     Vio_acceptFree(sock);
00754     Vio_dtor(&sock);
00755 
00756     return 1;
00757 
00758   VERROR1:
00759     Vio_dtor(&sock);
00760     Vnm_print(2, "Vgrid_readDX:  Format problem with input file <%s>\n",
00761       fname);
00762     return 0;
00763 
00764   VERROR2:
00765     Vio_dtor(&sock);
00766     Vnm_print(2, "Vgrid_readDX:  I/O problem with input file <%s>\n",
00767       fname);
00768     return 0;
00769 
00770 
00771 
00772 }
00773 
00774 /* ///////////////////////////////////////////////////////////////////////////
00775  // Routine:  Vgrid_writeGZ
00776  //
00777  // Author:   Nathan Baker
00779 VPUBLIC void Vgrid_writeGZ(Vgrid *thee, const char *iodev, const char *iofmt,
00780        const char *thost, const char *fname, char *title, double *pvec) {
00781  
00782 #ifdef HAVE_ZLIB
00783  double xmin, ymin, zmin, hx, hy, hzed;
00784  
00785  int nx, ny, nz;
00786  int icol, i, j, k, u, usepart, nxPART, nyPART, nzPART, gotit;
00787  double x, y, z, xminPART, yminPART, zminPART;
00788  
00789  int txyz;
00790  double txmin, tymin, tzmin;
00791  
00792  char header[8196];
00793  char footer[8196];
00794  char line[80];
00795  char newline[] = "\n";
00796  gzFile outfile;
00797  char precFormat[VMAX_BUFSIZE];
00798  
00799  if (thee == VNULL) {
00800   Vnm_print(2, "Vgrid_writeGZ:  Error -- got VNULL thee!\n");
00801   VASSERT(0);
00802  }
00803  if (!(thee->ctordata || thee->readdata)) {
00804   Vnm_print(2, "Vgrid_writeGZ:  Error -- no data available!\n");
00805   VASSERT(0);
00806  }
00807  
00808  hx = thee->hx;
00809  hy = thee->hy; 
00810  hzed = thee->hzed; 
00811  nx = thee->nx;
00812  ny = thee->ny;
00813  nz = thee->nz;
00814  xmin = thee->xmin;
00815  ymin = thee->ymin;
00816  zmin = thee->zmin;
00817  
00818  if (pvec == VNULL) usepart = 0;
00819  else usepart = 1;
00820  
00821  /* Set up the virtual socket */
00822  Vnm_print(0, "Vgrid_writeGZ:  Opening file...\n");
00823  outfile = gzopen(fname, "wb");
00824  
00825  if (usepart) {
00826   /* Get the lower corner and number of grid points for the local
00827    * partition */
00828   xminPART = VLARGE;
00829   yminPART = VLARGE;
00830   zminPART = VLARGE;
00831   nxPART = 0;
00832   nyPART = 0;
00833   nzPART = 0;
00834   /* First, search for the lower corner */
00835   for (k=0; k<nz; k++) {
00836    z = k*hzed + zmin;
00837    for (j=0; j<ny; j++) {
00838     y = j*hy + ymin;
00839     for (i=0; i<nx; i++) {
00840      x = i*hx + xmin;
00841      if (pvec[IJK(i,j,k)] > 0.0) {
00842       if (x < xminPART) xminPART = x;
00843       if (y < yminPART) yminPART = y;
00844       if (z < zminPART) zminPART = z;
00845      }
00846     }
00847    }
00848   }
00849   /* Now search for the number of grid points in the z direction */
00850   for (k=0; k<nz; k++) {
00851    gotit = 0;
00852    for (j=0; j<ny; j++) {
00853     for (i=0; i<nx; i++) {
00854      if (pvec[IJK(i,j,k)] > 0.0) {
00855       gotit = 1;
00856       break;
00857      }
00858     }
00859     if (gotit) break;
00860    }
00861    if (gotit) nzPART++;
00862   }
00863   /* Now search for the number of grid points in the y direction */
00864   for (j=0; j<ny; j++) {
00865    gotit = 0;
00866    for (k=0; k<nz; k++) {
00867     for (i=0; i<nx; i++) {
00868      if (pvec[IJK(i,j,k)] > 0.0) {
00869       gotit = 1;
00870       break;
00871      }
00872     }
00873     if (gotit) break;
00874    }
00875    if (gotit) nyPART++;
00876   }
00877   /* Now search for the number of grid points in the x direction */
00878   for (i=0; i<nx; i++) {
00879    gotit = 0;
00880    for (k=0; k<nz; k++) {
00881     for (j=0; j<ny; j++) {
00882      if (pvec[IJK(i,j,k)] > 0.0) {
00883       gotit = 1;
00884       break; 
00885      }
00886     }
00887     if (gotit) break;
00888    }
00889    if (gotit) nxPART++;
00890   }
00891   
00892   if ((nxPART != nx) || (nyPART != ny) || (nzPART != nz)) {
00893    Vnm_print(0, "Vgrid_writeGZ:  printing only subset of domain\n");
00894   }
00895   
00896   txyz = (nxPART*nyPART*nzPART);
00897   txmin = xminPART;
00898   tymin = yminPART;
00899   tzmin = zminPART;
00900   
00901  }else {
00902   
00903   txyz = (nx*ny*nz);
00904   txmin = xmin;
00905   tymin = ymin;
00906   tzmin = zmin;
00907   
00908  }
00909  
00910  /* Write off the title (if we're not XDR) */
00911  sprintf(header,
00912    "# Data from %s\n" \
00913    "# \n"       \
00914    "# %s\n"   \
00915    "# \n"       \
00916    "object 1 class gridpositions counts %i %i %i\n" \
00917    "origin %12.6e %12.6e %12.6e\n" \
00918    "delta %12.6e 0.000000e+00 0.000000e+00\n"  \
00919    "delta 0.000000e+00 %12.6e 0.000000e+00\n"  \
00920    "delta 0.000000e+00 0.000000e+00 %12.6e\n"  \
00921    "object 2 class gridconnections counts %i %i %i\n"\
00922    "object 3 class array type double rank 0 items %i data follows\n",
00923    PACKAGE_STRING,title,nx,ny,nz,txmin,tymin,tzmin,
00924    hx,hy,hzed,nx,ny,nz,txyz);
00925  gzwrite(outfile, header, strlen(header)*sizeof(char));
00926  
00927  /* Now write the data */
00928  icol = 0;
00929  for (i=0; i<nx; i++) {
00930   for (j=0; j<ny; j++) {
00931    for (k=0; k<nz; k++) {
00932     u = k*(nx)*(ny)+j*(nx)+i;
00933     if (pvec[u] > 0.0) {
00934      sprintf(line, "%12.6e ", thee->data[u]);
00935      gzwrite(outfile, line, strlen(line)*sizeof(char));
00936      icol++;
00937      if (icol == 3) {
00938       icol = 0;
00939       gzwrite(outfile, newline, strlen(newline)*sizeof(char));
00940      }
00941     }
00942    }
00943   }
00944  }
00945  if(icol < 3){
00946   char newline[] = "\n";
00947   gzwrite(outfile, newline, strlen(newline)*sizeof(char));
00948  }
00949  
00950  /* Create the field */
00951  sprintf(footer, "attribute \"dep\" string \"positions\"\n" \
00952    "object \"regular positions regular connections\" class field\n" \
00953    "component \"positions\" value 1\n" \
00954    "component \"connections\" value 2\n" \
00955    "component \"data\" value 3\n");
00956  gzwrite(outfile, footer, strlen(footer)*sizeof(char));
00957  
00958  gzclose(outfile);
00959 #else
00960  
00961  Vnm_print(0, "WARNING\n");
00962  Vnm_print(0, "Vgrid_readGZ:  gzip read/write support is disabled in this build\n");
00963  Vnm_print(0, "Vgrid_readGZ:  configure and compile without the --disable-zlib flag.\n");
00964  Vnm_print(0, "WARNING\n");
00965 #endif 
00966 }
00967 
00968 /* ///////////////////////////////////////////////////////////////////////////
00969 // Routine:  Vgrid_writeDX
00970 //
00971 // Author:   Nathan Baker
00973 VPUBLIC void Vgrid_writeDX(Vgrid *thee, const char *iodev, const char *iofmt,
00974   const char *thost, const char *fname, char *title, double *pvec) {
00975 
00976     double xmin, ymin, zmin, hx, hy, hzed;
00977     int nx, ny, nz;
00978     int icol, i, j, k, u, usepart, nxPART, nyPART, nzPART, gotit;
00979     double x, y, z, xminPART, yminPART, zminPART;
00980     Vio *sock;
00981     char precFormat[VMAX_BUFSIZE];
00982 
00983     if (thee == VNULL) {
00984         Vnm_print(2, "Vgrid_writeDX:  Error -- got VNULL thee!\n");
00985         VASSERT(0);
00986     }
00987     if (!(thee->ctordata || thee->readdata)) {
00988         Vnm_print(2, "Vgrid_writeDX:  Error -- no data available!\n");
00989         VASSERT(0);
00990     }
00991 
00992     hx = thee->hx;
00993     hy = thee->hy; 
00994     hzed = thee->hzed; 
00995     nx = thee->nx;
00996     ny = thee->ny;
00997     nz = thee->nz;
00998     xmin = thee->xmin;
00999     ymin = thee->ymin;
01000     zmin = thee->zmin;
01001 
01002     if (pvec == VNULL) usepart = 0;
01003     else usepart = 1;
01004 
01005     /* Set up the virtual socket */
01006     Vnm_print(0, "Vgrid_writeDX:  Opening virtual socket...\n");
01007     sock = Vio_ctor(iodev,iofmt,thost,fname,"w");
01008     if (sock == VNULL) {
01009         Vnm_print(2, "Vgrid_writeDX:  Problem opening virtual socket %s\n",
01010           fname);
01011         return;
01012     }
01013     if (Vio_connect(sock, 0) < 0) {
01014         Vnm_print(2, "Vgrid_writeDX: Problem connecting virtual socket %s\n",
01015           fname);
01016         return; 
01017     }
01018 
01019     Vio_setWhiteChars(sock, MCwhiteChars);
01020     Vio_setCommChars(sock, MCcommChars);
01021 
01022     Vnm_print(0, "Vgrid_writeDX:  Writing to virtual socket...\n");
01023 
01024     if (usepart) {
01025         /* Get the lower corner and number of grid points for the local
01026          * partition */
01027         xminPART = VLARGE;
01028         yminPART = VLARGE;
01029         zminPART = VLARGE;
01030         nxPART = 0;
01031         nyPART = 0;
01032         nzPART = 0;
01033         /* First, search for the lower corner */
01034         for (k=0; k<nz; k++) {
01035             z = k*hzed + zmin;
01036             for (j=0; j<ny; j++) {
01037                 y = j*hy + ymin;
01038                 for (i=0; i<nx; i++) {
01039                     x = i*hx + xmin;
01040                     if (pvec[IJK(i,j,k)] > 0.0) {
01041                         if (x < xminPART) xminPART = x;
01042                         if (y < yminPART) yminPART = y;
01043                         if (z < zminPART) zminPART = z;
01044                     }
01045                 }
01046             }
01047         }
01048         /* Now search for the number of grid points in the z direction */
01049         for (k=0; k<nz; k++) {
01050             gotit = 0;
01051             for (j=0; j<ny; j++) {
01052                 for (i=0; i<nx; i++) {
01053                     if (pvec[IJK(i,j,k)] > 0.0) {
01054                         gotit = 1;
01055                         break;
01056                     }
01057                 }
01058                 if (gotit) break;
01059             }
01060             if (gotit) nzPART++;
01061         }
01062         /* Now search for the number of grid points in the y direction */
01063         for (j=0; j<ny; j++) {
01064             gotit = 0;
01065             for (k=0; k<nz; k++) {
01066                 for (i=0; i<nx; i++) {
01067                     if (pvec[IJK(i,j,k)] > 0.0) {
01068                         gotit = 1;
01069                         break;
01070                     }
01071                 }
01072                 if (gotit) break;
01073             }
01074             if (gotit) nyPART++;
01075         }
01076         /* Now search for the number of grid points in the x direction */
01077         for (i=0; i<nx; i++) {
01078             gotit = 0;
01079             for (k=0; k<nz; k++) {
01080                 for (j=0; j<ny; j++) {
01081                     if (pvec[IJK(i,j,k)] > 0.0) {
01082                         gotit = 1;
01083                         break; 
01084                     }
01085                 }
01086                 if (gotit) break;
01087             }
01088             if (gotit) nxPART++;
01089         }
01090 
01091         if ((nxPART != nx) || (nyPART != ny) || (nzPART != nz)) {
01092             Vnm_print(0, "Vgrid_writeDX:  printing only subset of domain\n");
01093         }
01094 
01095 
01096         /* Write off the title (if we're not XDR) */
01097         if (Vstring_strcasecmp(iofmt, "XDR") == 0) {
01098             Vnm_print(0, "Vgrid_writeDX:  Skipping comments for XDR format.\n");
01099         } else {
01100             Vnm_print(0, "Vgrid_writeDX:  Writing comments for %s format.\n",
01101               iofmt);
01102             Vio_printf(sock, "# Data from %s\n", PACKAGE_STRING);
01103             Vio_printf(sock, "# \n");
01104             Vio_printf(sock, "# %s\n", title);
01105             Vio_printf(sock, "# \n");
01106         }
01107 
01108         /* Write off the DX regular positions */
01109         Vio_printf(sock, "object 1 class gridpositions counts %d %d %d\n",
01110           nxPART, nyPART, nzPART);
01111         
01112         sprintf(precFormat, Vprecision, xminPART, yminPART, zminPART);
01113         Vio_printf(sock, "origin %s\n", precFormat);
01114         sprintf(precFormat, Vprecision, hx, 0.0, 0.0);
01115         Vio_printf(sock, "delta %s\n", precFormat);
01116         sprintf(precFormat, Vprecision, 0.0, hy, 0.0);
01117         Vio_printf(sock, "delta %s\n", precFormat);
01118         sprintf(precFormat, Vprecision, 0.0, 0.0, hzed);
01119         Vio_printf(sock, "delta %s\n", precFormat);
01120         
01121         /* Write off the DX regular connections */
01122         Vio_printf(sock, "object 2 class gridconnections counts %d %d %d\n",
01123           nxPART, nyPART, nzPART);
01124  
01125         /* Write off the DX data */
01126         Vio_printf(sock, "object 3 class array type double rank 0 items %d \
01127 data follows\n", (nxPART*nyPART*nzPART));
01128         icol = 0;
01129         for (i=0; i<nx; i++) {
01130             for (j=0; j<ny; j++) {
01131                 for (k=0; k<nz; k++) {
01132                     u = k*(nx)*(ny)+j*(nx)+i;
01133                     if (pvec[u] > 0.0) {
01134                         Vio_printf(sock, "%12.6e ", thee->data[u]);
01135                         icol++;
01136                         if (icol == 3) {
01137                             icol = 0;
01138                             Vio_printf(sock, "\n");
01139                         }
01140                     }
01141                 }
01142             }
01143         }
01144 
01145         if (icol != 0) Vio_printf(sock, "\n");
01146 
01147         /* Create the field */
01148         Vio_printf(sock, "attribute \"dep\" string \"positions\"\n");
01149         Vio_printf(sock, "object \"regular positions regular connections\" \
01150 class field\n");
01151         Vio_printf(sock, "component \"positions\" value 1\n");
01152         Vio_printf(sock, "component \"connections\" value 2\n");
01153         Vio_printf(sock, "component \"data\" value 3\n");
01154 
01155     } else {
01156         /* Write off the title (if we're not XDR) */
01157         if (Vstring_strcasecmp(iofmt, "XDR") == 0) {
01158             Vnm_print(0, "Vgrid_writeDX:  Skipping comments for XDR format.\n");
01159         } else {
01160             Vnm_print(0, "Vgrid_writeDX:  Writing comments for %s format.\n",
01161               iofmt);
01162             Vio_printf(sock, "# Data from %s\n", PACKAGE_STRING);
01163             Vio_printf(sock, "# \n");
01164             Vio_printf(sock, "# %s\n", title);
01165             Vio_printf(sock, "# \n");
01166         }
01167 
01168 
01169         /* Write off the DX regular positions */
01170         Vio_printf(sock, "object 1 class gridpositions counts %d %d %d\n",
01171           nx, ny, nz);
01172 
01173         sprintf(precFormat, Vprecision, xmin, ymin, zmin);
01174         Vio_printf(sock, "origin %s\n", precFormat);
01175         sprintf(precFormat, Vprecision, hx, 0.0, 0.0);
01176         Vio_printf(sock, "delta %s\n", precFormat);
01177         sprintf(precFormat, Vprecision, 0.0, hy, 0.0);
01178         Vio_printf(sock, "delta %s\n", precFormat);
01179         sprintf(precFormat, Vprecision, 0.0, 0.0, hzed);
01180         Vio_printf(sock, "delta %s\n", precFormat);
01181     
01182         /* Write off the DX regular connections */
01183         Vio_printf(sock, "object 2 class gridconnections counts %d %d %d\n",
01184           nx, ny, nz);
01185 
01186         /* Write off the DX data */
01187         Vio_printf(sock, "object 3 class array type double rank 0 items %d \
01188 data follows\n", (nx*ny*nz));
01189         icol = 0;
01190         for (i=0; i<nx; i++) {
01191             for (j=0; j<ny; j++) { 
01192                 for (k=0; k<nz; k++) {
01193                     u = k*(nx)*(ny)+j*(nx)+i;
01194                     Vio_printf(sock, "%12.6e ", thee->data[u]);
01195                     icol++;
01196                     if (icol == 3) {
01197                         icol = 0;
01198                         Vio_printf(sock, "\n");
01199                     }
01200                 }
01201             }
01202         }
01203         if (icol != 0) Vio_printf(sock, "\n");
01204            
01205         /* Create the field */
01206         Vio_printf(sock, "attribute \"dep\" string \"positions\"\n");
01207         Vio_printf(sock, "object \"regular positions regular connections\" \
01208 class field\n");
01209         Vio_printf(sock, "component \"positions\" value 1\n");
01210         Vio_printf(sock, "component \"connections\" value 2\n");
01211         Vio_printf(sock, "component \"data\" value 3\n");
01212     }
01213 
01214     /* Close off the socket */
01215     Vio_connectFree(sock);
01216     Vio_dtor(&sock);
01217 }
01218 
01219 /* ///////////////////////////////////////////////////////////////////////////
01220 // Routine:  Vgrid_writeUHBD
01221 // Author:   Nathan Baker
01223 VPUBLIC void Vgrid_writeUHBD(Vgrid *thee, const char *iodev, const char *iofmt,
01224   const char *thost, const char *fname, char *title, double *pvec) {
01225 
01226     int icol, i, j, k, u, nx, ny, nz, gotit;
01227     double xmin, ymin, zmin, hzed, hy, hx;
01228     Vio *sock;
01229 
01230     if (thee == VNULL) {
01231         Vnm_print(2, "Vgrid_writeUHBD:  Error -- got VNULL thee!\n");
01232         VASSERT(0);
01233     }
01234     if (!(thee->ctordata || thee->readdata)) {
01235         Vnm_print(2, "Vgrid_writeUHBD:  Error -- no data available!\n");
01236         VASSERT(0);
01237     }
01238 
01239     if ((thee->hx!=thee->hy) || (thee->hy!=thee->hzed)
01240       || (thee->hx!=thee->hzed)) {
01241         Vnm_print(2, "Vgrid_writeUHBD: can't write UHBD mesh with non-uniform \
01242 spacing\n");
01243         return;
01244     }
01245 
01246     /* Set up the virtual socket */
01247     sock = Vio_ctor(iodev,iofmt,thost,fname,"w");
01248     if (sock == VNULL) {
01249         Vnm_print(2, "Vgrid_writeUHBD: Problem opening virtual socket %s\n",
01250           fname);
01251         return;
01252     }
01253     if (Vio_connect(sock, 0) < 0) {
01254         Vnm_print(2, "Vgrid_writeUHBD: Problem connecting virtual socket %s\n",
01255           fname);
01256         return;
01257     }
01258 
01259     /* Get the lower corner and number of grid points for the local
01260      * partition */
01261     hx = thee->hx;
01262     hy = thee->hy;
01263     hzed = thee->hzed;
01264     nx = thee->nx;
01265     ny = thee->ny;
01266     nz = thee->nz;
01267     xmin = thee->xmin;
01268     ymin = thee->ymin;
01269     zmin = thee->zmin;
01270 
01271     /* Let interested folks know that partition information is ignored */
01272     if (pvec != VNULL) {
01273         gotit = 0;
01274         for (i=0; i<(nx*ny*nz); i++) {
01275             if (pvec[i] == 0) {
01276                 gotit = 1;
01277                 break;
01278             }
01279         }
01280         if (gotit) {
01281             Vnm_print(2, "Vgrid_writeUHBD:  IGNORING PARTITION INFORMATION!\n");
01282             Vnm_print(2, "Vgrid_writeUHBD:  This means I/O from parallel runs \
01283 will have significant overlap.\n");
01284         }
01285     }
01286 
01287     /* Write out the header */
01288     Vio_printf(sock, "%72s\n", title);
01289     Vio_printf(sock, "%12.5e%12.5e%7d%7d%7d%7d%7d\n", 1.0, 0.0, -1, 0,
01290       nz, 1, nz);
01291     Vio_printf(sock, "%7d%7d%7d%12.5e%12.5e%12.5e%12.5e\n", nx, ny, nz,
01292       hx, (xmin-hx), (ymin-hx), (zmin-hx));
01293     Vio_printf(sock, "%12.5e%12.5e%12.5e%12.5e\n", 0.0, 0.0, 0.0, 0.0);
01294     Vio_printf(sock, "%12.5e%12.5e%7d%7d", 0.0, 0.0, 0, 0);
01295 
01296     /* Write out the entries */
01297     icol = 0; 
01298     for (k=0; k<nz; k++) {
01299         Vio_printf(sock, "\n%7d%7d%7d\n", k+1, thee->nx, thee->ny);
01300         icol = 0;
01301         for (j=0; j<ny; j++) {
01302             for (i=0; i<nx; i++) {
01303                 u = k*(nx)*(ny)+j*(nx)+i;
01304                 icol++;
01305                 Vio_printf(sock, " %12.5e", thee->data[u]);
01306                 if (icol == 6) {
01307                     icol = 0;
01308                     Vio_printf(sock, "\n");
01309                 }
01310             }
01311         }
01312     }
01313     if (icol != 0) Vio_printf(sock, "\n");
01314 
01315     /* Close off the socket */
01316     Vio_connectFree(sock);
01317     Vio_dtor(&sock);
01318 }
01319 
01320 VPUBLIC double Vgrid_integrate(Vgrid *thee) {
01321 
01322     int i, j, k, nx, ny, nz;
01323     double sum, w;
01324 
01325     if (thee == VNULL) {
01326         Vnm_print(2, "Vgrid_integrate:  Got VNULL thee!\n");
01327         VASSERT(0);
01328     }
01329 
01330     nx = thee->nx;
01331     ny = thee->ny;
01332     nz = thee->nz;
01333 
01334     sum = 0.0;
01335 
01336     for (k=0; k<nz; k++) {
01337   w = 1.0;
01338   if ((k==0) || (k==(nz-1))) w = w * 0.5;
01339         for (j=0; j<ny; j++) {
01340    w = 1.0;
01341    if ((j==0) || (j==(ny-1))) w = w * 0.5;
01342             for (i=0; i<nx; i++) {
01343     w = 1.0;
01344     if ((i==0) || (i==(nx-1))) w = w * 0.5;
01345                 sum = sum + w*(thee->data[IJK(i,j,k)]);
01346             }
01347         }
01348     }
01349 
01350     sum = sum*(thee->hx)*(thee->hy)*(thee->hzed);
01351 
01352     return sum;
01353 
01354 }
01355 
01356 
01357 VPUBLIC double Vgrid_normL1(Vgrid *thee) {
01358 
01359     int i, j, k, nx, ny, nz;
01360     double sum;
01361 
01362     if (thee == VNULL) {
01363         Vnm_print(2, "Vgrid_normL1:  Got VNULL thee!\n");
01364         VASSERT(0);
01365     }
01366 
01367     nx = thee->nx;
01368     ny = thee->ny;
01369     nz = thee->nz;
01370 
01371     sum = 0.0;
01372     for (k=0; k<nz; k++) {
01373         for (j=0; j<ny; j++) {
01374             for (i=0; i<nx; i++) {
01375                 sum = sum + VABS(thee->data[IJK(i,j,k)]);
01376             }
01377         }
01378     }
01379 
01380     sum = sum*(thee->hx)*(thee->hy)*(thee->hzed);
01381 
01382     return sum;
01383 
01384 }
01385 
01386 VPUBLIC double Vgrid_normL2(Vgrid *thee) {
01387 
01388     int i, j, k, nx, ny, nz;
01389     double sum;
01390 
01391     if (thee == VNULL) {
01392         Vnm_print(2, "Vgrid_normL2:  Got VNULL thee!\n");
01393         VASSERT(0);
01394     }
01395 
01396     nx = thee->nx;
01397     ny = thee->ny;
01398     nz = thee->nz;
01399 
01400     sum = 0.0;
01401     for (k=0; k<nz; k++) {
01402         for (j=0; j<ny; j++) {
01403             for (i=0; i<nx; i++) {
01404                 sum = sum + VSQR(thee->data[IJK(i,j,k)]);
01405             }
01406         }
01407     }
01408 
01409     sum = sum*(thee->hx)*(thee->hy)*(thee->hzed);
01410 
01411     return VSQRT(sum);
01412 
01413 }
01414 
01415 VPUBLIC double Vgrid_seminormH1(Vgrid *thee) {
01416 
01417     int i, j, k, d, nx, ny, nz;
01418     double pt[3], grad[3], sum, hx, hy, hzed, xmin, ymin, zmin;
01419 
01420     if (thee == VNULL) {
01421         Vnm_print(2, "Vgrid_seminormH1:  Got VNULL thee!\n");
01422         VASSERT(0);
01423     }
01424 
01425     nx = thee->nx;
01426     ny = thee->ny;
01427     nz = thee->nz;
01428     hx = thee->hx;
01429     hy = thee->hy;
01430     hzed = thee->hzed;
01431     xmin = thee->xmin;
01432     ymin = thee->ymin;
01433     zmin = thee->zmin;
01434 
01435     sum = 0.0;
01436     for (k=0; k<nz; k++) {
01437         pt[2] = k*hzed + zmin;
01438         for (j=0; j<ny; j++) {
01439             pt[1] = j*hy + ymin;
01440             for (i=0; i<nx; i++) {
01441                 pt[0] = i*hx + xmin;
01442                 VASSERT(Vgrid_gradient(thee, pt, grad));
01443                 for (d=0; d<3; d++) sum = sum + VSQR(grad[d]);
01444             }
01445         }
01446     }
01447 
01448     sum = sum*(hx)*(hy)*(hzed);
01449 
01450     if (VABS(sum) < VSMALL) sum = 0.0;
01451     else sum = VSQRT(sum);
01452 
01453     return sum;
01454 
01455 }
01456 
01457 VPUBLIC double Vgrid_normH1(Vgrid *thee) {
01458 
01459     double sum = 0.0;
01460 
01461     if (thee == VNULL) {
01462         Vnm_print(2, "Vgrid_normH1:  Got VNULL thee!\n");
01463         VASSERT(0);
01464     }
01465 
01466     sum = VSQR(Vgrid_seminormH1(thee)) + VSQR(Vgrid_normL2(thee));
01467 
01468     return VSQRT(sum);
01469 
01470 }
01471 
01472 VPUBLIC double Vgrid_normLinf(Vgrid *thee) {
01473 
01474     int i, j, k, nx, ny, nz, gotval;
01475     double sum, val;
01476 
01477     if (thee == VNULL) {
01478         Vnm_print(2, "Vgrid_normLinf:  Got VNULL thee!\n");
01479         VASSERT(0);
01480     }
01481 
01482     nx = thee->nx;
01483     ny = thee->ny;
01484     nz = thee->nz;
01485 
01486     sum = 0.0;
01487     gotval = 0;
01488     for (k=0; k<nz; k++) {
01489         for (j=0; j<ny; j++) {
01490             for (i=0; i<nx; i++) {
01491                 val = VABS(thee->data[IJK(i,j,k)]);
01492                 if (!gotval) {
01493                     gotval = 1;
01494                     sum = val;
01495                 } 
01496                 if (val > sum) sum = val;
01497             }
01498         }
01499     }
01500 
01501     return sum;
01502 
01503 }
01504 

Generated on Wed Oct 20 2010 12:01:34 for APBS by  doxygen 1.7.2