Added equal() method to SimulationDataContainer

This commit is contained in:
Joakim Hove 2016-02-06 11:17:16 +01:00
parent d2ab19f318
commit ee6836bd39
3 changed files with 120 additions and 1 deletions

View File

@ -17,7 +17,7 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>. along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <opm/common/util/numeric/cmp.hpp>
#include <opm/common/data/SimulationDataContainer.hpp> #include <opm/common/data/SimulationDataContainer.hpp>
namespace Opm { namespace Opm {
@ -96,12 +96,51 @@ namespace Opm {
return iter->second; return iter->second;
} }
void SimulationDataContainer::registerFaceData( const std::string& name , size_t components , double initialValue) { void SimulationDataContainer::registerFaceData( const std::string& name , size_t components , double initialValue) {
if (!hasFaceData( name )) { if (!hasFaceData( name )) {
m_face_data.insert( std::pair<std::string , std::vector<double>>( name , std::vector<double>(components * m_num_faces , initialValue ))); m_face_data.insert( std::pair<std::string , std::vector<double>>( name , std::vector<double>(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. */ /* This is very deprecated. */
void SimulationDataContainer::addDefaultFields() { void SimulationDataContainer::addDefaultFields() {

View File

@ -27,6 +27,21 @@
namespace Opm { 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 { class SimulationDataContainer {
public: public:
SimulationDataContainer(size_t num_cells, size_t num_faces , size_t num_phases); SimulationDataContainer(size_t num_cells, size_t num_faces , size_t num_phases);
@ -36,14 +51,19 @@ namespace Opm {
size_t numCells() const; size_t numCells() const;
bool hasCellData( const std::string& name ) const; bool hasCellData( const std::string& name ) const;
/// Will register a data vector of size numCells() * /// Will register a data vector of size numCells() *
/// components. /// components.
void registerCellData( const std::string& name , size_t components , double initialValue = 0.0 ); void registerCellData( const std::string& name , size_t components , double initialValue = 0.0 );
std::vector<double>& getCellData( const std::string& name ); std::vector<double>& getCellData( const std::string& name );
const std::vector<double>& getCellData( const std::string& name ) const;
bool hasFaceData( 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 ); void registerFaceData( const std::string& name , size_t components , double initialValue = 0.0 );
std::vector<double>& getFaceData( const std::string& name ); std::vector<double>& getFaceData( const std::string& name );
const std::vector<double>& getFaceData( const std::string& name ) const;
bool equal(const SimulationDataContainer& other) const;
/* Old deprecated */ /* Old deprecated */

View File

@ -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<double>& f = container1.getFaceData( "FIELD1" );
f[0] *= 1.1;
BOOST_CHECK( !container1.equal( container2 ));
}
}