diff --git a/opm/common/data/SimulationDataContainer.cpp b/opm/common/data/SimulationDataContainer.cpp index 7901e1696..9cabf925b 100644 --- a/opm/common/data/SimulationDataContainer.cpp +++ b/opm/common/data/SimulationDataContainer.cpp @@ -17,7 +17,7 @@ along with OPM. If not, see . */ - +#include #include namespace Opm { @@ -96,12 +96,51 @@ namespace Opm { return iter->second; } + void SimulationDataContainer::registerFaceData( const std::string& name , size_t components , double initialValue) { if (!hasFaceData( name )) { m_face_data.insert( std::pair>( name , std::vector(components * m_num_faces , initialValue ))); } } + bool SimulationDataContainer::equal( const SimulationDataContainer& other ) const { + if ((m_num_cells != other.m_num_cells) || + (m_num_phases != other.m_num_phases) || + (m_num_faces != other.m_num_faces)) + return false; + + if ((m_face_data.size() != other.m_face_data.size()) || + (m_cell_data.size() != other.m_cell_data.size())) + return false; + + for (const auto& cell_data : m_cell_data) { + const auto key = cell_data.first; + const auto data = cell_data.second; + + if (other.hasCellData( key )) { + const auto& other_data = other.getCellData( key ); + if (!cmp::double_vector_equal( data , other_data )) + return false; + } else + return false; + } + + for (const auto& face_data : m_face_data) { + const auto key = face_data.first; + const auto data = face_data.second; + + if (other.hasFaceData( key )) { + const auto& other_data = other.getFaceData( key ); + if (!cmp::double_vector_equal( data , other_data )) + return false; + } else + return false; + } + + return true; + } + + /* This is very deprecated. */ void SimulationDataContainer::addDefaultFields() { diff --git a/opm/common/data/SimulationDataContainer.hpp b/opm/common/data/SimulationDataContainer.hpp index da814dbaf..51f1fb396 100644 --- a/opm/common/data/SimulationDataContainer.hpp +++ b/opm/common/data/SimulationDataContainer.hpp @@ -27,6 +27,21 @@ namespace Opm { + + /// The SimulationDataContainer is a simple container to manage + /// simulation data. The container is instantiated with information + /// of how many cells, faces and phases are present in the + /// reservoirmodel. You can then add data to the container by using the + /// + /// registerCellData() + /// registerFaceData() + /// + /// functions. The container owns and manages the data, but + /// mutable references are returned with the getCellData() and + /// getFaceData() methods, and the content will typically be + /// modified by external scope. + + class SimulationDataContainer { public: SimulationDataContainer(size_t num_cells, size_t num_faces , size_t num_phases); @@ -36,14 +51,19 @@ namespace Opm { size_t numCells() const; bool hasCellData( const std::string& name ) const; + /// Will register a data vector of size numCells() * /// components. void registerCellData( const std::string& name , size_t components , double initialValue = 0.0 ); std::vector& getCellData( const std::string& name ); + const std::vector& getCellData( const std::string& name ) const; bool hasFaceData( const std::string& name ) const; void registerFaceData( const std::string& name , size_t components , double initialValue = 0.0 ); std::vector& getFaceData( const std::string& name ); + const std::vector& getFaceData( const std::string& name ) const; + + bool equal(const SimulationDataContainer& other) const; /* Old deprecated */ diff --git a/tests/test_SimulationDataContainer.cpp b/tests/test_SimulationDataContainer.cpp index 73baab131..3aaf803b1 100644 --- a/tests/test_SimulationDataContainer.cpp +++ b/tests/test_SimulationDataContainer.cpp @@ -113,3 +113,63 @@ BOOST_AUTO_TEST_CASE(TestRegisterCellData) { } +BOOST_AUTO_TEST_CASE(Test_Equal) { + { + SimulationDataContainer container1(100 , 10 , 2); + SimulationDataContainer container2(100 , 10 , 2); + BOOST_CHECK( container1.equal( container2 )); + } + + { + SimulationDataContainer container1(100 , 10 , 2); + SimulationDataContainer container2(100 , 10 , 1); + BOOST_CHECK( !container1.equal( container2 )); + } + + { + SimulationDataContainer container1(100 , 10 , 2); + SimulationDataContainer container2(100 , 10 , 2); + + container1.registerCellData( "FIELDX" , 1 , 123 ); + BOOST_CHECK( !container1.equal( container2 )); + container2.registerCellData( "FIELDX" , 1 , 123 ); + BOOST_CHECK( container1.equal( container2 )); + + container1.registerFaceData( "FACEX" , 1 , 123 ); + BOOST_CHECK( !container1.equal( container2 )); + container2.registerFaceData( "FACEX" , 1 , 123 ); + BOOST_CHECK( container1.equal( container2 )); + } + + { + SimulationDataContainer container1(100 , 10 , 2); + SimulationDataContainer container2(100 , 10 , 2); + + container1.registerCellData( "FIELD1" , 1 , 123 ); + container2.registerCellData( "FIELD2" , 1 , 123 ); + BOOST_CHECK( !container1.equal( container2 )); + } + + { + SimulationDataContainer container1(100 , 10 , 2); + SimulationDataContainer container2(100 , 10 , 2); + + container1.registerFaceData( "FIELD1" , 1 , 123 ); + container2.registerFaceData( "FIELD2" , 1 , 123 ); + BOOST_CHECK( !container1.equal( container2 )); + } + + { + SimulationDataContainer container1(100 , 10 , 2); + SimulationDataContainer container2(100 , 10 , 2); + + container1.registerFaceData( "FIELD1" , 1 , 123 ); + container2.registerFaceData( "FIELD1" , 1 , 123 ); + BOOST_CHECK( container1.equal( container2 )); + + std::vector& f = container1.getFaceData( "FIELD1" ); + f[0] *= 1.1; + BOOST_CHECK( !container1.equal( container2 )); + } +} +