/* --------------------------------------------------------------------  */
/*                          CALCULIX                                     */
/*                   - GRAPHICAL INTERFACE -                             */
/*                                                                       */
/*     A 3-dimensional pre- and post-processor for finite elements       */
/*              Copyright (C) 1996 Klaus Wittig                          */
/*                                                                       */
/*     This program is free software; you can redistribute it and/or     */
/*     modify it under the terms of the GNU General Public License as    */
/*     published by the Free Software Foundation; version 2 of           */
/*     the License.                                                      */
/*                                                                       */
/*     This program is distributed in the hope that it will be useful,   */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of    */ 
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the      */
/*     GNU General Public License for more details.                      */
/*                                                                       */
/*     You should have received a copy of the GNU General Public License */
/*     along with this program; if not, write to the Free Software       */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.         */
/* --------------------------------------------------------------------  */

#include <cgx.h>
#include <ctype.h>

#define INI_FIELD_SIZE 100000
#define MAXVALUE 2147483647
#define MAX_MATERIALS 100

#define WRITE_FBL 0
#define ANSYS 0
#define TEST  0

#define     BOU         "+bou"
#define     DEP         "+dep"
#define     IND         "+ind"
#define     MPC         "+mpc"
#define     CLO         "+clo"
#define     DLO         "+dlo"
#define     RAD         "+rad"
#define     FLM         "+flm"
#define     CFLX        "+cflx"
#define     DFLX        "+dflx"

unsigned char ansys_dof[6][5]={"UX","UY","UZ","ROTX","ROTY","ROTZ"}; /* SHORT-CUTS FOR ANSYS DOFS */
unsigned char ansys_frc[6][5]={"FX","FY","FZ"}; /* SHORT-CUTS FOR ANSYS FORCE */

FILE *handlefbl;
FILE *handlein;
FILE *handleans;

typedef struct {
    int   nset;
    double d;
}Elprop;

Elprop    elprop[1];
int      *selected_elems=NULL;
int       filesopen;
FILE     *openfile[100]; 


int      matnr=0, ansFlag=0;
unsigned char **dat=NULL;

Elements  *elemx;
Nodes     *nodex;
Datasets  *lcasex;
Summen    *anzx;
int       ***facebuf=NULL;
int       anz_fb=-1;

extern int MAKE_SETS_DEF;
extern unsigned char buffer[MAX_LINE_LENGTH];
extern Alias     *alias;
extern Sets      *set;
extern SumGeo    anzGeo[1];
extern SumAsci   sumAsci[1];
extern unsigned char printFlag;
Elements  *e_enqirex;     /* feld in dem e_enqire[elem[i].nr].xx abgelegt ist, statt elem[i].xx */

void annotation( void )
{
  printf("******************************************************************************\n");
  printf("*  read ccx input                                   4.4.2002 wi              *\n");
  printf("*                                                                            *\n");
  printf("*                                                                            *\n");
  printf("* -only free-format is supported. fixed format can lead to a prg-crash.      *\n");
  printf("* -a set must be defined before a keyword uses this set.                     *\n");
  printf("*                                                                            *\n");
  printf("*                                                                            *\n");
  printf("******************************************************************************\n");
}


/* return:           */
/* 1: regular string */
/* -2:  end of data block     */
/* -1:  all files closed      */ 
int abqrecord( unsigned char *string)
{
  int l;

 getNextLine:;

  /* check for comments */
  do{ l=frecord( handlein, string); }while((string[0]=='*')&&(string[1]=='*'));
  /*  printf("abqrecord:%s\n", string); */

  /* check for include */
  if(string[0]=='*') 
  {
    if ((compare(string, "*INCLUDE",5)==5)||(compare(string, "*include",5)==5)) 
    {
     /* change the filepointer */
     filesopen=getFilePointer( filesopen, openfile, string);
     goto getNextLine;
    }
    else return(-2);
  }

  if (string[l] == (unsigned char)EOF)
  {
    /* are there files open? */
    if(filesopen>1)
    {
        fclose(handlein);
	filesopen--;
	handlein=openfile[filesopen-1];
        if(printFlag) printf("open file again:%d\n",filesopen);
    goto getNextLine;
    }
    else return(-1);
  }
  return(l);
}



int crecord( unsigned char *rec_str, unsigned char **dat)
{
  int i,j;
  int nextarg=0, letter=0;

  /* scan all args divided by comma */
  nextarg=0;letter=0;
  for(j=0; j<MAX_LINE_LENGTH; j++) dat[nextarg][j]=(unsigned char)NULL; 
  for(i=0; i<MAX_LINE_LENGTH; i++)
  {
    if(rec_str[i]==(unsigned char)EOF) {  break; } 
    if(rec_str[i]=='\n') {  break; } 
    if(rec_str[i]==0) {  break; } 
    if(rec_str[i]==',') 
    {
      nextarg++;
      letter=0;
      for(j=0; j<MAX_LINE_LENGTH; j++) dat[nextarg][j]=(unsigned char)NULL; 
    }
    else
    {
      dat[nextarg][letter]=rec_str[i];
      letter++;
    }
  }
  return(nextarg+1);
}


int getBoundaries(unsigned char *rec_str)
{
  int i,j;
  int args, nset;
  int ival[80];
  double fval;
  static int setNr[7]={0,0,0,0,0,0,0};


  if(printFlag) printf("*BOUNDARY\n");

  do{
    i=abqrecord(rec_str);
    if(i==-1) return(-1);
    if(i==-2) break;

    /* get the arguments of the string */
    args=crecord(rec_str, dat);

    /* screen dump */
    if(printFlag)
    {
      for(i=0; i<args; i++) printf("%s ",dat[i]);
      printf(" (%d)\n",i);
    }

    /* extract node, dofs, disp */
    for(i=0; i<80; i++) ival[i]=0; fval=0.;
    for(i=0; i<args; i++) ival[i]=atoi(dat[i]);
    fval=atof(dat[i]);

    /* write to file */
    if(ival[2]<ival[1]) ival[2]=ival[1];

    for(i=ival[1]; i<=ival[2]; i++)
    {
      /* if the nodes are stored in a set */
      if(ival[0]<1)
      {
        nset=getSetNr(dat[0]);
        if(nset<0)
        {
          printf("ERROR: In *BOUNDARY set:%s not defined\n",dat[0]);
          return(-1);
        }
        for(j=0; j<set[nset].anz_n; j++)
        {
          if(!setNr[0])
          {
            setNr[0]=pre_seta( BOU, "i", 0);
            if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", BOU);
          }
          if(!setNr[i])
          {
            sprintf(buffer, "%s%d", BOU,i);
            setNr[i]=pre_seta( buffer, "i", 0);
            if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", buffer);
          }
          seta(setNr[0], "n", set[nset].node[j]);
          seta(setNr[i], "n", set[nset].node[j]);
    
          if(ansFlag)
          {
            /* write in ansys-format */
            if ((i<1)||(i>6)) printf ("ERROR dof:%d not known\a\n", i);
            else fprintf(handleans, "D,%d, %s, 0.  \n", set[nset].node[j], ansys_dof[i-1]);
          }
        }
      }
      else
      {
        if(!setNr[0])
        {
          setNr[0]=pre_seta( BOU, "i", 0);
          if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", BOU);
        }
        if(!setNr[i])
        {
          sprintf(buffer, "%s%d", BOU,i);
          setNr[i]=pre_seta( buffer, "i", 0);
          if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", buffer);
        }
        seta(setNr[0], "n", ival[0]);
        seta(setNr[i], "n", ival[0]);
  
        if(ansFlag)
        {
          /* write in ansys-format */
          if ((i<1)||(i>6)) printf ("ERROR dof:%d not known\a\n", i);
          else fprintf(handleans, "D,%d, %s, 0.  \n", ival[0], ansys_dof[i-1]);
        }
      }
    }
  }while(1);     
  return(1);
}


int getEquations(unsigned char *rec_str)
{
  int i,n, neq;
  int args;
  int   nd[80], dof[80];
  double val[80];
  static int neqn=0;
  static int setDep[80], setInd[80];


  if(printFlag) printf("*EQUATION:\n");
  do
  {
    i=abqrecord(rec_str);
    if(i==-1) return(-1);
    if(i==-2) break;

    /* get the number of terms */
    neq=atoi(rec_str);

    n=0;
    do
    {
      /* get the terms */
      i=abqrecord(rec_str);
      if(i==-1) return(-1);
      if(i==-2) break;

      args=crecord(rec_str, dat);

      /* ignore trailing comma */
      args=(int)(args/3)*3;  
      /* extract node, dof, disp of each term */
      for(i=0; i<args; i+=3)
      {
        if(printFlag) printf("eq:%d %s %s %s\n", n, dat[i],dat[i+1],dat[i+2]);
        nd[n]  = atoi(dat[i]);
        dof[n] = atoi(dat[i+1]);
        val[n] = atof(dat[i+2]);
        n++;
      }
    }while(n<neq);

    /* write sets */
    if(!setDep[0]) { setDep[0]=pre_seta( DEP, "i", 0); if ( handlefbl!= NULL ) fprintf(handlefbl, "read dep.nod -n\n"); }
    if(!setInd[0]) { setInd[0]=pre_seta( IND, "i", 0); if ( handlefbl!= NULL ) fprintf(handlefbl, "read ind.nod -n\n"); }

    for(i=0; i<neq; i++)
    {
      if(!i)
      {
        if(!setDep[dof[0]])
        {
          sprintf(buffer, "%s%d", DEP,dof[0]);
          setDep[dof[0]]=pre_seta( buffer, "i", 0);
        }
        seta(setDep[0], "n", nd[0]);
        seta(setDep[dof[0]], "n", nd[0]);
      }
      else
      {
        if(!setInd[dof[i]])
        {
          sprintf(buffer, "%s%d", IND,dof[i]);
          setInd[dof[i]]=pre_seta( buffer, "i", 0);
        }
        seta(setInd[0], "n", nd[i]);
        seta(setInd[dof[i]], "n", nd[i]);
      }
    }

    if(ansFlag)
    {
      /* write to ans-file */
      neqn++;
      for (i=0;i<neq;i++)
        fprintf(handleans, "CE,%d,0,%d,%s,%8.5f\n", neqn, nd[i], ansys_dof[dof[i]-1], val[i]);
    }
  }while(1);     
  return(1);
}


int getSurfaces(unsigned char *rec_str)
{
  int i,j,n,args, length, el, eset;
  unsigned char name[MAX_LINE_LENGTH], type[MAX_LINE_LENGTH];
  int setNr, face, faceFlag=0;

  /* defaults */
  strcpy(type, "NODE"); 

  /* get the arguments of the string */
  args=crecord(rec_str, dat);

  /* extract the setname */
  sprintf(name, "+sur");
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"NAME=",5)==5) { strcpy(name,&buffer[5]); } 
    if(compare(dat[i],"TYPE=",5)==5) { strcpy(type,&dat[i][5]); } 
  }
  if(compareStrings(dat[0], "*SURFACE")<1)
  {
    printf("ignore statement: %s\n", dat[0]);
    length=abqrecord(rec_str);
    return(1);
  }    
  if(printFlag) printf("*SURFACE: NAME=%s, TYPE=%s\n", name, type);
  if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s.sur -e\n",name);

  setNr=getSetNr(name);
  if(setNr<0) setNr=pre_seta( name, "i", 0);

  /* store setNr, element and face for later use in cgx */
  if(compare( type, "NODE", 4)==4)
  {
    do{
      length=abqrecord(rec_str);
      if(length==-1) return(-1);
      if(length==-2) break;
  
      args=crecord(rec_str, dat);
      el=atoi(dat[0]);
       
      /* check if the node-number is 0, then an eset is defined */
      if (el==0)
      {
        eset=getSetNr(dat[0]);
        if(eset<0)
        {
          printf("ERROR: In *SURFACE set:%s not defined\n", dat[0]);
          return(-1);
        }
        for(j=0; j<set[eset].anz_n; j++) seta(setNr, "n", set[eset].node[j]);
      }
      else seta(setNr, "n", el);
    }while(1);
  }
  if(compare( type, "ELEM", 4)==4)
  {
    do{
      length=abqrecord(rec_str);
      if(length==-1) return(-1);
      if(length==-2) break;
  
      args=crecord(rec_str, dat);
      el=atoi(dat[0]);
       
      /* check if the node-number is 0, then an eset is defined */
      if (el==0)
      {
        eset=getSetNr(dat[0]);
        if(eset<0)
        {
          printf("ERROR: In *SURFACE set:%s not defined\n", dat[0]);
          return(-1);
        }
        for(j=0; j<set[eset].anz_e; j++) seta(setNr, "e", set[eset].elem[j]);
      }
      else
      {
        /* if single elements are specified, check if the face is also */
        seta(setNr, "e", el);
        if(args>1)
        {
          if(faceFlag==0)
          {
            faceFlag=1;
  
            /* store setNr, element and face for later use in cgx */
            anz_fb++;
            if ( (facebuf= (int ***)realloc(facebuf, (anz_fb+1)* sizeof(int **))) == NULL )
              printf("\n\n ERROR: malloc failed \n\n") ;
          
            if ( (facebuf[anz_fb]= (int **)malloc((anzx->emax+1)* sizeof(int *))) == NULL )
              printf("\n\n ERROR: malloc failed \n\n") ;
            for(i=0; i<=anzx->emax; i++) 
            {
              if ( (facebuf[anz_fb][i]= (int *)malloc(6* sizeof(int))) == NULL )
                printf("\n\n ERROR: malloc failed \n\n") ;
              for(j=0; j<6; j++)  facebuf[anz_fb][i][j]=0;
            }

            /* store the setNr at elemnr 0 and face 1 */
            facebuf[anz_fb][0][1]=setNr;
          }
          face=(int)atoi(&dat[1][strlen(dat[1])-1])-1;
          if(face>=0) facebuf[anz_fb][el][face]=1;
        }
      }
    }while(1);
  }
  return(1);
}


int getNodes(unsigned char *rec_str)
{
  int i,j,n,args,setFlag=0,setNr=0, length;
  double x,y,z;
  unsigned char set[MAX_LINE_LENGTH];

  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the setname */
  sprintf(set, "NONE");
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"NSET=",5)==5) { setFlag=1; strcpy(set,&buffer[5]); } 
  }
  if(compareStrings(dat[0], "*NODE")<1)
  {
    printf("ignore statement: %s\n", dat[0]);
    length=abqrecord(rec_str);
    return(1);
  }    
  if(printFlag) printf("*NODE: NSET=%s\n", set);

  if(setFlag)
  {
    if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s.nod -n\n",set);
    setNr=getSetNr(set);
    if(setNr<0) setNr=pre_seta( set, "i", 0);
  }
  do{
    i=abqrecord(rec_str);
    if(i==-1) return(-1);
    if(i==-2) break;

    x=y=z=0.;
    /* length=sscanf ( rec_str, "%d,%lf, %lf, %lf,\n", &n, &x,&y,&z);*/
    args=crecord(rec_str, dat);
    n=atoi(dat[0]);
    x=atof(dat[1]);
    y=atof(dat[2]);
    z=atof(dat[3]);

    if ((n>=INI_FIELD_SIZE)&&(n>anzx->nmax))
    {
      if ( (nodex = (Nodes *)realloc((Nodes *)nodex, (n+2) * sizeof(Nodes))) == NULL )
      {
        printf("\n\n ERROR: realloc failed, nodenr:%d\n\n", n) ;
        return(1);
      }
      for(j=anzx->nmax+1; j<=n; j++) nodex[j].indx=-1;
    }
    /* check if that node is already existend */
    nodex[n].nx=x;
    nodex[n].ny=y;
    nodex[n].nz=z;
    if(nodex[n].indx<0)
    {
      nodex[anzx->n].nr=n;
      nodex[n].indx=anzx->n;
      if (n >  anzx->nmax)  anzx->nmax=n;
      if (n <  anzx->nmin)  anzx->nmin=n;
      if(setFlag) seta(setNr, "n" ,n);
      anzx->n++; 
    }
#if TEST
    printf("node: %d xyz: %lf %lf %lf\n", n, nodex[n].nx,nodex[n].ny,nodex[n].nz);
#endif
    if(ansFlag)
    {
      /* write in ansys */
      fprintf(handleans, "N,%d,%12.5e,%12.5e,%12.5e,0,0,0\n", n, nodex[n].nx, nodex[n].ny, nodex[n].nz );
    }

  }while(1);     
  return(1);
}


int getMpc(unsigned char *rec_str)
{
  int i,n,length,args,setNr=0;

  if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s.nod -n\n",MPC);
  setNr=getSetNr(MPC);
  if(setNr<0) setNr=pre_seta( MPC, "i", 0);

  /* get the arguments of the string */
  /* args=crecord(rec_str, dat);     */
  
  /* scan the node-names and write to file */
  do
  {
    length=abqrecord(rec_str);

    if(length==-1) return(-1);
    if(length==-2) break;
    args=crecord(rec_str, dat);

    for(i=1; i<args; i++)
    {
      /* write in list format */
      n=atoi(dat[i]);
      if(n) seta( setNr, "n", n);
    }
  }while(1); 
  return(1);
}


int writeElemFrd(int e, int t, int g, int m, int *nd)
{
  int i, ipuf=0;

  if (anzx->e>=INI_FIELD_SIZE)
  {
    if((elemx=(Elements *)realloc((Elements *)elemx,(anzx->e+1)*sizeof(Elements)))==NULL)
    {
      printf("\n\n ERROR: realloc failed, elem-index:%d\n\n", anzx->e) ;
      return(1);
    }
  }
  elemx[anzx->e].nr   = e;
  elemx[anzx->e].type = t; 
  elemx[anzx->e].group= g;
  elemx[anzx->e].mat  = m;
  if (elemx[anzx->e].nr >  anzx->emax)  anzx->emax=elemx[anzx->e].nr;
  if (elemx[anzx->e].nr <  anzx->emin)  anzx->emin=elemx[anzx->e].nr;
  if (elemx[anzx->e].type == 1) ipuf = 8;  /* CHEXA8  */
  else if (elemx[anzx->e].type == 2) ipuf = 6;   /* PE6   */
  else if (elemx[anzx->e].type == 3) ipuf = 4;   /* TET4   */
  else if (elemx[anzx->e].type == 4) ipuf = 20;  /* HEXA20 */
  else if (elemx[anzx->e].type == 5) ipuf = 15;  /* PE15  */
  else if (elemx[anzx->e].type == 6) ipuf = 10;  /* TET10  */
  else if (elemx[anzx->e].type == 7) ipuf = 3;   /* TRI3   */
  else if (elemx[anzx->e].type == 8) ipuf = 6;   /* TRI6   */
  else if (elemx[anzx->e].type == 9) ipuf = 4;   /* QUAD4  */
  else if (elemx[anzx->e].type == 10) ipuf = 10; /* QUAD8  */
  else if (elemx[anzx->e].type == 11) ipuf = 2;  /* BEAM2   */
  else if (elemx[anzx->e].type == 12) ipuf = 3;  /* BEAM3   */
  if (ipuf==0)
  {
    printf (" elem(%d) not a known type (%d)\n", elemx[anzx->e].nr, elemx[anzx->e].type);
  }
  else
  {
    anzx->etype[elemx[anzx->e].type]++;
    for (i=0; i<ipuf; i++) { elemx[anzx->e].nod[i] = nd[i];  }
  }

  /* ini e_enqire-feld in dem e_enqire[elem[i].nr].xx abgelegt ist, statt elem[i].xx  */
  if(e>=anzx->emax)
    if((e_enqirex =(Elements *)realloc((Elements *)e_enqirex, (e+1) * sizeof(Elements))) == NULL )
      printf("\n\n ERROR: malloc failed in main() e_enqire\n\n") ;
  if(anzx->e>anzx->emax)
  {
    printf("ERROR: found more elements:%d than the maximum number:%d\n probably elements are redefined. This is not permitted.\n", anzx->e,anzx->emax );
    exit(-1);
  } 
  e_enqirex[ e ].type    = t;
  e_enqirex[ e ].group   = g;
  e_enqirex[ e ].mat     = m;
  for (i=0; i<ipuf; i++)
  {
    e_enqirex[ e ].nod[i]=nd[i];
  }
  anzx->e++;
  return(1);
}


int writeElemAns(int mat, int *selected_elems, Summen *ans, Nodes *nodex, Elements *elemx)
{
  int i,j;
  static int type=0, anz_e=0;
  int el, nd[20];  

  for(i=0; i<anzx->e; i++) if(selected_elems[elemx[i].nr])
  {

    el=elemx[i].nr;
    for(j=0; j<20; j++) nd[j]=elemx[i].nod[j];

    if(elemx[i].type==1)
    {
      if (((anz_e>0)&&(type != 1))||(anz_e==0))
      {
        fprintf (handleans, "ET, %d, 45\n", elemx[i].type);
        fprintf (handleans, "TYPE, %d\n", elemx[i].type);
      }
      fprintf (handleans, "EN,%8d,%8d,%8d,%8d,%8d,%8d,%8d,%8d,%8d \n",el, nd[0], nd[1], nd[2], nd[3],nd[4], nd[5], nd[6], nd[7] );
      type=1;
      anz_e++;
    }

    if(elemx[i].type==4)
    {
      if (((anz_e>0)&&(type != 4))||(anz_e==0))
      {
        fprintf (handleans, "ET, %d, 95\n", elemx[i].type);
        fprintf (handleans, "TYPE, %d\n", elemx[i].type);
      }
      fprintf (handleans, "EN,%8d,%8d,%8d,%8d,%8d,%8d,%8d,%8d,%8d \n",el, nd[0], nd[1], nd[2], nd[3],nd[4], nd[5], nd[6], nd[7] );
      fprintf (handleans, "EMORE,%8d,%8d,%8d,%8d,%8d,%8d,%8d,%8d \n", nd[8],  nd[9], nd[10], nd[11], nd[16], nd[17], nd[18], nd[19] );
      fprintf (handleans, "EMORE,%8d,%8d,%8d,%8d \n", nd[12], nd[13], nd[14], nd[15] );
      type=4;
      anz_e++;
    }

    if(elemx[i].type==9)
    {
      if (((anz_e>0)&&(type != 9))||(anz_e==0))
      {
	/* plastic shell */
        fprintf (handleans, "ET, %d, 43\n", elemx[i].type);
        fprintf (handleans, "TYPE, %d\n", elemx[i].type);
        /* elastic shell */
        /* fprintf (handleans, "ET, %d, 63\n", elem[i].type); */
      }
      fprintf (handleans, "EN,%8d,%8d,%8d,%8d,%8d \n",el, nd[0], nd[1], nd[2], nd[3] );
      type=9;
      anz_e++;
    }

    if(elemx[i].type==7)
    {
      if (((anz_e>0)&&(type != 7))||(anz_e==0))
      {
	/* plastic shell */
        fprintf (handleans, "ET, %d, 43\n", elemx[i].type);
        /* elastic shell */
        /* fprintf (handleans, "ET, %d, 63\n", elem[i].type); */
        fprintf (handleans, "TYPE, %d\n", elemx[i].type);
      }
      fprintf (handleans, "EN,%8d,%8d,%8d,%8d,%8d \n",el, nd[0], nd[1], nd[2], nd[2] );
      type=7;
      anz_e++;
    }

    if(elemx[i].type==6)
    {
      if (((anz_e>0)&&(type != 6))||(anz_e==0))
      {
        fprintf (handleans, "ET, %d, 92\n", elemx[i].type);
        fprintf (handleans, "TYPE, %d\n", elemx[i].type);
      }
      fprintf (handleans, "EN,%8d,%8d,%8d,%8d,%8d,%8d,%8d,%8d,%8d,\n",el, nd[0], nd[1], nd[2], nd[3],nd[4], nd[5], nd[6], nd[7]);
      fprintf (handleans, "EMORE,%8d,%8d \n", nd[8],  nd[9]);
      type=6;
      anz_e++;
    }
  }
  return(1);
}


int getMatNr(unsigned char *material)
{
  int i,l1,l2;
  static int matnr=1;
  static unsigned char materials[MAX_MATERIALS][MAX_LINE_LENGTH]; 

  for(i=1; i<matnr; i++)
  {
    l1=strlen(material);
    l2=strlen(materials[i]);
    if((l1==l2)&&(compare(material,materials[i],l1)==l1)) return(i);

  }
  strcpy(materials[matnr], material);
  matnr++;
  return(matnr-1);
}


int getAbqElement(unsigned char *rec_str, int nn, int *el, int *nd)
{
  int i,j,n;

      n=0;
      i=abqrecord(rec_str);
      if(i==-1) return(-1);
      if(i==-2) return(-2);
      i=crecord(rec_str, dat);
      *el=atoi(dat[0]);
      for(j=0; j<i; j++)
      {
        nd[n]=atoi(dat[j+1]);
        n++;
        /*  printf("a j:%d n:%d nd:%d\n",j,n-1,nd[n-1]);  */ 
        if(nd[n-1]==0) { n--; break; }
      }
      while(n<nn)
      {
        i=abqrecord(rec_str);
        if(i==-1) return(-1);
        if(i==-2) return(-2);
        i=crecord(rec_str, dat);
        for(j=0; j<i; j++)
        {
          nd[n]=atoi(dat[j]);
          n++;
          /* printf("b j:%d n:%d nd:%d\n",j,n-1,nd[n-1]); */
          if(nd[n-1]==0) { n--; break; }
	}
      }
  return(1);
}

    
int getElements(unsigned char *rec_str)
{
  int i,j,n, args, setFlag=0, setNr=0;
  unsigned char set[MAX_LINE_LENGTH], elemtyp[MAX_LINE_LENGTH];
  int el, nd[40];  


  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract element-type and setname */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"TYPE=",5)==5) strcpy(elemtyp,&dat[i][5]);
    if(compare(dat[i],"ELSET=",6)==6) { strcpy(set,&buffer[6]); setFlag=1; } 
  }
  if(printFlag) printf("*ELEMENT: TYPE=%s ELSET=%s\n", elemtyp, set);

  /* write to file */
  if(setFlag)
  { 
    if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s.elm -e\n",set);
    setNr=getSetNr(set);
    if(setNr<0) setNr=pre_seta( set, "i", 0);
  }

  if(compare(elemtyp,"C3D8",4)==4)
  {
    do{
      if(getAbqElement(rec_str, 8, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 1, 1, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if(compare(elemtyp,"C3D20",5)==5)
  {
    do{
      if(getAbqElement(rec_str, 20, &el, nd) <0) break;
      for(i=0; i<12; i++) nd[20+i]=nd[i];
      for(i=0; i<4; i++) nd[32+i]=nd[i+16];
      for(i=0; i<4; i++) nd[36+i]=nd[i+12];

      /* write the element in frd format */
      writeElemFrd(el, 4, 4, 1, &nd[20]);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if(compare(elemtyp,"C3D4",4)==4)
  {
    do{
      if(getAbqElement(rec_str, 4, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 3, 3, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if(compare(elemtyp,"C3D6",5)==5)
  {
    do{
      if(getAbqElement(rec_str, 6, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 2, 2, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if(compare(elemtyp,"C3D10",5)==5)
  {
    do{
      if(getAbqElement(rec_str, 10, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 6, 6, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if(compare(elemtyp,"C3D15",5)==5)
  {
    do{
      if(getAbqElement(rec_str, 15, &el, nd) <0) break;
      for(i=0; i<9; i++) nd[20+i]=nd[i];
      for(i=0; i<3; i++) nd[29+i]=nd[i+12];
      for(i=0; i<3; i++) nd[32+i]=nd[i+9];

      /* write the element in frd format */
      writeElemFrd(el, 5, 5, 1, &nd[20]);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if((compare(elemtyp,"DS3",3)==3)||(compare(elemtyp,"M3D3",4)==4)||(compare(elemtyp,"S3",2)==2)||(compare(elemtyp,"SFM3D3",6)==6)||(compare(elemtyp,"STRI3",5)==5))
  {
    do{
      if(getAbqElement(rec_str, 3, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 7, 7, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if((compare(elemtyp,"S6",2)==2)||(compare(elemtyp,"DS6",3)==3)||(compare(elemtyp,"M3D6",4)==4)||(compare(elemtyp,"SC6",3)==3)||(compare(elemtyp,"SFM3D6",6)==6)||(compare(elemtyp,"STRI65",5)==5))
  {
    do{
      if(getAbqElement(rec_str, 6, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 8, 8, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if((compare(elemtyp,"DS4",3)==3)||(compare(elemtyp,"M3D4",4)==4)||(compare(elemtyp,"S4",2)==2)||(compare(elemtyp,"SFM3D4",6)==6))
  {
    do{
      if(getAbqElement(rec_str, 4, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 9, 9, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if((compare(elemtyp,"M3D8",4)==4)||(compare(elemtyp,"S8",2)==2)||
          (compare(elemtyp,"DS8",3)==3)||(compare(elemtyp,"CAX8",4)==4)||
          (compare(elemtyp,"CPE8",4)==4)||(compare(elemtyp,"CPS8",4)==4)||
          (compare(elemtyp,"SC8",3)==3)||(compare(elemtyp,"SFM3D8",6)==6)||
          (compare(elemtyp,"S8R",3)==3)||(compare(elemtyp,"S8R5",4)==4))
  {
    do{
      if(getAbqElement(rec_str, 8, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 10, 10, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if((compare(elemtyp,"B21",3)==3)||(compare(elemtyp,"B31",3)==3))
  {
    do{
      if(getAbqElement(rec_str, 2, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 11, 11, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else if((compare(elemtyp,"B22",3)==3)||(compare(elemtyp,"B32",3)==3)||(compare(elemtyp,"D",1)==strlen(elemtyp)))
  {
    do{
      if(getAbqElement(rec_str, 3, &el, nd) <0) break;

      /* write the element in frd format */
      writeElemFrd(el, 12, 12, 1, nd);

      /* write set */
      if(setFlag) seta( setNr, "e", el);
    }while(1); 
  }
  else    
  {
    i=abqrecord(rec_str);
    if(i==-1) return(-1);
  }
  return(1);
}


int getMaterial(unsigned char *rec_str)
{
  int i,j,n, args;
  unsigned char mname[MAX_LINE_LENGTH];


  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the material-name */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"NAME=",5)==5) strcpy(mname,&buffer[5]);
  }
  if(printFlag) printf("*MATERIAL:%s\n", mname);

  /* get the next line */
  i=abqrecord(rec_str);
  if(i==-1) return(-1);

  /* get or define a material-number */
  matnr=getMatNr(mname);

  if(ansFlag) 
    fprintf( handleans, "! ----  MATERIAL: %s, NR: %d -----\n",mname, matnr);
  return(1);
}


int getElastic(unsigned char *rec_str)
{
  int i,j,n, args,length;
  static unsigned char **dat=NULL;
  unsigned char type[MAX_LINE_LENGTH];
  double E,nue,t;

  if((dat = (unsigned char **)realloc((unsigned char **)dat, (80)*sizeof(unsigned char*)))==NULL)
    printf("\n\n ERROR: realloc failed for **dat\a\n" );
    for (i=0; i<80; i++)
    {
      dat[i]=NULL;
      if((dat[i] = (unsigned char *)realloc((unsigned char *)dat[i], (MAX_LINE_LENGTH)*sizeof(unsigned char)))==NULL)
        printf("\n\n ERROR: realloc failed for *dat\a\n" );
    }

  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the type */
  strcpy(type,"ISO"); /*default */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"TYPE=",5)==5) strcpy(type,&dat[i][5]);
  }
  if(printFlag) printf("*ELASTIC: TYPE:%s\n", type);

  /* scan the properties and write to file */
  if(compare(type,"ISO",3)==3)
  {
    i=0;
    do{
      length=abqrecord(rec_str);
      if(length==-1) return(-1);
      if(length==-2) break;
      length=sscanf ( rec_str, "%lf, %lf, %lf", &E,&nue,&t);
      if(printFlag) printf("temp:%lf E:%lf nue:%lf\n",t,E,nue); 
      if(ansFlag) 
      {
        i++;
        /* write in ansys format */
        fprintf(handleans, "MPTEMP, %d, %lf,\n",i, t);
        fprintf(handleans, "MPDATA, EX, %d, %d, %e,\n",matnr, i, E);
        fprintf(handleans, "MPDATA, NUXY, %d, %d, %lf,\n",matnr, i, nue);
      }
    }while(1); 
  }
  else
  {
    printf(" WARNING: TYPE:%s not known\a\n",type);
      length=abqrecord(rec_str);
      if(length==-1) return(-1);
  }
  return(1);
}


int getExpansion(unsigned char *rec_str)
{
  int i,j,n, args,length;
  unsigned char type[MAX_LINE_LENGTH];
  double a,t;


  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the type */
  strcpy(type,"ISO"); /*default */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"TYPE=",5)==5) strcpy(type,&dat[i][5]);
  }
  if(printFlag) printf("*EXPANSION:TYPE:%s\n", type);

  /* scan the properties and write to file */
  if(compare(type,"ISO",3)==3)
  {
    i=0;
    do{
      length=abqrecord(rec_str);
      if(length==-1) return(-1);
      if(length==-2) break;
      length=sscanf ( rec_str, "%lf, %lf", &a,&t);
      if(printFlag) printf("temp:%lf alpha:%lf\n",t,a); 
      if(ansFlag) 
      {
        i++;
        /* write in ansys format */
        fprintf(handleans, "MPTEMP, %d, %lf,\n",i, t);
        fprintf(handleans, "MPDATA, ALPX, %d, %d, %e,\n",matnr, i, a);
      }
    }while(1); 
  }
  else
  {
    printf(" WARNING: TYPE:%s not known\a\n",type);
      length=abqrecord(rec_str);
      if(length==-1) return(-1);
  }
  return(1);
}


int getDensity(unsigned char *rec_str)
{
  int length;
  double rho=0.,t=0.;

  /* scan the properties and write to file */
  do{
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;
    length=sscanf ( rec_str, "%lf, %lf", &rho,&t);
    if(printFlag) printf("*DENSITY: %e %lf\n", rho, t);
 
    if(ansFlag) 
    {
      /* write in ansys format */
      fprintf(handleans, "MP,DENS, %d, %e,\n", matnr, rho);
    }
  }while(1); 
  return(1);
}


int writeTempFrd(int nd, double t)
{

  if (nd>anzx->nmax)
  {
    printf("ERROR: found nodeNr:%d in Dataset higher than in Geometry allocated:%d\n"
    , nd, anzx->nmax);
    exit(-1);
  }
  lcasex[anzx->l].dat[0][nd]       = t;

  if (lcasex[anzx->l].dat[0][nd] >  lcasex[anzx->l].max[0])
  {  lcasex[anzx->l].max[0]=lcasex[anzx->l].dat[0][nd]; lcasex[anzx->l].nmax[0]=nd; }
  if (lcasex[anzx->l].dat[0][nd] <  lcasex[anzx->l].min[0])
  {  lcasex[anzx->l].min[0]=lcasex[anzx->l].dat[0][nd]; lcasex[anzx->l].nmin[0]=nd; }
  return(1);
}

int writeCloadFrd(int nd, double f, int dof)
{

  if (nd>anzx->nmax)
  {
    printf("ERROR: found nodeNr:%d in Dataset higher than in Geometry allocated:%d\n"
    , nd, anzx->nmax);
    exit(-1);
  }
  lcasex[anzx->l].dat[dof][nd]       = f;
  if (lcasex[anzx->l].dat[dof][nd] > lcasex[anzx->l].max[dof]) { lcasex[anzx->l].max[dof]=lcasex[anzx->l].dat[dof][nd]; lcasex[anzx->l].nmax[dof]=nd; }
  if (lcasex[anzx->l].dat[dof][nd] < lcasex[anzx->l].min[dof]) { lcasex[anzx->l].min[dof]=lcasex[anzx->l].dat[dof][nd]; lcasex[anzx->l].nmin[dof]=nd; }
  return(1);
}


int getElemFaceNodes(int el, int face, int *nface)
{
  int i,ipuf=0;

  if(e_enqirex[el].type == 1)   /* HEXA8  */
  {
    if (face==0)
    {  
      nface[3]=e_enqirex[el].nod[0];
      nface[2]=e_enqirex[el].nod[1];
      nface[1]=e_enqirex[el].nod[2];
      nface[0]=e_enqirex[el].nod[3];
    } 			               	 
    else if (face==1)           	 
    {  			               	 
      nface[3]=e_enqirex[el].nod[4];
      nface[2]=e_enqirex[el].nod[7];
      nface[1]=e_enqirex[el].nod[6];
      nface[0]=e_enqirex[el].nod[5];
    } 			               	 
    else if (face==2)           	 
    {  			               	 
      nface[3]=e_enqirex[el].nod[0];
      nface[2]=e_enqirex[el].nod[4];
      nface[1]=e_enqirex[el].nod[5];
      nface[0]=e_enqirex[el].nod[1];
    } 			               	 
    else if (face==3)           	 
    {  			               	 
      nface[3]=e_enqirex[el].nod[1];
      nface[2]=e_enqirex[el].nod[5];
      nface[1]=e_enqirex[el].nod[6];
      nface[0]=e_enqirex[el].nod[2];
    } 			               	 
    else if (face==4)           	 
      {   			               	 
      nface[3]=e_enqirex[el].nod[2];
      nface[2]=e_enqirex[el].nod[6];
      nface[1]=e_enqirex[el].nod[7];
      nface[0]=e_enqirex[el].nod[3];
    } 			               	 
    else if (face==5)           	 
    {   			               	 
      nface[3]=e_enqirex[el].nod[3];
      nface[2]=e_enqirex[el].nod[7];
      nface[1]=e_enqirex[el].nod[4];
      nface[0]=e_enqirex[el].nod[0];
    }
    else  return(-1);
    return(4);
  } 
  else if (e_enqirex[el].type == 2)   /* PE6   */
  {
    printf("Error: Element:%d of type:%d is not supported in this function\n",el,e_enqirex[el].type);
  }
  else if (e_enqirex[el].type == 3)   /* TET4   */
  {
    if (face==0)
    {  
      nface[0]=e_enqirex[el].nod[0];
      nface[1]=e_enqirex[el].nod[2];
      nface[2]=e_enqirex[el].nod[1];
    } 			               	 
    else if (face==1)
      {  
      nface[0]=e_enqirex[el].nod[0];
      nface[1]=e_enqirex[el].nod[1];
      nface[2]=e_enqirex[el].nod[3];
    } 			               	 
    else if (face==2)
      {  
      nface[0]=e_enqirex[el].nod[1];
      nface[1]=e_enqirex[el].nod[2];
      nface[2]=e_enqirex[el].nod[3];
    } 			               	 
    else if (face==3)
      {  
      nface[0]=e_enqirex[el].nod[2];
      nface[1]=e_enqirex[el].nod[0];
      nface[2]=e_enqirex[el].nod[3];
    } 			               	 
    else  return(-1);
    return(3);
  }
  else if (e_enqirex[el].type == 4)   /* HEXA20 */
  {
    if (face==0)
    {  
      nface[3]=e_enqirex[el].nod[0];
      nface[2]=e_enqirex[el].nod[1];
      nface[1]=e_enqirex[el].nod[2];
      nface[0]=e_enqirex[el].nod[3];
      nface[6]=e_enqirex[el].nod[8];
      nface[5]=e_enqirex[el].nod[9];
      nface[4]=e_enqirex[el].nod[10];
      nface[7]=e_enqirex[el].nod[11];
    } 			               	 
    else if (face==1)           	 
    {
      nface[3]=e_enqirex[el].nod[4];
      nface[2]=e_enqirex[el].nod[7];
      nface[1]=e_enqirex[el].nod[6];
      nface[0]=e_enqirex[el].nod[5];
      nface[6]=e_enqirex[el].nod[19];
      nface[5]=e_enqirex[el].nod[18];
      nface[4]=e_enqirex[el].nod[17];
      nface[7]=e_enqirex[el].nod[16];
    } 			               	 
    else if (face==2)           	 
    {  			               	 
      nface[3]=e_enqirex[el].nod[0];
      nface[2]=e_enqirex[el].nod[4];
      nface[1]=e_enqirex[el].nod[5];
      nface[0]=e_enqirex[el].nod[1];
      nface[6]=e_enqirex[el].nod[12];
      nface[5]=e_enqirex[el].nod[16];
      nface[4]=e_enqirex[el].nod[13];
      nface[7]=e_enqirex[el].nod[8];
    } 			               	 
    else if (face==3)           	 
    {  			               	 
      nface[3]=e_enqirex[el].nod[1];
      nface[2]=e_enqirex[el].nod[5];
      nface[1]=e_enqirex[el].nod[6];
      nface[0]=e_enqirex[el].nod[2];
      nface[6]=e_enqirex[el].nod[13];
      nface[5]=e_enqirex[el].nod[17];
      nface[4]=e_enqirex[el].nod[14];
      nface[7]=e_enqirex[el].nod[9];
    } 			               	 
    else if (face==4)           	 
      {  			               	 
      nface[3]=e_enqirex[el].nod[2];
      nface[2]=e_enqirex[el].nod[6];
      nface[1]=e_enqirex[el].nod[7];
      nface[0]=e_enqirex[el].nod[3];
      nface[6]=e_enqirex[el].nod[14];
      nface[5]=e_enqirex[el].nod[18];
      nface[4]=e_enqirex[el].nod[15];
      nface[7]=e_enqirex[el].nod[10];
    } 			               	 
    else if (face==5)           	 
      {  			               	 
      nface[3]=e_enqirex[el].nod[3];
      nface[2]=e_enqirex[el].nod[7];
      nface[1]=e_enqirex[el].nod[4];
      nface[0]=e_enqirex[el].nod[0];
      nface[6]=e_enqirex[el].nod[15];
      nface[5]=e_enqirex[el].nod[19];
      nface[4]=e_enqirex[el].nod[12];
      nface[7]=e_enqirex[el].nod[11];
    } 
    else  return(-1);
    return(8);
  }
  else if (e_enqirex[el].type == 5)   /* PE15  */
  {
    printf("Error: Element:%d of type:%d is not supported in this function\n",el,e_enqirex[el].type);
  }
  else if (e_enqirex[el].type == 6)   /* TET10  */
  {
    if (face==0)
    {  
      nface[0]=e_enqirex[el].nod[0];
      nface[3]=e_enqirex[el].nod[6];
      nface[1]=e_enqirex[el].nod[2];
      nface[4]=e_enqirex[el].nod[5];
      nface[2]=e_enqirex[el].nod[1];
      nface[5]=e_enqirex[el].nod[4];
    } 			               	 
    else if (face==1)           	 
    {  			               	 
      nface[0]=e_enqirex[el].nod[0];
      nface[3]=e_enqirex[el].nod[4];
      nface[1]=e_enqirex[el].nod[1];
      nface[4]=e_enqirex[el].nod[8];
      nface[2]=e_enqirex[el].nod[3];
      nface[5]=e_enqirex[el].nod[7];
    } 			               	 
    else if (face==2)           	 
    {  			               	 
      nface[0]=e_enqirex[el].nod[1];
      nface[3]=e_enqirex[el].nod[5];
      nface[1]=e_enqirex[el].nod[2];
      nface[4]=e_enqirex[el].nod[9];
      nface[2]=e_enqirex[el].nod[3];
      nface[5]=e_enqirex[el].nod[8];
    } 			               	 
    else if (face==3)           	 
    {			               	 
      nface[0]=e_enqirex[el].nod[2];
      nface[3]=e_enqirex[el].nod[6];
      nface[1]=e_enqirex[el].nod[0];
      nface[4]=e_enqirex[el].nod[7];
      nface[2]=e_enqirex[el].nod[3];
      nface[5]=e_enqirex[el].nod[9];
    } 			               	 
    else  return(-1);
    return(6);
  }
  else if (e_enqirex[el].type == 7) ipuf = 3;   /* TRI3   */
  else if (e_enqirex[el].type == 8) ipuf = 6;   /* TRI6   */
  else if (e_enqirex[el].type == 9) ipuf = 4;   /* QUAD4  */
  else if (e_enqirex[el].type == 10) ipuf = 8; /* QUAD8  */
  else if (e_enqirex[el].type == 11) ipuf = 2;  /* BEAM2   */
  else if (e_enqirex[el].type == 12) ipuf = 3;  /* BEAM3   */

  for (i=0; i<ipuf; i++) nface[i]=e_enqirex[el].nod[i];
  return(ipuf);
}


int writeFaceValsFrd(int el, int face, int nr, double *val)
{
  int i,j, anzn, nface[20];
  unsigned char setName[MAX_LINE_LENGTH], nodeName[MAX_LINE_LENGTH];

  if (el>anzx->emax)
  {
    printf("ERROR: found elementNr:%d in Dataset higher than in Geometry allocated:%d\n"
    , el, anzx->emax);
    exit(-1);
  }
  if(el>0)
  {
    anzn=getElemFaceNodes(el,face,nface);
    for (i=0; i<anzn; i++)
    {
      /* load */
      for(j=0; j<nr; j++) lcasex[anzx->l].dat[j][nface[i]]+=val[j];
      /* number of loads per node */
      lcasex[anzx->l].dat[nr][nface[i]]++;

      /* add node to special set */
      if(MAKE_SETS_DEF)
      {
        for(j=0; j<nr; j++)
        {
          sprintf( setName, "_%d", (int)val[j]); 
          sprintf( nodeName, "%d", nface[i]); 
          pre_seta( setName, "n", nodeName);
        }
      }
    }
  }
  else /* all elems are scanned. divide the load by the amount of nodes */
  {
    for (i=0; i<anzx->n; i++)
    {
      if(lcasex[anzx->l].dat[nr][nodex[i].nr])
      {
        for(j=0; j<nr; j++) lcasex[anzx->l].dat[j][nodex[i].nr]/= lcasex[anzx->l].dat[nr][nodex[i].nr];
      }
      for(j=0; j<lcasex[anzx->l].ncomps; j++)
      {
        if (lcasex[anzx->l].dat[j][nodex[i].nr] >  lcasex[anzx->l].max[j]) 
        {
          lcasex[anzx->l].max[j]=lcasex[anzx->l].dat[j][nodex[i].nr];
          lcasex[anzx->l].nmax[j]=nodex[i].nr;
        }
        if (lcasex[anzx->l].dat[j][nodex[i].nr] <  lcasex[anzx->l].min[j])
        {
          lcasex[anzx->l].min[j]=lcasex[anzx->l].dat[j][nodex[i].nr];
          lcasex[anzx->l].nmin[j]=nodex[i].nr;
        }
      }
    }
  }
  return(1);
}



int getTemperatures(unsigned char *rec_str)
{
  int i,j; 
  int length, nd, foundLoads=0, nset, args, comp;
  double t;
  static int ld7=1;

  if(printFlag) printf("*TEMPERATURE\n");
  /* scan the properties and write to file */

  sprintf( lcasex[anzx->l].name,"TEMP%d",ld7);
  printf ("lcase[%d].name= %s\n", anzx->l, lcasex[anzx->l].name);
  comp=0;
  lcasex[anzx->l].ncomps=1;
  lcasex[anzx->l].irtype=1;

  strcpy(lcasex[anzx->l].analysis_name,"");
  strcpy(lcasex[anzx->l].dataset_name,"");
  strcpy(lcasex[anzx->l].dataset_text,"");
  lcasex[anzx->l].step_number=anzx->l;
  lcasex[anzx->l].analysis_type=1;

  if ( (lcasex[anzx->l].nmax = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].nmin = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].max = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].min = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].dat = (float **)malloc(lcasex[anzx->l].ncomps * sizeof(float *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].compName = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icname = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].ictype = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind1 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind2 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].iexist = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  for(i=comp; i<lcasex[anzx->l].ncomps; i++)
  {
    if ( (lcasex[anzx->l].dat[i] = (float *)calloc( (anzx->nmax+1), sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );	               
    if ( (lcasex[anzx->l].compName[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    if ( (lcasex[anzx->l].icname[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    lcasex[anzx->l].max[i]=-MAXVALUE;
    lcasex[anzx->l].min[i]=MAXVALUE;

    lcasex[anzx->l].ictype[i] = 1;
    lcasex[anzx->l].icind1[i] = 0;
    lcasex[anzx->l].icind2[i] = 0;
    lcasex[anzx->l].iexist[i] = 0;
    lcasex[anzx->l].max[i]=-MAXVALUE;  lcasex[anzx->l].min[i]=MAXVALUE;
  }
  
  strcpy ( lcasex[anzx->l].compName[comp], "Temp   ");

  do{
    length=abqrecord(rec_str);
    if(length==-1) { anzx->l++; return(-1); }
    if(length==-2) break;
    foundLoads=1;
 
    args=crecord(rec_str, dat);
    nd=atoi(dat[0]);
    t=atof(dat[1]);
    /* check if the node-number is 0, then a set was defined */
    if (nd==0)
    {
      nset=getSetNr(dat[0]);
      if(nset<0)
      {
        printf("ERROR: In *TEMPERATURE set:%s not defined\n", dat[0]);
        return(-1);
      }
      for(j=0; j<set[nset].anz_n; j++)
      {
        writeTempFrd(set[nset].node[j], t);
        if(ansFlag)
        {
          /* write in ansys format */
          fprintf(handleans, "BF, %d, TEMP, %lf\n", set[nset].node[j], t);
        }
      }
    }
    else
    {
      writeTempFrd(nd, t);
      if(ansFlag)
      {
        /* write in ansys format */
        fprintf(handleans, "BF, %d, TEMP, %lf\n", nd, t);
      }
    }
  }while(1); 
  if(foundLoads) { anzx->l++; ld7++; }
  return(1);
}


int getCload(unsigned char *rec_str)
{
  int i,j; 
  int length, nd, dof, foundLoads=0,nset,args;
  double f;
  static int setNr[7]={0,0,0,0,0,0,0};
  static int ld4=1;

  if(printFlag) printf("*CLOAD:\n");
  /* scan the properties and write to file */

  /* ini lcasex */
  sprintf( lcasex[anzx->l].name,"%s%d", CLO,ld4);
  printf ("lcasex[%d].name= %s\n", anzx->l, lcasex[anzx->l].name);
  lcasex[anzx->l].ncomps=3;
  lcasex[anzx->l].irtype=1;

  strcpy(lcasex[anzx->l].analysis_name,"");
  strcpy(lcasex[anzx->l].dataset_name,"");
  strcpy(lcasex[anzx->l].dataset_text,"");
  lcasex[anzx->l].step_number=anzx->l;
  lcasex[anzx->l].analysis_type=1;

  if ( (lcasex[anzx->l].nmax = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].nmin = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].max = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].min = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].dat = (float **)malloc(lcasex[anzx->l].ncomps * sizeof(float *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].compName = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icname = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].ictype = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind1 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind2 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].iexist = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  for(i=0; i<lcasex[anzx->l].ncomps; i++)
  {
    if ( (lcasex[anzx->l].dat[i] = (float *)calloc( (anzx->nmax+1), sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );	               
    if ( (lcasex[anzx->l].compName[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    if ( (lcasex[anzx->l].icname[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    lcasex[anzx->l].max[i]=-MAXVALUE;
    lcasex[anzx->l].min[i]=MAXVALUE;

    lcasex[anzx->l].ictype[i] = 2;
    lcasex[anzx->l].icind1[i] = i+1;
    lcasex[anzx->l].icind2[i] = 0;
    lcasex[anzx->l].iexist[i] = 0;
  }
  
  strcpy ( lcasex[anzx->l].compName[0], "xx ");
  strcpy ( lcasex[anzx->l].compName[1], "yy ");
  strcpy ( lcasex[anzx->l].compName[2], "zz ");

  do{
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;

    args=crecord(rec_str, dat);
    nd=atoi(dat[0]);
    dof=atoi(dat[1]);
    f=atof(dat[2]);
    
    /* check if the node-number is 0, then a set was defined */
    if (nd==0)
    {
      nset=getSetNr(dat[0]);
      if(nset<0)
      {
        printf("ERROR: In *CLOAD set:%s not defined\n", dat[0]);
        return(-1);
      }
      for(j=0; j<set[nset].anz_n; j++)
      {
        foundLoads=1;
        if(!setNr[0])
        {
          setNr[0]=pre_seta( CLO, "i", 0);
          if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", CLO);
        }
        if(!setNr[dof])
        {
          sprintf(buffer,"%s%d", CLO, dof);
          setNr[dof]=pre_seta( buffer, "i", 0);
          if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", buffer);
        }
        seta(setNr[0], "n", set[nset].node[j]);
        seta(setNr[dof], "n", set[nset].node[j]);
        writeCloadFrd(set[nset].node[j], f, dof-1);
      }
    }
    else
    {
      foundLoads=1;
      if(!setNr[0])
      {
        setNr[0]=pre_seta( CLO, "i", 0);
        if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", CLO);
      }
      if(!setNr[dof])
      {
        sprintf(buffer,"%s%d", CLO, dof);
        setNr[dof]=pre_seta( buffer, "i", 0);
        if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", buffer);
      }
      seta(setNr[0], "n", nd);
      seta(setNr[dof], "n", nd);
      writeCloadFrd(nd, f, dof-1);
    }

    /* write in ansys format */
    if(ansFlag)
    {
      if ((dof<1)||(dof>6)) printf ("ERROR dof:%d not known\a\n", dof);
      else fprintf(handleans, "F,%d, %s, %lf\n", nd, ansys_frc[dof-1], f);
    }
  }while(1); 
  if(foundLoads) { anzx->l++;  ld4++; }
  return(1);
}


int getCflux(unsigned char *rec_str)
{
  int i,j; 
  int length, nd, dof, foundLoads=0,nset,args;
  double f;
  static int setNr[7]={0,0,0,0,0,0,0};
  static int ld4=1;

  if(printFlag) printf("*CFLUX:\n");
  /* scan the properties and write to file */

  /* ini lcasex */
  sprintf( lcasex[anzx->l].name,"%s%d", CFLX,ld4);
  printf ("lcase[%d].name= %s\n", anzx->l, lcasex[anzx->l].name);
  lcasex[anzx->l].ncomps=1;
  lcasex[anzx->l].irtype=1;

  strcpy(lcasex[anzx->l].analysis_name,"");
  strcpy(lcasex[anzx->l].dataset_name,"");
  strcpy(lcasex[anzx->l].dataset_text,"");
  lcasex[anzx->l].step_number=anzx->l;
  lcasex[anzx->l].analysis_type=1;

  if ( (lcasex[anzx->l].nmax = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].nmin = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].max = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].min = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].dat = (float **)malloc(lcasex[anzx->l].ncomps * sizeof(float *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].compName = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icname = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].ictype = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind1 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind2 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].iexist = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  for(i=0; i<lcasex[anzx->l].ncomps; i++)
  {
    if ( (lcasex[anzx->l].dat[i] = (float *)calloc( (anzx->nmax+1), sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcasex[anzx->l].compName[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    if ( (lcasex[anzx->l].icname[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    lcasex[anzx->l].max[i]=-MAXVALUE;
    lcasex[anzx->l].min[i]=MAXVALUE;

    lcasex[anzx->l].ictype[i] = 1;
    lcasex[anzx->l].icind1[i] = i+1;
    lcasex[anzx->l].icind2[i] = 0;
    lcasex[anzx->l].iexist[i] = 0;
  }
  
  strcpy ( lcasex[anzx->l].compName[0], "flux");

  do{
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;

    args=crecord(rec_str, dat);
    nd=atoi(dat[0]);
    dof=0;
    f=atof(dat[2]);
    
    /* check if the node-number is 0, then a set was defined */
    if (nd==0)
    {
      nset=getSetNr(dat[0]);
      if(nset<0)
      {
        printf("ERROR: In *CFLUX set:%s not defined\n", dat[0]);
        return(-1);
      }
      for(j=0; j<set[nset].anz_n; j++)
      {
        foundLoads=1;
        if(!setNr[0])
        {
          setNr[0]=pre_seta( CFLX, "i", 0);
          if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", CFLX);
        }
        seta(setNr[0], "n", set[nset].node[j]);
        writeCloadFrd(set[nset].node[j], f, dof);
      }
    }
    else
    {
      foundLoads=1;
      if(!setNr[0])
      {
        setNr[0]=pre_seta( CFLX, "i", 0);
        if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -n\n", CFLX);
      }
      seta(setNr[0], "n", nd);
      writeCloadFrd(nd, f, dof);
    }

    /* write in ansys format */
    if(ansFlag)
    {
	printf("ERROR: CFLUX for ansys not yet supported\n");
    }
  }while(1); 
  if(foundLoads) { anzx->l++;  ld4++; }
  return(1);
}


int getDload(unsigned char *rec_str)
{
  int j,i; 
  int length, el, eset,args,face, foundLoads=0;
  static int ld4=1;
  int setNr;
  double vals[1];

  if(printFlag) printf("*DLOAD:\n");
  /* scan the properties and write to file */

  /* ini lcase */
  sprintf( lcasex[anzx->l].name,"%s%d", DLO,ld4);
  printf ("lcase[%d].name= %s\n", anzx->l, lcasex[anzx->l].name);
  lcasex[anzx->l].ncomps=2;
  lcasex[anzx->l].irtype=1;

  strcpy(lcasex[anzx->l].analysis_name,"");
  strcpy(lcasex[anzx->l].dataset_name,"");
  strcpy(lcasex[anzx->l].dataset_text,"");
  lcasex[anzx->l].step_number=anzx->l;
  lcasex[anzx->l].analysis_type=1;

  if ( (lcasex[anzx->l].nmax = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].nmin = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].max = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].min = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].dat = (float **)malloc(lcasex[anzx->l].ncomps * sizeof(float *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].compName = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icname = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].ictype = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind1 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind2 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].iexist = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  for(i=0; i<lcasex[anzx->l].ncomps; i++)
  {
    if ( (lcasex[anzx->l].dat[i] = (float *)calloc( (anzx->nmax+1), sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );	               
    if ( (lcasex[anzx->l].compName[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    if ( (lcasex[anzx->l].icname[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    lcasex[anzx->l].max[i]=-MAXVALUE;
    lcasex[anzx->l].min[i]=MAXVALUE;

    lcasex[anzx->l].ictype[i] = 1;
    lcasex[anzx->l].icind1[i] = i+1;
    lcasex[anzx->l].icind2[i] = 0;
    lcasex[anzx->l].iexist[i] = 0;
  }
  strcpy ( lcasex[anzx->l].compName[0], "load");
  strcpy ( lcasex[anzx->l].compName[1], "nodes");

  setNr=getSetNr(lcasex[anzx->l].name);
  if(setNr<0)
  {
    setNr=pre_seta( lcasex[anzx->l].name, "i", 0);
    if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -e\n",lcasex[anzx->l].name );
  }

  /* store setNr, element and face for later use in cgx */
  anz_fb++;
  if ( (facebuf= (int ***)realloc(facebuf, (anz_fb+1)* sizeof(int **))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;

  if ( (facebuf[anz_fb]= (int **)malloc((anzx->emax+1)* sizeof(int *))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;
  for(i=0; i<=anzx->emax; i++) 
  {
    if ( (facebuf[anz_fb][i]= (int *)malloc(6* sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failed \n\n") ;
    for(j=0; j<6; j++)  facebuf[anz_fb][i][j]=0;
  }
  /* store the setNr at elemnr 0 and face 1 */
  facebuf[anz_fb][0][1]=setNr;

  do{
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;

    args=crecord(rec_str, dat);
    el=atoi(dat[0]);
    vals[0]=atof(dat[2]);
    face=(int)atoi(&dat[1][strlen(dat[1])-1])-1;
     
    /* check if the node-number is 0, then an eset is defined */
    if (el==0)
    {
      eset=getSetNr(dat[0]);
      if(eset<0)
      {
        printf("ERROR: In *DLOAD set:%s not defined\n", dat[0]);
        return(-1);
      }
      for(j=0; j<set[eset].anz_e; j++)
      {
        foundLoads=1;
        if(face>=0) facebuf[anz_fb][el][face]=1;
        /* seta(setNr, "e", set[eset].elem[j]); */
        writeFaceValsFrd(set[eset].elem[j], face, 1, vals);
      }
    }
    else
    {
      foundLoads=1;
      if(face>=0) facebuf[anz_fb][el][face]=1;
      /* seta(setNr, "e", el); */
      writeFaceValsFrd(el, face, 1, vals);
    }

    /* write in ansys format */
    if(ansFlag)
    {
      printf ("ERROR dload not supported\a\n");
    }
  }while(1);
  printf("foundLoads:%d\n",foundLoads);  
  if(foundLoads) {  writeFaceValsFrd(-1, 0, 1, 0); anzx->l++;  ld4++; }
  return(1);
}


int getDflux(unsigned char *rec_str)
{
  int j,i; 
  int length, el, eset,args,face, foundLoads=0;
  static int ld4=1;
  int setNr;
  double vals[1];


  if(printFlag) printf("*FLUX:\n");
  /* scan the properties and write to file */

  /* ini lcasex */
  sprintf( lcasex[anzx->l].name,"%s%d", DFLX,ld4);
  printf ("lcasex[%d].name= %s\n", anzx->l, lcasex[anzx->l].name);
  lcasex[anzx->l].ncomps=2;
  lcasex[anzx->l].irtype=1;

  strcpy(lcasex[anzx->l].analysis_name,"");
  strcpy(lcasex[anzx->l].dataset_name,"");
  strcpy(lcasex[anzx->l].dataset_text,"");
  lcasex[anzx->l].step_number=anzx->l;
  lcasex[anzx->l].analysis_type=1;

  if ( (lcasex[anzx->l].nmax = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].nmin = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].max = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].min = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].dat = (float **)malloc(lcasex[anzx->l].ncomps * sizeof(float *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].compName = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icname = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].ictype = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind1 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind2 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].iexist = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  for(i=0; i<lcasex[anzx->l].ncomps; i++)
  {
    if ( (lcasex[anzx->l].dat[i] = (float *)calloc( (anzx->nmax+1), sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );	               
    if ( (lcasex[anzx->l].compName[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    if ( (lcasex[anzx->l].icname[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    lcasex[anzx->l].max[i]=-MAXVALUE;
    lcasex[anzx->l].min[i]=MAXVALUE;

    lcasex[anzx->l].ictype[i] = 1;
    lcasex[anzx->l].icind1[i] = i+1;
    lcasex[anzx->l].icind2[i] = 0;
    lcasex[anzx->l].iexist[i] = 0;
  }
  strcpy ( lcasex[anzx->l].compName[0], "load");
  strcpy ( lcasex[anzx->l].compName[1], "nodes");

  setNr=getSetNr(lcasex[anzx->l].name);
  if(setNr<0)
  {
    setNr=pre_seta( lcasex[anzx->l].name, "i", 0);
    if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -e\n",lcasex[anzx->l].name );
  }

  /* store setNr, element and face for later use in cgx */
  anz_fb++;
  if ( (facebuf= (int ***)realloc(facebuf, (anz_fb+1)* sizeof(int **))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;

  if ( (facebuf[anz_fb]= (int **)malloc((anzx->emax+1)* sizeof(int *))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;
  for(i=0; i<=anzx->emax; i++) 
  {
    if ( (facebuf[anz_fb][i]= (int *)malloc(6* sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failed \n\n") ;
    for(j=0; j<6; j++)  facebuf[anz_fb][i][j]=0;
  }
  /* store the setNr at elemnr 0 and face 1 */
  facebuf[anz_fb][0][1]=setNr;

  do{
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;

    args=crecord(rec_str, dat);
    el=atoi(dat[0]);
    vals[0]=atof(dat[2]);
    face=(int)atoi(&dat[1][strlen(dat[1])-1])-1;
     
    /* check if the node-number is 0, then an eset is defined */
    if (el==0)
    {
      eset=getSetNr(dat[0]);
      if(eset<0)
      {
        printf("ERROR: In *DFLUX set:%s not defined\n", dat[0]);
        return(-1);
      }
      for(j=0; j<set[eset].anz_e; j++)
      {
        foundLoads=1;
        if(face>=0) facebuf[anz_fb][el][face]=1;
        /* seta(setNr, "e", set[eset].elem[j]); */
        writeFaceValsFrd(set[eset].elem[j], face, 1, vals);
      }
    }
    else
    {
      foundLoads=1;
      if(face>=0) facebuf[anz_fb][el][face]=1;
      /* seta(setNr, "e", el); */
      writeFaceValsFrd(el, face, 1, vals);
    }

    /* write in ansys format */
    if(ansFlag)
    {
      printf ("ERROR dflux not supported\a\n");
    }
  }while(1); 
  if(foundLoads) {  writeFaceValsFrd(-1, 0, 1, 0); anzx->l++;  ld4++; }
  return(1);
}


int getRadiation(unsigned char *rec_str)
{
  int j,i; 
  int length, el, eset,args,face, foundLoads=0;
  static int ld4=1;
  int setNr;
  double vals[2];


  if(printFlag) printf("*RADIATION:\n");
  /* scan the properties and write to file */

  /* ini lcasex */
  sprintf( lcasex[anzx->l].name,"%s%d", RAD,ld4);
  printf ("lcase[%d].name= %s\n", anzx->l, lcasex[anzx->l].name);
  lcasex[anzx->l].ncomps=3;
  lcasex[anzx->l].irtype=1;

  strcpy(lcasex[anzx->l].analysis_name,"");
  strcpy(lcasex[anzx->l].dataset_name,"");
  strcpy(lcasex[anzx->l].dataset_text,"");
  lcasex[anzx->l].step_number=anzx->l;
  lcasex[anzx->l].analysis_type=1;

  if ( (lcasex[anzx->l].nmax = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].nmin = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].max = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].min = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].dat = (float **)malloc(lcasex[anzx->l].ncomps * sizeof(float *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].compName = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icname = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].ictype = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind1 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind2 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].iexist = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  for(i=0; i<lcasex[anzx->l].ncomps; i++)
  {
    if ( (lcasex[anzx->l].dat[i] = (float *)calloc( (anzx->nmax+1), sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );	               
    if ( (lcasex[anzx->l].compName[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    if ( (lcasex[anzx->l].icname[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    lcasex[anzx->l].max[i]=-MAXVALUE;
    lcasex[anzx->l].min[i]=MAXVALUE;

    lcasex[anzx->l].ictype[i] = 1;
    lcasex[anzx->l].icind1[i] = i+1;
    lcasex[anzx->l].icind2[i] = 0;
    lcasex[anzx->l].iexist[i] = 0;
  }
  strcpy ( lcasex[anzx->l].compName[0], "temp");
  strcpy ( lcasex[anzx->l].compName[1], "emis");
  strcpy ( lcasex[anzx->l].compName[2], "nodes");

  setNr=getSetNr(lcasex[anzx->l].name);
  if(setNr<0)
  {
    setNr=pre_seta( lcasex[anzx->l].name, "i", 0);
    if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -e\n",lcasex[anzx->l].name );
  }

  /* store setNr, element and face for later use in cgx */
  anz_fb++;
  if ( (facebuf= (int ***)realloc(facebuf, (anz_fb+1)* sizeof(int **))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;

  if ( (facebuf[anz_fb]= (int **)malloc((anzx->emax+1)* sizeof(int *))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;
  for(i=0; i<=anzx->emax; i++) 
  {
    if ( (facebuf[anz_fb][i]= (int *)malloc(6* sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failed \n\n") ;
    for(j=0; j<6; j++)  facebuf[anz_fb][i][j]=0;
  }
  /* store the setNr at elemnr 0 and face 1 */
  facebuf[anz_fb][0][1]=setNr;

  do{
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;

    args=crecord(rec_str, dat);
    el=atoi(dat[0]);
    vals[0]=atof(dat[2]);
    vals[1]=atof(dat[3]);
    /* delete blanks */
    i=0; for(j=0;j<strlen(dat[1]); j++) if(dat[1][j]!=' ') { buffer[i]=dat[1][j]; i++; }
    buffer[i]=(unsigned char)NULL;
    face=atoi(&buffer[1])-1;
    /* check if the node-number is 0, then an eset is defined */
    if (el==0)
    {
      eset=getSetNr(dat[0]);
      if(eset<0)
      {
        printf("ERROR: In *RADIATE set:%s not defined\n", dat[0]);
        return(-1);
      }
      for(j=0; j<set[eset].anz_e; j++)
      {
        foundLoads=1;
        if(face>=0) facebuf[anz_fb][el][face]=1;
        /* seta(setNr, "e", set[eset].elem[j]); */
        writeFaceValsFrd(set[eset].elem[j], face, 2, vals);
      }
    }
    else
    {
      foundLoads=1;
      if(face>=0) facebuf[anz_fb][el][face]=1;
      /* seta(setNr, "e", el); */
      writeFaceValsFrd(el, face, 2, vals);
    }

    /* write in ansys format */
    if(ansFlag)
    {
      printf ("ERROR radiation not supported\a\n");
    }
  }while(1); 
  if(foundLoads) { writeFaceValsFrd(-1, 0, 2, 0); anzx->l++;  ld4++; }
  return(1);
}


int getFilm(unsigned char *rec_str)
{
  int j,i; 
  int length, el, eset,args,face, foundLoads=0;
  static int ld4=1;
  int setNr;
  double vals[2];


  if(printFlag) printf("*FILM:\n");
  /* scan the properties and write to file */

  /* ini lcasex */
  sprintf( lcasex[anzx->l].name,"%s%d", FLM,ld4);
  printf ("lcase[%d].name= %s\n", anzx->l, lcasex[anzx->l].name);
  lcasex[anzx->l].ncomps=3;
  lcasex[anzx->l].irtype=1;

  strcpy(lcasex[anzx->l].analysis_name,"");
  strcpy(lcasex[anzx->l].dataset_name,"");
  strcpy(lcasex[anzx->l].dataset_text,"");
  lcasex[anzx->l].step_number=anzx->l;
  lcasex[anzx->l].analysis_type=1;

  if ( (lcasex[anzx->l].nmax = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].nmin = (int *)malloc( lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].max = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].min = (float *)malloc(lcasex[anzx->l].ncomps * sizeof(float))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].dat = (float **)malloc(lcasex[anzx->l].ncomps * sizeof(float *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].compName = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icname = (unsigned char **)malloc(lcasex[anzx->l].ncomps * sizeof(unsigned char *))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].ictype = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind1 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].icind2 = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if ( (lcasex[anzx->l].iexist = (int *)malloc(lcasex[anzx->l].ncomps * sizeof(int))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  for(i=0; i<lcasex[anzx->l].ncomps; i++)
  {
    if ( (lcasex[anzx->l].dat[i] = (float *)calloc( (anzx->nmax+1), sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );	               
    if ( (lcasex[anzx->l].compName[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    if ( (lcasex[anzx->l].icname[i] = (unsigned char *)calloc( MAX_LINE_LENGTH, sizeof(unsigned char))) == NULL )
      printf("\n\n ERROR: malloc failed\n\n" );
    lcasex[anzx->l].max[i]=-MAXVALUE;
    lcasex[anzx->l].min[i]=MAXVALUE;

    lcasex[anzx->l].ictype[i] = 1;
    lcasex[anzx->l].icind1[i] = i+1;
    lcasex[anzx->l].icind2[i] = 0;
    lcasex[anzx->l].iexist[i] = 0;
  }
  strcpy ( lcasex[anzx->l].compName[0], "temp");
  strcpy ( lcasex[anzx->l].compName[1], "alpha");
  strcpy ( lcasex[anzx->l].compName[2], "nodes");

  setNr=getSetNr(lcasex[anzx->l].name);
  if(setNr<0)
  {
    setNr=pre_seta( lcasex[anzx->l].name, "i", 0);
    if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s -e\n",lcasex[anzx->l].name );
  }

  /* store setNr, element and face for later use in cgx */
  anz_fb++;
  if ( (facebuf= (int ***)realloc(facebuf, (anz_fb+1)* sizeof(int **))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;

  if ( (facebuf[anz_fb]= (int **)malloc((anzx->emax+1)* sizeof(int *))) == NULL )
    printf("\n\n ERROR: malloc failed \n\n") ;
  for(i=0; i<=anzx->emax; i++) 
  {
    if ( (facebuf[anz_fb][i]= (int *)malloc(6* sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failed \n\n") ;
    for(j=0; j<6; j++)  facebuf[anz_fb][i][j]=0;
  }
  /* store the setNr at elemnr 0 and face 1 */
  facebuf[anz_fb][0][1]=setNr;

  do{
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;

    args=crecord(rec_str, dat);
    for(j=0; j<args; j++) printf(" %s",dat[j] );
    printf("\n"); 
    el=atoi(dat[0]);
    vals[0]=atof(dat[2]);
    vals[1]=atof(dat[3]);
    /* delete blanks */
    i=0; for(j=0;j<strlen(dat[1]); j++) if(dat[1][j]!=' ') { buffer[i]=dat[1][j]; i++; }
    buffer[i]=(unsigned char)NULL;
    face=atoi(&buffer[1])-1;
    /* check if the node-number is 0, then an eset is defined */
    if (el==0)
    {
      eset=getSetNr(dat[0]);
      if(eset<0)
      {
        printf("ERROR: In *FILM set:%s not defined\n", dat[0]);
        return(-1);
      }
      for(j=0; j<set[eset].anz_e; j++)
      {
        foundLoads=1;
        if(face>=0) facebuf[anz_fb][el][face]=1;
        /* seta(setNr, "e", set[eset].elem[j]); */
        writeFaceValsFrd(set[eset].elem[j], face, 2, vals);
      }
    }
    else
    {
      foundLoads=1;
      if(face>=0) facebuf[anz_fb][el][face]=1;
      /* seta(setNr, "e", el); */
      writeFaceValsFrd(el, face, 2, vals);
    }

    /* write in ansys format */
    if(ansFlag)
    {
      printf ("ERROR film not supported\a\n");
    }
  }while(1); 
  if(foundLoads) { writeFaceValsFrd(-1, 0, 2, 0); anzx->l++;  ld4++; }
  return(1);
}


int getElset(unsigned char *rec_str)
{
  int i,j,n, args,length, generate=0, setNr;
  unsigned char set[MAX_LINE_LENGTH];
  int el[20];

  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the type */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"ELSET=",6)==6) strcpy(set,&buffer[6]);
    if(compare(dat[i],"GENERATE",5)==5) generate=1;
  }
  if(printFlag) printf("*ELSET: ELSET:%s\n", set);
  if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s.elm -e\n",set);
  setNr=getSetNr(set);
  if(setNr<0) setNr=pre_seta( set, "i", 0);

  /* scan the element-names and write to file */
  do
  {
    length=abqrecord(rec_str);
    if(length==-1) return(-1);
    if(length==-2) break;
    if(generate)
    {
      el[0]=el[1]=el[2]=1;
      length=sscanf ( rec_str, "%d, %d, %d", &el[0],&el[1],&el[2]);
      /* write in list format */
      for(i=el[0]; i<=el[1]; i+=el[2])
      {
        seta( setNr, "e", i);
      }
    }
    else
    {
      args=crecord(rec_str, dat);
      /* extract the type */
      for(i=0; i<args; i++)
      {
        /* write in list format */
        el[0]=atoi(dat[i]);
        if(el[0]) seta( setNr, "e", el[0]);
      }
    }
  }while(1); 
  return(1);
}


int getNset(unsigned char *rec_str)
{
  int i,j,n, args,length, generate=0, setNr;
  unsigned char set[MAX_LINE_LENGTH];
  int nd[20];  


  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the type */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"NSET=",5)==5) strcpy(set,&buffer[5]);
    if(compare(dat[i],"GENERATE",5)==5) generate=1;
  }
  if(printFlag) printf("*NSET: NSET:%s\n", set);
  if ( handlefbl!= NULL ) fprintf(handlefbl, "read %s.nod -n\n",set);
  setNr=getSetNr(set);
  if(setNr<0) setNr=pre_seta( set, "i", 0);

  /* scan the node-names and write to file */
  do
  {
    length=abqrecord(rec_str);

    if(length==-1) return(-1);
    if(length==-2) break;
    if(generate)
    {
      nd[0]=nd[1]=nd[2]=1;
      length=sscanf ( rec_str, "%d, %d, %d", &nd[0],&nd[1],&nd[2]);
      /* write in list format */
      for(i=nd[0]; i<=nd[1]; i+=nd[2])
      {
        seta( setNr, "n", i);
      }
    }
    else
    {
      args=crecord(rec_str, dat);
      /* extract the type */
      for(i=0; i<args; i++)
      {
        /* write in list format */
        nd[0]=atoi(dat[i]);
        if(nd[0]) seta( setNr, "n", nd[0]);
      }
    }
  }while(1); 
  return(1);
}


int getShellSelection(unsigned char *rec_str)
{
  int i,j,n, args,length, setNr;
  unsigned char elset[MAX_LINE_LENGTH];
  unsigned char material[MAX_LINE_LENGTH];
  int ip=5;
  double d=0.;


  if((selected_elems=(int *)realloc((int *)selected_elems,(anzx->emax+1)*sizeof(int )))==NULL)
  {
    printf("\n\n ERROR: realloc failed in getSolidSelection\n") ;
    return(1);
  }
  for(i=0; i<=anzx->emax; i++) selected_elems[i]=0; 


  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the material-name */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"ELSET=",6)==6) strcpy(elset,&buffer[6]);
    if(compare(dat[i],"MATERIAL=",9)==9) strcpy(material,&buffer[9]);
    if((compare(buffer,"NODAL",5)==5)||
       (compare(buffer,"COMPOS",5)==5)||
       (compare(buffer,"ORIENT",5)==5))
    {
      printf("WARNING: option:%s not supported, keyword ignored\a\n", buffer);
      return(1);
    }
  }
  if(printFlag) printf("*SHELL SECTION: ELSET:%s MATERIAL:%s\n", elset, material);

  /* get the next line */
  length=abqrecord(rec_str);
  if(length==-1) return(-1);
  sscanf(rec_str, "%lf, %d",&d, &ip);
  elprop->nset++;
  elprop->d=d;
  if(printFlag) printf("set:%d d:%lf ip:%d\n",elprop->nset, elprop->d, ip);

  /* get or define a material-number */
  matnr=getMatNr(material);

  if(ansFlag) 
  {
    /* write ansys commands */
    setNr=getSetNr(elset);
    if ( setNr<0 )  { printf ("\nThe set \"%s\" could not be opened. The setdefinition was used before the set was created.\n\n",elset); exit(-1); }
    
    fprintf (handleans,"! ----------- ELSET: %s MAT:%s ---------------\n", elset, material);
    fprintf (handleans, "REAL, %d\n", elprop->nset);
    fprintf (handleans, "R, %d, %lf\n", elprop->nset, elprop->d);
    fprintf (handleans,"MAT, %d\n",matnr); 
    for(i=0; i<set[setNr].anz_e; i++)
    {
      selected_elems[set[setNr].elem[i]]=1;
    }
    writeElemAns(matnr, selected_elems, anzx, nodex, elemx);
  }
  return(1);
}


int getSolidSelection(unsigned char *rec_str)
{
  int i,j,n, args,length, setNr;
  unsigned char elset[MAX_LINE_LENGTH];
  unsigned char material[MAX_LINE_LENGTH];


  if((selected_elems=(int *)realloc((int *)selected_elems,(anzx->emax+1)*sizeof(int )))==NULL)
  {
    printf("\n\n ERROR: realloc failed in getSolidSelection\n") ;
    return(1);
  }
  for(i=0; i<=anzx->emax; i++) selected_elems[i]=0; 

  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the material-name */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"ELSET=",6)==6) strcpy(elset,&buffer[6]);
    if(compare(dat[i],"MATERIAL=",9)==9) strcpy(material,&buffer[9]);
    if((compare(buffer,"COMPOS",5)==5)||
       (compare(buffer,"ORIENT",5)==5))
    {
      printf("WARNING: option:%s not supported, keyword ignored\a\n", buffer);
      /* get the next line */
      /*
      length=abqrecord(rec_str);
      if(length==-1) return(-1);
      return(1);
      */
    }
  }
  if(printFlag) printf("*SOLID SECTION: ELSET:%s MATERIAL:%s\n", elset, material);

  /* get or define a material-number */
  matnr=getMatNr(material);

  if(ansFlag) 
  {
    /* write ansys commands */
    setNr=getSetNr(elset);
    if ( setNr<0 )  { printf ("\nThe set \"%s\" could not be opened. The setdefinition was used before the set was created.\n\n",elset); exit(-1); }

    fprintf(handleans,"! ----------- ELSET: %s MAT:%s ---------------\n", elset, material);
    fprintf(handleans,"MAT, %d\n",matnr); 
    for(i=0; i<set[setNr].anz_e; i++)
    {
      selected_elems[set[setNr].elem[i]]=1;
    }
    writeElemAns(matnr, selected_elems, anzx, nodex, elemx);
  }

  /* get the next line */
  length=abqrecord(rec_str);
  if(length==-1) return(-1);
  return(1);
}


int getInitialConditions(unsigned char *rec_str)
{
  int i,j,n, args, length, setNr;


  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the name */
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
    if(compare(dat[i],"TYPE=",5)==5)
    {
      strcpy(dat[i],&buffer[5]);
      /* delete blanks */
      n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
      buffer[n]=(unsigned char)NULL;
      for(j=0;j<=strlen(buffer); j++) dat[i][j]=toupper(buffer[j]);
      if(compare(dat[i],"TEMPERATURE",6)==6)
      {
        if(printFlag) printf("*INITIAL CONDITIONS: TYPE:%s \n", buffer);


        if(ansFlag) 
        {
          /* get the next line */
          length=abqrecord(rec_str);
          if(length==-1) return(-1);
          if(length==-2) return(1);

          /* get the arguments of the string */
          args=crecord(rec_str, dat);

          /* extract the set-name */
          /* delete blanks */
          n=0; for(j=0;j<strlen(dat[0]); j++) if(dat[0][j]!=' ') { buffer[n]=dat[0][j]; n++; }        buffer[n]=(unsigned char)NULL;
  
          if((selected_elems=(int *)realloc((int *)selected_elems,(anzx->nmax+1)*sizeof(int )))==NULL)
          {
            printf("\n\n ERROR: realloc failed in getSolidSelection\n") ;
            return(1);
          }
          for(j=0; j<anzx->nmax; j++) selected_elems[j]=0;
  
          /* check if a setname or a node-number is provided */
          if(atoi(buffer)>0)
          { 
            length=abqrecord(rec_str);
  
            if(length==-1) return(-1);
            if(length==-2) break;
            args=crecord(rec_str, dat);
            selected_elems[atoi(dat[0])]=1;
          }
          else
          {
            /* open the file which contains the affected nodes */
            setNr=getSetNr(buffer);
            if ( setNr<0 )  { printf ("\nThe set \"%s\" could not be opened. The setdefinition was used before the set was created.\n\n", buffer); exit(-1); }
            for(i=0; i<set[setNr].anz_n; i++)
            { 
              selected_elems[set[setNr].node[i]]=1;
            }
          }
        
          /* write ansys */
          for(j=0; j<anzx->n; j++) if(selected_elems[nodex[j].nr]==0)
	  {
            printf("WARNING: TREF not unique, no TREF written for set:%s. n:%d ntot:%d\n", buffer, j,anzx->n);
            return(1);
          }
          fprintf(handleans,"! ----------- INITIAL CONDITIONS, SET:%s ---------------\n", buffer);
          fprintf(handleans, "TREF, %s\n",dat[1]);
        }
        else getTemperatures(rec_str);
        return(1);
      }
      else 
      {
        printf("WARNING: option:%s not supported, keyword ignored\a\n", buffer);
        /* get the next line */
        length=abqrecord(rec_str);
        if(length==-1) return(-1);
        return(1);
      }
    }
  }
  /* get the next line */
  length=abqrecord(rec_str);
  if(length==-1) return(-1);
  return(1);
}


int getFilePointer( int filesopen, FILE **openfile, unsigned char *rec_str)
{
  int i,j,n,args,setFlag=0, length;
  unsigned char set[MAX_LINE_LENGTH];

  /* get the arguments of the string */
  args=crecord(rec_str, dat);
  
  /* extract the filename */
  sprintf(set, "NONE");
  for(i=0; i<args; i++)
  {
    /* delete blanks */
    n=0; for(j=0;j<strlen(dat[i]); j++) if(dat[i][j]!=' ') { buffer[n]=dat[i][j]; n++; }
    buffer[n]=(unsigned char)NULL;
    if(compare(buffer,"INPUT=",6)==6) { setFlag=1; strcpy(set,&buffer[6]); } 
    if(compare(buffer,"input=",6)==6) { setFlag=1; strcpy(set,&buffer[6]); } 
    if(compare(buffer,"Input=",6)==6) { setFlag=1; strcpy(set,&buffer[6]); } 
  }
  if(!setFlag) 
  { 
    printf(" WARNING: No input file provided %s\n", rec_str);
    length=abqrecord(rec_str);
    if(length==-1) return(filesopen);
    return(filesopen);
  }
  if(printFlag) printf("*INCLUDE: FILE=%s\n", set);

  /* open input file */
  handlein = fopen(set, "r");
  if ( handlein== NULL ) { printf ("\nThe input file \"%s\" could not be opened.\n\n",set); return(filesopen); }
  else 
  {
    openfile[filesopen]=handlein; filesopen++;
  }

  return(filesopen);
}


int readccx(unsigned char *datin, Summen *apre, Nodes **nptr, Elements **eptr, Datasets **lptr, int ****fptr )
{
  int i, args, length;
  unsigned char rec_str[MAX_LINE_LENGTH];

  annotation();

  anzx=apre;
  nodex=*nptr;
  elemx=*eptr;
  lcasex=*lptr;
  facebuf=*fptr;

#if ANSYS
  ansFlag=1;
  /* open the ansys file */
  handleans = fopen("ccx.ans", "w+");
  elprop->nset=0;
  fprintf(handleans, "/PREP7\n");
  fprintf(handleans, "SHPP, OFF\n");
#endif
#if TEST
  printFlag=1; 
#endif

  handlein = fopen(datin, "r+");
  if ( handlein== NULL )  { printf ("\nThe input file \"%s\" could not be opened.\n\n",datin); return(-1);}
  else { filesopen=1; openfile[0]=handlein; }


  /* open the fbl file */
#if WRITE_FBL
  handlefbl = fopen("ccx.fbl", "w+");
  if ( handlefbl== NULL )
  {
    printf ("\nERROR: The input file \"ccx.fbl\" could not be opened.\n");
    return(-1);
  }
#else
  handlefbl = NULL;
#endif
 
  /* allocate space */
  if ( (lcasex = (Datasets *)realloc((Datasets *)lcasex, MAX_LOADCASES * sizeof(Datasets))) == NULL )
    printf("\n\n ERROR: realloc failed\n\n") ;
  if ( (nodex = (Nodes *)realloc((Nodes *)nodex, INI_FIELD_SIZE * sizeof(Nodes))) == NULL )
    printf("\n\n ERROR: realloc failed\n\n") ;
  for(i=0; i<INI_FIELD_SIZE; i++) nodex[i].indx=-1;
  if ( (elemx = (Elements *)realloc((Elements *)elemx, INI_FIELD_SIZE * sizeof(Elements))) == NULL )
    printf("\n\n ERROR: realloc failed\n\n") ;

  if((dat = (unsigned char **)realloc((unsigned char **)dat, (80)*sizeof(unsigned char*)))==NULL)
  printf("\n\n ERROR: realloc failed for **dat\a\n" );
  for (i=0; i<80; i++)
  {
    dat[i]=NULL;
    if((dat[i] = (unsigned char *)realloc((unsigned char *)dat[i], (MAX_LINE_LENGTH)*sizeof(unsigned char)))==NULL)
      printf("\n\n ERROR: realloc failed for *dat\a\n" );
  }

  /* scan through the input-file and look for keywords */
  do
  {
    /* all subroutines return eventually the next keyword which could be any. Therefore check again */
    length=abqrecord(rec_str);
    if(length==-1) break;
    checknextstr:;

    /* get the arguments of the string */
    args=crecord(rec_str, dat);

    /* get the arguments of the string */
    i=0; while(dat[0][i]!=(unsigned char)NULL) { dat[0][i]=toupper(dat[0][i]); i++; }

    /* get the nodes */
    if (compare(dat[0], "*NODE",5)==5) { length=getNodes(rec_str); goto checknextstr; }

    /* get the elements */
    else if (compare(dat[0], "*ELEMENT",8)==8) { length=getElements(rec_str); goto checknextstr; }

    /* get the cload */
    else if (compare(dat[0], "*CLOAD",5)==5) { length=getCload(rec_str); goto checknextstr; }

    /* get the cflux */
    else if (compare(dat[0], "*CFLUX",5)==5) { length=getCflux(rec_str); goto checknextstr; }

    /* get the dload */
    else if (compare(dat[0], "*DLOAD",5)==5) { length=getDload(rec_str); goto checknextstr; }

    /* get the radiation */
    else if (compare(dat[0], "*RADIATION",5)==5) { length=getRadiation(rec_str); goto checknextstr; }

    /* get the film */
    else if (compare(dat[0], "*FILM",5)==5) { length=getFilm(rec_str); goto checknextstr; }

    /* get the dflux */
    else if (compare(dat[0], "*DFLUX",5)==5) { length=getDflux(rec_str); goto checknextstr; }

    /* get the surfaces */
    else if (compare(dat[0], "*SURFACE",7)==7) { length=getSurfaces(rec_str); goto checknextstr; }

    /* get the initial conditions */
    else if (compare(dat[0], "*INITIAL CONDITIONS",19)==19) { length=getInitialConditions(rec_str); goto checknextstr; }

    /* get the boundaries */
    else if (compare(dat[0], "*BOUNDARY",9)==9) { length=getBoundaries(rec_str); goto checknextstr; }

    /* get the equations */
    else if (compare(dat[0], "*EQUATION",9)==9) { length=getEquations(rec_str); goto checknextstr; }

    /* get the mpc */
    else if (compare(dat[0], "*MPC",4)==4) { length=getMpc(rec_str); goto checknextstr; }

    /* open the material-file */
    else if (compare(dat[0], "*MATERIAL",9)==9) { length=getMaterial(rec_str); goto checknextstr; }

    /* get the material-definitions */
    else if (compare(dat[0], "*ELASTIC",8)==8) { length=getElastic(rec_str); goto checknextstr; }
    else if (compare(dat[0], "*EXPANSION",8)==8) { length=getExpansion(rec_str); goto checknextstr; }
    else if (compare(dat[0], "*DENSITY",8)==8) { length=getDensity(rec_str); goto checknextstr; }

    /* get the set-definitions */
    else if (compare(dat[0], "*ELSET",6)==6) { length=getElset(rec_str); goto checknextstr; }
    else if (compare(dat[0], "*NSET",5)==5) { length=getNset(rec_str); goto checknextstr; }

    /* asign material to sets */
    else if (compare(dat[0], "*SHELL SECTION",14)==14) { length=getShellSelection(rec_str); goto checknextstr; }
    else if (compare(dat[0], "*SOLID SECTION",14)==14) { length=getSolidSelection(rec_str); goto checknextstr; }

    /* asign TEMPERATURE to sets */
    else if (compare(dat[0], "*TEMPERATURE",12)==12) { length=getTemperatures(rec_str); goto checknextstr; }

    else 
    {
      if ((dat[0][0]=='*')&&(dat[0][1]!='*')) printf("WARNING: %s not known. Data ignored.\n", rec_str);
    }
  }while(length!=-1);

  /* check if nodes or elements were found */
  if(anzx->nmax==-MAXVALUE)
  {
    /* no nodes allocated */
    anzx->nmax=anzx->nmin=0;
  }
  if(anzx->emax==-MAXVALUE)
  {
    /* no elements allocated */
    anzx->emax=anzx->emin=0;
  }

  elemChecker( anzx, nodex, elemx);

  /* store the nr of facebuffers at elemnr 0 and face 0 of the first facebuf */
  if(facebuf!=NULL)
  {
    facebuf[0][0][0]=anz_fb;
    *fptr= facebuf;
  }
  *nptr =  nodex; *eptr = elemx; *lptr = lcasex; 
  if(ansFlag) fclose(handleans);
#if WRITE_FBL
  fclose(handlefbl);
#endif
  return(1);
}



