add utility function for verifying simulator output directory

use this in fvbaseproblem.hh. allows to encapsulate some includes
This commit is contained in:
Arne Morten Kvarving 2024-09-05 13:19:30 +02:00
parent 5d2a25ac14
commit e56fc10b4c
3 changed files with 38 additions and 20 deletions

View File

@ -44,9 +44,6 @@
#include <limits>
#include <string>
#include <sys/stat.h>
#include <unistd.h>
namespace Opm::Properties {
template <class TypeTag, class MyTypeTag>
@ -208,23 +205,7 @@ public:
*/
std::string outputDir() const
{
std::string outputDir = Parameters::Get<Parameters::OutputDir>();
if (outputDir.empty())
outputDir = ".";
// TODO: replace this by std::filesystem once we require c++-2017
struct stat st;
if (::stat(outputDir.c_str(), &st) != 0)
throw std::runtime_error("Could not access output directory '"+outputDir+"':"
+strerror(errno));
if (!S_ISDIR(st.st_mode))
throw std::runtime_error("Path to output directory '"+outputDir+"' exists but is not a directory");
if (access(outputDir.c_str(), W_OK) != 0)
throw std::runtime_error("Output directory '"+outputDir+"' exists but is not writeable");
return outputDir;
return simulatorOutputDir();
}
/*!

View File

@ -24,10 +24,16 @@
#include <config.h>
#include <opm/models/utils/simulatorutils.hpp>
#include <opm/models/discretization/common/fvbaseparameters.hh>
#include <opm/models/utils/parametersystem.hpp>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>
#if HAVE_QUAD
@ -110,6 +116,32 @@ std::string humanReadableTime(double timeInSeconds, bool isAmendment)
return oss.str();
}
std::string simulatorOutputDir()
{
std::string outputDir = Parameters::Get<Parameters::OutputDir>();
if (outputDir.empty()) {
outputDir = ".";
}
// TODO: replace this by std::filesystem once we require c++-2017
struct stat st;
if (::stat(outputDir.c_str(), &st) != 0)
throw std::runtime_error("Could not access output directory '" + outputDir + "':" +
strerror(errno));
if (!S_ISDIR(st.st_mode)) {
throw std::runtime_error("Path to output directory '" +outputDir +
"' exists but is not a directory");
}
if (access(outputDir.c_str(), W_OK) != 0) {
throw std::runtime_error("Output directory '" + outputDir +
"' exists but is not writeable");
}
return outputDir;
}
template<class Scalar>
std::vector<Scalar> readTimeStepFile(const std::string& file)
{

View File

@ -37,6 +37,11 @@ namespace Opm {
*/
std::string humanReadableTime(double timeInSeconds, bool isAmendment = true);
/*!
* \brief Determine and check the configured directory for simulation output.
*/
std::string simulatorOutputDir();
/*!
* \brief Read explicitly defined time steps from file.
* \param file File to read