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
00068
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
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
00266
00267
00268
00269
00270
00271
00272
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
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
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
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 );
00339 }
00340
00341 *value = curv;
00342 return 1;
00343
00344 VERROR1:
00345 return 0;
00346
00347 }
00348
00349
00350
00351
00352
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
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
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
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
00430
00431
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
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
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
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
00512
00513
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
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
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
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
00552
00553
00555
00556
00557
00558
00559
00560
00561
00562
00563
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
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
00588
00589 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00590 VJMPERR1(!strcmp(tok, "object"));
00591
00592 VJMPERR2(1 == Vio_scanf(sock, "%d", &itmp));
00593
00594 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00595 VJMPERR1(!strcmp(tok, "class"));
00596
00597 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00598 VJMPERR1(!strcmp(tok, "gridpositions"));
00599
00600 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00601 VJMPERR1(!strcmp(tok, "counts"));
00602
00603 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00604 VJMPERR1(1 == sscanf(tok, "%d", &(thee->nx)));
00605
00606 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00607 VJMPERR1(1 == sscanf(tok, "%d", &(thee->ny)));
00608
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
00614 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00615 VJMPERR1(!strcmp(tok, "origin"));
00616
00617 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00618 VJMPERR1(1 == sscanf(tok, "%lf", &(thee->xmin)));
00619
00620 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00621 VJMPERR1(1 == sscanf(tok, "%lf", &(thee->ymin)));
00622
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
00628 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00629 VJMPERR1(!strcmp(tok, "delta"));
00630
00631 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00632 VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hx)));
00633
00634 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00635 VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00636 VJMPERR1(dtmp == 0.0);
00637
00638 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00639 VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00640 VJMPERR1(dtmp == 0.0);
00641
00642 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00643 VJMPERR1(!strcmp(tok, "delta"));
00644
00645 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00646 VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00647 VJMPERR1(dtmp == 0.0);
00648
00649 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00650 VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hy)));
00651
00652 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00653 VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00654 VJMPERR1(dtmp == 0.0);
00655
00656 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00657 VJMPERR1(!strcmp(tok, "delta"));
00658
00659 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00660 VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00661 VJMPERR1(dtmp == 0.0);
00662
00663 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00664 VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
00665 VJMPERR1(dtmp == 0.0);
00666
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
00672 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00673 VJMPERR1(!strcmp(tok, "object"));
00674
00675 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00676
00677 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00678 VJMPERR1(!strcmp(tok, "class"));
00679
00680 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00681 VJMPERR1(!strcmp(tok, "gridconnections"));
00682
00683 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00684 VJMPERR1(!strcmp(tok, "counts"));
00685
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
00690 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00691 VJMPERR1(!strcmp(tok, "object"));
00692
00693 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00694
00695 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00696 VJMPERR1(!strcmp(tok, "class"));
00697
00698 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00699 VJMPERR1(!strcmp(tok, "array"));
00700
00701 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00702 VJMPERR1(!strcmp(tok, "type"));
00703
00704 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00705 VJMPERR1(!strcmp(tok, "double"));
00706
00707 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00708 VJMPERR1(!strcmp(tok, "rank"));
00709
00710 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00711
00712 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00713 VJMPERR1(!strcmp(tok, "items"));
00714
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
00719 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00720 VJMPERR1(!strcmp(tok, "data"));
00721
00722 VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
00723 VJMPERR1(!strcmp(tok, "follows"));
00724
00725
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
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
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
00776
00777
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822 Vnm_print(0, "Vgrid_writeGZ: Opening file...\n");
00823 outfile = gzopen(fname, "wb");
00824
00825 if (usepart) {
00826
00827
00828 xminPART = VLARGE;
00829 yminPART = VLARGE;
00830 zminPART = VLARGE;
00831 nxPART = 0;
00832 nyPART = 0;
00833 nzPART = 0;
00834
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
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
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
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
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
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
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
00970
00971
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
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
01026
01027 xminPART = VLARGE;
01028 yminPART = VLARGE;
01029 zminPART = VLARGE;
01030 nxPART = 0;
01031 nyPART = 0;
01032 nzPART = 0;
01033
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
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
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
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
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
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
01122 Vio_printf(sock, "object 2 class gridconnections counts %d %d %d\n",
01123 nxPART, nyPART, nzPART);
01124
01125
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
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
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
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
01183 Vio_printf(sock, "object 2 class gridconnections counts %d %d %d\n",
01184 nx, ny, nz);
01185
01186
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
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
01215 Vio_connectFree(sock);
01216 Vio_dtor(&sock);
01217 }
01218
01219
01220
01221
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
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
01260
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
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
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
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
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