1 /* --------------------------------------------------------------------  */
2 /*                          CALCULIX                                     */
3 /*                   - GRAPHICAL INTERFACE -                             */
4 /*                                                                       */
5 /*     A 3-dimensional pre- and post-processor for finite elements       */
6 /*              Copyright (C) 1996 Klaus Wittig                          */
7 /*                                                                       */
8 /*     This program is free software; you can redistribute it and/or     */
9 /*     modify it under the terms of the GNU General Public License as    */
10 /*     published by the Free Software Foundation; version 2 of           */
11 /*     the License.                                                      */
12 /*                                                                       */
13 /*     This program is distributed in the hope that it will be useful,   */
14 /*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */
15 /*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
16 /*     GNU General Public License for more details.                      */
17 /*                                                                       */
18 /*     You should have received a copy of the GNU General Public License */
19 /*     along with this program; if not, write to the Free Software       */
20 /*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
21 /* --------------------------------------------------------------------  */
22 
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <pthread.h>
28 #include "CalculiX.h"
29 
30 #include <stdlib.h>
31 #include <math.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include "readfrd.h"
36 
37 #define TEST     0
38 
39 #define INI_FIELD_SIZE 1000000
40 
41 
42 /* ToDo:
43 */
44 
45 
freeDatasets(Datasets * lcase,int nr)46 void freeDatasets(Datasets *lcase, int nr)
47 {
48   register int i;
49 
50   printf(" free lc[%d] ncomps:%d\n",nr,lcase[nr].ncomps);
51   if(lcase[nr].loaded)
52   {
53     for(i=0; i<lcase[nr].ncomps; i++) SFREE(lcase[nr].dat[i]);
54   }
55   /* always allocated */
56   SFREE(lcase[nr].dat);
57   lcase[nr].dat=NULL;
58 
59   if(lcase[nr].npheader)
60   {
61     for(i=0; i<lcase[nr].npheader; i++) SFREE(lcase[nr].pheader[i]);
62     SFREE(lcase[nr].pheader);
63     lcase[nr].pheader=NULL;
64   }
65   for(i=0; i<lcase[nr].ncomps; i++)
66   {
67     SFREE(lcase[nr].compName[i]);
68     SFREE(lcase[nr].icname[i]);
69   }
70   /* always allocated */
71   SFREE(lcase[nr].compName);
72   SFREE(lcase[nr].icname);
73   lcase[nr].compName=NULL;
74   lcase[nr].icname=NULL;
75 
76   SFREE(lcase[nr].ictype);
77   SFREE(lcase[nr].icind1);
78   SFREE(lcase[nr].icind2);
79   SFREE(lcase[nr].iexist);
80   SFREE(lcase[nr].max);
81   SFREE(lcase[nr].menu);
82   SFREE(lcase[nr].min);
83   SFREE(lcase[nr].nmax);
84   SFREE(lcase[nr].nmin);
85   SFREE(lcase[nr].fileptr);
86   lcase[nr].fileptr=NULL;
87   lcase[nr].loaded=0;
88 
89   /* edat not propper implemented or deleted */
90   // for(i=0; i<3; i++) for(e=0; e<anz->e; e++) SFREE(lcase[nr].edat[i][e]);
91 }
92 
93 
94 /* read_mode=0: jump '100C' data-blocks and read them later on demand */
95 /* in any case for each results of a new step the first data-block is readed to see how much nodes are included */
96 
readfrd(char * datin,Summen * anz,Nodes ** nptr,Elements ** eptr,Datasets ** lptr,int read_mode)97 int readfrd( char *datin, Summen *anz, Nodes **nptr, Elements **eptr, Datasets **lptr, int read_mode )
98 {
99   FILE *handle;
100   int i=0, j=0;
101   int  nodeflag=0, elemflag=0, errFlag=0, firsttime=1;
102   int n;  /* used in format_flag */
103   long offset=0;
104   fpos_t *filepntr=NULL;
105   int elem_data=0,nod_data=0, nod_1st_block=0; /* nodes in resultblock, nodes in 1st block (if no "nr of nodes" are given in frd file, 100C-line) */
106 
107   int  ncomps, indx, maxcomps=0, nvals, nentities;
108   char rec_str[MAX_LINE_LENGTH];
109   int  node_field_size, elem_field_size;
110   int  e_nmax=1, e_nmin=1;
111   int  length, flag, format_flag;
112   int  ipuf, nodenr=0;
113   static float *value=NULL;
114   static double *dvalue=NULL;
115 
116   char **dat, **compName;
117   int          *menu, *ictype, *icind1, *icind2, *iexist;
118 
119   int anz_p=-1;
120   char **pheader=NULL;
121 
122   Nodes     *node=NULL;
123   Elements  *elem=NULL;
124   Datasets  *lcase=NULL;
125 
126 
127   if ( (lcase = (Datasets *)malloc( 1 * sizeof(Datasets))) == NULL )
128     printf("\n\n ERROR: malloc failed\n\n") ;
129 
130   anz->u=anz->n=anz->e=anz->l=-1;
131   anz->nmax=0;  anz->nmin=MAX_INTEGER;
132   anz->emax=0;  anz->emin=MAX_INTEGER;
133   length = 1;
134   format_flag=0;
135 
136   /* Open the files and check to see that it was opened correctly */
137   handle = fopen (datin, "rb");
138   if ( handle== NULL )  { printf ("ERROR in readfrd: The input file \"%s\" could not be opened.\n\n", datin); return(-1); }
139   else  printf (" file:%s opened\n", datin);
140 
141 
142   printf (" reading frd format\n");
143   length = frecord( handle, rec_str);
144   rec_str[length]='\0';
145   flag = stoi(rec_str,4,5);
146   if (flag == 1 )
147   {
148     stos(rec_str,7,12,anz->model);
149     printf (" MODEL NAME:  %s", anz->model);
150   }
151   else
152   {
153     printf ("\n\nFATAL ERROR: no proper file-format found.\n\n");
154     return (-1);
155   }
156 
157   while(length)
158   {
159 
160     /* store the beginning of the data-block for later reading */
161     if(filepntr==NULL)
162     {  if( (filepntr=(fpos_t *)malloc(1*sizeof(fpos_t))) == NULL ) printf(" ERROR: malloc failed\n"); }
163 
164     if(fgetpos( handle, (fpos_t *)filepntr)!=0) { printf("error in fgetpos"); return(-1); }
165 
166     read_again:;
167     length = frecord( handle, rec_str);
168     if (rec_str[length] == (char)EOF) break;
169     else rec_str[length] =(char)0;
170     printf ("record:%s\n", rec_str);
171 
172 
173     flag = stoi(rec_str,1,5);
174     format_flag = stoi(rec_str,74,75);
175     //printf ("OPCODE:%d IFORMT:%d\n", flag, format_flag );
176 
177     if(flag == 9999) goto read_again;
178     if(( (nodeflag==1)&&(flag == 2) ) || ( (elemflag==1)&&(flag == 3) ))
179     {
180       printf ("found a second mesh. This mesh will be ignored\n");
181       if(format_flag < 2) flag=-1;
182       else flag*=-1;
183     }
184     if(flag == 1)
185     {
186       /* User Header used to store general information */
187       if(rec_str[5]=='U')
188       {
189         anz->u++;
190         if(!anz->u)
191         { if(( anz->uheader=(char **)malloc( sizeof(char *))) == NULL )
192           printf("\n\n ERROR: malloc failed\n\n") ; }
193         else if(( anz->uheader=(char **)realloc((char **)anz->uheader, (anz->u+1)*sizeof(char *))) == NULL )
194           printf("\n\n ERROR: realloc failed\n\n") ;
195         if(( anz->uheader[anz->u]=(char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
196           printf("\n\n ERROR: malloc failed\n\n") ;
197         strcpy(anz->uheader[anz->u],rec_str);
198       }
199 
200       /* Project Header used to store additional Dataset information */
201       if(rec_str[5]=='P')
202       {
203         anz_p++;
204         if(!anz_p)
205         { if(( pheader=(char **)malloc( sizeof(char *))) == NULL )
206           printf("\n\n ERROR: malloc failed\n\n") ; }
207         else if(( pheader=(char **)realloc((char **)pheader, (anz_p+1)*sizeof(char *))) == NULL )
208           printf("\n\n ERROR: realloc failed\n\n") ;
209         if(( pheader[anz_p]=(char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
210           printf("\n\n ERROR: malloc failed\n\n") ;
211         strcpy(pheader[anz_p],rec_str);
212       }
213     }
214 
215     else if(flag == 2)
216     {
217       /* store the pheaders which are leading this block */
218       anz->p=anz_p+1;
219       anz->pheader=pheader;
220       anz_p=-1;
221       pheader=NULL;
222 
223       printf ("reading Nodes\n");
224       nodeflag=1;
225 
226       /* nr of nodes per block can be read from the frd file, this is not documented in the original frd-spec. */
227       nod_data=stoi( rec_str, 25, 36 );
228       if(nod_data>0) node_field_size=nod_data;
229       else node_field_size=INI_FIELD_SIZE;
230       do
231       {
232         if ( (node = (Nodes *)realloc( (Nodes *)node, (node_field_size+1) * sizeof(Nodes))) == NULL )
233         {
234           printf("WARNING: in readfrd() is INI_FIELD_SIZE:%d to large and is reduced\n", node_field_size );
235           node_field_size/=2;
236         }
237         if(node_field_size<0)
238         {
239           printf("\n\n ERROR: not enough memory in readfrd()\n\n");
240           exit(-1);
241         }
242       }while(!node);
243       for(i=0; i<node_field_size; i++) node[i].indx=-1;
244 
245       if (format_flag < 2)
246       {
247        do
248        {
249         length = frecord( handle, rec_str);
250         if (rec_str[length] == (char)EOF) break;
251         flag = stoi(rec_str,1,3);
252         anz->n++;
253         if (flag == -3) break;
254         if (!format_flag) node[anz->n].nr = stoi(rec_str,4,8);
255         else              node[anz->n].nr = stoi(rec_str,4,13);
256         if (node[anz->n].nr>=node_field_size)
257 	{
258           if(node[anz->n].nr<MAX_INTEGER/2) node_field_size=node[anz->n].nr*2+1; else node_field_size=MAX_INTEGER-2;
259           nodenr=node[anz->n].nr;
260           do
261           {
262             if ( (node = (Nodes *)realloc( (Nodes *)node, (node_field_size+1) * sizeof(Nodes))) == NULL )
263             {
264               printf("WARNING: in readfrd() is INI_FIELD_SIZE:%d to large and is reduced\n", node_field_size );
265               node_field_size=nodenr+(node_field_size-nodenr)/2;
266             }
267             if(node_field_size<=nodenr)
268             {
269               printf("\n\n ERROR: not enough memory in readfrd() for node-nr:%d available\n\n", nodenr);
270               exit(-1);
271             }
272           }while(!node);
273           for(i=anz->nmax+1; i<node_field_size; i++) node[i].indx=-1;
274         }
275         /* save only nodes which are not already stored */
276         if(node[node[anz->n].nr].indx<0)
277         {
278           node[node[anz->n].nr].indx=anz->n;
279           if (!format_flag)
280           {
281             node[node[anz->n].nr].nx = stof(&rec_str[8],1,12);
282             node[node[anz->n].nr].ny = stof(&rec_str[20],1,12);
283             node[node[anz->n].nr].nz = stof(&rec_str[32],1,12);
284           }
285           else
286           {
287             node[node[anz->n].nr].nx = stof(&rec_str[13],1,12);
288             node[node[anz->n].nr].ny = stof(&rec_str[25],1,12);
289             node[node[anz->n].nr].nz = stof(&rec_str[37],1,12);
290           }
291           if (node[anz->n].nr >  anz->nmax)  anz->nmax=node[anz->n].nr;
292           if (node[anz->n].nr <  anz->nmin)  anz->nmin=node[anz->n].nr;
293 #if TEST
294         printf (" n=%d x=%lf y=%lf z=%lf \n",  node[anz->n].nr,
295           node[node[anz->n].nr].nx, node[node[anz->n].nr].ny,
296           node[node[anz->n].nr].nz);
297 #endif
298         }
299        } while(flag != -3);
300       }
301 
302       /* binary format */
303       else
304       {
305        if ( (value = (float *)realloc((float *)value, (3) * sizeof(float))) == NULL )
306          printf("\n\n ERROR: realloc failed, value\n\n") ;
307        for(i=0; i<nod_data; i++)
308        {
309         anz->n++;
310         length=fread((int *)&node[anz->n].nr,sizeof(int),1,handle);
311 	//printf("n:%d\n", node[anz->n].nr);
312         if (node[anz->n].nr>=node_field_size)
313 	{
314           if(node[anz->n].nr<MAX_INTEGER/2) node_field_size=node[anz->n].nr*2+1; else node_field_size=MAX_INTEGER-2;
315           nodenr= node[anz->n].nr;
316           do
317           {
318             if ( (node = (Nodes *)realloc( (Nodes *)node, (node_field_size+1) * sizeof(Nodes))) == NULL )
319             {
320               printf("WARNING: in readfrd() is INI_FIELD_SIZE:%d to large and is reduced\n", node_field_size );
321               node_field_size=nodenr+(node_field_size-nodenr)/2;
322             }
323             if(node_field_size<=nodenr)
324             {
325               printf("\n\n ERROR: not enough memory in readfrd() for the node-nr:%d available\n\n", nodenr);
326               exit(-1);
327             }
328           }while(!node);
329           for(n=anz->nmax+1; n<node_field_size; n++) node[n].indx=-1;
330         }
331         /* save only nodes which are not already stored */
332         if (format_flag == 2)
333 	{
334           length=fread((float *)value,sizeof(float),3,handle);
335 	  //printf("n:%f %f %f\n", value[0],value[1],value[2]);
336           if(node[node[anz->n].nr].indx<0)
337           {
338             node[node[anz->n].nr].indx=anz->n;
339 	    node[node[anz->n].nr].nx = value[0];
340             node[node[anz->n].nr].ny = value[1];
341             node[node[anz->n].nr].nz = value[2];
342           }
343           else fseeko(handle, 3*sizeof(float), SEEK_CUR);
344 	}
345         else
346 	{
347           if(node[node[anz->n].nr].indx<0)
348           {
349             length=fread((double *)&node[node[anz->n].nr].nx,sizeof(double),3,handle);
350             node[node[anz->n].nr].indx=anz->n;
351           }
352           else fseeko(handle, 3*sizeof(double), SEEK_CUR);
353 	}
354         if (node[anz->n].nr >  anz->nmax)  anz->nmax=node[anz->n].nr;
355         if (node[anz->n].nr <  anz->nmin)  anz->nmin=node[anz->n].nr;
356 #if TEST
357         printf (" n=%d x=%lf y=%lf z=%lf \n",  node[anz->n].nr,
358           node[node[anz->n].nr].nx, node[node[anz->n].nr].ny,
359           node[node[anz->n].nr].nz);
360 #endif
361        }
362        anz->n++;
363       }
364       node_field_size=anz->nmax+1;
365       if((node = (Nodes *)realloc( (Nodes *)node, node_field_size * sizeof(Nodes))) == NULL )
366         printf("\n\n ERROR: realloc failed\n\n") ;
367       else
368         printf ("\n %d nodes reallocated \n",anz->nmax);
369       //nod_1st_block=anz->n;
370     }
371 
372     else if(flag == 3)
373     {
374       printf ("reading Elements\n");
375       // anz->emax=-MAX_INTEGER;  anz->emin=MAX_INTEGER;
376       elemflag=1;
377       e_nmax=-MAX_INTEGER;  e_nmin=MAX_INTEGER;
378 
379       /* nr of elems per block can be read from the frd file, this is not documented in the original frd-spec. */
380       elem_data=stoi( rec_str, 25, 36 );
381       if(elem_data>0) elem_field_size=elem_data;
382       else elem_field_size=INI_FIELD_SIZE;
383       do
384       {
385         if((elem = (Elements *)realloc( (Elements *)elem, (elem_field_size+1) * sizeof(Elements))) == NULL )
386         {
387           printf("WARNING: in readfrd() is INI_FIELD_SIZE:%d to large and is reduced\n", elem_field_size );
388           elem_field_size/=2;
389         }
390         if(elem_field_size<0)
391         {
392           printf("\n\n ERROR: not enough memory in readfrd()\n\n");
393           exit(-1);
394         }
395       }while(!elem);
396 
397       /* binary format */
398       if (format_flag == 2)
399       {
400         if ( (elem = (Elements *)realloc((Elements *)elem, elem_data * sizeof(Elements))) == NULL )
401           printf("\n\n ERROR: in readfrd realloc failed\n\n") ;
402         else
403           printf ("\n %d elements allocated \n", elem_data);
404         for (i=0; i<elem_data; i++)
405         {
406           anz->e++;
407           length=fread((int *)&elem[anz->e].nr,sizeof(int),1,handle);
408           length=fread((int *)&elem[anz->e].type,sizeof(int),1,handle);
409           length=fread((int *)&elem[anz->e].group,sizeof(int),1,handle);
410           length=fread((int *)&elem[anz->e].mat,sizeof(int),1,handle);
411 	  elem[anz->e].attr = 0;
412           anz->etype[elem[anz->e].type]++;
413           if (elem[anz->e].nr >  anz->emax)  anz->emax=elem[anz->e].nr;
414           if (elem[anz->e].nr <  anz->emin)  anz->emin=elem[anz->e].nr;
415           if (elem[anz->e].type == 1)      ipuf = 8;   /* HEXA8  */
416           else if (elem[anz->e].type == 2) ipuf = 6;   /* PE6   */
417           else if (elem[anz->e].type == 3) ipuf = 4;   /* TET4   */
418           else if (elem[anz->e].type == 4) ipuf = 20;  /* HEXA20 */
419           else if (elem[anz->e].type == 5) ipuf = 15;  /* PE15  */
420           else if (elem[anz->e].type == 6) ipuf = 10;  /* TET10  */
421           else if (elem[anz->e].type == 7) ipuf = 3;   /* TRI3   */
422           else if (elem[anz->e].type == 8) ipuf = 6;   /* TRI6   */
423           else if (elem[anz->e].type == 9) ipuf = 4;   /* QUAD4  */
424           else if (elem[anz->e].type == 10) ipuf = 8; /* QUAD8  */
425           else if (elem[anz->e].type == 11) ipuf = 2;  /* BEAM2   */
426           else if (elem[anz->e].type == 12) ipuf = 3;  /* BEAM3   */
427 	  //printf("el:%d t:%d g:%d m:%d n:%d\n", elem[anz->e].nr, elem[anz->e].type,elem[anz->e].group,elem[anz->e].mat, ipuf);
428           length=fread((int *)elem[anz->e].nod,sizeof(int),ipuf,handle);
429 	  //for(j=0;j<ipuf; j++) printf(" %d",elem[anz->e].nod[j]); printf("\n");
430         }
431         anz->e++;
432       }
433       else
434       {
435        do
436        {
437         length = frecord( handle, rec_str);
438         if (rec_str[length] == (char)EOF) break;
439         flag = stoi(rec_str,1,3);
440         anz->e++;
441 
442         if (flag == -3) break;
443         else if ((flag == -1)||(flag == -2))
444         {
445           if (anz->e>=elem_field_size)
446           {
447             if(anz->e<MAX_INTEGER/2) elem_field_size=anz->e*2+1; else elem_field_size=MAX_INTEGER-2;
448             do
449             {
450               if((elem = (Elements *)realloc( (Elements *)elem, (elem_field_size+1) * sizeof(Elements))) == NULL )
451               {
452                 printf("WARNING: in readfrd() is INI_FIELD_SIZE:%d to large and is reduced\n", elem_field_size );
453                 elem_field_size=anz->e+(elem_field_size-anz->e)/2;
454               }
455               if(elem_field_size<=anz->e)
456               {
457                 printf("\n\n ERROR: not enough memory in readfrd()\n\n");
458                 exit(-1);
459               }
460             }while(!elem);
461           }
462           if (!format_flag)
463           {
464 	    elem[anz->e].nr = stoi(&rec_str[3], 1, 5);
465 	    elem[anz->e].type = stoi(&rec_str[8], 1, 5);
466 	    elem[anz->e].group = stoi(&rec_str[13], 1, 5);
467 	    elem[anz->e].mat = stoi(&rec_str[18], 1, 5);
468           }
469           else
470           {
471 	    elem[anz->e].nr = stoi(&rec_str[3], 1, 10);
472 	    elem[anz->e].type = stoi(&rec_str[13], 1, 5);
473 	    elem[anz->e].group = stoi(&rec_str[18], 1, 5);
474 	    elem[anz->e].mat = stoi(&rec_str[23], 1, 5);
475           }
476 	  elem[anz->e].attr = 0;
477           ipuf=0;
478           if (elem[anz->e].nr >  anz->emax)  anz->emax=elem[anz->e].nr;
479           if (elem[anz->e].nr <  anz->emin)  anz->emin=elem[anz->e].nr;
480           if (elem[anz->e].type == 1)      ipuf = 8;   /* HEXA8  */
481           else if (elem[anz->e].type == 2) ipuf = 6;   /* PE6   */
482           else if (elem[anz->e].type == 3) ipuf = 4;   /* TET4   */
483           else if (elem[anz->e].type == 4) ipuf = 20;  /* HEXA20 */
484           else if (elem[anz->e].type == 5) ipuf = 15;  /* PE15  */
485           else if (elem[anz->e].type == 6) ipuf = 10;  /* TET10  */
486           else if (elem[anz->e].type == 7) ipuf = 3;   /* TRI3   */
487           else if (elem[anz->e].type == 8) ipuf = 6;   /* TRI6   */
488           else if (elem[anz->e].type == 9) ipuf = 4;   /* QUAD4  */
489           else if (elem[anz->e].type == 10) ipuf = 8; /* QUAD8  */
490           else if (elem[anz->e].type == 11) ipuf = 2;  /* BEAM2   */
491           else if (elem[anz->e].type == 12) ipuf = 3;  /* BEAM3   */
492 #if TEST
493           printf ("\n%d e=%d typ=%d grp=%d mat=%d \n", flag, elem[anz->e].nr,
494                     elem[anz->e].type, elem[anz->e].group, elem[anz->e].mat );
495 #endif
496           length = frecord( handle, rec_str );
497           if (ipuf==0)
498           {
499             printf (" element:%d is from unknown type:%d\n", elem[anz->e].nr, elem[anz->e].type);
500           }
501           else
502           {
503             anz->etype[elem[anz->e].type]++;
504             /* read the node-lines */
505             if (!format_flag)
506             {
507               j=0;
508               for (i=0; i<ipuf; i++)
509               {
510 		elem[anz->e].nod[i] = stoi(&rec_str[3+j*5], 1, 5);
511                 if (j<14) j++;
512                 else
513                 {
514                   if (i<ipuf-1) length = frecord( handle, rec_str ); j=0;
515                 }
516               }
517             }
518             else
519             {
520               j=0;
521               for (i=0; i<ipuf; i++)
522               {
523                 elem[anz->e].nod[i] = stoi(&rec_str[3+j*10], 1, 10);
524                 if (j<9) j++;
525                 else
526                 {
527                   if (i<ipuf-1) length = frecord( handle, rec_str ); j=0;
528                 }
529               }
530             }
531           }
532         }
533         else
534         {
535           printf ("ERROR: flag:%d is not expected, must be -1 or -2!\n%s", flag, rec_str );
536           exit(-1);
537         }
538        } while(flag != -3);
539        elem_field_size=anz->e+1;
540        if ( (elem = (Elements *)realloc((Elements *)elem, elem_field_size * sizeof(Elements))) == NULL )
541          printf("\n\n ERROR: in readfrd realloc failed\n\n") ;
542        else
543          printf ("\n %d elements reallocated \n", anz->e);
544       }
545     }
546 
547     else if(flag == 100)
548     {
549       anz->l++;
550 
551       printf ("reading Dataset No:%d\n",anz->l+1);
552       if ( (lcase = (Datasets *)realloc((Datasets *)lcase, (anz->l+2) * sizeof(Datasets))) == NULL )
553       { printf("\n\n ERROR: malloc failure\n\n" ); exit(1); }
554 
555       lcase[anz->l].handle=(FILE *)NULL;
556 
557       /* store the pheaders which are leading this block */
558       lcase[anz->l].npheader=anz_p+1;
559       lcase[anz->l].pheader=pheader;
560       lcase[anz->l].fileptr=NULL;
561       lcase[anz->l].loaded=1;
562       lcase[anz->l].format_flag=format_flag;
563       anz_p=-1;
564       pheader=NULL;
565       offset=0;
566 
567       stos( rec_str, 7, 12, lcase[anz->l].dataset_name);
568       lcase[anz->l].value=stof( rec_str, 13, 24 );
569 
570       /* nr of nodes per block can be read from the frd file, this is not documented in the original frd-spec. */
571       nod_data=stoi( rec_str, 25, 36 );
572       /* because of a bug in ccx2.0 this nr can be wrong. In this case it is higher than the actual nr of nodes. */
573       if(nod_data>anz->n)
574       {
575         printf(" WARNING: in this result-block are more nodes announced:%d than in the model defined:%d\n Please inform the program-admin of the originator of the frd-file\n\n", nod_data, anz->n);
576         //exit(0);
577         nod_data=anz->n;
578       }
579 #ifdef DEVEL
580       if(!nod_data)
581       {
582         nod_data=nod_1st_block;
583         printf("nods in block assumed:%d\n",nod_data );
584       }
585 #endif
586       stos( rec_str, 37, 56, lcase[anz->l].dataset_text);
587       lcase[anz->l].analysis_type=stoi( rec_str, 57, 58 );
588       lcase[anz->l].step_number=stoi( rec_str, 59, 63 );
589       if(strlen(rec_str)>72 )
590       {
591         stos(rec_str,64,73,lcase[anz->l].analysis_name);
592       }
593       else
594       {
595         strcpy(lcase[anz->l].analysis_name,"");
596       }
597       ncomps=nentities=indx=0;
598       if (!format_flag) n=8;
599       else n=13;
600       errFlag=0;
601       firsttime=1;
602 
603       ipuf=-1;
604       //if(lcase[anz->l].analysis_type==2) //in the moment ccx writes the wrong number, therefore:
605       if(lcase[anz->l].analysis_type>=2)
606       {
607         for(i=0;i<lcase[anz->l].npheader; i++)
608         {
609           if(compare(&lcase[anz->l].pheader[i][5],"PHID", 4)==4)
610           {
611             sscanf(lcase[anz->l].pheader[i],"%*s %d", &ipuf);
612             if(ipuf>-1) sprintf(lcase[anz->l].dataset_text,"ND:%d",ipuf);
613           }
614 	}
615         if(ipuf!=-1) for(i=0;i<lcase[anz->l].npheader; i++)
616         {
617           if(compare(&lcase[anz->l].pheader[i][5],"PMODE", 5)==5)
618           {
619             sscanf(lcase[anz->l].pheader[i],"%*s %d", &ipuf);
620             if(ipuf>-1) sprintf(&lcase[anz->l].dataset_text[strlen(lcase[anz->l].dataset_text)]," MODE:%d",ipuf);
621           }
622         }
623       }
624 
625       /* initialize */
626       lcase[anz->l].ncomps=0;
627       do
628       {
629         /* bin mode, active after last column definition was read (-5 lines). Attention flag:-6 not permitted so far! */
630         if (( format_flag>=2)&&(lcase[anz->l].ncomps>0)&&(ncomps==lcase[anz->l].ncomps))
631         {
632           //printf("format_flag=%d ncomps:%d lcncomps:%d\n",format_flag,ncomps,lcase[anz->l].ncomps);
633 
634 	  /* if offset is known jump the filepointer before the next block or else continue reading assuming values for all nodes are provided */
635 	  if(offset)
636           {
637             lcase[anz->l].loaded=0;
638 	    if (firsttime)
639 	    {
640               firsttime=0;
641 
642               /* store the beginning of the data-block for later reading */
643               lcase[anz->l].fileptr=filepntr;
644               filepntr=NULL;
645               lcase[anz->l].handle=handle;
646               strcpy(lcase[anz->l].filename,datin);
647 
648               if( fseeko( handle, offset, SEEK_CUR )!=0) printf("error in fseeko\n");
649 	    }
650 	  }
651           else if(format_flag==2)
652 	  {
653             if ( (value = (float *)realloc((float *)value, (lcase[anz->l].ncomps) * sizeof(float))) == NULL )
654               printf("\n\n ERROR: realloc failed, value\n\n") ;
655             for(n=0; n<nod_data; n++)
656             {
657               length=fread((int *)&nodenr,sizeof(int),1,handle);
658               length=fread((float *)value,sizeof(float),lcase[anz->l].ncomps,handle);
659 	      // printf("n:%d N:%d ",n+1, nodenr);
660               for(i=0; i<lcase[anz->l].ncomps; i++)
661               {
662 	        // printf(" %f",value[i]);
663                 lcase[anz->l].dat[i][nodenr]= value[i];
664               }
665               // printf("\n");
666             }
667 	  }
668           else if(format_flag==3)
669 	  {
670             if ( (dvalue = (double *)realloc((double *)dvalue, (lcase[anz->l].ncomps) * sizeof(double))) == NULL )
671               printf("\n\n ERROR: realloc failed, dvalue\n\n") ;
672             for(n=0; n<nod_data; n++)
673             {
674               length=fread((int *)&nodenr,sizeof(int),1,handle);
675               length=fread((double *)dvalue,sizeof(double),lcase[anz->l].ncomps,handle);
676 	      // printf("n:%d N:%d ",n+1, nodenr);
677               for(i=0; i<lcase[anz->l].ncomps; i++)
678               {
679 	        // printf(" %f",dvalue[i]);
680                 lcase[anz->l].dat[i][nodenr]= dvalue[i];
681               }
682               // printf("\n");
683             }
684 	  }
685           break;
686         }
687 
688         length = frecord( handle, rec_str);
689         if (rec_str[length] == (char)EOF) break;
690         flag = stoi(rec_str,1,3);
691 	//printf("flag:%d\n", flag);
692 	//printf("rec in block:%s\n", rec_str);
693 
694         if(flag == -1)
695         {
696 	  /* if offset is known jump the filepointer before the next block and continue reading until flag=-3  */
697 	  if(offset)
698           {
699             lcase[anz->l].loaded=0;
700 	    if (firsttime)
701 	    {
702               firsttime=0;
703 
704               /* store the beginning of the data-block for later reading */
705               lcase[anz->l].fileptr=filepntr;
706               filepntr=NULL;
707               lcase[anz->l].handle=handle;
708               strcpy(lcase[anz->l].filename,datin);
709 
710               /* reduce the offset by the current record */
711               if( fseeko( handle, offset-length, SEEK_CUR )!=0) printf("error in fseeko\n");
712 	    }
713 	  }
714           else
715 	  {
716             nod_1st_block++;
717             if (format_flag) nodenr = stoi(rec_str,4,13); else nodenr = stoi(rec_str,4,8);
718             if (nodenr>anz->nmax)
719             {
720               if (!errFlag) { errFlag=1; printf("WARNING: found node:%d in Dataset higher than in geometry allocated:%d\n", nodenr, anz->nmax); }
721             }
722             else if ( lcase[anz->l].irtype == 1 )
723     	    {
724               if(maxcomps==6)
725               {
726                 i=6;
727                 if ( format_flag)
728                 {
729                   lcase[anz->l].dat[0][nodenr]= stof(&rec_str[  13  ], 1, 12);
730                   lcase[anz->l].dat[1][nodenr]= stof(&rec_str[  25  ], 1, 12);
731                   lcase[anz->l].dat[2][nodenr]= stof(&rec_str[  37  ], 1, 12);
732                   lcase[anz->l].dat[3][nodenr]= stof(&rec_str[  49  ], 1, 12);
733                   lcase[anz->l].dat[4][nodenr]= stof(&rec_str[  61  ], 1, 12);
734                   lcase[anz->l].dat[5][nodenr]= stof(&rec_str[  73  ], 1, 12);
735                 }
736                 else
737                 {
738                   lcase[anz->l].dat[0][nodenr]= stof(&rec_str[  8   ], 1, 12);
739                   lcase[anz->l].dat[1][nodenr]= stof(&rec_str[  20  ], 1, 12);
740                   lcase[anz->l].dat[2][nodenr]= stof(&rec_str[  32  ], 1, 12);
741                   lcase[anz->l].dat[3][nodenr]= stof(&rec_str[  44  ], 1, 12);
742                   lcase[anz->l].dat[4][nodenr]= stof(&rec_str[  56  ], 1, 12);
743                   lcase[anz->l].dat[5][nodenr]= stof(&rec_str[  68  ], 1, 12);
744                 }
745               }
746               else
747               {
748 	        for(i=0; i<maxcomps; i++) lcase[anz->l].dat[i][nodenr]= stof(&rec_str[n+i*12], 1, 12);
749 	      }
750     	      /* printf("%d", nodenr); for (i=0; i<maxcomps; i++) printf(" %f",lcase[anz->l].dat[i][nodenr] ); printf("\n"); */
751             }
752             else i=0;
753 	  }
754 	}
755         else if(flag == -2)
756 	{
757           if (!format_flag) n=8;
758           else n=13;
759           j=0;
760 
761           /* in case the data should be read directly and not on demand */
762 	  if(!offset)
763           {
764             do
765             {
766               lcase[anz->l].dat[i][nodenr]= stof(&rec_str[n+j*12], 1, 12);
767               i++;j++;
768             }while((j<6)&&(i<lcase[anz->l].ncomps));
769 	  }
770         }
771         else if (flag == -4)
772         {
773           stos( rec_str, 6, 13, lcase[anz->l].name);
774           lcase[anz->l].ncomps = stoi(rec_str,14,18);
775           lcase[anz->l].irtype = stoi(rec_str,19,23);
776 
777           if( lcase[anz->l].irtype > 2 )
778           {
779             printf(" Found ELEMENT DATA, this is not suported!\n");
780             anz->l--;
781             goto next;
782           }
783 
784           if ( (lcase[anz->l].nmax = (int *)malloc( (lcase[anz->l].ncomps) * sizeof(int))) == NULL )
785             printf("\n\n ERROR: malloc failure\n\n" );
786           if ( (lcase[anz->l].nmin = (int *)malloc( (lcase[anz->l].ncomps) * sizeof(int))) == NULL )
787             printf("\n\n ERROR: malloc failure\n\n" );
788           if ( (lcase[anz->l].max = (CGXFLOAT *)malloc( (lcase[anz->l].ncomps) * sizeof(CGXFLOAT))) == NULL )
789             printf("\n\n ERROR: malloc failure\n\n" );
790           if ( (lcase[anz->l].min = (CGXFLOAT *)malloc( (lcase[anz->l].ncomps) * sizeof(CGXFLOAT))) == NULL )
791             printf("\n\n ERROR: malloc failure\n\n" );
792           if ( (lcase[anz->l].compName = (char **)malloc( (lcase[anz->l].ncomps) * sizeof(char *))) == NULL )
793             printf("\n\n ERROR: malloc failure\n\n" );
794           if ( (lcase[anz->l].icname = (char **)malloc( (lcase[anz->l].ncomps) * sizeof(char *))) == NULL )
795             printf("\n\n ERROR: malloc failure\n\n" );
796           if ( (lcase[anz->l].menu = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
797             printf("\n\n ERROR: malloc failure\n\n" );
798           if ( (lcase[anz->l].ictype = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
799             printf("\n\n ERROR: malloc failure\n\n" );
800           if ( (lcase[anz->l].icind1 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
801             printf("\n\n ERROR: malloc failure\n\n" );
802           if ( (lcase[anz->l].icind2 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
803             printf("\n\n ERROR: malloc failure\n\n" );
804           if ( (lcase[anz->l].iexist = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
805             printf("\n\n ERROR: malloc failure\n\n" );
806           if ( (lcase[anz->l].dat = (CGXFLOAT **)malloc( (lcase[anz->l].ncomps) * sizeof(CGXFLOAT *))) == NULL )
807             printf("\n\n ERROR: malloc failure\n\n" );
808   printf(" gen lc[%d] ncomps:%d\n",anz->l,lcase[anz->l].ncomps);
809           for(i=0; i<(lcase[anz->l].ncomps); i++)
810 	  {
811             if ( (lcase[anz->l].compName[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
812                printf("\n\n ERROR: malloc failed\n\n" );
813             if ( (lcase[anz->l].icname[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
814                printf("\n\n ERROR: malloc failed\n\n" );
815             lcase[anz->l].max[i]=-MAX_FLOAT;
816             lcase[anz->l].min[i]=MAX_FLOAT;
817 	  }
818         }
819         else if(flag == -5)
820         {
821           if(indx<lcase[anz->l].ncomps)
822 	  {
823             stos(rec_str, 6, 13, lcase[anz->l].compName[indx]);
824             lcase[anz->l].menu[indx] = stoi(rec_str,14,18);
825             lcase[anz->l].ictype[indx] = stoi(rec_str,19,23);
826             lcase[anz->l].icind1[indx] = stoi(rec_str,24,28);
827             lcase[anz->l].icind2[indx] = stoi(rec_str,29,33);
828             lcase[anz->l].iexist[indx] = stoi(rec_str,34,38);
829 
830             /* requests for additional components are not supported so far */
831             if(lcase[anz->l].iexist[indx]==1)
832 	    {
833               SFREE(lcase[anz->l].compName[indx]); lcase[anz->l].compName[indx]=NULL;
834               SFREE(lcase[anz->l].icname[indx]); lcase[anz->l].icname[indx]=NULL;
835 	    }
836             else ncomps++;
837             indx++;
838 	  }
839           else
840 	  {
841 	    rec_str[14]='\0';
842 	    printf(" WARNING: unallocated component:%d \"%s\" %d\n", ncomps, &rec_str[5],lcase[anz->l].ncomps);
843 	    exit(0);
844 	  }
845           nentities++;
846 
847           /* this is the last -5 line, try to figure out an offset (length of data-block) for the file-pointer */
848           /* and allocate data if no offset is defined (first time) */
849           if(nentities==lcase[anz->l].ncomps)
850 	  {
851             lcase[anz->l].ncomps=ncomps;
852             if(lcase[anz->l].ncomps<6) maxcomps=lcase[anz->l].ncomps;
853             else maxcomps=6;
854 
855             nvals=0;
856             for (i=0; i<ncomps; i++) if(lcase[anz->l].iexist[i]!=1) nvals++;
857 	    printf("ncomps:%d nvals:%d\n", ncomps, nvals);
858 
859             if(!read_mode)
860             {
861               if(nod_data)
862               {
863                 if (format_flag==2)
864 		{
865                   offset= nod_data * (4+nvals*4);
866 		}
867                 else if (format_flag==3)
868 		{
869                   offset= nod_data * (4+nvals*8);
870 		}
871                 else
872 		{
873                   /* just to get an approximate offset: */
874                   if (!format_flag) n=8;
875                   else n=13;
876                   if(nvals<=6) offset= nod_data * (n+nvals*12+1);
877                   else
878                   {
879                     offset=0;
880                     for(i=0; i<nvals/6; i++)
881                       offset+= nod_data * (n+6*12+1);
882                     if(nvals%6)
883                       offset+= nod_data * (n+(nvals%6)*12+1);
884       	          }
885 		  //printf("offset:%d nod_data:%d n:%d nvals:%d\n", offset,nod_data,n,nvals);
886 		}
887               }
888             }
889 
890             if(!offset)
891             {
892               /* in case the data should be read directly and not on demand */
893               for(i=0; i<(lcase[anz->l].ncomps); i++)
894 	      {
895                 if ( (lcase[anz->l].dat[i] = (CGXFLOAT *)malloc( (anz->nmax+1) * sizeof(CGXFLOAT))) == NULL )
896                   printf("\n\n ERROR: malloc failure\n\n" );
897                 for(j=0; j<=anz->nmax; j++) lcase[anz->l].dat[i][j]=0.;
898 	      }
899 	    }
900 	  }
901 
902         }
903         else if(flag == -6)
904         {
905           length= strsplt( rec_str, ' ', &dat);
906           ipuf=atoi(dat[2]);
907           if ( (compName = (char **)malloc( ipuf * sizeof(char *))) == NULL )
908             printf("\n\n ERROR: malloc failure\n\n" );
909           for(i=0; i<ipuf; i++)
910 	  {
911             if ( (compName[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
912                printf("\n\n ERROR: malloc failed\n\n" );
913 	  }
914           for (i=0; i<ipuf; i++) strcpy(compName[i], lcase[anz->l].compName[ atoi(dat[i+3])-1 ]);
915           for (i=0; i<ipuf; i++) strcpy(lcase[anz->l].compName[i],compName[i]);
916           if ( (menu = (int *)malloc( ipuf * sizeof(int))) == NULL )
917             printf("\n\n ERROR: malloc failure\n\n" );
918           if ( (ictype = (int *)malloc( ipuf * sizeof(int))) == NULL )
919             printf("\n\n ERROR: malloc failure\n\n" );
920           if ( (icind1 = (int *)malloc( ipuf * sizeof(int))) == NULL )
921             printf("\n\n ERROR: malloc failure\n\n" );
922           if ( (icind2 = (int *)malloc( ipuf * sizeof(int))) == NULL )
923             printf("\n\n ERROR: malloc failure\n\n" );
924           if ( (iexist = (int *)malloc( ipuf * sizeof(int))) == NULL )
925             printf("\n\n ERROR: malloc failure\n\n" );
926           for (i=0; i<ipuf; i++) menu[i] = lcase[anz->l].menu[atoi(dat[i+3])-1];
927           for (i=0; i<ipuf; i++) lcase[anz->l].menu[i] =menu[i];
928           for (i=0; i<ipuf; i++) ictype[i] = lcase[anz->l].ictype[atoi(dat[i+3])-1];
929           for (i=0; i<ipuf; i++) lcase[anz->l].ictype[i] =ictype[i];
930           for (i=0; i<ipuf; i++) icind1[i] = lcase[anz->l].icind1[atoi(dat[i+3])-1];
931           for (i=0; i<ipuf; i++) lcase[anz->l].icind1[i] =icind1[i];
932           for (i=0; i<ipuf; i++) icind2[i] = lcase[anz->l].icind2[atoi(dat[i+3])-1];
933           for (i=0; i<ipuf; i++) lcase[anz->l].icind2[i] =icind2[i];
934           for (i=0; i<ipuf; i++) iexist[i] = lcase[anz->l].iexist[atoi(dat[i+3])-1];
935           for (i=0; i<ipuf; i++) lcase[anz->l].iexist[i] =iexist[i];
936 
937           for(i=0; i<length; i++) SFREE(dat[i]);
938           SFREE(dat);
939 
940           for(i=0; i<ipuf; i++) SFREE(compName[i]);
941           SFREE(compName);
942           SFREE(menu);
943           SFREE(ictype);
944           SFREE(icind1);
945           SFREE(icind2);
946           SFREE(iexist);
947 	}
948       }while(flag!=-3);
949 
950       /* in case the data should be read directly and not on demand */
951       if(!offset)
952       {
953        for(j=0; j<anz->n; j++)
954        {
955         for(i=0; i<lcase[anz->l].ncomps; i++)
956         {
957           if(lcase[anz->l].dat[i][node[j].nr] > lcase[anz->l].max[i])
958           {
959             lcase[anz->l].max[i]=lcase[anz->l].dat[i][node[j].nr];
960             lcase[anz->l].nmax[i]=node[j].nr;
961           }
962           if(lcase[anz->l].dat[i][node[j].nr] < lcase[anz->l].min[i])
963           {
964             lcase[anz->l].min[i]=lcase[anz->l].dat[i][node[j].nr];
965             lcase[anz->l].nmin[i]=node[j].nr;
966           }
967         }
968        }
969       }
970     }
971 
972     else if(flag == 4)
973     {
974       anz->l++;
975       printf ("reading Dataset No:%d\n",anz->l+1);
976       if ( (lcase = (Datasets *)realloc((Datasets *)lcase, (anz->l+2) * sizeof(Datasets))) == NULL )
977       { printf("\n\n ERROR: malloc failure\n\n" ); exit(1); }
978 
979       lcase[anz->l].value=stof( rec_str, 13, 25 );
980       strcpy(lcase[anz->l].name,"DISP    ");
981       lcase[anz->l].ncomps = 3;
982       lcase[anz->l].irtype = 1;
983       lcase[anz->l].loaded = 1;
984       lcase[anz->l].format_flag=format_flag;
985 
986       if ( (lcase[anz->l].nmax = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
987         printf("\n\n ERROR: malloc failure\n\n" );
988       if ( (lcase[anz->l].nmin = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
989         printf("\n\n ERROR: malloc failure\n\n" );
990       if ( (lcase[anz->l].max = (CGXFLOAT *)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT))) == NULL )
991         printf("\n\n ERROR: malloc failure\n\n" );
992       if ( (lcase[anz->l].min = (CGXFLOAT *)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT))) == NULL )
993         printf("\n\n ERROR: malloc failure\n\n" );
994       if ( (lcase[anz->l].dat = (CGXFLOAT **)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT *))) == NULL )
995         printf("\n\n ERROR: malloc failure\n\n" );
996       if ( (lcase[anz->l].compName = (char **)malloc( lcase[anz->l].ncomps * sizeof(char *))) == NULL )
997         printf("\n\n ERROR: malloc failure\n\n" );
998       if ( (lcase[anz->l].icname = (char **)malloc( lcase[anz->l].ncomps * sizeof(char *))) == NULL )
999         printf("\n\n ERROR: malloc failure\n\n" );
1000       for(i=0; i<lcase[anz->l].ncomps; i++)
1001       {
1002         if ( (lcase[anz->l].compName[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
1003            printf("\n\n ERROR: malloc failed\n\n" );
1004         if ( (lcase[anz->l].icname[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
1005            printf("\n\n ERROR: malloc failed\n\n" );
1006         lcase[anz->l].max[i]=-MAX_FLOAT;
1007         lcase[anz->l].min[i]=MAX_FLOAT;
1008         if ( (lcase[anz->l].dat[i] = (CGXFLOAT *)malloc( (anz->nmax+1) * sizeof(CGXFLOAT))) == NULL )
1009           printf("\n\n ERROR: malloc failure\n\n" );
1010         for(j=0; j<=anz->nmax; j++) lcase[anz->l].dat[i][j]=0.;
1011       }
1012       if ( (lcase[anz->l].menu = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1013         printf("\n\n ERROR: malloc failure\n\n" );
1014       if ( (lcase[anz->l].ictype = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1015         printf("\n\n ERROR: malloc failure\n\n" );
1016       if ( (lcase[anz->l].icind1 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1017         printf("\n\n ERROR: malloc failure\n\n" );
1018       if ( (lcase[anz->l].icind2 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1019         printf("\n\n ERROR: malloc failure\n\n" );
1020       if ( (lcase[anz->l].iexist = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1021         printf("\n\n ERROR: malloc failure\n\n" );
1022 
1023       for(i=0; i<lcase[anz->l].ncomps; i++)
1024       {
1025         lcase[anz->l].menu[i] = 1;
1026         lcase[anz->l].ictype[i] = 2;
1027         lcase[anz->l].icind1[i] = i+1;
1028         lcase[anz->l].icind2[i] = 0;
1029         lcase[anz->l].iexist[i] = 0;
1030       }
1031 
1032       strcpy(lcase[anz->l].compName[0], "x       ");
1033       strcpy(lcase[anz->l].compName[1], "y       ");
1034       strcpy(lcase[anz->l].compName[2], "z       ");
1035       errFlag=0;
1036       do
1037       {
1038         length = frecord( handle, rec_str);
1039         if (rec_str[length] == (char)EOF) break;
1040         flag = stoi(rec_str,1,3);
1041         if(flag == -1)
1042         {
1043           if (!format_flag) nodenr = stoi(rec_str,4,8);
1044           else              nodenr = stoi(rec_str,4,13);
1045           if (nodenr>anz->nmax)
1046           {
1047             if (!errFlag) { errFlag=1; printf("WARNING: found node:%d in Dataset higher than in geometry allocated:%d\n", nodenr, anz->nmax); }
1048           }
1049           else if (!format_flag)
1050           {
1051             for(i=0; i<lcase[anz->l].ncomps; i++)
1052               lcase[anz->l].dat[i][nodenr]= stof(&rec_str[8+i*12], 1, 12);
1053           }
1054           else
1055           {
1056             for(i=0; i<lcase[anz->l].ncomps; i++)
1057               lcase[anz->l].dat[i][nodenr]= stof(&rec_str[13+i*12], 1, 12);
1058           }
1059         }
1060       }while(flag!=-3);
1061       for(n=0; n<anz->n; n++)
1062       {
1063         nodenr=node[n].nr;
1064         for(i=0; i<lcase[anz->l].ncomps; i++)
1065         {
1066           if(lcase[anz->l].dat[i][nodenr] > lcase[anz->l].max[i])
1067           {
1068             lcase[anz->l].max[i]=lcase[anz->l].dat[i][nodenr];
1069             lcase[anz->l].nmax[i]=nodenr;
1070           }
1071           if(lcase[anz->l].dat[i][nodenr] < lcase[anz->l].min[i])
1072           {
1073             lcase[anz->l].min[i]=lcase[anz->l].dat[i][nodenr];
1074             lcase[anz->l].nmin[i]=nodenr;
1075           }
1076         }
1077       }
1078     }
1079 
1080     else if(flag == 5)
1081     {
1082       anz->l++;
1083       printf ("reading Dataset No:%d\n",anz->l+1);
1084       if ( (lcase = (Datasets *)realloc((Datasets *)lcase, (anz->l+2) * sizeof(Datasets))) == NULL )
1085       { printf("\n\n ERROR: malloc failure\n\n" ); exit(1); }
1086 
1087       lcase[anz->l].value=stof( rec_str, 13, 25 );
1088       strcpy(lcase[anz->l].name,"STRESS  ");
1089       lcase[anz->l].ncomps = 6;
1090       lcase[anz->l].irtype = 1;
1091       lcase[anz->l].loaded = 1;
1092       lcase[anz->l].format_flag=format_flag;
1093 
1094       if ( (lcase[anz->l].nmax = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1095         printf("\n\n ERROR: malloc failure\n\n" );
1096       if ( (lcase[anz->l].nmin = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1097         printf("\n\n ERROR: malloc failure\n\n" );
1098       if ( (lcase[anz->l].max = (CGXFLOAT *)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT))) == NULL )
1099         printf("\n\n ERROR: malloc failure\n\n" );
1100       if ( (lcase[anz->l].min = (CGXFLOAT *)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT))) == NULL )
1101         printf("\n\n ERROR: malloc failure\n\n" );
1102       if ( (lcase[anz->l].dat = (CGXFLOAT **)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT *))) == NULL )
1103         printf("\n\n ERROR: malloc failure\n\n" );
1104       if ( (lcase[anz->l].compName = (char **)malloc( lcase[anz->l].ncomps * sizeof(char *))) == NULL )
1105         printf("\n\n ERROR: malloc failure\n\n" );
1106       if ( (lcase[anz->l].icname = (char **)malloc( lcase[anz->l].ncomps * sizeof(char *))) == NULL )
1107         printf("\n\n ERROR: malloc failure\n\n" );
1108       for(i=0; i<lcase[anz->l].ncomps; i++)
1109       {
1110         if ( (lcase[anz->l].dat[i] = (CGXFLOAT *)malloc( (anz->nmax+1) * sizeof(CGXFLOAT))) == NULL )
1111           printf("\n\n ERROR: malloc failure\n\n" );
1112         if ( (lcase[anz->l].compName[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
1113            printf("\n\n ERROR: malloc failed\n\n" );
1114         if ( (lcase[anz->l].icname[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
1115            printf("\n\n ERROR: malloc failed\n\n" );
1116         lcase[anz->l].max[i]=-MAX_FLOAT;
1117         lcase[anz->l].min[i]=MAX_FLOAT;
1118         for(j=0; j<=anz->nmax; j++) lcase[anz->l].dat[i][j]=0.;
1119       }
1120       if ( (lcase[anz->l].menu = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1121         printf("\n\n ERROR: malloc failure\n\n" );
1122       if ( (lcase[anz->l].ictype = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1123         printf("\n\n ERROR: malloc failure\n\n" );
1124       if ( (lcase[anz->l].icind1 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1125         printf("\n\n ERROR: malloc failure\n\n" );
1126       if ( (lcase[anz->l].icind2 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1127         printf("\n\n ERROR: malloc failure\n\n" );
1128       if ( (lcase[anz->l].iexist = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1129         printf("\n\n ERROR: malloc failure\n\n" );
1130 
1131       for(i=0; i<lcase[anz->l].ncomps; i++)
1132       {
1133         lcase[anz->l].menu[i] = 1;
1134         lcase[anz->l].ictype[i] = 4;
1135         lcase[anz->l].iexist[i] = 0;
1136       }
1137       lcase[anz->l].icind1[0] = 1;
1138       lcase[anz->l].icind2[0] = 1;
1139       lcase[anz->l].icind1[1] = 2;
1140       lcase[anz->l].icind2[1] = 2;
1141       lcase[anz->l].icind1[2] = 3;
1142       lcase[anz->l].icind2[2] = 3;
1143       lcase[anz->l].icind1[3] = 1;
1144       lcase[anz->l].icind2[3] = 2;
1145       lcase[anz->l].icind1[4] = 2;
1146       lcase[anz->l].icind2[4] = 3;
1147       lcase[anz->l].icind1[5] = 3;
1148       lcase[anz->l].icind2[5] = 1;
1149 
1150       strcpy(lcase[anz->l].compName[0], "xx      ");
1151       strcpy(lcase[anz->l].compName[1], "yy      ");
1152       strcpy(lcase[anz->l].compName[2], "zz      ");
1153       strcpy(lcase[anz->l].compName[3], "xy      ");
1154       strcpy(lcase[anz->l].compName[4], "yz      ");
1155       strcpy(lcase[anz->l].compName[5], "zx      ");
1156       errFlag=0;
1157       do
1158       {
1159         length = frecord( handle, rec_str);
1160         if (rec_str[length] == (char)EOF) break;
1161         flag = stoi(rec_str,1,3);
1162         if(flag == -1)
1163         {
1164           if (!format_flag) nodenr = stoi(rec_str,4,8);
1165           else              nodenr = stoi(rec_str,4,13);
1166           if (nodenr>anz->nmax)
1167           {
1168             if (!errFlag) { errFlag=1; printf("WARNING: found node:%d in Dataset higher than in geometry allocated:%d\n", nodenr, anz->nmax); }
1169           }
1170           else
1171           {
1172             /* new line */
1173             length = frecord( handle, rec_str);
1174             if (rec_str[length] == (char)EOF) break;
1175             if (!format_flag)
1176             {
1177               for(i=0; i<lcase[anz->l].ncomps; i++)
1178                 lcase[anz->l].dat[i][nodenr]= stof(&rec_str[8+i*12], 1, 12);
1179             }
1180             else
1181             {
1182               for(i=0; i<lcase[anz->l].ncomps; i++)
1183                 lcase[anz->l].dat[i][nodenr]= stof(&rec_str[13+i*12], 1, 12);
1184             }
1185           }
1186         }
1187       }while(flag!=-3);
1188       for(n=0; n<anz->n; n++)
1189       {
1190         nodenr=node[n].nr;
1191         for(i=0; i<lcase[anz->l].ncomps; i++)
1192         {
1193           if(lcase[anz->l].dat[i][nodenr] > lcase[anz->l].max[i])
1194           {
1195             lcase[anz->l].max[i]=lcase[anz->l].dat[i][nodenr];
1196             lcase[anz->l].nmax[i]=nodenr;
1197           }
1198           if(lcase[anz->l].dat[i][nodenr] < lcase[anz->l].min[i])
1199           {
1200             lcase[anz->l].min[i]=lcase[anz->l].dat[i][nodenr];
1201             lcase[anz->l].nmin[i]=nodenr;
1202           }
1203         }
1204       }
1205     }
1206 
1207     else if((flag == 7)||(flag == 9))
1208     {
1209       anz->l++;
1210       printf ("reading Dataset No:%d\n",anz->l+1);
1211       if ( (lcase = (Datasets *)realloc((Datasets *)lcase, (anz->l+2) * sizeof(Datasets))) == NULL )
1212       { printf("\n\n ERROR: malloc failure\n\n" ); exit(1); }
1213 
1214       lcase[anz->l].value=stof( rec_str, 13, 25 );
1215       strcpy(lcase[anz->l].name,"TEMP    ");
1216       lcase[anz->l].ncomps = 1;
1217       lcase[anz->l].irtype = 1;
1218       lcase[anz->l].loaded = 1;
1219       lcase[anz->l].format_flag=format_flag;
1220 
1221       if ( (lcase[anz->l].nmax = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1222         printf("\n\n ERROR: malloc failure\n\n" );
1223       if ( (lcase[anz->l].nmin = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1224         printf("\n\n ERROR: malloc failure\n\n" );
1225       if ( (lcase[anz->l].max = (CGXFLOAT *)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT))) == NULL )
1226         printf("\n\n ERROR: malloc failure\n\n" );
1227       if ( (lcase[anz->l].min = (CGXFLOAT *)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT))) == NULL )
1228         printf("\n\n ERROR: malloc failure\n\n" );
1229       if ( (lcase[anz->l].dat = (CGXFLOAT **)malloc( lcase[anz->l].ncomps * sizeof(CGXFLOAT *))) == NULL )
1230         printf("\n\n ERROR: malloc failure\n\n" );
1231       if ( (lcase[anz->l].compName = (char **)malloc( lcase[anz->l].ncomps * sizeof(char *))) == NULL )
1232         printf("\n\n ERROR: malloc failure\n\n" );
1233       if ( (lcase[anz->l].icname = (char **)malloc( lcase[anz->l].ncomps * sizeof(char *))) == NULL )
1234         printf("\n\n ERROR: malloc failure\n\n" );
1235       for(i=0; i<lcase[anz->l].ncomps; i++)
1236       {
1237         if ( (lcase[anz->l].dat[i] = (CGXFLOAT *)malloc( (anz->nmax+1) * sizeof(CGXFLOAT))) == NULL )
1238           printf("\n\n ERROR: malloc failure\n\n" );
1239         if ( (lcase[anz->l].compName[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
1240            printf("\n\n ERROR: malloc failed\n\n" );
1241         if ( (lcase[anz->l].icname[i] = (char *)malloc( MAX_LINE_LENGTH * sizeof(char))) == NULL )
1242            printf("\n\n ERROR: malloc failed\n\n" );
1243         lcase[anz->l].max[i]=-MAX_FLOAT;
1244         lcase[anz->l].min[i]=MAX_FLOAT;
1245         for(j=0; j<=anz->nmax; j++) lcase[anz->l].dat[i][j]=0.;
1246       }
1247       if ( (lcase[anz->l].menu = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1248         printf("\n\n ERROR: malloc failure\n\n" );
1249       if ( (lcase[anz->l].ictype = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1250         printf("\n\n ERROR: malloc failure\n\n" );
1251       if ( (lcase[anz->l].icind1 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1252         printf("\n\n ERROR: malloc failure\n\n" );
1253       if ( (lcase[anz->l].icind2 = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1254         printf("\n\n ERROR: malloc failure\n\n" );
1255       if ( (lcase[anz->l].iexist = (int *)malloc( lcase[anz->l].ncomps * sizeof(int))) == NULL )
1256         printf("\n\n ERROR: malloc failure\n\n" );
1257 
1258       for(i=0; i<lcase[anz->l].ncomps; i++)
1259       {
1260         lcase[anz->l].menu[i] = 1;
1261         lcase[anz->l].ictype[i] = 1;
1262         lcase[anz->l].icind1[i] = i+1;
1263         lcase[anz->l].icind2[i] = 0;
1264         lcase[anz->l].iexist[i] = 0;
1265       }
1266 
1267       strcpy(lcase[anz->l].compName[0], "Value   ");
1268       errFlag=0;
1269       do
1270       {
1271         length = frecord( handle, rec_str);
1272         if (rec_str[length] == (char)EOF) break;
1273         flag = stoi(rec_str,1,3);
1274         if(flag == -1)
1275         {
1276           if (!format_flag) nodenr = stoi(rec_str,4,8);
1277           else              nodenr = stoi(rec_str,4,13);
1278           if (nodenr>anz->nmax)
1279           {
1280             if (!errFlag) { errFlag=1; printf("WARNING: found node:%d in Dataset higher than in geometry allocated:%d\n", nodenr, anz->nmax); }
1281           }
1282           else if (!format_flag)
1283           {
1284             for(i=0; i<lcase[anz->l].ncomps; i++)
1285               lcase[anz->l].dat[i][nodenr]= stof(&rec_str[8+i*12], 1, 12);
1286           }
1287           else
1288           {
1289             for(i=0; i<lcase[anz->l].ncomps; i++)
1290               lcase[anz->l].dat[i][nodenr]= stof(&rec_str[13+i*12], 1, 12);
1291           }
1292         }
1293       }while(flag!=-3);
1294       for(n=0; n<anz->n; n++)
1295       {
1296         nodenr=node[n].nr;
1297         for(i=0; i<lcase[anz->l].ncomps; i++)
1298         {
1299           if(lcase[anz->l].dat[i][nodenr] > lcase[anz->l].max[i])
1300           {
1301             lcase[anz->l].max[i]=lcase[anz->l].dat[i][nodenr];
1302             lcase[anz->l].nmax[i]=nodenr;
1303           }
1304           if(lcase[anz->l].dat[i][nodenr] < lcase[anz->l].min[i])
1305           {
1306             lcase[anz->l].min[i]=lcase[anz->l].dat[i][nodenr];
1307             lcase[anz->l].nmin[i]=nodenr;
1308           }
1309         }
1310       }
1311     }
1312 
1313     else if(flag == -1)
1314     {
1315   next:;
1316       printf (" overread Block: %s\n", rec_str);
1317       do
1318       {
1319         length = frecord( handle, rec_str);
1320         if (rec_str[length] == (char)EOF) break;
1321         /* printf ("\n record:%d %s\n", length, rec_str);   */
1322         if (length != 0)
1323         {
1324           flag = stoi(rec_str,1,5);
1325         }
1326       } while(flag != -3);
1327     }
1328     else if(flag == -2)
1329     {
1330       /* nr of nodes per block can be read from the frd file, this is not documented in the original frd-spec. */
1331       nod_data=stoi( rec_str, 25, 36 );
1332       printf ("scip %d nodes\n",nod_data);
1333       if(format_flag == 2) fseeko(handle, nod_data*(sizeof(int)+3*sizeof(float)), SEEK_CUR);
1334       else  fseeko(handle, nod_data*(sizeof(int)+3*sizeof(double)), SEEK_CUR);
1335     }
1336     else if(flag == -3)
1337     {
1338       /* nr of elems per block can be read from the frd file, this is not documented in the original frd-spec. */
1339       elem_data=stoi( rec_str, 25, 36 );
1340       printf ("scip %d elements\n",elem_data);
1341       for (i=0; i<elem_data; i++)
1342       {
1343         fseeko(handle, sizeof(int), SEEK_CUR);
1344         length=fread((int *)&j,sizeof(int),1,handle);
1345         fseeko(handle, 2*sizeof(int), SEEK_CUR);
1346         if (j == 1)      ipuf = 8;   /* HEXA8  */
1347         else if (j == 2) ipuf = 6;   /* PE6   */
1348         else if (j == 3) ipuf = 4;   /* TET4   */
1349         else if (j == 4) ipuf = 20;  /* HEXA20 */
1350         else if (j == 5) ipuf = 15;  /* PE15  */
1351         else if (j == 6) ipuf = 10;  /* TET10  */
1352         else if (j == 7) ipuf = 3;   /* TRI3   */
1353         else if (j == 8) ipuf = 6;   /* TRI6   */
1354         else if (j == 9) ipuf = 4;   /* QUAD4  */
1355         else if (j == 10) ipuf = 8; /* QUAD8  */
1356         else if (j == 11) ipuf = 2;  /* BEAM2   */
1357         else if (j == 12) ipuf = 3;  /* BEAM3   */
1358         fseeko(handle, ipuf*sizeof(int), SEEK_CUR);
1359       }
1360     }
1361     else
1362     {
1363       printf (" overread Block: %s\n", rec_str);
1364       do
1365       {
1366         length = frecord( handle, rec_str);
1367         if (rec_str[length] == (char)EOF) break;
1368         /* printf ("\n record:%d %s\n", length, rec_str);   */
1369         if (length != 0)
1370         {
1371           flag = stoi(rec_str,1,5);
1372         }
1373       } while(flag != -3);
1374     }
1375   }
1376 
1377   for(i=0; i<anz_p; i++) SFREE(pheader[i]); SFREE(pheader);
1378   SFREE( filepntr);
1379 
1380   anz->u++;
1381   anz->l++;
1382   if(anz->n<0) anz->n=0;
1383   if(anz->e<0) anz->e=0;
1384 
1385   if ( e_nmax > (anz->nmax) )
1386   {
1387     printf ("\nWARNING: element requestes a nodename higher than allocated\n\n");
1388     printf (" e_nmax=%d e_nmin=%d\n", e_nmax, e_nmin );
1389   }
1390   if ( e_nmin < 1 )
1391   {
1392     printf ("\nWARNING: element requestes a nodename lower than allocated\n\n");
1393     printf (" e_nmax=%d e_nmin=%d\n", e_nmax, e_nmin );
1394   }
1395 
1396   anz->orign    = anz->n;
1397   anz->orignmax = anz->nmax;
1398   anz->olc = anz->l;
1399 
1400   *nptr = node; *eptr = elem; *lptr = lcase;
1401   return(1);
1402 }
1403 
1404 
1405 
1406 /* if a block was skipped during first read of frd-file read it now */
1407 /* return -1 if failure */
readfrdblock(int lc,Summen * anz,Nodes * node,Datasets * lcase)1408 int readfrdblock(int lc, Summen *anz,   Nodes     *node, Datasets *lcase )
1409 {
1410   register int i,j, n;
1411   int  length, flag, format_flag, nodenr=0, ncomps;
1412   int  nod_data=0, maxcomps=0;
1413   int  errFlag=0;
1414   char rec_str[MAX_LINE_LENGTH];
1415   static float *value=NULL;
1416   static double *dvalue=NULL;
1417   FILE *handle;
1418 
1419   // printf("readfrdblock for file:%s\n", lcase[lc].filename);
1420 
1421   /* Open the files and check to see that it was opened correctly */
1422   handle = lcase[lc].handle;
1423   if ( handle== NULL )  { printf ("ERROR in readfrdblock: The input file \"%s\" could not be opened.\n\n", lcase[lc].filename); return(-1); }
1424 
1425   if( fsetpos( handle, (fpos_t *)lcase[lc].fileptr)!=0) { printf("error in fsetpos"); return(-1); }
1426   lcase[lc].loaded=1;
1427 
1428   length = frecord( handle, rec_str);
1429   if (rec_str[length] == (char)EOF) return(-1);
1430 
1431   flag = stoi(rec_str,1,5);
1432   format_flag = stoi(rec_str,74,75);
1433 
1434   if(lcase[lc].ncomps<6) maxcomps=lcase[lc].ncomps;
1435   else maxcomps=6;
1436 
1437   if( lcase[lc].irtype > 2 )
1438   {
1439     printf(" ERROR: Found ELEMENT DATA, this is not suported!\n");
1440     return(-1);
1441   }
1442 
1443   if ( (lcase[lc].dat = (CGXFLOAT **)malloc( (lcase[lc].ncomps) * sizeof(CGXFLOAT *))) == NULL )
1444     printf("\n\n ERROR: malloc failure\n\n" );
1445   for(i=0; i<(lcase[lc].ncomps); i++)
1446   {
1447     if ( (lcase[lc].dat[i] = (CGXFLOAT *)malloc( (anz->nmax+1) * sizeof(CGXFLOAT))) == NULL )
1448       printf("\n\n ERROR: malloc failure\n\n" );
1449     for(j=0; j<=anz->nmax; j++) lcase[lc].dat[i][j]=0.;
1450   }
1451 
1452 
1453   if(flag == 100)
1454   {
1455     nod_data=stoi( rec_str, 25, 36 );
1456     if (!format_flag) n=8;
1457     else n=13;
1458     do
1459     {
1460       length = frecord( handle, rec_str);
1461       if (rec_str[length] == (char)EOF) break;
1462       flag = stoi(rec_str,1,3);
1463 
1464       /* bin mode, active after last column definition was read (-5 lines). Attention flag:-6 not permitted so far! */
1465       if ( format_flag>=2)
1466       {
1467         if (flag == -4)
1468         {
1469           ncomps = stoi(rec_str,14,18);
1470         }
1471         else continue;
1472 
1473         //printf("format_flag=%d ncomps:%d\n",format_flag,ncomps);
1474 
1475         /* skip the meta-data */
1476         for(i=0; i<ncomps; i++) length = frecord( handle, rec_str);
1477 
1478         if ( format_flag==2)
1479         {
1480           if ( (value = (float *)realloc((float *)value, (lcase[lc].ncomps) * sizeof(float))) == NULL )
1481             printf("\n\n ERROR: realloc failed, value\n\n") ;
1482           for(n=0; n<nod_data; n++)
1483           {
1484             length=fread((int *)&nodenr,sizeof(int),1,handle);
1485             length=fread((float *)value,sizeof(float),lcase[lc].ncomps,handle);
1486   	  // printf("N:%d ",nodenr);
1487             for(i=0; i<lcase[lc].ncomps; i++)
1488             {
1489   	    // printf(" %f",value[i]);
1490               lcase[lc].dat[i][nodenr]= value[i];
1491             }
1492   	  // printf("\n");
1493           }
1494         }
1495         if ( format_flag==3)
1496         {
1497           if ( (dvalue = (double *)realloc((double *)dvalue, (lcase[lc].ncomps) * sizeof(double))) == NULL )
1498             printf("\n\n ERROR: realloc failed, dvalue\n\n") ;
1499           for(n=0; n<nod_data; n++)
1500           {
1501             length=fread((int *)&nodenr,sizeof(int),1,handle);
1502             length=fread((double *)dvalue,sizeof(double),lcase[lc].ncomps,handle);
1503   	  // printf("N:%d ",nodenr);
1504             for(i=0; i<lcase[lc].ncomps; i++)
1505             {
1506   	    // printf(" %f",dvalue[i]);
1507               lcase[lc].dat[i][nodenr]= dvalue[i];
1508             }
1509   	  // printf("\n");
1510           }
1511         }
1512         break;
1513       }
1514 
1515       else if(flag == -1)
1516       {
1517         if (format_flag) nodenr = stoi(rec_str,4,13); else nodenr = stoi(rec_str,4,8);
1518         if (nodenr>anz->nmax)
1519         {
1520           if (!errFlag) { errFlag=1; printf("WARNING: found node:%d in Dataset higher than in geometry allocated:%d\n", nodenr, anz->nmax); }
1521         }
1522         else if ( lcase[lc].irtype == 1 )
1523 	{
1524           if(maxcomps==6)
1525           {
1526             i=6;
1527             if ( format_flag)
1528             {
1529               lcase[lc].dat[0][nodenr]= stof(&rec_str[  13  ], 1, 12);
1530               lcase[lc].dat[1][nodenr]= stof(&rec_str[  25  ], 1, 12);
1531               lcase[lc].dat[2][nodenr]= stof(&rec_str[  37  ], 1, 12);
1532               lcase[lc].dat[3][nodenr]= stof(&rec_str[  49  ], 1, 12);
1533               lcase[lc].dat[4][nodenr]= stof(&rec_str[  61  ], 1, 12);
1534               lcase[lc].dat[5][nodenr]= stof(&rec_str[  73  ], 1, 12);
1535             }
1536             else
1537             {
1538               lcase[lc].dat[0][nodenr]= stof(&rec_str[  8   ], 1, 12);
1539               lcase[lc].dat[1][nodenr]= stof(&rec_str[  20  ], 1, 12);
1540               lcase[lc].dat[2][nodenr]= stof(&rec_str[  32  ], 1, 12);
1541               lcase[lc].dat[3][nodenr]= stof(&rec_str[  44  ], 1, 12);
1542               lcase[lc].dat[4][nodenr]= stof(&rec_str[  56  ], 1, 12);
1543               lcase[lc].dat[5][nodenr]= stof(&rec_str[  68  ], 1, 12);
1544             }
1545           }
1546           else for(i=0; i<maxcomps; i++) lcase[lc].dat[i][nodenr]= stof(&rec_str[n+i*12], 1, 12);
1547         }
1548         else i=0;
1549       }
1550       else if(flag == -2)
1551       {
1552         if (!format_flag) n=8;
1553         else n=13;
1554         j=0;
1555         do
1556         {
1557           lcase[lc].dat[i][nodenr]= stof(&rec_str[n+j*12], 1, 12);
1558           i++;j++;
1559         }while((j<6)&&(i<lcase[lc].ncomps));
1560       }
1561     }while(flag!=-3);
1562 
1563     for(j=0; j<anz->orign; j++)
1564     {
1565       for(i=0; i<lcase[lc].ncomps; i++)
1566       {
1567         if(lcase[lc].dat[i][node[j].nr] > lcase[lc].max[i])
1568         {
1569           lcase[lc].max[i]=lcase[lc].dat[i][node[j].nr];
1570           lcase[lc].nmax[i]=node[j].nr;
1571         }
1572         if(lcase[lc].dat[i][node[j].nr] < lcase[lc].min[i])
1573         {
1574           lcase[lc].min[i]=lcase[lc].dat[i][node[j].nr];
1575           lcase[lc].nmin[i]=node[j].nr;
1576         }
1577       }
1578     }
1579   }
1580 
1581   return(0);
1582 }
1583 
1584 
1585 /* regula falsi to find the matching record fast */
1586 /* not finished */
getRecord(FILE * handle,int n,int x0)1587 char *getRecord(FILE *handle, int n, int x0 )
1588 {
1589     int ii, m, n1,n2, x=0, offset=0;
1590 
1591     /* search the intersection */
1592     n1=0;
1593     n2=n;
1594     for(ii=0; ii<n; ii++)
1595     {
1596       m=(n2+n1)/2;
1597 
1598 
1599       if( fseeko( handle, offset, SEEK_CUR )!=0) printf("error in fseeko\n");
1600 
1601       if(x0>= x ) n1=m;
1602       if(x0 < x ) n2=m;
1603       if((n2-n1) == 1) break;
1604     }
1605 #if TEST
1606     printf("x:%d x0:%d\n", x,x0);
1607 #endif
1608   return(NULL);
1609 }
1610 
1611 
1612 /* return -1 if failure */
1613 /* return 0 if successfull */
readOneNode(int lc,Summen * anz,Datasets * lcase,int nodenr,double ** vptr,long * byte_offset)1614 int readOneNode( int lc, Summen *anz, Datasets *lcase, int nodenr, double **vptr, long *byte_offset )
1615 {
1616   register int i=0,j, n;
1617   int length, flag, inodenr=0;
1618   int   maxcomps=0, ncomps;
1619   long offset=0;
1620   int  errFlag=0, readFlag=0, bailout=0;
1621   char rec_str[MAX_LINE_LENGTH];
1622   FILE *handle;
1623   static float *value=NULL;
1624   static double *dvalue=NULL;
1625   double *dat;
1626   int  nod_data=0, nvals=0;
1627 
1628   // printf("readOneNode for file:%s *byte_offset:%d\n", lcase[lc].filename, *byte_offset);
1629 
1630   /* Open the files and check to see that it was opened correctly */
1631   handle = lcase[lc].handle;
1632   if ( handle== NULL )  { printf ("ERROR in readOneNode: The input file \"%s\" could not be opened.\n\n", lcase[lc].filename); return(-1); }
1633 
1634   if( fsetpos( handle, (fpos_t *)lcase[lc].fileptr)!=0) { printf("error in fsetpos"); return(-1); }
1635 
1636   /* the header-lines must be skipped in case byte_offset==0 and format == 2(bin) */
1637   if((lcase[lc].format_flag>=2)&&(!*byte_offset))
1638   {
1639     do
1640     {
1641       length = frecord( handle, rec_str);
1642       if (rec_str[length] == (char)EOF) return(-1);
1643       *byte_offset+=length+1;
1644       printf ("record:%s\n", rec_str);
1645       flag = stoi(rec_str,1,5);
1646       if (flag == -4)
1647       {
1648         ncomps = stoi(rec_str,14,18);
1649       }
1650       else continue;
1651 
1652       /* skip the meta-data */
1653       for(i=0; i<ncomps; i++) *byte_offset+= frecord( handle, rec_str)+1;
1654       break;
1655     }while(1);
1656   }
1657   else{ if( fseeko( handle, *byte_offset, SEEK_CUR )!=0) printf("error in fseeko\n"); }
1658 
1659   offset=*byte_offset;
1660 
1661   if ( (dat = (double *)malloc( (lcase[lc].ncomps) * sizeof(double ))) == NULL )
1662     printf("\n\n ERROR: malloc failure\n\n" );
1663   *vptr=dat;
1664 
1665   /* if bin mode */
1666   if(lcase[lc].format_flag==2)
1667   {
1668     if ( (value = (float *)realloc((float *)value, (lcase[lc].ncomps) * sizeof(float))) == NULL )
1669       printf("\n\n ERROR: realloc failed, value\n\n") ;
1670     do
1671     {
1672       length=fread((int *)&inodenr,sizeof(int),1,handle)*sizeof(int);
1673       length+=fread((float *)value,sizeof(float),lcase[lc].ncomps,handle)*sizeof(float);
1674       //printf("N:%d ",inodenr);
1675       //for(i=0; i<lcase[lc].ncomps; i++) printf(" %f",value[i]);
1676       //printf("\n");
1677       if(inodenr==nodenr) break;
1678       else offset+=length;
1679     }while(1);
1680     for(i=0; i<lcase[lc].ncomps; i++)
1681     {
1682 	//printf(" %f",value[i]);
1683       dat[i]= value[i];
1684     }
1685 
1686     *byte_offset=offset;
1687     //printf("offset:%d\n", offset);
1688     return(0);
1689   }
1690   if(lcase[lc].format_flag==3)
1691   {
1692     if ( (dvalue = (double *)realloc((double *)dvalue, (lcase[lc].ncomps) * sizeof(double))) == NULL )
1693       printf("\n\n ERROR: realloc failed, dvalue\n\n") ;
1694     do
1695     {
1696       length=fread((int *)&inodenr,sizeof(int),1,handle)*sizeof(int);
1697       length+=fread((double *)dvalue,sizeof(double),lcase[lc].ncomps,handle)*sizeof(double);
1698       //printf("N:%d ",inodenr);
1699       //for(i=0; i<lcase[lc].ncomps; i++) printf(" %f",dvalue[i]);
1700       //printf("\n");
1701       if(inodenr==nodenr) break;
1702       else offset+=length;
1703     }while(1);
1704     for(i=0; i<lcase[lc].ncomps; i++)
1705     {
1706 	//printf(" %f",dvalue[i]);
1707       dat[i]= dvalue[i];
1708     }
1709 
1710     *byte_offset=offset;
1711     //printf("offset:%d\n", offset);
1712     return(0);
1713   }
1714 
1715   if(lcase[lc].ncomps<6) maxcomps=lcase[lc].ncomps;
1716   else maxcomps=6;
1717   if (!lcase[lc].format_flag) n=8;
1718   else n=13;
1719 
1720   if( lcase[lc].irtype > 2 )
1721   {
1722     printf(" ERROR: Found ELEMENT DATA, this is not suported!\n");
1723     return(-1);
1724   }
1725 
1726  repeat_search:;
1727   do
1728   {
1729     length = frecord( handle, rec_str);
1730     if (rec_str[length] == (char)EOF)
1731     {
1732       /* offset obviously false */
1733       offset=0;
1734       if( fsetpos( handle, (fpos_t *)lcase[lc].fileptr)!=0) { printf("error in fsetpos"); return(-1); }
1735     }
1736     flag = stoi(rec_str,1,3);
1737 
1738     if((readFlag)&&(flag == -2))
1739     {
1740       //printf("-2 node found:%d %d at pos:%d\n", inodenr,nodenr, nod_data);
1741       if (!lcase[lc].format_flag) n=8;
1742       else n=13;
1743       j=0;
1744       do
1745       {
1746         dat[i]= stof(&rec_str[n+j*12], 1, 12);
1747         i++;j++;
1748       }while((j<6)&&(i<lcase[lc].ncomps));
1749     }
1750     else if(readFlag)
1751     {
1752       /* leave after all records of that node are read */
1753 
1754       nod_data--;
1755 
1756       /* get the offset for the next call: */
1757       for (i=0; i<lcase[lc].ncomps; i++) if(lcase[lc].iexist[i]!=1) nvals++;
1758 
1759       if (!lcase[lc].format_flag) n=8;
1760       else n=13;
1761       if(nvals<=6) offset+= nod_data * (n+nvals*12);
1762       else
1763       {
1764         for(i=0; i<nvals/6; i++)
1765           offset+= nod_data * (n+6*12+1);
1766         if(nvals%6)
1767           offset+= nod_data * (n+(nvals%6)*12+1);
1768       }
1769       *byte_offset=offset;
1770       //printf("offset:%d nod_data:%d n:%d nvals:%d dat:%f\n", offset,nod_data,n,nvals, dat[0]);
1771       return(0);
1772     }
1773     else if(flag == -1)
1774     {
1775       if (lcase[lc].format_flag) inodenr = stoi(rec_str,4,13); else inodenr = stoi(rec_str,4,8);
1776       nod_data++;
1777       //printf("node:%d %d at pos:%d ir:%d\n", inodenr,nodenr, nod_data, lcase[lc].irtype);
1778       if (inodenr>anz->nmax)
1779       {
1780         if (!errFlag) { errFlag=1; printf("WARNING: found node:%d in Dataset higher than in geometry allocated:%d\n", inodenr, anz->nmax); }
1781       }
1782       else if (( inodenr==nodenr)&&(lcase[lc].irtype == 1 ))
1783       {
1784         //printf("node found:%d %d at pos:%d\n", inodenr,nodenr, nod_data);
1785         readFlag=1;
1786         if(maxcomps==6)
1787         {
1788           i=6;
1789           if ( lcase[lc].format_flag)
1790           {
1791             dat[0]= stof(&rec_str[  13  ], 1, 12);
1792             dat[1]= stof(&rec_str[  25  ], 1, 12);
1793             dat[2]= stof(&rec_str[  37  ], 1, 12);
1794             dat[3]= stof(&rec_str[  49  ], 1, 12);
1795             dat[4]= stof(&rec_str[  61  ], 1, 12);
1796             dat[5]= stof(&rec_str[  73  ], 1, 12);
1797           }
1798           else
1799           {
1800             dat[0]= stof(&rec_str[  8   ], 1, 12);
1801             dat[1]= stof(&rec_str[  20  ], 1, 12);
1802             dat[2]= stof(&rec_str[  32  ], 1, 12);
1803             dat[3]= stof(&rec_str[  44  ], 1, 12);
1804             dat[4]= stof(&rec_str[  56  ], 1, 12);
1805             dat[5]= stof(&rec_str[  68  ], 1, 12);
1806           }
1807         }
1808         else for(i=0; i<maxcomps; i++) dat[i]= stof(&rec_str[n+i*12], 1, 12);
1809       }
1810       else i=0;
1811     }
1812 
1813   }while(flag!=-3);
1814 
1815   if(!bailout)
1816   {
1817     bailout=1;
1818     offset=0;
1819     //printf("repeat search\n");
1820     if( fsetpos( handle, (fpos_t *)lcase[lc].fileptr)!=0) { printf("error in fsetpos"); return(-1); }
1821     goto repeat_search;
1822   }
1823   return(-1);
1824 }
1825 
1826