Skip to content

Commit

Permalink
Merge pull request equinor#540 from stefoss23/well_ts_x_cpp
Browse files Browse the repository at this point in the history
Well ts cpp
  • Loading branch information
joakim-hove authored Nov 21, 2018
2 parents 3791ad1 + 6c4df1c commit 5f16ebf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 48 deletions.
3 changes: 2 additions & 1 deletion lib/ecl/tests/well_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ int main(int argc , char ** argv) {
well_info_load_rstfile( well_info , stringlist_iget(file_list , i), true);
well_info_free( well_info );
}

ecl_grid_free( grid );
free( grid_file );

exit(0);
}
78 changes: 32 additions & 46 deletions lib/ecl/well_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
#include <stdlib.h>
#include <stdbool.h>

#include <string>
#include <vector>
#include <algorithm>

#include <ert/util/util.h>
#include <ert/util/vector.hpp>

Expand All @@ -84,14 +88,14 @@ typedef struct {

struct well_ts_struct {
UTIL_TYPE_ID_DECLARATION;
char * well_name;
vector_type * ts;
std::string well_name;
std::vector<well_node_type*> ts;
};

/******************************************************************/

static well_node_type * well_node_alloc( well_state_type * well_state) {
well_node_type * node = (well_node_type*)util_malloc( sizeof * node );
well_node_type * node = new well_node_type();
UTIL_TYPE_ID_INIT( node , WELL_NODE_TYPE_ID );
node->report_nr = well_state_get_report_nr( well_state );
node->sim_time = well_state_get_sim_time( well_state );
Expand All @@ -100,41 +104,23 @@ static well_node_type * well_node_alloc( well_state_type * well_state) {
}


static UTIL_SAFE_CAST_FUNCTION( well_node , WELL_NODE_TYPE_ID )
static UTIL_SAFE_CAST_FUNCTION_CONST( well_node , WELL_NODE_TYPE_ID )


static void well_node_free( well_node_type * well_node ) {
well_state_free( well_node->well_state );
free( well_node );
}

static void well_node_free__( void * arg ) {
well_node_type * node = well_node_safe_cast( arg );
well_node_free( node );
delete well_node;
}

static int well_node_time_cmp( const void * arg1 , const void * arg2) {
const well_node_type * node1 = well_node_safe_cast_const( arg1 );
const well_node_type * node2 = well_node_safe_cast_const( arg2 );

if (node1->sim_time < node2->sim_time)
return -1;
else if (node1->sim_time == node2->sim_time)
return 0;
else
return 1;

static bool well_node_time_lt( const well_node_type * node1 , const well_node_type * node2) {
return (node1->sim_time < node2->sim_time);
}


/*****************************************************************/

static well_ts_type * well_ts_alloc_empty( ) {
well_ts_type * well_ts = (well_ts_type*)util_malloc( sizeof * well_ts );
well_ts_type * well_ts = new well_ts_type();
UTIL_TYPE_ID_INIT( well_ts , WELL_TS_TYPE_ID );

well_ts->ts = vector_alloc_new();
return well_ts;
}

Expand All @@ -143,22 +129,22 @@ static UTIL_SAFE_CAST_FUNCTION( well_ts , WELL_TS_TYPE_ID )

well_ts_type * well_ts_alloc( const char * well_name ) {
well_ts_type * well_ts = well_ts_alloc_empty();
well_ts->well_name = util_alloc_string_copy( well_name );
well_ts->well_name = well_name;
return well_ts;
}

char * well_ts_get_name( const well_ts_type * well_ts) {
return well_ts->well_name;
const char * well_ts_get_name( const well_ts_type * well_ts) {
return well_ts->well_name.c_str();
}

static int well_ts_get_index__( const well_ts_type * well_ts , int report_step , time_t sim_time , bool use_report) {
const int size = vector_get_size( well_ts->ts );
const int size = well_ts->ts.size();
if (size == 0)
return 0;

else {
const well_node_type * first_node = (const well_node_type*)vector_iget_const( well_ts->ts , 0 );
const well_node_type * last_node = (const well_node_type*)vector_get_last_const( well_ts->ts );
const well_node_type * first_node = well_ts->ts[0];
const well_node_type * last_node = well_ts->ts.back();

if (use_report) {
if (report_step < first_node->report_nr)
Expand All @@ -181,7 +167,7 @@ static int well_ts_get_index__( const well_ts_type * well_ts , int report_step ,

while (true) {
int center_index = (lower_index + upper_index) / 2;
const well_node_type * center_node = (const well_node_type*)vector_iget_const( well_ts->ts , center_index );
const well_node_type * center_node = well_ts->ts[center_index];
double cmp;
if (use_report)
cmp = center_node->report_nr - report_step;
Expand Down Expand Up @@ -221,11 +207,11 @@ static int well_ts_get_index( const well_ts_type * well_ts , int report_step , t
// Inline check that the index is correct
{
bool OK = true;
const well_node_type * node = (const well_node_type*)vector_iget_const( well_ts->ts , index );
const well_node_type * node = well_ts->ts[index];
well_node_type * next_node = NULL;

if (index < (vector_get_size( well_ts->ts ) - 1))
next_node = (well_node_type*)vector_iget( well_ts->ts , index + 1);
if (index < (static_cast<int>(well_ts->ts.size()) - 1) )
next_node = well_ts->ts[index + 1];

if (use_report) {
if (index < 0) {
Expand Down Expand Up @@ -265,24 +251,24 @@ static int well_ts_get_index( const well_ts_type * well_ts , int report_step , t

void well_ts_add_well( well_ts_type * well_ts , well_state_type * well_state ) {
well_node_type * new_node = well_node_alloc( well_state );
vector_append_owned_ref( well_ts->ts , new_node , well_node_free__ );
well_ts->ts.push_back( new_node );

if (vector_get_size( well_ts->ts ) > 1) {
const well_node_type * last_node = (const well_node_type*)vector_get_last_const(well_ts->ts );
if (well_ts->ts.size() > 1) {
const well_node_type * last_node = well_ts->ts.back();
if (new_node->sim_time < last_node->sim_time)
// The new node is chronologically before the previous node;
// i.e. we must sort the nodes in time. This should probably happen
// quite seldom:
vector_sort( well_ts->ts , well_node_time_cmp );
// quite seldom:
std::sort( well_ts->ts.begin(), well_ts->ts.end(), well_node_time_lt );
}
}



void well_ts_free( well_ts_type * well_ts ){
free( well_ts->well_name );
vector_free( well_ts->ts );
free( well_ts );
for (size_t i = 0; i < well_ts->ts.size(); i++)
well_node_free( well_ts->ts[i] );
delete well_ts;
}


Expand All @@ -294,7 +280,7 @@ void well_ts_free__( void * arg ) {


int well_ts_get_size( const well_ts_type * well_ts) {
return vector_get_size( well_ts->ts );
return well_ts->ts.size();
}


Expand All @@ -304,12 +290,12 @@ well_state_type * well_ts_get_first_state( const well_ts_type * well_ts) {


well_state_type * well_ts_get_last_state( const well_ts_type * well_ts) {
return well_ts_iget_state( well_ts , vector_get_size( well_ts->ts ) - 1);
return well_ts_iget_state( well_ts , well_ts->ts.size() - 1);
}


well_state_type * well_ts_iget_state( const well_ts_type * well_ts , int index) {
well_node_type * node = (well_node_type*)vector_iget( well_ts->ts , index );
well_node_type * node = well_ts->ts[index];

return node->well_state;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/include/ert/ecl_well/well_ts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extern "C" {
well_state_type * well_ts_get_state_from_report( const well_ts_type * well_ts , int report_nr);
well_state_type * well_ts_iget_state( const well_ts_type * well_ts , int index);
int well_ts_get_size( const well_ts_type * well_ts);
char * well_ts_get_name( const well_ts_type * well_ts);
const char * well_ts_get_name( const well_ts_type * well_ts);
well_state_type * well_ts_get_first_state( const well_ts_type * well_ts);
well_state_type * well_ts_get_last_state( const well_ts_type * well_ts);

Expand Down

0 comments on commit 5f16ebf

Please sign in to comment.