Merge pull request #76 from joakim-hove/set-state-component

Added SimulationDataContainer::setCellDataComponent()
This commit is contained in:
Atgeirr Flø Rasmussen 2016-02-19 10:43:12 +01:00
commit b8d18a6909
3 changed files with 70 additions and 1 deletions

View File

@ -17,6 +17,7 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opm/common/ErrorMacros.hpp>
#include <opm/common/util/numeric/cmp.hpp>
#include <opm/common/data/SimulationDataContainer.hpp>
@ -75,6 +76,39 @@ namespace Opm {
}
void SimulationDataContainer::setCellDataComponent( const std::string& key ,
size_t component ,
const std::vector<int>& cells ,
const std::vector<double>& 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 );
}

View File

@ -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<int>& cells , const std::vector<double>& values);
/* Old deprecated */
std::vector<double>& pressure ();
std::vector<double>& temperature ();

View File

@ -23,7 +23,7 @@
#include <boost/test/unit_test.hpp>
#include <stdexcept>
#include <iostream>
#include <opm/common/data/SimulationDataContainer.hpp>
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<int> cells = { 1,2,3};
std::vector<int> cells2 = { 1,2,3,4};
std::vector<int> cells3 = { 1,2,100};
std::vector<double> values0 = {20,30,40};
std::vector<double> 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 );
}