URI:
       tsphere_status.c - sphere - GPU-based 3D discrete element method algorithm with optional fluid coupling
  HTML git clone git://src.adamsgaard.dk/sphere
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
       tsphere_status.c (4234B)
       ---
            1 #include <stdio.h>
            2 #include <stdlib.h>
            3 #include <unistd.h>
            4 #include <string.h>
            5 #include <sys/types.h>
            6 #include <dirent.h>
            7 
            8 int print_usage(FILE* stream, char* argv0, int return_status);
            9 int open_status_file(char* cwd, char* sim_name, int format);
           10 
           11 int main(int argc, char *argv[])
           12 {
           13 
           14     // Read path to current working directory
           15     char *cwd;
           16     cwd = getcwd(0, 0);
           17     if (!cwd) {  // Terminate program execution if path is not obtained
           18         perror("Could not read path to current workind directory "
           19                 "(getcwd failed)");
           20         return 1;
           21     }
           22 
           23     if (argc == 1 || argc != 2) {
           24         return print_usage(stderr, argv[0], 1);
           25     } else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
           26         return print_usage(stdout, argv[0], 0);
           27     } else if (strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "--list") == 0) {
           28         struct dirent **namelist;
           29         int n, i;
           30         char outputdir[1000];
           31         char* dotpos;
           32         char outstring[100];
           33         char* p;
           34         snprintf(outputdir, sizeof(outputdir), "%s/output/", cwd);
           35         n = scandir(outputdir, &namelist, 0 , alphasort);
           36         if (n < 0) {
           37             fprintf(stderr, "Error: could not open directory: %s\n", outputdir);
           38             return 1;
           39         } else {
           40             puts("Simulations with the following ID's are found in the "
           41                     "./output/ folder:");
           42 
           43             for (i = 0; i<n; i++) {
           44                 if ((dotpos = strstr(namelist[i]->d_name, ".status.dat"))
           45                         != NULL) {
           46                 
           47                     *dotpos = '\0';
           48                     snprintf(outstring, sizeof(outstring), "%-54s ",
           49                              namelist[i]->d_name);
           50                     for (p = outstring; *p != '\0'; p++)
           51                         if (*p == ' ') *p = '.';
           52                     printf("  %s", outstring);
           53                     (void)open_status_file(cwd, namelist[i]->d_name, 1);
           54                     puts("");
           55                 }
           56                 free(namelist[i]);
           57             }
           58             free(namelist);
           59         }
           60         return 0;
           61 
           62     }
           63     int ret = open_status_file(cwd, argv[1], 0);
           64     free(cwd);
           65     return ret;
           66 }
           67 
           68 int print_usage(FILE* stream, char* argv0, int return_status)
           69 {
           70     fprintf(stream, "sphere simulation status checker. Usage:\n"
           71             " %s [simulation id]\n"
           72             " %s [-h,--help]\n"
           73             " %s [-l,--list]\n"
           74             "Arguments:\n"
           75             " simulation id\tShow detailed status of simulation.\n"
           76             " -h, --help\tShow this help message.\n"
           77             " -l, --list\tPrint a list of simulations found in the ./output/ "
           78             "folder.\n"
           79             "\t\tEach simulation ID will be appended by a string showing:\n"
           80             "\t([CURRENT SIMULATION TIME] s, [PERCENTAGE COMPLETED] %%, "
           81             "[LATEST OUTPUT FILE])\n", argv0, argv0, argv0);
           82     return return_status;
           83 }
           84 
           85 
           86 int open_status_file(char* cwd, char* sim_name, int format) {
           87     // Open the simulation status file
           88     FILE *fp;
           89     char file[1000]; // Complete file path+name variable
           90     snprintf(file, sizeof(file), "%s/output/%s.status.dat", cwd, sim_name);
           91 
           92     if ((fp = fopen(file, "rt"))) {
           93         float time_current;
           94         float time_percentage;
           95         unsigned int file_nr;
           96 
           97         if (fscanf(fp, "%f%f%d", &time_current, &time_percentage, &file_nr)
           98                 != 3) {
           99             fprintf(stderr, "Error: could not parse file %s\n", file);
          100             return 1;
          101         }
          102 
          103         if (format == 1) {
          104             printf("%6.2fs / %3.0f%% / %5d",
          105                     time_current, time_percentage, file_nr);
          106         } else {
          107             printf("Reading %s:\n"
          108                     " - Current simulation time:  %f s\n"
          109                     " - Percentage completed:     %f %%\n"
          110                     " - Latest output file:       %s.output%05d.bin\n",
          111                     file, time_current, time_percentage, sim_name, file_nr);
          112         }
          113 
          114         fclose(fp);
          115 
          116         return 0; // Exit program successfully
          117 
          118     } else {
          119         fprintf(stderr, "Error: Could not open file %s\n"
          120                 "Run this program with `--help` flag for help.\n", file);
          121         return 1;
          122     }
          123 }
          124 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4