/* ROVERSTR.C */
/* Program to help calculate tensions, stresses, and column buckling for
the framework of ROVER

Written by Ron Horn   10-14-91
*/

#include <stdio.h>
#include <math.h>
#include <graphics.h>
#pragma hdrstop
#include <conio.h>
#include "main.h"

void main()
{
  char buf[201];
  static char ch;
  FILE *in;
  int i, num, i1,i2,i3,i4,i5,i6,i7;
  float pi = 3.1415927;
  float scr;
  float oairwt[20],oh20wt[20],width[20],height[20],thick[20],mat[20];
  float pwidth[20],pthick[20],pmat[20],od[20],id[20],rmat[20];
  float type[400],length[400],otype[100];
  float axarea[20],pxarea[20],rxarea[20], vol[400];
  float h20wt[400], airwt[400];
  float aballast, h20ballast, totalawt, totalh20wt;
  float x1[400],y1[400],z1[400],x2[400],y2[400],z2[400];
  float ox1[100],oy1[100],oz1[100],ox2[100],oy2[100],oz2[100];
  float x1bal,y1bal,z1bal,x2bal,y2bal,z2bal;
  float totalmom[3], totalcgmom[3], totalcbmom[3];
  float totalbuoywt, totalnonbuoywt;
  float x,y,z;
  float totlen = 0;

  firstsection = 1;

  /* read input file */
  if(((in = fopen("parts.in","r")) == NULL)) {
    printf("\nCan't open 'parts.in'\n");
    exit(0);
  }

  i1 = 0; /* set pointers */
  i2 = 0;
  i3 = 0;
  i4 = 0;
  i5 = 0;
  i6 = 0;
  i7 = 0;
  while(1) {
    if(fgets(buf,200,in) == NULL) break;
    if((buf[0] != '*') & (strlen(buf) > 2)) {
      sscanf(buf,"%d",&num);
      if(num < 20) { /* structual material info */
	sscanf(buf,"%f%f%f%f%f%f",&scr,&density[i1],&emod[i1],&yield[i1],
		&compress[i1],&shear[i1]);
	i1++;
      }
      else if(num < 40) { /* other parts info */
	sscanf(buf,"%f%f%f", &scr,&oairwt[i2],&oh20wt[i2]);
	i2++;
      }
      else if(num < 60) { /* angle types */
	sscanf(buf,"%f%f%f%f%f%f%f%f%f", &scr, &width[i3],&height[i3],
	 &thick[i3],&mat[i3],&momi[i3],&secm[i3],&aradg[i3],&aarea[i3]);
	i3++;
      }
      else if(num < 80) { /* plate types */
	sscanf(buf,"%f%f%f%f", &scr, &pwidth[i4],&pthick[i4],&pmat[i4]);
	i4++;
      }
      else if(num < 100) { /* rod/tube types */
	sscanf(buf,"%f%f%f%f", &scr, &od[i5],&id[i5],&rmat[i5]);
	i5++;
      }
      else if(num < 500) { /* structure element */
	sscanf(buf,"%f%f%f%f%f%f%f%f", &scr, &type[i6],
	  &x1[i6],&y1[i6],&z1[i6],&x2[i6],&y2[i6],&z2[i6]);
	x = x2[i6] - x1[i6];
	y = y2[i6] - y1[i6];
	z = z2[i6] - z1[i6];
	length[i6] = sqrt(x*x + y*y + z*z);
/*
	/* print individual lengths */
	if(type[i6] == 86) printf("%.3lf  ", length[i6]);

	/* calculate total length of anything here */
	if(type[i6] == 86) totlen = totlen + length[i6]; /* type 40 */
*/
	i6++;
      }
      else if(num < 1000) { /* other elements with no length */
	sscanf(buf,"%f%f%f%f%f%f%f%f", &scr, &otype[i7],
	  &ox1[i7],&oy1[i7],&oz1[i7],&ox2[i7],&oy2[i7],&oz2[i7]);
	i7++;
      }
      else if(num == 1000) { /* ballast */
	sscanf(buf,"%f%f%f%f%f%f%f%f%f", &scr, &aballast, &h20ballast,
	  &x1bal,&y1bal,&z1bal,&x2bal,&y2bal,&z2bal);
      }
      else { /* bad data file */
	printf("\nBad 'parts.in' file\n");
	exit(0);
      }
    }
  }
/*
  /* print total length of something */
  printf("\nTotal length of 1.0D TI tube = %.2lf in.\n", totlen);
  getch();
*/
  /* calculate section areas */
  for(i = 0; i < i3; i++) /* angles */
    axarea[i] = (width[i] + height[i] - thick[i]) * thick[i];
  for(i = 0; i < i4; i++) /* plates */
    pxarea[i] = pwidth[i] * pthick[i];
  for(i = 0; i < i5; i++) /* rods & tubes */
    rxarea[i] = pi * (od[i]*od[i]/4 - id[i]*id[i]/4);

  /* calculate volumes & weights for structual elements */
  for(i = 0; i < i6; i++) {
    if(type[i] < 60) { /* angles */
      vol[i] = axarea[(int)type[i] - 40] * length[i];
      airwt[i] = vol[i] * density[(int)mat[(int)type[i]-40]];
      h20wt[i] = airwt[i] - vol[i] * 0.037;
    }
    else if(type[i] < 80) { /* plates */
      vol[i] = pxarea[(int)type[i] - 60] * length[i];
      airwt[i] = vol[i] * density[(int)pmat[(int)type[i]-60]];
      h20wt[i] = airwt[i] - vol[i] * 0.037;
    }
    else if(type[i] < 100) { /* rods or tubes */
      vol[i] = rxarea[(int)type[i] - 80] * length[i];
      airwt[i] = vol[i] * density[(int)rmat[(int)type[i]-80]];
      h20wt[i] = airwt[i] - vol[i] * 0.037;
    }
    else { /* data file error */
      printf("\nError in 'parts.in'\n");
      exit(0);
    }
  }

  /* calculate weights */
  totalawt = 0;
  totalh20wt = 0;
  for(i = 0; i < i6; i++) { /* structual elements */
    totalawt += airwt[i];
    totalh20wt += h20wt[i];
  }
  for(i = 0; i < i7; i++) { /* other elements */
    totalawt += oairwt[(int)otype[i] - 20];
    totalh20wt += oh20wt[(int)otype[i] - 20];
  }

  /* calculate center of mass in air & water, and center of buoyancy */
  for(i = 0; i < 3; i++) { /* zero x,y,z centers */
    totalmom[i] = 0;    /* index 0 = x, 1 = y, 2 = z */
    totalcgmom[i] = 0;
    totalcbmom[i] = 0;
  }
  totalbuoywt = 0;
  totalnonbuoywt = 0;
  for(i = 0; i < i6; i++) { /* structual elements */
    totalmom[0] += airwt[i] * (x1[i] + x2[i]) / 2;
    totalmom[1] += airwt[i] * (y1[i] + y2[i]) / 2;
    totalmom[2] += airwt[i] * (z1[i] + z2[i]) / 2;

    if(h20wt[i] > 0) { /* not buoyant */
      totalcgmom[0] += h20wt[i] * (x1[i] + x2[i]) / 2;
      totalcgmom[1] += h20wt[i] * (y1[i] + y2[i]) / 2;
      totalcgmom[2] += h20wt[i] * (z1[i] + z2[i]) / 2;
      totalnonbuoywt += h20wt[i];
    }
    else { /* buoyant elements */
      totalcbmom[0] += h20wt[i] * (x1[i] + x2[i]) / 2;
      totalcbmom[1] += h20wt[i] * (y1[i] + y2[i]) / 2;
      totalcbmom[2] += h20wt[i] * (z1[i] + z2[i]) / 2;
      totalbuoywt += h20wt[i];
    }
  }
  for(i = 0; i < i7; i++) { /* other elements */
    totalmom[0] += oairwt[(int)otype[i] - 20] * (ox1[i] + ox2[i]) / 2;
    totalmom[1] += oairwt[(int)otype[i] - 20] * (oy1[i] + oy2[i]) / 2;
    totalmom[2] += oairwt[(int)otype[i] - 20] * (oz1[i] + oz2[i]) / 2;

    if(oh20wt[(int)otype[i] - 20] > 0) { /* not buoyant */
      totalcgmom[0] += oh20wt[(int)otype[i] - 20] * (ox1[i] + ox2[i]) / 2;
      totalcgmom[1] += oh20wt[(int)otype[i] - 20] * (oy1[i] + oy2[i]) / 2;
      totalcgmom[2] += oh20wt[(int)otype[i] - 20] * (oz1[i] + oz2[i]) / 2;
      totalnonbuoywt += oh20wt[(int)otype[i] - 20];
    }
    else { /* buoyant other elements */
      totalcbmom[0] += oh20wt[(int)otype[i] - 20] * (ox1[i] + ox2[i]) / 2;
      totalcbmom[1] += oh20wt[(int)otype[i] - 20] * (oy1[i] + oy2[i]) / 2;
      totalcbmom[2] += oh20wt[(int)otype[i] - 20] * (oz1[i] + oz2[i]) / 2;
      totalbuoywt += oh20wt[(int)otype[i] - 20];
    }
  }

  /* print stuff */

  textcolor(LIGHTGRAY);
  cprintf("              ROVER WEIGHT & BALANCE CALCULATIONS\n\r\n\r");
  textcolor(LIGHTBLUE);
  cprintf("    Ref: Center of bottom of pressure pads = (0, -1.0, 0)\n\r");
  cprintf("           +X = Port     +Y = Stern     +Z = Up\n\r\n\r");
  textcolor(YELLOW);
  cprintf(" Center of buoyancy (x,y,z): %.1f, %.1f, %.1f in.",
   totalcbmom[0]/totalbuoywt, totalcbmom[1]/totalbuoywt,
   totalcbmom[2]/totalbuoywt);
  cprintf("  (buoy. force = %.1f lbs.)\n\r\n\r", totalbuoywt);
  textcolor(LIGHTRED);
  cprintf("With ballast:");
  cprintf(" (%.1f lbs. at %.1f,%.1f,%.1f)", aballast, (x1bal+x2bal)/2,
        (y1bal+y2bal)/2, (z1bal+z2bal)/2);
  printf("\n\tTotal in-air weight:\t\t\t%.1f lbs.\n",
	totalawt + aballast);
  printf("\tTotal in-water weight:\t\t\t%.1f lbs.\n",
	totalh20wt + h20ballast);

  scr = aballast * (x1bal + x2bal) / 2;
  printf("\tIn-air c.g. (x,y,z):\t\t\t%.1f,",
	 (scr+totalmom[0]) / (totalawt+aballast));
  scr = aballast * (y1bal + y2bal) / 2;
  printf(" %.1f,", (scr+totalmom[1]) / (totalawt+aballast));
  scr = aballast * (z1bal + z2bal) / 2;
  printf(" %.1f in.\n", (scr+totalmom[2]) / (totalawt+aballast));

  scr = h20ballast * (x1bal + x2bal) / 2;
  x=(scr+totalcgmom[0]+totalcbmom[0]) / (h20ballast+totalnonbuoywt+totalbuoywt);
  scr = h20ballast * (y1bal + y2bal) / 2;
  y=(scr+totalcgmom[1]+totalcbmom[1]) / (h20ballast+totalnonbuoywt+totalbuoywt);
  scr = h20ballast * (z1bal + z2bal) / 2;
  z=(scr+totalcgmom[2]+totalcbmom[2]) / (h20ballast+totalnonbuoywt+totalbuoywt);
  printf("\tOn-bottom (cg + cb) (x,y,z):\t\t%.1f, %.1f, %.1f in.\n",x,y,z);

  scr = h20ballast * (x1bal + x2bal) / 2;
  printf("\tIn-water (non-bouyant) c.g.  (x,y,z):\t%.1f,",
	 (scr+totalcgmom[0]) / (totalnonbuoywt+h20ballast));
  scr = h20ballast * (y1bal + y2bal) / 2;
  printf(" %.1f,", (scr+totalcgmom[1]) / (totalnonbuoywt+h20ballast));
  scr = h20ballast * (z1bal + z2bal) / 2;
  printf(" %.1f in.\n", (scr+totalcgmom[2]) / (totalnonbuoywt+h20ballast));

  scr = h20ballast * (x1bal + x2bal) / 2;
  x = totalcbmom[0]/totalbuoywt - (scr+totalcgmom[0])/(totalnonbuoywt+h20ballast);
  scr = h20ballast * (y1bal + y2bal) / 2;
  y = totalcbmom[1]/totalbuoywt - (scr+totalcgmom[1])/(totalnonbuoywt+h20ballast);
  scr = h20ballast * (z1bal + z2bal) / 2;
  z = totalcbmom[2]/totalbuoywt - (scr+totalcgmom[2])/(totalnonbuoywt+h20ballast);
  scr = sqrt(x*x + y*y + z*z); /* distance between c.g. & c.b. */
  printf("\tDistance between c.g. & c.b.:\t\t%.1f in.\n", scr);

  scr = acos(z/scr) * 180/3.1416;
  printf("\tAngle between c.g - c.b line & vert.:\t%.0f degrees\n\n", scr);

  textcolor(LIGHTGREEN);
  cprintf("No ballast:");
  printf("\n\tTotal in-air weight:\t\t\t%.1f lbs.\n", totalawt);
  printf("\tTotal in-water weight:\t\t\t%.1f lbs.\n", totalh20wt);

  printf("\tIn-air c.g. (x,y,z):\t\t\t%.1f, %.1f, %.1f in.\n",
   totalmom[0]/totalawt, totalmom[1]/totalawt, totalmom[2]/totalawt);

  x = (totalcgmom[0] + totalcbmom[0]) / (totalnonbuoywt + totalbuoywt);
  y = (totalcgmom[1] + totalcbmom[1]) / (totalnonbuoywt + totalbuoywt);
  z = (totalcgmom[2] + totalcbmom[2]) / (totalnonbuoywt + totalbuoywt);
  printf("\tOn-bottom (cg + cb) (x,y,z):\t\t%.1f, %.1f, %.1f in.\n",x,y,z);
  printf("\tIn-water (non-bouyant) c.g. (x,y,z):\t%.1f, %.1f, %.1f in.\n",
   totalcgmom[0]/totalnonbuoywt, totalcgmom[1]/totalnonbuoywt,
   totalcgmom[2]/totalnonbuoywt);

  x = totalcbmom[0]/totalbuoywt - totalcgmom[0]/totalnonbuoywt;
  y = totalcbmom[1]/totalbuoywt - totalcgmom[1]/totalnonbuoywt;
  z = totalcbmom[2]/totalbuoywt - totalcgmom[2]/totalnonbuoywt;
  scr = sqrt(x*x + y*y + z*z); /* distance between c.g. & c.b. */
  printf("\tDistance between c.g. & c.b.:\t\t%.1f in.\n", scr);
  scr = acos(z/scr) * 180/3.1416;
  printf("\tAngle between c.g - c.b line & vert.:\t%.0f degrees\n", scr);

  /* now do select stress, buckling, shear calcs */
  textcolor(LIGHTMAGENTA + BLINK);
  getch();
  printf("\n\n");
  while(1) {
    cprintf("Select:");
    printf("\n\tt)ensile stress\n");
    printf("\tb)ending stress\n");
    printf("\ts)hear stress\n");
    printf("\tc)olumn stress\n");
    printf("\tq)uit\n");
    ch = getch();
    clrscr();
    if(ch == 'q') break;
    if(ch=='t'||ch=='b'||ch=='s'||ch=='c') {
      section();
      if(ch == 'b') beam();
      if(ch == 'c') column();
      if(ch == 't') tensilestress();
      if(ch == 's') shearstress();
    }
  }
}
