/* --------------------------------------------------------------------  */
/*                          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.         */
/* --------------------------------------------------------------------  */

/*
TODO:
  sendSliders() and sendSurfNormalen(): still working on node basis instead of using the facees
*/



#include <cgx.h>

#define     TEST            0     /* debugging */

#define     DOFX        1
#define     DOFY        2
#define     DOFZ        3
#define     DOFT        11
#define     MIN_VECTOR  0.0001

extern int       neqn;                 /* offset der equations fuer ansys, bzw. MPC ID fuer Nast. */

extern int   basCol[3];                     /* color indexes due to basic colormap */
extern int   width_ini, height_ini; /* Grafig-Fensterbreite/hoehe */
extern int   width_menu, height_menu;
extern int   w0, w1, w_index, w_rgb, activWindow;
extern int   width_w0, height_w0;
extern int   width_w1, height_w1;
extern int   MouseMode;                                   /* status maustasten */
extern double dtx, dty, drx, dry, drz, ds;                 /* Verschiebungen */
extern GLdouble R[4][4];                                   /* Rotationsmatrix */
extern unsigned char  surfFlag;                /* zeichne nur Oberflaechenelemente (1), sonst (0)*/
extern unsigned char  modelEdgeFlag;                /* zeichne mit Modell-Ecken (1), sonst (0)*/
extern unsigned char  cullFlag;                     /* (1) enable Culling */
extern unsigned char  frameFlag;               /* mit (1) oder ohne Rahmen um das Grafigfenster */
extern double dx ,dy;                                      /* Mauskoordinaten */
extern int   steps;                   /* Schrittweite der Farbscala, und NURB aufloesung */

extern int     num_olc;          /* nr-of-original-loadcases (lc from input-file) */

extern Scale     scale[1];
extern double     *value_n;
extern Summen    anz[1];
extern Edges     *edge;
extern Nodes     *node;
extern Nodes     *n_anim;
extern Elements  *e_enqire;
extern Datasets *lcase;
extern Faces     *face;

extern Alias     *alias;
extern Sets      *set;
extern Points    *point;
extern Lines     *line;
extern Lcmb      *lcmb;
extern Gsur      *surf;
extern Gbod      *body;
extern Nurbs     *nurbs;
extern SumGeo    anzGeo[1];

extern int       offset;

/* for CFD-meshing */
extern int       anz_cfdSurfs;


/* Set Management */
extern unsigned char  printFlag;                     /* printf 1:on 0:off */


/* search all surfaces who are fully described by the selected nodes and add them to the set */
int compSurfElems( int setNr, Elements **ptr)
{
  int      i, j, k, n=0, elem2nr=0, anz_n;
  int      *surnod=NULL;
  Elements *elem2;

  elem2=*ptr;

  /* mark the surface nodes for easy element identification */
  if( (surnod=(int *)realloc((int *)surnod, (anz->nmax+1)*sizeof(int) ) )==NULL) 
  { printf(" ERROR: malloc failure\n"); return(0); }
  for (i=0; i<=anz->nmax; i++) surnod[i]=0;
  for (i=0; i<set[setNr].anz_n; i++) surnod[set[setNr].node[i]]=1;


  for(i=0; i<anz->f; i++)
  {
    anz_n=n=0;
    if (face[i].type==7) anz_n=3;
    if (face[i].type==8) anz_n=6;
    if (face[i].type==9) anz_n=4;
    if (face[i].type==10) anz_n=8;

    if(anz_n)
    {
      for (k=0; k<anz_n; k++) if (surnod[face[i].nod[k]]) n++;
      if (n==anz_n)
      {
        if ( (elem2 = (Elements *)realloc((Elements *)elem2, (elem2nr+1) * sizeof(Elements))) == NULL )
        { printf("\n\nERROR: malloc failed in compSurfElems\n\n"); return(0); }
        seta( setNr, "e", face[i].elem_nr);
        elem2[elem2nr].nr      = elem2nr+1+anz->emax;
        elem2[elem2nr].type    = face[i].type;
        elem2[elem2nr].group   = face[i].nr;

        for (j=0; j<anz_n; j++)
        {
          elem2[elem2nr].nod[j]=face[i].nod[j];
        }
        elem2nr++;
      }
      else if(n>anz_n)
      {
        printf("ERROR: to much corners found on elem:%d\n", face[i].elem_nr);
      }
    }
  }


  *ptr=elem2;
  free(surnod);
  return( elem2nr );
}


typedef struct {
  int nod[4];
  double ncol[4][3];
  int elem_nr;
  int group;
  int mat;
} CQuad4;

typedef struct {
  double side[48][3];                 /* side[Nr.][x|y|z] */
} Normalen;


void calcNormale_quad4(int i, int n1, int n2, int n3, int n4, int f, Nodes *node, Normalen *norm)
{
  static double v1[3], v2[3], v3[3];

      /* Vereinfachte Normale auf Flaeche des i.Elements*/
      v_result( &node[n1].nx, &node[n3].nx, v1);
      v_result( &node[n2].nx, &node[n4].nx, v2);
      v_prod( v1, v2, v3 );
      v_norm( v3, norm[i].side[f] );

}

void getCQuad4Normalen( CQuad4 *cquad4, Nodes *node, int *num_etype, Normalen *norm )
{
  int i;

  for (i=0; i<*num_etype; i++ )
  {
    calcNormale_quad4(i,cquad4[i].nod[0], cquad4[i].nod[1], cquad4[i].nod[2],
                  cquad4[i].nod[3], 0, node, norm);
  }
}


void sendSurfNormalen( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire,
               Datasets *lcase , Scale *scale )
{
  int  setNr, length, i, j, n=0, ipuf, elem2nr=0, comp, faces;
  unsigned char prognam[MAX_LINE_LENGTH];
  static int   *isort=NULL;
  Summen    anz2[1];
  static Nodes     *node2=NULL;
  static Elements  *elem2=NULL;
  static Datasets *lcase2=NULL;
  static CQuad4    *face4=NULL;
  static Normalen  *norm2=NULL;
  extern int compareInt();


  strcpy ( prognam, setname);
  length= strlen ( setname );
  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  /* search all element-faces who are fully described by the selected nodes and add them to the set */
  printf("Please wait\n");
  elem2nr=compSurfElems( setNr, &elem2);

  /* calculate the normals on the new faces */
  faces=0;
  if ( (face4 = (CQuad4 *)malloc( (elem2nr+1) * sizeof(CQuad4))) == NULL )
    printf("\n\nERROR: malloc failed\n\n") ;
  for (i=0; i<elem2nr; i++ )
  {
    if(elem2[i].type==9)
    {
      for (n=0; n<4; n++)
      {
       face4[faces].nod[n]=elem2[i].nod[n];
      }
      face4[faces].elem_nr = elem2[i].nr;
      face4[faces].group   = elem2[i].group;
      face4[faces].mat     = elem2[i].mat;
      faces++;
    }
  }

  if(faces>0)
  {
    anz2->l = 1;
    if ( (norm2 = (Normalen *)malloc( (faces+1)*4 * sizeof(Normalen))) == NULL )
      printf("\n\nERROR: malloc failed in sendSurfNormalen\n\n") ;
    getCQuad4Normalen( face4, node, &faces, norm2);

    /* send the surfs and normals in the frd format */
    if ( (lcase2 = (Datasets *)malloc( 2 * sizeof(Datasets))) == NULL )
      printf("\n\n ERROR: malloc failed lcase2\n\n") ;
    
    sprintf( lcase2[0].name,"NORM");
    comp=0;
    lcase2[0].ncomps=3;
    lcase2[0].irtype=3;
    lcase2[0].npheader=0;    
    if ( (lcase2[0].nmax = (int *)malloc(lcase2[0].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].nmin = (int *)malloc(lcase2[0].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].max = (float *)malloc(lcase2[0].ncomps * sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].min = (float *)malloc(lcase2[0].ncomps * sizeof(float))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].compName = (unsigned char **)malloc(lcase2[0].ncomps * sizeof(unsigned char *))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].icname = (unsigned char **)malloc(lcase2[0].ncomps * sizeof(unsigned char *))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].ictype = (int *)malloc(lcase2[0].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].icind1 = (int *)malloc(lcase2[0].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].icind2 = (int *)malloc(lcase2[0].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    if ( (lcase2[0].iexist = (int *)malloc(lcase2[0].ncomps * sizeof(int))) == NULL )
      printf("\n\n ERROR: malloc failure\n\n" );
    
    for(i=comp; i<lcase2[0].ncomps; i++)
    {
      if ( (lcase2[0].compName[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
        printf("\n\n ERROR: malloc failed\n\n" );
      if ( (lcase2[0].icname[i] = (unsigned char *)malloc( MAX_LINE_LENGTH * sizeof(unsigned char))) == NULL )
        printf("\n\n ERROR: malloc failed\n\n" );
      lcase2[0].max[i]=-MAXVALUE;
      lcase2[0].min[i]=MAXVALUE;
    
      lcase2[0].ictype[i] = 2;
      lcase2[0].icind1[i] = i+1;
      lcase2[0].icind2[i] = 0;
      lcase2[0].iexist[i] = 0;
    }
    
    strcpy ( lcase2[0].compName[0], "x ");
    strcpy ( lcase2[0].compName[1], "y ");
    strcpy ( lcase2[0].compName[2], "z ");
    
    if ( (lcase2[0].edat = (float ***)malloc(  (lcase2[0].ncomps)*sizeof(float **))) == NULL )
      printf("\n\n ERROR: malloc failed esxx\n\n" );
    if ( (lcase2[0].edat[0] = (float **)malloc(  (elem2nr+anz->emax+1)*sizeof(float *))) == NULL )
      printf("\n\n ERROR: malloc failed esxx\n\n" );
    if ( (lcase2[0].edat[1] = (float **)malloc(  (elem2nr+anz->emax+1)*sizeof(float *))) == NULL )
      printf("\n\n ERROR: malloc failed esyy\n\n" );
    if ( (lcase2[0].edat[2] = (float **)malloc(  (elem2nr+anz->emax+1)*sizeof(float *))) == NULL )
      printf("\n\n ERROR: malloc failed eszz\n\n" );
    
    for (i=0; i<elem2nr; i++ )
    {
      for (j=0; j<lcase2[0].ncomps; j++)
      {
        if ( (lcase2[0].edat[j][elem2[i].nr] = (float *)malloc( (4) * sizeof(float))) == NULL )
            printf("\n\n ERROR: malloc failed esxx[%d]\n\n", i );
      }
    }

    for (i=0; i<faces; i++ )
    {
      for (j=0; j<4; j++)
      {
        lcase2[0].edat[0][face4[i].elem_nr][j]=norm2[i].side[0][0];
        lcase2[0].edat[1][face4[i].elem_nr][j]=norm2[i].side[0][1];
        lcase2[0].edat[2][face4[i].elem_nr][j]=norm2[i].side[0][2];
      }
    }
  }
  else anz2->l = 0;

  anz2->emax=MINVALUE;
  anz2->emin=MAXVALUE;
  for (i=0; i<elem2nr; i++)
  {
    if (anz2->emax<elem2[i].nr ) anz2->emax = elem2[i].nr;
    if (anz2->emin>elem2[i].nr ) anz2->emin = elem2[i].nr;
    ipuf=0;
    if (elem2[i].type == 1) ipuf = 8;  /* CHEXA8 */
    else if (elem2[i].type == 3) ipuf = 4;  /* CTET4 */
    else if (elem2[i].type == 4) ipuf = 20;  /* CHEXA20 */
    else if (elem2[i].type == 6) ipuf = 10;  /* CTET10 */
    else if (elem2[i].type == 7) ipuf = 3;  /* CTRI3  */
    else if (elem2[i].type == 8) ipuf = 6;  /* CTRI6  */
    else if (elem2[i].type == 9) ipuf = 4;  /* CQUAD4 */
    else if (elem2[i].type == 10) ipuf = 8;  /* CQUAD8 */
    else if (elem2[i].type == 11) ipuf = 2;  /* CBEAM */
    else if (elem2[i].type == 12) ipuf = 3;  /* CBEAM3 */
    else
    {
      if(printFlag) printf (" elem(%d) not a known type (%d)\n", elem2[i].nr, elem2[i].type);
    }
    for (j=0; j<ipuf; j++)
    {
      seta( setNr, "n", elem2[i].nod[j]);
    }
  }
  /* nodes must follow the elements (seta n above!) */
  /* sort the nodenumbers */
  if ( (node2 = (Nodes *)malloc( (anz->nmax+1) * sizeof(Nodes))) == NULL )
    printf("\n\nERROR: malloc failed\n\n") ;

  anz2->nmax = anz2->nmin = 0;

  if ( (isort = (int *)realloc( isort, (set[setNr].anz_n+1) * sizeof(int))) == NULL )
    printf("ERROR: realloc failed: isort\n\n" ); 
  for( i=0; i<set[setNr].anz_n; i++) isort[i]=set[setNr].node[i];
  qsort( isort, set[setNr].anz_n, sizeof(int), (void *)compareInt );

  for (i=0; i<set[setNr].anz_n; i++)
  {
    node2[i].nr = isort[i];
    node2[isort[i]].nx = node[isort[i]].nx;
    node2[isort[i]].ny = node[isort[i]].ny;
    node2[isort[i]].nz = node[isort[i]].nz;
    if (anz2->nmax<node2[i].nr) anz2->nmax = node2[i].nr;
    if (anz2->nmin>node2[i].nr) anz2->nmin = node2[i].nr;
  }
  strcpy( anz2->model, setname);
  anz2->n = set[setNr].anz_n;
  anz2->e = elem2nr;
  anz2->u = 0;
  anz2->p = 0;

  descalNodes ( set[setNr].anz_n, node2, scale);
  write2frd( "surface.frd", anz2, node2, elem2, lcase2);
  scalNodes ( set[setNr].anz_n, node2, scale );

  /* free temporary memory */
  if(faces)
  {
    for (i=0; i<lcase2[0].ncomps; i++)
    {
      for(j=0; j<elem2nr; j++)
      {
          free(lcase2[0].edat[i][elem2[j].nr]);
          lcase2[0].edat[i][elem2[j].nr]=NULL;
      }
      free(lcase2[0].edat[i]); 
      lcase2[0].edat[i]=NULL;
    }
    free(lcase2);
    lcase2=NULL;
  }
  free(node2);
  free(face4);
  free(elem2);
  free(norm2);
  node2=NULL;
  face4=NULL;
  elem2=NULL;
  norm2=NULL;

  printf (" ready\n");
}


void sendSliders( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, unsigned char *spcType )
{
  int   setNr;
  int  length, i, n, elem2nr=0, anz_n;
  unsigned char prognam[MAX_LINE_LENGTH];

  double *n_normx, *n_normy, *n_normz, xx,yy,zz;
  int   *sum_n;

  double p1[3], p2[3], p3[3], N[3];

  static Elements  *elem2=NULL;
  static CQuad4    *face4=NULL;
  static Normalen  *norm2=NULL;
  static int       *surnod=NULL;

  FILE *handle;


  strcpy ( prognam, setname);
  length= strlen ( setname );
  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  /* allocate memory for the normals of the sliding planes */
  if ( (n_normx = (double *)malloc( (anz->nmax+1) * sizeof(double))) == NULL )
    printf("\n\nERROR: malloc failed in sendSliders\n\n") ;
  if ( (n_normy = (double *)malloc( (anz->nmax+1) * sizeof(double))) == NULL )
    printf("\n\nERROR: malloc failed in sendSliders\n\n") ;
  if ( (n_normz = (double *)malloc( (anz->nmax+1) * sizeof(double))) == NULL )
    printf("\n\nERROR: malloc failed in sendSliders\n\n") ;
  if ( (sum_n = (int *)malloc( (anz->nmax+1) * sizeof(int))) == NULL )
    printf("\n\nERROR: malloc failed in sendSliders\n\n") ;
  /* ini all = 0 */
  for (i=0; i<anz->n; i++ )
  {
    sum_n[node[i].nr]=0;
    n_normx[node[i].nr]=n_normy[node[i].nr]=n_normz[node[i].nr]=0.;
  }

  if( compare(spcType, "s", 1) == 1)
  {
    /* nodes slide on the element surfaces */
  
    /* search all element-faces who are fully described by the selected nodes and add them to the set */
    printf("Please wait\n");
    elem2nr=compSurfElems( setNr, &elem2);
  
    /* calculate the normals on the new faces */
    if ( (face4 = (CQuad4 *)malloc( (elem2nr+1) * sizeof(CQuad4))) == NULL )
      printf("\n\nERROR: malloc failed\n\n") ;
    for (i=0; i<elem2nr; i++ )
    {
      if((elem2[i].type==7)||(elem2[i].type==8))
      {
        for (n=0; n<3; n++)
        {
          face4[i].nod[n]=elem2[i].nod[n];
        }
        face4[i].nod[3]=elem2[i].nod[2];
        face4[i].elem_nr = elem2[i].nr;
        face4[i].group   = elem2[i].group;
        face4[i].mat     = elem2[i].mat;
      }
      else if((elem2[i].type==9)||(elem2[i].type==10))
      {
        for (n=0; n<4; n++)
        {
          face4[i].nod[n]=elem2[i].nod[n];
        }
        face4[i].elem_nr = elem2[i].nr;
        face4[i].group   = elem2[i].group;
        face4[i].mat     = elem2[i].mat;
      }
      else
      {
	  printf("Warning: No slider could be created for elem:%d, type:%d is not supported in this function\n", elem2[i].mat, elem2[i].type);
      }
    }
  
    if ( (norm2 = (Normalen *)malloc( (elem2nr+1)*4 * sizeof(Normalen))) == NULL )
      printf("\n\nERROR: malloc failed in sendSurfNormalen\n\n") ;
    getCQuad4Normalen( face4, node, &elem2nr, norm2);


    /* calculate the average normal on every node */
    /* - add the normals per node */
    for (i=0; i<elem2nr; i++ )
    {
      if(elem2[i].type==10) anz_n=8;
      else if(elem2[i].type==9) anz_n=4;
      else if(elem2[i].type==8) anz_n=6;
      else anz_n=0;
        for (n=0; n<anz_n; n++)
        {
          n_normx[elem2[i].nod[n]]+=norm2[i].side[0][0];
          n_normy[elem2[i].nod[n]]+=norm2[i].side[0][1];
          n_normz[elem2[i].nod[n]]+=norm2[i].side[0][2];
          sum_n[elem2[i].nod[n]]++;
        }
    }
    /* - calc the average normal */
    for (i=0; i<anz->n; i++ )
    {
      if( sum_n[node[i].nr] >0 )
      {
        n_normx[node[i].nr]/=sum_n[node[i].nr];
        n_normy[node[i].nr]/=sum_n[node[i].nr];
        n_normz[node[i].nr]/=sum_n[node[i].nr];
      }
    }
    free(surnod);
    surnod=NULL;
  }  
  else if( compare(spcType, "rx", 2) == 2)
  {
    /* calculate the average normal on every node */
    p2[0]=1.;
    p2[1]=0.;
    p2[2]=0.;
    for (i=0; i<set[setNr].anz_n; i++)
    {
      n= set[setNr].node[i];
      sum_n[n]=1;
      p1[0]=node[n].nx* scale->w+scale->x;
      p1[1]=node[n].ny* scale->w+scale->y;
      p1[2]=node[n].nz* scale->w+scale->z;
      v_prod( p1, p2, p3);
      v_norm( p3, N ); 
      n_normx[n]=N[0];
      n_normy[n]=N[1];
      n_normz[n]=N[2];
    }
  }  
  else if( compare(spcType, "ry", 2) == 2)
  {
    /* calculate the average normal on every node */
    p2[0]=0.;
    p2[1]=1.;
    p2[2]=0.;
    for (i=0; i<set[setNr].anz_n; i++)
    {
      n= set[setNr].node[i];
      sum_n[n]=1;
      p1[0]=node[n].nx* scale->w+scale->x;
      p1[1]=node[n].ny* scale->w+scale->y;
      p1[2]=node[n].nz* scale->w+scale->z;
      v_prod( p1, p2, p3);
      v_norm( p3, N ); 
      n_normx[n]=N[0];
      n_normy[n]=N[1];
      n_normz[n]=N[2];
    }
  }  
  else if( compare(spcType, "rz", 2) == 2)
  {
    /* calculate the average normal on every node */
    p2[0]=0.;
    p2[1]=0.;
    p2[2]=1.;
    for (i=0; i<set[setNr].anz_n; i++)
    {
      n= set[setNr].node[i];
      sum_n[n]=1;
      p1[0]=node[n].nx* scale->w+scale->x;
      p1[1]=node[n].ny* scale->w+scale->y;
      p1[2]=node[n].nz* scale->w+scale->z;
      v_prod( p1, p2, p3);
      v_norm( p3, N ); 
      n_normx[n]=N[0];
      n_normy[n]=N[1];
      n_normz[n]=N[2];
    }
  }  
  else if( compare(spcType, "tx", 2) == 2)
  {
    /* calculate the average normal on every node */
    for (i=0; i<set[setNr].anz_n; i++)
    {
      n= set[setNr].node[i];
      sum_n[n]=1;
      n_normx[n]=0.;
      n_normy[n]=node[n].ny* scale->w+scale->y;
      n_normz[n]=node[n].nz* scale->w+scale->z;
    }
  }  
  else if( compare(spcType, "ty", 2) == 2)
  {
    /* calculate the average normal on every node */
    for (i=0; i<set[setNr].anz_n; i++)
    {
      n= set[setNr].node[i];
      sum_n[n]=1;
      n_normx[n]=node[n].nx* scale->w+scale->x;
      n_normy[n]=0.;
      n_normz[n]=node[n].nz* scale->w+scale->z;
    }
  }  
  else if( compare(spcType, "tz", 2) == 2)
  {
    /* calculate the average normal on every node */
    for (i=0; i<set[setNr].anz_n; i++)
    {
      n= set[setNr].node[i];
      sum_n[n]=1;
      n_normx[n]=node[n].nx* scale->w+scale->x;
      n_normy[n]=node[n].ny* scale->w+scale->y;
      n_normz[n]=0.;
    }
  }  
  else
  {
    errMsg ("\nERROR: format:%s not known\n", spcType);
    return;
  }  

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".equ");

  /* write the sliders in nastran-format */
  /* write the sliders in abaqus-format */
  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    {
      printf ("\nThe input file %s could not be opened.\n\n", prognam); 
      return;
    }
    fprintf(handle, "** Sliders based on %s\n", setname);
    for (i=0; i<anz->n; i++ )
    {
      if( sum_n[node[i].nr] >0 )
      {
        printf("node %d Norm:%lf %lf %lf Koord:%lf %lf %lf\n"
               , node[i].nr
	       , n_normx[node[i].nr], n_normy[node[i].nr], n_normz[node[i].nr]
               ,node[node[i].nr].nx,node[node[i].nr].ny,node[node[i].nr].nz ); 

        xx=n_normx[node[i].nr]*n_normx[node[i].nr];
        yy=n_normy[node[i].nr]*n_normy[node[i].nr];
        zz=n_normz[node[i].nr]*n_normz[node[i].nr];
        if((xx>MIN_VECTOR)&&(yy>MIN_VECTOR)
        &&(zz>MIN_VECTOR))
        {
          fprintf(handle, "*EQUATION\n");
          fprintf(handle, "%d\n", 3);
          fprintf(handle, "%d,%d,%lf, %d,%d,%lf, %d,%d,%lf \n"
          , node[i].nr, DOFX, n_normx[node[i].nr]
          , node[i].nr, DOFY, n_normy[node[i].nr]
	  , node[i].nr, DOFZ, n_normz[node[i].nr] );
        }
        else if((xx<=MIN_VECTOR)
        &&(yy>MIN_VECTOR)&&(zz>MIN_VECTOR))
        {
          fprintf(handle, "*EQUATION\n");
          fprintf(handle, "%d\n", 2);
          fprintf(handle, "%d,%d,%lf, %d,%d,%lf \n"
          , node[i].nr, DOFY, n_normy[node[i].nr]
	  , node[i].nr, DOFZ, n_normz[node[i].nr] );
        }
        else if((yy<=MIN_VECTOR)
        &&(xx>MIN_VECTOR)&&(zz>MIN_VECTOR))
        {
          fprintf(handle, "*EQUATION\n");
          fprintf(handle, "%d\n", 2);
          fprintf(handle, "%d,%d,%lf, %d,%d,%lf \n"
          , node[i].nr, DOFX, n_normx[node[i].nr]
	  , node[i].nr, DOFZ, n_normz[node[i].nr] );
        }
        else if((zz<=MIN_VECTOR)
        &&(xx>MIN_VECTOR)&&(yy>MIN_VECTOR))
        {
          fprintf(handle, "*EQUATION\n");
          fprintf(handle, "%d\n", 2);
          fprintf(handle, "%d,%d,%lf, %d,%d,%lf \n"
          , node[i].nr, DOFX, n_normx[node[i].nr]
	  , node[i].nr, DOFY, n_normy[node[i].nr] );
        }
        else if((xx>MIN_VECTOR)
        &&(yy<=MIN_VECTOR)&&(zz<=MIN_VECTOR))
        {
          fprintf(handle, "*BOUNDARY\n");
          fprintf(handle, "%d,%d \n"
          , node[i].nr, DOFX );
        }
        else if((yy>MIN_VECTOR)
        &&(xx<=MIN_VECTOR)&&(zz<=MIN_VECTOR))
        {
          fprintf(handle, "*BOUNDARY\n");
          fprintf(handle, "%d,%d \n"
          , node[i].nr, DOFY );
        }
        else if((zz>MIN_VECTOR)
        &&(xx<=MIN_VECTOR)&&(yy<=MIN_VECTOR))
        {
          fprintf(handle, "*BOUNDARY\n");
          fprintf(handle, "%d,%d \n"
          , node[i].nr, DOFZ );
        }
        else printf("ERROR; no slider created for node:%d\n", node[i].nr);
      }
    }
    fclose(handle);
  }
  /* write the sliders in ansys-format */
  else if (compare( format, "ans", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    {
      printf ("\nThe input file %s could not be opened.\n\n", prognam); 
      return;
    }
    fprintf(handle, "! Sliders based on %s\n", setname);
    for (i=0; i<anz->n; i++ )
    {
      if( sum_n[node[i].nr] >0 )
      {
        printf("node %d Norm:%lf %lf %lf Koord:%lf %lf %lf\n"
               , node[i].nr
	       , n_normx[node[i].nr], n_normy[node[i].nr], n_normz[node[i].nr]
               ,node[node[i].nr].nx,node[node[i].nr].ny,node[node[i].nr].nz ); 

        xx=n_normx[node[i].nr]*n_normx[node[i].nr];
        yy=n_normy[node[i].nr]*n_normy[node[i].nr];
        zz=n_normz[node[i].nr]*n_normz[node[i].nr];
        if((xx>MIN_VECTOR)&&(yy>MIN_VECTOR)
        &&(zz>MIN_VECTOR))
        {
          neqn++;
          fprintf(handle, "CE,%d,0,%d,%s,%lf, %d,%s,%lf,  %d,%s,%lf\n", neqn
          , node[i].nr, "UX", n_normx[node[i].nr] 
          , node[i].nr, "UY", n_normy[node[i].nr]
          , node[i].nr, "UZ", n_normz[node[i].nr] );
        }
        else if((xx<=MIN_VECTOR)
        &&(yy>MIN_VECTOR)&&(zz>MIN_VECTOR))
        {
          neqn++;
          fprintf(handle, "CE,%d,0,%d,%s,%lf, %d,%s,%lf,  %d,%s,%lf\n", neqn
          , -node[i].nr, "UX", n_normx[node[i].nr] 
          , node[i].nr, "UY", n_normy[node[i].nr]
          , node[i].nr, "UZ", n_normz[node[i].nr] );
        }
        else if((yy<=MIN_VECTOR)
        &&(xx>MIN_VECTOR)&&(zz>MIN_VECTOR))
        {
          neqn++;
          fprintf(handle, "CE,%d,0,%d,%s,%lf, %d,%s,%lf,  %d,%s,%lf\n", neqn
          , node[i].nr, "UX", n_normx[node[i].nr] 
          , -node[i].nr, "UY", n_normy[node[i].nr]
          , node[i].nr, "UZ", n_normz[node[i].nr] );
        }
        else if((zz<=MIN_VECTOR)
        &&(xx>MIN_VECTOR)&&(yy>MIN_VECTOR))
        {
          neqn++;
          fprintf(handle, "CE,%d,0,%d,%s,%lf, %d,%s,%lf,  %d,%s,%lf\n", neqn
          , node[i].nr, "UX", n_normx[node[i].nr] 
          , node[i].nr, "UY", n_normy[node[i].nr]
          , -node[i].nr, "UZ", n_normz[node[i].nr] );
        }
        else if((xx>MIN_VECTOR)
        &&(yy<=MIN_VECTOR)&&(zz<=MIN_VECTOR))
        {
          fprintf(handle, "D, %d, UX, 0. \n"
          , node[i].nr );
        }
        else if((yy>MIN_VECTOR)
        &&(xx<=MIN_VECTOR)&&(zz<=MIN_VECTOR))
        {
          fprintf(handle, "D, %d, UY, 0. \n"
          , node[i].nr );
        }
        else if((zz>MIN_VECTOR)
        &&(xx<=MIN_VECTOR)&&(yy<=MIN_VECTOR))
        {
          fprintf(handle, "D, %d, UZ, 0. \n"
          , node[i].nr );
        }
        else printf("ERROR; no slider created for node:%d\n", node[i].nr);
      }
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }

  free(face4);
  free(elem2);
  free(norm2);
  free(n_normx);
  free(n_normy);
  free(n_normz);
  free(sum_n);
  face4=NULL;
  elem2=NULL;
  norm2=NULL;
  n_normx=NULL;
  n_normy=NULL;
  n_normz=NULL;
  sum_n=NULL;

  printf (" ready\n");
}


void sendPressure( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, unsigned char *val1, unsigned char *val2 )
{
  int   setNr;
  int  length, i,j;
  unsigned char prognam[MAX_LINE_LENGTH];
  double *pressure;
  FILE *handle;
  int  n, lc, entity;

  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".dlo");

  if((pressure = (double *)malloc((set[setNr].anz_f+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  /* check if a constant value or a dataset has to be used */
  if(compare(val1, "lc", 2)==2)
  {
    lc=atoi(&val1[2])-1;
    entity=atoi(&val2[1])-1;

    /* check if this lc and entity are valid */
    if((lc>=anz->l)||(lcase[lc].ncomps<entity))
    { printf("ERROR: lc:%d or entity:%d not defined\n",lc,entity); return; }

    /* calculate the average load on that face */
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      pressure[j]=0.;
      if(face[i].type==7)
      {
        for(n=0; n<3; n++) pressure[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        pressure[j]/=3.;
      }
      else if(face[i].type==8)
      {
        for(n=0; n<6; n++) pressure[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        pressure[j]/=6.;
      }
      else if(face[i].type==9)
      {
        for(n=0; n<4; n++) pressure[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        pressure[j]/=4.;
      }
      else if(face[i].type==10)
      {
        for(n=0; n<8; n++) pressure[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        pressure[j]/=8.;
      }
      else
      {
        printf(" Not supported for this element type, load set to 0.\n");
        pressure[i]=0.;
      }
    }
  }
  else
  {
    for (j=0; j<set[setNr].anz_f; j++ ) pressure[j]=atof(val1);
  }

  /* write the pressure in nastran-format */
  if (compare( format, "nas", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "$ Pressure based on %s\n", setname);
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      if (face[i].type==9)
      {
        fprintf(handle, "PLOAD4,%8d,%8d,%12.5e, , , ,%8d,%8d\n",
          1, face[i].elem_nr, pressure[j], face[i].nod[0], face[i].nod[2] );
      }
      else
        printf(" Pressure not supported for this element type:%d\n", face[i].type);
    }
    fclose(handle);
  }
  /* write the pressure in abaqus-format */
  else if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** Pressure based on %s\n", setname);
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      if (face[i].nr>=0)
      {
        fprintf(handle, "%d, P%d, %lf \n",
          face[i].elem_nr, face[i].nr+1,  pressure[j] );
      }
      else if (face[i].nr==-1)
      {
        fprintf(handle, "%d, P, %lf \n",
          face[i].elem_nr,  pressure[j] );
      }
      else
        printf(" Not supported for this element type\n");
    }
    fclose(handle);
  }
  /* write the pressure in ansys-format */
  else if (compare( format, "ans", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "! Pressure based on %s\n", setname);
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      if (face[i].nr==0)
        fprintf(handle, "SFE, %d, 1, PRES, ,%lf \n",
        face[i].elem_nr,  pressure[j] );
      else if (face[i].nr==1)
        fprintf(handle, "SFE, %d, 6, PRES, ,%lf \n",
        face[i].elem_nr,  pressure[j] );
      else if (face[i].nr==2)
        fprintf(handle, "SFE, %d, 2, PRES, ,%lf \n",
        face[i].elem_nr,  pressure[j] );
      else if (face[i].nr==3)
        fprintf(handle, "SFE, %d, 3, PRES, ,%lf \n",
        face[i].elem_nr,  pressure[j] );
      else if (face[i].nr==4)
        fprintf(handle, "SFE, %d, 4, PRES, ,%lf \n",
        face[i].elem_nr,  pressure[j] );
      else if (face[i].nr==5)
        fprintf(handle, "SFE, %d, 5, PRES, ,%lf \n",
        face[i].elem_nr,  pressure[j] );
      else
        printf(" Not supported for this element type\n");
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  free(pressure);
  printf (" ready\n");
}


void sendFilm( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, unsigned char *val1, unsigned char *val2, unsigned char *val3, unsigned char *val4 )
{
  int   setNr;
  int  length, i,j;
  unsigned char prognam[MAX_LINE_LENGTH];
  double *temp, *alpha;
  FILE *handle;
  int  n, lc, entity;

  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".flm");

  if((temp = (double *)malloc((set[setNr].anz_f+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if((alpha = (double *)malloc((set[setNr].anz_f+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  /* check if a constant value or a dataset has to be used */
  if(compare(val1, "lc", 2)==2)
  {
    lc=atoi(&val1[2])-1;
    entity=atoi(&val2[1])-1;

    /* check if this lc and entity are valid */
    if((lc>=anz->l)||(lcase[lc].ncomps<entity))
    { printf("ERROR: lc:%d or entity:%d not defined\n",lc,entity); return; }

    /* calculate the average load on that face */
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      temp[j]=0.;
      if(face[i].type==7)
      {
        for(n=0; n<4; n++) temp[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        temp[j]/=4.;
      }
      else if(face[i].type==10)
      {
        for(n=0; n<8; n++) temp[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        temp[j]/=8.;
      }
      else
      {
        printf(" Not supported for this element type, temp set to 0.\n");
        temp[i]=0.;
      }
    }
  }
  else
  {
    for (j=0; j<set[setNr].anz_f; j++ ) temp[j]=atof(val1);
  }

  if((compare(val2, "lc", 2)==2)||(compare(val3, "lc", 2)==2))
  {
    if(compare(val2, "lc", 2))
    {
      lc=atoi(&val2[2])-1;
      entity=atoi(&val3[1])-1;
    }
    else
    {
      lc=atoi(&val3[2])-1;
      entity=atoi(&val4[1])-1;
    }

    /* check if this lc and entity are valid */
    if((lc>=anz->l)||(lcase[lc].ncomps<entity))
    { printf("ERROR: lc:%d or entity:%d not defined\n",lc,entity); return; }

    /* calculate the average load on that face */
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      alpha[j]=0.;
      if(face[i].type==7)
      {
        for(n=0; n<4; n++) temp[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        alpha[j]/=4.;
      }
      else if(face[i].type==10)
      {
        for(n=0; n<8; n++) alpha[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        alpha[j]/=8.;
      }
      else
      {
        printf(" Not supported for this element type, alpha set to 0.\n");
        alpha[i]=0.;
      }
    }
  }
  else
  {
    if(compare(val1, "lc", 2)==2)
      for (j=0; j<set[setNr].anz_f; j++ ) alpha[j]=atof(val3);
    else
      for (j=0; j<set[setNr].anz_f; j++ ) alpha[j]=atof(val2);
  }

  /* write in abaqus-format */
  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** Film based on %s\n", setname);
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      if (face[i].nr==-1)
      {
        fprintf(handle, "%d, F, %lf, %e\n",
          face[i].elem_nr, temp[j], alpha[j] );
      }
      else if (face[i].nr>=0)
      {
        fprintf(handle, "%d, F%d, %lf, %e \n",
          face[i].elem_nr, face[i].nr+1, temp[j], alpha[j] );
      }
      else
        printf(" Not supported for this element type\n");
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  free(temp);
  free(alpha);
  printf (" ready\n");
}


void sendRadiate( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, unsigned char *val1, unsigned char *val2, unsigned char *val3, unsigned char *val4 )
{
  int   setNr;
  int  length, i,j;
  unsigned char prognam[MAX_LINE_LENGTH];
  double *temp, *emisivity;
  FILE *handle;
  int  n, lc, entity;

  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".rad");

  if((temp = (double *)malloc((set[setNr].anz_f+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );
  if((emisivity = (double *)malloc((set[setNr].anz_f+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  /* check if a constant value or a dataset has to be used */
  if(compare(val1, "lc", 2)==2)
  {
    lc=atoi(&val1[2])-1;
    entity=atoi(&val2[1])-1;

    /* check if this lc and entity are valid */
    if((lc>=anz->l)||(lcase[lc].ncomps<entity))
    { printf("ERROR: lc:%d or entity:%d not defined\n",lc,entity); return; }

    /* calculate the average load on that face */
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      temp[j]=0.;
      if(face[i].type==7)
      {
        for(n=0; n<4; n++) temp[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        temp[j]/=4.;
      }
      else if(face[i].type==10)
      {
        for(n=0; n<8; n++) temp[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        temp[j]/=8.;
      }
      else
      {
        printf(" Not supported for this element type, temp set to 0.\n");
        temp[i]=0.;
      }
    }
  }
  else
  {
    for (j=0; j<set[setNr].anz_f; j++ ) temp[j]=atof(val1);
  }

  if((compare(val2, "lc", 2)==2)||(compare(val3, "lc", 2)==2))
  {
    if(compare(val2, "lc", 2))
    {
      lc=atoi(&val2[2])-1;
      entity=atoi(&val3[1])-1;
    }
    else
    {
      lc=atoi(&val3[2])-1;
      entity=atoi(&val4[1])-1;
    }

    /* check if this lc and entity are valid */
    if((lc>=anz->l)||(lcase[lc].ncomps<entity))
    { printf("ERROR: lc:%d or entity:%d not defined\n",lc,entity); return; }

    /* calculate the average load on that face */
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      emisivity[j]=0.;
      if(face[i].type==7)
      {
        for(n=0; n<4; n++) temp[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        emisivity[j]/=4.;
      }
      else if(face[i].type==10)
      {
        for(n=0; n<8; n++) emisivity[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        emisivity[j]/=8.;
      }
      else
      {
        printf(" Not supported for this element type, emisivity set to 0.\n");
        emisivity[i]=0.;
      }
    }
  }
  else
  {
    if(compare(val1, "lc", 2)==2)
      for (j=0; j<set[setNr].anz_f; j++ ) emisivity[j]=atof(val3);
    else
      for (j=0; j<set[setNr].anz_f; j++ ) emisivity[j]=atof(val2);
  }

  /* write in abaqus-format */
  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** Radiate based on %s\n", setname);
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      if (face[i].nr==-1)
      {
        fprintf(handle, "%d, R, %lf, %e\n",
          face[i].elem_nr, temp[j], emisivity[j] );
      }
      else if (face[i].nr>=0)
      {
        fprintf(handle, "%d, R%d, %lf, %e \n",
          face[i].elem_nr, face[i].nr+1, temp[j], emisivity[j] );
      }
      else
        printf(" Not supported for this element type\n");
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  free(temp);
  free(emisivity);
  printf (" ready\n");
}


void sendDflux( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, unsigned char *val1, unsigned char *val2 )
{
  int   setNr;
  int  length, i,j;
  unsigned char prognam[MAX_LINE_LENGTH];
  double *load;
  FILE *handle;
  int  n, lc, entity;

  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".dfl");

  if((load = (double *)malloc((set[setNr].anz_f+1) * sizeof(double))) == NULL )
    printf("\n\n ERROR: malloc failure\n\n" );

  /* check if a constant value or a dataset has to be used */
  if(compare(val1, "lc", 2)==2)
  {
    lc=atoi(&val1[2])-1;
    entity=atoi(&val2[1])-1;

    /* check if this lc and entity are valid */
    if((lc>=anz->l)||(lcase[lc].ncomps<entity))
    { printf("ERROR: lc:%d or entity:%d not defined\n",lc,entity); return; }

    /* calculate the average load on that face */
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      load[j]=0.;
      if(face[i].type==7)
      {
        for(n=0; n<4; n++) load[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        load[j]/=4.;
      }
      else if(face[i].type==10)
      {
        for(n=0; n<8; n++) load[j]+=lcase[lc].dat[entity][face[i].nod[n]];
        load[j]/=8.;
      }
      else
      {
        printf(" Not supported for this element type, load set to 0.\n");
        load[i]=0.;
      }
    }
  }
  else
  {
    for (j=0; j<set[setNr].anz_f; j++ ) load[j]=atof(val1);
  }

  /* write in abaqus-format */
  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** DFlux based on %s\n", setname);
    for (j=0; j<set[setNr].anz_f; j++ )
    {
      i=set[setNr].face[j];
      if (face[i].nr==-1)
      {
        fprintf(handle, "%d, S, %e\n",
          face[i].elem_nr, load[j] );
      }
      else if (face[i].nr>=0)
      {
        fprintf(handle, "%d, S%d, %e \n",
          face[i].elem_nr, face[i].nr+1, load[j] );
      }
      else
        printf(" Not supported for this element type\n");
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  free(load);
  printf (" ready\n");
}


void sendSPC( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, unsigned char *value, unsigned char *val2 )
{
  int   setNr;
  int  length, i,j, dofi, disp_lc;
  unsigned char prognam[MAX_LINE_LENGTH], dofa[2]={"  "};

  FILE *handle;


  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".bou");

  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** BOUNDARY based on %s\n", setname);
    length=strlen( value );
    for (i=0; i<length; i++)
    {
      dofa[0]=value[i];
      dofi=atoi(dofa);
      if ((value[i]!='t')&&((dofi<0)||(dofi>6)))  errMsg ("ERROR dof:%d not known\n", dofi);
      else 
      {
        if(val2[0]=='r')
	{
          disp_lc=atoi(&val2[1])-1;
          if(lcase[disp_lc].ncomps<dofi)
          { printf ("\nERROR: The dataset %s has no %d. component.\n\n", lcase[disp_lc].name, dofi); return; }
          for (j=0; j<set[setNr].anz_n; j++ )
          {
            fprintf(handle, "%d, %1c, , %e\n",set[setNr].node[j], value[i],  lcase[disp_lc].dat[dofi-1][set[setNr].node[j]] );
          }
	}
        else
	{
          for (j=0; j<set[setNr].anz_n; j++ )
          {
            if( value[i]=='t') fprintf(handle, "%d, %d, , %s\n",set[setNr].node[j], DOFT, val2  );
            else fprintf(handle, "%d, %1c, , %s\n",set[setNr].node[j], value[i], val2  );
          }
	}
      }
    }
    fclose(handle);
  }
  else if (compare( format, "ans", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "! BOUNDARY based on %s\n", setname);
    length=strlen( value );
    dofa[1]=(unsigned char)NULL;
    for (i=0; i<length; i++)
    {
      dofa[0]=value[i];
      dofi=atoi(dofa);
      if ((dofi<1)||(dofi>6))  errMsg ("ERROR dof:%d not known\n", dofi);
      else {
      for (j=0; j<set[setNr].anz_n; j++ )
      {
        if (dofi==1) fprintf(handle, "D,%d, UX, 0.  \n",set[setNr].node[j]);
        if (dofi==2) fprintf(handle, "D,%d, UY, 0.  \n",set[setNr].node[j]);
        if (dofi==3) fprintf(handle, "D,%d, UZ, 0.  \n",set[setNr].node[j]);
        if (dofi==4) fprintf(handle, "D,%d, ROTX, 0. \n",set[setNr].node[j]);
        if (dofi==5) fprintf(handle, "D,%d, ROTY, 0. \n",set[setNr].node[j]);
        if (dofi==6) fprintf(handle, "D,%d, ROTZ, 0. \n",set[setNr].node[j]);
      }
      }
    }
    fclose(handle);
  }
  else if (compare( format, "nas", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "$ BOUNDARY based on %s\n", setname);
    length=strlen( value );
    dofa[1]=(unsigned char)NULL;
    for (i=0; i<length; i++)
    {
      dofa[0]=value[i];
      dofi=atoi(dofa);
      if ((dofi<1)||(dofi>6))  errMsg ("ERROR dof:%d not known\n", dofi);
      else {
       for (j=0; j<set[setNr].anz_n; j++ )
       {
        fprintf(handle, "SPC1, 1,%8d,%8d\n", dofi, set[setNr].node[j]);
       }
      }
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  printf (" ready\n");
}


void sendCflux( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, unsigned char *value, unsigned char *val2 )
{
  int   setNr;
  int  length, i,j, dofi=0;
  unsigned char prognam[MAX_LINE_LENGTH];

  FILE *handle;


  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".cfl");

  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** Cflux based on %s\n", setname);
    length=strlen( value );
    for (i=0; i<length; i++)
    {
      if (value[i]!='t') errMsg ("ERROR dof:%d not known\n", dofi);
      else 
      {
        for (j=0; j<set[setNr].anz_n; j++ )
        {
          fprintf(handle, "%d, %d, %s\n",set[setNr].node[j], DOFT, val2  );
	}
      }
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  printf (" ready\n");
}


void sendForce( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire, double *f )
{
  int   setNr;
  int  length, i,j;
  unsigned char prognam[MAX_LINE_LENGTH];

  FILE *handle;


  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".frc");

  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** Forces based on %s\n", setname );
    for (j=0; j<set[setNr].anz_n; j++ )
    {
      for (i=0; i<3; i++)
      {
        if(f[i]) fprintf(handle, "%d, %d, %lf\n",set[setNr].node[j], i+1, f[i]  );
      }
    }
    fclose(handle);
  }
  else if (compare( format, "ans", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "! Forces based on %s\n", setname );
    for (j=0; j<set[setNr].anz_n; j++ )
    {
      if(f[0]) fprintf(handle, "F, %d, FX, %lf\n",set[setNr].node[j], f[0] );
      if(f[1]) fprintf(handle, "F, %d, FY, %lf\n",set[setNr].node[j], f[1] );
      if(f[2]) fprintf(handle, "F, %d, FZ, %lf\n",set[setNr].node[j], f[2] );
    }
    fclose(handle);
  }
  else if (compare( format, "nas", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "$ Forces based on %s\n", setname );
    for (j=0; j<set[setNr].anz_n; j++ )
    {
      fprintf(handle, "FORCE, 1,%8d,%8d,%12.5e,%12.5e,%12.5e,%12.5e\n", set[setNr].node[j],0,1.,f[0],f[1],f[2]);
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  printf (" ready\n");
}


void sendNames( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire )
{
  int   j,incr=0;
  int   setNr, length;
  unsigned char prognam[MAX_LINE_LENGTH];

  FILE *handle;


  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".nam");
  printf (" write file: %s\n", prognam );

  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** Names based on %s\n", setname );
    if (set[setNr].anz_n>0)
    {
      fprintf(handle, "*NSET,NSET=N%s \n",set[setNr].name  );
      for (j=0; j<set[setNr].anz_n; j++ )
      {
        fprintf(handle, "%d, \n",set[setNr].node[j]  );
      }
    }
    if (set[setNr].anz_e>0)
    {
      fprintf(handle, "*ELSET,ELSET=E%s \n",set[setNr].name  );
      for (j=0; j<set[setNr].anz_e; j++ )
      {
        fprintf(handle, "%d, \n",set[setNr].elem[j]  );
      }
    }
    fclose(handle);
  }
  else if (compare( format, "ans", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "! Names based on %s\n", setname );
    if (set[setNr].anz_n>0)
    {
      fprintf(handle, "NSEL, NONE \n"); 
      fprintf(handle, "NSEL, S, NODE, , %d \n",set[setNr].node[0]);
      for (j=1; j<set[setNr].anz_n; j++ )
      {
        fprintf(handle, "NSEL, A, NODE, , %d \n",set[setNr].node[j]);
      }
      fprintf(handle, "CM, %s, NODE \n",set[setNr].name); 
      fprintf(handle, "NSEL, ALL \n"); 
    }
    if (set[setNr].anz_e>0)
    {
      fprintf(handle, "ESEL, NONE \n"); 
      fprintf(handle, "ESEL, S, ELEM, , %d \n",set[setNr].elem[0]);
      for (j=1; j<set[setNr].anz_e; j++ )
      {
        fprintf(handle, "ESEL, A, ELEM, , %d \n",set[setNr].elem[j]);
      }
      fprintf(handle, "CM, %s, ELEM \n",set[setNr].name); 
      fprintf(handle, "ESEL, ALL \n"); 
    }
    fclose(handle);
  }
  /* interface to code_aster from Paul CARRICO */
  else if (compare( format, "ast", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL ) 
    {
       printf ("\nThe input file %s could not be opened.\n\n", prognam); 
       return;
    }
    if (set[setNr].anz_n>0)
    {
      fprintf(handle, "GROUP_NO\n%s\n",set[setNr].name);
      for (j=0; j<set[setNr].anz_n; j++ )
      {
        fprintf(handle, "N%d ",set[setNr].node[j]);
        incr++;
        if (incr == 8)
        {
          fprintf(handle, "\n");
          incr = 0;
        }
      }
      if (incr != 0) fprintf(handle, "\n");
      fprintf(handle, "FINSF\n");
    }
    if (set[setNr].anz_e>0)
    {
      fprintf(handle, "GROUP_MA\n");
      fprintf(handle, "%s\n",set[setNr].name  );
      incr = 0;
      for (j=0; j<set[setNr].anz_e; j++ )
      {
        fprintf(handle, "M%d ",set[setNr].elem[j]  );
        incr++;
        if (incr == 8) 
	{
          fprintf(handle, "\n");
	  incr = 0;
        }
      }
      if (incr != 0) fprintf(handle, "\n");
      fprintf(handle, "FINSF\n");
    }
    fclose(handle);
  }
  /* interface to Samcef/Bacon from Paul CARRICO */
  else if (compare( format, "sam", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL ) 
    {
       printf ("\nThe input file %s could not be opened.\n\n", prognam); 
       return;
    }
    if (set[setNr].anz_n>0)
    {
      fprintf(handle, ".SEL GROUPE \"%s\" NOEUDS\n",set[setNr].name);
      for (j=0; j<set[setNr].anz_n; j++ )
      {
        fprintf(handle, " I %d\n",set[setNr].node[j]);
      }
    }
    if (set[setNr].anz_e>0)
    {
      fprintf(handle, ".SEL GROUPE \"%s\" MAILLES\n",set[setNr].name);
      for (j=0; j<set[setNr].anz_e; j++ )
      {
        fprintf(handle, " I %d\n",set[setNr].elem[j]  );
      }
    }
    fclose(handle);
  }

  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  printf (" ready\n");
}



void sendSurfaces( unsigned char *setname, unsigned char *format, Summen *anz, Nodes *node, Elements *e_enqire )
{
  int   i,j;
  int   setNr, length;
  unsigned char prognam[MAX_LINE_LENGTH];

  FILE *handle;


  setNr=getSetNr(setname);
  if (setNr<0)
  {
    printf (" ERROR: set:%s does not exist\n", setname);
    return;
  }

  strcpy ( prognam, setname);
  length= strlen ( setname );
  strcpy (&prognam[length], ".sur");
  printf (" write file: %s\n", prognam );

  if (compare( format, "abq", 3)== 3)
  {
    handle = fopen (prognam, "w");
    if ( handle== NULL )
    { printf ("\nThe input file %s could not be opened.\n\n", prognam); return; }
    fprintf(handle, "** Surfaces based on %s\n", setname );
    if (set[setNr].anz_f>0)
    {
      for (j=0; j<set[setNr].anz_f; j++ )
      {
        i=set[setNr].face[j];
        if (face[i].nr>=0)
        {
          fprintf(handle, "%d, S%d \n",
            face[i].elem_nr, face[i].nr+1 );
        }
        else if (face[i].nr==-1)
        {
          fprintf(handle, "%d, S \n",
            face[i].elem_nr );
        }
        else
          printf(" Not supported for this element type\n");
        }
    }
    fclose(handle);
  }
  else
  {
    errMsg(" ERROR: format %s not yet supported\n", format );
  }
  printf (" ready\n");
}



