int main(int argc, char** argv)
/*****************************************************************************/
{

  struct options opts[] =
  {
    { 1, "rank",   "rank of processor (0) ", "r", 1 },
    { 2, "num_procs", "number of processors (1)", "n", 1 },
    { 3, "all",      "generate all meshes for number of processors (false)",      "a", 0 },
    { 4, "dimension",    "dimension (3D)",       "d", 1 },
    { 5, "file",    "mesh description file (no default)",       "f", 1 },
    { 0, NULL,      NULL,                      NULL, 0 }
  };

  char *args;
  int c;
  int all = FALSE;
  int rank = 0;
  int issz;
  int num_procs = 1;
  int start_rank = 0;
  int end_rank = 0;
  int dimension = 3;
  char * file_name = NULL;
  char * out_file_name = NULL;
  FILE * infile = NULL;
  long size;
  char *file_char_array = NULL;

  if(argc == 1){
    getopts_usage(argv[0],opts);
    return 1;
  }

  while ((c = getopts(argc, argv, opts, &args)) != 0) { switch(c)
        {
	  /* Special Case: Recognize options that we didn't set above. */
	case -2:
	  printf("Unknown Option: \n");/* <<  args << std::endl;*/
	  getopts_usage(argv[0],opts);
	  return 1;
	  break;
	  /* Special Case: getopts() can't allocate memory.
	     Should probably exit() here */
	case -1:
	  printf("Unabled to allocate memory from getopts().\n");
	  break;
	case 1:
	  rank = atoi(args);
	  break;
        case 2:
	  num_procs = atoi(args);
	  break;
        case 3:
	  all = TRUE;
	  break;
        case 4:
	  dimension = atoi(args);
	  break;
        case 5:
	  {
	    int sl = strlen(args);
	    file_name = (char *)malloc(sl+1);
	    file_name[sl] = '\0';
	    strcpy(file_name,args);
	  }
	  break;
	default:
	  break;
        }
/*   This free() is required since getopts() automagically allocates space */
/*     for "args" everytime it's called.  */
      free(args);
    }

  if(rank < 0 || rank >= num_procs){
    getopts_usage(argv[0],opts);
    printf("rank must be positive and between 0 and  the number of processors %i\n",rank);
  }

  if(num_procs < 0){
    getopts_usage(argv[0],opts);
    printf("number of processors must be positive %i\n", num_procs);
  }


  /*deal with all switch*/
  end_rank = num_procs;
  if(!all){
    start_rank = rank;
    end_rank = start_rank+1;
  }

  infile = fopen(file_name,"r");
  if(!infile){
    printf("unable to open input file ");
    return 1;
  }
  fseek(infile, 0, SEEK_END);
  size = ftell(infile);
  fseek(infile, 0, SEEK_SET);
  file_char_array = (char *)malloc(size + 1);
  file_char_array[size] = '\0';
  fread(file_char_array, sizeof(char), size, infile);
  fclose(infile);

  /*create the out_file_name*/
  out_file_name = (char*)malloc(MAX_STR_LENGTH+1);

  for( rank = start_rank; rank != end_rank; rank ++){
    int cr_result;
    sprintf(out_file_name,"%s.exo.%i.%i",file_name,num_procs,rank);

    cr_result = Create_Pamgen_Mesh(file_char_array,
				   dimension,
				   rank,
				   num_procs,
				   INT_MAX);


    if (cr_result == ERROR_PARSING_DEFINITION){
      int essz = getPamgenEchoStreamSize();
      char * echo_char_array = (char *)malloc(essz+1);
      printf("PARSE ERROR\n");
      echo_char_array[essz] = '\0';
      echo_char_array = getPamgenEchoStream(echo_char_array);
      if(echo_char_array)printf("%s",echo_char_array);
      if(cr_result == ERROR_CREATING_IMD)printf("ERROR Failure to create Inline_Mesh_Desc creation\n");
      if(echo_char_array)free(echo_char_array);
      return 1;
    }

    if(cr_result == ERROR_CREATING_MS){
      int essz = getPamgenErrorStreamSize();
      char * error_char_array = (char *)malloc(essz+1);
      error_char_array[essz] = '\0';
      error_char_array = getPamgenErrorStream(error_char_array);
      if(error_char_array)printf("%s",error_char_array);
      printf("\nERROR Failure to create Mesh_Specification\n");
      if(error_char_array)free(error_char_array);
      return 1;
    }


    {
      int wssz = getPamgenWarningStreamSize();
      if(wssz){
	char * warning_char_array = (char *)malloc(wssz+1);
	warning_char_array[wssz] = '\0';
	warning_char_array = getPamgenWarningStream(warning_char_array);
	printf("WARNING Records\n");
	printf("%s",warning_char_array);
	free(warning_char_array);
      }
    }


    {
      issz = getPamgenInfoStreamSize();
      if(issz){
	char * info_char_array = (char *)malloc(issz+1);
	info_char_array[issz] = '\0';
	info_char_array = getPamgenInfoStream(info_char_array);
	printf("INFO Records\n");
	printf("%s",info_char_array);
	free(info_char_array);
      }
    }

    read_mesh_to_memory();

    write_mesh_to_stdout();

#ifdef HAVE_PAMGEN_NEMESIS
    write_to_exodus(rank,num_procs,out_file_name);
#endif/* HAVE_PAMGEN_NEMESIS*/

    Delete_Pamgen_Mesh();
    free_memory();
  }/* end loop over all output ranks*/

  if(file_char_array)free(file_char_array);
  if(out_file_name)free(out_file_name);
  if(file_name)free(file_name);

  return 0;
}
Exemple #2
0
/* int getopts()
 *
 * Returns: -1 - Couldn't allocate memory.  Please handle me. 
 *          0  - No arguments to parse
 *          #  - The number in the struct for the matched arg.
 *
*/
int getopts(int argc, char **argv, struct options opts[], char **args)
{
  int argCounter, sizeOfArgs;
  if (argc == 1 || option_index == argc)
    return 0;
  
/* Search for '-h' or '--help' first.  Then we can just exit */
  for (argCounter = 1; argCounter < argc; argCounter++)
    {
      if (!strcmp(argv[argCounter], "-h") || !strcmp(argv[argCounter], "--help"))
        {
useage:
          getopts_usage(argv[0], opts);
          exit(0);
        }
    }
/* End of -h --help section */
  
  *args = NULL;
  if (option_index <= argc)
    {
      for (argCounter = 0; opts[argCounter].number!=0; argCounter++)
        {
          if ((opts[argCounter].name && !strcmp(opts[argCounter].name, (argv[option_index]+2))) || 
              (opts[argCounter].shortName && !strcmp(opts[argCounter].shortName, (argv[option_index]+1))))
            {
              if (opts[argCounter].args)
                {
                  option_index++;
                  if (option_index >= argc)
                    goto useage;
/* This grossness that follows is to support having a '-' in the argument.  */
                  if (*argv[option_index] == '-')
                    {
                      int optionSeeker;
                      for (optionSeeker = 0; opts[optionSeeker].description; optionSeeker++)
                        {
                          if ((opts[optionSeeker].name && 
                               !strcmp(opts[optionSeeker].name, (argv[option_index]+2))) ||
                               (opts[optionSeeker].shortName && 
                               !strcmp(opts[optionSeeker].shortName, (argv[option_index]+1))))
                            {
                              goto useage;
                            }
                        }
/* End of gross hack for supporting '-' in arguments. */
                    }
                  sizeOfArgs = (int)strlen(argv[option_index]);
                  if ((*args = (char *)calloc(1, sizeOfArgs+1)) == NULL)
                    return -1;
                  strncpy(*args, argv[option_index], sizeOfArgs);
                }
              option_index++;
              return opts[argCounter].number;
            }
        }
/** The Option doesn't exist.  We should warn them. */
      if (*argv[option_index] == '-')
        {
          sizeOfArgs = (int)strlen(argv[option_index]);
          if ((*args = (char *)calloc(1, sizeOfArgs+1)) == NULL)
            return -1;
          strncpy(*args, argv[option_index], sizeOfArgs);
          option_index++;
          return -2;
        }
    }
  return 0;
}