Merge pull request #561 from joakim-hove/dynamicstate-update-equal

Add method update_equal() to DynamicState
This commit is contained in:
Joakim Hove
2018-11-16 09:43:25 +01:00
committed by GitHub
3 changed files with 60 additions and 2 deletions

View File

@@ -114,6 +114,35 @@ class DynamicState {
this->m_data[index] = value;
}
/*
Will assign all currently equal values starting at index with the new
value. Purpose of the method is to support manipulations of an existing
Schedule object, if e.g. a well is initially closed in the interval
[T1,T2] and then opened at time T1 < Tx < T2 then the open should be
applied for all times in the range [Tx,T2].
*/
void update_equal(size_t index, const T& value) {
if (this->m_data.size() <= index)
throw std::out_of_range("Invalid index for update_equal()");
const T prev_value = this->m_data[index];
if (prev_value == value)
return;
while (true) {
if (this->m_data[index] != prev_value)
break;
this->m_data[index] = value;
index++;
if (index == this->m_data.size())
break;
}
}
/// Will return the index of the first occurence of @value, or
/// -1 if @value is not found.
int find(const T& value) const {

View File

@@ -27,8 +27,6 @@
#include <opm/parser/eclipse/Deck/Deck.hpp>
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
#include <opm/parser/eclipse/Deck/Section.hpp>
#include <opm/parser/eclipse/Parser/Parser.hpp>
#include <opm/parser/eclipse/Parser/ParserKeyword.hpp>
namespace Opm {

View File

@@ -273,3 +273,34 @@ BOOST_AUTO_TEST_CASE( update_elm ) {
BOOST_CHECK_EQUAL( state[3],90 );
BOOST_CHECK_EQUAL( state[4],139 );
}
BOOST_AUTO_TEST_CASE( update_equal ) {
const std::time_t startDate = Opm::TimeMap::mkdate(2010, 1, 1);
Opm::TimeMap timeMap{ startDate };
for (size_t i = 0; i < 10; i++)
timeMap.addTStep((i+1) * 24 * 60 * 60);
Opm::DynamicState<int> state(timeMap , 0);
state.update( 5, 100 );
BOOST_REQUIRE_THROW(state.update_equal(100, 100), std::out_of_range);
BOOST_CHECK_EQUAL(state[0], 0);
BOOST_CHECK_EQUAL(state[4], 0);
BOOST_CHECK_EQUAL(state[5], 100);
state.update_equal(3,50);
BOOST_CHECK_EQUAL(state[2], 0);
BOOST_CHECK_EQUAL(state[3], 50);
BOOST_CHECK_EQUAL(state[4], 50);
BOOST_CHECK_EQUAL(state[5], 100);
state.update_equal(4,50);
BOOST_CHECK_EQUAL(state[4], 50);
BOOST_CHECK_EQUAL(state[5], 100);
state.update_equal(9,200);
BOOST_CHECK_EQUAL(state[8] , 100);
BOOST_CHECK_EQUAL(state[9] , 200);
BOOST_CHECK_EQUAL(state[10], 200);
}