Beispiel #1
0
void
ImageFunction::getFiles()
{
#ifdef LIBMESH_HAVE_VTK
  // Storage for the file names
  _files = vtkSmartPointer<vtkStringArray>::New();

  // Use specified file name
  if (isParamValid("file"))
  {
    std::string filename = getParam<FileName>("file");
    _files->InsertNextValue(filename);
    _file_type = filename.substr(filename.find_last_of(".") + 1);
  }

  // File stack
  else
  {
    // Separate the file base from the path
    std::pair<std::string, std::string> split_file = MooseUtils::splitFileName(_file_base);

    // Create directory object
    tinydir_dir dir;
    tinydir_open_sorted(&dir, split_file.first.c_str());

    // Regex for extracting numbers from file
    std::ostringstream oss;
    oss << "(" << split_file.second << ".*?(\\d+))\\..*";
    pcrecpp::RE re_base_and_file_num(oss.str()); // Will pull out the full base and the file number simultaneously

    // Loop through the files in the directory
    for (unsigned int i = 0; i < dir.n_files; i++)
    {
      // Upate the current file
      tinydir_file file;
      tinydir_readfile_n(&dir, &file, i);

      // Store the file if it has proper extension as in numeric range
      if (!file.is_dir && MooseUtils::hasExtension(file.name, _file_type))
      {
        std::string the_base;
        int file_num = 0;
        re_base_and_file_num.FullMatch(file.name, &the_base, &file_num);
        if (!the_base.empty() && file_num >= _file_range[0] && file_num <= _file_range[1])
          _files->InsertNextValue(split_file.first + "/" + file.name);
      }
    }
    tinydir_close(&dir);
  }

  // Error if no files where located
  if (_files->GetNumberOfValues() == 0)
    mooseError("No image file(s) located");
#endif
}
Beispiel #2
0
std::string
getRecoveryFileBase(const std::list<std::string> & checkpoint_files)
{
  // Create storage for newest restart files
  // Note that these might have the same modification time if the simulation was fast.
  // In that case we're going to save all of the "newest" files and sort it out momentarily
  time_t newest_time = 0;
  std::list<std::string> newest_restart_files;

  // Loop through all possible files and store the newest
  for (std::list<std::string>::const_iterator it = checkpoint_files.begin(); it != checkpoint_files.end(); ++it)
  {
      struct stat stats;
      stat(it->c_str(), &stats);

      time_t mod_time = stats.st_mtime;
      if (mod_time > newest_time)
      {
        newest_restart_files.clear(); // If the modification time is greater, clear the list
        newest_time = mod_time;
      }

      if (mod_time == newest_time)
        newest_restart_files.push_back(*it);
  }

  // Loop through all of the newest files according the number in the file name
  int max_file_num = -1;
  std::string max_base;
  pcrecpp::RE re_base_and_file_num("(.*?(\\d+))\\..*"); // Will pull out the full base and the file number simultaneously

  // Now, out of the newest files find the one with the largest number in it
  for (std::list<std::string>::const_iterator it = newest_restart_files.begin(); it != newest_restart_files.end(); ++it)
  {
    std::string the_base;
    int file_num = 0;

    re_base_and_file_num.FullMatch(*it, &the_base, &file_num);

    if (file_num > max_file_num)
    {
      max_file_num = file_num;
      max_base = the_base;
    }
  }

  // Error if nothing was located
  if (max_file_num == -1)
    max_base.clear();

  return max_base;
}
Beispiel #3
0
void
CommonOutputAction::setRecoverFileBase()
{
  // Extract the default directory for recover files
  std::string dir = getRecoveryDirectory();

  pcrecpp::RE re_base_and_file_num("(.*?(\\d+))\\..*"); // Will pull out the full base and the file number simultaneously

  tinydir_dir tdir;

  if (tinydir_open(&tdir, dir.c_str()) == -1)
    mooseError("Cannot open directory: " << dir);

  time_t newest_time = 0;
  std::vector<std::string> newest_restart_files;

  // First, the newest candidate files.
  // Note that these might have the same modification time if the simulation was fast
  // In that case we're going to save all of the "newest" files and sort it out momentarily
  while(tdir.has_next)
  {
    tinydir_file file;

    if (tinydir_readfile(&tdir, &file) == -1)
    {
      tinydir_next(&tdir);
      continue;
    }

    std::string file_name = file.name;

    if ((!file.is_dir))
    {
      struct stat stats;

      std::string full_path = dir + "/" + file_name;

      stat(full_path.c_str(), &stats);

      time_t mod_time = stats.st_mtime;
      if (mod_time > newest_time)
      {
        newest_restart_files.clear();
        newest_time = mod_time;
      }

      if (mod_time == newest_time)
        newest_restart_files.push_back(file_name);
    }

    tinydir_next(&tdir);
  }

  int max_file_num = -1;
  std::string max_base;

  // Now, out of the newest files find the one with the largest number in it
  for(unsigned int i=0; i<newest_restart_files.size(); i++)
  {
    std::string file_name = newest_restart_files[i];

    std::string the_base;
    int file_num = 0;

    re_base_and_file_num.FullMatch(file_name, &the_base, &file_num);

    if (file_num > max_file_num)
    {
      max_file_num = file_num;
      max_base = the_base;
    }
  }

  if (max_file_num == -1)
    mooseError("Unable to find suitable recovery file!");

  std::string recovery = dir + "/" + max_base;

  Moose::out << "\nUsing " << recovery << " for recovery.\n" << std::endl;

  _app.setRecoverFileBase(recovery);
}
Beispiel #4
0
std::string
RecoverBaseAction::newestRestartFileWithBase(std::string base_name)
{
  char * base_name_char = const_cast<char *>(base_name.c_str());

  std::string dir = dirname(base_name_char);
  std::string file_base = basename(base_name_char);

  dir = dir + "/" + file_base + "_" + getParam<std::string>("checkpoint_dir_suffix");

  pcrecpp::RE re_base_and_file_num("(.*?(\\d+))\\..*"); // Will pull out the full base and the file number simultaneously

  tinydir_dir tdir;

  if (tinydir_open(&tdir, dir.c_str()) == -1)
    mooseError("Cannot open directory!");

  time_t newest_time = 0;
  std::vector<std::string> newest_restart_files;

  // First, the newest candidate files.
  // Note that these might have the same modification time if the simulation was fast
  // In that case we're going to save all of the "newest" files and sort it out momentarily
  while(tdir.has_next)
  {
    tinydir_file file;

    if (tinydir_readfile(&tdir, &file) == -1)
    {
      tinydir_next(&tdir);
      continue;
    }

    std::string file_name = file.name;

    if ((!file.is_dir))
    {
      struct stat stats;

      std::string full_path = dir + "/" + file_name;

      stat(full_path.c_str(), &stats);

      time_t mod_time = stats.st_mtime;

      if (mod_time > newest_time)
      {
        newest_restart_files.clear();
        newest_time = mod_time;
      }

      if (mod_time == newest_time)
        newest_restart_files.push_back(file_name);
    }

    tinydir_next(&tdir);
  }

  int max_file_num = -1;
  std::string max_base;

  // Now, out of the newest files find the one with the largest number in it
  for(unsigned int i=0; i<newest_restart_files.size(); i++)
  {
    std::string file_name = newest_restart_files[i];

    std::string the_base;
    int file_num = 0;

    re_base_and_file_num.FullMatch(file_name, &the_base, &file_num);

    if (file_num > max_file_num)
    {
      max_file_num = file_num;
      max_base = the_base;
    }
  }

  if (max_file_num == -1)
    mooseError("Unable to find suitable recovery file!");

  return dir + "/" + max_base;
}