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



int write2frd(unsigned char *datout, Summen *anz, Nodes *node, Elements *elem, Datasets *lcase )
{
  FILE *handle1;
  register int  i, j, n, ipuf;
  int nlc, cnam, formatFlag=0;
  unsigned char buffer[MAX_LINE_LENGTH], format[10][MAX_LINE_LENGTH];
  unsigned char rec_str[MAX_LINE_LENGTH];

  /* Open the files and check to see that it was opened correctly */
  handle1 = fopen (datout, "w+b");
  if (handle1==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",
     datout); return(-1);}
  else  printf ("\n%s opened\n",datout);

  printf ("anz->nmax:%d anz->nmin:%d anz->emax:%d anz->emin:%d anz->l:%d\n",anz->nmax, anz->nmin, anz->emax, anz->emin, anz->l ); 

  printf ("\n write frd file\n");
  fprintf (handle1, "    1C%s\n", anz->model);

  /* determine if long or short format can be used */
  if (anz->n > 0)
    if (anz->nmax>99999) formatFlag=1;
  if (anz->e > 0)
    if(anz->emax>99999) formatFlag=1;



  if (anz->n > 0)
  {
    if (!formatFlag)
    {
      fprintf (handle1
      , "    2C                                                                   0\n");
      for (i=0; i<anz->n; i++)
      {
        fprintf (handle1, " -1%5d%12.5e%12.5e%12.5e\n"
        , node[i].nr, node[node[i].nr].nx, node[node[i].nr].ny, node[node[i].nr].nz);
      }
    }
    else
    {
      fprintf (handle1
      , "    2C                                                                   1\n");
      for (i=0; i<anz->n; i++)
      {
        fprintf (handle1, " -1%10d%12.5e%12.5e%12.5e\n"
        , node[i].nr, node[node[i].nr].nx, node[node[i].nr].ny, node[node[i].nr].nz);
      }
    }
    fprintf (handle1, " -3\n");
  }

  if (anz->e > 0)
  {
    if (!formatFlag) fprintf (handle1
      , "    3C                                                                   0\n");
    else fprintf (handle1
      , "    3C                                                                   1\n");
    for (i=0; i<anz->e; i++)
    {
      ipuf=0;
      if (elem[i].type == 1) ipuf = 8;  /* HEXA8  */
      if (elem[i].type == 2) ipuf = 6;  /* PENTA6   */
      if (elem[i].type == 3) ipuf = 4;  /* TET4   */
      if (elem[i].type == 4) ipuf = 20; /* HEXA20 */
      if (elem[i].type == 5) ipuf = 15; /* PENTA15  */
      if (elem[i].type == 6) ipuf = 10; /* TET10  */
      if (elem[i].type == 7) ipuf = 3;  /* TRI3   */
      if (elem[i].type == 8) ipuf = 6;  /* TRI6   */
      if (elem[i].type == 9) ipuf = 4;  /* QUAD4  */
      if (elem[i].type == 10) ipuf = 8; /* QUAD8  */
      if (elem[i].type == 11) ipuf = 2;  /* BEAM2  */
      if (elem[i].type == 12) ipuf = 3;  /* BEAM3  */
      if (ipuf==0)
      {
        printf (" elem(%d) not a known type (%d)\n", elem[i].nr, elem[i].type);
      }
      else
      {
        if (!formatFlag)
        {
          fprintf (handle1, " -1%5d%5d%5d%5d\n", elem[i].nr, elem[i].type, elem[i].group, elem[i].mat);
          fprintf (handle1, " -2");
          j=0;
          for (n=0; n<ipuf; n++)
          {
            fprintf (handle1, "%5d", elem[i].nod[n] );
            if (j<14) j++;
            else
            {
              if (n<ipuf-1) fprintf (handle1, "\n -2"); j=0;
            }
          }
          fprintf (handle1, "\n");
        }
        else
        {
          fprintf (handle1, " -1%10d%5d%5d%5d\n", elem[i].nr, elem[i].type, elem[i].group, elem[i].mat);
          fprintf (handle1, " -2");
          j=0;
          for (n=0; n<ipuf; n++)
          {
            fprintf (handle1, "%10d", elem[i].nod[n] );
            if (j<9) j++;
            else
            {
              if (n<ipuf-1) fprintf (handle1, "\n -2"); j=0;
            }
          }
          fprintf (handle1, "\n");
        }
      }
    }
    fprintf (handle1, " -3\n");
  }

  if (anz->l > 0)
  {
    for (nlc=0; nlc< anz->l; nlc++)
    {
      /* special purpose block, for element-normals only (sendSurfNormalen()) */
      if (lcase[nlc].irtype == 3)
      {
        printf ("   21C%s%12.5e\n", lcase[nlc].name, lcase[nlc].value);
        if (!formatFlag)
        {
          j=sprintf( buffer, "   21CL%-5d%12.5e", nlc+1, lcase[nlc].value); 
          for (i=j; i<MAX_LINE_LENGTH; i++) buffer[i]=' '; 
          buffer[73]='0'; buffer[75]='\n'; buffer[76]='\0';
          for (i=0; i<76; i++) fprintf( handle1, "%c", buffer[i]);
          strcpy( format[0]," -1%5d%5d%5d    1    1\n"); 
          strcpy( format[1]," -2%5d%12.5e%12.5e%12.5e%12.5e%12.5e%12.5e\n"); 
        }
        else 
        {
          j=sprintf( buffer, "   21CL%-5d%12.5e", nlc+1, lcase[nlc].value); 
          for (i=j; i<MAX_LINE_LENGTH; i++) buffer[i]=' '; 
          buffer[73]='1'; buffer[75]='\n'; buffer[76]='\0';
          for (i=0; i<76; i++) fprintf( handle1, "%c", buffer[i]);
          strcpy( format[0]," -1%10d%5d%5d    1    1\n"); 
          strcpy( format[1]," -2%10d%12.5e%12.5e%12.5e%12.5e%12.5e%12.5e\n"); 
        }
        for (i=0; i<anz->e; i++)
        {
          n=0;
          if (elem[i].type == 1) n=8;
          if (elem[i].type == 4) n=20;
          if (elem[i].type == 7) n=3;
          if (elem[i].type == 9) n=4;
          if (elem[i].type == 11) n=2;
          if (n>0)
          {
            fprintf (handle1, format[0], elem[i].nr, elem[i].type, elem[i].group );
            for (j=0; j<n; j++)
  	    fprintf (handle1, format[1], elem[i].nod[j], 
            lcase[nlc].edat[0][elem[i].nr][j], lcase[nlc].edat[1][elem[i].nr][j], lcase[nlc].edat[2][elem[i].nr][j],0.,0.,0. );
          }
        }
        fprintf (handle1, " -3\n");
      }
      else if (lcase[nlc].irtype == 1)
      {
        for(i=0; i<MAX_LINE_LENGTH; i++) rec_str[i]=' ';
        sprintf(rec_str,"  100C");
        sins(lcase[nlc].dataset_name, 7, 12, rec_str);
        sprintf(buffer,"%-12.5e",lcase[nlc].value);
        sins(buffer, 13, 24, rec_str);
        sins( lcase[nlc].dataset_text, 37, 56, rec_str );
        sprintf(buffer,"%-2d",lcase[nlc].analysis_type);
        sins(buffer , 57, 58, rec_str );
        sprintf(buffer,"%-5d",lcase[nlc].step_number);
        sins(buffer , 59, 63, rec_str );

        sins(lcase[nlc].analysis_name, 64, 73, rec_str);
        if (!formatFlag)
        {
          rec_str[73]='0';
          strcpy( format[0]," -1%5d");
        }
        else 
        {
          rec_str[73]='1';
          strcpy( format[0]," -1%10d");
        }
        /* rec_str[75]='\n';*/
        rec_str[76]=0;

        printf ("%s\n",rec_str);
        fprintf(handle1,"%s\n",rec_str);

        /* get rid of the additional columns which where calc in cgx */
        cnam=0;
        for (i=0; i<lcase[nlc].ncomps; i++ ) if(lcase[nlc].iexist[i]!=0) cnam++;
        fprintf (handle1, " -4  %.8s%5d%5d\n", lcase[nlc].name, lcase[nlc].ncomps-cnam, lcase[nlc].irtype );
        for (i=0; i<lcase[nlc].ncomps-cnam; i++ )
          fprintf (handle1, " -5  %.8s%5d%5d%5d%5d%5d\n", lcase[nlc].compName[i], 1, lcase[nlc].ictype[i], lcase[nlc].icind1[i], lcase[nlc].icind2[i], lcase[nlc].iexist[i]);
        for (i=0; i<anz->n; i++)
        {
	  fprintf (handle1, format[0], node[i].nr);
          for (j=0; j<lcase[nlc].ncomps-cnam; j++)
          {
            if((j%6==0)&&(j>0))
	      { fprintf (handle1,"\n"); if (!formatFlag) fprintf (handle1," -2     " ); else fprintf (handle1," -2          " ); }
            fprintf (handle1,"%12.5e", lcase[nlc].dat[j][node[i].nr]);
	  }
          fprintf (handle1, "\n");
        }
        fprintf (handle1, " -3\n");
      }
      else
      {
        printf("type of Dataset not known:%d\n", lcase[nlc].irtype);
      }

    }
  }

  fprintf (handle1, " 9999\n");
  fclose(handle1);
  return (1);
}

