Skip to content

Commit

Permalink
Redo Public Interface of OutputStream::Restart
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bska committed May 27, 2019
1 parent 992d3b0 commit df1a011
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 55 deletions.
72 changes: 69 additions & 3 deletions opm/io/eclipse/OutputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ios>
#include <memory>
#include <string>
#include <vector>

namespace Opm { namespace ecl {

Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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
Expand Down
58 changes: 55 additions & 3 deletions src/opm/io/eclipse/OutputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
84 changes: 35 additions & 49 deletions tests/test_OutputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,27 +424,23 @@ 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"});
}

{
auto rst = ::Opm::ecl::OutputStream::Restart{ rset, fmt, unif };

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"});
}

{
Expand Down Expand Up @@ -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"});
}

{
Expand Down Expand Up @@ -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"});
}

{
Expand Down Expand Up @@ -745,27 +737,23 @@ 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"});
}

{
auto rst = ::Opm::ecl::OutputStream::Restart{ rset, fmt, unif };

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"});
}

{
Expand Down Expand Up @@ -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"});
}

{
Expand Down

0 comments on commit df1a011

Please sign in to comment.