Added DynamicState template class

This commit is contained in:
Joakim Hove
2013-11-05 12:56:51 +01:00
parent 61b640f00f
commit 6d30a6a7fa
4 changed files with 208 additions and 1 deletions

View File

@@ -0,0 +1,85 @@
/*
Copyright 2013 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DYNAMICSTATE_HPP_
#define DYNAMICSTATE_HPP_
#include <opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/date_time.hpp>
#include <stdexcept>
namespace Opm {
template <class T>
class DynamicState {
public:
DynamicState(const TimeMapConstPtr timeMap, T defaultValue) {
m_timeMap = timeMap;
m_currentValue = defaultValue;
}
T get(size_t index) const {
if (index >= m_timeMap->size())
throw std::range_error("Index value is out range.");
if (index >= m_data.size())
return m_currentValue;
return m_data[index];
}
void add(size_t index , T value) {
if (index >= (m_timeMap->size() - 1))
throw std::range_error("Index value is out range - can not add elements to the last timestep.");
if (m_data.size() > 0) {
if (index < (m_data.size() - 1))
throw std::invalid_argument("Elements must be added in weakly increasing order");
}
{
size_t currentSize = m_data.size();
if (currentSize <= index) {
for (size_t i = currentSize; i <= index; i++)
m_data.push_back( m_currentValue );
}
}
m_data[index] = value;
m_currentValue = value;
}
private:
std::vector<T> m_data;
TimeMapConstPtr m_timeMap;
T m_currentValue;
};
}
#endif

View File

@@ -6,3 +6,13 @@ add_test(NAME runTimeMapTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${
add_executable(runScheduleTests ScheduleTests.cpp)
target_link_libraries(runScheduleTests Parser ${Boost_LIBRARIES})
add_test(NAME runScheduleTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runScheduleTests )
add_executable(runWellTests WellTests.cpp)
target_link_libraries(runWellTests Parser ${Boost_LIBRARIES})
add_test(NAME runWellTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runWellTests )
add_executable(runDynamicStateTests DynamicStateTests.cpp)
target_link_libraries(runDynamicStateTests Parser ${Boost_LIBRARIES})
add_test(NAME runDynamicStateTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${EXECUTABLE_OUTPUT_PATH}/runDynamicStateTests )

View File

@@ -0,0 +1,111 @@
/*
Copyright 2013 Statoil ASA.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include <iostream>
#include <boost/filesystem.hpp>
#define BOOST_TEST_MODULE DynamicStateTests
#include <boost/test/unit_test.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/Well.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/DynamicState.hpp>
BOOST_AUTO_TEST_CASE(CreateDynamicTest) {
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
Opm::TimeMapPtr timeMap(new Opm::TimeMap(startDate));
Opm::DynamicState<double> state(timeMap , 9.99);
}
BOOST_AUTO_TEST_CASE(DynamicStateGetOutOfRangeThrows) {
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
Opm::TimeMapPtr timeMap(new Opm::TimeMap(startDate));
Opm::DynamicState<double> state(timeMap , 9.99);
BOOST_CHECK_THROW( state.get(1) , std::range_error);
}
BOOST_AUTO_TEST_CASE(DynamicStateGetDefault) {
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
Opm::TimeMapPtr timeMap(new Opm::TimeMap(startDate));
Opm::DynamicState<int> state(timeMap , 137);
BOOST_CHECK_EQUAL( 137 , state.get(0));
}
BOOST_AUTO_TEST_CASE(DynamicStateSetOutOfRangeThrows) {
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
Opm::TimeMapPtr timeMap(new Opm::TimeMap(startDate));
Opm::DynamicState<int> state(timeMap , 137);
for (size_t i = 0; i < 2; i++)
timeMap->addTStep( boost::posix_time::hours( (i+1) * 24 ));
BOOST_CHECK_THROW( state.add(2 , 100) , std::range_error);
}
BOOST_AUTO_TEST_CASE(DynamicStateSetOK) {
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
Opm::TimeMapPtr timeMap(new Opm::TimeMap(startDate));
Opm::DynamicState<int> state(timeMap , 137);
for (size_t i = 0; i < 10; i++)
timeMap->addTStep( boost::posix_time::hours( (i+1) * 24 ));
state.add(2 , 23 );
BOOST_CHECK_EQUAL( 137 , state.get(0));
BOOST_CHECK_EQUAL( 137 , state.get(1));
BOOST_CHECK_EQUAL( 23 , state.get(2));
BOOST_CHECK_EQUAL( 23 , state.get(5));
state.add(2 , 17);
BOOST_CHECK_EQUAL( 137 , state.get(0));
BOOST_CHECK_EQUAL( 137 , state.get(1));
BOOST_CHECK_EQUAL( 17 , state.get(2));
BOOST_CHECK_EQUAL( 17 , state.get(5));
state.add(6 , 60);
BOOST_CHECK_EQUAL( 17 , state.get(2));
BOOST_CHECK_EQUAL( 17 , state.get(5));
BOOST_CHECK_EQUAL( 60 , state.get(6));
BOOST_CHECK_EQUAL( 60 , state.get(8));
BOOST_CHECK_EQUAL( 60 , state.get(9));
}
BOOST_AUTO_TEST_CASE(DynamicStateAddIndexAlreadySetThrows) {
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
Opm::TimeMapPtr timeMap(new Opm::TimeMap(startDate));
Opm::DynamicState<int> state(timeMap , 137);
for (size_t i = 0; i < 10; i++)
timeMap->addTStep( boost::posix_time::hours( (i+1) * 24 ));
state.add( 5 , 60);
BOOST_CHECK_THROW( state.add(3 , 78) , std::invalid_argument);
}