Пример #1
0
int main(int argc, char * argv[]) {
	double t = 1;
	double *results;
	double couple[3] = {-1,-1,-1};
	for (t = 4; t > 0; t-= 0.1) {
		results = run_model(t, 0, couple, 1000, 5, 5, 10, 2, 1);
		printf("%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", t, results[0],  results[2], results[3], results[4], results[5]);
	}
	return(0);
}
Пример #2
0
int main(int argc, char * argv[]) {
	int i;
	double *results;
	double coupling[3] = {-1,-1,-1};
	
//	printf("one/number of Blocks vs error\n");

	for (i = 1; i < 100; i += 1) {
		results = run_model(1, 0.0, coupling, i, 10, 10, 10, 1, 2, 1);
		printf("%lf\t%lf\t%lf\n", (double) 1/i, results[2], results[3]);
	}
	return(0);
}
Пример #3
0
int main(int argc, const char *argv[]) {
    
    const char* argument;
    
    long int initial_timestamp;
    float initial_lat, initial_lng, initial_alt;
    float burst_alt, ascent_rate, drag_coeff, rmswinderror;
    int descent_mode;
    int scenario_idx, n_scenarios;
    int alarm_time;
    char* endptr;       // used to check for errors on strtod calls 
    
    wind_file_cache_t* file_cache;
    dictionary*        scenario = NULL;
    
    // configure command-line options parsing
    void *options = gopt_sort(&argc, argv, gopt_start(
        gopt_option('h', 0, gopt_shorts('h', '?'), gopt_longs("help")),
        gopt_option('z', 0, gopt_shorts(0), gopt_longs("version")),
        gopt_option('v', GOPT_REPEAT, gopt_shorts('v'), gopt_longs("verbose")),
        gopt_option('o', GOPT_ARG, gopt_shorts('o'), gopt_longs("output")),
        gopt_option('k', GOPT_ARG, gopt_shorts('k'), gopt_longs("kml")),
        gopt_option('t', GOPT_ARG, gopt_shorts('t'), gopt_longs("start_time")),
        gopt_option('i', GOPT_ARG, gopt_shorts('i'), gopt_longs("data_dir")),
        gopt_option('d', 0, gopt_shorts('d'), gopt_longs("descending")),
        gopt_option('e', GOPT_ARG, gopt_shorts('e'), gopt_longs("wind_error")),
        gopt_option('a', GOPT_ARG, gopt_shorts('a'), gopt_longs("alarm"))
    ));

    if (gopt(options, 'h')) {
        // Help!
        // Print usage information
        printf("Usage: %s [options] [scenario files]\n", argv[0]);
        printf("Options:\n\n");
        printf(" -h --help               Display this information.\n");
        printf(" --version               Display version information.\n");
        printf(" -v --verbose            Display more information while running,\n");
        printf("                           Use -vv, -vvv etc. for even more verbose output.\n");
        printf(" -t --start_time <int>   Start time of model, defaults to current time.\n");
        printf("                           Should be a UNIX standard format timestamp.\n");
        printf(" -o --output <file>      Output file for CSV data, defaults to stdout. Overrides scenario.\n");
        printf(" -k --kml <file>         Output KML file.\n");
        printf(" -d --descending         We are in the descent phase of the flight, i.e. after\n");
        printf("                           burst or cutdown. burst_alt and ascent_rate ignored.\n");
        printf(" -i --data_dir <dir>     Input directory for wind data, defaults to current dir.\n\n");
        printf(" -e --wind_error <err>   RMS windspeed error (m/s).\n");
        printf(" -a --alarm <seconds>    Use alarm() to kill pred incase it hangs.\n");
        printf("The scenario file is an INI-like file giving the launch scenario. If it is\n");
        printf("omitted, the scenario is read from standard input.\n");
      exit(0);
    }

    if (gopt(options, 'z')) {
      // Version information
      printf("Landing Prediction version: %s\nCopyright (c) CU Spaceflight 2009\n", VERSION);
      exit(0);
    }

    if (gopt_arg(options, 'a', &argument) && strcmp(argument, "-")) {
      alarm_time = strtol(argument, &endptr, 0);
      if (endptr == argument) {
        fprintf(stderr, "ERROR: %s: invalid alarm length\n", argument);
        exit(1);
      }
      alarm(alarm_time);
    }
    
    verbosity = gopt(options, 'v');
    
    if (gopt(options, 'd'))
        descent_mode = DESCENT_MODE_DESCENDING;
    else
        descent_mode = DESCENT_MODE_NORMAL;
      
    if (gopt_arg(options, 'k', &argument) && strcmp(argument, "-")) {
      kml_file = fopen(argument, "wb");
      if (!kml_file) {
        fprintf(stderr, "ERROR: %s: could not open KML file for output\n", argument);
        exit(1);
      }
    }
    else
      kml_file = NULL;

    if (gopt_arg(options, 't', &argument) && strcmp(argument, "-")) {
      initial_timestamp = strtol(argument, &endptr, 0);
      if (endptr == argument) {
        fprintf(stderr, "ERROR: %s: invalid start timestamp\n", argument);
        exit(1);
      }
    } else {
      initial_timestamp = time(NULL);
    }
    
    if (!(gopt_arg(options, 'i', &data_dir) && strcmp(data_dir, "-")))
      data_dir = "./";


    // populate wind data file cache
    file_cache = wind_file_cache_new(data_dir);

    // read in flight parameters
    n_scenarios = argc - 1;
    if(n_scenarios == 0) {
        // we'll parse from std in
        n_scenarios = 1;
    }

    for(scenario_idx = 0; scenario_idx < n_scenarios; ++scenario_idx) {
        char* scenario_output = NULL;

        if(argc > scenario_idx+1) {
            scenario = iniparser_load(argv[scenario_idx+1]);
        } else {
            scenario = iniparser_loadfile(stdin);
        }

        if(!scenario) {
            fprintf(stderr, "ERROR: could not parse scanario file.\n");
            exit(1);
        }

        if(verbosity > 1) {
            fprintf(stderr, "INFO: Parsed scenario file:\n");
            iniparser_dump_ini(scenario, stderr);
        }

        scenario_output = iniparser_getstring(scenario, "output:filename", NULL);

        if (gopt_arg(options, 'o', &argument) && strcmp(argument, "-")) {
            if(verbosity > 0) {
                fprintf(stderr, "INFO: Writing output to file specified on command line: %s\n", argument);
            }
            output = fopen(argument, "wb");
            if (!output) {
                fprintf(stderr, "ERROR: %s: could not open CSV file for output\n", argument);
                exit(1);
            }
        } else if (scenario_output != NULL) {
            if(verbosity > 0) {
                fprintf(stderr, "INFO: Writing output to file specified in scenario: %s\n", scenario_output);
            }
            output = fopen(scenario_output, "wb");
            if (!output) {
                fprintf(stderr, "ERROR: %s: could not open CSV file for output\n", scenario_output);
                exit(1);
            }
        } else {
            if(verbosity > 0) {
                fprintf(stderr, "INFO: Writing output to stdout.\n");
            }
            output = stdout;
        }

        // write KML header
        if (kml_file)
            start_kml();

        // The observant amongst you will notice that there are default values for
        // *all* keys. This information should not be spread around too well.
        // Unfortunately, this means we lack some error checking.

        initial_lat = iniparser_getdouble(scenario, "launch-site:latitude", 0.0);
        initial_lng = iniparser_getdouble(scenario, "launch-site:longitude", 0.0);
        initial_alt = iniparser_getdouble(scenario, "launch-site:altitude", 0.0);

        ascent_rate = iniparser_getdouble(scenario, "altitude-model:ascent-rate", 1.0);

        // The 1.1045 comes from a magic constant buried in
        // ~cuspaceflight/public_html/predict/index.php.
        drag_coeff = iniparser_getdouble(scenario, "altitude-model:descent-rate", 1.0) * 1.1045;

        burst_alt = iniparser_getdouble(scenario, "altitude-model:burst-altitude", 1.0);

        rmswinderror = iniparser_getdouble(scenario, "atmosphere:wind-error", 0.0);
        if(gopt_arg(options, 'e', &argument) && strcmp(argument, "-")) {
            rmswinderror = strtod(argument, &endptr);
            if (endptr == argument) {
                fprintf(stderr, "ERROR: %s: invalid RMS wind speed error\n", argument);
                exit(1);
            }
        }

        {
            int year, month, day, hour, minute, second;
            year = iniparser_getint(scenario, "launch-time:year", -1);
            month = iniparser_getint(scenario, "launch-time:month", -1);
            day = iniparser_getint(scenario, "launch-time:day", -1);
            hour = iniparser_getint(scenario, "launch-time:hour", -1);
            minute = iniparser_getint(scenario, "launch-time:minute", -1);
            second = iniparser_getint(scenario, "launch-time:second", -1);

            if((year >= 0) && (month >= 0) && (day >= 0) && (hour >= 0)
                    && (minute >= 0) && (second >= 0)) 
            {
                struct tm timeval = { 0 };
                time_t scenario_launch_time = -1;

                if(verbosity > 0) {
                    fprintf(stderr, "INFO: Using launch time from scenario: "
                            "%i/%i/%i %i:%i:%i\n",
                            year, month, day, hour, minute, second);
                }

                timeval.tm_sec = second;
                timeval.tm_min = minute;
                timeval.tm_hour = hour;
                timeval.tm_mday = day; /* 1 - 31 */
                timeval.tm_mon = month - 1; /* 0 - 11 */
                timeval.tm_year = year - 1900; /* f**k you Millenium Bug! */

#ifndef _BSD_SOURCE
#               warning This version of mktime does not allow explicit setting of timezone. 
#else
                timeval.tm_zone = "UTC";
#endif

                scenario_launch_time = mktime(&timeval);
                if(scenario_launch_time <= 0) {
                    fprintf(stderr, "ERROR: Launch time in scenario is invalid\n");
                    exit(1);
                } else {
                    initial_timestamp = scenario_launch_time;
                }
            }
        }

        if(verbosity > 0) {
            fprintf(stderr, "INFO: Scenario loaded:\n");
            fprintf(stderr, "    - Initial latitude  : %lf deg N\n", initial_lat);
            fprintf(stderr, "    - Initial longitude : %lf deg E\n", initial_lng);
            fprintf(stderr, "    - Initial altitude  : %lf m above sea level\n", initial_alt);
            fprintf(stderr, "    - Initial timestamp : %li\n", initial_timestamp);
            fprintf(stderr, "    - Drag coeff.       : %lf\n", drag_coeff);
            if(!descent_mode) {
                fprintf(stderr, "    - Ascent rate       : %lf m/s\n", ascent_rate);
                fprintf(stderr, "    - Burst alt.        : %lf m\n", burst_alt);
            }
            fprintf(stderr, "    - Windspeed err.    : %f m/s\n", rmswinderror);
        }
        
        {
            // do the actual stuff!!
            altitude_model_t* alt_model = altitude_model_new(descent_mode, burst_alt, 
                                                             ascent_rate, drag_coeff);
            if(!alt_model) {
                    fprintf(stderr, "ERROR: error initialising altitude profile\n");
                    exit(1);
            }

            if (!run_model(file_cache, alt_model, 
                           initial_lat, initial_lng, initial_alt, initial_timestamp,
                           rmswinderror)) {
                    fprintf(stderr, "ERROR: error during model run!\n");
                    exit(1);
            }

            altitude_model_free(alt_model);
        }

        // release the scenario
        iniparser_freedict(scenario);
        
        // write footer to KML and close output files
        if (kml_file) {
            finish_kml();
            fclose(kml_file);
        }

        if (output != stdout) {
            fclose(output);
        }
    }

    // release gopt data, 
    gopt_free(options);

    // release the file cache resources.
    wind_file_cache_free(file_cache);

    return 0;
}