This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
vgrid.c
1978 lines (1721 loc) · 59 KB
/
vgrid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file vgrid.c
* @author Nathan Baker
* @brief Class Vgrid methods
* @ingroup Vgrid
* @version $Id$
* @attention
* @verbatim
*
* APBS -- Adaptive Poisson-Boltzmann Solver
*
* Nathan A. Baker ([email protected])
* Pacific Northwest National Laboratory
*
* Additional contributing authors listed in the code documentation.
*
* Copyright (c) 2010-2020 Battelle Memorial Institute. Developed at the
* Pacific Northwest National Laboratory, operated by Battelle Memorial
* Institute, Pacific Northwest Division for the U.S. Department of Energy.
*
* Portions Copyright (c) 2002-2010, Washington University in St. Louis.
* Portions Copyright (c) 2002-2020, Nathan A. Baker.
* Portions Copyright (c) 1999-2002, The Regents of the University of
* California.
* Portions Copyright (c) 1995, Michael Holst.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the developer nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* @endverbatim
*/
#include "vgrid.h"
#include <stdio.h>
VEMBED(rcsid="$Id$")
#if !defined(VINLINE_VGRID)
VPUBLIC unsigned long int Vgrid_memChk(Vgrid *thee) {
return Vmem_bytes(thee->mem);
}
#endif
#define IJK(i,j,k) (((k)*(nx)*(ny))+((j)*(nx))+(i))
#if defined(_WIN32) && (_MSC_VER < 1800)
#include <float.h>
int isnan(double d)
{
return _isnan(d);
}
#endif
VPRIVATE char *MCwhiteChars = " =,;\t\n";
VPRIVATE char *MCcommChars = "#%";
VPRIVATE double Vcompare;
VPRIVATE char Vprecision[26];
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_ctor
// Author: Nathan Baker
/////////////////////////////////////////////////////////////////////////// */
VPUBLIC Vgrid* Vgrid_ctor(int nx,
int ny,
int nz,
double hx,
double hy,
double hzed,
double xmin,
double ymin,
double zmin,
double *data
) {
Vgrid *thee = VNULL;
thee = (Vgrid*)Vmem_malloc(VNULL, 1, sizeof(Vgrid));
VASSERT(thee != VNULL);
VASSERT(Vgrid_ctor2(thee, nx, ny, nz, hx, hy, hzed,
xmin, ymin, zmin, data));
return thee;
}
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_ctor2
// Author: Nathan Baker
/////////////////////////////////////////////////////////////////////////// */
VPUBLIC int Vgrid_ctor2(Vgrid *thee, int nx, int ny, int nz,
double hx, double hy, double hzed,
double xmin, double ymin, double zmin,
double *data) {
if (thee == VNULL) return 0;
thee->nx = nx;
thee->ny = ny;
thee->nz = nz;
thee->hx = hx;
thee->hy = hy;
thee->hzed = hzed;
thee->xmin = xmin;
thee->xmax = xmin + (nx-1)*hx;
thee->ymin = ymin;
thee->ymax = ymin + (ny-1)*hy;
thee->zmin = zmin;
thee->zmax = zmin + (nz-1)*hzed;
if (data == VNULL) {
thee->ctordata = 0;
thee->readdata = 0;
} else {
thee->ctordata = 1;
thee->readdata = 0;
thee->data = data;
}
thee->mem = Vmem_ctor("APBS:VGRID");
Vcompare = pow(10,-1*(VGRID_DIGITS - 2));
sprintf(Vprecision,"%%12.%de %%12.%de %%12.%de", VGRID_DIGITS,
VGRID_DIGITS, VGRID_DIGITS);
return 1;
}
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_dtor
// Author: Nathan Baker
/////////////////////////////////////////////////////////////////////////// */
VPUBLIC void Vgrid_dtor(Vgrid **thee) {
if ((*thee) != VNULL) {
Vgrid_dtor2(*thee);
Vmem_free(VNULL, 1, sizeof(Vgrid), (void **)thee);
(*thee) = VNULL;
}
}
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_dtor2
// Author: Nathan Baker
/////////////////////////////////////////////////////////////////////////// */
VPUBLIC void Vgrid_dtor2(Vgrid *thee) {
if (thee->readdata) {
Vmem_free(thee->mem, (thee->nx*thee->ny*thee->nz), sizeof(double),
(void **)&(thee->data));
}
Vmem_dtor(&(thee->mem));
}
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_value
// Author: Nathan Baker
/////////////////////////////////////////////////////////////////////////// */
VPUBLIC int Vgrid_value(Vgrid *thee, double pt[3], double *value) {
int nx, ny, nz;
size_t ihi, jhi, khi, ilo, jlo, klo;
double hx, hy, hzed, xmin, ymin, zmin, ifloat, jfloat, kfloat;
double xmax, ymax, zmax;
double u, dx, dy, dz;
if (thee == VNULL) {
Vnm_print(2, "Vgrid_value: Error -- got VNULL thee!\n");
VASSERT(0);
}
if (!(thee->ctordata || thee->readdata)) {
Vnm_print(2, "Vgrid_value: Error -- no data available!\n");
VASSERT(0);
}
nx = thee->nx;
ny = thee->ny;
nz = thee->nz;
hx = thee->hx;
hy = thee->hy;
hzed = thee->hzed;
xmin = thee->xmin;
ymin = thee->ymin;
zmin = thee->zmin;
xmax = thee->xmax;
ymax = thee->ymax;
zmax = thee->zmax;
u = 0;
ifloat = (pt[0] - xmin)/hx;
jfloat = (pt[1] - ymin)/hy;
kfloat = (pt[2] - zmin)/hzed;
ihi = (int)ceil(ifloat);
jhi = (int)ceil(jfloat);
khi = (int)ceil(kfloat);
ilo = (int)floor(ifloat);
jlo = (int)floor(jfloat);
klo = (int)floor(kfloat);
if (VABS(pt[0] - xmin) < Vcompare) ilo = 0;
if (VABS(pt[1] - ymin) < Vcompare) jlo = 0;
if (VABS(pt[2] - zmin) < Vcompare) klo = 0;
if (VABS(pt[0] - xmax) < Vcompare) ihi = nx-1;
if (VABS(pt[1] - ymax) < Vcompare) jhi = ny-1;
if (VABS(pt[2] - zmax) < Vcompare) khi = nz-1;
/* See if we're on the mesh */
/*the condions starting with ilo>=0 seem unnecessary since they are of type size_t*/
if ((ihi<nx) && (jhi<ny) && (khi<nz) /*&&
(ilo>=0) && (jlo>=0) && (klo>=0)*/) {
dx = ifloat - (double)(ilo);
dy = jfloat - (double)(jlo);
dz = kfloat - (double)(klo);
u = dx *dy *dz *(thee->data[IJK(ihi,jhi,khi)])
+ dx *(1.0-dy)*dz *(thee->data[IJK(ihi,jlo,khi)])
+ dx *dy *(1.0-dz)*(thee->data[IJK(ihi,jhi,klo)])
+ dx *(1.0-dy)*(1.0-dz)*(thee->data[IJK(ihi,jlo,klo)])
+ (1.0-dx)*dy *dz *(thee->data[IJK(ilo,jhi,khi)])
+ (1.0-dx)*(1.0-dy)*dz *(thee->data[IJK(ilo,jlo,khi)])
+ (1.0-dx)*dy *(1.0-dz)*(thee->data[IJK(ilo,jhi,klo)])
+ (1.0-dx)*(1.0-dy)*(1.0-dz)*(thee->data[IJK(ilo,jlo,klo)]);
*value = u;
if (isnan(u)) {
Vnm_print(2, "Vgrid_value: Got NaN!\n");
Vnm_print(2, "Vgrid_value: (x, y, z) = (%4.3f, %4.3f, %4.3f)\n",
pt[0], pt[1], pt[2]);
Vnm_print(2, "Vgrid_value: (ihi, jhi, khi) = (%d, %d, %d)\n",
ihi, jhi, khi);
Vnm_print(2, "Vgrid_value: (ilo, jlo, klo) = (%d, %d, %d)\n",
ilo, jlo, klo);
Vnm_print(2, "Vgrid_value: (nx, ny, nz) = (%d, %d, %d)\n",
nx, ny, nz);
Vnm_print(2, "Vgrid_value: (dx, dy, dz) = (%4.3f, %4.3f, %4.3f)\n",
dx, dy, dz);
Vnm_print(2, "Vgrid_value: data[IJK(ihi,jhi,khi)] = %g\n",
thee->data[IJK(ihi,jhi,khi)]);
Vnm_print(2, "Vgrid_value: data[IJK(ihi,jlo,khi)] = %g\n",
thee->data[IJK(ihi,jlo,khi)]);
Vnm_print(2, "Vgrid_value: data[IJK(ihi,jhi,klo)] = %g\n",
thee->data[IJK(ihi,jhi,klo)]);
Vnm_print(2, "Vgrid_value: data[IJK(ihi,jlo,klo)] = %g\n",
thee->data[IJK(ihi,jlo,klo)]);
Vnm_print(2, "Vgrid_value: data[IJK(ilo,jhi,khi)] = %g\n",
thee->data[IJK(ilo,jhi,khi)]);
Vnm_print(2, "Vgrid_value: data[IJK(ilo,jlo,khi)] = %g\n",
thee->data[IJK(ilo,jlo,khi)]);
Vnm_print(2, "Vgrid_value: data[IJK(ilo,jhi,klo)] = %g\n",
thee->data[IJK(ilo,jhi,klo)]);
Vnm_print(2, "Vgrid_value: data[IJK(ilo,jlo,klo)] = %g\n",
thee->data[IJK(ilo,jlo,klo)]);
}
return 1;
} else {
*value = 0;
return 0;
}
return 0;
}
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_curvature
//
// Notes: cflag=0 ==> Reduced Maximal Curvature
// cflag=1 ==> Mean Curvature (Laplace)
// cflag=2 ==> Gauss Curvature
// cflag=3 ==> True Maximal Curvature
//
// Authors: Stephen Bond and Nathan Baker
/////////////////////////////////////////////////////////////////////////// */
VPUBLIC int Vgrid_curvature(Vgrid *thee, double pt[3], int cflag,
double *value) {
double hx, hy, hzed, curv;
double dxx, dyy, dzz;
double uleft, umid, uright, testpt[3];
if (thee == VNULL) {
Vnm_print(2, "Vgrid_curvature: Error -- got VNULL thee!\n");
VASSERT(0);
}
if (!(thee->ctordata || thee->readdata)) {
Vnm_print(2, "Vgrid_curvature: Error -- no data available!\n");
VASSERT(0);
}
hx = thee->hx;
hy = thee->hy;
hzed = thee->hzed;
curv = 0.0;
testpt[0] = pt[0];
testpt[1] = pt[1];
testpt[2] = pt[2];
/* Compute 2nd derivative in the x-direction */
VJMPERR1(Vgrid_value( thee, testpt, &umid));
testpt[0] = pt[0] - hx;
VJMPERR1(Vgrid_value( thee, testpt, &uleft));
testpt[0] = pt[0] + hx;
VJMPERR1(Vgrid_value( thee, testpt, &uright));
testpt[0] = pt[0];
dxx = (uright - 2*umid + uleft)/(hx*hx);
/* Compute 2nd derivative in the y-direction */
VJMPERR1(Vgrid_value( thee, testpt, &umid));
testpt[1] = pt[1] - hy;
VJMPERR1(Vgrid_value( thee, testpt, &uleft));
testpt[1] = pt[1] + hy;
VJMPERR1(Vgrid_value( thee, testpt, &uright));
testpt[1] = pt[1];
dyy = (uright - 2*umid + uleft)/(hy*hy);
/* Compute 2nd derivative in the z-direction */
VJMPERR1(Vgrid_value( thee, testpt, &umid));
testpt[2] = pt[2] - hzed;
VJMPERR1(Vgrid_value( thee, testpt, &uleft));
testpt[2] = pt[2] + hzed;
VJMPERR1(Vgrid_value( thee, testpt, &uright));
dzz = (uright - 2*umid + uleft)/(hzed*hzed);
if ( cflag == 0 ) {
curv = fabs(dxx);
curv = ( curv > fabs(dyy) ) ? curv : fabs(dyy);
curv = ( curv > fabs(dzz) ) ? curv : fabs(dzz);
} else if ( cflag == 1 ) {
curv = (dxx + dyy + dzz)/3.0;
} else {
Vnm_print(2, "Vgrid_curvature: support for cflag = %d not available!\n", cflag);
VASSERT( 0 ); /* Feature Not Coded Yet! */
}
*value = curv;
return 1;
VERROR1:
return 0;
}
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_gradient
//
// Authors: Nathan Baker and Stephen Bond
/////////////////////////////////////////////////////////////////////////// */
VPUBLIC int Vgrid_gradient(Vgrid *thee, double pt[3], double grad[3]) {
double hx, hy, hzed;
double uleft, umid, uright, testpt[3];
int haveleft, haveright;
if (thee == VNULL) {
Vnm_print(2, "Vgrid_gradient: Error -- got VNULL thee!\n");
VASSERT(0);
}
if (!(thee->ctordata || thee->readdata)) {
Vnm_print(2, "Vgrid_gradient: Error -- no data available!\n");
VASSERT(0);
}
hx = thee->hx;
hy = thee->hy;
hzed = thee->hzed;
/* Compute derivative in the x-direction */
testpt[0] = pt[0];
testpt[1] = pt[1];
testpt[2] = pt[2];
VJMPERR1( Vgrid_value( thee, testpt, &umid));
testpt[0] = pt[0] - hx;
if (Vgrid_value( thee, testpt, &uleft)) haveleft = 1;
else haveleft = 0;
testpt[0] = pt[0] + hx;
if (Vgrid_value( thee, testpt, &uright)) haveright = 1;
else haveright = 0;
if (haveright && haveleft) grad[0] = (uright - uleft)/(2*hx);
else if (haveright) grad[0] = (uright - umid)/hx;
else if (haveleft) grad[0] = (umid - uleft)/hx;
else VJMPERR1(0);
/* Compute derivative in the y-direction */
testpt[0] = pt[0];
testpt[1] = pt[1];
testpt[2] = pt[2];
VJMPERR1(Vgrid_value(thee, testpt, &umid));
testpt[1] = pt[1] - hy;
if (Vgrid_value( thee, testpt, &uleft)) haveleft = 1;
else haveleft = 0;
testpt[1] = pt[1] + hy;
if (Vgrid_value( thee, testpt, &uright)) haveright = 1;
else haveright = 0;
if (haveright && haveleft) grad[1] = (uright - uleft)/(2*hy);
else if (haveright) grad[1] = (uright - umid)/hy;
else if (haveleft) grad[1] = (umid - uleft)/hy;
else VJMPERR1(0);
/* Compute derivative in the z-direction */
testpt[0] = pt[0];
testpt[1] = pt[1];
testpt[2] = pt[2];
VJMPERR1(Vgrid_value(thee, testpt, &umid));
testpt[2] = pt[2] - hzed;
if (Vgrid_value( thee, testpt, &uleft)) haveleft = 1;
else haveleft = 0;
testpt[2] = pt[2] + hzed;
if (Vgrid_value( thee, testpt, &uright)) haveright = 1;
else haveright = 0;
if (haveright && haveleft) grad[2] = (uright - uleft)/(2*hzed);
else if (haveright) grad[2] = (uright - umid)/hzed;
else if (haveleft) grad[2] = (umid - uleft)/hzed;
else VJMPERR1(0);
return 1;
VERROR1:
return 0;
}
/* ///////////////////////////////////////////////////////////////////////////
// Routine: Vgrid_readGZ
//
// Author: David Gohara
/////////////////////////////////////////////////////////////////////////// */
#ifdef HAVE_ZLIB
#define off_t long
#include "zlib.h"
#endif
VPUBLIC int Vgrid_readGZ(Vgrid *thee, const char *fname) {
#ifdef HAVE_ZLIB
size_t i, j, k, u;
size_t len; // Temporary counter variable for loop conditionals
size_t header, incr;
double *temp;
double dtmp1, dtmp2, dtmp3;
gzFile infile;
char line[VMAX_ARGLEN];
header = 0;
/* Check to see if the existing data is null and, if not, clear it out */
if (thee->data != VNULL) {
Vnm_print(1, "%s: destroying existing data!\n", __func__);
Vmem_free(thee->mem, thee->nx * thee->ny * thee->nz, sizeof(double),
(void **)&(thee->data));
}
thee->readdata = 1;
thee->ctordata = 0;
infile = gzopen(fname, "rb");
if (infile == Z_NULL) {
Vnm_print(2, "%s: Problem opening compressed file %s\n", __func__, fname);
return VRC_FAILURE;
}
thee->hx = 0.0;
thee->hy = 0.0;
thee->hzed = 0.0;
//read data here
while (header < 7) {
if(gzgets(infile, line, VMAX_ARGLEN) == Z_NULL){
return VRC_FAILURE;
}
// Skip comments and newlines
if(strncmp(line, "#", 1) == 0) continue;
if(line[0] == '\n') continue;
switch (header) {
case 0:
sscanf(line, "object 1 class gridpositions counts %d %d %d",
&(thee->nx),&(thee->ny),&(thee->nz));
break;
case 1:
sscanf(line, "origin %lf %lf %lf",
&(thee->xmin),&(thee->ymin),&(thee->zmin));
break;
case 2:
case 3:
case 4:
sscanf(line, "delta %lf %lf %lf",&dtmp1,&dtmp2,&dtmp3);
thee->hx += dtmp1;
thee->hy += dtmp2;
thee->hzed += dtmp3;
break;
default:
break;
}
header++;
}
/* Allocate space for the data */
Vnm_print(0, "%s: allocating %d x %d x %d doubles for storage\n",
__func__, thee->nx, thee->ny, thee->nz);
len = thee->nx * thee->ny * thee->nz;
thee->data = VNULL;
thee->data = Vmem_malloc(thee->mem, len, sizeof(double));
if (thee->data == VNULL) {
Vnm_print(2, "%s: Unable to allocate space for data!\n", __func__);
return 0;
}
/* Allocate a temporary buffer to store the compressed
* data into (column major order). Add 2 to ensure the buffer is
* big enough to take extra data on the final read loop.
*/
temp = (double *)malloc(len * (2 * sizeof(double)));
for (i = 0; i < len; i += 3){
memset(&line, 0, sizeof(line));
gzgets(infile, line, VMAX_ARGLEN);
sscanf(line, "%lf %lf %lf", &temp[i], &temp[i+1], &temp[i+2]);
}
/* Now move the data to row major order */
incr = 0;
for (i=0; i<thee->nx; i++) {
for (j=0; j<thee->ny; j++) {
for (k=0; k<thee->nz; k++) {
u = k*(thee->nx)*(thee->ny)+j*(thee->nx)+i;
(thee->data)[u] = temp[incr++];
}
}
}
/* calculate grid maxima */
thee->xmax = thee->xmin + (thee->nx-1)*thee->hx;
thee->ymax = thee->ymin + (thee->ny-1)*thee->hy;
thee->zmax = thee->zmin + (thee->nz-1)*thee->hzed;
/* Close off the socket */
gzclose(infile);
free(temp);
#else
Vnm_print(0, "WARNING\n");
Vnm_print(0, "Vgrid_readGZ: gzip read/write support is disabled in this build\n");
Vnm_print(0, "Vgrid_readGZ: configure and compile without the --disable-zlib flag.\n");
Vnm_print(0, "WARNING\n");
#endif
return VRC_SUCCESS;
}
/**
* Load grid from an input file using sockets.
* @author Nathan Baker
*/
VPUBLIC int Vgrid_readDX(Vgrid *thee,
const char *iodev,
const char *iofmt,
const char *thost,
const char *fname
) {
size_t i, j, k, itmp, u;
double dtmp;
char tok[VMAX_BUFSIZE];
Vio *sock;
/* Check to see if the existing data is null and, if not, clear it out */
if (thee->data != VNULL) {
Vnm_print(1, "Vgrid_readDX: destroying existing data!\n");
Vmem_free(thee->mem, (thee->nx*thee->ny*thee->nz), sizeof(double),
(void **)&(thee->data)); }
thee->readdata = 1;
thee->ctordata = 0;
/* Set up the virtual socket */
sock = Vio_ctor(iodev,iofmt,thost,fname,"r");
if (sock == VNULL) {
Vnm_print(2, "Vgrid_readDX: Problem opening virtual socket %s\n",
fname);
return 0;
}
if (Vio_accept(sock, 0) < 0) {
Vnm_print(2, "Vgrid_readDX: Problem accepting virtual socket %s\n",
fname);
return 0;
}
Vio_setWhiteChars(sock, MCwhiteChars);
Vio_setCommChars(sock, MCcommChars);
/* Read in the DX regular positions */
/* Get "object" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "object"));
/* Get "1" */
VJMPERR2(1 == Vio_scanf(sock, "%d", &itmp));
/* Get "class" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "class"));
/* Get "gridpositions" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "gridpositions"));
/* Get "counts" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "counts"));
/* Get nx */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%d", &(thee->nx)));
/* Get ny */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%d", &(thee->ny)));
/* Get nz */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%d", &(thee->nz)));
Vnm_print(0, "Vgrid_readDX: Grid dimensions %d x %d x %d grid\n",
thee->nx, thee->ny, thee->nz);
/* Get "origin" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "origin"));
/* Get xmin */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &(thee->xmin)));
/* Get ymin */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &(thee->ymin)));
/* Get zmin */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &(thee->zmin)));
Vnm_print(0, "Vgrid_readDX: Grid origin = (%g, %g, %g)\n",
thee->xmin, thee->ymin, thee->zmin);
/* Get "delta" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "delta"));
/* Get hx */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hx)));
/* Get 0.0 */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
VJMPERR1(dtmp == 0.0);
/* Get 0.0 */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
VJMPERR1(dtmp == 0.0);
/* Get "delta" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "delta"));
/* Get 0.0 */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
VJMPERR1(dtmp == 0.0);
/* Get hy */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hy)));
/* Get 0.0 */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
VJMPERR1(dtmp == 0.0);
/* Get "delta" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "delta"));
/* Get 0.0 */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
VJMPERR1(dtmp == 0.0);
/* Get 0.0 */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
VJMPERR1(dtmp == 0.0);
/* Get hz */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &(thee->hzed)));
Vnm_print(0, "Vgrid_readDX: Grid spacings = (%g, %g, %g)\n",
thee->hx, thee->hy, thee->hzed);
/* Get "object" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "object"));
/* Get "2" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
/* Get "class" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "class"));
/* Get "gridconnections" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "gridconnections"));
/* Get "counts" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "counts"));
/* Get the dimensions again */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
/* Get "object" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "object"));
/* Get # */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
/* Get "class" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "class"));
/* Get "array" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "array"));
/* Get "type" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "type"));
/* Get "double" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "double"));
/* Get "rank" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "rank"));
/* Get # */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
/* Get "items" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "items"));
/* Get # */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lu", &itmp));
u = (size_t)thee->nx * thee->ny * thee->nz;
VJMPERR1(u == itmp);
/* Get "data" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "data"));
/* Get "follows" */
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(!strcmp(tok, "follows"));
/* Allocate space for the data */
Vnm_print(0, "Vgrid_readDX: allocating %d x %d x %d doubles for storage\n",
thee->nx, thee->ny, thee->nz);
thee->data = VNULL;
thee->data = (double*)Vmem_malloc(thee->mem, u, sizeof(double));
if (thee->data == VNULL) {
Vnm_print(2, "Vgrid_readDX: Unable to allocate space for data!\n");
return 0;
}
for (i=0; i<thee->nx; i++) {
for (j=0; j<thee->ny; j++) {
for (k=0; k<thee->nz; k++) {
u = k*(thee->nx)*(thee->ny)+j*(thee->nx)+i;
VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
VJMPERR1(1 == sscanf(tok, "%lf", &dtmp));
(thee->data)[u] = dtmp;
}
}
}
/* calculate grid maxima */
thee->xmax = thee->xmin + (thee->nx-1)*thee->hx;
thee->ymax = thee->ymin + (thee->ny-1)*thee->hy;
thee->zmax = thee->zmin + (thee->nz-1)*thee->hzed;
/* Close off the socket */
Vio_acceptFree(sock);
Vio_dtor(&sock);
return 1;
VERROR1:
Vio_dtor(&sock);
Vnm_print(2, "Vgrid_readDX: Format problem with input file <%s>\n",
fname);
return 0;
VERROR2:
Vio_dtor(&sock);
Vnm_print(2, "Vgrid_readDX: I/O problem with input file <%s>\n",
fname);
return 0;
}
/**
* Load grid from an input dx binary file.
* @author Juan Brandi
*/
VPUBLIC int Vgrid_readDXBIN(Vgrid *thee, const char *iodev, const char *iofmt,
const char *thost, const char *fname) {
size_t i, j, k, itmp, u;
double dtmp, dtmp2;
char tok[VMAX_BUFSIZE];
int isBinary = 0;
//Vio *sock;
/* Check to see if the existing data is null and, if not, clear it out */
if (thee->data != VNULL) {
Vnm_print(1, "Vgrid_readDXBIN: destroying existing data!\n");
Vmem_free(thee->mem, (thee->nx*thee->ny*thee->nz), sizeof(double),
(void **)&(thee->data)); }
thee->readdata = 1;
thee->ctordata = 0;
/*Opend file fd for binary reading*/
FILE *fd = fopen(fname,"rb");
if(fd == NULL){
printf("Vgrid_readDXBIN: Problem opening file %s\n",fname);
fclose(fd);
return 0;
}
/* Set up the virtual socket */
// sock = Vio_ctor(iodev,iofmt,thost,fname,"r");
// if (sock == VNULL) {
// Vnm_print(2, "Vgrid_readDX: Problem opening virtual socket %s\n",
// fname);
// return 0;
// }
// if (Vio_accept(sock, 0) < 0) {
// Vnm_print(2, "Vgrid_readDX: Problem accepting virtual socket %s\n",
// fname);
// return 0;
// }
//
// Vio_setWhiteChars(sock, MCwhiteChars);
// Vio_setCommChars(sock, MCcommChars);
//skip comments
do{
fgets(tok, VMAX_BUFSIZE, fd);
}
while(tok[0]=='#');
//get counts
if(sscanf(tok,"object 1 class gridpositions counts %i %i %i\n",&(thee->nx),&(thee->ny),&(thee->nz))!= 3){
printf("Vgrid_readDXBIN: Failed to read dimensions.\n");
fclose(fd);
return 0;
}
printf("Vgrid_readDXBIN: Grid dimensions %d x %d x %d grid\n",thee->nx, thee->ny, thee->nz);
/* Read in the DX regular positions */
/* Get "object" */
// VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
// VJMPERR1(!strcmp(tok, "object"));
// /* Get "1" */
// VJMPERR2(1 == Vio_scanf(sock, "%d", &itmp));
// /* Get "class" */
// VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
// VJMPERR1(!strcmp(tok, "class"));
// /* Get "gridpositions" */
// VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
// VJMPERR1(!strcmp(tok, "gridpositions"));
// /* Get "counts" */
// VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
// VJMPERR1(!strcmp(tok, "counts"));
// /* Get nx */
// VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
// VJMPERR1(1 == sscanf(tok, "%d", &(thee->nx)));
// /* Get ny */
// VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
// VJMPERR1(1 == sscanf(tok, "%d", &(thee->ny)));
// /* Get nz */
// VJMPERR2(1 == Vio_scanf(sock, "%s", tok));
// VJMPERR1(1 == sscanf(tok, "%d", &(thee->nz)));
// Vnm_print(0, "Vgrid_readDX: Grid dimensions %d x %d x %d grid\n",
// thee->nx, thee->ny, thee->nz);
if(fgets(tok,VMAX_BUFSIZE, fd) == NULL){
printf("Vgrid_readDXBIN: unexpected end of file.\n");
fclose(fd);
return 0;
}
if(sscanf(tok,"origin %lf %lf %lf",&(thee->xmin),&(thee->ymin),&(thee->zmin))!=3){
printf("Vgrid_readDXBIN: Failed to read origin cell data.\n");
fclose(fd);
return 0;
}
printf("Vgrid_readDXBIN: Grid origin = (%g %g %g)\n",thee->xmin, thee->ymin, thee->zmin);
//get Delta x
if(fgets(tok,VMAX_BUFSIZE, fd) == NULL){
printf("Vgrid_readDXBIN: unexpected end of file.\n");
fclose(fd);
return 0;
}
if(sscanf(tok,"delta %lf %lf %lf",&(thee->hx),&dtmp,&dtmp2)!=3){
printf("Vgrid_readDXBIN: Failed to read delta x data.\n");
fclose(fd);
return 0;
}
//get Delta y
if(fgets(tok,VMAX_BUFSIZE, fd) == NULL){
printf("Vgrid_readDXBIN: Unexpected end of file\n");
fclose(fd);
return 0;
}
if(sscanf(tok,"delta %lf %lf %lf",&dtmp,&(thee->hy),&dtmp2)!=3){
printf("Vgrid_readDXBIN: Failed to read delta y data.\n");
fclose(fd);
return 0;
}
//get Delta z
if(fgets(tok,VMAX_BUFSIZE, fd) == NULL){
printf("Vgrid_readDXBIN: Unexpected end of file.\n");
fclose(fd);
return 0;
}
if(sscanf(tok,"delta %lf %lf %lf",&dtmp,&dtmp2,&(thee->hzed))!=3){
printf("Vgrid_readDXBIN: Failed to read delta z data.\n");
fclose(fd);
return 0;
}
printf("Vgrid_readDXBIN: Grid spacings = (%g, %g, %g)\n",thee->hx, thee->hy, thee->hzed);
//skip a line
if(fgets(tok,VMAX_BUFSIZE, fd) == NULL){
printf("Vgrid_readDXBIN: Unexpected end of file.\n");
fclose(fd);
return 0;
}
//scan the buffer for the word binary
if(fgets(tok,VMAX_BUFSIZE, fd) == NULL){
printf("Vgrid_readDXBIN: Unexpected end of file.\n");
fclose(fd);
return 0;
}
if(strstr(tok,"binary")){
isBinary = 1;
}
else{
printf("Vgrid_readDXBIN: Binary tag not found. Will continue to try to read binary data.");
}
u = (size_t)thee->nx * thee->ny * thee->nz;
int tot = thee->nx * thee->ny * thee->nz;
/*Allocate space for the data*/
printf("Vgrid_readDXBIN: allocating %d x %d x %d doubled for storage\n", thee->nx, thee->ny, thee->nz);
thee->data = NULL;
thee->data = (double *)malloc(tot*sizeof(double));
if(thee->data == NULL){
printf("Vgrid_readDXBIN: Unable to allocate space for data!\n");
fclose(fd);
return 0;
}
int counter = 0, r;
for (i=0; i<thee->nx; i++) {
for (j=0; j<thee->ny; j++) {
for (k=0; k<thee->nz; k++) {
u = k*(thee->nx)*(thee->ny)+j*(thee->nx)+i;
r = fread(&dtmp,sizeof(double),1,fd);
(thee->data)[u] = dtmp;
if(r!= 1){
printf("Vgrid_readDXBIN: Failed to read doubles.\n");
return 0;
}
counter++;
}
}
}
if(counter!=tot){
printf("Vgrid_readDXBIN: Read double = %d not equal to items = %d\n",counter, tot);
}
/* calculate grid maxima */
thee->xmax = thee->xmin + (thee->nx-1)*thee->hx;
thee->ymax = thee->ymin + (thee->ny-1)*thee->hy;
thee->zmax = thee->zmin + (thee->nz-1)*thee->hzed;
fclose(fd);