#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

double c1,c2,damp,fi,fn[10000];
double rho,t,tdr,x,y;

double f1,flast;

long i,j,last;

long idamp;

FILE *pFile[4];
char filename[4][FILENAME_MAX];

void main()
{ 
	   printf("\n\n sinevrs.cpp   version 1.2  August 12, 2008 \n");
	   printf("\n\n by Tom Irvine ");
	   printf("\n Email: tomirvine@aol.com \n\n");


       printf( "\n This program calculates the vibration response spectrum ");
       printf( "\n for a pure sine base input. \n\n");

	   printf("\n Enter excitation frequency (Hz)\n");
	   scanf("%lf",&fi);


	   printf("\n Enter input amplitude (G)\n");
 	   scanf("%lf",&y);


       printf("\n Enter damping format. \n 1= damping ratio   2= Q \n");
	   scanf("%ld",&idamp);

	   if(idamp == 1 )
	   {
	      printf("\n Enter damping ratio (typically 0.05) ");
	      scanf("%lf",&damp);
	   }
	   else
	   {
	      printf("\n Enter amplification factor Q (typically 10) ");
	      scanf("%lf",&damp);
	      damp = 1./(2.*damp);
	   }

	   if(damp >= 1.0)
	   {
		  printf("\n Error:  damping value must be < 1 \n");
		  exit(1);
	   }

	   printf("\n Enter starting analysis frequency (Hz) \n");
	   scanf("%lf",&f1);

	   printf("\n Enter final analysis frequency (Hz) \n");
	   scanf("%lf",&flast);

	   printf( "\n\n Enter the output filename for peak G response: \n");
	   scanf("%s",filename[1]);
	   pFile[1]=fopen(filename[1], "w");


	   printf( "\n\n Enter the output filename for GRMS response: \n");
	   scanf("%s",filename[2]);
	   pFile[2]=fopen(filename[2], "w");



       fn[0]=f1;

       for(i=1;i<1000; i++)
       {
          fn[i]=fn[i-1]*pow(2.,(1./24.));
          if( fn[i] > flast){break;}
       }
       last = i;


       for(j=0; j <last; j++) 
       {
		    rho = fi/fn[j];
			tdr=2.*damp*rho;
            
			c1=0.;
			c2=0.;

            c1= pow( tdr, 2. );
			c2= pow( ( 1.- pow(rho,2.)), 2.);

			t= sqrt( (1.+ c1 ) / ( c2 + c1 )  );
            x=t*y;

			fprintf(pFile[1],"%14.4e \t %14.4e\n",fn[j],x);

			fprintf(pFile[2],"%14.4e \t %14.4e\n",fn[j],x*0.707);
	  }

      printf("\n\n The output files are: \n");
	  
	  printf("\n %s:  natural frequency (Hz)   accel (G peak) \n",filename[1]);
	  printf("\n %s:  natural frequency (Hz)   accel (GRMS)   \n",filename[2]);


	  printf("\n\n Calculation complete. ");
	  printf("\n Please call the output file into your own plotting program.");
	  printf("\n\n Press any key to exit.");
	  getch();
}
