Redo Public Interface of OutputStream::Restart

In particular, make the stream() function into a private detail of
the implementation and add an overload set for outputting keyword
data and messages to the underlying output stream.  Update unit test
accordingly.

Suggested by: Atgeirr F. Rasmussen
This commit is contained in:
Bård Skaflestad 2019-05-27 11:44:24 +02:00
parent 992d3b0ce5
commit df1a011b78
3 changed files with 159 additions and 55 deletions

View File

@ -23,6 +23,7 @@
#include <ios>
#include <memory>
#include <string>
#include <vector>
namespace Opm { namespace ecl {
@ -94,10 +95,65 @@ namespace Opm { namespace ecl { namespace OutputStream {
return this->unified_;
}
/// Access writable output stream.
/// Generate a message string (keyword type 'MESS') in underlying
/// output stream.
///
/// Must not be called prior to \c prepareStep.
EclOutput& stream();
/// Must not be called prior to \c prepareStep
///
/// \param[in] msg Message string (e.g., "STARTSOL").
void message(const std::string& msg);
/// Write integer data 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<int>& data);
/// Write boolean data 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<bool>& data);
/// Write single precision floating point data 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<float>& data);
/// Write double precision floating point data 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<double>& data);
/// Write unpadded string data 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<std::string>& data);
private:
ResultSet rset_;
@ -139,6 +195,16 @@ namespace Opm { namespace ecl { namespace OutputStream {
/// place output indicator at end of file (i.e, simple append).
void openExisting(const std::string& fname,
const std::streampos writePos);
/// Access writable output stream.
///
/// Must not be called prior to \c prepareStep.
EclOutput& stream();
/// Implementation function for public \c write overload set.
template <typename T>
void writeImpl(const std::string& kw,
const std::vector<T>& data);
};
/// Derive filename corresponding to output stream of particular result

View File

@ -158,10 +158,44 @@ void Opm::ecl::OutputStream::Restart::prepareStep(const int seqnum)
}
}
Opm::ecl::EclOutput&
Opm::ecl::OutputStream::Restart::stream()
void Opm::ecl::OutputStream::Restart::message(const std::string& msg)
{
return *this->stream_;
this->stream().message(msg);
}
void
Opm::ecl::OutputStream::Restart::
write(const std::string& kw, const std::vector<int>& data)
{
this->writeImpl(kw, data);
}
void
Opm::ecl::OutputStream::Restart::
write(const std::string& kw, const std::vector<bool>& data)
{
this->writeImpl(kw, data);
}
void
Opm::ecl::OutputStream::Restart::
write(const std::string& kw, const std::vector<float>& data)
{
this->writeImpl(kw, data);
}
void
Opm::ecl::OutputStream::Restart::
write(const std::string& kw, const std::vector<double>& data)
{
this->writeImpl(kw, data);
}
void
Opm::ecl::OutputStream::Restart::
write(const std::string& kw, const std::vector<std::string>& data)
{
this->writeImpl(kw, data);
}
void
@ -239,6 +273,24 @@ openExisting(const std::string& fname,
}
}
Opm::ecl::EclOutput&
Opm::ecl::OutputStream::Restart::stream()
{
return *this->stream_;
}
namespace Opm { namespace ecl { namespace OutputStream {
template <typename T>
void Restart::writeImpl(const std::string& kw,
const std::vector<T>& data)
{
this->stream().write(kw, data);
}
}}}
std::string
Opm::ecl::OutputStream::outputFileName(const ResultSet& rsetDescriptor,
const std::string& ext)

View File

@ -424,13 +424,11 @@ BOOST_AUTO_TEST_CASE(Unformatted_Unified)
rst.prepareStep(1);
auto& rstFile = rst.stream();
rstFile.write("I", std::vector<int> {1, 7, 2, 9});
rstFile.write("L", std::vector<bool> {true, false, false, true});
rstFile.write("S", std::vector<float> {3.1f, 4.1f, 59.265f});
rstFile.write("D", std::vector<double> {2.71, 8.21});
rstFile.write("Z", std::vector<std::string>{"W1", "W2"});
rst.write("I", std::vector<int> {1, 7, 2, 9});
rst.write("L", std::vector<bool> {true, false, false, true});
rst.write("S", std::vector<float> {3.1f, 4.1f, 59.265f});
rst.write("D", std::vector<double> {2.71, 8.21});
rst.write("Z", std::vector<std::string>{"W1", "W2"});
}
{
@ -438,13 +436,11 @@ BOOST_AUTO_TEST_CASE(Unformatted_Unified)
rst.prepareStep(13);
auto& rstFile = rst.stream();
rstFile.write("I", std::vector<int> {35, 51, 13});
rstFile.write("L", std::vector<bool> {true, true, true, false});
rstFile.write("S", std::vector<float> {17.29e-02f, 1.4142f});
rstFile.write("D", std::vector<double> {0.6931, 1.6180, 123.45e6});
rstFile.write("Z", std::vector<std::string>{"G1", "FIELD"});
rst.write("I", std::vector<int> {35, 51, 13});
rst.write("L", std::vector<bool> {true, true, true, false});
rst.write("S", std::vector<float> {17.29e-02f, 1.4142f});
rst.write("D", std::vector<double> {0.6931, 1.6180, 123.45e6});
rst.write("Z", std::vector<std::string>{"G1", "FIELD"});
}
{
@ -537,13 +533,11 @@ BOOST_AUTO_TEST_CASE(Unformatted_Unified)
rst.prepareStep(5); // Before 13. Should overwrite 13
auto& rstFile = rst.stream();
rstFile.write("I", std::vector<int> {1, 2, 3, 4});
rstFile.write("L", std::vector<bool> {false, false, false, true});
rstFile.write("S", std::vector<float> {1.23e-04f, 1.234e5f, -5.4321e-9f});
rstFile.write("D", std::vector<double> {0.6931, 1.6180});
rstFile.write("Z", std::vector<std::string>{"HELLO", ", ", "WORLD"});
rst.write("I", std::vector<int> {1, 2, 3, 4});
rst.write("L", std::vector<bool> {false, false, false, true});
rst.write("S", std::vector<float> {1.23e-04f, 1.234e5f, -5.4321e-9f});
rst.write("D", std::vector<double> {0.6931, 1.6180});
rst.write("Z", std::vector<std::string>{"HELLO", ", ", "WORLD"});
}
{
@ -637,13 +631,11 @@ BOOST_AUTO_TEST_CASE(Unformatted_Unified)
rst.prepareStep(13);
auto& rstFile = rst.stream();
rstFile.write("I", std::vector<int> {35, 51, 13});
rstFile.write("L", std::vector<bool> {true, true, true, false});
rstFile.write("S", std::vector<float> {17.29e-02f, 1.4142f});
rstFile.write("D", std::vector<double> {0.6931, 1.6180, 123.45e6});
rstFile.write("Z", std::vector<std::string>{"G1", "FIELD"});
rst.write("I", std::vector<int> {35, 51, 13});
rst.write("L", std::vector<bool> {true, true, true, false});
rst.write("S", std::vector<float> {17.29e-02f, 1.4142f});
rst.write("D", std::vector<double> {0.6931, 1.6180, 123.45e6});
rst.write("Z", std::vector<std::string>{"G1", "FIELD"});
}
{
@ -745,13 +737,11 @@ BOOST_AUTO_TEST_CASE(Formatted_Separate)
rst.prepareStep(1);
auto& rstFile = rst.stream();
rstFile.write("I", std::vector<int> {1, 7, 2, 9});
rstFile.write("L", std::vector<bool> {true, false, false, true});
rstFile.write("S", std::vector<float> {3.1f, 4.1f, 59.265f});
rstFile.write("D", std::vector<double> {2.71, 8.21});
rstFile.write("Z", std::vector<std::string>{"W1", "W2"});
rst.write("I", std::vector<int> {1, 7, 2, 9});
rst.write("L", std::vector<bool> {true, false, false, true});
rst.write("S", std::vector<float> {3.1f, 4.1f, 59.265f});
rst.write("D", std::vector<double> {2.71, 8.21});
rst.write("Z", std::vector<std::string>{"W1", "W2"});
}
{
@ -759,13 +749,11 @@ BOOST_AUTO_TEST_CASE(Formatted_Separate)
rst.prepareStep(13);
auto& rstFile = rst.stream();
rstFile.write("I", std::vector<int> {35, 51, 13});
rstFile.write("L", std::vector<bool> {true, true, true, false});
rstFile.write("S", std::vector<float> {17.29e-02f, 1.4142f});
rstFile.write("D", std::vector<double> {0.6931, 1.6180, 123.45e6});
rstFile.write("Z", std::vector<std::string>{"G1", "FIELD"});
rst.write("I", std::vector<int> {35, 51, 13});
rst.write("L", std::vector<bool> {true, true, true, false});
rst.write("S", std::vector<float> {17.29e-02f, 1.4142f});
rst.write("D", std::vector<double> {0.6931, 1.6180, 123.45e6});
rst.write("Z", std::vector<std::string>{"G1", "FIELD"});
}
{
@ -848,13 +836,11 @@ BOOST_AUTO_TEST_CASE(Formatted_Separate)
rst.prepareStep(5); // Separate output. Step 13 should be unaffected.
auto& rstFile = rst.stream();
rstFile.write("I", std::vector<int> {1, 2, 3, 4});
rstFile.write("L", std::vector<bool> {false, false, false, true});
rstFile.write("S", std::vector<float> {1.23e-04f, 1.234e5f, -5.4321e-9f});
rstFile.write("D", std::vector<double> {0.6931, 1.6180});
rstFile.write("Z", std::vector<std::string>{"HELLO", ", ", "WORLD"});
rst.write("I", std::vector<int> {1, 2, 3, 4});
rst.write("L", std::vector<bool> {false, false, false, true});
rst.write("S", std::vector<float> {1.23e-04f, 1.234e5f, -5.4321e-9f});
rst.write("D", std::vector<double> {0.6931, 1.6180});
rst.write("Z", std::vector<std::string>{"HELLO", ", ", "WORLD"});
}
{