Cleanup when ESMRY renamle fails

Temporary ESMRY file name is now includeing simulation root name
Delete temp esmry file when the rename temp ESMRY file to simulation ESMRY file.
This commit is contained in:
Torbjørn Skille 2022-10-28 13:50:17 +02:00
parent 75b78bf659
commit 8f4c0b4e92
2 changed files with 27 additions and 7 deletions

View File

@ -63,6 +63,7 @@ private:
std::array<int, 3> ijk_from_global_index(const GridDims& dims, int globInd) const;
std::vector<std::string> make_modified_keys(const std::vector<std::string>& valueKeys, const GridDims& dims);
bool rename_tmpfile(const std::string& tmp_fname);
};

View File

@ -20,6 +20,7 @@
#include <opm/io/eclipse/EclFile.hpp>
#include <opm/io/eclipse/ExtSmryOutput.hpp>
#include <opm/io/eclipse/EclOutput.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <opm/input/eclipse/EclipseState/EclipseState.hpp>
@ -29,7 +30,6 @@
#include <string>
#include <filesystem>
namespace Opm { namespace EclIO {
@ -99,8 +99,12 @@ void ExtSmryOutput::write(const std::vector<float>& ts_data, int report_step, bo
{
const auto tp = std::chrono::system_clock::now();
auto sec_since_epoch = std::chrono::duration_cast<std::chrono::seconds>(tp.time_since_epoch()).count();
std::string tmp_file_name = "TMP_" + std::to_string(sec_since_epoch) + ".ESMRY";
std::filesystem::path esmry_file(m_outputFileName);
std::filesystem::path rootName = esmry_file.parent_path() / esmry_file.stem();
std::string tmp_file_name = rootName.string() + "_TMP_" + std::to_string(sec_since_epoch) + ".ESMRY";
{
Opm::EclIO::EclOutput outFile(tmp_file_name, m_fmt, std::ios::out);
@ -123,16 +127,31 @@ void ExtSmryOutput::write(const std::vector<float>& ts_data, int report_step, bo
}
}
const std::filesystem::path from_file = tmp_file_name;
const std::filesystem::path to_file = m_outputFileName;
std::filesystem::rename(from_file, to_file);
m_last_write = std::chrono::system_clock::now();
if (rename_tmpfile(tmp_file_name)){
m_last_write = std::chrono::system_clock::now();
} else {
Opm::OpmLog::warning("Not able to rename temporary ESMRY file " + tmp_file_name);
std::filesystem::path tmp_file(tmp_file_name);
std::filesystem::remove(tmp_file);
}
}
m_nTimeSteps++;
}
bool ExtSmryOutput::rename_tmpfile(const std::string& tmp_fname)
{
try {
std::filesystem::path from_file(tmp_fname);
std::filesystem::path to_file(m_outputFileName);
std::filesystem::rename(from_file, to_file);
} catch (...){
return false;
}
return true;
}
std::vector<std::string> ExtSmryOutput::make_modified_keys(const std::vector<std::string>& valueKeys, const GridDims& dims)
{