#include "stdio.h" #include "math.h" #include "PitchBlkOut.h" // # // # Global Variables & Declarations // # int i = 0,j = 0,k = 0; int NumStages; int CONTROL; int STAGE; int VEHICLE = 1; double Latitude; double Longitude; double Altitude; double Acc; double TIME; double COT[5]; double THRUST[5]; double WEIGHT[5]; double FLOW[5]; double XSEC[5]; double CD[5]; double TIN[5]; double AZ; double PANG; double VEND; double PEND; double PLAT; double PLONG; double HEIGHT; double Velocity; double Gravity; double Pressure; double Temperature; double Celerity; double Mach; double Q; double AF; double C[3] = {0, 0, 0}; double G[3] = {0, 0, -1}; double F[3] = {0, 0, 0}; double lfLatitude,lfLongitude,lfHeight; double lfThrust[5],lfWeight[5],lfFlow[5],lfCrossSection[5],lfDrag[5],lfCutOff[5],lfStepSize[5]; double lfVertRise,lfPitchAngle,lfPitchEnd,lfLaunchAzimuth; double X[7]; // Finds the product double lgproduct( double x[], int n , double X, int i, int upperBound,int lowerBound) { double product = 1; // Initial Product Value int j = 0; for(j = lowerBound; j <= upperBound ; j++) { if (j != i) { product = (product) * ((X - x[j])/(x[i]- x[j])); // Calculate the product } } return product; }// For calculating the sum of products double lgsummation(double x[], double y[], int n, double X, int upperBound, int lowerBound) { double summation = 0; int i = lowerBound; n = upperBound; for(i = lowerBound; i <= upperBound; i++) { summation = summation + ( y[i] * lgproduct(x,n,X,i,upperBound,lowerBound)); } return summation; } double interpolate(double x[], double y[], int k, double X, int n) { int i = 0; int lowerBound = 0; int upperBound = 0; int range = 0; double fX = 0; // Finding the upper and lower bounds according to K for (i = 0; i < n; i++) { if (x[i] > X) { lowerBound = i-1; upperBound = i; break; } } range = k/2-1; upperBound = upperBound + range; lowerBound = lowerBound - range; if (lowerBound < 0) { upperBound = abs(lowerBound)+upperBound; lowerBound = 0; } else if (upperBound > n) { lowerBound = lowerBound - (abs(upperBound - n)); upperBound = n; } fX = lgsummation(x,y,n,X,upperBound,lowerBound); return fX; } double Pressure15(double alt) { double A[6]; alt = alt/1000.0; if (alt <= 25) { A[0] = -9.6667e-04; A[1] = 7.8333e-02; A[2] = -2.3792; A[3] = 3.4292e+01; A[4] = -2.5117e+02; A[5] = 1000; return (A[0]*pow(alt,5) + A[1]*pow(alt,4) + A[2]*pow(alt,3) + A[3]*pow(alt,2) + A[4]*pow(alt,1) + A[5])*1000*.010197162; } else if(alt <= 65) { A[0] = -2.0833e-06; A[1] = 5.2083e-04; A[2] = -5.1042e-02; A[3] = 2.4729; A[4] = -6.1125e+01; A[5] = 6.6500e+02; return (A[0]*pow(alt,5) + A[1]*pow(alt,4) + A[2]*pow(alt,3) + A[3]*pow(alt,2) + A[4]*pow(alt,1) + A[5])*1000*.010197162; } else { return 0; } } double Temperature15(double alt) { double A[15]; double T[15]; alt = alt/1000.0; A[0] = 0.0; A[1] = 5.0; A[2] = 10.0; A[3] = 15; A[4] = 20; A[5] = 25; A[6] = 30; A[7] = 35; A[8] = 40; A[9] = 45; A[10] = 50; A[11] = 55; A[12] = 60; A[13] = 65; A[14] = 70; T[0] = 290; T[1] = 240; T[2] = 220; T[3] = 225; T[4] = 230; T[5] = 245; T[6] = 255; T[7] = 265; T[8] = 270; T[9] = 275; T[10] = 270; T[11] = 255; T[12] = 235; T[13] = 215; T[14] = 200; if (alt <= 65) { return interpolate(A,T,4,alt,15); //Computer Interpolation } else { return 180; } } double Celerity15(double temp) { if (temp == 180) { return 1000; } return sqrt(((1.4)*((8.31432e+03)/(28.9644))*temp)); } double Gravity15(double alt) { return (3.98622e+14)/(pow(((6.378178e+06)+(alt)),2)); } void output(int j, double T, double Y[]) { int i; double tmpLong, tmpZ, tmplat, range, C[3]; Latitude = -(Y[0] /(1852 * 60)) + Latitude; Longitude = -(Y[1] /(1852 * 60)) + Longitude; if(T != 0.0) { C[0] = Y[3] / Velocity; C[1] = Y[4] / Velocity; C[2] = Y[5] / Velocity; } else { C[0] = 0.0; C[1] = 0.0; C[2] = 0.0; } range = sqrt(pow(Y[0], 2.0) + pow(Y[1], 2.0))/1852.0; BLKOUT(0, CONTROL, TIME, Y, C, F, G, PANG, VEND, PEND, AZ, -FLOW[STAGE], XSEC[STAGE], THRUST[STAGE], Gravity, Pressure, Temperature, Mach, Celerity, Q, CD[STAGE], AF, Latitude, Longitude, range, Y[5], Acc, COT[STAGE], TIN[STAGE]); } void XP_System(double T, double Y[], double DY[]) { double Fg = 0; double Ft = 0; double Fa = 0; double mass = 0; int i; Altitude = Y[2]; Gravity = Gravity15(Y[2]); Pressure = Pressure15(Y[2]); Temperature = Temperature15(Y[2]); Celerity = Celerity15(Temperature); Velocity = sqrt(Y[3]*Y[3] + Y[4]*Y[4] + Y[5]*Y[5]); Mach = Velocity / Celerity; Q = 0.7 * Pressure * (Mach * Mach); Ft = THRUST[STAGE]; mass = WEIGHT[STAGE] - FLOW[STAGE] * T; Fg = (mass) * Gravity; Fa = CD[STAGE] * Q * XSEC[STAGE]; Acc = (Ft - Fa) / (9.80665 * mass); AF = Fa; if(T != 0.0) { C[0] = Y[3] / Velocity; C[1] = Y[4] / Velocity; C[2] = Y[5] / Velocity; } else { C[0] = 0.0; C[1] = 0.0; C[2] = 0.0; } PITCH(T,AZ,PANG,VEND,PEND,F,C); DY[0] = Y[3]; DY[1] = Y[4]; DY[2] = Y[5]; DY[3] = (Ft * F[0] + Fg * G[0] - Fa * C[0]) / (mass); DY[4] = (Ft * F[1] + Fg * G[1] - Fa * C[1]) / (mass); DY[5] = (Ft * F[2] + Fg * G[2] - Fa * C[2]) / (mass); DY[6] = -FLOW[STAGE]; } void RK4_System1(int n, double h, double T, double X[], int nsteps) { double Y[n]; double K[4][n]; output(0,T,X); for (j = 0; j < nsteps; j++) { XP_System(T, X, K[0]); for (i=0; i