Merge pull request #456 from joakim-hove/Dynamicstate-initial
Dynamicstate initial
This commit is contained in:
@@ -33,9 +33,11 @@ namespace Opm {
|
||||
class DynamicState {
|
||||
public:
|
||||
|
||||
DynamicState(const TimeMapConstPtr timeMap, T defaultValue) {
|
||||
DynamicState(const TimeMapConstPtr timeMap, T initialValue) {
|
||||
m_timeMap = timeMap;
|
||||
m_currentValue = defaultValue;
|
||||
m_currentValue = initialValue;
|
||||
m_initialValue = initialValue;
|
||||
m_initialRange = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +52,11 @@ namespace Opm {
|
||||
}
|
||||
|
||||
|
||||
const T& operator[](size_t index) const {
|
||||
return at(index);
|
||||
}
|
||||
|
||||
|
||||
T get(size_t index) const {
|
||||
if (index >= m_timeMap->size())
|
||||
throw std::range_error("Index value is out range.");
|
||||
@@ -62,6 +69,16 @@ namespace Opm {
|
||||
|
||||
|
||||
|
||||
void updateInitial(T initialValue) {
|
||||
if (m_initialValue != initialValue) {
|
||||
size_t index;
|
||||
m_initialValue = initialValue;
|
||||
for (index = 0; index < m_initialRange; index++)
|
||||
m_data[index] = m_initialValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
size_t size() const {
|
||||
return m_data.size();
|
||||
}
|
||||
@@ -86,6 +103,8 @@ namespace Opm {
|
||||
|
||||
m_data[index] = value;
|
||||
m_currentValue = value;
|
||||
if (m_initialRange == 0)
|
||||
m_initialRange = index;
|
||||
}
|
||||
|
||||
|
||||
@@ -94,6 +113,8 @@ namespace Opm {
|
||||
std::vector<T> m_data;
|
||||
TimeMapConstPtr m_timeMap;
|
||||
T m_currentValue;
|
||||
T m_initialValue;
|
||||
size_t m_initialRange;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -146,3 +146,54 @@ BOOST_AUTO_TEST_CASE(DynamicStateCheckSize) {
|
||||
state.add( 6 , 10 );
|
||||
BOOST_CHECK_EQUAL( 7U , state.size() );
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DynamicStateOperatorSubscript) {
|
||||
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
|
||||
Opm::TimeMapPtr timeMap(new Opm::TimeMap(boost::posix_time::ptime(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( 10 , 200 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 137 );
|
||||
BOOST_CHECK_EQUAL( state[0] , 137 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DynamicStateInitial) {
|
||||
boost::gregorian::date startDate( 2010 , boost::gregorian::Jan , 1);
|
||||
Opm::TimeMapPtr timeMap(new Opm::TimeMap(boost::posix_time::ptime(startDate)));
|
||||
Opm::DynamicState<int> state(timeMap , 137);
|
||||
Opm::DynamicState<int> state2(timeMap , 137);
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
timeMap->addTStep( boost::posix_time::hours( (i+1) * 24 ));
|
||||
|
||||
|
||||
state.add( 10 , 200 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 137 );
|
||||
BOOST_CHECK_EQUAL( state[0] , 137 );
|
||||
BOOST_CHECK_EQUAL( state[10] , 200 );
|
||||
|
||||
state.updateInitial( 63 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 63 );
|
||||
BOOST_CHECK_EQUAL( state[0] , 63 );
|
||||
BOOST_CHECK_EQUAL( state[10] , 200 );
|
||||
|
||||
state.updateInitial( 73 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 73 );
|
||||
BOOST_CHECK_EQUAL( state[0] , 73 );
|
||||
BOOST_CHECK_EQUAL( state[10] , 200 );
|
||||
|
||||
|
||||
state2.add( 10 , 200 );
|
||||
BOOST_CHECK_EQUAL( state2[9] , 137 );
|
||||
BOOST_CHECK_EQUAL( state2[0] , 137 );
|
||||
BOOST_CHECK_EQUAL( state2[10] , 200 );
|
||||
state.updateInitial( 73 );
|
||||
BOOST_CHECK_EQUAL( state2[9] , 137 );
|
||||
BOOST_CHECK_EQUAL( state2[0] , 137 );
|
||||
BOOST_CHECK_EQUAL( state2[10] , 200 );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user