COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
convert_test.cpp File Reference
#include "mathlib.h"
#include "convertlib.h"
#include <stdio.h>
#include <string.h>
Include dependency graph for convert_test.cpp:

Macros

#define MAX_NAME   15
 

Functions

int openFileLine (FILE **fp, const char filename[], int startLine)
 
void skipLines (FILE *fp, int lines)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

#define MAX_NAME   15

Function Documentation

int openFileLine ( FILE **  fp,
const char  filename[],
int  startLine 
)
213  {
214  char filePath[40] = {"convert_test_data/"};//the folder containing the test files
215  *fp = fopen(strcat(filePath, filename), "r"); //open the file
216  if (fp == NULL) { //print error message if necessary.
217  printf("\n\tError opening necessary data file!!!\n");
218  printf("Please check that the text file '%s' exists and\n", filename);
219  printf("is in the directory titled 'convert_test_data'.\n\n");
220  return(0); //return 0 for failure....
221  }
222  skipLines(*fp, startLine); //move the pointer to startLine
223  return(1); //return 1 for sucess!!!!
224 }
FILE * fp
Definition: rw_test.cpp:38
void skipLines(FILE *fp, int lines)
Definition: convert_test.cpp:226
void skipLines ( FILE *  fp,
int  lines 
)
226  {
227  char c;
228  for (lines; lines>0; lines--) { //skip 'lines' many lines
229  c=0;
230  do { //scan through file untill the end of a line is reached
231  c = fgetc(fp);
232  if (c==EOF) return; //We've gone too far!!!
233  } while(c!='\n');
234  }
235 }
FILE * fp
Definition: rw_test.cpp:38
int main ( int  argc,
char *  argv[] 
)
43 {
44 /*remember to adjust the number of loops in line 18 to match the number of names here*/
45 char names[][MAX_NAME] = {"rearth","kep2eci","eci2kep"}; //the name of every function being tested, must be IN ORDER!
46 int i, jumpto = -1, skip = -1; //normally we don't jump to or skip anything, so those are initialized out of range.
47 double error, adjerror,avgerror, minerror, maxerror, terror, tminerror, tmaxerror;
48 error = adjerror = avgerror = minerror = maxerror = terror = tminerror = tmaxerror = 0.;
49 i=0;
50 while (argc>1&&i<3) { //If arguments were entered: loop through the names array,...
51  if (strcmp(argv[1], names[i]) == 0) { //and determine if we're to supposed to jump to a specific function,...
52  jumpto = i;
53  printf("\nFunctionality Test:\t%s\n\n", names[i]);
54  break;
55  } else if ((*argv[1] == '-')&&(strcmp(&argv[1][1], names[i]) == 0)) {//or skip a specific function.
56  skip = i;
57  jumpto = 0;
58  printf("\nFunctionality Tests:\tconvertlib (without %s)\n\n", names[i]);
59  break;
60  }
61  i++;
62 }
63 /*Tests can test a single function, or a small group of functions, but
64 must provide error values individually for every function within the test.
65 Each test is contained within a switch case, so they don't interfere with
66 eachother, and can be excluded or run alone.*/
67 switch (jumpto) {
68 case -1: //Default first line if no arguments were entered
69  printf("\nFunctionality Tests:\tconvertlib\n\n");
70 case 0:
71  if (skip!=0){ //if skip is the same as this case value, skip this function
72  printf("Function: rearth()\n");
73  FILE *LATvRdata;
74  if (openFileLine(&LATvRdata, "LATvR.txt", 0)!=0) {
75  i=0;
76  int z = 30; //display one test out of every ___
77  int numTests; //the number of tests (written at top of file)
78  fscanf(LATvRdata, "%d", &numTests);
79  skipLines(LATvRdata, 2);
80  double lat, rad, maxErrorL, minErrorL, maxErrorR, minErrorR;
81  error = adjerror = avgerror = 0.0;
82  printf("Latitude: \tRadius(function):\tRadius(correct):\t Delta:\n");
83  printf("(deg)\t(rad) \t(m)\t\t\t(m)\t\t\t (m)\n");//this is very nicely formatted, please don't break it.
84  while (i<numTests&&!feof(LATvRdata)) {
85  fscanf(LATvRdata, "%lf%lf", &lat, &rad);
86  error = rearth(lat) - rad;
87  if (i==0) {
88  minerror = maxerror = error;
89  maxErrorL = minErrorL = lat;
90  maxErrorR = minErrorR = rad;
91  } else if (fabs(error)>fabs(maxerror)) {
92  maxerror = error;
93  maxErrorL = lat;
94  maxErrorR = rad;
95  } else if (fabs(error)<fabs(minerror)) {
96  minerror = error;
97  minErrorL = lat;
98  minErrorR = rad;
99  }
100  avgerror += fabs(error);
101  adjerror = avgerror/rad;
102  if (i%z==0||i==0) {
103  printf("%0.0f \t%0.15f\t%0.9f (-) %0.9f (=) %11.5g\n", DEGOF(lat), lat, rearth(lat), rad, error);
104  }
105  i++;
106  }
107  fclose(LATvRdata);
108  printf("%d tests total. One out of every %d shown.\n",i,z);
109  printf("\n");
110  adjerror = adjerror/i;
111  avgerror = avgerror/i;
112  printf("Max Error:%0.0f \t%0.15f\t%0.9f (-) %0.9f (=) %11.5g\n", DEGOF(maxErrorL), maxErrorL, rearth(maxErrorL), maxErrorR, maxerror);
113  printf("Min Error:%0.0f \t%0.15f\t%0.9f (-) %0.9f (=) %11.5g\n", DEGOF(minErrorL), minErrorL, rearth(minErrorL), minErrorR, minerror);
114  printf("Average Error:%11.5g\tAdjusted Error:%11.5g\n", avgerror, adjerror);
115  printf("\n");
116  terror += adjerror;
117  printf("rearth() Error: %11.5g\tTerror:%11.5g\n",adjerror, terror);
118  printf("\n");
119  }
120  if (jumpto!=-1) break; //this function was jumped to, no other functions need to be tested.
121  }
122 case 1: //this is the first of a group of functions tested together, drop through cases to the actuall test.
123  if (skip!=1) {
124  printf("Function: kep2eci()\n");
125  if (jumpto!=-1) break;
126  }
127 case 2:
128  if (skip!=2) {
129  printf("Function: eci2kep()\n");
130  FILE *KeplerData;
131  FILE *ECIData;
132  if ((openFileLine(&KeplerData, "kepler_data.txt", 0)!=0)&&(openFileLine(&ECIData, "eci_data.txt", 2)!=0)) {
133  int numTests;
134  fscanf(KeplerData, "%d", &numTests);
135  skipLines(KeplerData, 2);
136  kepstruc kepcorrect, keptest, Kmaxerror, Kminerror;
137  cartpos eciinput, Emaxerror, Eminerror;
138  int z = 100; //display one test out of every ___
139  i=0;
140  printf("ECI vector input |#|Kepler element output\n");
141  while (i<numTests&&(!feof(KeplerData)||!feof(ECIData))) {
142  fscanf(KeplerData, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &kepcorrect.beta, &kepcorrect.period, &kepcorrect.alat, &kepcorrect.ap, &kepcorrect.ea, &kepcorrect.e, &kepcorrect.h.col[0], &kepcorrect.fa, &kepcorrect.i, &kepcorrect.ma, &kepcorrect.mm, &kepcorrect.raan, &kepcorrect.a, &kepcorrect.ta);
143  kepcorrect.fa -= RADOF(90.);
144  fscanf(ECIData, "%lf%lf%lf%lf%lf%lf%lf%lf%lf", &eciinput.s.col[0], &eciinput.s.col[1], &eciinput.s.col[2], &eciinput.v.col[0], &eciinput.v.col[1], &eciinput.v.col[2], &eciinput.a.col[0], &eciinput.a.col[1], &eciinput.a.col[2]);
145  eciinput.utc = 55927;
146  eci2kep(&eciinput, &keptest);
147  error = fabs(keptest.beta - kepcorrect.beta)+fabs(keptest.period - kepcorrect.period)+fabs(keptest.alat - kepcorrect.alat)+fabs(keptest.ap - kepcorrect.ap)+fabs(keptest.ea - kepcorrect.ea)+fabs(keptest.e - kepcorrect.e)/*+fabs(keptest.h.col[0] - kepcorrect.h.col[0])*/+fabs(keptest.fa - kepcorrect.fa)+fabs(keptest.i - kepcorrect.i)+fabs(keptest.ma - kepcorrect.ma)+fabs(keptest.mm - kepcorrect.mm)+fabs(keptest.raan - kepcorrect.raan)+fabs(keptest.a - kepcorrect.a)+fabs(keptest.ta - kepcorrect.ta);
148  if (i==0) {
149  minerror = maxerror = error;
150  Kmaxerror = Kminerror = kepcorrect;
151  Emaxerror = Eminerror = eciinput;
152  } else if (fabs(error)>fabs(maxerror)) {
153  maxerror = error;
154  Kmaxerror = kepcorrect;
155  Emaxerror = eciinput;
156  } else if (fabs(error)<fabs(minerror)) {
157  minerror = error;
158  Kminerror = kepcorrect;
159  Eminerror = eciinput;
160  }
161  avgerror += fabs(error);
162  if (i%z==0||i==0) {
163  printf("%d______________________________________________________________________________________________________________\n",i);
164  printf("pos x:%11.5g|#| Beta Angle |Period |alat |arg per |E anom |e |flight ang\n",eciinput.s.col[0]);
165  printf(" y:%11.5g|#|function:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",eciinput.s.col[1],keptest.beta,keptest.period,keptest.alat,keptest.ap,keptest.ea,keptest.e,keptest.fa);
166  printf(" z:%11.5g|#| correct:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",eciinput.s.col[2],kepcorrect.beta,kepcorrect.period,kepcorrect.alat,kepcorrect.ap,kepcorrect.ea,kepcorrect.e,kepcorrect.fa);
167  printf("vel x:%11.5g|#| delta:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",eciinput.v.col[0],(keptest.beta-kepcorrect.beta),(keptest.period-kepcorrect.period),(keptest.alat-kepcorrect.alat),(keptest.ap-kepcorrect.ap),(keptest.ea-kepcorrect.ea),(keptest.e-kepcorrect.e),(keptest.fa-kepcorrect.fa));
168  printf(" y:%11.5g|#| i |mean anom |mean motion|raan |a |true anomaly \n",eciinput.v.col[1]);
169  printf(" z:%11.5g|#|function:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",eciinput.v.col[2],keptest.i,keptest.ma,keptest.mm,keptest.raan,keptest.a,keptest.ta);
170  printf("acc x:%11.5g|#| correct:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",eciinput.a.col[0],kepcorrect.i,kepcorrect.ma,kepcorrect.mm,kepcorrect.raan,kepcorrect.a,kepcorrect.ta);
171  printf(" y:%11.5g|#| delta:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",eciinput.a.col[1],(keptest.i-kepcorrect.i),(keptest.ma-kepcorrect.ma),(keptest.mm-kepcorrect.mm),(keptest.raan-kepcorrect.raan),(keptest.a-kepcorrect.a),(keptest.ta-kepcorrect.ta));
172  printf(" z:%11.5g|#|\n",eciinput.a.col[2]);
173  }
174  i++;
175  }
176  printf("________________________________________________________________________________________________________________\n");
177  fclose(KeplerData);
178  fclose(ECIData);
179  printf("\n%d tests total, one out of every %d shown.\n",i,z);
180  avgerror = avgerror/i;
181  printf("\nMaximum Error:__________________________________________________________________________________________\n");
182  eci2kep(&Emaxerror, &keptest);
183  printf("pos x:%11.5g|#| Beta Angle |Period |alat |arg per |E anom |e |flight ang\n",Emaxerror.s.col[0]);
184  printf(" y:%11.5g|#|function:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",Emaxerror.s.col[1],keptest.beta,keptest.period,keptest.alat,keptest.ap,keptest.ea,keptest.e,keptest.fa);
185  printf(" z:%11.5g|#| correct:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",Emaxerror.s.col[2],Kmaxerror.beta,Kmaxerror.period,Kmaxerror.alat,Kmaxerror.ap,Kmaxerror.ea,Kmaxerror.e,Kmaxerror.fa);
186  printf("vel x:%11.5g|#| delta:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",Emaxerror.v.col[0],(keptest.beta-Kmaxerror.beta),(keptest.period-Kmaxerror.period),(keptest.alat-Kmaxerror.alat),(keptest.ap-Kmaxerror.ap),(keptest.ea-Kmaxerror.ea),(keptest.e-Kmaxerror.e),(keptest.fa-Kmaxerror.fa));
187  printf(" y:%11.5g|#| i |mean anom |mean motion|raan |a |true anomaly \n",Emaxerror.v.col[1]);
188  printf(" z:%11.5g|#|function:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",Emaxerror.v.col[2],keptest.i,keptest.ma,keptest.mm,keptest.raan,keptest.a,keptest.ta);
189  printf("acc x:%11.5g|#| correct:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",Emaxerror.a.col[0],Kmaxerror.i,Kmaxerror.ma,Kmaxerror.mm,Kmaxerror.raan,Kmaxerror.a,Kmaxerror.ta);
190  printf(" y:%11.5g|#| delta:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",Emaxerror.a.col[1],(keptest.i-Kmaxerror.i),(keptest.ma-Kmaxerror.ma),(keptest.mm-Kmaxerror.mm),(keptest.raan-Kmaxerror.raan),(keptest.a-Kmaxerror.a),(keptest.ta-Kmaxerror.ta));
191  printf(" z:%11.5g|#|\n",Emaxerror.a.col[2]);
192  printf("\nMinimum Error:__________________________________________________________________________________________\n");
193  eci2kep(&Eminerror, &keptest);
194  printf("pos x:%11.5g|#| Beta Angle |Period |alat |arg per |E anom |e |flight ang\n",Eminerror.s.col[0]);
195  printf(" y:%11.5g|#|function:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",Eminerror.s.col[1],keptest.beta,keptest.period,keptest.alat,keptest.ap,keptest.ea,keptest.e,keptest.fa);
196  printf(" z:%11.5g|#| correct:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",Eminerror.s.col[2],Kminerror.beta,Kminerror.period,Kminerror.alat,Kminerror.ap,Kminerror.ea,Kminerror.e,Kminerror.fa);
197  printf("vel x:%11.5g|#| delta:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g\n",Eminerror.v.col[0],(keptest.beta-Kminerror.beta),(keptest.period-Kminerror.period),(keptest.alat-Kminerror.alat),(keptest.ap-Kminerror.ap),(keptest.ea-Kminerror.ea),(keptest.e-Kminerror.e),(keptest.fa-Kminerror.fa));
198  printf(" y:%11.5g|#| i |mean anom |mean motion|raan |a |true anomaly \n",Eminerror.v.col[1]);
199  printf(" z:%11.5g|#|function:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",Eminerror.v.col[2],keptest.i,keptest.ma,keptest.mm,keptest.raan,keptest.a,keptest.ta);
200  printf("acc x:%11.5g|#| correct:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",Eminerror.a.col[0],Kminerror.i,Kminerror.ma,Kminerror.mm,Kminerror.raan,Kminerror.a,Kminerror.ta);
201  printf(" y:%11.5g|#| delta:%11.5g|%11.5g|%11.5g|%11.5g|%11.5g|%11.5g \n",Eminerror.a.col[1],(keptest.i-Kminerror.i),(keptest.ma-Kminerror.ma),(keptest.mm-Kminerror.mm),(keptest.raan-Kminerror.raan),(keptest.a-Kminerror.a),(keptest.ta-Kminerror.ta));
202  printf(" z:%11.5g|#|\n",Eminerror.a.col[2]);
203  }
204  if (jumpto!=-1) break;
205  }
206 default:
207  printf("\nconvertlib total error");
208  if (skip!=-1) printf(" (without %s)",names[skip]); //indicate whether any function was skipped.
209  printf(":\t%11.5g\n",terror); //final error stats for entire library.
210 }
211 }
int openFileLine(FILE **fp, const char filename[], int startLine)
Definition: convert_test.cpp:213
double ta
True Anomoly.
Definition: convertdef.h:557
int32_t eci2kep(cartpos &eci, kepstruc &kep)
Definition: convertlib.cpp:2934
double mm
Mean Motion.
Definition: convertdef.h:561
rvector a
Acceleration.
Definition: convertdef.h:167
double utc
UTC of Position.
Definition: convertdef.h:161
int i
Definition: rw_test.cpp:37
double e
Eccentricity.
Definition: convertdef.h:540
Cartesian full position structure.
Definition: convertdef.h:158
double ma
Mean Anomoly.
Definition: convertdef.h:555
rvector h
Angular Momentum vector.
Definition: convertdef.h:542
#define MAX_NAME
Definition: convert_test.cpp:36
rvector s
Location.
Definition: convertdef.h:163
double period
Orbital Period in seconds.
Definition: convertdef.h:536
double fa
Definition: convertdef.h:562
#define DEGOF(rad)
Degrees of a Radian value.
Definition: math/constants.h:33
double raan
Right Ascension of the Ascending Node in radians.
Definition: convertdef.h:549
Classical elements structure.
Definition: convertdef.h:529
double ap
Argument of Perigee.
Definition: convertdef.h:551
double col[3]
Definition: vector.h:55
double i
Orbital Inclination in radians.
Definition: convertdef.h:547
double beta
Solar Beta Angle in radians.
Definition: convertdef.h:544
double a
Semi-Major Axis in meters.
Definition: convertdef.h:538
void skipLines(FILE *fp, int lines)
Definition: convert_test.cpp:226
double ea
Eccentric Anomoly.
Definition: convertdef.h:559
double rearth(double lat)
Definition: convertlib.cpp:1556
rvector v
Velocity.
Definition: convertdef.h:165
double alat
Argument of Latitude.
Definition: convertdef.h:553
#define RADOF(deg)
Radians of a Degree value.
Definition: math/constants.h:29