
#include <extUtil.h>

int write2aba( unsigned char *datout, Summen *anz, Nodes *node, Elements *elem, Datasets *lcase, unsigned char **dat )
{
  FILE *handle1=NULL, *handle2=NULL;
  int  i,j, lc, lendatout;
  double oldtime=0, iniTime=0.001, time1=0, time2=0, frc1=0, frc2=0, tmin, tmax;

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

  if((anz->l>0)&&(dat!=0))
  {
   if(strlen(dat[0])>0)
   {
    if(compare( dat[0], "lc", 2)== 2)
      sprintf( &datout[lendatout], "_lc%d.tem", atoi(dat[1]));
    else strcpy (&datout[lendatout], ".tem");
    handle2 = fopen (datout, "w+b");
    if (handle2==NULL) { printf ("\nThe output file \"%s\" could not be opened.\n\n",
      datout); return(-1);}
    else  printf (" file %s opened\n",datout);
   }
  }

  printf (" write abaqus data \n");

  fprintf (handle1, "*NODE, NSET=N%s\n", anz->model);
  for (i=0; i<anz->n; i++)
  {
    fprintf (handle1, "%8d, %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);
  }

  for (i=0; i<anz->e; i++)
  {
    if (elem[i].type == 1)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        if (elem[i].attr==2)  fprintf (handle1, "*ELEMENT, TYPE=C3D8I, ELSET=E%s\n", anz->model);
        else if (elem[i].attr==1) fprintf (handle1, "*ELEMENT, TYPE=C3D8R, ELSET=E%s\n", anz->model);
        else                 fprintf (handle1, "*ELEMENT, TYPE=C3D8, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3], elem[i].nod[4], elem[i].nod[5], elem[i].nod[6], elem[i].nod[7]);
    }
    else if (elem[i].type == 2)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=C3D6, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d,%6d,%6d,%6d,%6d,%6d,%6d\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3], elem[i].nod[4], elem[i].nod[5]);
    }
    else if (elem[i].type == 3)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=C3D4, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d,%6d,%6d,%6d,%6d \n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3]);
    }
    else if (elem[i].type == 4)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        if (elem[i].attr==1) fprintf (handle1, "*ELEMENT, TYPE=C3D20R, ELSET=E%s\n", anz->model);
        else            fprintf (handle1, "*ELEMENT, TYPE=C3D20, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3], elem[i].nod[4], elem[i].nod[5], elem[i].nod[6],
        elem[i].nod[7], elem[i].nod[8], elem[i].nod[9]);
      fprintf (handle1, "      %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d\n",
        elem[i].nod[10], elem[i].nod[11], elem[i].nod[16], elem[i].nod[17],
        elem[i].nod[18], elem[i].nod[19], elem[i].nod[12], elem[i].nod[13],
        elem[i].nod[14], elem[i].nod[15] );
    }
    else if (elem[i].type == 5)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=C3D15, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3], elem[i].nod[4], elem[i].nod[5], elem[i].nod[6],
        elem[i].nod[7], elem[i].nod[8]);
      fprintf (handle1, "      %6d,%6d,%6d,%6d,%6d,%6d\n",
        elem[i].nod[12], elem[i].nod[13], elem[i].nod[14], elem[i].nod[9],
        elem[i].nod[10], elem[i].nod[11] );
    }
    else if (elem[i].type == 6)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=C3D10, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d \n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3], elem[i].nod[4], elem[i].nod[5], elem[i].nod[6],
        elem[i].nod[7], elem[i].nod[8], elem[i].nod[9]);
    }
    else if (elem[i].type == 7)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=S3R, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d,%6d, %6d, %6d \n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2] );
    }
    else if (elem[i].type == 8)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=STRI65, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d, %6d, %6d, %6d, %6d, %6d, %6d\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3], elem[i].nod[4], elem[i].nod[5]);
    }
    else if (elem[i].type == 9)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=S4, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d, %6d, %6d, %6d, %6d\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2],
        elem[i].nod[3]);
    }
    else if (elem[i].type == 10)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=S8, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d, %6d, %6d, %6d, %6d, %6d, %6d, %6d, %6d\n"
      , elem[i].nr, elem[i].nod[0], elem[i].nod[1], elem[i].nod[2], elem[i].nod[3]
      , elem[i].nod[4], elem[i].nod[5], elem[i].nod[6], elem[i].nod[7]);
    }
    else if (elem[i].type == 11)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=B31, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d, %6d, %6d\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[1] );
    }
    else if (elem[i].type == 12)
    {
      if((i==0) || ((i>0)&&(elem[i-1].type!=elem[i].type)) || ((i>0)&&(elem[i-1].attr!=elem[i].attr)))
      {
        fprintf (handle1, "*ELEMENT, TYPE=B32, ELSET=E%s\n", anz->model);
      }
      fprintf (handle1, "%6d, %6d, %6d, %6d\n",
        elem[i].nr, elem[i].nod[0], elem[i].nod[2], elem[i].nod[1] );
    }
    else
    {
      printf (" WARNING: elem(%d) not a known type (%d)\n", elem[i].nr, elem[i].type);
    }
  }


  if((anz->l>0)&&(dat!=0))
  {
   j=0;
   for (lc=0; lc<anz->l; lc++)
   {
    if (lcase[lc].ncomps == 1)
    {
      if((compare( dat[0], "lc", 2)== 2)&&(atoi(dat[1])!=lc+1)) goto next;
      
      j++;
      if(compare( dat[0], "tmf", 3)== 3)
      {
        fprintf (handle2, "** ---  LC%d ---\n", j);
        fprintf (handle2, "*STEP\n");
        /* fprintf (handle2, "*STATIC, SOLVER=SPOOLES\n"); */
        fprintf (handle2, "*STATIC\n");
        fprintf (handle2, "*TEMPERATURE\n");
      }
      if(compare( dat[0], "crp", 3)== 3)
      {
        /* local step time and load */
        /* load_new = load_old * speed**2 / refspeed**2 */
        time1 = 0.;
        frc1  = frc2;
        time2 = (lcase[lc].value*atof(dat[1]))-oldtime;
        oldtime=(lcase[lc].value*atof(dat[1]));
        if (time2<iniTime) time2=iniTime;
        frc2  = (atof(lcase[lc].dataset_text)*atof(lcase[lc].dataset_text))/(atof(dat[2])*atof(dat[2]));
        tmin  = time2/10.;
        tmax  = time2;
        fprintf (handle2, "** ---  LC%d ---\n", j);
        fprintf (handle2, "*AMPLITUDE, NAME=FORCE%d\n", j);
        fprintf (handle2, "%lf, %lf, %lf, %lf\n", time1, frc1, time2, frc2);
        fprintf (handle2, "*STEP, NLGEOM, INC=400, AMPLITUDE=RAMP\n");
        fprintf (handle2, "*VISCO, CETOL=5.e-4\n");
        fprintf (handle2, "%lf, %lf,,,\n", tmin, tmax);
        fprintf (handle2, "*CLOAD, AMPLITUDE=FORCE%d\n", j);
        fprintf (handle2, "*include, input=creep.clo\n");
        fprintf (handle2, "*TEMPERATURE\n");
      }

      for (i=0; i<anz->n; i++)
      {
        fprintf (handle2, "%8d, %12.5f\n", node[i].nr,
        lcase[lc].dat[0][node[i].nr]);
      }
      
      if(compare( dat[0], "tmf", 3)== 3)
      {
        fprintf (handle2, "*NODE PRINT, FREQUENCY=0 \n");
        fprintf (handle2, "*EL PRINT, FREQUENCY=0 \n");
        fprintf (handle2, "*NODE FILE\n");
        fprintf (handle2, "U, NT \n");
        fprintf (handle2, "*EL FILE,POSITION=AVERAGED AT NODES\n");
        fprintf (handle2, "S \n");
        fprintf (handle2, "*END STEP\n");
      }
      if(compare( dat[0], "crp", 3)== 3)
      {
        fprintf (handle2, "*NODE PRINT, FREQUENCY=0 \n");
        fprintf (handle2, "*EL PRINT, FREQUENCY=0 \n");
        fprintf (handle2, "*NODE FILE, FREQUENCY=%d\n", atoi(dat[3]));
        fprintf (handle2, "U, NT \n");
        fprintf (handle2, "*EL FILE, FREQUENCY=%d,POSITION=AVERAGED AT NODES\n", atoi(dat[3]));
        fprintf (handle2, "S, CE \n");
        fprintf (handle2, "*END STEP\n");
      }
    next:;
    }
   }
   fclose(handle2);
  }

  fclose(handle1);
  return (1);
}

