diff --git a/opm/common/data/SimulationDataContainer.cpp b/opm/common/data/SimulationDataContainer.cpp
index 9cabf925b..e799931c7 100644
--- a/opm/common/data/SimulationDataContainer.cpp
+++ b/opm/common/data/SimulationDataContainer.cpp
@@ -17,6 +17,7 @@
along with OPM. If not, see .
*/
+#include
#include
#include
@@ -75,6 +76,39 @@ namespace Opm {
}
+ void SimulationDataContainer::setCellDataComponent( const std::string& key ,
+ size_t component ,
+ const std::vector& cells ,
+ const std::vector& values) {
+ auto& data = getCellData( key );
+ if (component >= m_num_phases)
+ OPM_THROW(std::invalid_argument, "The component number: " << component << " is invalid");
+
+
+ if (cells.size() != values.size())
+ OPM_THROW(std::invalid_argument, "size mismatch between cells and values");
+
+ // This is currently quite broken; the setCellDataComponent
+ // method assumes that the number of components in the field
+ // we are currently focusing on has num_phases components in
+ // total. This restriction should be lifted by allowing a per
+ // field number of components.
+
+ if (data.size() != m_num_phases * m_num_cells)
+ OPM_THROW(std::invalid_argument , "Can currently only be used on fields with num_components == num_phases (i.e. saturation...) ");
+
+
+ for (size_t i = 0; i < cells.size(); i++) {
+ if (cells[i] < m_num_cells) {
+ auto field_index = cells[i] * m_num_phases + component;
+ data[field_index] = values[i];
+ } else {
+ OPM_THROW(std::invalid_argument , "The cell number: " << cells[i] << " is invalid.");
+ }
+ }
+ }
+
+
bool SimulationDataContainer::hasFaceData( const std::string& name ) const {
return ( m_face_data.find( name ) == m_face_data.end() ? false : true );
}
diff --git a/opm/common/data/SimulationDataContainer.hpp b/opm/common/data/SimulationDataContainer.hpp
index 51f1fb396..e89965eb3 100644
--- a/opm/common/data/SimulationDataContainer.hpp
+++ b/opm/common/data/SimulationDataContainer.hpp
@@ -66,6 +66,11 @@ namespace Opm {
bool equal(const SimulationDataContainer& other) const;
+ /// Will set the values of component nr @component in the
+ /// field @key. All the cells in @cells will be set to the
+ /// values in @values.
+ void setCellDataComponent( const std::string& key , size_t component , const std::vector& cells , const std::vector& values);
+
/* Old deprecated */
std::vector& pressure ();
std::vector& temperature ();
diff --git a/tests/test_SimulationDataContainer.cpp b/tests/test_SimulationDataContainer.cpp
index 3aaf803b1..f07c2ff75 100644
--- a/tests/test_SimulationDataContainer.cpp
+++ b/tests/test_SimulationDataContainer.cpp
@@ -23,7 +23,7 @@
#include
#include
-
+#include
#include
using namespace Opm;
@@ -173,3 +173,33 @@ BOOST_AUTO_TEST_CASE(Test_Equal) {
}
}
+
+
+BOOST_AUTO_TEST_CASE(TestSetComponent) {
+
+ SimulationDataContainer container(100 , 10 , 2);
+ container.registerCellData("FIELDX" , 2 , 123 );
+ std::vector cells = { 1,2,3};
+ std::vector cells2 = { 1,2,3,4};
+ std::vector cells3 = { 1,2,100};
+ std::vector values0 = {20,30,40};
+ std::vector values1 = {2,3,4};
+
+ BOOST_CHECK_THROW( container.setCellDataComponent( "FIELDY" , 0 , cells , values0 ) , std::invalid_argument );
+ BOOST_CHECK_THROW( container.setCellDataComponent( "FIELDX" , 2 , cells , values0 ) , std::invalid_argument );
+ BOOST_CHECK_THROW( container.setCellDataComponent( "FIELDX" , 0 , cells2 , values0 ) , std::invalid_argument );
+ BOOST_CHECK_THROW( container.setCellDataComponent( "FIELDX" , 0 , cells3 , values0 ) , std::invalid_argument );
+
+ container.setCellDataComponent( "FIELDX" , 0 , cells , values0 );
+ container.setCellDataComponent( "FIELDX" , 1 , cells , values1 );
+ const auto& data = container.getCellData( "FIELDX" );
+
+ BOOST_CHECK_EQUAL( data[1*2 + 1] , 2 );
+ BOOST_CHECK_EQUAL( data[2*2 + 1] , 3 );
+ BOOST_CHECK_EQUAL( data[3*2 + 1] , 4 );
+
+ BOOST_CHECK_EQUAL( data[1*2] , 20 );
+ BOOST_CHECK_EQUAL( data[2*2] , 30 );
+ BOOST_CHECK_EQUAL( data[3*2] , 40 );
+
+}