All pastes #50117 Raw Edit

Unnamed

public text v1 · immutable
#50117 ·published 2006-04-20 10:32 UTC
rendered paste body
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "PitchBlkOut.h"
#define RK_order 4
#define N_Max 100
static const int Neq = 2;


void XP_System(int Neq, double T, double Y[], double DY[]);
void RK4_System(int n, double h, double T, double x[], int nsteps);

double interpolate(int numberofpairs, double x[], double y[], double bigx, int k);
double product(double x[], double bigx, int i, int upperbound,int lowerbound) ;
double summation(double x[], double y[], int numberofpairs, double bigx, int upperbound, int lowerbound);

double pressure23(double altitude);
double temperature23(double altitude);
double gravity23(double alt);
double celerity23(double temperature);

void onerun();

void output(int j, double T, double Y[]);

 int anykey;

int numstages;
int control;
int stage=1;

//A  compute in atmospheric routine
double pressure;
double temperature;

//F compute in the function evaluation part of the integration
double velocity;
double c[3]; //unit vector for velocity

//
double celerity;
double mach;
double q;
double af;

//G compute in gravity routine
double gravity;
double g[3];

//O compute in output routine
double lattitude;
double longitude;
double altitude;
double acceleration;
double range;

//P  compute in pitch program routine
double az;
double pang;
double vend;  //vertical rise time
double pend;
//
double f[3];


//s  adjust values at staging
double COT[2];
double thrust[2];
double weight[2];
double flow[2];
double crosssection[2];
double CD[2];
double TI[2];

//I input quantities
double plat;
double plong;
double height;

//three coefficients for unit vectors
double ft;
double fg;
double fa;


void onerun()
{
	int nsteps;
	int i;
	double y[7];
	
	g[0] = 0.0;
	g[1] = 0.0;
	g[2] = -1.0;
	
	y[0] =0.0;
	y[1] =0.0;
	y[2] = height;
	y[3] =0.0;
	y[4] =0.0;
	y[5] =0.0;				
	y[6] = weight[0];				
	
	for (i = 0; i < numstages; i++)		
	{
		stage =i;
		nsteps = (int) COT[i] / TI[i];	
		y[6] = weight[i];			
		RK4_System(7, TI[i], 0, y, nsteps);		
	}	
}

void XP_System(int Neq, double T, double Y[], double DY[])
{

	int i;

	temperature = temperature23(Y[2]);	
	celerity = celerity23(temperature);					
	velocity = sqrt(pow(Y[3], 2.0) + pow(Y[4], 2.0) + pow(Y[5], 2.0));		
	mach =  velocity / celerity;
	pressure = pressure23(Y[2]);
	q = 0.7 * pressure * pow(mach, 2.0);
	
	if (velocity > 0)
	{
	c[0]=Y[3]/velocity;
	c[1]=Y[4]/velocity;
	c[2]=Y[5]/velocity;
	}


	fa = CD[stage] * q * crosssection[stage];	

		
		
	gravity = gravity23(Y[2]);	
	
	printf("Pressure = %lf, Temperature = %lf, Celerity = %lf, Gravity = %lf\n", pressure, temperature, celerity, g[2]);
	
	
	PITCH(T, az, pang, vend, pend, f, c);

	DY[0] = c[0];								
	DY[1] = c[1];								
	DY[2] = c[2];
	DY[3] = ((thrust[stage] * f[0]) - (fa * c[0])) / Y[6];
	DY[4] = ((thrust[stage] * f[1]) - (fa * c[1])) / Y[6];	
	DY[5] = ((thrust[stage] * f[2]) - g[2] - (fa * c[2])) / Y[6];
	DY[6] = -(flow[stage]);
	
	acceleration = sqrt(pow(DY[3], 2.0) + pow(DY[4], 2.0) + pow(DY[5], 2.0));	

	
}

void RK4_System(int n, double h, double T, double x[], int nsteps)
{
	double Y[n];							
	double K[4][n];
	int i = 0; 
	int j = 0;

	output(0, T, x);
	

	for(j = 1; j <= nsteps; j++)
	{
	scanf("%d",&anykey);	
		
		XP_System(n, T, x, K[0]);
		for(i = 0; i < n; i++)
		{
			Y[i] = x[i] + 0.5 * h * K[0][i];
		}


		XP_System(n,T + 0.5 * h, Y, K[1]);
		for(i = 0; i < n; i++)
		{
			Y[i] = x[i] + 0.5 * h * K[1][i];
		}


		XP_System(n, T + 0.5 * h, Y, K[2]);
		for(i = 0; i < n; i++)
		{
			Y[i] = x[i] + h * K[2][i];
		}

		XP_System(n, T + h, Y, K[3]);
		for(i = 0; i < n; i++)
		{
			x[i] = x[i] + (1.0/6.0) * h * (K[0][i] + 2 * K[1][i] + 2 * K[2][i] + K[3][i]); 
		}
		T+=h;
		output(j, T, x);
	}
}

double product(double x[], double bigx, int i, int upperbound,int lowerbound) 
{ 
     
	double result = 1;  
	int j = 0;
     
	for(j=lowerbound; j<=upperbound; j++) 
     	{ 
		if (j!=i)
		{ 
			result = (result) * ((bigx - x[j]) / (x[i]- x[j]));  
		} 
	} 
	return result; 
}




double summation(double x[], double y[], int numberofpairs, double bigx, int upperbound, int lowerbound)
{

	double sum = 0;
	int i=lowerbound;
	numberofpairs=upperbound;
	
	/*
	printf("inside summation\n");
	printf("bigx is %lf\n", bigx);
	printf("upperbound is is %d lowerbound is %d\n", upperbound, lowerbound);
	printf("npair is %d\n", numberofpairs);	*/
	

	for(i=lowerbound; i<=upperbound; i++) 
	{ 
		sum+=(y[i] * product(x,bigx,i,upperbound,lowerbound)); 
	} 
	return sum; 
}





double interpolate(int numberofpairs, double x[], double y[], double bigx, int k)
{
	int i=0,j=0,width=0;
	int upperbound=0,lowerbound = 0;
	double fx=0;
	
/*
	printf("bigx is %lf\n", bigx);
	printf("k is %d\n", k);
	printf("npair is %d\n", numberofpairs);
*/	



	for (i=0;i<numberofpairs;i++) 
	{
		if (x[i]>bigx)
		{
			lowerbound=i-1;
			upperbound=i;
			break;
		}
	}
	width = (k/2) - 1;
	upperbound+=width;
	lowerbound-=width;

	if (lowerbound < 0) 
	{
		upperbound = (abs(lowerbound)+upperbound);
		lowerbound = 0; 	
	}
	else 
	{

		if(upperbound > numberofpairs)  
		{
			lowerbound=lowerbound-(abs(upperbound-numberofpairs));
			upperbound=numberofpairs;
		}
	}

	fx = summation(x,y,numberofpairs,bigx,upperbound,lowerbound);
	//printf("fx is %lf\n", fx);
	return fx;
}




double temperature23(double altitude)
{

	double X[20];
	double Y[20];
	double temp = 0;


	X[0]=0.0, Y[0]=290.0;      
	X[1]=5000.0, Y[1]=240.0;      
	X[2]=10000.0, Y[2]=220.0;      
	X[3]=15000.0, Y[3]=225.0;      
	X[4]=20000.0, Y[4]=230.0;      
	X[5]=25000.0, Y[5]=245.0;      
	X[6]=30000.0, Y[6]=255.0;      
	X[7]=35000.0, Y[7]=265.0;      
	X[8]=40000.0, Y[8]=270.0;      
	X[9]=45000.0, Y[9]=275.0;      
	X[10]=50000.0, Y[10]=270.0;    
	X[11]=55000.0, Y[11]=255.0;  
	X[12]=60000.0, Y[12]=235.0; 
	X[13]=65000.0, Y[13]=215.0;      
	X[14]=70000.0, Y[14]=200.0;

	
	temp= interpolate(15,X,Y,altitude,4);

	return temp;


}



double pressure23(double altitude)
{
	double pressure =0.0;
	
	double X[6];
	//X = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
	double results[6];	
	
	int i =0;
	double tempsum=0;

	if (altitude > 65)
	{
		pressure =0;
	}
	else	
	{
		if (altitude > 25)
		{  //we got these values by running gauss....

			X[0] = -0.000002;
			X[1] = 0.000521;
			X[2] = -0.051042;
			X[3] = 2.472917;
			X[4] = -61.125000;
			X[5] = 665.000000;

			
			
			
		}
		else
		{
	
			X[0] = -0.000967;
			X[1] = 0.078333;
			X[2] = -2.379167;
			X[3] = 34.291670;
			X[4] = -251.166700;
			X[5] = 1000.000000;



		}
		
		
		results[0] = (X[0] * (pow ( altitude, 5)) );
		results[1] = (X[1] * (pow ( altitude, 4)) );
		results[2] = (X[2] * (pow ( altitude, 3)) );
		results[3] = (X[3] * (pow ( altitude, 2)) );
		results[4] = (X[4] * (pow ( altitude, 1)) );
		results[5] = (X[5]);
		
		/*
		printf("%lf * %lf ^ 5  = %lf\n", X[0], altitude, results[0]);
		printf("%lf * %lf ^ 4  = %lf\n", X[1], altitude, results[1]);
		printf("%lf * %lf ^ 3  = %lf\n", X[2], altitude, results[2]);
		printf("%lf * %lf ^ 2  = %lf\n", X[3], altitude, results[3]);
		printf("%lf * %lf ^ 1  = %lf\n", X[4], altitude, results[4]);
		printf("%lf * %lf ^ 0  = %lf\n", X[5], altitude, results[5]);
		*/

		pressure = results[0] + results[1] + results[2] + results[3] + results[4] + results[5];   



		/*
		pressure =(X[0] * (pow ( altitude, 5)) ) 
			 + (X[1] * (pow ( altitude, 4)) )
			 + (X[2] * (pow ( altitude, 3)) )
			 + (X[3] * (pow ( altitude, 2)) )
			 + (X[4] * (pow ( altitude, 1)) )
			 + (X[5]);
		*/

	}
	
	return pressure * 1000 * .010197162;
}

//scanf("%lf",&altval);


void output(int j, double T, double Y[])
{
	int i;
	double chglongt, chglatt;

	chglatt = (lattitude + Y[0]) / (1852.0 * 60.0);		
	chglongt = (longitude + Y[1]) / (1852.0 * 60.0);

	lattitude = lattitude + chglatt;
	longitude = longitude + chglongt;


	BLKOUT(1, control, T, Y, c, f, g, pang, vend, pend, az, flow[stage], crosssection[stage], thrust[stage], gravity, pressure, 
		temperature, mach, celerity, q, CD[stage], fa, lattitude, longitude,
		sqrt(pow(Y[0], 2.0) + pow(Y[1], 2.0)), velocity, acceleration, COT[stage], TI[stage]);

}


double gravity23(double alt)
{
	return (3.98622e+14) / (pow(((6.378178e+06) + (alt)),2));
}

double celerity23(double temperature)
{
	return sqrt(((1.4)*((8.31432e+03) / (28.9644)) * temperature));
}


int main()
{
int i;

	//gather input section==================================================================


	printf("enter the number of stages\n");
	scanf("%d",&numstages);
	printf("enter control variable\n");
	scanf("%d",&control);
	printf("enter lattitude\n");
	scanf("%lf",&lattitude);
	printf("enter longitude\n");
	scanf("%lf",&longitude);
	printf("enter height\n");
	scanf("%lf",&height);
	
	//loop twice	
	for(i=0;i<2;i++)
	{
		printf("enter thrust\n");
		scanf("%lf",&thrust[i]);
		thrust[i] = thrust[i] * 0.4535924;
		
		printf("enter weight\n");
		scanf("%lf",&weight[i]);
		weight[i] = weight[i] * .04625455;

		printf("enter flow rate\n");
		scanf("%lf",&flow[i]);
		flow[i] = flow[i] * .04625455;

		printf("enter cross sectional area\n");
		scanf("%lf",&crosssection[i]);
		crosssection[i] = crosssection[i] * (0.3048 * 0.3048);

		printf("enter drag coefficient\n");
		scanf("%lf",&CD[i]);
		printf("enter cut off time\n");
		scanf("%lf",&COT[i]);
		printf("enter integration step size\n");
		scanf("%lf",&TI[i]);
	
	}
	
	printf("enter vertical rise time\n");
	scanf("%lf",&vend);
	
	printf("enter pitch angle\n");
	scanf("%lf",&pang);
	pang = (pang * 1.745329e-2);
	
	printf("enter time of end of pitch program\n");
	scanf("%lf",&pend);
	
	printf("enter launch azimuth (in degrees from north)\n");
	scanf("%lf",&az);
	az = (az * 1.745329e-2);



//ECHO input section==================================================================
	printf("\n echo\n");
	printf("\nenter the number of stages\t");
	printf("%d",numstages);
	printf("\nenter control variable\t");
	printf("%d",control);
	printf("\nenter lattitude\t");
	printf("%lf",lattitude);
	printf("\nenter longitude\t");
	printf("%lf",longitude);
	printf("\nenter height\t");
	printf("%lf",height);

	//loop twice	
	for(i=0;i<2;i++)
	{
	
		printf("\nenter thrust\t");
		printf("%lf",thrust[i]);
		printf("\nenter weight\t");
		printf("%lf",weight[i]);
		printf("\nenter flow rate\t");
		printf("%lf",flow[i]);
		printf("\nenter cross sectional area\t");
		printf("%lf",crosssection[i]);
		printf("\nenter drag coefficient\t");
		printf("%lf",CD[i]);
		printf("\nenter cut off time\t");
		printf("%lf",COT[i]);
		printf("\nenter integration step size \t");
		printf("%lf",TI[i]);
	}

	printf("\nenter vertical rise time \t");
	printf("%lf",vend);
	printf("\nenter pitch angle \t");
	printf("%lf",pang);
	printf("\nenter time of end of pitch program \t");
	printf("%lf",pend);
	printf("\nenter launch azimuth (in degrees from north) \t");
	printf("%lf",az);
//=========================================================================




onerun();

return 0;
}