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

Dynamicstate update equal
This commit is contained in:
Bård Skaflestad 2020-09-17 08:03:29 +02:00 committed by GitHub
commit 7a2350bc3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 42 deletions

View File

@ -21,10 +21,11 @@
#ifndef DYNAMICSTATE_HPP_
#define DYNAMICSTATE_HPP_
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <optional>
#include <stdexcept>
#include <utility>
#include <vector>
#include <opm/parser/eclipse/EclipseState/Schedule/TimeMap.hpp>
@ -155,50 +156,49 @@ class DynamicState {
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.
*/
void update_equal(size_t index, const T& value) {
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];
if (prev_value == value)
return;
//if (prev_value == value)
// return {};
while (true) {
if (this->m_data[index] != prev_value)
break;
return index;
this->m_data[index] = value;
index++;
if (index == this->m_data.size())
break;
return {};
}
}
/// Will return the index of the first occurence of @value, or
/// -1 if @value is not found.
int find(const T& value) const {
/// 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 -1;
if( iter == this->m_data.end() ) return {};
return std::distance( m_data.begin() , iter );
}
template<typename P>
int find_if(P&& pred) const {
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 -1;
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, or -1
/// if all values are == @value
int find_not(const T& value) const {
/// 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 -1;
if( iter == this->m_data.end() ) return {};
return std::distance( m_data.begin() , iter );
}

View File

@ -2618,8 +2618,8 @@ void Schedule::invalidNamePattern( const std::string& namePattern, std::size_t
for (const auto& well_pair : this->wells_static) {
const auto& well_name = well_pair.first;
const auto& dynamic_state = well_pair.second;
auto open_step = static_cast<std::size_t>(dynamic_state.find_not(nullptr));
if (open_step <= timeStep)
auto open_step = dynamic_state.find_not(nullptr);
if (open_step.value() <= timeStep)
names.push_back(well_name);
}
return names;

View File

@ -202,28 +202,29 @@ BOOST_AUTO_TEST_CASE( find ) {
Opm::TimeMap timeMap = make_timemap(6);
Opm::DynamicState<int> state(timeMap , 137);
BOOST_CHECK_EQUAL( state.find( 137 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 200 ) , -1 );
BOOST_CHECK_EQUAL( state.find_not(137), -1);
BOOST_CHECK_EQUAL( state.find_not(200), 0);
BOOST_CHECK_EQUAL( state.find( 137 ).value() , 0 );
BOOST_CHECK_EQUAL( state.find_not(200).value(), 0);
BOOST_CHECK( !state.find( 200 ));
BOOST_CHECK( !state.find_not(137));
state.update( 0 , 200 );
BOOST_CHECK_EQUAL( state.find( 137 ) , -1 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
BOOST_CHECK( !state.find( 137 ) );
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0 );
state.update( 2 , 300 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ) , 2 );
BOOST_CHECK_EQUAL( state.find_not( 200 ) , 2 );
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ).value() , 2 );
BOOST_CHECK_EQUAL( state.find_not( 200 ).value() , 2 );
state.update( 4 , 400 );
BOOST_CHECK_EQUAL( state.find( 200 ) , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ) , 2 );
BOOST_CHECK_EQUAL( state.find( 400 ) , 4 );
BOOST_CHECK_EQUAL( state.find( 500 ) , -1 );
BOOST_CHECK_EQUAL( state.find( 200 ).value() , 0 );
BOOST_CHECK_EQUAL( state.find( 300 ).value() , 2 );
BOOST_CHECK_EQUAL( state.find( 400 ).value() , 4 );
BOOST_CHECK( !state.find( 500 ));
auto pred = [] (const int& elm) { return elm == 400 ;};
BOOST_CHECK_EQUAL( state.find_if(pred), 4);
BOOST_CHECK_EQUAL( state.find_if(pred).value(), 4);
}
@ -265,15 +266,21 @@ BOOST_AUTO_TEST_CASE( update_equal ) {
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);
{
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() , 5);
}
state.update_equal(9,200);
BOOST_CHECK_EQUAL(state[8] , 100);
BOOST_CHECK_EQUAL(state[9] , 200);
BOOST_CHECK_EQUAL(state[10], 200);
{
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);
}
}