Remove functionality from DynamicState
This commit is contained in:
@@ -57,7 +57,6 @@ namespace Opm {
|
||||
|
||||
template< class T >
|
||||
class DynamicState {
|
||||
friend class Schedule;
|
||||
public:
|
||||
typedef typename std::vector< T >::iterator iterator;
|
||||
|
||||
@@ -81,46 +80,8 @@ class DynamicState {
|
||||
return m_data.back();
|
||||
}
|
||||
|
||||
const T& at( size_t index ) const {
|
||||
return this->m_data.at( index );
|
||||
}
|
||||
|
||||
const T& operator[](size_t index) const {
|
||||
return this->at( index );
|
||||
}
|
||||
|
||||
const T& get(size_t index) const {
|
||||
return this->at( index );
|
||||
}
|
||||
|
||||
void updateInitial( T initial ) {
|
||||
std::fill_n( this->m_data.begin(), this->initial_range, initial );
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::pair<std::size_t, T>> unique() const {
|
||||
if (this->m_data.empty())
|
||||
return {};
|
||||
|
||||
const auto * current_value = std::addressof(this->m_data[0]);
|
||||
std::size_t current_index = 0;
|
||||
std::vector<std::pair<std::size_t, T>> result{{current_index, *current_value}};
|
||||
while (true) {
|
||||
if (this->m_data[current_index] != *current_value) {
|
||||
current_value = std::addressof(this->m_data[current_index]);
|
||||
result.emplace_back(current_index, *current_value);
|
||||
}
|
||||
|
||||
current_index++;
|
||||
if (current_index == this->m_data.size())
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool is_new_data(size_t index) const {
|
||||
return index == 0 || (at(index) != at(index - 1));
|
||||
return this->m_data.at( index );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,95 +103,9 @@ class DynamicState {
|
||||
return true;
|
||||
}
|
||||
|
||||
void update_elm( size_t index, const T& value ) {
|
||||
if (this->m_data.size() <= index)
|
||||
throw std::out_of_range("Invalid index for update_elm()");
|
||||
|
||||
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].
|
||||
|
||||
The return value is the index of the first element different from value,
|
||||
or an empty optional if there is no such element.
|
||||
*/
|
||||
std::optional<std::size_t> 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];
|
||||
auto update_end = std::find_if(this->m_data.begin() + index, this->m_data.end(), [&prev_value](const T& v) { return v != prev_value; });
|
||||
std::fill(this->m_data.begin() + index, update_end, value);
|
||||
if (update_end == this->m_data.end())
|
||||
return {};
|
||||
|
||||
return std::distance(this->m_data.begin(), update_end);
|
||||
}
|
||||
|
||||
|
||||
void update_range(std::size_t start_index, std::size_t end_index, const T& value) {
|
||||
if (end_index < start_index)
|
||||
throw std::invalid_argument("Must have growing index");
|
||||
|
||||
if (end_index > this->m_data.size())
|
||||
throw std::invalid_argument("Invalid range");
|
||||
|
||||
std::fill(this->m_data.begin() + start_index, this->m_data.begin() + end_index, value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Will return the index of the first occurence of @value
|
||||
std::optional<std::size_t> find(const T& value) const {
|
||||
auto iter = std::find( m_data.begin() , m_data.end() , value);
|
||||
if( iter == this->m_data.end() ) return {};
|
||||
|
||||
return std::distance( m_data.begin() , iter );
|
||||
}
|
||||
|
||||
template<typename P>
|
||||
std::optional<std::size_t> find_if(P&& pred) const {
|
||||
auto iter = std::find_if(m_data.begin(), m_data.end(), std::forward<P>(pred));
|
||||
if( iter == this->m_data.end() ) return {};
|
||||
|
||||
return std::distance( m_data.begin() , iter );
|
||||
}
|
||||
|
||||
/// Will return the index of the first value which is != @value
|
||||
std::optional<std::size_t> find_not(const T& value) const {
|
||||
auto iter = std::find_if_not( m_data.begin() , m_data.end() , [&value] (const T& elm) { return value == elm; });
|
||||
if( iter == this->m_data.end() ) return {};
|
||||
|
||||
return std::distance( m_data.begin() , iter );
|
||||
}
|
||||
|
||||
iterator begin() {
|
||||
return this->m_data.begin();
|
||||
}
|
||||
|
||||
|
||||
iterator end() {
|
||||
return this->m_data.end();
|
||||
}
|
||||
|
||||
|
||||
std::size_t size() const {
|
||||
return this->m_data.size();
|
||||
}
|
||||
|
||||
const std::vector<T>& data() const {
|
||||
return m_data;
|
||||
}
|
||||
|
||||
size_t initialRange() const {
|
||||
return initial_range;
|
||||
}
|
||||
|
||||
bool operator==(const DynamicState<T>& data) const {
|
||||
return m_data == data.m_data &&
|
||||
|
||||
@@ -584,7 +584,7 @@ RestartConfig::RestartConfig( const Deck& deck, const std::pair<std::time_t, std
|
||||
|
||||
|
||||
const std::map< std::string, int >& RestartConfig::getRestartKeywords( size_t timestep ) const {
|
||||
return restart_keywords.at( timestep );
|
||||
return restart_keywords.get( timestep );
|
||||
}
|
||||
|
||||
int RestartConfig::getKeyword( const std::string& keyword, size_t timeStep) const {
|
||||
|
||||
@@ -98,83 +98,23 @@ BOOST_AUTO_TEST_CASE(DynamicStateSetOK) {
|
||||
BOOST_CHECK_EQUAL( 60 , state.back());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DynamicStateAddAt) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
Opm::DynamicState<int> state(timeMap , 0);
|
||||
|
||||
state.update( 10 , 77 );
|
||||
{
|
||||
const int& v1 = state.at(10);
|
||||
int v2 = state.get(10);
|
||||
BOOST_CHECK_EQUAL( v1 , 77 );
|
||||
BOOST_CHECK_EQUAL( v1 , v2 );
|
||||
BOOST_CHECK( &v1 != &v2 );
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DynamicStateOperatorSubscript) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
Opm::DynamicState<int> state(timeMap , 137);
|
||||
|
||||
state.update( 10 , 200 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 137 );
|
||||
BOOST_CHECK_EQUAL( state[0] , 137 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(DynamicStateInitial) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
Opm::DynamicState<int> state(timeMap , 137);
|
||||
Opm::DynamicState<int> state2(timeMap , 137);
|
||||
|
||||
state.update( 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.update( 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 );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( ResetGlobal ) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
Opm::DynamicState<int> state(timeMap , 137);
|
||||
|
||||
state.update(5 , 100);
|
||||
BOOST_CHECK_EQUAL( state[0] , 137 );
|
||||
BOOST_CHECK_EQUAL( state[4] , 137 );
|
||||
BOOST_CHECK_EQUAL( state[5] , 100 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 100 );
|
||||
BOOST_CHECK_EQUAL( state.get(0) , 137 );
|
||||
BOOST_CHECK_EQUAL( state.get(4) , 137 );
|
||||
BOOST_CHECK_EQUAL( state.get(5) , 100 );
|
||||
BOOST_CHECK_EQUAL( state.get(9) , 100 );
|
||||
|
||||
state.updateInitial( 22 );
|
||||
BOOST_CHECK_EQUAL( state[0] , 22 );
|
||||
BOOST_CHECK_EQUAL( state[4] , 22 );
|
||||
BOOST_CHECK_EQUAL( state[5] , 100 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 100 );
|
||||
|
||||
state.globalReset( 88 );
|
||||
BOOST_CHECK_EQUAL( state[0] , 88 );
|
||||
BOOST_CHECK_EQUAL( state[4] , 88 );
|
||||
BOOST_CHECK_EQUAL( state[5] , 88 );
|
||||
BOOST_CHECK_EQUAL( state[9] , 88 );
|
||||
BOOST_CHECK_EQUAL( state.get(0) , 88 );
|
||||
BOOST_CHECK_EQUAL( state.get(4) , 88 );
|
||||
BOOST_CHECK_EQUAL( state.get(5) , 88 );
|
||||
BOOST_CHECK_EQUAL( state.get(9) , 88 );
|
||||
}
|
||||
|
||||
|
||||
@@ -188,129 +128,5 @@ BOOST_AUTO_TEST_CASE( CheckReturn ) {
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( UpdateEmptyInitial ) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
Opm::DynamicState<int> state(timeMap , 137);
|
||||
|
||||
BOOST_CHECK_EQUAL( state[5] , 137 );
|
||||
state.updateInitial( 99 );
|
||||
BOOST_CHECK_EQUAL( state[5] , 99 );
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( find ) {
|
||||
Opm::TimeMap timeMap = make_timemap(6);
|
||||
Opm::DynamicState<int> state(timeMap , 137);
|
||||
|
||||
BOOST_CHECK_EQUAL( state.find( 137 ).value() , 0U );
|
||||
BOOST_CHECK_EQUAL( state.find_not(200).value(), 0U);
|
||||
BOOST_CHECK( !state.find( 200 ));
|
||||
BOOST_CHECK( !state.find_not(137));
|
||||
|
||||
state.update( 0 , 200 );
|
||||
BOOST_CHECK( !state.find( 137 ) );
|
||||
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0U );
|
||||
|
||||
state.update( 2 , 300 );
|
||||
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0U );
|
||||
BOOST_CHECK_EQUAL( state.find( 300 ).value() , 2U );
|
||||
BOOST_CHECK_EQUAL( state.find_not( 200 ).value() , 2U );
|
||||
|
||||
state.update( 4 , 400 );
|
||||
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0U );
|
||||
BOOST_CHECK_EQUAL( state.find( 300 ).value() , 2U );
|
||||
BOOST_CHECK_EQUAL( state.find( 400 ).value() , 4U );
|
||||
BOOST_CHECK( !state.find( 500 ));
|
||||
|
||||
|
||||
auto pred = [] (const int& elm) { return elm == 400 ;};
|
||||
BOOST_CHECK_EQUAL( state.find_if(pred).value(), 4U);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( update_elm ) {
|
||||
Opm::TimeMap timeMap = make_timemap(6);
|
||||
Opm::DynamicState<int> state(timeMap , 137);
|
||||
state.update( 5, 88 );
|
||||
BOOST_CHECK_THROW( state.update_elm(10,88) , std::out_of_range );
|
||||
BOOST_CHECK_EQUAL( state[2],137 );
|
||||
BOOST_CHECK_EQUAL( state[3],137 );
|
||||
BOOST_CHECK_EQUAL( state[4],137 );
|
||||
|
||||
state.update_elm(3,88);
|
||||
BOOST_CHECK_EQUAL( state[2],137 );
|
||||
BOOST_CHECK_EQUAL( state[3],88 );
|
||||
BOOST_CHECK_EQUAL( state[4],137 );
|
||||
|
||||
for (auto& v : state)
|
||||
v += 2;
|
||||
|
||||
BOOST_CHECK_EQUAL( state[2],139 );
|
||||
BOOST_CHECK_EQUAL( state[3],90 );
|
||||
BOOST_CHECK_EQUAL( state[4],139 );
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE( update_equal ) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
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);
|
||||
|
||||
{
|
||||
auto next_index = state.update_equal(4,50);
|
||||
BOOST_CHECK_EQUAL(state[4], 50);
|
||||
BOOST_CHECK_EQUAL(state[5], 100);
|
||||
BOOST_CHECK_EQUAL(next_index.value() , 5U);
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto next_index = state.update_equal(9,200);
|
||||
BOOST_CHECK_EQUAL(state[8] , 100);
|
||||
BOOST_CHECK_EQUAL(state[9] , 200);
|
||||
BOOST_CHECK_EQUAL(state[10], 200);
|
||||
BOOST_CHECK(!next_index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( update_range) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
Opm::DynamicState<int> state(timeMap , 0);
|
||||
|
||||
BOOST_CHECK_THROW( state.update_range(5,1,99), std::exception);
|
||||
BOOST_CHECK_THROW( state.update_range(10,200,99), std::exception);
|
||||
state.update_range(3,5,99);
|
||||
BOOST_CHECK_EQUAL( state[3], 99);
|
||||
BOOST_CHECK_EQUAL( state[4], 99);
|
||||
BOOST_CHECK_EQUAL( state[5], 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE( UNIQUE ) {
|
||||
Opm::TimeMap timeMap = make_timemap(11);
|
||||
Opm::DynamicState<int> state(timeMap , 13);
|
||||
auto unique0 = state.unique();
|
||||
BOOST_CHECK_EQUAL(unique0.size(), 1U);
|
||||
BOOST_CHECK(unique0[0] == std::make_pair(std::size_t{0}, 13));
|
||||
|
||||
state.update(3,300);
|
||||
state.update(6,600);
|
||||
auto unique1 = state.unique();
|
||||
BOOST_CHECK_EQUAL(unique1.size(), 3U);
|
||||
BOOST_CHECK(unique1[0] == std::make_pair(std::size_t{0}, 13));
|
||||
BOOST_CHECK(unique1[1] == std::make_pair(std::size_t{3}, 300));
|
||||
BOOST_CHECK(unique1[2] == std::make_pair(std::size_t{6}, 600));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user