1 /**************************************************************************/
2 /* Copyright Notice                                                       */
3 /*                                                                        */
4 /* For Scientific and Technical Information Only                          */
5 /* Copyright 2006 Los Alamos National Security, LLC All rights reserved   */
6 /*                                                                        */
7 /* For All Information Unless otherwise indicated, this information has   */
8 /* been authored by an employee or employees of the Los Alamos National   */
9 /* Security, LLC (LANS), operator of the Los Alamos National Laboratory   */
10 /* under Contract No. DE-AC52-06NA25396 with the U.S. Department of       */
11 /* Energy. The U.S. Government has rights to use, reproduce, and          */
12 /* distribute this information. The public may copy and use this          */
13 /* information without charge, provided that this Notice and any          */
14 /* statement of authorship are reproduced on all copies. Neither the      */
15 /* Government nor LANS makes any warranty, express or implied, or assumes */
16 /* any liability or responsibility for the use of this information.       */
17 /*                                                                        */
18 /* Author: Frank A. Ortega fao[at]lanl.gov                                */
19 /**************************************************************************/
20 
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #define RDATA_INIT
27 #include "gmvread.h"
28 /* #include "gmvrayread.h" */
29 
30 #include <sys/types.h>
31 /* #include <malloc.h> */
32 #include <math.h>
33 
34 #define FREE(a) { if (a) free(a); a = NULL; }
35 
36 #define CHAR 0
37 #define SHORT 1
38 #define INT 2
39 #define FLOAT 3
40 #define WORD 4
41 #define DOUBLE 5
42 #define LONGLONG 6
43 
44 #define CHARSIZE 1
45 #define SHORTSIZE 2
46 #define INTSIZE 4
47 #define WORDSIZE 4
48 #define FLOATSIZE 4
49 #define LONGSIZE 4
50 #define DOUBLESIZE 8
51 #define LONGLONGSIZE 8
52 
53 #define IEEE 0
54 #define ASCII 1
55 #define IEEEI4R4 0
56 #define IEEEI4R8 2
57 #define IEEEI8R4 3
58 #define IEEEI8R8 4
59 #define IECXI4R4 5
60 #define IECXI4R8 6
61 #define IECXI8R4 7
62 #define IECXI8R8 8
63 
64 static int charsize = CHARSIZE,
65            intsize = INTSIZE,
66            floatsize = FLOATSIZE,
67            doublesize = DOUBLESIZE,
68            longlongsize = LONGLONGSIZE, charsize_in;
69 
70 static long numnodes, numcells, lncells, numcellsin, numfaces, lnfaces,
71             numfacesin, ncells_struct;
72 
73 static int numsurf, lnsurf, numsurfin, numtracers, numunits;
74 
75 static short amrflag_in, structflag_in, fromfileflag, fromfileskip = 0;
76 static short nodes_read = 0, cells_read = 0, faces_read = 0,
77              surface_read = 0, iend = 0, swapbytes_on = 0, skipflag = 0,
78              reading_fromfile = 0, vfaceflag = 0, node_inp_type,
79              printon = 0;
80 
81 static int curr_keyword, ftype, ftype_sav, readkeyword, ff_keyword = -1;
82 
83 static unsigned wordbuf;
84 static char sav_keyword[9], input_dir[300];
85 
86 void swapbytes(void *from, int size, int nitems),
87      readnodes(FILE *gmvin, int ftype),
88      readcells(FILE *gmvin, int ftype),
89      readfaces(FILE *gmvin, int ftype),
90      readvfaces(FILE *gmvin, int ftype),
91      readxfaces(FILE *gmvin, int ftype),
92      readmats(FILE *gmvin, int ftype),
93      readvels(FILE *gmvin, int ftype),
94      readvars(FILE *gmvin, int ftype),
95      readflags(FILE *gmvin, int ftype),
96      readpolygons(FILE *gmvin, int ftype),
97      readtracers(FILE *gmvin, int ftype),
98      readnodeids(FILE *gmvin, int ftype),
99      readcellids(FILE *gmvin, int ftype),
100      readfaceids(FILE *gmvin, int ftype),
101      readtracerids(FILE *gmvin, int ftype),
102      readunits(FILE *gmvin, int ftype),
103      readsurface(FILE *gmvin, int ftype),
104      readsurfvel(FILE *gmvin, int ftype),
105      readsurfmats(FILE *gmvin, int ftype),
106      readsurfvars(FILE *gmvin, int ftype),
107      readsurfflag(FILE *gmvin, int ftype),
108      readsurfids(FILE *gmvin, int ftype),
109      readvinfo(FILE *gmvin, int ftype),
110      readcomments(FILE *gmvin, int ftype),
111      readgroups(FILE *gmvin, int ftype),
112      readcellpes(FILE *gmvin, int ftype),
113      readsubvars(FILE *gmvin, int ftype),
114      readghosts(FILE *gmvin, int ftype),
115      readvects(FILE *gmvin, int ftype),
116      gmvrdmemerr(), ioerrtst(FILE *gmvin), endfromfile();
117 static FILE *gmvin, *gmvin_sav;
118 
119 
120 
121 int binread(void* ptr, int size, int type, long nitems, FILE* stream);
122 int word2int(unsigned wordin);
123 
124 
125 
gmvread_checkfile(char * filnam)126 int gmvread_checkfile(char *filnam)
127 {
128   /*                           */
129   /*  Check a GMV input file.  */
130   /*                           */
131   int chkend;
132   char magic[9], filetype[9];
133   FILE *gmvchk;
134 
135   int chk_gmvend(FILE *gmvin);
136 
137    gmvchk = fopen(filnam,"r");
138 
139    if (gmvchk == NULL)
140      {
141       fprintf(stderr,"GMV cannot open file %s\n",filnam);
142       return 1;
143      }
144 
145    /*  Read header. */
146    binread(magic,charsize, CHAR, (long)8, gmvchk);
147    if (strncmp(magic,"gmvinput",8) != 0)
148      {
149       fprintf(stderr,"This is not a GMV input file.\n");
150       fclose(gmvchk);
151       return 2;
152      }
153 
154    /*  Check that gmv input file has "endgmv".  */
155    if (strncmp(magic,"gmvinput",8) == 0)
156      {
157       chkend = chk_gmvend(gmvchk);
158       if (!chkend)
159         {
160          fprintf(stderr,"Error - endgmv not found.\n");
161          fclose(gmvchk);
162          return 3;
163         }
164      }
165 
166    /*  Read file type and set ftype: 0-ieee binary, 1-ascii text,  */
167    /*  0-ieeei4r4 binary, 2-ieeei4r8 binary, 3-ieeei8r4 binary,    */
168    /*  4-ieeei8r8 binary, 5-iecxi4r4 binary, 6-iecxi4r8 binary,    */
169    /*  7-iecxi8r4 binary or 8-iecxi8r8 binary.                     */
170 
171    binread(filetype,charsize, CHAR, (long)8, gmvchk);
172 
173    /*  Rewind the file and re-read header for file type.  */
174    ftype = -1;
175    if (strncmp(filetype,"ascii",5) == 0) ftype = ASCII;
176    if (strncmp(filetype," ascii",6) == 0) ftype = ASCII;
177    if (strncmp(filetype,"  ascii",7) == 0) ftype = ASCII;
178    if (strncmp(filetype,"   ascii",8) == 0) ftype = ASCII;
179    if (strncmp(filetype,"ieee",4) == 0) ftype = IEEEI4R4;
180    if (strncmp(filetype," ieee",5) == 0) ftype = IEEEI4R4;
181    if (strncmp(filetype,"ieeei4r4",8) == 0) ftype = IEEEI4R4;
182    if (strncmp(filetype," ieeei4r4",9) == 0) ftype = IEEEI4R4;
183    if (strncmp(filetype,"ieeei4r8",8) == 0) ftype = IEEEI4R8;
184    if (strncmp(filetype," ieeei4r8",9) == 0) ftype = IEEEI4R8;
185    if (strncmp(filetype,"ieeei8r4",8) == 0) ftype = IEEEI8R4;
186    if (strncmp(filetype," ieeei8r4",9) == 0) ftype = IEEEI8R4;
187    if (strncmp(filetype,"ieeei8r8",8) == 0) ftype = IEEEI8R8;
188    if (strncmp(filetype," ieeei8r8",9) == 0) ftype = IEEEI8R8;
189    if (strncmp(filetype,"iecxi4r4",8) == 0) ftype = IECXI4R4;
190    if (strncmp(filetype," iecxi4r4",9) == 0) ftype = IECXI4R4;
191    if (strncmp(filetype,"iecxi4r8",8) == 0) ftype = IECXI4R8;
192    if (strncmp(filetype," iecxi4r8",9) == 0) ftype = IECXI4R8;
193    if (strncmp(filetype,"iecxi8r4",8) == 0) ftype = IECXI8R4;
194    if (strncmp(filetype," iecxi8r4",9) == 0) ftype = IECXI8R4;
195    if (strncmp(filetype,"iecxi8r8",8) == 0) ftype = IECXI8R8;
196    if (strncmp(filetype," iecxi8r8",9) == 0) ftype = IECXI8R8;
197 
198    /*  Check for valid file type.  */
199    if (ftype == -1)
200      {
201       fprintf(stderr,"Invalid GMV input file type.  Type must be:\n");
202       fprintf(stderr,
203        "  ascii, ieee, ieeei4r4, ieeei4r8, ieeei8r4, ieeei8r8,\n");
204       fprintf(stderr,
205        "  iecxi4r4, iecxi4r8, iecxi8r4, iecxi8r8,\n");
206       fclose(gmvchk);
207       return 4;
208      }
209 
210    /*  Check that machine can read I8 types.  */
211    if (ftype == IEEEI8R4 || ftype == IEEEI8R8 || ftype == IECXI8R4 ||
212        ftype == IECXI8R8)
213      {
214       if (sizeof(long) < 8)
215         {
216          fprintf(stderr,"Cannot read 64bit I* types on this machine.\n");
217          fclose(gmvchk);
218          return 5;
219         }
220      }
221 
222    fclose(gmvchk);
223    return 0;
224 }
225 
226 
gmvread_open(char * filnam)227 int gmvread_open(char *filnam)
228 {
229   /*                                    */
230   /*  Open and check a GMV input file.  */
231   /*                                    */
232   int chkend, ilast, i, isize;
233   char magic[9], filetype[9];
234 
235   int chk_gmvend(FILE *gmvin);
236 
237    gmvin = fopen(filnam,"r");
238 
239    if (gmvin == NULL)
240       {
241        fprintf(stderr,"GMV cannot open file %s\n",filnam);
242        return 1;
243       }
244 
245    /*  Read header. */
246 
247    binread(magic,charsize, CHAR, (long)8, gmvin);
248    if (strncmp(magic,"gmvinput",8) != 0)
249       {
250        fprintf(stderr,"This is not a GMV input file.\n");
251        return 2;
252       }
253 
254    /*  Check that gmv input file has "endgmv".  */
255    if (strncmp(magic,"gmvinput",8) == 0)
256       {
257        chkend = chk_gmvend(gmvin);
258        if (!chkend)
259          {
260           fprintf(stderr,"Error - endgmv not found.\n");
261           return 3;
262          }
263       }
264 
265    /*  Read file type and set ftype: 0-ieee binary, 1-ascii text,  */
266    /*  0-ieeei4r4 binary, 2-ieeei4r8 binary, 3-ieeei8r4 binary,    */
267    /*  or 4-ieeei8r8 binary.                                       */
268 
269    binread(filetype,charsize, CHAR, (long)8, gmvin);
270 
271    /*  Rewind the file and re-read header for file type.  */
272    ftype = -1;
273    if (strncmp(filetype,"ascii",5) == 0) ftype = ASCII;
274    if (strncmp(filetype," ascii",6) == 0) ftype = ASCII;
275    if (strncmp(filetype,"  ascii",7) == 0) ftype = ASCII;
276    if (strncmp(filetype,"   ascii",8) == 0) ftype = ASCII;
277    if (strncmp(filetype,"ieee",4) == 0) ftype = IEEEI4R4;
278    if (strncmp(filetype," ieee",5) == 0) ftype = IEEEI4R4;
279    if (strncmp(filetype,"ieeei4r4",8) == 0) ftype = IEEEI4R4;
280    if (strncmp(filetype," ieeei4r4",9) == 0) ftype = IEEEI4R4;
281    if (strncmp(filetype,"ieeei4r8",8) == 0) ftype = IEEEI4R8;
282    if (strncmp(filetype," ieeei4r8",9) == 0) ftype = IEEEI4R8;
283    if (strncmp(filetype,"ieeei8r4",8) == 0) ftype = IEEEI8R4;
284    if (strncmp(filetype," ieeei8r4",9) == 0) ftype = IEEEI8R4;
285    if (strncmp(filetype,"ieeei8r8",8) == 0) ftype = IEEEI8R8;
286    if (strncmp(filetype," ieeei8r8",9) == 0) ftype = IEEEI8R8;
287    if (strncmp(filetype,"iecxi4r4",8) == 0) ftype = IECXI4R4;
288    if (strncmp(filetype," iecxi4r4",9) == 0) ftype = IECXI4R4;
289    if (strncmp(filetype,"iecxi4r8",8) == 0) ftype = IECXI4R8;
290    if (strncmp(filetype," iecxi4r8",9) == 0) ftype = IECXI4R8;
291    if (strncmp(filetype,"iecxi8r4",8) == 0) ftype = IECXI8R4;
292    if (strncmp(filetype," iecxi8r4",9) == 0) ftype = IECXI8R4;
293    if (strncmp(filetype,"iecxi8r8",8) == 0) ftype = IECXI8R8;
294    if (strncmp(filetype," iecxi8r8",9) == 0) ftype = IECXI8R8;
295 
296    /*  Determine character input size.  */
297    charsize_in = 8;
298    if (ftype == ASCII || ftype > IEEEI8R8) charsize_in = 32;
299 
300    /*  Reset IECX types back to regular types.  */
301    if (ftype == IECXI4R4) ftype = IEEEI4R4;
302    if (ftype == IECXI4R8) ftype = IEEEI4R8;
303    if (ftype == IECXI8R4) ftype = IEEEI8R4;
304    if (ftype == IECXI8R8) ftype = IEEEI8R8;
305 
306    /*  Check for valid file type.  */
307    if (ftype == -1)
308      {
309       fprintf(stderr,"Invalid GMV input file type.  Type must be:\n");
310       fprintf(stderr,
311        "  ascii, ieee, ieeei4r4, ieeei4r8, ieeei8r4, ieeei8r8.\n");
312       fprintf(stderr,
313        "  iecxi4r4, iecxi4r8, iecxi8r4, iecxi8r8.\n");
314       return 4;
315      }
316 
317    /*  Check that machine can read I8 types.  */
318    if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
319      {
320       if (sizeof(long) < 8)
321         {
322          fprintf(stderr,"Cannot read 64bit I* types on this machine.\n");
323          return 4;
324         }
325      }
326 
327    rewind(gmvin);
328    if (ftype != ASCII)
329      {
330       binread(magic,charsize,CHAR,(long)8,gmvin);
331       binread(filetype,charsize,CHAR,(long)8,gmvin);
332      }
333    if (ftype == ASCII) fscanf(gmvin,"%s%s",magic,filetype);
334 
335    readkeyword = 1;
336 
337    /*  Get directory of GMV input file from the file name  */
338    /*  for use in case any fromfiles found in the file.    */
339    /*  First find the last /.                              */
340    if (fromfileskip == 0)
341      {
342       isize = strlen(filnam);
343       ilast = -1;
344       for (i = 0; i < isize - 1; i++)
345         {
346          if (strncmp((filnam+i),"/",1) == 0) ilast = i;
347         }
348       if (ilast > -1)
349         {
350          strncpy(input_dir,filnam,ilast+1);
351         }
352      }
353 
354    return 0;
355 }
356 
357 
gmvread_open_fromfileskip(char * filnam)358 int gmvread_open_fromfileskip(char *filnam)
359 {
360   /*                                    */
361   /*  Open and check a GMV input file.  */
362   /*  Skip read of fromfiles.           */
363   /*                                    */
364   int ierr;
365 
366    fromfileskip = 1;
367 
368    ierr = gmvread_open(filnam);
369    return ierr;
370 }
371 
372 
gmvread_close()373 void gmvread_close()
374 {
375    fclose(gmvin);
376    fromfileskip = 0;
377    nodes_read = 0;  cells_read = 0;  faces_read = 0;
378    surface_read = 0;  iend = 0;  swapbytes_on = 0;  skipflag = 0;
379    reading_fromfile = 0;  vfaceflag = 0;
380 }
381 
382 
gmvread_printon()383 void gmvread_printon()
384 {
385    printon = 1;
386 }
387 
388 
gmvread_printoff()389 void gmvread_printoff()
390 {
391    printon = 0;
392 }
393 
394 
gmvread_data()395 void gmvread_data()
396 {
397   char keyword[9], tmpchar[20];
398   double ptime;
399   float tmptime;
400   int cycleno, before_nodes_ok;
401   void fromfilecheck(int keyword);
402 
403    /*  Zero gmv_data and free structure arrays.  */
404    gmv_data.keyword = 0;
405    gmv_data.datatype = 0;
406    strcpy(gmv_data.name1,"                   ");
407    gmv_data.num = 0;
408    gmv_data.num2 = 0;
409    gmv_data.ndoubledata1 = 0;
410    gmv_data.ndoubledata2 = 0;
411    gmv_data.ndoubledata3 = 0;
412    FREE(gmv_data.doubledata1);
413    FREE(gmv_data.doubledata2);
414    FREE(gmv_data.doubledata3);
415    gmv_data.nlongdata1 = 0;
416    gmv_data.nlongdata2 = 0;
417    FREE(gmv_data.longdata1);
418    FREE(gmv_data.longdata2);
419    gmv_data.nchardata1 = 0;
420    gmv_data.nchardata2 = 0;
421    FREE(gmv_data.chardata1);
422    FREE(gmv_data.chardata2);
423 
424    /*  Check current keyword type for continued reading.  */
425    if (readkeyword == 0)
426      {
427       switch (curr_keyword)
428         {
429          case(CELLS):
430             readcells(gmvin,ftype);
431             break;
432          case(FACES):
433             readfaces(gmvin,ftype);
434             break;
435          case(VFACES):
436             readvfaces(gmvin,ftype);
437             break;
438          case(XFACES):
439             readxfaces(gmvin,ftype);
440             break;
441          case(VARIABLE):
442             readvars(gmvin,ftype);
443             break;
444          case(FLAGS):
445             readflags(gmvin,ftype);
446             break;
447          case(POLYGONS):
448             readpolygons(gmvin,ftype);
449             break;
450          case(TRACERS):
451             readtracers(gmvin,ftype);
452             break;
453          case(SURFACE):
454             readsurface(gmvin,ftype);
455             break;
456          case(SURFVARS):
457             readsurfvars(gmvin,ftype);
458             break;
459          case(SURFFLAG):
460             readsurfflag(gmvin,ftype);
461             break;
462          case(UNITS):
463             readunits(gmvin,ftype);
464             break;
465          case(VINFO):
466             readvinfo(gmvin,ftype);
467             break;
468          case(GROUPS):
469             readgroups(gmvin,ftype);
470             break;
471          case(SUBVARS):
472             readsubvars(gmvin,ftype);
473             break;
474          case(VECTORS):
475             readvects(gmvin,ftype);
476             break;
477         }
478      }
479 
480    /*  Read and process keyword based data until endgmv found.  */
481    if (readkeyword == 1)
482      {
483       if (ftype != ASCII)
484         {
485          binread(keyword,charsize,CHAR,(long)8,gmvin);
486          *(keyword+8)=(char)0;
487         }
488       if (ftype == ASCII) fscanf(gmvin,"%s",keyword);
489 
490       if ((feof(gmvin) != 0) | (ferror(gmvin) != 0)) iend = 1;
491 
492       /*  If comments keyword, read through comments,  */
493       /*  then read and proces next keyword.           */
494       if (strncmp(keyword,"comments",8) == 0)
495         {
496          readcomments(gmvin,ftype);
497          if (ftype != ASCII)
498            {
499             binread(keyword,charsize,CHAR,(long)8,gmvin);
500             *(keyword+8)=(char)0;
501            }
502          if (ftype == ASCII) fscanf(gmvin,"%s",keyword);
503          if ((feof(gmvin) != 0) | (ferror(gmvin) != 0)) iend = 1;
504         }
505 
506       if (strncmp(keyword,"endgmv",6) == 0)
507         {
508          curr_keyword = GMVEND;
509          iend = 1;
510         }
511       else if (strncmp(keyword,"nodes",5) == 0)
512         {
513          curr_keyword = NODES;
514          node_inp_type = 0;
515         }
516       else if (strncmp(keyword,"nodev",5) == 0)
517         {
518          curr_keyword = NODES;
519          node_inp_type = 1;
520         }
521       else if (strncmp(keyword,"cells",5) == 0) curr_keyword = CELLS;
522       else if (strncmp(keyword,"faces",5) == 0) curr_keyword = FACES;
523       else if (strncmp(keyword,"vfaces",6) == 0) curr_keyword = VFACES;
524       else if (strncmp(keyword,"xfaces",6) == 0) curr_keyword = XFACES;
525       else if (strncmp(keyword,"material",8) == 0) curr_keyword = MATERIAL;
526       else if (strncmp(keyword,"velocity",8) == 0) curr_keyword = VELOCITY;
527       else if (strncmp(keyword,"variable",8) == 0) curr_keyword = VARIABLE;
528       else if (strncmp(keyword,"flags",5) == 0) curr_keyword = FLAGS;
529       else if (strncmp(keyword,"polygons",8) == 0) curr_keyword = POLYGONS;
530       else if (strncmp(keyword,"tracers",7) == 0) curr_keyword = TRACERS;
531       else if (strncmp(keyword,"probtime",8) == 0) curr_keyword = PROBTIME;
532       else if (strncmp(keyword,"cycleno",7) == 0) curr_keyword = CYCLENO;
533       else if (strncmp(keyword,"nodeids",7) == 0) curr_keyword = NODEIDS;
534       else if (strncmp(keyword,"cellids",7) == 0) curr_keyword = CELLIDS;
535       else if (strncmp(keyword,"surface",7) == 0) curr_keyword = SURFACE;
536       else if (strncmp(keyword,"surfmats",8) == 0) curr_keyword = SURFMATS;
537       else if (strncmp(keyword,"surfvel",7) == 0) curr_keyword = SURFVEL;
538       else if (strncmp(keyword,"surfvars",8) == 0) curr_keyword = SURFVARS;
539       else if (strncmp(keyword,"surfflag",8) == 0) curr_keyword = SURFFLAG;
540       else if (strncmp(keyword,"surfids",7) == 0) curr_keyword = SURFIDS;
541       else if (strncmp(keyword,"units",5) == 0) curr_keyword = UNITS;
542       else if (strncmp(keyword,"vinfo",5) == 0) curr_keyword = VINFO;
543       else if (strncmp(keyword,"traceids",7) == 0) curr_keyword = TRACEIDS;
544       else if (strncmp(keyword,"groups",6) == 0) curr_keyword = GROUPS;
545       else if (strncmp(keyword,"codename",8) == 0) curr_keyword = CODENAME;
546       else if (strncmp(keyword,"codever",7) == 0) curr_keyword = CODEVER;
547       else if (strncmp(keyword,"simdate",7) == 0) curr_keyword = SIMDATE;
548       else if (strncmp(keyword,"cellpes",7) == 0) curr_keyword = CELLPES;
549       else if (strncmp(keyword,"subvars",7) == 0) curr_keyword = SUBVARS;
550       else if (strncmp(keyword,"ghosts",6) == 0) curr_keyword = GHOSTS;
551       else if (strncmp(keyword,"vectors",7) == 0) curr_keyword = VECTORS;
552       else curr_keyword = INVALIDKEYWORD;
553 
554       /*  If invalid keyword, send error.  */
555       if (curr_keyword == INVALIDKEYWORD)
556         {
557          gmv_data.keyword = GMVERROR;
558          fprintf(stderr,"Error, %s is an invalid keyword.\n",keyword);
559         }
560 
561       strcpy(sav_keyword,keyword);
562 
563       /*  Set skipflag if reading from a fromfile.  */
564       if (reading_fromfile)
565         {
566          if (curr_keyword == ff_keyword) skipflag = 0;
567          else skipflag = 1;
568         }
569 
570       /*  Check for keywords that are allowed before nodes, cells, etc.  */
571       before_nodes_ok = 0;
572       if (curr_keyword == CODENAME || curr_keyword == CODEVER ||
573           curr_keyword == SIMDATE) before_nodes_ok = 1;
574 
575       /*  Check that nodes have been input.  */
576       if (curr_keyword > NODES && nodes_read == 0 && before_nodes_ok == 0)
577         {
578          fprintf(stderr,"Error, 'nodes' keyword missing.\n");
579          gmv_data.keyword = GMVERROR;
580         }
581 
582       /*  Check that cells, faces, vfaces or xfaces have been input.  */
583       if (curr_keyword > XFACES && cells_read == 0 && faces_read == 0 &&
584           before_nodes_ok == 0)
585         {
586          fprintf(stderr,"Error, 'cells, faces or xfaces' keyword missing.\n");
587          gmv_data.keyword = GMVERROR;
588         }
589 
590       /*  Read current keyword data.  */
591       switch (curr_keyword)
592         {
593          case(NODES):
594             fromfilecheck(NODES);
595             if (fromfileflag == 1) break;
596             readnodes(gmvin,ftype);
597             readkeyword = 1;
598             break;
599          case(CELLS):
600             fromfilecheck(CELLS);
601             if (fromfileflag == 1) break;
602             readcells(gmvin,ftype);
603             if (amrflag_in == 0 && structflag_in == 0)
604                readkeyword = 0;
605             break;
606          case(FACES):
607             fromfilecheck(FACES);
608             if (fromfileflag == 1) break;
609             readfaces(gmvin,ftype);
610             readkeyword = 0;
611             break;
612          case(VFACES):
613             fromfilecheck(VFACES);
614             if (fromfileflag == 1) break;
615             readvfaces(gmvin,ftype);
616             readkeyword = 0;
617             break;
618          case(XFACES):
619             fromfilecheck(XFACES);
620             if (fromfileflag == 1) break;
621             readxfaces(gmvin,ftype);
622             readkeyword = 0;
623             break;
624          case(MATERIAL):
625             fromfilecheck(MATERIAL);
626             if (fromfileflag == 1) break;
627             readmats(gmvin,ftype);
628             readkeyword = 1;
629             break;
630          case(VELOCITY):
631             readvels(gmvin,ftype);
632             readkeyword = 1;
633             break;
634          case(VARIABLE):
635             readvars(gmvin,ftype);
636             if (readkeyword == 1) readkeyword = 0;
637             break;
638          case(FLAGS):
639             fromfilecheck(FLAGS);
640             if (fromfileflag == 1) break;
641             readflags(gmvin,ftype);
642             if (readkeyword == 1) readkeyword = 0;
643             break;
644          case(POLYGONS):
645             fromfilecheck(POLYGONS);
646             if (fromfileflag == 1) break;
647             readpolygons(gmvin,ftype);
648             if (readkeyword == 1) readkeyword = 0;
649             break;
650          case(TRACERS):
651             readtracers(gmvin,ftype);
652             if (readkeyword == 1) readkeyword = 0;
653             break;
654          case(PROBTIME):
655             if (ftype != ASCII)
656               {
657                if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
658                   binread(&ptime,doublesize,DOUBLE,(long)1,gmvin);
659                else
660                  {
661                   binread(&tmptime,floatsize,FLOAT,(long)1,gmvin);
662                   ptime = tmptime;
663                  }
664               }
665             if (ftype == ASCII) fscanf(gmvin,"%lf",&ptime);
666             gmv_data.keyword = PROBTIME;
667             gmv_data.datatype = 0;
668             gmv_data.ndoubledata1 = 1;
669             gmv_data.doubledata1 = (double *)malloc(1*sizeof(double));
670             gmv_data.doubledata1[0] = ptime;
671             readkeyword = 1;
672             break;
673          case(CYCLENO):
674             if (ftype != ASCII) binread(&cycleno,intsize,INT,
675                                         (long)1,gmvin);
676             if (ftype == ASCII) fscanf(gmvin,"%d",&cycleno);
677             gmv_data.keyword = CYCLENO;
678             gmv_data.num = cycleno;
679             readkeyword = 1;
680             break;
681          case(NODEIDS):
682             fromfilecheck(NODEIDS);
683             if (fromfileflag == 1) break;
684             readnodeids(gmvin,ftype);
685             readkeyword = 1;
686             break;
687          case(CELLIDS):
688             fromfilecheck(CELLIDS);
689             readcellids(gmvin,ftype);
690             readkeyword = 1;
691             break;
692          case(FACEIDS):
693             fromfilecheck(FACEIDS);
694             readfaceids(gmvin,ftype);
695             readkeyword = 1;
696             break;
697          case(TRACEIDS):
698             fromfilecheck(TRACEIDS);
699             readtracerids(gmvin,ftype);
700             readkeyword = 1;
701             break;
702          case(SURFACE):
703             fromfilecheck(SURFACE);
704             if (fromfileflag == 1) break;
705             readsurface(gmvin,ftype);
706             readkeyword = 0;
707             break;
708          case(SURFMATS):
709             fromfilecheck(SURFMATS);
710             if (fromfileflag == 1) break;
711             readsurfmats(gmvin,ftype);
712             readkeyword = 1;
713             break;
714          case(SURFVEL):
715             readsurfvel(gmvin,ftype);
716             readkeyword = 1;
717             break;
718          case(SURFVARS):
719             readsurfvars(gmvin,ftype);
720             if (readkeyword == 1) readkeyword = 0;
721             break;
722          case(SURFFLAG):
723             readsurfflag(gmvin,ftype);
724             if (readkeyword == 1) readkeyword = 0;
725             break;
726          case(SURFIDS):
727             fromfilecheck(SURFIDS);
728             readsurfids(gmvin,ftype);
729             readkeyword = 1;
730             break;
731          case(UNITS):
732             fromfilecheck(UNITS);
733             if (fromfileflag == 1) break;
734             readunits(gmvin,ftype);
735             if (readkeyword == 1) readkeyword = 0;
736             break;
737          case(VINFO):
738             readvinfo(gmvin,ftype);
739             if (readkeyword == 1) readkeyword = 0;
740             break;
741          case(GROUPS):
742             fromfilecheck(GROUPS);
743             if (fromfileflag == 1) break;
744             readgroups(gmvin,ftype);
745             if (readkeyword == 1) readkeyword = 0;
746             break;
747          case(CODENAME):  case(CODEVER):  case(SIMDATE):
748             if (ftype != ASCII)
749               {
750                binread(tmpchar,charsize,CHAR,(long)8,gmvin);
751                *(tmpchar+8)=(char)0;
752               }
753             if (ftype == ASCII)
754               {
755                fscanf(gmvin,"%s",tmpchar);
756                *(tmpchar+8)=(char)0;
757               }
758             ioerrtst(gmvin);
759             gmv_data.keyword = curr_keyword;
760             gmv_data.datatype = 0;
761             strcpy(gmv_data.name1,tmpchar);
762             readkeyword = 1;
763             break;
764          case(CELLPES):
765             fromfilecheck(CELLPES);
766             readcellpes(gmvin,ftype);
767             readkeyword = 1;
768             break;
769          case(SUBVARS):
770             readsubvars(gmvin,ftype);
771             if (readkeyword == 1) readkeyword = 0;
772             break;
773          case(GHOSTS):
774             readghosts(gmvin,ftype);
775             readkeyword = 1;
776             break;
777          case(VECTORS):
778             readvects(gmvin,ftype);
779             if (readkeyword == 1) readkeyword = 0;
780             break;
781         }
782      }
783 
784    if (iend)
785      {
786       gmv_data.keyword = GMVEND;
787       /* fclose(gmvin); */
788      }
789 
790    if (gmv_data.keyword == GMVERROR) fclose(gmvin);
791 
792    if (readkeyword == 1 && reading_fromfile && curr_keyword == ff_keyword)
793       endfromfile();
794 
795    if (readkeyword == 2)
796      {
797       if (reading_fromfile && curr_keyword == ff_keyword) endfromfile();
798       readkeyword = 1;
799      }
800 
801    if (fromfileflag == 1 && fromfileskip == 1) fromfileflag = 0;
802    if (fromfileflag == 1 && reading_fromfile == 0) fromfileflag = 0;
803 }
804 
805 
rdints(int iarray[],int nvals,FILE * gmvin)806 void rdints(int iarray[], int nvals, FILE* gmvin)
807 {
808   /*                                                  */
809   /*  Read an integer array from an ASCII text file.  */
810   /*                                                  */
811   int i;
812 
813    for (i = 0; i < nvals; i++)
814      {
815       fscanf(gmvin,"%d",&iarray[i]);
816 
817       if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
818         {
819          fprintf(stderr,"I/O error while reading gmv input file.\n");
820          gmv_data.keyword = GMVERROR;
821          return;
822         }
823      }
824 }
825 
826 
rdlongs(long iarray[],long nvals,FILE * gmvin)827 void rdlongs(long iarray[], long nvals, FILE* gmvin)
828 {
829   /*                                                  */
830   /*  Read an integer array from an ASCII text file.  */
831   /*                                                  */
832   long i;
833 
834    for (i = 0; i < nvals; i++)
835      {
836       fscanf(gmvin,"%ld",&iarray[i]);
837 
838       if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
839         {
840          fprintf(stderr,"I/O error while reading gmv input file.\n");
841          gmv_data.keyword = GMVERROR;
842          return;
843         }
844      }
845 }
846 
847 
rdfloats(double farray[],long nvals,FILE * gmvin)848 void rdfloats(double farray[], long nvals, FILE* gmvin)
849 {
850   /*                                                  */
851   /*  Read an integer array from an ASCII text file.  */
852   /*                                                  */
853   int i;
854 
855    for (i = 0; i < nvals; i++)
856      {
857       fscanf(gmvin,"%lf",&farray[i]);
858 
859       if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
860         {
861          fprintf(stderr,"I/O error while reading gmv input file.\n");
862          gmv_data.keyword = GMVERROR;
863          return;
864         }
865      }
866 }
867 
868 
rdcellkeyword(FILE * gmvin,int ftype,char * keystring)869 int rdcellkeyword(FILE* gmvin, int ftype, char* keystring)
870 {
871   char ckeyword[9];
872 
873    if (ftype != ASCII)
874      {
875       binread(ckeyword, charsize, CHAR, (long)8, gmvin);
876 
877       if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
878         return (-1);
879 
880       *(ckeyword+8)=(char)0;
881      }
882    if (ftype == ASCII) fscanf(gmvin,"%s",ckeyword);
883 
884    return
885      strncmp(ckeyword,keystring,strlen(keystring));
886 }
887 
888 
fromfilecheck(int fkeyword)889 void fromfilecheck(int fkeyword)
890 {
891   long pos_after_keyword;
892   int base_ftype;
893   void checkfromfile();
894   FILE *basefile;
895 
896    basefile = gmvin;
897    base_ftype = ftype;
898    pos_after_keyword = ftell(gmvin);
899    checkfromfile();
900 
901    if (gmvin == basefile)
902      {
903       if (fromfileflag == 0)
904          fseek(gmvin, pos_after_keyword, SEEK_SET);
905       return;
906      }
907    else
908      {
909 
910       /*  Save current data.  */
911       gmvin_sav = basefile;
912       ftype_sav = base_ftype;
913 
914       /*  Skip to fkeyword.  */
915       ff_keyword = fkeyword;
916       reading_fromfile = 1;
917       while (9)
918         {
919          gmvread_data();
920          if (gmv_data.keyword == fkeyword) fromfileflag = 1;
921          if (gmv_data.keyword == fkeyword) break;
922         }
923 
924      }
925 
926    return;
927 }
928 
929 
endfromfile()930 void endfromfile()
931 {
932    /*  Reset current data.  */
933    ftype = ftype_sav;
934    fromfileskip = 0;
935    fclose(gmvin);
936    gmvin = gmvin_sav;
937    fromfileflag = 0;
938    reading_fromfile = 0;
939    ff_keyword = -1;
940    skipflag = 0;
941 }
942 
943 
checkfromfile()944 void checkfromfile()
945 {
946   char c, charptr[300], *charptr2, tmpbuf[200], stringbuf[100];
947   int i, ierr, fkeyword;
948 
949    /*  Check for "from".  */
950    if (ftype != ASCII)
951       binread(stringbuf, charsize, CHAR, (long)4, gmvin);
952    else
953       fscanf(gmvin,"%s",stringbuf);
954 
955    if (strncmp("from",stringbuf, 4) != 0) return;
956 
957    if (ftype != ASCII)
958      {
959       /* Gobble the "file" part */
960       binread(&wordbuf, intsize, WORD, (long)1, gmvin);
961 
962       /* Get the doublequote */
963       binread(&c, charsize, CHAR, (long)1, gmvin);
964       for (i = 0; 1; i++)
965         {
966          binread(&c, charsize, CHAR, (long)1, gmvin);
967          if (c == '"') break;
968          tmpbuf[i] = c;
969          tmpbuf[i + 1] = '\0';
970         }
971       charptr2 = tmpbuf;
972      }
973 
974    if (ftype == ASCII)
975      {
976       fscanf(gmvin,"%s",tmpbuf);
977       charptr2 = strpbrk(&tmpbuf[1],"\"");
978       *charptr2 = '\0';
979       charptr2 = &tmpbuf[1];
980      }
981 
982    /*  If the first character of the fromfile filename is    */
983    /*  not /, then add the input directory to the filename.  */
984    if (strncmp(charptr2,"/",1) != 0)
985      {
986       strcpy(charptr,input_dir);
987       strcat(charptr,charptr2);
988      }
989    else
990       strcpy(charptr,charptr2);
991 
992    /*  Only returning fromfile filename.  */
993    if (fromfileskip == 1 && fromfileflag == 0)
994      {
995 
996       /*  Check nodes, cells, faces and surface    */
997       /*  and read numbers for the other keywords.  */
998       if ((curr_keyword == NODES && nodes_read == 0) ||
999           (curr_keyword == CELLS && cells_read == 0) ||
1000           (curr_keyword == FACES && faces_read == 0) ||
1001           (curr_keyword == VFACES && faces_read == 0) ||
1002           (curr_keyword == XFACES && faces_read == 0) ||
1003           (curr_keyword == SURFACE && surface_read == 0))
1004         {
1005          gmvin_sav = gmvin;
1006          ftype_sav = ftype;
1007 
1008          ierr = gmvread_open_fromfileskip(charptr);
1009          if (ierr > 0)
1010             {
1011              fprintf(stderr,"GMV cannot read fromfile %s\n",charptr);
1012              exit(0);
1013             }
1014 
1015          /*  Skip to fkeyword.  */
1016          ff_keyword = curr_keyword;
1017          fkeyword = curr_keyword;
1018          reading_fromfile = 1;
1019          while (9)
1020            {
1021             gmvread_data();
1022             if (gmv_data.keyword == fkeyword) break;
1023            }
1024 
1025          if (ff_keyword > -1)
1026            {
1027             fclose(gmvin);
1028             gmvin = gmvin_sav;
1029             ftype = ftype_sav;
1030            }
1031          skipflag = 0;
1032          reading_fromfile = 0;
1033          ff_keyword = -1;
1034          readkeyword = 1;
1035          fromfileskip = 1;
1036         }
1037 
1038       fromfileflag = 1;
1039       gmv_data.keyword = curr_keyword;
1040       gmv_data.datatype = FROMFILE;
1041       i = strlen(charptr);
1042       gmv_data.nchardata1 = i;
1043       gmv_data.chardata1 = (char *)malloc(i*sizeof(char));
1044       strcpy(gmv_data.chardata1,charptr);
1045       return;
1046      }
1047 
1048    if (fromfileskip == 1 && fromfileflag == 1) return;
1049 
1050    /*  Open fromfile, but do not allow more fromfiles.  */
1051    ierr = gmvread_open_fromfileskip(charptr);
1052    if (ierr > 0)
1053       {
1054        fprintf(stderr,"GMV cannot read fromfile %s\n",charptr);
1055        exit(0);
1056       }
1057    printf("GMV reading %s from fromfile %s\n",sav_keyword,charptr);
1058 
1059    return;
1060 }
1061 
1062 
readnodes(FILE * gmvin,int ftype)1063 void readnodes(FILE* gmvin, int ftype)
1064 {
1065   int i, k, iswap, lnxv, lnyv, lnzv, lstructuredflag;
1066   long lnodes, tmplnodes;
1067   double *lxic, *lyic, *lzic, *tmpdouble;
1068   long pos_after_lnodes, exp_cell_pos;
1069   float *tmpfloat;
1070   char ckkeyword[9];
1071 
1072    if (ftype == ASCII)
1073      {
1074       fscanf(gmvin,"%ld",&lnodes);
1075       ioerrtst(gmvin);
1076      }
1077    else
1078      {
1079       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1080         {
1081          binread(&lnodes,longlongsize,LONGLONG,(long)1,gmvin);
1082         }
1083       else
1084         {
1085          binread(&i,intsize,INT,(long)1,gmvin);
1086          iswap = i;
1087          swapbytes(&iswap, intsize, 1);
1088          lnodes = i;
1089         }
1090      }
1091 
1092    /*  Check for byte swapping.  */
1093    if (lnodes < -10 && ftype != ASCII)
1094      {
1095       swapbytes_on = 1;
1096       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1097          swapbytes(&lnodes, longlongsize, 1);
1098       else
1099          lnodes = iswap;
1100      }
1101    /* Is more than the bottom half of the word full? */
1102    else if (ftype != ASCII && lnodes > 0xffff)
1103      {
1104       tmplnodes = lnodes;
1105       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1106          swapbytes(&tmplnodes, longlongsize, 1);
1107       else
1108          tmplnodes = iswap;
1109       if (tmplnodes == -2)
1110         {
1111          /* Byte swapped, structured grid */
1112          lnodes = tmplnodes;
1113          swapbytes_on = 1;
1114         }
1115       else
1116         {
1117 
1118          /*  Unstructured grid, seek to "cells" keyword and see     */
1119          /*  if it is where we expect it. If so, not byte swapped.  */
1120 
1121          pos_after_lnodes = ftell(gmvin);
1122          if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
1123             exp_cell_pos = 3 * lnodes * doublesize;
1124          else
1125             exp_cell_pos = 3 * lnodes * floatsize;
1126          fseek(gmvin, exp_cell_pos, SEEK_CUR);
1127          if (ftype != ASCII)
1128            {
1129             binread(ckkeyword,charsize,CHAR,(long)8,gmvin);
1130             *(ckkeyword+8)=(char)0;
1131            }
1132          if (ftype == ASCII) fscanf(gmvin,"%s",ckkeyword);
1133          if (strncmp(ckkeyword,"cells",5) != 0 &&
1134              strncmp(ckkeyword,"faces",5) != 0 &&
1135              strncmp(ckkeyword,"xfaces",6) != 0 &&
1136              strncmp(ckkeyword,"endgmv",6) != 0)
1137            {
1138             /* We are not where expected, must be byte swapped */
1139 
1140             swapbytes_on = 1;
1141             if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1142                swapbytes(&lnodes, longlongsize, 1);
1143             else
1144                lnodes = iswap;
1145            }
1146 
1147          /* Go back to the previous position */
1148          fseek(gmvin, pos_after_lnodes, SEEK_SET);
1149         }
1150      }
1151    else if (ftype != ASCII && lnodes == -1)
1152      {
1153       /* Special case, -1 is still -1 if byte swapped */
1154 
1155       pos_after_lnodes = ftell(gmvin);
1156 
1157       binread(&lnxv,intsize,INT,(long)1,gmvin);
1158       binread(&lnyv,intsize,INT,(long)1,gmvin);
1159       binread(&lnzv,intsize,INT,(long)1,gmvin);
1160 
1161       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
1162          exp_cell_pos = (lnxv + lnyv + lnzv) * doublesize;
1163       else
1164          exp_cell_pos = (lnxv + lnyv + lnzv) * floatsize;
1165       fseek(gmvin, exp_cell_pos, SEEK_CUR);
1166       if (ftype != ASCII)
1167         {
1168          binread(ckkeyword,charsize,CHAR,(long)8,gmvin);
1169          *(ckkeyword+8)=(char)0;
1170         }
1171       if (ftype == ASCII) fscanf(gmvin,"%s",ckkeyword);
1172       if (strncmp(ckkeyword,"cells",5) != 0 &&
1173           strncmp(ckkeyword,"faces",5) != 0 &&
1174           strncmp(ckkeyword,"vfaces",6) != 0)
1175         {
1176          /* We are not where expected, must be byte swapped */
1177 
1178          swapbytes_on = 1;
1179         }
1180 
1181       /* Go back to the previous position */
1182 
1183       fseek(gmvin, pos_after_lnodes, SEEK_SET);
1184      }
1185 
1186    /*  If lnodes is -1, then block structured grid.      */
1187    /*  If lnodes is -2, then logically structured grid.  */
1188    /*  Read lnxv, lnyv, lnzv. for structured grids.      */
1189    lstructuredflag = 0;
1190    structflag_in = 0;
1191    if (lnodes == -1 || lnodes == -2)
1192      {
1193       if (lnodes == -1) lstructuredflag = 1;
1194       if (lnodes == -2) lstructuredflag = 2;
1195       if (ftype != ASCII)
1196         {
1197          binread(&lnxv,intsize,INT,(long)1,gmvin);
1198          binread(&lnyv,intsize,INT,(long)1,gmvin);
1199          binread(&lnzv,intsize,INT,(long)1,gmvin);
1200          ioerrtst(gmvin);
1201         }
1202       if (ftype == ASCII)
1203         {
1204          fscanf(gmvin,"%d%d%d",&lnxv,&lnyv,&lnzv);
1205          ioerrtst(gmvin);
1206         }
1207 
1208       lnodes = lnxv * lnyv * lnzv;
1209       structflag_in = 1;
1210      }
1211 
1212    /*  If lnodes is -3, then amr grid.  Read      */
1213    /*  lnxv, lnyv, lnzv, start xyz and dx,dy,dz.  */
1214    amrflag_in = 0;
1215    if (lnodes == -3)
1216      {
1217       amrflag_in = 1;
1218       if (ftype != ASCII)
1219         {
1220          binread(&lnxv,intsize,INT,(long)1,gmvin);
1221          binread(&lnyv,intsize,INT,(long)1,gmvin);
1222          binread(&lnzv,intsize,INT,(long)1,gmvin);
1223          ioerrtst(gmvin);
1224         }
1225       if (ftype == ASCII)
1226         {
1227          fscanf(gmvin,"%d%d%d",&lnxv,&lnyv,&lnzv);
1228          ioerrtst(gmvin);
1229         }
1230 
1231       lxic = (double *)malloc((3)*sizeof(double));
1232       lyic = (double *)malloc((3)*sizeof(double));
1233       if (ftype != ASCII)
1234         {
1235          if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
1236            {
1237             binread(lxic,doublesize,DOUBLE,(long)3,gmvin);
1238             ioerrtst(gmvin);
1239             binread(lyic,doublesize,DOUBLE,(long)3,gmvin);
1240             ioerrtst(gmvin);
1241            }
1242          else
1243            {
1244             tmpfloat = (float *)malloc((3)*sizeof(float));
1245             binread(tmpfloat,floatsize,FLOAT,(long)3,gmvin);
1246             ioerrtst(gmvin);
1247             for (i = 0; i < 3; i++) lxic[i] = tmpfloat[i];
1248             binread(tmpfloat,floatsize,FLOAT,(long)3,gmvin);
1249             ioerrtst(gmvin);
1250             for (i = 0; i < 3; i++) lyic[i] = tmpfloat[i];
1251             free(tmpfloat);
1252            }
1253         }
1254       if (ftype == ASCII)
1255         {
1256          rdfloats(lxic,(long)3,gmvin);
1257          rdfloats(lyic,(long)3,gmvin);
1258         }
1259 
1260       gmv_data.keyword = NODES;
1261       gmv_data.datatype = AMR;
1262       gmv_data.num2 = lnxv;
1263       gmv_data.nlongdata1 = lnyv;
1264       gmv_data.nlongdata2 = lnzv;
1265       gmv_data.ndoubledata1 = 3;
1266       gmv_data.doubledata1 = lxic;
1267       gmv_data.ndoubledata2 = 3;
1268       gmv_data.doubledata2 = lyic;
1269       numnodes = lnxv * lnyv * lnzv;
1270       nodes_read = 1;
1271       return;
1272      }
1273 
1274    if (printon)
1275       printf("Reading %ld nodes.\n",lnodes);
1276 
1277    /*  Allocate and read node x,y,z arrays.  */
1278 
1279    if (lstructuredflag == 0 || lstructuredflag == 2)
1280      {
1281       lxic = (double *)malloc((lnodes)*sizeof(double));
1282       lyic = (double *)malloc((lnodes)*sizeof(double));
1283       lzic = (double *)malloc((lnodes)*sizeof(double));
1284       if (lxic == NULL || lyic == NULL || lzic == NULL)
1285         {
1286          gmvrdmemerr();
1287          return;
1288         }
1289       if (ftype != ASCII)
1290         {
1291          if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
1292            {
1293             tmpdouble = (double *)malloc((3*lnodes)*sizeof(double));
1294             if (tmpdouble == NULL)
1295               {
1296                gmvrdmemerr();
1297                return;
1298               }
1299             binread(tmpdouble,doublesize,DOUBLE,3*lnodes,gmvin);
1300             ioerrtst(gmvin);
1301             if (node_inp_type == 0)  /*  nodes type  */
1302               {
1303                for (i = 0; i < lnodes; i++)
1304                  {
1305                   lxic[i] = tmpdouble[i];
1306                   lyic[i] = tmpdouble[lnodes+i];
1307                   lzic[i] = tmpdouble[2*lnodes+i];
1308                  }
1309               }
1310             if (node_inp_type == 1)  /*  nodev type  */
1311               {
1312                for (i = 0; i < lnodes; i++)
1313                  {
1314                   lxic[i] = tmpdouble[3*i];
1315                   lyic[i] = tmpdouble[3*i+1];
1316                   lzic[i] = tmpdouble[3*i+2];
1317                  }
1318               }
1319             FREE(tmpdouble);
1320            }
1321          else
1322            {
1323             tmpfloat = (float *)malloc((3*lnodes)*sizeof(float));
1324             if (tmpfloat == NULL)
1325               {
1326                gmvrdmemerr();
1327                return;
1328               }
1329             binread(tmpfloat,floatsize,FLOAT,3*lnodes,gmvin);
1330             ioerrtst(gmvin);
1331             if (node_inp_type == 0)  /*  nodes type  */
1332               {
1333                for (i = 0; i < lnodes; i++)
1334                  {
1335                   lxic[i] = tmpfloat[i];
1336                   lyic[i] = tmpfloat[lnodes+i];
1337                   lzic[i] = tmpfloat[2*lnodes+i];
1338                  }
1339               }
1340             if (node_inp_type == 1)  /*  nodev type  */
1341               {
1342                for (i = 0; i < lnodes; i++)
1343                  {
1344                   lxic[i] = tmpfloat[3*i];
1345                   lyic[i] = tmpfloat[3*i+1];
1346                   lzic[i] = tmpfloat[3*i+2];
1347                  }
1348               }
1349             FREE(tmpfloat);
1350            }
1351         }
1352       if (ftype == ASCII)
1353         {
1354          tmpdouble = (double *)malloc((3*lnodes)*sizeof(double));
1355          if (tmpdouble == NULL)
1356               {
1357                gmvrdmemerr();
1358                return;
1359               }
1360          rdfloats(tmpdouble,3*lnodes,gmvin);
1361          if (node_inp_type == 0)  /*  nodes type  */
1362            {
1363             for (i = 0; i < lnodes; i++)
1364               {
1365                lxic[i] = tmpdouble[i];
1366                lyic[i] = tmpdouble[lnodes+i];
1367                lzic[i] = tmpdouble[2*lnodes+i];
1368               }
1369            }
1370          if (node_inp_type == 1)  /*  nodev type  */
1371            {
1372             for (i = 0; i < lnodes; i++)
1373               {
1374                lxic[i] = tmpdouble[3*i];
1375                lyic[i] = tmpdouble[3*i+1];
1376                lzic[i] = tmpdouble[3*i+2];
1377               }
1378            }
1379          FREE(tmpdouble);
1380         }
1381      }
1382 
1383    if (lstructuredflag == 1)
1384      {
1385       lxic = (double *)malloc((lnxv)*sizeof(double));
1386       lyic = (double *)malloc((lnyv)*sizeof(double));
1387       lzic = (double *)malloc((lnzv)*sizeof(double));
1388       if (lxic == NULL || lyic == NULL || lzic == NULL)
1389         {
1390          gmvrdmemerr();
1391          return;
1392         }
1393       if (ftype != ASCII)
1394         {
1395          if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
1396            {
1397             binread(lxic,doublesize,DOUBLE,(long)lnxv,gmvin);
1398             ioerrtst(gmvin);
1399             binread(lyic,doublesize,DOUBLE,(long)lnyv,gmvin);
1400             ioerrtst(gmvin);
1401             binread(lzic,doublesize,DOUBLE,(long)lnzv,gmvin);
1402             ioerrtst(gmvin);
1403            }
1404          else
1405            {
1406             k = (lnxv > lnyv) ? lnxv : lnyv;
1407             k = (k > lnzv) ? k : lnzv;
1408             tmpfloat = (float *)malloc((k)*sizeof(float));
1409             if (tmpfloat == NULL)
1410               {
1411                gmvrdmemerr();
1412                return;
1413               }
1414             binread(tmpfloat,floatsize,FLOAT,(long)lnxv,gmvin);
1415             ioerrtst(gmvin);
1416             for (i = 0; i < lnxv; i++) lxic[i] = tmpfloat[i];
1417             binread(tmpfloat,floatsize,FLOAT,(long)lnyv,gmvin);
1418             ioerrtst(gmvin);
1419             for (i = 0; i < lnyv; i++) lyic[i] = tmpfloat[i];
1420             binread(tmpfloat,floatsize,FLOAT,(long)lnzv,gmvin);
1421             ioerrtst(gmvin);
1422             for (i = 0; i < lnzv; i++) lzic[i] = tmpfloat[i];
1423             free(tmpfloat);
1424            }
1425         }
1426       if (ftype == ASCII)
1427         {
1428          rdfloats(lxic,(long)lnxv,gmvin);
1429          rdfloats(lyic,(long)lnyv,gmvin);
1430          rdfloats(lzic,(long)lnzv,gmvin);
1431         }
1432      }
1433 
1434    if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
1435      {
1436       fprintf(stderr,"I/O error while reading nodes.\n");
1437       gmv_data.keyword = GMVERROR;
1438       return;
1439      }
1440 
1441    if (!skipflag)
1442      {
1443       numnodes = lnodes;
1444       nodes_read = 1;
1445      }
1446 
1447    if (amrflag_in == 0)
1448      {
1449       gmv_data.keyword = NODES;
1450       if (lstructuredflag == 0) gmv_data.datatype = UNSTRUCT;
1451       if (lstructuredflag == 1) gmv_data.datatype = STRUCT;
1452       if (lstructuredflag == 2) gmv_data.datatype = LOGICALLY_STRUCT;
1453       gmv_data.num = lnodes;
1454       gmv_data.num2 = NODES;
1455       if (node_inp_type == 1) gmv_data.num2 = NODE_V;
1456       if (lstructuredflag == 1 || lstructuredflag == 2)
1457         {
1458          gmv_data.ndoubledata1 = lnxv;
1459          gmv_data.doubledata1 = lxic;
1460          gmv_data.ndoubledata2 = lnyv;
1461          gmv_data.doubledata2 = lyic;
1462          gmv_data.ndoubledata3 = lnzv;
1463          gmv_data.doubledata3 = lzic;
1464          ncells_struct = (lnxv-1) * (lnyv-1) * (lnzv-1);
1465          if (lnzv == 1) ncells_struct = (lnxv-1) * (lnyv-1);
1466         }
1467       else
1468         {
1469          gmv_data.ndoubledata1 = lnodes;
1470          gmv_data.doubledata1 = lxic;
1471          gmv_data.ndoubledata2 = lnodes;
1472          gmv_data.doubledata2 = lyic;
1473          gmv_data.ndoubledata3 = lnodes;
1474          gmv_data.doubledata3 = lzic;
1475         }
1476      }
1477 }
1478 
1479 
readcells(FILE * gmvin,int ftype)1480 void readcells(FILE* gmvin, int ftype)
1481 {
1482   int i, ndat, nverts[1000], totverts, *verts,
1483       *cellnodenos, nfaces;
1484   long *lfaces, numtop, *daughters;
1485   long *lcellnodenos;
1486   char keyword[9];
1487 
1488    if (readkeyword == 1)
1489      {
1490       numcellsin = 0;
1491       if (ftype == ASCII)
1492         {
1493          fscanf(gmvin,"%ld",&lncells);
1494          ioerrtst(gmvin);
1495         }
1496       else
1497         {
1498          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1499            {
1500             binread(&lncells,longlongsize,LONGLONG,(long)1,gmvin);
1501            }
1502          else
1503            {
1504             binread(&i,intsize,INT,(long)1,gmvin);
1505             lncells = i;
1506            }
1507         }
1508 
1509       if (printon)
1510          printf("Reading %ld cells.\n",lncells);
1511 
1512       if (!skipflag)
1513         {
1514          numcells = lncells;
1515          cells_read = 1;
1516         }
1517      }
1518 
1519    /*  If amr, read numtop and daughter list.  */
1520    if (amrflag_in)
1521      {
1522       if (ftype == ASCII)
1523         {
1524          fscanf(gmvin,"%ld",&numtop);
1525          ioerrtst(gmvin);
1526         }
1527       else
1528         {
1529          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1530            {
1531             binread(&numtop,longlongsize,LONGLONG,(long)1,gmvin);
1532            }
1533          else
1534            {
1535             binread(&i,intsize,INT,(long)1,gmvin);
1536             numtop = i;
1537            }
1538         }
1539 
1540       daughters = (long *)malloc(lncells*sizeof(long));
1541       if (daughters == NULL)
1542         {
1543          gmvrdmemerr();
1544          return;
1545         }
1546       if (ftype != ASCII)
1547         {
1548          if (ftype == IEEEI4R4 || ftype == IEEEI4R8)
1549            {
1550             cellnodenos = (int *)malloc(lncells*sizeof(int));
1551             if (cellnodenos == NULL)
1552               {
1553                gmvrdmemerr();
1554                return;
1555               }
1556             binread(cellnodenos,intsize,INT,lncells,gmvin);
1557             ioerrtst(gmvin);
1558             for (i = 0; i < lncells; i++)
1559               daughters[i] = cellnodenos[i];
1560             FREE(cellnodenos);
1561            }
1562          else
1563            {
1564             binread(daughters,longlongsize,LONGLONG,lncells,gmvin);
1565             ioerrtst(gmvin);
1566            }
1567         }
1568       if (ftype == ASCII) rdlongs(daughters,lncells,gmvin);
1569 
1570       gmv_data.keyword = CELLS;
1571       gmv_data.datatype = AMR;
1572       gmv_data.num = lncells;
1573       gmv_data.num2 = numtop;
1574       gmv_data.nlongdata1 = lncells;
1575       gmv_data.longdata1 = daughters;
1576       numcells = numtop;
1577       readkeyword = 1;
1578       return;
1579      }
1580 
1581    /*  See if all cells read.  */
1582    numcellsin++;
1583    if (numcellsin > lncells)
1584      {
1585       readkeyword = 2;
1586       if (numcells == 0) readkeyword = 1;
1587 
1588       /*  If structured mesh, reset numcells.  */
1589       if (structflag_in == 1) numcells = ncells_struct;
1590       gmv_data.keyword = CELLS;
1591       gmv_data.datatype = ENDKEYWORD;
1592       gmv_data.num = numcells;
1593       return;
1594      }
1595 
1596    /*  Read a cell at at time.  */
1597    if (ftype != ASCII)
1598      {
1599       binread(keyword,charsize,CHAR,(long)8,gmvin);
1600       *(keyword+8)=(char)0;
1601       binread(&ndat,intsize,INT,(long)1,gmvin);
1602       ioerrtst(gmvin);
1603      }
1604    if (ftype == ASCII) fscanf(gmvin,"%s%d",keyword,&ndat);
1605 
1606    /*  Check cell type.  */
1607    if (strncmp(keyword,"general",7) != 0 &&
1608        strncmp(keyword,"line",4) != 0 &&
1609        strncmp(keyword,"tri",3) != 0 &&
1610        strncmp(keyword,"quad",4) != 0 &&
1611        strncmp(keyword,"tet",3) != 0 &&
1612        strncmp(keyword,"hex",3) != 0 &&
1613        strncmp(keyword,"prism",5) != 0 &&
1614        strncmp(keyword,"pyramid",7) != 0 &&
1615        strncmp(keyword,"vface2d",7) != 0 &&
1616        strncmp(keyword,"vface3d",7) != 0 &&
1617        strncmp(keyword,"phex8",5) != 0 &&
1618        strncmp(keyword,"phex20",6) != 0 &&
1619        strncmp(keyword,"phex27",6) != 0 &&
1620        strncmp(keyword,"ppyrmd5",7) != 0 &&
1621        strncmp(keyword,"ppyrmd13",8) != 0 &&
1622        strncmp(keyword,"pprism6",7) != 0 &&
1623        strncmp(keyword,"pprism15",8) != 0 &&
1624        strncmp(keyword,"ptet4",5) != 0 &&
1625        strncmp(keyword,"ptet10",6) != 0 &&
1626        strncmp(keyword,"6tri",4) != 0 &&
1627        strncmp(keyword,"8quad",5) != 0 &&
1628        strncmp(keyword,"3line",5) != 0)
1629      {
1630       fprintf(stderr,
1631               "Error, %s is an invalid cell type.\n",keyword);
1632       gmv_data.keyword = GMVERROR;
1633       return;
1634      }
1635    if ((strncmp(keyword,"line",4) == 0 && ndat != 2) ||
1636        (strncmp(keyword,"tri",3) == 0 && ndat != 3) ||
1637        (strncmp(keyword,"quad",4) == 0 && ndat != 4) ||
1638        (strncmp(keyword,"tet",3) == 0 && ndat != 4) ||
1639        (strncmp(keyword,"hex",3) == 0 && ndat != 8) ||
1640        (strncmp(keyword,"prism",5) == 0 && ndat != 6) ||
1641        (strncmp(keyword,"pyramid",7) == 0 && ndat != 5) ||
1642        (strncmp(keyword,"phex8",5) == 0 && ndat != 8) ||
1643        (strncmp(keyword,"phex20",6) == 0 && ndat != 20) ||
1644        (strncmp(keyword,"phex27",6) == 0 && ndat != 27) ||
1645        (strncmp(keyword,"ppyrmd5",7) == 0 && ndat != 5) ||
1646        (strncmp(keyword,"ppyrmd13",8) == 0 && ndat != 13) ||
1647        (strncmp(keyword,"pprism6",7) == 0 && ndat != 6) ||
1648        (strncmp(keyword,"pprism15",8) == 0 && ndat != 15) ||
1649        (strncmp(keyword,"ptet4",5) == 0 && ndat != 4) ||
1650        (strncmp(keyword,"ptet10",6) == 0 && ndat != 10) ||
1651        (strncmp(keyword,"6tri",4) == 0 && ndat != 6) ||
1652        (strncmp(keyword,"8quad",5) == 0 && ndat != 8) ||
1653        (strncmp(keyword,"3line",5) == 0 && ndat != 3))
1654      {
1655       fprintf(stderr,
1656               "Error, %d nodes is invalid for a %s.\n",ndat,keyword);
1657       gmv_data.keyword = GMVERROR;
1658       return;
1659      }
1660 
1661    if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
1662      {
1663       fprintf(stderr,"I/O error while reading cells.\n");
1664       gmv_data.keyword = GMVERROR;
1665       return;
1666      }
1667 
1668    /*  At first cell, set vfaceflag.  */
1669    if (readkeyword == 1)
1670      {
1671       vfaceflag = 0;
1672       if (strncmp(keyword,"vface2d",7) == 0) vfaceflag = 2;
1673       if (strncmp(keyword,"vface3d",7) == 0) vfaceflag = 3;
1674      }
1675 
1676    /*  After first cell, check for vface consistency.  */
1677    if (readkeyword == 0)
1678      {
1679       if ((vfaceflag == 0 && strncmp(keyword,"vface",5) == 0) ||
1680           (vfaceflag > 0 && strncmp(keyword,"vface",5) != 0))
1681         {
1682          fprintf(stderr,
1683            "Error, cannot mix vface2d or vface3d with other cell types.\n");
1684          gmv_data.keyword = GMVERROR;
1685          return;
1686         }
1687       if ((vfaceflag == 2 && strncmp(keyword,"vface3d",7) == 0) ||
1688           (vfaceflag == 3 && strncmp(keyword,"vface2d",7) == 0))
1689         {
1690          fprintf(stderr,
1691            "Error, cannot mix vface2d and vface3d cell types.\n");
1692          gmv_data.keyword = GMVERROR;
1693          return;
1694         }
1695      }
1696 
1697    /*  Read general cell data.  */
1698    if (strncmp(keyword,"general",7) == 0)
1699      {
1700 
1701       /*  Read no. of vertices per face.  */
1702       nfaces = ndat;
1703       if (nfaces > 1000)
1704         {
1705          fprintf(stderr,
1706                   "Error, Read %d faces - 1000 faces per cell allowed.\n",
1707                    nfaces);
1708          gmv_data.keyword = GMVERROR;
1709          return;
1710         }
1711       if (ftype != ASCII) binread(nverts,intsize,INT,(long)nfaces,gmvin);
1712       if (ftype == ASCII) rdints(nverts,nfaces,gmvin);
1713       ioerrtst(gmvin);
1714 
1715       /*  Read all face vertices, reallocate if needed.  */
1716       totverts = 0;
1717       for (i = 0; i < nfaces; i++) totverts += nverts[i];
1718       lcellnodenos = (long *)malloc(totverts*sizeof(long));
1719       if (lcellnodenos == NULL)
1720         {
1721          gmvrdmemerr();
1722          return;
1723         }
1724       if (ftype != ASCII)
1725         {
1726          if (ftype == IEEEI4R4 || ftype == IEEEI4R8)
1727            {
1728             cellnodenos = (int *)malloc(totverts*sizeof(int));
1729             if (cellnodenos == NULL)
1730               {
1731                gmvrdmemerr();
1732                return;
1733               }
1734             binread(cellnodenos,intsize,INT,(long)totverts,gmvin);
1735             for (i = 0; i < totverts; i++)
1736               lcellnodenos[i] = cellnodenos[i];
1737             FREE(cellnodenos);
1738            }
1739          else
1740             binread(lcellnodenos,longlongsize,LONGLONG,
1741                     (long)totverts,gmvin);
1742          ioerrtst(gmvin);
1743         }
1744       if (ftype == ASCII) rdlongs(lcellnodenos,(long)totverts,gmvin);
1745       lfaces = (long *)malloc(nfaces*sizeof(long));
1746       if (lfaces == NULL)
1747         {
1748          gmvrdmemerr();
1749          return;
1750         }
1751       for (i = 0; i < nfaces; i++)
1752         lfaces[i] = nverts[i];
1753 
1754       gmv_data.keyword = CELLS;
1755       gmv_data.datatype = GENERAL;
1756       strcpy(gmv_data.name1,keyword);
1757       gmv_data.num = lncells;
1758       gmv_data.num2 = nfaces;
1759       gmv_data.nlongdata1 = nfaces;
1760       gmv_data.longdata1 = lfaces;
1761       gmv_data.nlongdata2 = totverts;
1762       gmv_data.longdata2 = lcellnodenos;
1763      }
1764 
1765    /*  Else read vface2d or vface3d data.  */
1766    else if (strncmp(keyword,"vface2d",7) == 0 ||
1767             strncmp(keyword,"vface3d",7) == 0)
1768      {
1769 
1770       /*  Check no. of  faces.  */
1771       nfaces = ndat;
1772       if (nfaces > 1000)
1773         {
1774          fprintf(stderr,
1775                   "Error, Read %d faces - 1000 faces per cell allowed.\n",
1776                    nfaces);
1777          gmv_data.keyword = GMVERROR;
1778          return;
1779         }
1780 
1781       /*  Read all face numbers.  */
1782       lfaces = (long *)malloc(nfaces*sizeof(long));
1783       if (lfaces == NULL)
1784         {
1785          gmvrdmemerr();
1786          return;
1787         }
1788       if (ftype != ASCII)
1789         {
1790          if (ftype == IEEEI4R4 || ftype == IEEEI4R8)
1791            {
1792             cellnodenos = (int *)malloc(nfaces*sizeof(int));
1793             if (cellnodenos == NULL)
1794               {
1795                gmvrdmemerr();
1796                return;
1797               }
1798             binread(cellnodenos,intsize,INT,(long)nfaces,gmvin);
1799             for (i = 0; i < nfaces; i++)
1800               lfaces[i] = cellnodenos[i];
1801             FREE(cellnodenos);
1802            }
1803          else
1804             binread(lfaces,longlongsize,LONGLONG,
1805                     (long)nfaces,gmvin);
1806          ioerrtst(gmvin);
1807         }
1808       if (ftype == ASCII) rdlongs(lfaces,(long)nfaces,gmvin);
1809 /*
1810       for (i = 0; i < nfaces; i++)
1811          lfaces[i]--;
1812 */
1813 
1814       gmv_data.keyword = CELLS;
1815       if (strncmp(keyword,"vface2d",7) == 0)
1816          gmv_data.datatype = VFACE2D;
1817       else gmv_data.datatype = VFACE3D;
1818       strcpy(gmv_data.name1,keyword);
1819       gmv_data.num = lncells;
1820       gmv_data.num2 = nfaces;
1821       gmv_data.nlongdata1 = nfaces;
1822       gmv_data.longdata1 = lfaces;
1823      }
1824 
1825    /*  Else read regular cells.  */
1826    else
1827      {
1828       lcellnodenos = (long *)malloc(ndat*sizeof(long));
1829       if (lcellnodenos == NULL)
1830         {
1831          gmvrdmemerr();
1832          return;
1833         }
1834       if (ftype != ASCII)
1835         {
1836          if (ftype == IEEEI4R4 || ftype == IEEEI4R8)
1837            {
1838             verts = (int *)malloc(ndat*sizeof(int));
1839             if (verts == NULL)
1840               {
1841                gmvrdmemerr();
1842                return;
1843               }
1844             binread(verts,intsize,INT,(long)ndat,gmvin);
1845             for (i = 0; i < ndat; i++)
1846               lcellnodenos[i] = verts[i];
1847             free(verts);
1848            }
1849          else
1850             binread(lcellnodenos,longlongsize,LONGLONG,(long)ndat,gmvin);
1851          ioerrtst(gmvin);
1852         }
1853       if (ftype == ASCII) rdlongs(lcellnodenos,(long)ndat,gmvin);
1854 
1855       if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
1856         {
1857          fprintf(stderr,"I/O error while reading cells.\n");
1858          gmv_data.keyword = GMVERROR;
1859          return;
1860         }
1861 
1862       gmv_data.keyword = CELLS;
1863       gmv_data.datatype = REGULAR;
1864       strcpy(gmv_data.name1,keyword);
1865       gmv_data.num = lncells;
1866       gmv_data.num2 = ndat;
1867       gmv_data.nlongdata1 = ndat;
1868       gmv_data.longdata1 = lcellnodenos;
1869      }
1870 }
1871 
1872 
readfaces(FILE * gmvin,int ftype)1873 void readfaces(FILE* gmvin, int ftype)
1874 {
1875   int i, nverts, *tmpvertsin;
1876   long *vertsin;
1877 
1878    if (readkeyword == 1)
1879      {
1880 
1881       /*  Read no. of faces to read and cells to generate.  */
1882       if (ftype == ASCII)
1883         {
1884          fscanf(gmvin,"%ld",&lnfaces);
1885          fscanf(gmvin,"%ld",&lncells);
1886         }
1887       else
1888         {
1889          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1890            {
1891             binread(&lnfaces,longlongsize,LONGLONG,(long)1,gmvin);
1892             binread(&lncells,longlongsize,LONGLONG,(long)1,gmvin);
1893            }
1894          else
1895            {
1896             binread(&i,intsize,INT,(long)1,gmvin);
1897             lnfaces = i;
1898             binread(&i,intsize,INT,(long)1,gmvin);
1899             lncells = i;
1900            }
1901         }
1902       ioerrtst(gmvin);
1903       numfacesin = 0;
1904 
1905       if (printon)
1906          printf("Reading %ld faces.\n",lnfaces);
1907 
1908       if (!skipflag)
1909         {
1910          numfaces = lnfaces;
1911          numcells = lncells;
1912          faces_read = 1;
1913         }
1914      }
1915 
1916    /*  Check no. of faces read.  */
1917    numfacesin++;
1918    if (numfacesin > lnfaces)
1919      {
1920       readkeyword = 2;
1921       gmv_data.keyword = FACES;
1922       gmv_data.num = lnfaces;
1923       gmv_data.num2 = lncells;
1924       gmv_data.datatype = ENDKEYWORD;
1925       return;
1926      }
1927 
1928    /*  Read face vertices and cell info for each face.  */
1929    if (ftype != ASCII) binread(&nverts,intsize,INT,(long)1,gmvin);
1930    if (ftype == ASCII) fscanf(gmvin,"%d",&nverts);
1931    ioerrtst(gmvin);
1932 
1933    /*  Read a face vertices and cells.  */
1934    vertsin = (long *)malloc((nverts+2)*sizeof(long));
1935    if (vertsin == NULL)
1936      {
1937       gmvrdmemerr();
1938       return;
1939      }
1940    if (ftype == ASCII) rdlongs(vertsin,(long)nverts+2,gmvin);
1941    else
1942      {
1943       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1944         {
1945          binread(vertsin,longlongsize,LONGLONG,(long)nverts+2,gmvin);
1946         }
1947       else
1948         {
1949          tmpvertsin = (int *)malloc((nverts+2)*sizeof(int));
1950          if (tmpvertsin == NULL)
1951            {
1952             gmvrdmemerr();
1953             return;
1954            }
1955          binread(tmpvertsin,intsize,INT,(long)nverts+2,gmvin);
1956          for (i = 0; i < nverts+2; i++)
1957             vertsin[i] = tmpvertsin[i];
1958          free(tmpvertsin);
1959         }
1960       ioerrtst(gmvin);
1961      }
1962 
1963    if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
1964      {
1965       fprintf(stderr,"I/O error while reading faces.\n");
1966       gmv_data.keyword = GMVERROR;
1967       return;
1968      }
1969 
1970    gmv_data.keyword = FACES;
1971    gmv_data.datatype = REGULAR;
1972    gmv_data.num = lnfaces;
1973    gmv_data.num2 = lncells;
1974    gmv_data.nlongdata1 = nverts + 2;
1975    gmv_data.longdata1 = vertsin;
1976 }
1977 
1978 
readvfaces(FILE * gmvin,int ftype)1979 void readvfaces(FILE* gmvin, int ftype)
1980 {
1981   int i, nverts, *tmpvertsin, facepe, oppfacepe;
1982   long *vertsin, oppface, cellid;
1983 
1984    if (readkeyword == 1)
1985      {
1986 
1987       /*  Read no. of vfaces to read.  */
1988       if (ftype == ASCII) fscanf(gmvin,"%ld",&lnfaces);
1989       else
1990         {
1991          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
1992            {
1993             binread(&lnfaces,longlongsize,LONGLONG,(long)1,gmvin);
1994            }
1995          else
1996            {
1997             binread(&i,intsize,INT,(long)1,gmvin);
1998             lnfaces = i;
1999            }
2000         }
2001       ioerrtst(gmvin);
2002       numfacesin = 0;
2003 
2004       if (printon)
2005          printf("Reading %ld vfaces.\n",lnfaces);
2006 
2007       if (!skipflag)
2008         {
2009          numfaces = lnfaces;
2010         }
2011      }
2012 
2013    /*  Check no. of vfaces read.  */
2014    numfacesin++;
2015    if (numfacesin > lnfaces)
2016      {
2017       readkeyword = 2;
2018       gmv_data.keyword = VFACES;
2019       gmv_data.datatype = ENDKEYWORD;
2020       return;
2021      }
2022 
2023    /*  Read vface vertices, PE number, opposite  */
2024    /*  face info and cell info for each face.    */
2025    if (ftype != ASCII)
2026      {
2027       binread(&nverts,intsize,INT,(long)1,gmvin);
2028       binread(&facepe,intsize,INT,(long)1,gmvin);
2029       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
2030         {
2031          binread(&oppface,longlongsize,LONGLONG,(long)1,gmvin);
2032          binread(&oppfacepe,intsize,INT,(long)1,gmvin);
2033          binread(&cellid,longlongsize,LONGLONG,(long)1,gmvin);
2034         }
2035       else
2036         {
2037          binread(&i,intsize,INT,(long)1,gmvin);
2038          oppface = i;
2039          binread(&oppfacepe,intsize,INT,(long)1,gmvin);
2040          binread(&i,intsize,INT,(long)1,gmvin);
2041          cellid = i;
2042         }
2043      }
2044    if (ftype == ASCII)
2045       {
2046        fscanf(gmvin,"%d%d",&nverts,&facepe);
2047        fscanf(gmvin,"%ld",&oppface);
2048        fscanf(gmvin,"%d",&oppfacepe);
2049        fscanf(gmvin,"%ld",&cellid);
2050       }
2051    ioerrtst(gmvin);
2052 
2053    /*  Read vface vertices.  */
2054    vertsin = (long *)malloc((nverts)*sizeof(long));
2055    if (vertsin == NULL)
2056      {
2057       gmvrdmemerr();
2058       return;
2059      }
2060    if (ftype == ASCII) rdlongs(vertsin,(long)nverts,gmvin);
2061    else
2062      {
2063       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
2064         {
2065          binread(vertsin,longlongsize,LONGLONG,(long)nverts,gmvin);
2066         }
2067       else
2068         {
2069          tmpvertsin = (int *)malloc((nverts)*sizeof(int));
2070          if (tmpvertsin == NULL)
2071            {
2072             gmvrdmemerr();
2073             return;
2074            }
2075          binread(tmpvertsin,intsize,INT,(long)nverts,gmvin);
2076          for (i = 0; i < nverts; i++)
2077             vertsin[i] = tmpvertsin[i];
2078          free(tmpvertsin);
2079         }
2080       ioerrtst(gmvin);
2081      }
2082 
2083    if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
2084      {
2085       fprintf(stderr,"I/O error while reading faces.\n");
2086       gmv_data.keyword = GMVERROR;
2087       return;
2088      }
2089 
2090    gmv_data.keyword = VFACES;
2091    gmv_data.datatype = REGULAR;
2092    gmv_data.num = lnfaces;
2093    gmv_data.nlongdata1 = nverts;
2094    gmv_data.longdata1 = vertsin;
2095    gmv_data.nlongdata2 = 4;
2096    gmv_data.longdata2 = (long *)malloc(4*sizeof(long));
2097    gmv_data.longdata2[0] = facepe;
2098    gmv_data.longdata2[1] = oppface;
2099    /* gmv_data.longdata2[1] = oppface - 1; */
2100    gmv_data.longdata2[2] = oppfacepe;
2101    gmv_data.longdata2[3] = cellid;
2102 }
2103 
2104 
readxfaces(FILE * gmvin,int ftype)2105 void readxfaces(FILE* gmvin, int ftype)
2106 {
2107   int i, j, *tmpftvin;
2108   long *nxvertsin, *vertsin, totverts;
2109   static int xfaceloc;
2110 
2111 
2112    if (readkeyword == 1)
2113      {
2114 
2115       /*  Read no. of xfaces to read.  */
2116       if (ftype == ASCII) fscanf(gmvin,"%ld",&lnfaces);
2117       else
2118         {
2119          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
2120            {
2121             binread(&lnfaces,longlongsize,LONGLONG,(long)1,gmvin);
2122            }
2123          else
2124            {
2125             binread(&i,intsize,INT,(long)1,gmvin);
2126             lnfaces = i;
2127            }
2128         }
2129       ioerrtst(gmvin);
2130       xfaceloc = 0;
2131 
2132       if (printon)
2133          printf("Reading %ld xfaces.\n",lnfaces);
2134 
2135       if (!skipflag)
2136         {
2137          numfaces = lnfaces;
2138          faces_read = 1;
2139         }
2140      }
2141 
2142    /*  Check location of xfaces to determine what to read.  */
2143    if (xfaceloc == 0)
2144      {
2145 
2146       /*  Allocate and read the face to nodes array.  */
2147       nxvertsin = (long *)malloc(lnfaces*sizeof(long));
2148       if (nxvertsin == NULL)
2149         {
2150          gmvrdmemerr();
2151          return;
2152         }
2153       if (ftype == ASCII) rdlongs(nxvertsin,(long)lnfaces,gmvin);
2154       else
2155         {
2156          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
2157            {
2158             binread(nxvertsin,longlongsize,LONGLONG,(long)lnfaces,gmvin);
2159            }
2160          else
2161            {
2162             tmpftvin = (int *)malloc(lnfaces*sizeof(int));
2163             if (tmpftvin == NULL)
2164               {
2165                gmvrdmemerr();
2166                return;
2167               }
2168             binread(tmpftvin,intsize,INT,(long)lnfaces,gmvin);
2169             for (i = 0; i < lnfaces; i++)
2170               {
2171                nxvertsin[i] = tmpftvin[i];
2172               }
2173             free(tmpftvin);
2174            }
2175         }
2176       ioerrtst(gmvin);
2177 
2178       /*  Count the total number of vertices to read.  */
2179       totverts = 0;
2180       for (i = 0; i < lnfaces; i++)
2181         {
2182          totverts += nxvertsin[i];
2183         }
2184 
2185       /*  Allocate and read the vertices.  */
2186       vertsin = (long *)malloc(totverts*sizeof(long));
2187       if (vertsin == NULL)
2188         {
2189          gmvrdmemerr();
2190          return;
2191         }
2192       if (ftype == ASCII) rdlongs(vertsin,(long)totverts,gmvin);
2193       else
2194         {
2195          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
2196            {
2197             binread(vertsin,longlongsize,LONGLONG,(long)totverts,gmvin);
2198            }
2199          else
2200            {
2201             tmpftvin = (int *)malloc(totverts*sizeof(int));
2202             if (tmpftvin == NULL)
2203               {
2204                gmvrdmemerr();
2205                return;
2206               }
2207             binread(tmpftvin,intsize,INT,(long)totverts,gmvin);
2208             for (i = 0; i < totverts; i++)
2209               {
2210                vertsin[i] = tmpftvin[i];
2211               }
2212             free(tmpftvin);
2213            }
2214         }
2215       ioerrtst(gmvin);
2216 
2217       gmv_data.nlongdata1 = lnfaces;
2218       gmv_data.longdata1 = nxvertsin;
2219       gmv_data.nlongdata2 = totverts;
2220       gmv_data.longdata2 = vertsin;
2221      }
2222 
2223    if (xfaceloc > 0 && xfaceloc < 5)
2224      {
2225 
2226       /*  Allocate and read face sized data.  */
2227       /*  If xfaceloc == 1, cell per face.    */
2228       /*  If xfaceloc == 2, opposite face.    */
2229       /*  If xfaceloc == 3, face pe.          */
2230       /*  If xfaceloc == 4, opposite face pe. */
2231 
2232       vertsin = (long *)malloc(lnfaces*sizeof(long));
2233       if (vertsin == NULL)
2234         {
2235          gmvrdmemerr();
2236          return;
2237         }
2238       if (ftype == ASCII) rdlongs(vertsin,(long)lnfaces,gmvin);
2239       else
2240         {
2241          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
2242            {
2243             binread(vertsin,longlongsize,LONGLONG,(long)lnfaces,gmvin);
2244            }
2245          else
2246            {
2247             tmpftvin = (int *)malloc(lnfaces*sizeof(int));
2248             if (tmpftvin == NULL)
2249               {
2250                gmvrdmemerr();
2251                return;
2252              }
2253             binread(tmpftvin,intsize,INT,(long)lnfaces,gmvin);
2254             for (i = 0; i < lnfaces; i++)
2255               {
2256                vertsin[i] = tmpftvin[i];
2257               }
2258             free(tmpftvin);
2259            }
2260         }
2261       ioerrtst(gmvin);
2262 
2263       /*  Get the number of cells from the largest cell  */
2264       /*  when reading the cells per face data.          */
2265       if (xfaceloc == 1)
2266         {
2267          j = 0;
2268          for (i = 0; i < lnfaces; i++)
2269            {
2270             if (j < vertsin[i]) j = vertsin[i];
2271            }
2272          numcells = lncells = j;
2273         }
2274 
2275       gmv_data.nlongdata1 = lnfaces;
2276       gmv_data.longdata1 = vertsin;
2277      }
2278 
2279    gmv_data.keyword = XFACES;
2280    gmv_data.datatype = REGULAR;
2281    gmv_data.num = lnfaces;
2282    gmv_data.num2 = xfaceloc;
2283    if (xfaceloc > 4)
2284      {
2285       readkeyword = 2;
2286       gmv_data.num = lnfaces;
2287       gmv_data.num2 = lncells;
2288       gmv_data.datatype = ENDKEYWORD;
2289      }
2290 
2291    xfaceloc++;
2292 }
2293 
2294 
readmats(FILE * gmvin,int ftype)2295 void readmats(FILE* gmvin, int ftype)
2296 {
2297   /*                               */
2298   /*  Read and set material data.  */
2299   /*                               */
2300   int i, data_type, *matin, lnmatin, lmmats;
2301   char mname[33], *matnames;
2302 
2303    /*  Read no. of materials and data type (cells or nodes).  */
2304    if (ftype != ASCII)
2305       binread(&lmmats,intsize,INT,(long)1,gmvin);
2306    else fscanf(gmvin,"%d",&lmmats);
2307    ioerrtst(gmvin);
2308 
2309    if (ftype != ASCII)
2310       binread(&i,intsize,INT,(long)1,gmvin);
2311    if (ftype == ASCII) fscanf(gmvin,"%d",&i);
2312    ioerrtst(gmvin);
2313    if (i == 0) data_type = CELL;
2314    if (i == 1) data_type = NODE;
2315 
2316    /*  Check for existence of data_type.  */
2317    if (data_type == CELL && numcells == 0)
2318      {
2319       fprintf(stderr,"Error, no cells exist for cell materials.\n");
2320       gmv_data.keyword = GMVERROR;
2321       return;
2322      }
2323    if (data_type == NODE && numnodes == 0)
2324      {
2325       fprintf(stderr,"Error, no nodes exist for node materials.\n");
2326       gmv_data.keyword = GMVERROR;
2327       return;
2328      }
2329 
2330    /*  Allocate and read 8 or 32 character material names.  */
2331    matnames = (char *)malloc(lmmats*33*sizeof(char));
2332    if (matnames == NULL)
2333      {
2334       gmvrdmemerr();
2335       return;
2336      }
2337    for (i = 0; i < lmmats; i++)
2338       {
2339       if (ftype != ASCII)
2340         {
2341          binread(mname, charsize_in*charsize, CHAR, (long)1, gmvin);
2342          ioerrtst(gmvin);
2343         }
2344       if (ftype == ASCII)
2345         {
2346          fscanf(gmvin,"%s",mname);
2347          ioerrtst(gmvin);
2348         }
2349       strncpy(&matnames[i*33],mname,32);
2350       *(matnames+i*33+charsize_in) = (char) 0;
2351      }
2352 
2353    /*  Allocate and read material nos.  */
2354    if (data_type == CELL) lnmatin = numcells;
2355    if (data_type == NODE) lnmatin = numnodes;
2356    matin=(int *)malloc(lnmatin*sizeof(int));
2357    if (matin == NULL)
2358      {
2359       gmvrdmemerr();
2360       return;
2361      }
2362 
2363    if (ftype != ASCII)
2364      {
2365       binread(matin,intsize,INT,(long)lnmatin,gmvin);
2366       ioerrtst(gmvin);
2367      }
2368    if (ftype == ASCII) rdints(matin,lnmatin,gmvin);
2369 
2370    gmv_data.keyword = MATERIAL;
2371    gmv_data.datatype = data_type;
2372    gmv_data.num = lmmats;
2373    gmv_data.nchardata1 = lmmats;
2374    gmv_data.chardata1 = matnames;
2375    gmv_data.nlongdata1 = lnmatin;
2376    gmv_data.longdata1 = (long *)malloc(lnmatin*sizeof(long));
2377    if (gmv_data.longdata1 == NULL)
2378      {
2379       gmvrdmemerr();
2380       return;
2381      }
2382    for (i = 0; i < lnmatin; i++)
2383       gmv_data.longdata1[i] = matin[i];
2384    free(matin);
2385 }
2386 
2387 
readvels(FILE * gmvin,int ftype)2388 void readvels(FILE* gmvin, int ftype)
2389 {
2390   /*                               */
2391   /*  Read and set velocity data.  */
2392   /*                               */
2393   int i, data_type, nvelin;
2394   double *uin, *vin, *win;
2395   float *tmpfloat;
2396 
2397    /*  Read data type (cells, nodes or faces).  */
2398    if (ftype != ASCII) binread(&i,intsize,INT,(long)1,gmvin);
2399    if (ftype == ASCII) fscanf(gmvin,"%d",&i);
2400    ioerrtst(gmvin);
2401    if (i == 0) data_type = CELL;
2402    if (i == 1) data_type = NODE;
2403    if (i == 2) data_type = FACE;
2404 
2405    /*  Check for existence of data_type.  */
2406    if (data_type == CELL && numcells == 0)
2407      {
2408       fprintf(stderr,"Error, no cells exist for cell velocities.\n");
2409       gmv_data.keyword = GMVERROR;
2410       return;
2411      }
2412    if (data_type == NODE && numnodes == 0)
2413      {
2414       fprintf(stderr,"Error, no nodes exist for node velocities.\n");
2415       gmv_data.keyword = GMVERROR;
2416       return;
2417      }
2418    if (data_type == FACE && numfaces == 0)
2419      {
2420       fprintf(stderr,"Error, no faces exist for node velocities.\n");
2421       gmv_data.keyword = GMVERROR;
2422       return;
2423      }
2424 
2425    /*  Allocate and read velocity data.  */
2426    if (data_type == CELL) nvelin = numcells;
2427    if (data_type == NODE) nvelin = numnodes;
2428    if (data_type == FACE) nvelin = numfaces;
2429 
2430    uin = (double *)malloc(nvelin*sizeof(double));
2431    vin = (double *)malloc(nvelin*sizeof(double));
2432    win = (double *)malloc(nvelin*sizeof(double));
2433    if (uin == NULL || vin == NULL || win == NULL)
2434      {
2435       gmvrdmemerr();
2436       return;
2437      }
2438 
2439    if (ftype != ASCII)
2440      {
2441       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
2442         {
2443          binread(uin,doublesize,DOUBLE,(long)nvelin,gmvin);
2444          ioerrtst(gmvin);
2445          binread(vin,doublesize,DOUBLE,(long)nvelin,gmvin);
2446          ioerrtst(gmvin);
2447          binread(win,doublesize,DOUBLE,(long)nvelin,gmvin);
2448          ioerrtst(gmvin);
2449         }
2450       else
2451         {
2452          tmpfloat = (float *)malloc(nvelin*sizeof(float));
2453          if (tmpfloat == NULL)
2454            {
2455             gmvrdmemerr();
2456             return;
2457            }
2458          binread(tmpfloat,floatsize,FLOAT,(long)nvelin,gmvin);
2459          ioerrtst(gmvin);
2460          for (i = 0; i < nvelin; i++) uin[i] = tmpfloat[i];
2461          binread(tmpfloat,floatsize,FLOAT,(long)nvelin,gmvin);
2462          ioerrtst(gmvin);
2463          for (i = 0; i < nvelin; i++) vin[i] = tmpfloat[i];
2464          binread(tmpfloat,floatsize,FLOAT,(long)nvelin,gmvin);
2465          ioerrtst(gmvin);
2466          for (i = 0; i < nvelin; i++) win[i] = tmpfloat[i];
2467          free(tmpfloat);
2468         }
2469      }
2470    if (ftype == ASCII)
2471      {
2472       rdfloats(uin,(long)nvelin,gmvin);
2473       rdfloats(vin,(long)nvelin,gmvin);
2474       rdfloats(win,(long)nvelin,gmvin);
2475      }
2476 
2477    gmv_data.keyword = VELOCITY;
2478    gmv_data.datatype = data_type;
2479    gmv_data.num = nvelin;
2480    gmv_data.ndoubledata1 = nvelin;
2481    gmv_data.doubledata1 = uin;
2482    gmv_data.ndoubledata2 = nvelin;
2483    gmv_data.doubledata2 = vin;
2484    gmv_data.ndoubledata3 = nvelin;
2485    gmv_data.doubledata3 = win;
2486 }
2487 
2488 
readvars(FILE * gmvin,int ftype)2489 void readvars(FILE* gmvin, int ftype)
2490 {
2491   /*                                     */
2492   /*  Read and set variable field data.  */
2493   /*                                     */
2494   int i, data_type, nvarin;
2495   double *varin;
2496   float *tmpfloat;
2497   char varname[33];
2498 
2499    /*  Read a variable name and data type (cells or nodes). */
2500    if (ftype != ASCII)
2501      {
2502       binread(varname,charsize,CHAR,(long)8,gmvin);
2503       if (strncmp(varname,"endvars",7) != 0 && charsize_in == 32)
2504         {
2505          fseek(gmvin,-8L,SEEK_CUR);
2506          binread(varname,charsize,CHAR,(long)charsize_in,gmvin);
2507         }
2508       *(varname+charsize_in)=(char)0;
2509       if (strncmp(varname,"endvars",7) != 0)
2510          binread(&i,intsize,INT,(long)1,gmvin);
2511      }
2512    if (ftype == ASCII)
2513      {
2514       fscanf(gmvin,"%s",varname);
2515       if (strncmp(varname,"endvars",7) != 0)
2516       fscanf(gmvin,"%d",&i);
2517      }
2518    ioerrtst(gmvin);
2519 
2520    /*  Check for endvars.  */
2521    if (strncmp(varname,"endvars",7) == 0)
2522      {
2523       readkeyword = 2;
2524       gmv_data.keyword = VARIABLE;
2525       gmv_data.datatype = ENDKEYWORD;
2526       return;
2527      }
2528 
2529    if (i == 0) data_type = CELL;
2530    if (i == 1) data_type = NODE;
2531    if (i == 2) data_type = FACE;
2532 
2533    /*  Check for existence of data_type.  */
2534    if (data_type == CELL && numcells == 0)
2535      {
2536       fprintf(stderr,"Error, no cells exist for cell variable %s.\n",varname);
2537       gmv_data.keyword = GMVERROR;
2538       return;
2539      }
2540    if (data_type == NODE && numnodes == 0)
2541      {
2542       fprintf(stderr,"Error, no nodes exist for node variable %s.\n",varname);
2543       gmv_data.keyword = GMVERROR;
2544       return;
2545      }
2546    if (data_type == FACE && numfaces == 0)
2547      {
2548       fprintf(stderr,"Error, no faces exist for face variable: %s.\n",varname);
2549       gmv_data.keyword = GMVERROR;
2550       return;
2551      }
2552 
2553    /*  Read one set of variable data.  */
2554    if (data_type == CELL) nvarin = numcells;
2555    if (data_type == NODE) nvarin = numnodes;
2556    if (data_type == FACE) nvarin = numfaces;
2557    varin = (double *)malloc(nvarin*sizeof(double));
2558    if (varin == NULL)
2559      {
2560       gmvrdmemerr();
2561       return;
2562      }
2563 
2564    if (ftype != ASCII)
2565      {
2566       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
2567         {
2568          binread(varin,doublesize,DOUBLE,(long)nvarin,gmvin);
2569          ioerrtst(gmvin);
2570         }
2571       else
2572         {
2573          tmpfloat = (float *)malloc(nvarin*sizeof(float));
2574          if (tmpfloat == NULL)
2575            {
2576             gmvrdmemerr();
2577             return;
2578            }
2579          binread(tmpfloat,floatsize,FLOAT,(long)nvarin,gmvin);
2580          ioerrtst(gmvin);
2581          for (i = 0; i < nvarin; i++) varin[i] = tmpfloat[i];
2582          free(tmpfloat);
2583         }
2584      }
2585    if (ftype == ASCII) rdfloats(varin,(long)nvarin,gmvin);
2586 
2587    gmv_data.keyword = VARIABLE;
2588    gmv_data.datatype = data_type;
2589    gmv_data.num = nvarin;
2590    strcpy(gmv_data.name1,varname);
2591    gmv_data.ndoubledata1 = nvarin;
2592    gmv_data.doubledata1 = varin;
2593 }
2594 
2595 
readflags(FILE * gmvin,int ftype)2596 void readflags(FILE* gmvin, int ftype)
2597 {
2598   /*                                     */
2599   /*  Read and set selection flag data.  */
2600   /*                                     */
2601   int i, data_type, ntypes, nflagin;
2602   int *flagin;
2603   char flgname[33], fname[33], *fnames;
2604 
2605    /*  Read flag name, no. flag types,  */
2606    /*  and data type (cells or nodes).  */
2607    if (ftype != ASCII)
2608      {
2609       binread(flgname,charsize,CHAR,(long)8,gmvin);
2610       if (strncmp(flgname,"endflag",7) != 0 && charsize_in == 32)
2611         {
2612          fseek(gmvin,-8L,SEEK_CUR);
2613          binread(flgname,charsize,CHAR,(long)charsize_in,gmvin);
2614         }
2615       *(flgname+charsize_in)=(char)0;
2616       if (strncmp(flgname,"endflag",7) != 0)
2617         {
2618          binread(&ntypes,intsize,INT,(long)1,gmvin);
2619          binread(&i,intsize,INT,(long)1,gmvin);
2620         }
2621      }
2622    if (ftype == ASCII)
2623      {
2624       fscanf(gmvin,"%s",flgname);
2625       if (strncmp(flgname,"endflag",7) != 0)
2626          fscanf(gmvin,"%d%d",&ntypes,&i);
2627      }
2628    ioerrtst(gmvin);
2629 
2630    /*  Check for endflag.  */
2631    if (strncmp(flgname,"endflag",7) == 0)
2632      {
2633       readkeyword = 2;
2634       gmv_data.keyword = FLAGS;
2635       gmv_data.datatype = ENDKEYWORD;
2636       return;
2637      }
2638 
2639    if (i == 0) data_type = CELL;
2640    if (i == 1) data_type = NODE;
2641 
2642    /*  Check for existence of data_type.  */
2643    if (data_type == CELL && numcells == 0)
2644      {
2645       fprintf(stderr,"Error, no cells exist for cell flags %s.\n",flgname);
2646       gmv_data.keyword = GMVERROR;
2647       return;
2648      }
2649    if (data_type == NODE && numnodes == 0)
2650      {
2651       fprintf(stderr,"Error, no nodes exist for node flags %s.\n",flgname);
2652       gmv_data.keyword = GMVERROR;
2653       return;
2654      }
2655 
2656    /*  Read one set of flag data.  */
2657    fnames = (char *)malloc(ntypes*33*sizeof(char));
2658    if (fnames == NULL)
2659      {
2660       gmvrdmemerr();
2661       return;
2662      }
2663 
2664    /*  Read 8 or 32 character flag names.  */
2665    for (i = 0; i < ntypes; i++)
2666      {
2667       if (ftype != ASCII)
2668         {
2669          binread(fname, charsize_in*charsize, CHAR, (long)1, gmvin);
2670          ioerrtst(gmvin);
2671          *(fname+charsize_in) = (char) 0;
2672         }
2673       if (ftype == ASCII)
2674         {
2675          fscanf(gmvin,"%s",fname);
2676          ioerrtst(gmvin);
2677          *(fname+charsize_in) = (char) 0;
2678         }
2679       strncpy(&fnames[i*33],fname,charsize_in);
2680       *(fnames+i*33+charsize_in) = (char) 0;
2681      }
2682 
2683    /*  Allocate and read flag data.  */
2684    if (data_type == CELL) nflagin = numcells;
2685    if (data_type == NODE) nflagin = numnodes;
2686    flagin = (int *)malloc(nflagin*sizeof(int));
2687    if (flagin == NULL)
2688      {
2689       gmvrdmemerr();
2690       return;
2691      }
2692 
2693    if (ftype != ASCII)
2694      {
2695       binread(flagin,intsize,INT,(long)nflagin,gmvin);
2696       ioerrtst(gmvin);
2697      }
2698    if (ftype == ASCII) rdints(flagin,nflagin,gmvin);
2699 
2700    gmv_data.keyword = FLAGS;
2701    gmv_data.datatype = data_type;
2702    strcpy(gmv_data.name1,flgname);
2703    gmv_data.num = nflagin;
2704    gmv_data.num2 = ntypes;
2705    gmv_data.nlongdata1 = nflagin;
2706    gmv_data.longdata1 = (long *)malloc(nflagin*sizeof(long));
2707    if (gmv_data.longdata1 == NULL)
2708      {
2709       gmvrdmemerr();
2710       return;
2711      }
2712    for (i = 0; i < nflagin; i++)
2713       gmv_data.longdata1[i] =  flagin[i];
2714    free(flagin);
2715    gmv_data.nchardata1 = ntypes;
2716    gmv_data.chardata1 = fnames;
2717 }
2718 
2719 
readpolygons(FILE * gmvin,int ftype)2720 void readpolygons(FILE* gmvin, int ftype)
2721 {
2722   /*                                             */
2723   /*  Read and set interface/boundary polygons.  */
2724   /*                                             */
2725   int i, limatno, nvertsin, junk;
2726   double *vertsin;
2727   float *tmpfloat;
2728   char varname[5], *tmpchar;
2729 
2730    /*  Read material no.  */
2731    if (ftype != ASCII)
2732      {
2733       binread(&wordbuf,intsize,WORD,(long)1,gmvin);
2734       tmpchar = (char *) &wordbuf;
2735       for (i = 0; i < 4; i++) varname[i] = tmpchar[i];
2736       varname[4] = '\0';
2737       limatno = word2int(wordbuf);
2738      }
2739    if (ftype == ASCII)
2740      {
2741       fscanf(gmvin,"%s",varname);
2742       sscanf(varname,"%d", &limatno);
2743      }
2744    ioerrtst(gmvin);
2745 
2746    /*  Check for endpoly.  */
2747    if (strncmp(varname,"end",3) == 0)
2748      {
2749 
2750       /*  Read second part of endpoly for bin data.  */
2751       if (ftype != ASCII) binread(&junk,intsize,INT,(long)1,gmvin);
2752       ioerrtst(gmvin);
2753 
2754       readkeyword = 2;
2755       gmv_data.keyword = POLYGONS;
2756       gmv_data.datatype = ENDKEYWORD;
2757       return;
2758      }
2759 
2760    /*  Read no. vertices. */
2761    if (ftype != ASCII) binread(&nvertsin,intsize,INT,(long)1,gmvin);
2762    if (ftype == ASCII) fscanf(gmvin,"%d",&nvertsin);
2763    ioerrtst(gmvin);
2764 
2765    /*  Read vertices and set data.  */
2766    vertsin = (double *)malloc(3*nvertsin*sizeof(double));
2767    if (vertsin == NULL)
2768      {
2769       gmvrdmemerr();
2770       return;
2771      }
2772    if (ftype != ASCII)
2773      {
2774       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
2775         {
2776          binread(vertsin,doublesize,DOUBLE,(long)3*nvertsin,gmvin);
2777          ioerrtst(gmvin);
2778         }
2779       else
2780         {
2781          tmpfloat = (float *)malloc(3*nvertsin*sizeof(float));
2782          if (tmpfloat == NULL)
2783            {
2784             gmvrdmemerr();
2785             return;
2786            }
2787          binread(tmpfloat,floatsize,FLOAT,(long)3*nvertsin,gmvin);
2788          ioerrtst(gmvin);
2789          for (i = 0; i < 3*nvertsin; i++) vertsin[i] = tmpfloat[i];
2790          free(tmpfloat);
2791         }
2792      }
2793    if (ftype == ASCII) rdfloats(vertsin,(long)3*nvertsin,gmvin);
2794 
2795    gmv_data.keyword = POLYGONS;
2796    gmv_data.datatype = REGULAR;
2797    gmv_data.num = limatno;
2798    gmv_data.ndoubledata1 = nvertsin;
2799    gmv_data.doubledata1 = (double *)malloc(nvertsin*sizeof(double));
2800    if (gmv_data.doubledata1 == NULL)
2801      {
2802       gmvrdmemerr();
2803       return;
2804      }
2805    for (i = 0; i < nvertsin; i++)
2806       gmv_data.doubledata1[i] = vertsin[i];
2807    gmv_data.ndoubledata2 = nvertsin;
2808    gmv_data.doubledata2 = (double *)malloc(nvertsin*sizeof(double));
2809    if (gmv_data.doubledata2 == NULL)
2810      {
2811       gmvrdmemerr();
2812       return;
2813      }
2814    for (i = 0; i < nvertsin; i++)
2815       gmv_data.doubledata2[i] = vertsin[nvertsin+i];
2816    gmv_data.ndoubledata3 = nvertsin;
2817    gmv_data.doubledata3 = (double *)malloc(nvertsin*sizeof(double));
2818    if (gmv_data.doubledata3 == NULL)
2819      {
2820       gmvrdmemerr();
2821       return;
2822      }
2823    for (i = 0; i < nvertsin; i++)
2824       gmv_data.doubledata3[i] = vertsin[2*nvertsin+i];
2825    free(vertsin);
2826 }
2827 
2828 
readtracers(FILE * gmvin,int ftype)2829 void readtracers(FILE* gmvin, int ftype)
2830 {
2831   /*                             */
2832   /*  Read and set tracer data.  */
2833   /*                             */
2834   char varname[33];
2835   int i;
2836   double *lxtr, *lytr, *lztr, *lfieldtr;
2837   float *tmpfloat;
2838 
2839    if (readkeyword == 1)
2840      {
2841 
2842       /*  Read no of tracers and x,y,z data.  */
2843       if (ftype != ASCII) binread(&numtracers,intsize,INT,(long)1,gmvin);
2844       if (ftype == ASCII) fscanf(gmvin,"%d",&numtracers);
2845       ioerrtst(gmvin);
2846 
2847       lxtr = NULL;  lytr = NULL;  lztr = NULL;
2848       if (numtracers > 0)
2849         {
2850 
2851          /*  Allocate and read tracer x,y,z's.  */
2852          lxtr = (double *)malloc(numtracers*sizeof(double));
2853          lytr = (double *)malloc(numtracers*sizeof(double));
2854          lztr = (double *)malloc(numtracers*sizeof(double));
2855          if (lxtr == NULL || lytr == NULL || lztr == NULL)
2856            {
2857             gmvrdmemerr();
2858             return;
2859            }
2860 
2861          if (ftype != ASCII)
2862            {
2863             if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
2864               {
2865                binread(lxtr,doublesize,DOUBLE,(long)numtracers,gmvin);
2866                ioerrtst(gmvin);
2867                binread(lytr,doublesize,DOUBLE,(long)numtracers,gmvin);
2868                ioerrtst(gmvin);
2869                binread(lztr,doublesize,DOUBLE,(long)numtracers,gmvin);
2870                ioerrtst(gmvin);
2871               }
2872             else
2873               {
2874                tmpfloat = (float *)malloc(numtracers*sizeof(float));
2875                if (tmpfloat == NULL)
2876                  {
2877                   gmvrdmemerr();
2878                   return;
2879                  }
2880                binread(tmpfloat,floatsize,FLOAT,(long)numtracers,gmvin);
2881                ioerrtst(gmvin);
2882                for (i = 0; i < numtracers; i++) lxtr[i] = tmpfloat[i];
2883                binread(tmpfloat,floatsize,FLOAT,(long)numtracers,gmvin);
2884                ioerrtst(gmvin);
2885                for (i = 0; i < numtracers; i++) lytr[i] = tmpfloat[i];
2886                binread(tmpfloat,floatsize,FLOAT,(long)numtracers,gmvin);
2887                ioerrtst(gmvin);
2888                for (i = 0; i < numtracers; i++) lztr[i] = tmpfloat[i];
2889                free(tmpfloat);
2890               }
2891            }
2892          if (ftype == ASCII)
2893            {
2894             rdfloats(lxtr,(long)numtracers,gmvin);
2895             rdfloats(lytr,(long)numtracers,gmvin);
2896             rdfloats(lztr,(long)numtracers,gmvin);
2897            }
2898         }
2899 
2900       gmv_data.keyword = TRACERS;
2901       gmv_data.datatype = XYZ;
2902       gmv_data.num = numtracers;
2903       gmv_data.ndoubledata1 = numtracers;
2904       gmv_data.doubledata1 = lxtr;
2905       gmv_data.ndoubledata2 = numtracers;
2906       gmv_data.doubledata2 = lytr;
2907       gmv_data.ndoubledata3 = numtracers;
2908       gmv_data.doubledata3 = lztr;
2909 
2910       readkeyword = 0;
2911       return;
2912      }
2913 
2914    /*  Read tracer field name and data.  */
2915    if (ftype != ASCII)
2916      {
2917       binread(varname,charsize,CHAR,(long)8,gmvin);
2918       if (strncmp(varname,"endtrace",8) != 0 && charsize_in == 32)
2919         {
2920          fseek(gmvin,-8L,SEEK_CUR);
2921          binread(varname,charsize,CHAR,(long)charsize_in,gmvin);
2922         }
2923       *(varname+charsize_in)=(char)0;
2924      }
2925    if (ftype == ASCII) fscanf(gmvin,"%s",varname);
2926    ioerrtst(gmvin);
2927 
2928    /*  Check for endtrace.  */
2929    if (strncmp(varname,"endtrace",8) == 0)
2930      {
2931       readkeyword = 2;
2932       gmv_data.keyword = TRACERS;
2933       gmv_data.datatype = ENDKEYWORD;
2934       gmv_data.num = numtracers;
2935       return;
2936      }
2937 
2938    lfieldtr = NULL;
2939    if (numtracers > 0)
2940      {
2941       lfieldtr=(double *)malloc(numtracers*sizeof(double));
2942       if (lfieldtr == NULL)
2943         {
2944          gmvrdmemerr();
2945          return;
2946         }
2947 
2948       if (ftype != ASCII)
2949         {
2950          if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
2951            {
2952             binread(lfieldtr,doublesize,DOUBLE,(long)numtracers,gmvin);
2953             ioerrtst(gmvin);
2954            }
2955          else
2956            {
2957             tmpfloat = (float *)malloc(numtracers*sizeof(float));
2958             if (tmpfloat == NULL)
2959               {
2960                gmvrdmemerr();
2961                return;
2962               }
2963             binread(tmpfloat,floatsize,FLOAT,(long)numtracers,gmvin);
2964             ioerrtst(gmvin);
2965             for (i = 0; i < numtracers; i++) lfieldtr[i] = tmpfloat[i];
2966             free(tmpfloat);
2967            }
2968         }
2969       if (ftype == ASCII) rdfloats(lfieldtr,(long)numtracers,gmvin);
2970      }
2971 
2972    gmv_data.keyword = TRACERS;
2973    gmv_data.datatype = TRACERDATA;
2974    strcpy(gmv_data.name1,varname);
2975    gmv_data.num = numtracers;
2976    gmv_data.ndoubledata1 = numtracers;
2977    gmv_data.doubledata1 = lfieldtr;
2978 }
2979 
2980 
readnodeids(FILE * gmvin,int ftype)2981 void readnodeids(FILE* gmvin, int ftype)
2982 {
2983   /*                                              */
2984   /*  Read and set alternate node numbers (ids).  */
2985   /*                                              */
2986   long *lnodeids = NULL;
2987   int *tmpids, i;
2988 
2989    /*  Allocate node ids.  */
2990    lnodeids=(long *)malloc(numnodes*sizeof(long));
2991    if (lnodeids == NULL)
2992      {
2993       gmvrdmemerr();
2994       return;
2995      }
2996 
2997    /*  Read node ids.  */
2998    if (ftype != ASCII)
2999      {
3000       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
3001         {
3002          binread(lnodeids,longlongsize,LONGLONG,numnodes,gmvin);
3003         }
3004       else
3005         {
3006          tmpids=(int *)malloc(numtracers*sizeof(int));
3007          if (tmpids == NULL)
3008            {
3009             gmvrdmemerr();
3010             return;
3011            }
3012          binread(tmpids,intsize,INT,numnodes,gmvin);
3013          for (i = 0; i < numnodes; i++)
3014             lnodeids[i] = tmpids[i];
3015          free(tmpids);
3016         }
3017       ioerrtst(gmvin);
3018      }
3019    if (ftype == ASCII) rdlongs(lnodeids,numnodes,gmvin);
3020 
3021    gmv_data.keyword = NODEIDS;
3022    gmv_data.datatype = REGULAR;
3023    gmv_data.num = numnodes;
3024    gmv_data.nlongdata1 = numnodes;
3025    gmv_data.longdata1 = lnodeids;
3026 }
3027 
3028 
readcellids(FILE * gmvin,int ftype)3029 void readcellids(FILE* gmvin, int ftype)
3030 {
3031   /*                                              */
3032   /*  Read and set alternate cell numbers (ids).  */
3033   /*                                              */
3034   long *lcellids = NULL;
3035   int *tmpids, i;
3036 
3037    /*  Allocate cell ids.  */
3038    lcellids=(long *)malloc(numcells*sizeof(long));
3039    if (lcellids == NULL)
3040      {
3041       gmvrdmemerr();
3042       return;
3043      }
3044 
3045    /*  Read cell ids.  */
3046    if (ftype != ASCII)
3047      {
3048       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
3049         {
3050          binread(lcellids,longlongsize,LONGLONG,numcells,gmvin);
3051         }
3052       else
3053         {
3054          tmpids=(int *)malloc(numcells*sizeof(int));
3055          if (tmpids == NULL)
3056            {
3057             gmvrdmemerr();
3058             return;
3059            }
3060          binread(tmpids,intsize,INT,numcells,gmvin);
3061          for (i = 0; i < numcells; i++)
3062             lcellids[i] = tmpids[i];
3063          free(tmpids);
3064         }
3065       ioerrtst(gmvin);
3066      }
3067    if (ftype == ASCII) rdlongs(lcellids,numcells,gmvin);
3068 
3069    gmv_data.keyword = CELLIDS;
3070    gmv_data.datatype = REGULAR;
3071    gmv_data.num = numcells;
3072    gmv_data.nlongdata1 = numcells;
3073    gmv_data.longdata1 = lcellids;
3074 }
3075 
3076 
readfaceids(FILE * gmvin,int ftype)3077 void readfaceids(FILE* gmvin, int ftype)
3078 {
3079   /*                                              */
3080   /*  Read and set alternate face numbers (ids).  */
3081   /*                                              */
3082   long *lfaceids = NULL;
3083   int *tmpids, i;
3084 
3085    /*  Check that faces have been read.  */
3086    if (numfaces == 0)
3087      {
3088       fprintf(stderr,"Error, no faces exist for faceids.\n");
3089       gmv_data.keyword = GMVERROR;
3090       return;
3091      }
3092 
3093    /*  Allocate face ids.  */
3094    lfaceids=(long *)malloc(numfaces*sizeof(long));
3095    if (lfaceids == NULL)
3096      {
3097       gmvrdmemerr();
3098       return;
3099      }
3100 
3101    /*  Read face ids.  */
3102    if (ftype != ASCII)
3103      {
3104       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
3105         {
3106          binread(lfaceids,longlongsize,LONGLONG,numcells,gmvin);
3107         }
3108       else
3109         {
3110          tmpids=(int *)malloc(numfaces*sizeof(int));
3111          if (tmpids == NULL)
3112            {
3113             gmvrdmemerr();
3114             return;
3115            }
3116          binread(tmpids,intsize,INT,numfaces,gmvin);
3117          for (i = 0; i < numfaces; i++)
3118             lfaceids[i] = tmpids[i];
3119          free(tmpids);
3120         }
3121       ioerrtst(gmvin);
3122      }
3123    if (ftype == ASCII) rdlongs(lfaceids,numfaces,gmvin);
3124 
3125    gmv_data.keyword = FACEIDS;
3126    gmv_data.datatype = REGULAR;
3127    gmv_data.num = numcells;
3128    gmv_data.nlongdata1 = numfaces;
3129    gmv_data.longdata1 = lfaceids;
3130 }
3131 
3132 
readtracerids(FILE * gmvin,int ftype)3133 void readtracerids(FILE* gmvin, int ftype)
3134 {
3135   /*                                                */
3136   /*  Read and set alternate tracer numbers (ids).  */
3137   /*                                                */
3138   long *ltracerids = NULL;
3139   int *tmpids, i;
3140 
3141    /*  Allocate tracer ids.  */
3142    if (numtracers > 0)
3143      {
3144       ltracerids=(long *)malloc(numtracers*sizeof(long));
3145       if (ltracerids == NULL)
3146         {
3147          gmvrdmemerr();
3148          return;
3149         }
3150 
3151       /*  Read tracer ids.  */
3152       if (ftype != ASCII)
3153         {
3154          if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
3155            {
3156             binread(ltracerids,longlongsize,LONGLONG,numtracers,gmvin);
3157            }
3158          else
3159            {
3160             tmpids=(int *)malloc(numtracers*sizeof(int));
3161             if (tmpids == NULL)
3162               {
3163                gmvrdmemerr();
3164                return;
3165               }
3166             binread(tmpids,intsize,INT,numtracers,gmvin);
3167             for (i = 0; i < numtracers; i++)
3168                ltracerids[i] = tmpids[i];
3169             free(tmpids);
3170            }
3171          ioerrtst(gmvin);
3172         }
3173       if (ftype == ASCII) rdlongs(ltracerids,(long)numtracers,gmvin);
3174      }
3175 
3176    gmv_data.keyword = TRACEIDS;
3177    gmv_data.datatype = REGULAR;
3178    gmv_data.num = numtracers;
3179    gmv_data.nlongdata1 = numtracers;
3180    gmv_data.longdata1 = ltracerids;
3181 }
3182 
3183 
readunits(FILE * gmvin,int ftype)3184 void readunits(FILE* gmvin, int ftype)
3185 {
3186   /*                                   */
3187   /*  Read and set units information.  */
3188   /*                                   */
3189   int i;
3190   char unittype[9], fldname[9], unitname[17];
3191   char *fldstr, *unitstr;
3192 
3193    /*  Read unit type (xyz, velocity, nodes, cells).  */
3194    if (ftype != ASCII)
3195      {
3196       binread(unittype,charsize,CHAR,(long)8,gmvin);
3197       *(unittype+8)=(char)0;
3198      }
3199    if (ftype == ASCII)
3200      {
3201       fscanf(gmvin,"%s",unittype);
3202      }
3203    ioerrtst(gmvin);
3204 
3205    /*  Check for endunit.  */
3206    if (strncmp(unittype,"endunit",7) == 0)
3207      {
3208       readkeyword = 2;
3209       gmv_data.keyword = UNITS;
3210       gmv_data.datatype = ENDKEYWORD;
3211       return;
3212      }
3213 
3214    numunits = 0;
3215    gmv_data.keyword = UNITS;
3216 
3217    /*  If xyz or velocity type, read units.  */
3218    if (strncmp(unittype,"xyz",3) == 0 ||
3219        strncmp(unittype,"velocity",8) == 0)
3220      {
3221       if (ftype != ASCII)
3222         {
3223          binread(unitname, 16*charsize, CHAR, (long)1, gmvin);
3224          ioerrtst(gmvin);
3225          *(unitname+16) = (char) 0;
3226         }
3227       if (ftype == ASCII)
3228         {
3229          fscanf(gmvin,"%s",unitname);
3230          ioerrtst(gmvin);
3231          *(unitname+16) = (char) 0;
3232         }
3233       if (strncmp(unittype,"xyz",3) == 0)
3234          gmv_data.datatype = XYZ;
3235       else gmv_data.datatype = VEL;
3236       gmv_data.nchardata1 = 1;
3237       gmv_data.chardata1 = (char *)malloc(20*sizeof(char));
3238       if (gmv_data.chardata1 == NULL)
3239         {
3240          gmvrdmemerr();
3241          return;
3242         }
3243       strcpy(gmv_data.chardata1,unittype);
3244       gmv_data.nchardata2 = 1;
3245       gmv_data.chardata2 = (char *)malloc(20*sizeof(char));
3246       if (gmv_data.chardata2 == NULL)
3247         {
3248          gmvrdmemerr();
3249          return;
3250         }
3251       strcpy(gmv_data.chardata2,unitname);
3252       return;
3253      }
3254 
3255    /*  If nodes or cells type, read field units.  */
3256    if (strncmp(unittype,"cells",5) == 0 ||
3257        strncmp(unittype,"nodes",5) == 0 ||
3258        strncmp(unittype,"faces",5) == 0)
3259      {
3260 
3261       /*  Read no. of field units to read.  */
3262       if (ftype != ASCII)
3263         {
3264 	 binread((void*)(&numunits),intsize,INT,(long)1,gmvin);
3265          ioerrtst(gmvin);
3266         }
3267       if (ftype == ASCII)
3268         {
3269          fscanf(gmvin,"%d",&numunits);
3270          ioerrtst(gmvin);
3271         }
3272 
3273       /*  Allocate two character stings.  */
3274       fldstr = (char *)malloc(numunits*33*sizeof(char));
3275       unitstr = (char *)malloc(numunits*33*sizeof(char));
3276       if (fldstr == NULL || unitstr == NULL)
3277         {
3278          gmvrdmemerr();
3279          return;
3280         }
3281 
3282       for (i = 0; i < numunits; i++)
3283         {
3284          if (ftype != ASCII)
3285            {
3286             binread(fldname, 8*charsize, CHAR, (long)1, gmvin);
3287             ioerrtst(gmvin);
3288             *(fldname+8) = (char) 0;
3289             binread(unitname, 16*charsize, CHAR, (long)1, gmvin);
3290             ioerrtst(gmvin);
3291             *(unitname+16) = (char) 0;
3292            }
3293          if (ftype == ASCII)
3294            {
3295             fscanf(gmvin,"%s",fldname);
3296             ioerrtst(gmvin);
3297             *(fldname+8) = (char) 0;
3298             fscanf(gmvin,"%s",unitname);
3299             ioerrtst(gmvin);
3300             *(unitname+16) = (char) 0;
3301            }
3302          strcpy(&fldstr[i*33],fldname);
3303          strcpy(&unitstr[i*33],unitname);
3304         }
3305 
3306       if (strncmp(unittype,"nodes",5) == 0)
3307          gmv_data.datatype = NODE;
3308       if (strncmp(unittype,"nodes",5) == 0)
3309          gmv_data.datatype = CELL;
3310       if (strncmp(unittype,"faces",5) == 0)
3311          gmv_data.datatype = FACE;
3312       gmv_data.num = numunits;
3313       gmv_data.nchardata1 = numunits;
3314       gmv_data.chardata1 = fldstr;
3315       gmv_data.nchardata2 = numunits;
3316       gmv_data.chardata2 = unitstr;
3317      }
3318 }
3319 
3320 
readsurface(FILE * gmvin,int ftype)3321 void readsurface(FILE* gmvin, int ftype)
3322 {
3323   int i, nverts, *tmpverts;
3324   long *vertsin;
3325 
3326    if (readkeyword == 1)
3327      {
3328 
3329       /*  Read no. of surface facets to read.  */
3330       if (ftype == ASCII) fscanf(gmvin,"%d",&lnsurf);
3331       else
3332          binread(&lnsurf,intsize,INT,(long)1,gmvin);
3333       ioerrtst(gmvin);
3334       numsurfin = 0;
3335 
3336       if (!skipflag)
3337         {
3338          numsurf = lnsurf;
3339          surface_read = 1;
3340         }
3341      }
3342 
3343    /*  Check no. of surfaces read.  */
3344    numsurfin++;
3345    if (numsurfin > lnsurf)
3346      {
3347       readkeyword = 2;
3348       gmv_data.keyword = SURFACE;
3349       gmv_data.datatype = ENDKEYWORD;
3350       gmv_data.num = numsurf;
3351       if (numsurf == 0) readkeyword = 1;
3352       return;
3353      }
3354 
3355    /*  Read surface facet vertices.  */
3356    if (ftype != ASCII)
3357      {
3358       binread(&nverts,intsize,INT,(long)1,gmvin);
3359      }
3360    if (ftype == ASCII) fscanf(gmvin,"%d",&nverts);
3361    ioerrtst(gmvin);
3362 
3363    /*  Read all face vertices.  */
3364    vertsin = (long *)malloc(nverts*sizeof(long));
3365    if (vertsin == NULL)
3366      {
3367       gmvrdmemerr();
3368       return;
3369      }
3370    if (ftype != ASCII)
3371      {
3372       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
3373         {
3374          binread(vertsin,longlongsize,LONGLONG,(long)nverts,gmvin);
3375         }
3376       else
3377         {
3378          tmpverts = (int *)malloc(nverts*sizeof(int));
3379          if (tmpverts == NULL)
3380            {
3381             gmvrdmemerr();
3382             return;
3383            }
3384          binread(tmpverts,intsize,INT,(long)nverts,gmvin);
3385          for (i = 0; i < nverts; i++)
3386             vertsin[i] = tmpverts[i];
3387          free(tmpverts);
3388         }
3389       ioerrtst(gmvin);
3390      }
3391    if (ftype == ASCII) rdlongs(vertsin,(long)nverts,gmvin);
3392 
3393    if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
3394      {
3395       fprintf(stderr,"I/O error while reading surfaces.\n");
3396       gmv_data.keyword = GMVERROR;
3397       return;
3398      }
3399 
3400    gmv_data.keyword = SURFACE;
3401    gmv_data.datatype = REGULAR;
3402    gmv_data.num = lnsurf;
3403    gmv_data.nlongdata1 = nverts;
3404    gmv_data.longdata1 = vertsin;
3405 }
3406 
3407 
readsurfmats(FILE * gmvin,int ftype)3408 void readsurfmats(FILE* gmvin, int ftype)
3409 {
3410   /*                                       */
3411   /*  Read and set surface material data.  */
3412   /*                                       */
3413   int i, *matin;
3414 
3415    /*  Check that surfaces have been input.  */
3416    if (surface_read == 0)
3417      {
3418       fprintf(stderr,
3419               "Error, surface must be read before surfmats.\n");
3420       gmv_data.keyword = GMVERROR;
3421       return;
3422      }
3423 
3424    /*  Check for numsurf == 0.  */
3425    if (numsurf == 0) return;
3426 
3427    /*  Allocate and read surface material nos.  */
3428    matin=(int *)malloc((numsurf)*sizeof(int));
3429    if (matin == NULL)
3430      {
3431       gmvrdmemerr();
3432       return;
3433      }
3434 
3435    if (ftype != ASCII)
3436      {
3437       binread(matin,intsize,INT,(long)numsurf,gmvin);
3438       ioerrtst(gmvin);
3439      }
3440    if (ftype == ASCII) rdints(matin,numsurf,gmvin);
3441 
3442    gmv_data.keyword = SURFMATS;
3443    gmv_data.num = numsurf;
3444    gmv_data.nlongdata1 = numsurf;
3445    gmv_data.longdata1 = (long *)malloc(numsurf*sizeof(long));
3446    if (gmv_data.longdata1 == NULL)
3447      {
3448       gmvrdmemerr();
3449       return;
3450      }
3451    for (i = 0; i < numsurf; i++)
3452       gmv_data.longdata1[i] = matin[i];
3453    free(matin);
3454 }
3455 
3456 
readsurfvel(FILE * gmvin,int ftype)3457 void readsurfvel(FILE* gmvin, int ftype)
3458 {
3459   /*                                       */
3460   /*  Read and set surface velocity data.  */
3461   /*                                       */
3462   int i;
3463   double *uin, *vin, *win;
3464   float *tmpfloat;
3465 
3466    /*  Check that surfaces have been input.  */
3467    if (surface_read == 0)
3468      {
3469       fprintf(stderr,
3470               "Error, surface must be read before surfvel.\n");
3471       gmv_data.keyword = GMVERROR;
3472       return;
3473      }
3474 
3475    /*  Check for numsurf == 0.  */
3476    if (numsurf == 0)
3477      {
3478       gmv_data.keyword = SURFVEL;
3479       return;
3480      }
3481 
3482    /*  Allocate and read velocity data.  */
3483    uin = (double *)malloc(numsurf*sizeof(double));
3484    vin = (double *)malloc(numsurf*sizeof(double));
3485    win = (double *)malloc(numsurf*sizeof(double));
3486    if (uin == NULL || vin == NULL || win == NULL)
3487      {
3488       gmvrdmemerr();
3489       return;
3490      }
3491 
3492    if (ftype != ASCII)
3493      {
3494       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
3495         {
3496          binread(uin,doublesize,DOUBLE,(long)numsurf,gmvin);
3497          ioerrtst(gmvin);
3498          binread(vin,doublesize,DOUBLE,(long)numsurf,gmvin);
3499          ioerrtst(gmvin);
3500          binread(win,doublesize,DOUBLE,(long)numsurf,gmvin);
3501          ioerrtst(gmvin);
3502         }
3503       else
3504         {
3505          tmpfloat = (float *)malloc(numsurf*sizeof(float));
3506          if (tmpfloat == NULL)
3507            {
3508             gmvrdmemerr();
3509             return;
3510            }
3511          binread(tmpfloat,floatsize,FLOAT,(long)numsurf,gmvin);
3512          ioerrtst(gmvin);
3513          for (i = 0; i < numsurf; i++) uin[i] = tmpfloat[i];
3514          binread(tmpfloat,floatsize,FLOAT,(long)numsurf,gmvin);
3515          ioerrtst(gmvin);
3516          for (i = 0; i < numsurf; i++) vin[i] = tmpfloat[i];
3517          binread(tmpfloat,floatsize,FLOAT,(long)numsurf,gmvin);
3518          ioerrtst(gmvin);
3519          for (i = 0; i < numsurf; i++) win[i] = tmpfloat[i];
3520          free(tmpfloat);
3521         }
3522      }
3523    if (ftype == ASCII)
3524      {
3525       rdfloats(uin,(long)numsurf,gmvin);
3526       rdfloats(vin,(long)numsurf,gmvin);
3527       rdfloats(win,(long)numsurf,gmvin);
3528      }
3529 
3530    gmv_data.keyword = SURFVEL;
3531    gmv_data.num = numsurf;
3532    gmv_data.ndoubledata1 = numsurf;
3533    gmv_data.doubledata1 = uin;
3534    gmv_data.ndoubledata2 = numsurf;
3535    gmv_data.doubledata2 = vin;
3536    gmv_data.ndoubledata3 = numsurf;
3537    gmv_data.doubledata3 = win;
3538 }
3539 
3540 
readsurfvars(FILE * gmvin,int ftype)3541 void readsurfvars(FILE* gmvin, int ftype)
3542 {
3543   /*                                             */
3544   /*  Read and set surface variable field data.  */
3545   /*                                             */
3546   int i;
3547   double *varin;
3548   float *tmpfloat;
3549   char varname[33];
3550 
3551    /*  Check that surfaces have been input.  */
3552    if (surface_read == 0)
3553      {
3554       fprintf(stderr,
3555               "Error, surface must be read before surfvars.\n");
3556       gmv_data.keyword = GMVERROR;
3557       return;
3558      }
3559 
3560    /*  Read variable name. */
3561    if (ftype != ASCII)
3562      {
3563       binread(varname,charsize,CHAR,(long)8,gmvin);
3564       if (strncmp(varname,"endsvar",7) != 0 && charsize_in == 32)
3565         {
3566          fseek(gmvin,-8L,SEEK_CUR);
3567          binread(varname,charsize,CHAR,(long)charsize_in,gmvin);
3568         }
3569       *(varname+charsize_in)=(char)0;
3570      }
3571    if (ftype == ASCII) fscanf(gmvin,"%s",varname);
3572    ioerrtst(gmvin);
3573 
3574    /*  Check for endsvar.  */
3575    if (strncmp(varname,"endsvar",7) == 0)
3576      {
3577       readkeyword = 2;
3578       gmv_data.keyword = SURFVARS;
3579       gmv_data.datatype = ENDKEYWORD;
3580       return;
3581      }
3582 
3583    /*  Read variable data.  */
3584    varin = NULL;
3585    if (numsurf > 0)
3586      {
3587       varin = (double *)malloc(numsurf*sizeof(double));
3588       if (varin == NULL)
3589         {
3590          gmvrdmemerr();
3591          return;
3592         }
3593       if (ftype != ASCII)
3594         {
3595          if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
3596            {
3597             binread(varin,doublesize,DOUBLE,(long)numsurf,gmvin);
3598             ioerrtst(gmvin);
3599            }
3600          else
3601            {
3602             tmpfloat = (float *)malloc(numsurf*sizeof(float));
3603             if (tmpfloat == NULL)
3604               {
3605                gmvrdmemerr();
3606                return;
3607               }
3608             binread(tmpfloat,floatsize,FLOAT,(long)numsurf,gmvin);
3609             ioerrtst(gmvin);
3610             for (i = 0; i < numsurf; i++) varin[i] = tmpfloat[i];
3611              free(tmpfloat);
3612            }
3613         }
3614       if (ftype == ASCII) rdfloats(varin,(long)numsurf,gmvin);
3615      }
3616 
3617    gmv_data.keyword = SURFVARS;
3618    gmv_data.datatype = REGULAR;
3619    strcpy(gmv_data.name1,varname);
3620    gmv_data.num = numsurf;
3621    gmv_data.ndoubledata1 = numsurf;
3622    gmv_data.doubledata1 = varin;
3623 }
3624 
3625 
readsurfflag(FILE * gmvin,int ftype)3626 void readsurfflag(FILE* gmvin, int ftype)
3627 {
3628   /*                                     */
3629   /*  Read and set selection flag data.  */
3630   /*                                     */
3631   int i, ntypes;
3632   int *flagin;
3633   char flgname[33], fname[33], *fnames;
3634 
3635    /*  Check that surfaces have been input.  */
3636    if (surface_read == 0)
3637      {
3638       fprintf(stderr,
3639               "Error, surface must be read before surfflag.\n");
3640       gmv_data.keyword = GMVERROR;
3641       return;
3642      }
3643 
3644    /*  Read flag name.  */
3645    if (ftype != ASCII)
3646      {
3647       binread(&flgname,charsize,CHAR,(long)charsize_in,gmvin);
3648       *(flgname+charsize_in)='\0';
3649      }
3650    if (ftype == ASCII)
3651      {
3652       fscanf(gmvin,"%s",flgname);
3653      }
3654    ioerrtst(gmvin);
3655 
3656    /*  Check for endsflag.  */
3657    if (strncmp(flgname,"endsflag",8) == 0)
3658      {
3659       readkeyword = 2;
3660       gmv_data.keyword = SURFFLAG;
3661       gmv_data.datatype = ENDKEYWORD;
3662       return;
3663      }
3664 
3665    /*  Read no. of flag types.  */
3666    if (ftype != ASCII)
3667      {
3668       binread(&ntypes,intsize,INT,(long)1,gmvin);
3669      }
3670    if (ftype == ASCII)
3671      {
3672       fscanf(gmvin,"%d",&ntypes);
3673      }
3674    ioerrtst(gmvin);
3675 
3676    /*  Read one set of surface flag data.  */
3677    fnames = (char *)malloc(ntypes*33*sizeof(char));
3678    if (fnames == NULL)
3679      {
3680       gmvrdmemerr();
3681       return;
3682      }
3683    flagin = NULL;
3684    if (numsurf > 0)
3685      {
3686       flagin = (int *)malloc(numsurf*sizeof(int));
3687       if (flagin == NULL)
3688         {
3689          gmvrdmemerr();
3690          return;
3691         }
3692      }
3693 
3694    /*  Read 8 character flag names.  */
3695    for (i = 0; i < ntypes; i++)
3696      {
3697       if (ftype != ASCII)
3698         {
3699          binread(fname, charsize_in*charsize, CHAR, (long)1, gmvin);
3700          ioerrtst(gmvin);
3701          *(fname+charsize_in) = (char) 0;
3702         }
3703       if (ftype == ASCII)
3704         {
3705          fscanf(gmvin,"%s",fname);
3706          ioerrtst(gmvin);
3707          *(fname+charsize_in) = (char) 0;
3708         }
3709       strncpy(&fnames[i*33],fname,charsize_in);
3710       *(fnames+i*33+charsize_in) = (char) 0;
3711      }
3712 
3713    /*  Allocate and read flag data.  */
3714    if (numsurf > 0)
3715      {
3716       if (ftype != ASCII)
3717         {
3718          binread(flagin,intsize,INT,(long)numsurf,gmvin);
3719          ioerrtst(gmvin);
3720         }
3721       if (ftype == ASCII) rdints(flagin,numsurf,gmvin);
3722      }
3723 
3724    gmv_data.keyword = SURFFLAG;
3725    gmv_data.datatype = REGULAR;
3726    strcpy(gmv_data.name1,flgname);
3727    gmv_data.num = numsurf;
3728    gmv_data.num2 = ntypes;
3729    gmv_data.nlongdata1 = numsurf;
3730    if (numsurf > 0)
3731      {
3732       gmv_data.longdata1 = (long *)malloc(numsurf*sizeof(long));
3733       if (gmv_data.longdata1 == NULL)
3734         {
3735          gmvrdmemerr();
3736          return;
3737         }
3738       for (i = 0; i < numsurf; i++)
3739          gmv_data.longdata1[i] = flagin[i];
3740       free(flagin);
3741      }
3742    gmv_data.nchardata1 = ntypes;
3743    gmv_data.chardata1 = fnames;
3744 }
3745 
3746 
readsurfids(FILE * gmvin,int ftype)3747 void readsurfids(FILE* gmvin, int ftype)
3748 {
3749   /*                                                 */
3750   /*  Read and set alternate surface numbers (ids).  */
3751   /*                                                 */
3752   long *lsurfids = NULL;
3753   int *tmpids, i;
3754 
3755    /*  Check that surfaces have been input.  */
3756    if (surface_read == 0)
3757      {
3758       fprintf(stderr,
3759               "Error, surface must be read before surids.\n");
3760       gmv_data.keyword = GMVERROR;
3761       return;
3762      }
3763 
3764    /*  Check for numsurf == 0.  */
3765    if (numsurf == 0) return;
3766 
3767    /*  Allocate surf ids.  */
3768    lsurfids=(long *)malloc(numsurf*sizeof(long));
3769    if (lsurfids == NULL)
3770      {
3771       gmvrdmemerr();
3772       return;
3773      }
3774 
3775    /*  Read surf ids.  */
3776    if (ftype != ASCII)
3777      {
3778       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
3779         {
3780          binread(lsurfids,longlongsize,LONGLONG,numsurf,gmvin);
3781         }
3782       else
3783         {
3784          tmpids=(int *)malloc(numsurf*sizeof(int));
3785          if (tmpids == NULL)
3786            {
3787             gmvrdmemerr();
3788             return;
3789            }
3790          binread(tmpids,intsize,INT,numsurf,gmvin);
3791          for (i = 0; i < numsurf; i++)
3792             lsurfids[i] = tmpids[i];
3793          free(tmpids);
3794         }
3795       ioerrtst(gmvin);
3796      }
3797    if (ftype == ASCII) rdlongs(lsurfids,numsurf,gmvin);
3798 
3799    gmv_data.keyword = SURFIDS;
3800    gmv_data.datatype = REGULAR;
3801    gmv_data.num = numsurf;
3802    gmv_data.nlongdata1 = numsurf;
3803    gmv_data.longdata1 = lsurfids;
3804 }
3805 
3806 
readvinfo(FILE * gmvin,int ftype)3807 void readvinfo(FILE* gmvin, int ftype)
3808 {
3809   /*                               */
3810   /*  Read one set of vinfo data.  */
3811   /*                               */
3812   int i, nelem_line, nlines, nvarin;
3813   double *varin;
3814   float *tmpfloat;
3815   char varname[33];
3816 
3817    /*  Read a variable name, no. of elements per line, and no. of lines. */
3818    if (ftype != ASCII)
3819      {
3820       binread(varname,charsize,CHAR,(long)8,gmvin);
3821       if (strncmp(varname,"endvinfo",8) != 0 && charsize_in == 32)
3822         {
3823          fseek(gmvin,-8L,SEEK_CUR);
3824          binread(varname,charsize,CHAR,(long)charsize_in,gmvin);
3825         }
3826       *(varname+charsize_in)=(char)0;
3827       if (strncmp(varname,"endvinfo",8) != 0)
3828         {
3829          binread(&nelem_line,intsize,INT,(long)1,gmvin);
3830          binread(&nlines,intsize,INT,(long)1,gmvin);
3831         }
3832      }
3833    if (ftype == ASCII)
3834      {
3835       fscanf(gmvin,"%s",varname);
3836       if (strncmp(varname,"endvinfo",8) != 0)
3837       fscanf(gmvin,"%d%d",&nelem_line,&nlines);
3838      }
3839    ioerrtst(gmvin);
3840 
3841    /*  Check for endvinfo.  */
3842    if (strncmp(varname,"endvinfo",8) == 0)
3843      {
3844       readkeyword = 2;
3845       gmv_data.keyword = VINFO;
3846       gmv_data.datatype = ENDKEYWORD;
3847       return;
3848      }
3849 
3850    /*  Read one set of vinfo data.  */
3851    nvarin = nelem_line * nlines;
3852    varin = (double *)malloc(nvarin*sizeof(double));
3853    if (varin == NULL)
3854      {
3855       gmvrdmemerr();
3856       return;
3857      }
3858 
3859    if (ftype != ASCII)
3860      {
3861       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
3862         {
3863          binread(varin,doublesize,DOUBLE,(long)nvarin,gmvin);
3864          ioerrtst(gmvin);
3865         }
3866       else
3867         {
3868          tmpfloat = (float *)malloc(nvarin*sizeof(float));
3869          if (tmpfloat == NULL)
3870            {
3871             gmvrdmemerr();
3872             return;
3873            }
3874          binread(tmpfloat,floatsize,FLOAT,(long)nvarin,gmvin);
3875          ioerrtst(gmvin);
3876          for (i = 0; i < nvarin; i++) varin[i] = tmpfloat[i];
3877          free(tmpfloat);
3878         }
3879      }
3880    if (ftype == ASCII) rdfloats(varin,(long)nvarin,gmvin);
3881 
3882    if (ftype == IEEEI4R4 || ftype == IEEEI8R4)
3883       free(tmpfloat);
3884 
3885    gmv_data.keyword = VINFO;
3886    gmv_data.datatype = REGULAR;
3887    gmv_data.num = nelem_line;
3888    gmv_data.num2 = nlines;
3889    strcpy(gmv_data.name1,varname);
3890    gmv_data.ndoubledata1 = nvarin;
3891    gmv_data.doubledata1 = varin;
3892 }
3893 
3894 
readcomments(FILE * gmvin,int ftype)3895 void readcomments(FILE* gmvin, int ftype)
3896 {
3897   /*                                   */
3898   /*  Read through (ignore) comments.  */
3899   /*                                   */
3900   int rdcomms;
3901   char varname[100];
3902 
3903    /*  Read comments until endcomm found.  */
3904    rdcomms = 1;
3905    while (rdcomms)
3906      {
3907       fscanf(gmvin,"%s",varname);
3908       ioerrtst(gmvin);
3909       if (strncmp(varname,"endcomm",7) == 0)
3910          rdcomms = 0;
3911      }
3912 
3913    /*  If binary file, read space after endcomm.  */
3914    if (ftype != ASCII) binread(varname,charsize,CHAR,(long)1,gmvin);
3915 }
3916 
3917 
readgroups(FILE * gmvin,int ftype)3918 void readgroups(FILE* gmvin, int ftype)
3919 {
3920   /*                            */
3921   /*  Read and set group data.  */
3922   /*                            */
3923   int i, data_type, ngroupin;
3924   int *groupin;
3925   char grpname[40];
3926 
3927    /*  Read group name, data type (cells, nodes, faces, or  */
3928    /*  surfaces) and the number of elements in the group.   */
3929    if (ftype != ASCII)
3930      {
3931       binread(grpname,charsize,CHAR,(long)8,gmvin);
3932       if (strncmp(grpname,"endgrp",6) != 0 && charsize_in == 32)
3933         {
3934          fseek(gmvin,-8L,SEEK_CUR);
3935          binread(grpname,charsize,CHAR,(long)charsize_in,gmvin);
3936         }
3937       *(grpname+charsize_in)=(char)0;
3938       if (strncmp(grpname,"endgrp",6) != 0)
3939         {
3940          binread(&i,intsize,INT,(long)1,gmvin);
3941          binread(&ngroupin,intsize,INT,(long)1,gmvin);
3942         }
3943      }
3944    if (ftype == ASCII)
3945      {
3946       fscanf(gmvin,"%s",grpname);
3947       if (strncmp(grpname,"endgrp",6) != 0)
3948          fscanf(gmvin,"%d%d",&i,&ngroupin);
3949      }
3950    ioerrtst(gmvin);
3951 
3952    /*  Check for endflag.  */
3953    if (strncmp(grpname,"endgrp",6) == 0)
3954      {
3955       readkeyword = 2;
3956       gmv_data.keyword = GROUPS;
3957       gmv_data.datatype = ENDKEYWORD;
3958       return;
3959      }
3960 
3961    if (i == 0) data_type = CELL;
3962    if (i == 1) data_type = NODE;
3963    if (i == 2) data_type = FACE;
3964    if (i == 3) data_type = SURF;
3965 
3966    /*  Check for existence of data_type.  */
3967    if (data_type == CELL && numcells == 0)
3968      {
3969       fprintf(stderr,"Error, no cells exist for cell group %s.\n",grpname);
3970       gmv_data.keyword = GMVERROR;
3971       return;
3972      }
3973    if (data_type == NODE && numnodes == 0)
3974      {
3975       fprintf(stderr,"Error, no nodes exist for node group %s.\n",grpname);
3976       gmv_data.keyword = GMVERROR;
3977       return;
3978      }
3979    if (data_type == FACE && numfaces == 0)
3980      {
3981       fprintf(stderr,"Error, no faces exist for face group: %s.\n",grpname);
3982       gmv_data.keyword = GMVERROR;
3983       return;
3984      }
3985 
3986    /*  Read the group data.  */
3987    groupin = (int *)malloc(ngroupin*sizeof(int));
3988    if (groupin == NULL)
3989      {
3990       gmvrdmemerr();
3991       return;
3992      }
3993 
3994    if (ftype != ASCII)
3995      {
3996       binread(groupin,intsize,INT,(long)ngroupin,gmvin);
3997       ioerrtst(gmvin);
3998      }
3999    if (ftype == ASCII) rdints(groupin,ngroupin,gmvin);
4000 
4001    gmv_data.keyword = GROUPS;
4002    gmv_data.datatype = data_type;
4003    strcpy(gmv_data.name1,grpname);
4004    gmv_data.num = ngroupin;
4005    gmv_data.nlongdata1 = ngroupin;
4006    gmv_data.longdata1 = (long *)malloc(ngroupin*sizeof(long));
4007    if (gmv_data.longdata1 == NULL)
4008      {
4009       gmvrdmemerr();
4010       return;
4011      }
4012    for (i = 0; i < ngroupin; i++)
4013       gmv_data.longdata1[i] =  groupin[i];
4014    free(groupin);
4015 }
4016 
4017 
readcellpes(FILE * gmvin,int ftype)4018 void readcellpes(FILE* gmvin, int ftype)
4019 {
4020   /*                                                */
4021   /*  Read and set cell processor (pe) identifier.  */
4022   /*                                                */
4023   long *lcellpes = NULL;
4024   int *tmpids, i;
4025 
4026    /*  Allocate cell pes.  */
4027    lcellpes=(long *)malloc(numcells*sizeof(long));
4028    if (lcellpes == NULL)
4029      {
4030       gmvrdmemerr();
4031       return;
4032      }
4033 
4034    /*  Read cell pes.  */
4035    if (ftype != ASCII)
4036      {
4037       if (ftype == IEEEI8R4 || ftype == IEEEI8R8)
4038         {
4039          binread(lcellpes,longlongsize,LONGLONG,numcells,gmvin);
4040         }
4041       else
4042         {
4043          tmpids=(int *)malloc(numcells*sizeof(int));
4044          if (tmpids == NULL)
4045            {
4046             gmvrdmemerr();
4047             return;
4048            }
4049          binread(tmpids,intsize,INT,numcells,gmvin);
4050          for (i = 0; i < numcells; i++)
4051             lcellpes[i] = tmpids[i];
4052          free(tmpids);
4053         }
4054       ioerrtst(gmvin);
4055      }
4056    if (ftype == ASCII) rdlongs(lcellpes,numcells,gmvin);
4057 
4058    gmv_data.keyword = CELLPES;
4059    gmv_data.datatype = REGULAR;
4060    gmv_data.num = numcells;
4061    gmv_data.nlongdata1 = numcells;
4062    gmv_data.longdata1 = lcellpes;
4063 }
4064 
4065 
readsubvars(FILE * gmvin,int ftype)4066 void readsubvars(FILE* gmvin, int ftype)
4067 {
4068   /*                                     */
4069   /*  Read and set subvars field data.     */
4070   /*                                     */
4071   int i, data_type, nsubvarin, *subvarid;
4072   double *subvarin;
4073   float *tmpfloat;
4074   char varname[33];
4075 
4076    /*  Read a dvars name, data type (cells, nodes,  */
4077    /*  etc),and the number of elements in the set.  */
4078    if (ftype != ASCII)
4079      {
4080       binread(varname,charsize,CHAR,(long)8,gmvin);
4081       if (strncmp(varname,"endsubv",7) != 0 && charsize_in == 32)
4082         {
4083          fseek(gmvin,-8L,SEEK_CUR);
4084          binread(varname,charsize,CHAR,(long)charsize_in,gmvin);
4085         }
4086       *(varname+charsize_in)=(char)0;
4087       if (strncmp(varname,"endsubv",7) != 0)
4088         {
4089          binread(&i,intsize,INT,(long)1,gmvin);
4090          binread(&nsubvarin,intsize,INT,(long)1,gmvin);
4091         }
4092      }
4093    if (ftype == ASCII)
4094      {
4095       fscanf(gmvin,"%s",varname);
4096       if (strncmp(varname,"endsubv",7) != 0)
4097          fscanf(gmvin,"%d%d",&i,&nsubvarin);
4098      }
4099    ioerrtst(gmvin);
4100 
4101    /*  Check for endsubv.  */
4102    if (strncmp(varname,"endsubv",7) == 0)
4103      {
4104       readkeyword = 2;
4105       gmv_data.keyword = SUBVARS;
4106       gmv_data.datatype = ENDKEYWORD;
4107       return;
4108      }
4109 
4110    if (i == 0) data_type = CELL;
4111    if (i == 1) data_type = NODE;
4112    if (i == 2) data_type = FACE;
4113 
4114    /*  Check for existence of data_type.  */
4115    if (data_type == CELL && numcells == 0)
4116      {
4117       fprintf(stderr,"Error, no cells exist for cell subvars %s.\n",varname);
4118       gmv_data.keyword = GMVERROR;
4119       return;
4120      }
4121    if (data_type == NODE && numnodes == 0)
4122      {
4123       fprintf(stderr,"Error, no nodes exist for node subvars %s.\n",varname);
4124       gmv_data.keyword = GMVERROR;
4125       return;
4126      }
4127    if (data_type == FACE && numfaces == 0)
4128      {
4129       fprintf(stderr,"Error, no faces exist for face subvars: %s.\n",varname);
4130       gmv_data.keyword = GMVERROR;
4131       return;
4132      }
4133 
4134    /*  Read one set of subvars data.  */
4135    subvarid = (int *)malloc(nsubvarin*sizeof(int));
4136    subvarin = (double *)malloc(nsubvarin*sizeof(double));
4137    if (subvarid == NULL || subvarin == NULL)
4138      {
4139       gmvrdmemerr();
4140       return;
4141      }
4142 
4143    if (ftype != ASCII)
4144      {
4145       binread(subvarid,intsize,INT,(long)nsubvarin,gmvin);
4146       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
4147         {
4148          binread(subvarin,doublesize,DOUBLE,(long)nsubvarin,gmvin);
4149          ioerrtst(gmvin);
4150         }
4151       else
4152         {
4153          tmpfloat = (float *)malloc(nsubvarin*sizeof(float));
4154          if (tmpfloat == NULL)
4155            {
4156             gmvrdmemerr();
4157             return;
4158            }
4159          binread(tmpfloat,floatsize,FLOAT,(long)nsubvarin,gmvin);
4160          ioerrtst(gmvin);
4161          for (i = 0; i < nsubvarin; i++) subvarin[i] = tmpfloat[i];
4162          free(tmpfloat);
4163         }
4164      }
4165    if (ftype == ASCII)
4166      {
4167       rdints(subvarid,(long)nsubvarin,gmvin);
4168       rdfloats(subvarin,(long)nsubvarin,gmvin);
4169      }
4170 
4171    gmv_data.keyword = SUBVARS;
4172    gmv_data.datatype = data_type;
4173    gmv_data.num = nsubvarin;
4174    strcpy(gmv_data.name1,varname);
4175    gmv_data.nlongdata1 = nsubvarin;
4176    gmv_data.longdata1 = (long *)malloc(nsubvarin*sizeof(long));
4177    if (gmv_data.longdata1 == NULL)
4178      {
4179       gmvrdmemerr();
4180       return;
4181      }
4182    for (i = 0; i < nsubvarin; i++)
4183       gmv_data.longdata1[i] =  subvarid[i];
4184    gmv_data.ndoubledata1 = nsubvarin;
4185    gmv_data.doubledata1 = subvarin;
4186    free(subvarid);
4187 }
4188 
4189 
readghosts(FILE * gmvin,int ftype)4190 void readghosts(FILE* gmvin, int ftype)
4191 {
4192   /*                                     */
4193   /*  Read and set subvars field data.     */
4194   /*                                     */
4195   int i, data_type, nghostin, *ghostid;
4196 
4197    /*  Read the data type (cells or nodes),    */
4198    /*  and the number of elements in the set.  */
4199    if (ftype != ASCII)
4200      {
4201       binread(&i,intsize,INT,(long)1,gmvin);
4202       binread(&nghostin,intsize,INT,(long)1,gmvin);
4203      }
4204    if (ftype == ASCII)
4205      {
4206       fscanf(gmvin,"%d%d",&i,&nghostin);
4207      }
4208    ioerrtst(gmvin);
4209 
4210    if (i == 0) data_type = CELL;
4211    if (i == 1) data_type = NODE;
4212 
4213    /*  Check for existence of data_type.  */
4214    if (data_type == CELL && numcells == 0)
4215      {
4216       fprintf(stderr,"Error, no cells exist for ghost cells .\n");
4217       gmv_data.keyword = GMVERROR;
4218       return;
4219      }
4220    if (data_type == NODE && numnodes == 0)
4221      {
4222       fprintf(stderr,"Error, no nodes exist for ghosts nodes.\n");
4223       gmv_data.keyword = GMVERROR;
4224       return;
4225      }
4226 
4227    /*  Read one set of ghost data.  */
4228    ghostid = (int *)malloc(nghostin*sizeof(int));
4229    if (ghostid == NULL)
4230      {
4231       gmvrdmemerr();
4232       return;
4233      }
4234 
4235    if (ftype != ASCII)
4236      {
4237       binread(ghostid,intsize,INT,(long)nghostin,gmvin);
4238      }
4239    if (ftype == ASCII)
4240      {
4241       rdints(ghostid,(long)nghostin,gmvin);
4242      }
4243 
4244    gmv_data.keyword = GHOSTS;
4245    gmv_data.datatype = data_type;
4246    gmv_data.num = nghostin;
4247    gmv_data.nlongdata1 = nghostin;
4248    gmv_data.longdata1 = (long *)malloc(nghostin*sizeof(long));
4249    if (gmv_data.longdata1 == NULL)
4250      {
4251       gmvrdmemerr();
4252       return;
4253      }
4254    for (i = 0; i < nghostin; i++)
4255       gmv_data.longdata1[i] =  ghostid[i];
4256    free(ghostid);
4257 }
4258 
4259 
readvects(FILE * gmvin,int ftype)4260 void readvects(FILE* gmvin, int ftype)
4261 {
4262   /*                                   */
4263   /*  Read and set vector field data.  */
4264   /*                                   */
4265   int i, data_type, nvectin, ncomps, nreadin, cnamein;
4266   double *vectin;
4267   float *tmpfloat;
4268   char vectname[33], cvname[33], *cvnames;
4269 
4270    /*  Read a vector name, data type (cells, nodes, faces), */
4271    /*  the number of components in the vector and the       */
4272    /*  component name option.                               */
4273    if (ftype != ASCII)
4274      {
4275       binread(vectname,charsize,CHAR,(long)8,gmvin);
4276       if (strncmp(vectname,"endvect",7) != 0 && charsize_in == 32)
4277         {
4278          fseek(gmvin,-8L,SEEK_CUR);
4279          binread(vectname,charsize,CHAR,(long)charsize_in,gmvin);
4280         }
4281       *(vectname+charsize_in)=(char)0;
4282       if (strncmp(vectname,"endvect",7) != 0)
4283         {
4284          binread(&i,intsize,INT,(long)1,gmvin);
4285          binread(&ncomps,intsize,INT,(long)1,gmvin);
4286          binread(&cnamein,intsize,INT,(long)1,gmvin);
4287         }
4288      }
4289    if (ftype == ASCII)
4290      {
4291       fscanf(gmvin,"%s",vectname);
4292       if (strncmp(vectname,"endvect",7) != 0)
4293         {
4294          fscanf(gmvin,"%d",&i);
4295          fscanf(gmvin,"%d",&ncomps);
4296          fscanf(gmvin,"%d",&cnamein);
4297         }
4298      }
4299    ioerrtst(gmvin);
4300 
4301    /*  Check for endvect.  */
4302    if (strncmp(vectname,"endvect",7) == 0)
4303      {
4304       readkeyword = 2;
4305       gmv_data.keyword = VECTORS;
4306       gmv_data.datatype = ENDKEYWORD;
4307       return;
4308      }
4309 
4310    if (i == 0) data_type = CELL;
4311    if (i == 1) data_type = NODE;
4312    if (i == 2) data_type = FACE;
4313 
4314    /*  Check for existence of data_type.  */
4315    if (data_type == CELL && numcells == 0)
4316      {
4317       fprintf(stderr,"Error, no cells exist for cell vector %s.\n",vectname);
4318       gmv_data.keyword = GMVERROR;
4319       return;
4320      }
4321    if (data_type == NODE && numnodes == 0)
4322      {
4323       fprintf(stderr,"Error, no nodes exist for node vector %s.\n",vectname);
4324       gmv_data.keyword = GMVERROR;
4325       return;
4326      }
4327    if (data_type == FACE && numfaces == 0)
4328      {
4329       fprintf(stderr,"Error, no faces exist for face vector: %s.\n",vectname);
4330       gmv_data.keyword = GMVERROR;
4331       return;
4332      }
4333 
4334    /*  Read component names, if they exist.  */
4335    /*  Otherwise generate a name.            */
4336    cvnames = (char *)malloc(ncomps*33*sizeof(char));
4337    if (cvnames == NULL)
4338      {
4339       gmvrdmemerr();
4340       return;
4341      }
4342    if (cnamein)
4343      {
4344       for (i = 0; i < ncomps; i++)
4345         {
4346          if (ftype != ASCII)
4347            {
4348             binread(cvname, charsize_in*charsize, CHAR, (long)1, gmvin);
4349             ioerrtst(gmvin);
4350            }
4351          if (ftype == ASCII)
4352            {
4353             fscanf(gmvin,"%s",cvname);
4354             ioerrtst(gmvin);
4355            }
4356          strncpy(&cvnames[i*33],cvname,32);
4357          *(cvnames+i*33+charsize_in) = (char) 0;
4358         }
4359      }
4360    else
4361      {
4362       for (i = 0; i < ncomps; i++)
4363         {
4364          sprintf(cvname,"%d-%s",i+1,vectname);
4365          strncpy(&cvnames[i*33],cvname,32);
4366          *(cvnames+i*33+charsize_in) = (char) 0;
4367         }
4368      }
4369 
4370    /*  Read one set of vector data.  */
4371    if (data_type == CELL) nvectin = numcells;
4372    if (data_type == NODE) nvectin = numnodes;
4373    if (data_type == FACE) nvectin = numfaces;
4374    nreadin = nvectin * ncomps;
4375    vectin = (double *)malloc(nreadin*sizeof(double));
4376    if (vectin == NULL)
4377      {
4378       gmvrdmemerr();
4379       return;
4380      }
4381 
4382    if (ftype != ASCII)
4383      {
4384       if (ftype == IEEEI4R8 || ftype == IEEEI8R8)
4385         {
4386          binread(vectin,doublesize,DOUBLE,(long)nreadin,gmvin);
4387          ioerrtst(gmvin);
4388         }
4389       else
4390         {
4391          tmpfloat = (float *)malloc(nreadin*sizeof(float));
4392          if (tmpfloat == NULL)
4393            {
4394             gmvrdmemerr();
4395             return;
4396            }
4397          binread(tmpfloat,floatsize,FLOAT,(long)nreadin,gmvin);
4398          ioerrtst(gmvin);
4399          for (i = 0; i < nreadin; i++) vectin[i] = tmpfloat[i];
4400          free(tmpfloat);
4401         }
4402      }
4403    if (ftype == ASCII) rdfloats(vectin,(long)nreadin,gmvin);
4404 
4405    gmv_data.keyword = VECTORS;
4406    gmv_data.datatype = data_type;
4407    gmv_data.num = nvectin;
4408    gmv_data.num2 = ncomps;
4409    strcpy(gmv_data.name1,vectname);
4410    gmv_data.nchardata1 = ncomps;
4411    gmv_data.chardata1 = cvnames;
4412    gmv_data.ndoubledata1 = nreadin;
4413    gmv_data.doubledata1 = vectin;
4414 }
4415 
4416 
gmvrdmemerr()4417 void gmvrdmemerr()
4418 {
4419   /*                 */
4420   /*  Memory error.  */
4421   /*                 */
4422 
4423    fprintf(stderr,"Not enough memory to read gmv data.\n");
4424    gmv_data.keyword = GMVERROR;
4425    gmv_meshdata.intype = GMVERROR;
4426 }
4427 
4428 
gmvrdmemerr2()4429 void gmvrdmemerr2()
4430 {
4431   /*                 */
4432   /*  Memory error.  */
4433   /*                 */
4434 
4435    fprintf(stderr,"Not enough memory to fill gmv mesh data.\n");
4436    gmvread_close();
4437    exit(0);
4438   }
4439 
4440 
ioerrtst(FILE * gmvin)4441 void ioerrtst(FILE *gmvin)
4442 {
4443    /*                                      */
4444    /*  Test input file for eof and error.  */
4445    /*                                      */
4446 
4447    if ((feof(gmvin) != 0) | (ferror(gmvin) != 0))
4448      {
4449       fprintf(stderr,"I/O error while reading gmv input file.\n");
4450       exit(0);
4451      }
4452 }
4453 
4454 
4455 
binread(void * ptr,int size,int type,long nitems,FILE * stream)4456 int binread(void* ptr, int size, int type, long nitems, FILE* stream)
4457 {
4458   int ret_stat;
4459 
4460 #ifdef CRAY
4461 
4462   float *floatptr, *floatbuf;
4463   double *doubleptr, *doublebuf;
4464   int tierr, ttype, tbitoff;
4465   char *charptr;
4466   int  *intptr, *intbuf;
4467   short *shortptr, *shortbuf;
4468 
4469    tbitoff = 0;  tierr = 0;
4470    ret_stat = 0;
4471 
4472    switch(type)
4473      {
4474 
4475       case CHAR:
4476         charptr = (char *)ptr;
4477         ret_stat = fread(charptr, size, nitems, stream);
4478         break;
4479 
4480       case SHORT:
4481         ttype = 7;
4482         shortbuf = (short *)malloc(size*nitems);
4483         shortptr = (short *)ptr;
4484 
4485         ret_stat = fread(shortbuf, size, nitems, stream);
4486         tierr = IEG2CRAY(&ttype, &nitems, shortbuf, &tbitoff, shortptr);
4487         free(shortbuf);
4488         break;
4489 
4490       case INT:
4491         ttype = 1;
4492         intptr = (int *)ptr;
4493         intbuf = (int *)malloc(size*nitems);
4494 
4495         ret_stat = fread(intbuf, size, nitems, stream);
4496         tierr = IEG2CRAY(&ttype, &nitems, intbuf, &tbitoff, intptr);
4497         free(intbuf);
4498         break;
4499 
4500       case FLOAT:
4501         ttype = 2;
4502         floatptr = (float *)ptr;
4503         floatbuf = (float *)malloc(size*nitems);
4504 
4505         ret_stat = fread(floatbuf, size, nitems, stream);
4506         tierr = IEG2CRAY(&ttype, &nitems, floatbuf, &tbitoff, floatptr);
4507         free(floatbuf);
4508         break;
4509 
4510       case DOUBLE:
4511         ttype = 3;
4512         doubleptr = (double *)ptr;
4513         doublebuf = (double *)malloc(size*nitems);
4514 
4515         ret_stat = fread(doublebuf, size, nitems, stream);
4516         tierr = IEG2CRAY(&ttype, &nitems, doublebuf, &tbitoff, doubleptr);
4517         free(doublebuf);
4518         break;
4519 
4520       case WORD:
4521         intptr = (int *)ptr;
4522         ret_stat = fread(intptr, size, nitems, stream);
4523         break;
4524 
4525       default:
4526         fprintf(stderr,"Error: Cannot match input datatype.\n");
4527         gmv_data.keyword = GMVERROR;
4528         return;
4529      }
4530 
4531      if(tierr != 0)
4532        {
4533         fprintf(stderr,"Error: Cannot convert IEEE data to CRAY\n");
4534         gmv_data.keyword = GMVERROR;
4535         return;
4536        }
4537 
4538      return ret_stat;
4539 
4540 #else
4541 
4542    ret_stat = fread(ptr, size, nitems, stream);
4543 
4544    if (swapbytes_on && type != CHAR && type != WORD)
4545       swapbytes(ptr, size, nitems);
4546 
4547    return ret_stat;
4548 
4549 #endif
4550 
4551 }
4552 
4553 
word2int(unsigned wordin)4554 int word2int(unsigned wordin)
4555 {
4556 
4557   int intout;
4558 
4559 #ifdef CRAY
4560 
4561   int ttype, tnum, tbitoff;
4562 
4563      tnum = 1; tbitoff = 0; ttype = 1;
4564 
4565      if(IEG2CRAY(&ttype, &tnum, &wordin, &tbitoff, &intout) != 0)
4566        {
4567         fprintf(stderr,"Error: Cannot convert IEEE data to CRAY\n");
4568         gmv_data.keyword = GMVERROR;
4569         return;
4570        }
4571 
4572      return intout;
4573 
4574 #else
4575 
4576      intout = (int)wordin;
4577      if (swapbytes_on)
4578         swapbytes(&intout, intsize, 1);
4579      return intout;
4580 
4581 #endif
4582 
4583 }
4584 
4585 
swapbytes(void * from,int size,int nitems)4586 void swapbytes(void *from, int size, int nitems)
4587 {
4588   int i, j;
4589   char *tmp1, tmp2[8];
4590 
4591   tmp1 = (char*)(from);
4592 
4593    if (size == 8)
4594      {
4595       for (i = 0; i < nitems; i++)
4596         {
4597          j = i * size;
4598          tmp2[0] = tmp1[j+7];
4599          tmp2[1] = tmp1[j+6];
4600          tmp2[2] = tmp1[j+5];
4601          tmp2[3] = tmp1[j+4];
4602          tmp2[4] = tmp1[j+3];
4603          tmp2[5] = tmp1[j+2];
4604          tmp2[6] = tmp1[j+1];
4605          tmp2[7] = tmp1[j];
4606          tmp1[j]   = tmp2[0];
4607          tmp1[j+1] = tmp2[1];
4608          tmp1[j+2] = tmp2[2];
4609          tmp1[j+3] = tmp2[3];
4610          tmp1[j+4] = tmp2[4];
4611          tmp1[j+5] = tmp2[5];
4612          tmp1[j+6] = tmp2[6];
4613          tmp1[j+7] = tmp2[7];
4614         }
4615      }
4616    if (size == 4)
4617      {
4618       for (i = 0; i < nitems; i++)
4619         {
4620          j = i * size;
4621          tmp2[0] = tmp1[j+3];
4622          tmp2[1] = tmp1[j+2];
4623          tmp2[2] = tmp1[j+1];
4624          tmp2[3] = tmp1[j];
4625          tmp1[j]   = tmp2[0];
4626          tmp1[j+1] = tmp2[1];
4627          tmp1[j+2] = tmp2[2];
4628          tmp1[j+3] = tmp2[3];
4629         }
4630      }
4631    if (size == 2)
4632      {
4633       for (i = 0; i < nitems; i++)
4634         {
4635          j = i * size;
4636          tmp2[0] = tmp1[j+1];
4637          tmp2[1] = tmp1[j];
4638          tmp1[j]   = tmp2[0];
4639          tmp1[j+1] = tmp2[1];
4640         }
4641      }
4642 }
4643 
4644 
chk_gmvend(FILE * fin)4645 int chk_gmvend(FILE *fin)
4646 {
4647   /*                          */
4648   /*  Read a GMV input file.  */
4649   /*                          */
4650   int i, chkend;
4651   long int currpos;
4652   char rdend[21];
4653 
4654    /*  Get the current file position.  */
4655    /* currpos = ftell(fin); */
4656    currpos = 8;
4657 
4658    /*  Read the last 20 characters of the file.  */
4659    fseek(fin, -20L, 2);
4660    fread(rdend,sizeof(char), 20, fin);
4661 
4662    /*  Check the 20 characters for endgmv.  */
4663    chkend = 0;
4664    for (i = 0; i < 15; i++)
4665       if (strncmp((rdend+i),"endgmv",6) == 0) chkend = 1;
4666 
4667    /*  Reset file position.  */
4668    fseek(fin, currpos, 0);
4669 
4670    return chkend;
4671 }
4672 
4673 
4674 long *celltoface, *cell_faces, cellfaces_alloc, totfaces,
4675      *facetoverts, facetoverts_alloc, nfacesin,
4676      *faceverts, faceverts_alloc, nvertsin,
4677      *cellnnode, *cellnodes, cellnodes_alloc, totcellnodes;
4678 static short vfacetype;
4679 
4680 
gmvread_mesh()4681 void gmvread_mesh()
4682 {
4683   int nxv, nyv, nzv, nodetype_in, j, k;
4684   long nn, i, ip;
4685   double *xin, *yin, *zin, x0, y0, z0, dx, dy, dz;
4686   void rdcells(int nodetype_in);
4687   void rdfaces(), rdxfaces();
4688 
4689    /*  Read and return mesh info. from a gmv input file.  */
4690    gmv_meshdata.celltoface = NULL;
4691    gmv_meshdata.cellfaces = NULL;
4692    gmv_meshdata.facetoverts = NULL;
4693    gmv_meshdata.faceverts = NULL;
4694    gmv_meshdata.facecell1 = NULL;
4695    gmv_meshdata.facecell2 = NULL;
4696    gmv_meshdata.vfacepe = NULL;
4697    gmv_meshdata.vfaceoppface = NULL;
4698    gmv_meshdata.vfaceoppfacepe = NULL;
4699    gmv_meshdata.cellnnode = NULL;
4700    gmv_meshdata.cellnodes = NULL;
4701 
4702    if (printon)
4703       printf("Reading mesh data.\n");
4704 
4705    /*  Read and save node x,y,zs.  */
4706    /* gmvread_data(); */
4707 
4708    /*  Check for GMVERROR.  */
4709    if (gmv_data.keyword == GMVERROR)
4710      {
4711       gmv_meshdata.intype = GMVERROR;
4712       return;
4713      }
4714 
4715    if (gmv_data.keyword != NODES)
4716      {
4717       fprintf(stderr,"Error - nodes keyword missing.\n");
4718       gmvread_close();
4719       gmv_meshdata.intype = GMVERROR;
4720       return;
4721      }
4722    gmv_meshdata.nxv = 0;
4723    gmv_meshdata.nyv = 0;
4724    gmv_meshdata.nzv = 0;
4725    if (gmv_data.keyword == NODES)
4726      {
4727       gmv_meshdata.nnodes = gmv_data.num;
4728       nn = gmv_data.num;
4729       gmv_meshdata.intype = gmv_data.datatype;
4730       nodetype_in = gmv_data.datatype;
4731 
4732       if (gmv_data.datatype != AMR)
4733         {
4734          gmv_meshdata.x = (double *)malloc(nn*sizeof(double));
4735          gmv_meshdata.y = (double *)malloc(nn*sizeof(double));
4736          gmv_meshdata.z = (double *)malloc(nn*sizeof(double));
4737          if (gmv_meshdata.x == NULL || gmv_meshdata.y == NULL ||
4738              gmv_meshdata.z == NULL)
4739            {
4740             gmvrdmemerr2();
4741             return;
4742            }
4743         }
4744 
4745       if (gmv_data.datatype == UNSTRUCT ||
4746           gmv_data.datatype == LOGICALLY_STRUCT)
4747         {
4748          for (i = 0; i < nn; i++)
4749            {
4750             gmv_meshdata.x[i] = gmv_data.doubledata1[i];
4751             gmv_meshdata.y[i] = gmv_data.doubledata2[i];
4752             gmv_meshdata.z[i] = gmv_data.doubledata3[i];
4753            }
4754         }
4755 
4756       if (gmv_data.datatype == STRUCT ||
4757           gmv_data.datatype == LOGICALLY_STRUCT)
4758         {
4759          gmv_meshdata.nxv = gmv_data.ndoubledata1;
4760          gmv_meshdata.nyv = gmv_data.ndoubledata2;
4761          gmv_meshdata.nzv = gmv_data.ndoubledata3;
4762          nxv = gmv_meshdata.nxv;
4763          nyv = gmv_meshdata.nyv;
4764          nzv = gmv_meshdata.nzv;
4765 
4766          if (gmv_data.datatype == STRUCT)
4767            {
4768             xin = (double *)malloc(nxv*sizeof(double));
4769             yin = (double *)malloc(nyv*sizeof(double));
4770             zin = (double *)malloc(nzv*sizeof(double));
4771             if (xin == NULL || yin == NULL || zin == NULL)
4772               {
4773                gmvrdmemerr2();
4774                return;
4775               }
4776             for (i = 0; i < nxv; i++)
4777                xin[i] = gmv_data.doubledata1[i];
4778             for (i = 0; i < nyv; i++)
4779                yin[i] = gmv_data.doubledata2[i];
4780             for (i = 0; i < nzv; i++)
4781                zin[i] = gmv_data.doubledata3[i];
4782 
4783             /*  Generate nodes from direction vectors.  */
4784             ip = 0;
4785             for (k = 0; k < nzv; k++)
4786               {
4787                for (j = 0; j < nyv; j++)
4788                  {
4789                   for (i = 0; i < nxv; i++)
4790                     {
4791                      gmv_meshdata.x[ip] = xin[i];
4792                      gmv_meshdata.y[ip] = yin[j];
4793                      gmv_meshdata.z[ip] = zin[k];
4794                      ip++;
4795                     }
4796                  }
4797               }
4798            }
4799         }
4800 
4801       if (gmv_data.datatype == AMR)
4802         {
4803          nxv = gmv_data.num2;
4804          nyv = gmv_data.nlongdata1;
4805          nzv = gmv_data.nlongdata2;
4806          x0 = gmv_data.doubledata1[0];
4807          y0 = gmv_data.doubledata1[1];
4808          z0 = gmv_data.doubledata1[2];
4809          dx = gmv_data.doubledata2[0];
4810          dy = gmv_data.doubledata2[1];
4811          dz = gmv_data.doubledata2[2];
4812          gmv_meshdata.nxv = nxv;
4813          gmv_meshdata.nyv = nyv;
4814          gmv_meshdata.nzv = nzv;
4815          gmv_meshdata.x = (double *)malloc(2*sizeof(double));
4816          gmv_meshdata.y = (double *)malloc(2*sizeof(double));
4817          gmv_meshdata.z = (double *)malloc(2*sizeof(double));
4818          gmv_meshdata.x[0] = x0;  gmv_meshdata.x[1] = dx;
4819          gmv_meshdata.y[0] = y0;  gmv_meshdata.y[1] = dy;
4820          gmv_meshdata.z[0] = z0;  gmv_meshdata.z[1] = dz;
4821         }
4822      }
4823 
4824    /*  Read cell or face input.  */
4825    gmvread_data();
4826 
4827    /*  Check for GMVERROR.  */
4828    if (gmv_data.keyword == GMVERROR)
4829      {
4830       gmv_meshdata.intype = GMVERROR;
4831       return;
4832      }
4833 
4834    if (gmv_data.keyword == CELLS) rdcells(nodetype_in);
4835    if (gmv_data.keyword == FACES) rdfaces();
4836    if (gmv_data.keyword == XFACES) rdxfaces();
4837 }
4838 
4839 
rdcells(int nodetype_in)4840 void rdcells(int nodetype_in)
4841 {
4842   static long icell;
4843   int i, nfa, nna;
4844   long nc;
4845   void gencell(long icell, long nc);
4846   void regcell(long icell, long nc);
4847   void vfacecell(long icell, long nc), rdvfaces(long nc);
4848   void fillmeshdata(long nc);
4849   void structmesh();
4850 
4851    /*  Get first cell info.  */
4852    gmv_meshdata.ncells = gmv_data.num;
4853    nc = gmv_data.num;
4854 
4855    /*  If amr, save the daughter list and return.  */
4856    if (gmv_data.datatype == AMR)
4857      {
4858       gmv_meshdata.ncells = gmv_data.num2;
4859       gmv_meshdata.nfaces = gmv_data.num;
4860       nc = gmv_data.num;
4861       gmv_meshdata.celltoface = (long *)malloc(nc*sizeof(long));
4862       if (gmv_meshdata.celltoface == NULL)
4863         {
4864          gmvrdmemerr2();
4865          return;
4866         }
4867       for (i = 0; i < nc; i++)
4868          gmv_meshdata.celltoface[i] = gmv_data.longdata1[i];
4869       return;
4870      }
4871 
4872    /*  If structured, return.  */
4873    if (nodetype_in == STRUCT ||
4874        nodetype_in == LOGICALLY_STRUCT)
4875       return;
4876 
4877    /*  Check for vfaces cells.  */
4878    gmv_meshdata.intype = CELLS;
4879    vfacetype = 0;
4880    if (gmv_data.datatype == VFACE2D || gmv_data.datatype == VFACE3D)
4881      {
4882       if (gmv_data.datatype == VFACE2D)
4883         {
4884          gmv_meshdata.intype = VFACES2D;
4885          vfacetype = 2;
4886         }
4887       else
4888         {
4889          gmv_meshdata.intype = VFACES3D;
4890          vfacetype = 3;
4891         }
4892      }
4893 
4894    nfa = 6;
4895    nna = 24;
4896    if (nc < 100)
4897      {
4898       nfa = 48;
4899       nna = 144;
4900      }
4901    celltoface = (long *)malloc(nc*sizeof(long));
4902    cell_faces = (long *)malloc(nc*nfa*sizeof(long));
4903    if (nc > 0 && (celltoface == NULL || cell_faces == NULL))
4904      {
4905       gmvrdmemerr2();
4906       return;
4907      }
4908    cellfaces_alloc = nc*nfa;
4909    if (vfacetype == 0)
4910      {
4911       facetoverts = (long *)malloc(nc*nfa*sizeof(long));
4912       facetoverts_alloc = nc*nfa;
4913       faceverts = (long *)malloc(nc*nna*sizeof(long));
4914       faceverts_alloc = nc*nna;
4915       if (nc > 0 && (facetoverts == NULL || faceverts == NULL))
4916         {
4917          gmvrdmemerr2();
4918          return;
4919         }
4920      }
4921 
4922    if (gmv_meshdata.intype == CELLS)
4923      {
4924       cellnodes_alloc = 1;
4925       totcellnodes = 0;
4926       cellnnode = (long *)malloc(nc*sizeof(long));
4927       cellnodes = (long *)malloc(1*sizeof(long));
4928       for (i = 0; i < nc; i++) cellnnode[i] = 0;
4929      }
4930 
4931    /*  Loop through cells.  */
4932    icell = 0;  nfacesin = 0;  nvertsin = 0;
4933    while (gmv_data.datatype != ENDKEYWORD)
4934      {
4935       if (gmv_data.datatype == GENERAL) gencell(icell,nc);
4936       if (gmv_data.datatype == REGULAR) regcell(icell,nc);
4937       if (gmv_data.datatype == VFACE2D || gmv_data.datatype == VFACE3D)
4938          vfacecell(icell,nc);
4939       icell++;
4940       gmvread_data();
4941       if (gmv_data.datatype == ENDKEYWORD)
4942         {
4943          if (vfacetype > 0)
4944            {
4945             gmvread_data();
4946             if (gmv_data.keyword != VFACES)
4947               {
4948                fprintf(stderr,"Error, vfaces keyword not found.\n");
4949                gmv_meshdata.intype = GMVERROR;
4950                return;
4951               }
4952             rdvfaces(nc);
4953            }
4954          else
4955            {
4956             totfaces = nfacesin;
4957             fillmeshdata(nc);
4958             if (totcellnodes > 0)
4959               {
4960                cellnodes = (long *)realloc(cellnodes,totcellnodes*sizeof(long));
4961                gmv_meshdata.cellnnode = cellnnode;
4962                gmv_meshdata.cellnodes = cellnodes;
4963               }
4964             else
4965               {
4966                free(cellnnode);  free(cellnodes);
4967               }
4968            }
4969          return;
4970         }
4971 
4972       /*  Check for GMVERROR.  */
4973       if (gmv_data.keyword == GMVERROR)
4974         {
4975          gmv_meshdata.intype = GMVERROR;
4976          return;
4977         }
4978      }
4979 }
4980 
4981 
gencell(long icell,long nc)4982 void gencell(long icell, long nc)
4983 {
4984   /*                                */
4985   /*  Set data for a general cell.  */
4986   /*                                */
4987   int totverts, nfaces;
4988   long i, j, k, nverts[1000];
4989   static long sumverts = 0, gcellcount = 0;
4990 
4991    /*  Save first face location for cell to faces pointer.  */
4992    celltoface[icell] = nfacesin;
4993 
4994    /*  Fill cell_faces array.  */
4995    nfaces = gmv_data.nlongdata1;
4996    if (nfacesin+nfaces > cellfaces_alloc)
4997      {
4998       j = (nfacesin+1) / (icell+1);
4999       k = cellfaces_alloc + (nc-icell)*j;
5000       if (k < nfacesin+nfaces) k = nfacesin+nfaces + nc*j;
5001       cell_faces = (long *)realloc(cell_faces,k*sizeof(long));
5002       if (cell_faces == NULL)
5003         {
5004          gmvrdmemerr2();
5005          return;
5006         }
5007       cellfaces_alloc = k;
5008      }
5009    for (i = 0; i < nfaces; i++)
5010       cell_faces[nfacesin+i] = nfacesin+i;
5011 
5012    /*  Save all face vertices, reallocate if needed.  */
5013    totverts = gmv_data.nlongdata2;
5014    sumverts += totverts;
5015    gcellcount ++;
5016    if (nvertsin+totverts > faceverts_alloc)
5017      {
5018       j = sumverts / gcellcount;
5019       k = faceverts_alloc + (nc-icell)*j;
5020       if (k < nvertsin+totverts) k = nvertsin+totverts + (nc-icell)*j;
5021       faceverts = (long *)realloc(faceverts,k*sizeof(long));
5022       if (faceverts == NULL) gmvrdmemerr2();
5023       faceverts_alloc = k;
5024      }
5025    for (i = 0; i < totverts; i++)
5026       faceverts[nvertsin+i] = gmv_data.longdata2[i];
5027 
5028    /*  Set facetoverts pointer array.  */
5029    if (nfacesin+nfaces > facetoverts_alloc)
5030      {
5031       j = (nfacesin+1) / (icell+1);
5032       k = facetoverts_alloc + nc*j;
5033       if (k < nfacesin+nfaces) k = nfacesin+nfaces + nc*j;
5034       facetoverts = (long  *)realloc(facetoverts,k*sizeof(long));
5035       if (facetoverts == NULL) gmvrdmemerr2();
5036       facetoverts_alloc = k;
5037      }
5038    for (i = 0; i < nfaces; i++)
5039       nverts[i] = gmv_data.longdata1[i];
5040    j = 0;
5041    for (i = 0; i < nfaces; i++)
5042      {
5043       facetoverts[nfacesin+i] = nvertsin + j;
5044       j += nverts[i];
5045      }
5046 
5047    /*  Reset counters.  */
5048    nfacesin += nfaces;
5049    nvertsin += totverts;
5050 }
5051 
5052 
regcell(long icell,long nc)5053 void regcell(long icell, long nc)
5054 {
5055   /*                                                       */
5056   /*  Read and set data for a regular cell.                */
5057   /*  icelltype indicates the cell type,                   */
5058   /*    1-tri (triangle), 2-quad,  3-tet, 4-pyramid,       */
5059   /*    5-prism, 6-hex, 7-line, 8-phex8, 9-phex20,         */
5060   /*    10-pprymd5, 11-pprymd13, 12-pprism6, 13-pprism15,  */
5061   /*    14-ptet4, 15-ptet10, 16-6tri, 17-8quad,            */
5062   /*    18-3line, 19-phex27                                */
5063   /*                                                       */
5064   long i, j, k, cnodes[30], fverts[145], l1, l2;
5065   int nfaces, nverts[144], totverts,  dupflag, ncnodes,
5066       dupverts[145], dupnverts[145], ndup, nf;
5067   int icelltype;
5068   char ckeyword[9];
5069   short trinverts[1] = {3};
5070   short trifverts[3] = {1,2,3};
5071   short quadnverts[1] = {4};
5072   short quadfverts[4] = {1,2,3,4};
5073   short tetnverts[4] = {3,3,3,3};
5074   short tetfverts[12] = {1,2,3, 1,3,4, 1,4,2, 4,3,2};
5075   short pyrnverts[5] = {3,3,3,3,4};
5076   short pyrfverts[16] = {1,2,3, 1,3,4, 1,4,5, 1,5,2, 5,4,3,2};
5077   short prsnverts[5] = {3,4,4,4,3};
5078   short prsfverts[18] = {1,2,3, 1,4,5,2 ,2,5,6,3, 1,3,6,4, 6,5,4};
5079   short hexnverts[6] = {4,4,4,4,4,4};
5080   short hexfverts[24] = {1,2,3,4, 1,5,6,2, 2,6,7,3, 3,7,8,4,
5081                          4,8,5,1, 8,7,6,5};
5082   short linenverts[1] = {2};
5083   short linefverts[2] = {1,2};
5084   short phex8nverts[6] = {4,4,4,4,4,4};
5085   short phex8fverts[24] = {1,4,3,2, 1,2,6,5, 2,3,7,6, 3,4,8,7,
5086                            4,1,5,8, 5,6,7,8};
5087   short phex20nverts[6] = {8,8,8,8,8,8};
5088   short phex20fverts[48] = {12,4,11,3,10,2,9,1, 9,2,18,6,13,5,17,1,
5089                             10,3,19,7,14,6,18,2, 11,4,20,8,15,7,19,3,
5090                             12,1,17,5,16,8,20,4, 13,6,14,7,15,8,16,5};
5091   short pyr5nverts[5] = {4,3,3,3,3};
5092   short pyr5fverts[16] = {4,3,2,1, 1,2,5, 2,3,5, 3,4,5, 4,1,5};
5093   short pyr13nverts[5] = {8,6,6,6,6};
5094   short pyr13fverts[32] = {4,8,3,7,2,6,1,9, 1,6,2,11,5,10, 2,7,3,12,5,11,
5095                            3,8,4,13,5,12, 4,9,1,10,5,13};
5096   short prs6nverts[5] = {3,4,4,4,3};
5097   short prs6fverts[18] = {1,3,2, 1,2,5,4 ,2,3,6,5, 1,4,6,3, 5,6,4};
5098   short prs15nverts[5] = {6,8,8,8,6};
5099   short prs15fverts[36] = {1,9,3,8,2,7, 1,7,2,14,5,10,4,13, 2,8,3,15,6,11,5,14,
5100                            3,9,1,13,4,12,6,15, 5,11,6,12,4,10};
5101   short tet4nverts[4] = {3,3,3,3};
5102   short tet4fverts[12] = {1,3,2, 1,2,4, 1,3,4, 2,3,4};
5103   short tet10nverts[4] = {6,6,6,6};
5104   short tet10fverts[24] = {1,7,3,6,2,5, 1,5,2,9,4,8, 1,7,3,10,4,8,
5105                             2,6,3,10,4,9};
5106   short quad8nverts[1] = {8};
5107   short quad8fverts[8] = {1,5,2,6,3,7,4,8};
5108   short tri6nverts[1] = {6};
5109   short tri6fverts[6] = {1,4,2,5,3,6};
5110   short line3nverts[2] = {2,2};
5111   short line3fverts[4] = {1,2, 2,3};
5112   short phex27nverts[48] = {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
5113                             3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3};
5114   short phex27fverts[144] = {1,9,21,  9,2,21,  2,18,21, 18,6,21,
5115                              6,13,21, 13,5,21, 5,17,21, 17,1,21,
5116                              2,10,22, 10,3,22, 3,19,22, 19,7,22,
5117                              7,14,22, 14,6,22, 6,18,22, 18,2,22,
5118                              3,11,23, 11,4,23, 4,20,23, 20,8,23,
5119                              8,15,23, 15,7,23, 7,19,23, 19,3,23,
5120                              4,12,24, 12,1,24, 1,17,24, 17,5,24,
5121                              5,16,24, 16,8,24, 8,20,24, 20,4,24,
5122                              1,12,25, 12,4,25, 4,11,25, 11,3,25,
5123                              3,10,25, 10,2,25, 2,9,25,  9,1,25,
5124                              5,13,26, 13,6,26, 6,14,26, 14,7,26,
5125                              7,15,26, 15,8,26, 8,16,26, 16,5,26};
5126   short *nv, *fv;
5127 
5128    /*  Get cell nodes.  */
5129    ncnodes = gmv_data.nlongdata1;
5130    for (i = 0; i < ncnodes; i++)
5131       cnodes[i] = gmv_data.longdata1[i];
5132 
5133    /*  Save cell nodes for regular cells.  */
5134    cellnnode[icell] = ncnodes;
5135    if (totcellnodes + ncnodes > cellnodes_alloc)
5136      {
5137       i = (nc - icell + 1) * ncnodes;
5138       cellnodes_alloc += i;
5139       cellnodes = (long *)realloc(cellnodes,cellnodes_alloc*sizeof(long));
5140      }
5141    for (i = 0; i < ncnodes; i++)
5142       cellnodes[totcellnodes+i] = cnodes[i];
5143    totcellnodes += ncnodes;
5144 
5145    /*  Check for duplicate nodes.  */
5146    dupflag = 0;
5147    for (i = 0; i < ncnodes-1; i++)
5148      {
5149       for (j = i+1; j < ncnodes; j++)
5150         {
5151          if (cnodes[i] == cnodes[j])
5152             dupflag = 1;
5153          }
5154      }
5155 
5156    /*  Determine cell type.  */
5157    strcpy(ckeyword,gmv_data.name1);
5158    if (strncmp(ckeyword,"tri",3) == 0) icelltype = 1;
5159    else if (strncmp(ckeyword,"quad",4) == 0) icelltype = 2;
5160    else if (strncmp(ckeyword,"tet",3) == 0) icelltype = 3;
5161    else if (strncmp(ckeyword,"hex",3) == 0) icelltype = 4;
5162    else if (strncmp(ckeyword,"prism",5) == 0) icelltype = 5;
5163    else if (strncmp(ckeyword,"pyramid",7) == 0) icelltype = 6;
5164    else if (strncmp(ckeyword,"line",4) == 0) icelltype = 7;
5165    else if (strncmp(ckeyword,"phex8",5) == 0) icelltype = 8;
5166    else if (strncmp(ckeyword,"phex20",6) == 0) icelltype = 9;
5167    else if (strncmp(ckeyword,"ppyrmd5",7) == 0) icelltype = 10;
5168    else if (strncmp(ckeyword,"ppyrmd13",8) == 0) icelltype = 11;
5169    else if (strncmp(ckeyword,"pprism6",7) == 0) icelltype = 12;
5170    else if (strncmp(ckeyword,"pprism15",8) == 0) icelltype = 13;
5171    else if (strncmp(ckeyword,"ptet4",5) == 0) icelltype = 14;
5172    else if (strncmp(ckeyword,"ptet10",6) == 0) icelltype = 15;
5173    else if (strncmp(ckeyword,"6tri",4) == 0) icelltype = 16;
5174    else if (strncmp(ckeyword,"8quad",5) == 0) icelltype = 17;
5175    else if (strncmp(ckeyword,"3line",5) == 0) icelltype = 18;
5176    else if (strncmp(ckeyword,"phex27",6) == 0) icelltype = 19;
5177 
5178    /*  Set face information according to cell type.  */
5179    switch (icelltype)
5180      {
5181       case 1: nfaces = 1;    /* tri */
5182               totverts = 3;
5183               nv = trinverts;
5184               fv = trifverts;
5185               break;
5186       case 2: nfaces = 1;    /* quad */
5187               totverts = 4;
5188               nv = quadnverts;
5189               fv = quadfverts;
5190               break;
5191       case 3: nfaces = 4;    /* tet */
5192               totverts = 12;
5193               nv = tetnverts;
5194               fv = tetfverts;
5195               break;
5196       case 4: nfaces = 6;    /* hex */
5197               totverts = 24;
5198               nv = hexnverts;
5199               fv = hexfverts;
5200               break;
5201       case 5: nfaces = 5;    /* prism */
5202               totverts = 18;
5203               nv = prsnverts;
5204               fv = prsfverts;
5205               break;
5206       case 6: nfaces = 5;    /* pyramid */
5207               totverts = 16;
5208               nv = pyrnverts;
5209               fv = pyrfverts;
5210               break;
5211       case 7: nfaces = 1;    /* line */
5212               totverts = 2;
5213               nv = linenverts;
5214               fv = linefverts;
5215               break;
5216       case 8: nfaces = 6;    /* phex8 */
5217               totverts = 24;
5218               nv = phex8nverts;
5219               fv = phex8fverts;
5220               break;
5221       case 9: nfaces = 6;    /* phex20 */
5222               totverts = 48;
5223               nv = phex20nverts;
5224               fv = phex20fverts;
5225               break;
5226       case 10: nfaces = 5;    /* ppyrmd5, patran 5 point pyramid */
5227                totverts = 16;
5228                nv = pyr5nverts;
5229                fv = pyr5fverts;
5230                break;
5231       case 11: nfaces = 5;    /* ppyrmd13, patran 13 point pyramid */
5232                totverts = 32;
5233                nv = pyr13nverts;
5234                fv = pyr13fverts;
5235                break;
5236       case 12: nfaces = 5;    /* pprism6, patran 6 point prism */
5237                totverts = 18;
5238                nv = prs6nverts;
5239                fv = prs6fverts;
5240                break;
5241       case 13: nfaces = 5;    /* pprism15, patran 15 point prism */
5242                totverts = 36;
5243                nv = prs15nverts;
5244                fv = prs15fverts;
5245                break;
5246       case 14: nfaces = 4;    /* ptet4, patran tet */
5247                totverts = 12;
5248                nv = tet4nverts;
5249                fv = tet4fverts;
5250                break;
5251       case 15: nfaces = 4;    /* ptet10, patran tet */
5252                totverts = 24;
5253                nv = tet10nverts;
5254                fv = tet10fverts;
5255                break;
5256       case 16: nfaces = 1;    /* 6tri */
5257                totverts = 6;
5258                nv = tri6nverts;
5259                fv = tri6fverts;
5260                break;
5261       case 17: nfaces = 1;    /* 8quad */
5262                totverts = 8;
5263                nv = quad8nverts;
5264                fv = quad8fverts;
5265                break;
5266       case 18: nfaces = 2;    /* 3line, 3 point line */
5267                totverts = 4;
5268                nv = line3nverts;
5269                fv = line3fverts;
5270                break;
5271       case 19: nfaces = 48;    /* phex27, 27 point hex */
5272                totverts = 144;
5273                nv = phex27nverts;
5274                fv = phex27fverts;
5275                break;
5276       default: break;
5277      }
5278 
5279    /*  Build face information.  */
5280    for (i = 0; i < nfaces; i++)
5281       nverts[i] = nv[i];
5282 
5283    /*  Build face vertices.  */
5284    for (i = 0; i < totverts; i++)
5285      {
5286       j = fv[i] - 1;
5287       fverts[i] = cnodes[j];
5288      }
5289 
5290    /*  If duplicate nodes exist, check   */
5291    /*  and adjust for degenerate faces.  */
5292    if (dupflag)
5293      {
5294       ndup = 0;
5295       k = 0;
5296       nf = 0;
5297       for (i = 0; i < nfaces; i++)
5298         {
5299          if (i > 0) k += nverts[i-1];
5300          dupnverts[nf] = nverts[i];
5301          for (j = 0; j < nverts[i]; j++)
5302            {
5303             l1 = k + j;
5304             l2 = l1 + 1;
5305             if (j == nverts[i] - 1) l2 = k;
5306             if (fverts[l1] != fverts[l2])
5307               {
5308                dupverts[ndup] = fverts[l1];
5309                ndup++;
5310               }
5311             else
5312               {
5313                dupnverts[nf]--;
5314               }
5315            }
5316          if (dupnverts[nf] > 2) nf++;
5317          else ndup -= dupnverts[nf];
5318         }
5319 
5320       /*  If cell consists of all duplicate nodes, set to first node.  */
5321       if (ndup <= 0)
5322         {
5323          dupverts[0] = fverts[0];
5324          dupnverts[0] = 1;
5325         }
5326 
5327       nfaces = nf;
5328       if (nfaces == 0) nfaces = 1;
5329       totverts = 0;
5330       for (i = 0; i < nfaces; i++)
5331        {
5332         nverts[i] = dupnverts[i];
5333         totverts += dupnverts[i];
5334        }
5335       for (i = 0; i < totverts; i++)
5336         fverts[i] = dupverts[i];
5337      }
5338 
5339    /*  Save first face location for cell to faces pointer.  */
5340    celltoface[icell] = nfacesin;
5341 
5342    /*  Save no. of vertices per face, reallocate array if needed.  */
5343    if (nfacesin+nfaces > cellfaces_alloc)
5344      {
5345       k = cellfaces_alloc + (nc-icell)*nfaces;
5346       cell_faces = (long *)realloc(cell_faces,k*sizeof(long));
5347       if (cell_faces == NULL)
5348         {
5349          gmvrdmemerr2();
5350          return;
5351         }
5352       cellfaces_alloc = k;
5353      }
5354    for (i = 0; i < nfaces; i++)
5355       cell_faces[nfacesin+i] = nfacesin+i;
5356 
5357    /*  Save all face vertices, reallocate if needed.  */
5358    if (nvertsin+totverts > faceverts_alloc)
5359      {
5360       j = totverts;
5361       k = faceverts_alloc + (nc-icell)*j;
5362       if (k < nvertsin+totverts) k = nvertsin+totverts + (nc-icell)*j;
5363       faceverts = (long  *)realloc(faceverts,k*sizeof(long));
5364       if (faceverts == NULL) gmvrdmemerr2();
5365       faceverts_alloc = k;
5366      }
5367    for (i = 0; i < totverts; i++)
5368       faceverts[nvertsin+i] = fverts[i];
5369 
5370    /*  Set facetoverts pointer array.  */
5371    if (nfacesin+nfaces > facetoverts_alloc)
5372      {
5373       k = facetoverts_alloc + (nc-icell)*nfaces;
5374       facetoverts = (long *)realloc(facetoverts,k*sizeof(long));
5375       if (facetoverts == NULL) gmvrdmemerr2();
5376       facetoverts_alloc = k;
5377      }
5378    j = 0;
5379    for (i = 0; i < nfaces; i++)
5380      {
5381       facetoverts[nfacesin+i] = nvertsin + j;
5382       j += nverts[i];
5383      }
5384 
5385    /*  Reset counters.  */
5386    nfacesin += nfaces;
5387    nvertsin += totverts;
5388 }
5389 
5390 
vfacecell(long icell,long nc)5391 void vfacecell(long icell, long nc)
5392 {
5393   /*                                */
5394   /*  Set data for a vface cell.  */
5395   /*                                */
5396   int nfaces;
5397   long i, j, k;
5398 
5399    /*  Save first face location for cell to faces pointer.  */
5400    celltoface[icell] = nfacesin;
5401 
5402    /*  Read and fill cell_faces array, reallocate if needed.  */
5403    nfaces = gmv_data.nlongdata1;
5404    if (nfacesin+nfaces > cellfaces_alloc)
5405      {
5406       j = (nfacesin+1) / (icell+1);
5407       k = cellfaces_alloc + (nc-icell)*j;
5408       if (k < nfacesin+nfaces) k = nfacesin+nfaces + nc*j;
5409       cell_faces = (long *)realloc(cell_faces,k*sizeof(long));
5410       if (cell_faces == NULL) gmvrdmemerr2();
5411       cellfaces_alloc = k;
5412      }
5413    for (i = 0; i < nfaces; i++)
5414       cell_faces[nfacesin+i] = gmv_data.longdata1[i] - 1;
5415 
5416    /*  Reset counters.  */
5417    nfacesin += nfaces;
5418 }
5419 
5420 
fillmeshdata(long nc)5421 void fillmeshdata(long nc)
5422 {
5423   /*                                               */
5424   /*  Fill gmv_meshdata structure with cell data.  */
5425   /*                                               */
5426 
5427    gmv_meshdata.ncells = nc;
5428    gmv_meshdata.nfaces = nfacesin;
5429    gmv_meshdata.totfaces = totfaces;
5430    gmv_meshdata.totverts = nvertsin;
5431 
5432    if (nc == 0) return;
5433 
5434    celltoface = (long *)realloc(celltoface,(nc+1)*sizeof(long));
5435    gmv_meshdata.celltoface = celltoface;
5436    gmv_meshdata.celltoface[nc] = totfaces;
5437 
5438    cell_faces = (long *)realloc(cell_faces,(totfaces+1)*sizeof(long));
5439    gmv_meshdata.cellfaces = cell_faces;
5440    gmv_meshdata.cellfaces[totfaces] = nfacesin;
5441 
5442    facetoverts = (long *)realloc(facetoverts,(nfacesin+1)*sizeof(long));
5443    gmv_meshdata.facetoverts = facetoverts;
5444    gmv_meshdata.facetoverts[nfacesin] = nvertsin;
5445 
5446    faceverts = (long *)realloc(faceverts,nvertsin*sizeof(long));
5447    gmv_meshdata.faceverts = faceverts;
5448 }
5449 
5450 
rdfaces()5451 void rdfaces()
5452 {
5453   static long iface, *facecell1, *facecell2;
5454   long nc, i, k;
5455   int nverts;
5456   void fillmeshdata(long nc);
5457   void fillcellinfo(long nc, long *facecell1, long *facecell2);
5458 
5459    /*  Get first face info.  */
5460    gmv_meshdata.nfaces = gmv_data.num;
5461    gmv_meshdata.ncells = gmv_data.num2;
5462    nc = gmv_data.num2;
5463    nfacesin = gmv_data.num;
5464 
5465    gmv_meshdata.intype = FACES;
5466 
5467    celltoface = (long *)malloc(nc*sizeof(long));
5468    facetoverts = (long *)malloc(nfacesin*sizeof(long));
5469    faceverts = (long *)malloc(nfacesin*8*sizeof(long));
5470    faceverts_alloc = nfacesin*8;
5471    facecell1 = (long *)malloc(nfacesin*sizeof(long));
5472    facecell2 = (long *)malloc(nfacesin*sizeof(long));
5473    if (celltoface == NULL || faceverts == NULL || facecell1 == NULL ||
5474        facecell2 == NULL)
5475       gmvrdmemerr2();
5476 
5477    /*  Loop through faces.  */
5478    iface = 0;  nvertsin = 0;
5479    while (gmv_data.datatype != ENDKEYWORD)
5480      {
5481       nverts = gmv_data.nlongdata1 - 2;
5482 
5483       /*  Fill faceverts, allocate if needed.  */
5484       if (nvertsin+nverts > faceverts_alloc)
5485         {
5486          k = faceverts_alloc + nc*8;
5487          faceverts = (long *)realloc(faceverts,k*sizeof(long));
5488          if (faceverts == NULL) gmvrdmemerr2();
5489          faceverts_alloc = k;
5490         }
5491       for (i = 0; i < nverts; i++)
5492          faceverts[nvertsin+i] = gmv_data.longdata1[i];
5493 
5494       /*  Fill face cell nos.  */
5495       facecell1[iface] = gmv_data.longdata1[nverts];
5496       facecell2[iface] = gmv_data.longdata1[nverts+1];
5497 
5498       /*  Set facetoverts pointer array.  */
5499       facetoverts[iface] = nvertsin;
5500 
5501       nvertsin += nverts;
5502       iface++;
5503 
5504       gmvread_data();
5505       if (gmv_data.datatype == ENDKEYWORD)
5506         {
5507          fillcellinfo(nc, facecell1, facecell2);
5508          fillmeshdata(nc);
5509          return;
5510         }
5511 
5512       /*  Check for GMVERROR.  */
5513       if (gmv_data.keyword == GMVERROR)
5514         {
5515          gmv_meshdata.intype = GMVERROR;
5516          return;
5517         }
5518      }
5519 }
5520 
5521 
rdvfaces(long nc)5522 void rdvfaces(long nc)
5523 {
5524   static long iface, *facecell1, *facecell2;
5525   static long *facepe, *oppface, *oppfacepe;
5526   long i, k;
5527   int nverts;
5528   void fillmeshdata(long nc);
5529 
5530    /*  Get first vface info.  */
5531    gmv_meshdata.nfaces = gmv_data.num;
5532    gmv_meshdata.ncells = nc;
5533    if (gmv_data.num != nfacesin)
5534      {
5535       fprintf(stderr,"I/O error while reading vfaces.\n");
5536       gmv_meshdata.intype = GMVERROR;
5537       return;
5538      }
5539    nfacesin = gmv_data.num;
5540    totfaces = nfacesin;
5541 
5542    facetoverts = (long *)malloc(nfacesin*sizeof(long));
5543    faceverts = (long *)malloc(nfacesin*8*sizeof(long));
5544    faceverts_alloc = nfacesin*8;
5545    facecell1 = (long *)malloc(nfacesin*sizeof(long));
5546    facecell2 = (long *)malloc(nfacesin*sizeof(long));
5547    facepe = (long *)malloc(nfacesin*sizeof(long));
5548    oppface = (long *)malloc(nfacesin*sizeof(long));
5549    oppfacepe = (long *)malloc(nfacesin*sizeof(long));
5550    if (facetoverts == NULL || faceverts == NULL || facecell1 == NULL ||
5551        facecell2 == NULL || facepe == NULL || oppface == NULL ||
5552        oppfacepe == NULL)
5553       gmvrdmemerr2();
5554 
5555    /*  Loop through faces.  */
5556    iface = 0;  nvertsin = 0;
5557    while (gmv_data.datatype != ENDKEYWORD)
5558      {
5559       nverts = gmv_data.nlongdata1;
5560 
5561       /*  Fill faceverts, allocate if needed.  */
5562       if (nvertsin+nverts > faceverts_alloc)
5563         {
5564          k = faceverts_alloc + nc*8;
5565          faceverts = (long *)realloc(faceverts,k*sizeof(long));
5566          if (faceverts == NULL) gmvrdmemerr2();
5567          faceverts_alloc = k;
5568         }
5569       for (i = 0; i < nverts; i++)
5570          faceverts[nvertsin+i] = gmv_data.longdata1[i];
5571 
5572       /*  Fill face pe, oppface, oppfacepe and cell no.  */
5573       facepe[iface] = gmv_data.longdata2[0];
5574       oppface[iface] = gmv_data.longdata2[1] - 1;
5575       if (oppface[iface] >= nfacesin) oppface[iface] = 0;
5576       oppfacepe[iface] = gmv_data.longdata2[2];
5577       facecell1[iface] = gmv_data.longdata2[3];
5578       facecell2[iface] = 0;
5579 
5580       /*  Set facetoverts pointer array.  */
5581       facetoverts[iface] = nvertsin;
5582 
5583       nvertsin += nverts;
5584       iface++;
5585 
5586       gmvread_data();
5587       if (gmv_data.datatype == ENDKEYWORD)
5588         {
5589 
5590          /*  Fill second cellid for face.  */
5591          for (i = 0; i < nfacesin; i++)
5592            {
5593 
5594             /*  If face and opposite faces have same  */
5595             /*  pe, get cell.  Otherwise use ncells.  */
5596             if (oppface[i] >= 0)
5597               {
5598                if (facepe[i] == oppfacepe[i])
5599                  {
5600                   k = oppface[i];
5601                   facecell2[i] = facecell1[k];
5602                  }
5603               }
5604            }
5605 
5606          /*  Check for GMVERROR.  */
5607          if (gmv_data.keyword == GMVERROR)
5608            {
5609             gmv_meshdata.intype = GMVERROR;
5610             return;
5611            }
5612          fillmeshdata(nc);
5613 
5614          /*  Fill vface extra data.  */
5615          gmv_meshdata.facecell1 = facecell1;
5616          gmv_meshdata.facecell2 = facecell2;
5617          gmv_meshdata.vfacepe = facepe;
5618          gmv_meshdata.vfaceoppface = oppface;
5619          gmv_meshdata.vfaceoppfacepe = oppfacepe;
5620          return;
5621         }
5622      }
5623 }
5624 
5625 
5626 
rdxfaces()5627 void rdxfaces()
5628 {
5629   static long *facecell1, *facecell2;
5630   static long *facepe, *oppface, *oppfacepe;
5631   long nc, i, k, totverts;
5632   int maxnvert;
5633   void fillmeshdata(long nc);
5634   void fillcellinfo(long nc, long *facecell1, long *facecell2);
5635 
5636    /*  Get info for first xfaces read.  */
5637    gmv_meshdata.nfaces = gmv_data.num;
5638    nfacesin = gmv_data.num;
5639    totfaces = nfacesin;
5640    totverts = gmv_data.nlongdata2;
5641    nvertsin = totverts;
5642 
5643    facetoverts = (long *)malloc(nfacesin*sizeof(long));
5644    faceverts = (long *)malloc(totverts*sizeof(long));
5645    facecell1 = (long *)malloc(nfacesin*sizeof(long));
5646    facecell2 = (long *)malloc(nfacesin*sizeof(long));
5647    facepe = (long *)malloc(nfacesin*sizeof(long));
5648    oppface = (long *)malloc(nfacesin*sizeof(long));
5649    oppfacepe = (long *)malloc(nfacesin*sizeof(long));
5650    if (facetoverts == NULL || faceverts == NULL || facecell1 == NULL ||
5651        facecell2 == NULL || facepe == NULL || oppface == NULL ||
5652        oppfacepe == NULL)
5653       gmvrdmemerr2();
5654    for (i = 0; i < nfacesin; i++) facecell2[i] = 0;
5655 
5656    /*  Fill the face to verts and the faceverts arrays.  */
5657    k = 0;
5658    facetoverts[0] = 0;
5659    for (i = 0; i < nfacesin; i++)
5660      {
5661       k +=  gmv_data.longdata1[i];
5662       facetoverts[i+1] = k;
5663      }
5664    for (i = 0; i < totverts; i++)
5665       faceverts[i] = gmv_data.longdata2[i];
5666 
5667    /*  Determine if the mesh is 2D (max. nverts is 2).  */
5668    maxnvert = 0;
5669    for (i = 0; i < nfacesin; i++)
5670      {
5671       if (gmv_data.longdata1[i] > maxnvert)
5672          maxnvert = gmv_data.longdata1[i];
5673      }
5674 
5675    /*  Save the mesh type as VFACES2D or VFACES3D.  */
5676    if (maxnvert <= 2)
5677       gmv_meshdata.intype = VFACES2D;
5678    else
5679       gmv_meshdata.intype = VFACES3D;
5680 
5681    /*  Loop through xfaces data.  */
5682    while (gmv_data.datatype != ENDKEYWORD)
5683      {
5684       gmvread_data();
5685 
5686       if (gmv_data.datatype == ENDKEYWORD)
5687         {
5688 
5689          gmv_meshdata.ncells = gmv_data.num2;
5690          nc = gmv_data.num2;
5691          celltoface = (long *)malloc(nc*sizeof(long));
5692          if (celltoface == NULL) gmvrdmemerr2();
5693          fillcellinfo(nc, facecell1, facecell2);
5694          fillmeshdata(nc);
5695 
5696          /*  Fill second cellid for face.  */
5697          for (i = 0; i < nfacesin; i++)
5698            {
5699 
5700             /*  If face and opposite faces have same  */
5701             /*  pe, get cell.  Otherwise use ncells.  */
5702             facecell2[i] = 0;
5703             if (oppface[i] >= 0)
5704               {
5705                if (facepe[i] == oppfacepe[i])
5706                  {
5707                   k = oppface[i];
5708                   facecell2[i] = facecell1[k];
5709                  }
5710               }
5711            }
5712 
5713          /*  Fill vface extra data.  */
5714          gmv_meshdata.facecell1 = facecell1;
5715          gmv_meshdata.facecell2 = facecell2;
5716          gmv_meshdata.vfacepe = facepe;
5717          gmv_meshdata.vfaceoppface = oppface;
5718          gmv_meshdata.vfaceoppfacepe = oppfacepe;
5719          return;
5720         }
5721 
5722       /*  Check for GMVERROR.  */
5723       if (gmv_data.keyword == GMVERROR)
5724         {
5725          gmv_meshdata.intype = GMVERROR;
5726          return;
5727         }
5728 
5729       /*  Save cell1 info.  */
5730       if (gmv_data.num2 == 1)
5731         {
5732          for (i = 0; i < nfacesin; i++)
5733             facecell1[i] = gmv_data.longdata1[i];
5734         }
5735 
5736       /*  Save opposite face info.  */
5737       if (gmv_data.num2 == 2)
5738         {
5739          for (i = 0; i < nfacesin; i++)
5740             oppface[i] = gmv_data.longdata1[i] - 1;
5741         }
5742 
5743       /*  Save face pe info.  */
5744       if (gmv_data.num2 == 3)
5745         {
5746          for (i = 0; i < nfacesin; i++)
5747             facepe[i] = gmv_data.longdata1[i];
5748         }
5749 
5750       /*  Save opposite face pe info.  */
5751       if (gmv_data.num2 == 4)
5752         {
5753          for (i = 0; i < nfacesin; i++)
5754             oppfacepe[i] = gmv_data.longdata1[i];
5755         }
5756      }
5757 }
5758 
5759 
fillcellinfo(long nc,long * facecell1,long * facecell2)5760 void fillcellinfo(long nc, long *facecell1, long *facecell2)
5761 {
5762   long i, j, k, sumcount;
5763   int *fcount;
5764 
5765    /*  Count the number of faces per cell.  */
5766    fcount = (int *)malloc(nc*sizeof(int));
5767    if (fcount == NULL) gmvrdmemerr2();
5768    for (i = 0; i < nc; i++) fcount[i] = 0;
5769    for (i = 0; i < nfacesin; i++)
5770      {
5771       j = facecell1[i];
5772       if (j > 0) fcount[j-1]++;
5773       j = facecell2[i];
5774       if (j > 0) fcount[j-1]++;
5775      }
5776 
5777    /*  Get the total number of faces per cell    */
5778    /*  and fill the cellto face array by count.  */
5779    totfaces = 0;
5780    sumcount = 0;
5781    for (i = 0; i < nc; i++)
5782      {
5783       celltoface[i] = sumcount;
5784       sumcount += fcount[i];
5785       totfaces += fcount[i];
5786      }
5787 
5788    /*  Allocate and fill cell_faces pointer array.  */
5789    cell_faces = (long *)malloc(totfaces*sizeof(long));
5790    if (cell_faces == NULL) gmvrdmemerr2();
5791    for (i = 0; i < nc; i++) fcount[i] = 0;
5792    for (i = 0; i < nfacesin; i++)
5793      {
5794       j = facecell1[i];
5795       if (j > 0)
5796         {
5797          j--;
5798          k = celltoface[j] + fcount[j];
5799          cell_faces[k] = i;
5800          fcount[j]++;
5801         }
5802       j = facecell2[i];
5803       if (j > 0)
5804         {
5805          j --;
5806          k = celltoface[j] + fcount[j];
5807          cell_faces[k] = i;
5808          fcount[j]++;
5809         }
5810      }
5811    free(fcount);
5812 
5813    /*  Fill facecell1 and facecell2 in gmv_meshdata.  */
5814    facecell1 = (long *)realloc(facecell1,nfacesin*sizeof(long));
5815    facecell2 = (long *)realloc(facecell2,nfacesin*sizeof(long));
5816    if (facecell1 == NULL || facecell2 == NULL)
5817       gmvrdmemerr2();
5818    gmv_meshdata.facecell1 = facecell1;
5819    gmv_meshdata.facecell2 = facecell2;
5820 }
5821 
5822 
5823 
struct2vface()5824 void struct2vface()
5825 {
5826   int nxv, nyv, nzv, nxyv, nxc, nyc, nxyc, itemp[8], izc, ixy, iyc, ixc,
5827       nc, nf, nv, struct2dflag, nfaces, nfv, kf, jf, icell, icell2, ii, jj,
5828       kk, kv, j;
5829   short hexfverts[24] = {1,4,3,2, 1,2,6,5, 2,3,7,6, 3,4,8,7,
5830                          4,1,5,8, 5,6,7,8};
5831   short quadfverts[8] = {1,2, 2,3, 3,4, 4,1};
5832 
5833    /*  Generate a set of vface data from a structured grid.  */
5834    nxv = gmv_meshdata.nxv;
5835    nyv = gmv_meshdata.nyv;
5836    nzv = gmv_meshdata.nzv;
5837 
5838    /*  Check for 2D structured (nzv = 1). */
5839    gmv_meshdata.intype = VFACES3D;
5840    struct2dflag = 0;
5841    if (nzv == 1)
5842      {
5843       gmv_meshdata.intype = VFACES2D;
5844       struct2dflag = 1;
5845      }
5846 
5847    /*  Determine the number of cells, faces and vertices.  */
5848    nc = (nxv-1) * (nyv-1) * (nzv-1);
5849    nf = nc * 6;
5850    nfaces = 6;
5851    nv = nc * 24;
5852    nfv = 4;
5853    if (struct2dflag == 1)
5854      {
5855       nc = (nxv-1) * (nyv-1);
5856       nf = nc * 4;
5857       nfaces = 4;
5858       nv = nc * 8;
5859       nfv = 2;
5860      }
5861    gmv_meshdata.ncells = nc;
5862    gmv_meshdata.nfaces = nf;
5863    gmv_meshdata.totfaces = nf;
5864    gmv_meshdata.totverts = nv;
5865 
5866    /*  Set boundary info.  */
5867    nxyv = nxv * nyv;
5868    nxc = nxv - 1;
5869    nyc = nyv - 1;
5870    nxyc = nxc * nyc;
5871 
5872    /*  Allocate memory needed to fill mesh data.  */
5873    gmv_meshdata.celltoface = (long *)malloc((nc+1)*sizeof(long));
5874    if (gmv_meshdata.celltoface == NULL) gmvrdmemerr2();
5875    gmv_meshdata.cellfaces = (long *)malloc((nf+1)*sizeof(long));
5876    if (gmv_meshdata.cellfaces == NULL) gmvrdmemerr2();
5877    gmv_meshdata.facetoverts = (long *)malloc((nf+1)*
5878                                                    sizeof(long));
5879    if (gmv_meshdata.facetoverts == NULL) gmvrdmemerr2();
5880    gmv_meshdata.faceverts = (long *)malloc(nv*sizeof(long));
5881    if (gmv_meshdata.faceverts == NULL) gmvrdmemerr2();
5882    gmv_meshdata.facecell1 = (long *)malloc(nf*sizeof(long));
5883    gmv_meshdata.facecell2 = (long *)malloc(nf*sizeof(long));
5884    if (gmv_meshdata.facecell1 == NULL ||
5885        gmv_meshdata.facecell2 == NULL)
5886       gmvrdmemerr2();
5887    gmv_meshdata.vfacepe = (long *)malloc(nf*sizeof(long));
5888    gmv_meshdata.vfaceoppface = (long *)malloc(nf*sizeof(long));
5889    gmv_meshdata.vfaceoppfacepe = (long *)malloc(nf*sizeof(long));
5890    if (gmv_meshdata.vfacepe == NULL ||
5891        gmv_meshdata.vfaceoppface == NULL ||
5892        gmv_meshdata.vfaceoppfacepe == NULL)
5893       gmvrdmemerr2();
5894 
5895    /*  Loop over the number of cells, and fill gmvmesh_data.  */
5896    for (icell = 0; icell < nc; icell++)
5897      {
5898 
5899       /*  Determine cell vertices.  */
5900       izc = icell / nxyc;
5901       ixy = icell - izc * nxyc;
5902       iyc = ixy / nxc;
5903       ixc = ixy - iyc * nxc;
5904 
5905       itemp[0] = ixc + iyc*nxv + izc*nxyv + 1;
5906       itemp[1] = itemp[0] + 1;
5907       itemp[2] = itemp[1] + nxv;
5908       itemp[3] = itemp[0] + nxv;
5909       itemp[4] = itemp[0] + nxyv;
5910       itemp[5] = itemp[1] + nxyv;
5911       itemp[6] = itemp[2] + nxyv;
5912       itemp[7] = itemp[3] + nxyv;
5913 
5914       /*  Determine cell faces.  */
5915       kf = nfaces * icell;
5916       gmv_meshdata.celltoface[icell] = nfaces * icell;
5917       for (ii = 0; ii < nfaces; ii++)
5918         {
5919          jf = kf + ii;
5920          gmv_meshdata.cellfaces[jf] = jf;
5921          kv = jf * nfv;
5922          gmv_meshdata.facetoverts[jf] = kv;
5923          for (j = 0; j < nfv; j++)
5924            {
5925 
5926             /*  Determine face vertices.  */
5927             kk = (ii*nfv) + j;
5928             jj = hexfverts[kk] - 1;
5929             if (struct2dflag == 1) jj = quadfverts[kk] - 1;
5930             gmv_meshdata.faceverts[kv+j] = itemp[jj];
5931            }
5932          gmv_meshdata.facecell1[jf] = icell + 1;
5933          jj = ii;
5934          if (struct2dflag == 1) jj = ii + 1;
5935          switch (jj)
5936            {
5937             case 0: icell2 = icell - nxyc;
5938                     gmv_meshdata.facecell2[jf] = icell2 + 1;
5939                     gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 5;
5940                     if (izc == 0)
5941                        gmv_meshdata.facecell2[jf] = 0;
5942                     break;
5943             case 1: icell2 = icell - nxc;
5944                     gmv_meshdata.facecell2[jf] = icell2 + 1;
5945                     gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 3;
5946                     if (struct2dflag == 1)
5947                        gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 2;
5948                     if (iyc == 0)
5949                        gmv_meshdata.facecell2[jf] = 0;
5950                     break;
5951             case 2: icell2 = icell + 1;
5952                     gmv_meshdata.facecell2[jf] = icell2 + 1;
5953                     gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 4;
5954                     if (struct2dflag == 1)
5955                        gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 3;
5956                     if (ixc+1 == nxc)
5957                        gmv_meshdata.facecell2[jf] = 0;
5958                     break;
5959             case 3: icell2 = icell + nxc;
5960                     gmv_meshdata.facecell2[jf] = icell2 + 1;
5961                     gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 1;
5962                     if (struct2dflag == 1)
5963                        gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 0;
5964                     if (iyc+1 == nyc)
5965                        gmv_meshdata.facecell2[jf] = 0;
5966                     break;
5967             case 4: icell2 = icell - 1;
5968                     gmv_meshdata.facecell2[jf] = icell2 + 1;
5969                     gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 2;
5970                     if (struct2dflag == 1)
5971                        gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 1;
5972                     if (ixc == 0)
5973                        gmv_meshdata.facecell2[jf] = 0;
5974                     break;
5975             case 5: icell2 = icell + nxyc;
5976                     gmv_meshdata.facecell2[jf] = icell + 12;
5977                     gmv_meshdata.vfaceoppface[jf] = (icell2 * nfaces) + 0;
5978                     if (icell+nxyc >= nc)
5979                        gmv_meshdata.facecell2[jf] = 0;
5980                     break;
5981            }
5982          if (gmv_meshdata.facecell2[jf] == 0)
5983             gmv_meshdata.vfaceoppface[jf] = -1;
5984          gmv_meshdata.vfacepe[jf] = 0;
5985          gmv_meshdata.vfaceoppfacepe[jf] = 0;
5986         }
5987      }
5988    gmv_meshdata.celltoface[nc] = nf;
5989    gmv_meshdata.cellfaces[nf] = nf;
5990    gmv_meshdata.facetoverts[nf] = nv;
5991 }
5992 
5993 
struct2face()5994 void struct2face()
5995 {
5996   int nxv, nyv, nzv, nxyv, nxc, nyc, nxyc, itemp[8], izc, ixy, iyc, ixc,
5997       nc, nf, nv, struct2dflag, nfaces, nfv, kf, jf, icell, icell2, ii, jj,
5998       kk, kv, j, totf, lf, i;
5999   int nfxs, nfys, nxfaces, nyfaces,
6000       xface, yface, zface, iface[6];
6001   short hexfverts[24] = {1,4,3,2, 1,2,6,5, 2,3,7,6, 3,4,8,7,
6002                          4,1,5,8, 5,6,7,8};
6003 
6004    /*  Generate a set of vface data from a structured grid.  */
6005    nxv = gmv_meshdata.nxv;
6006    nyv = gmv_meshdata.nyv;
6007    nzv = gmv_meshdata.nzv;
6008 
6009    /*  Check for 2D structured (nzv = 1). */
6010    gmv_meshdata.intype = FACES;
6011    struct2dflag = 0;
6012    if (nzv == 1)
6013       struct2dflag = 1;
6014 
6015    /*  Determine the number of cells, faces and vertices.  */
6016    nc = (nxv-1) * (nyv-1) * (nzv-1);
6017    nf = (nxv*(nyv-1))*(nzv-1) + (nyv*(nxv-1))*(nzv-1) + (nxv-1)*(nyv-1)*nzv;
6018    totf = nc * 6;
6019    nfaces = 6;
6020    nv = nf * 4;
6021    nfv = 4;
6022    if (struct2dflag == 1)
6023      {
6024       nc = (nxv-1) * (nyv-1);
6025       nfaces = 1;
6026      }
6027    gmv_meshdata.ncells = nc;
6028    gmv_meshdata.nfaces = nf;
6029    gmv_meshdata.totfaces = totf;
6030    gmv_meshdata.totverts = nv;
6031 
6032    /*  Set boundary info.  */
6033    nxyv = nxv * nyv;
6034    nxc = nxv - 1;
6035    nyc = nyv - 1;
6036    nxyc = nxc * nyc;
6037 
6038    /*  Allocate memory needed to fill mesh data.  */
6039    gmv_meshdata.celltoface = (long *)malloc((nc+1)*sizeof(long));
6040    if (gmv_meshdata.celltoface == NULL) gmvrdmemerr2();
6041    gmv_meshdata.cellfaces = (long *)malloc((totf+1)*sizeof(long));
6042    if (gmv_meshdata.cellfaces == NULL) gmvrdmemerr2();
6043    gmv_meshdata.facetoverts = (long *)malloc((nf+1)*
6044                                                    sizeof(long));
6045    if (gmv_meshdata.facetoverts == NULL) gmvrdmemerr2();
6046    gmv_meshdata.faceverts = (long *)malloc(nv*sizeof(long));
6047    if (gmv_meshdata.faceverts == NULL) gmvrdmemerr2();
6048    gmv_meshdata.facecell1 = (long *)malloc(nf*sizeof(long));
6049    gmv_meshdata.facecell2 = (long *)malloc(nf*sizeof(long));
6050    if (gmv_meshdata.facecell1 == NULL ||
6051        gmv_meshdata.facecell2 == NULL)
6052       gmvrdmemerr2();
6053    for (i = 0; i < nf; i++)
6054       {
6055        gmv_meshdata.facecell1[i] = -1;
6056        gmv_meshdata.facecell2[i] = -1;
6057       }
6058 
6059    /*  Loop over the number of cells, and fill gmvmesh_data.  */
6060    for (icell = 0; icell < nc; icell++)
6061      {
6062 
6063       /*  Determine cell vertices.  */
6064       izc = icell / nxyc;
6065       ixy = icell - izc * nxyc;
6066       iyc = ixy / nxc;
6067       ixc = ixy - iyc * nxc;
6068 
6069       itemp[0] = ixc + iyc*nxv + izc*nxyv + 1;
6070       itemp[1] = itemp[0] + 1;
6071       itemp[2] = itemp[1] + nxv;
6072       itemp[3] = itemp[0] + nxv;
6073       itemp[4] = itemp[0] + nxyv;
6074       itemp[5] = itemp[1] + nxyv;
6075       itemp[6] = itemp[2] + nxyv;
6076       itemp[7] = itemp[3] + nxyv;
6077 
6078       /*  Determine cell faces.  */
6079       nfxs = nxv * (nyv-1);
6080       nfys = nyv * (nxv-1);
6081       nxfaces = nfxs * (nzv-1);
6082       nyfaces = nfys * (nzv-1);
6083       xface = iyc*nxv + ixc + nfxs*izc;
6084       yface = iyc*nxc + ixc + nfys*izc + nxfaces;
6085       zface = icell + nxfaces + nyfaces;
6086       iface[0] = zface;
6087       iface[1] = yface;
6088       iface[2] = xface + 1;
6089       iface[3] = yface + nxc;
6090       iface[4] = xface;
6091       iface[5] = zface + nxyc;
6092       kf = nfaces * icell;
6093       gmv_meshdata.celltoface[icell] = nfaces * icell;
6094       for (ii = 0; ii < nfaces; ii++)
6095         {
6096          lf = iface[ii];
6097          jf = kf + ii;
6098          gmv_meshdata.cellfaces[jf] = lf;
6099          kv = lf * nfv;
6100 
6101          /*  See if this face has been filled.  */
6102          if (gmv_meshdata.facecell1[lf] >= 0) continue;
6103 
6104          gmv_meshdata.facetoverts[lf] = kv;
6105          for (j = 0; j < nfv; j++)
6106            {
6107 
6108             /*  Determine face vertices.  */
6109             kk = (ii*nfv) + j;
6110             jj = hexfverts[kk] - 1;
6111             gmv_meshdata.faceverts[kv+j] = itemp[jj];
6112            }
6113          gmv_meshdata.facecell1[lf] = icell + 1;
6114          switch (ii)
6115            {
6116             case 0: icell2 = icell - nxyc;
6117                     gmv_meshdata.facecell2[lf] = icell2 + 1;
6118                     if (izc == 0)
6119                        gmv_meshdata.facecell2[lf] = 0;
6120                     break;
6121             case 1: icell2 = icell - nxc;
6122                     gmv_meshdata.facecell2[lf] = icell2 + 1;
6123                     if (iyc == 0)
6124                        gmv_meshdata.facecell2[lf] = 0;
6125                     break;
6126             case 2: icell2 = icell + 1;
6127                     gmv_meshdata.facecell2[lf] = icell2 + 1;
6128                     if (ixc+1 == nxc)
6129                        gmv_meshdata.facecell2[lf] = 0;
6130                     break;
6131             case 3: icell2 = icell + nxc;
6132                     gmv_meshdata.facecell2[lf] = icell2 + 1;
6133                     if (iyc+1 == nyc)
6134                        gmv_meshdata.facecell2[lf] = 0;
6135                     break;
6136             case 4: icell2 = icell - 1;
6137                     if (ixc == 0)
6138                        gmv_meshdata.facecell2[lf] = 0;
6139                     break;
6140             case 5: icell2 = icell + nxyc;
6141                     gmv_meshdata.facecell2[lf] = icell + 12;
6142                     if (icell+nxyc >= nc)
6143                        gmv_meshdata.facecell2[lf] = 0;
6144                     break;
6145            }
6146         }
6147      }
6148    gmv_meshdata.celltoface[nc] = totf;
6149    gmv_meshdata.cellfaces[totf] = nf;
6150    gmv_meshdata.facetoverts[nf] = nv;
6151 }
6152 
6153 
6154 
6155 
6156 
6157 
6158 
6159 
6160 
6161 
6162 
6163 
6164 
6165 
6166 
6167 
6168