Ecl Output: Add write<>() Support for PaddedOutputString<8>
This is in preparation of using the EclOutput output facility in
RestartIO::save()
This commit is contained in:
@@ -18,14 +18,14 @@
|
||||
#ifndef OPM_IO_ECLOUTPUT_HPP
|
||||
#define OPM_IO_ECLOUTPUT_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <ios>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
#include <ios>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <vector>
|
||||
|
||||
#include <opm/io/eclipse/EclIOdata.hpp>
|
||||
#include <opm/io/eclipse/PaddedOutputString.hpp>
|
||||
|
||||
namespace Opm { namespace EclIO { namespace OutputStream {
|
||||
class Restart;
|
||||
@@ -81,6 +81,7 @@ private:
|
||||
void writeBinaryArray(const std::vector<T>& data);
|
||||
|
||||
void writeBinaryCharArray(const std::vector<std::string>& data);
|
||||
void writeBinaryCharArray(const std::vector<PaddedOutputString<8>>& data);
|
||||
|
||||
void writeFormattedHeader(const std::string& arrName, int size, eclArrType arrType);
|
||||
|
||||
@@ -88,6 +89,7 @@ private:
|
||||
void writeFormattedArray(const std::vector<T>& data);
|
||||
|
||||
void writeFormattedCharArray(const std::vector<std::string>& data);
|
||||
void writeFormattedCharArray(const std::vector<PaddedOutputString<8>>& data);
|
||||
|
||||
std::string make_real_string(float value) const;
|
||||
std::string make_doub_string(double value) const;
|
||||
@@ -100,6 +102,12 @@ private:
|
||||
template<>
|
||||
void EclOutput::write<std::string>(const std::string& name,
|
||||
const std::vector<std::string>& data);
|
||||
|
||||
template <>
|
||||
void EclOutput::write<PaddedOutputString<8>>
|
||||
(const std::string& name,
|
||||
const std::vector<PaddedOutputString<8>>& data);
|
||||
|
||||
}} // namespace Opm::EclIO
|
||||
|
||||
#endif // OPM_IO_ECLOUTPUT_HPP
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#ifndef OPM_IO_OUTPUTSTREAM_HPP_INCLUDED
|
||||
#define OPM_IO_OUTPUTSTREAM_HPP_INCLUDED
|
||||
|
||||
#include <opm/io/eclipse/PaddedOutputString.hpp>
|
||||
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -140,6 +142,17 @@ namespace Opm { namespace EclIO { namespace OutputStream {
|
||||
void write(const std::string& kw,
|
||||
const std::vector<std::string>& data);
|
||||
|
||||
/// Write padded character data (8 characters per string)
|
||||
/// to underlying output stream.
|
||||
///
|
||||
/// Must not be called prior to \c prepareStep
|
||||
///
|
||||
/// \param[in] kw Name of output vector (keyword).
|
||||
///
|
||||
/// \param[in] data Output values.
|
||||
void write(const std::string& kw,
|
||||
const std::vector<PaddedOutputString<8>>& data);
|
||||
|
||||
private:
|
||||
/// Restart output stream.
|
||||
std::unique_ptr<EclOutput> stream_;
|
||||
|
||||
@@ -62,6 +62,20 @@ void EclOutput::write<std::string>(const std::string& name,
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void EclOutput::write<PaddedOutputString<8>>
|
||||
(const std::string& name,
|
||||
const std::vector<PaddedOutputString<8>>& data)
|
||||
{
|
||||
if (this->isFormatted) {
|
||||
writeFormattedHeader(name, data.size(), CHAR);
|
||||
writeFormattedCharArray(data);
|
||||
}
|
||||
else {
|
||||
writeBinaryHeader(name, data.size(), CHAR);
|
||||
writeBinaryCharArray(data);
|
||||
}
|
||||
}
|
||||
|
||||
void EclOutput::message(const std::string& msg)
|
||||
{
|
||||
@@ -233,6 +247,41 @@ void EclOutput::writeBinaryCharArray(const std::vector<std::string>& data)
|
||||
}
|
||||
}
|
||||
|
||||
void EclOutput::writeBinaryCharArray(const std::vector<PaddedOutputString<8>>& data)
|
||||
{
|
||||
const auto size = data.size();
|
||||
|
||||
const auto sizeData = block_size_data_binary(CHAR);
|
||||
|
||||
const int sizeOfElement = std::get<0>(sizeData);
|
||||
const int maxBlockSize = std::get<1>(sizeData);
|
||||
const int maxNumberOfElements = maxBlockSize / sizeOfElement;
|
||||
|
||||
int rest = size * sizeOfElement;
|
||||
|
||||
if (!ofileH.is_open()) {
|
||||
OPM_THROW(std::runtime_error,"fstream fileH not open for writing");
|
||||
}
|
||||
|
||||
auto elm = data.begin();
|
||||
while (rest > 0) {
|
||||
const auto numElm = (rest > maxBlockSize)
|
||||
? maxNumberOfElements
|
||||
: rest / sizeOfElement;
|
||||
|
||||
rest = (rest > maxBlockSize) ? rest - maxBlockSize : 0;
|
||||
|
||||
auto dhead = flipEndianInt(numElm * sizeOfElement);
|
||||
|
||||
ofileH.write(reinterpret_cast<char*>(&dhead), sizeof(dhead));
|
||||
|
||||
for (auto i = 0*numElm; i < numElm; ++i, ++elm) {
|
||||
ofileH.write(elm->c_str(), sizeOfElement);
|
||||
}
|
||||
|
||||
ofileH.write(reinterpret_cast<char*>(&dhead), sizeof(dhead));
|
||||
}
|
||||
}
|
||||
|
||||
void EclOutput::writeFormattedHeader(const std::string& arrName, int size, eclArrType arrType)
|
||||
{
|
||||
@@ -417,4 +466,25 @@ void EclOutput::writeFormattedCharArray(const std::vector<std::string>& data)
|
||||
}
|
||||
}
|
||||
|
||||
}} // namespace Opm::ecl
|
||||
void EclOutput::writeFormattedCharArray(const std::vector<PaddedOutputString<8>>& data)
|
||||
{
|
||||
const auto sizeData = block_size_data_formatted(CHAR);
|
||||
|
||||
const int nColumns = std::get<1>(sizeData);
|
||||
|
||||
const auto size = data.size();
|
||||
|
||||
for (auto i = 0*size; i < size; ++i) {
|
||||
ofileH << " '" << data[i].c_str() << '\'';
|
||||
|
||||
if ((i+1) % nColumns == 0) {
|
||||
ofileH << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
if ((size % nColumns) != 0) {
|
||||
ofileH << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
}} // namespace Opm::EclIO
|
||||
|
||||
@@ -192,6 +192,14 @@ write(const std::string& kw, const std::vector<std::string>& data)
|
||||
this->writeImpl(kw, data);
|
||||
}
|
||||
|
||||
void
|
||||
Opm::EclIO::OutputStream::Restart::
|
||||
write(const std::string& kw,
|
||||
const std::vector<PaddedOutputString<8>>& data)
|
||||
{
|
||||
this->writeImpl(kw, data);
|
||||
}
|
||||
|
||||
void
|
||||
Opm::EclIO::OutputStream::Restart::
|
||||
openUnified(const std::string& fname,
|
||||
|
||||
Reference in New Issue
Block a user