Skip to content

Commit

Permalink
Merge pull request #500 from stefoss23/fix_ecl_sum_panda_frame
Browse files Browse the repository at this point in the history
WIP: Fix ecl sum panda frame
  • Loading branch information
joakim-hove authored Oct 10, 2018
2 parents 1b38f53 + 79f99a4 commit 162d336
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 29 deletions.
41 changes: 32 additions & 9 deletions lib/ecl/ecl_smspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <time.h>
#include <errno.h>

#include <vector>
#include <map>

#include <ert/util/hash.hpp>
#include <ert/util/util.h>
#include <ert/util/vector.hpp>
Expand Down Expand Up @@ -118,6 +121,7 @@ struct ecl_smspec_struct {
bool write_mode;
bool need_nums;
int_vector_type * index_map;
std::map<int, int> inv_index_map;

/*-----------------------------------------------------------------*/

Expand Down Expand Up @@ -252,8 +256,7 @@ static const char* smspec_required_keywords[] = {
/*****************************************************************/

ecl_smspec_type * ecl_smspec_alloc_empty(bool write_mode , const char * key_join_string) {
ecl_smspec_type *ecl_smspec;
ecl_smspec = (ecl_smspec_type*)util_malloc(sizeof *ecl_smspec );
ecl_smspec_type * ecl_smspec = new ecl_smspec_type();
UTIL_TYPE_ID_INIT(ecl_smspec , ECL_SMSPEC_ID);

ecl_smspec->well_var_index = hash_alloc();
Expand Down Expand Up @@ -303,7 +306,7 @@ int * ecl_smspec_alloc_mapping( const ecl_smspec_type * self, const ecl_smspec_t


for (int i=0; i < ecl_smspec_num_nodes( self ); i++) {
const smspec_node_type * self_node = ecl_smspec_iget_node( self , i );
const smspec_node_type * self_node = ecl_smspec_iget_node_w_node_index( self , i );
int self_index = smspec_node_get_params_index( self_node );
const char * key = smspec_node_get_gen_key1( self_node );
if (ecl_smspec_has_general_var( other , key)) {
Expand All @@ -324,8 +327,23 @@ int * ecl_smspec_alloc_mapping( const ecl_smspec_type * self, const ecl_smspec_t
*/


const smspec_node_type * ecl_smspec_iget_node( const ecl_smspec_type * smspec , int index ) {
return (const smspec_node_type*)vector_iget_const( smspec->smspec_nodes , index );
const smspec_node_type * ecl_smspec_iget_node_w_node_index( const ecl_smspec_type * smspec , int node_index ) {
return (const smspec_node_type*)vector_iget_const( smspec->smspec_nodes , node_index );
}


/*
The ecl_smspec_iget_node() function is only retained for compatibility; should be
replaced with calls to the more explicit: ecl_smspec_iget_node_w_node_index().
*/

const smspec_node_type * ecl_smspec_iget_node(const ecl_smspec_type * smspec, int index) {
return ecl_smspec_iget_node_w_node_index(smspec, index);
}

const smspec_node_type * ecl_smspec_iget_node_w_params_index( const ecl_smspec_type * smspec , int params_index ) {
int node_index = smspec->inv_index_map.at(params_index);
return ecl_smspec_iget_node_w_node_index(smspec, node_index);
}

int ecl_smspec_num_nodes( const ecl_smspec_type * smspec) {
Expand All @@ -341,7 +359,7 @@ int ecl_smspec_num_nodes( const ecl_smspec_type * smspec) {
static ecl_data_type get_wgnames_type(const ecl_smspec_type * smspec) {
size_t max_len = 0;
for(int i = 0; i < ecl_smspec_num_nodes(smspec); ++i) {
const smspec_node_type * node = ecl_smspec_iget_node(smspec, i);
const smspec_node_type * node = ecl_smspec_iget_node_w_node_index(smspec, i);
const char * name = smspec_node_get_wgname( node );
if(name)
max_len = util_size_t_max(max_len, strlen(name));
Expand Down Expand Up @@ -433,7 +451,7 @@ static void ecl_smspec_fortio_fwrite( const ecl_smspec_type * smspec , fortio_ty
nums_kw = ecl_kw_alloc( NUMS_KW , num_nodes , ECL_INT);

for (int i=0; i < ecl_smspec_num_nodes( smspec ); i++) {
const smspec_node_type * smspec_node = ecl_smspec_iget_node( smspec , i );
const smspec_node_type * smspec_node = ecl_smspec_iget_node_w_node_index( smspec , i );
/*
It is possible to add variables with deferred initialisation
with the ecl_sum_add_blank_var() function. Before these
Expand Down Expand Up @@ -1093,6 +1111,7 @@ void ecl_smspec_insert_node(ecl_smspec_type * ecl_smspec, smspec_node_type * sms
if (!ecl_smspec->write_mode)
util_abort("%s: internal error \n",__func__);
smspec_node_set_params_index( smspec_node , internal_index);
ecl_smspec->inv_index_map.insert( std::make_pair(internal_index, internal_index) );

if (internal_index >= ecl_smspec->params_size)
ecl_smspec_set_params_size( ecl_smspec , internal_index + 1);
Expand Down Expand Up @@ -1210,6 +1229,7 @@ static bool ecl_smspec_fread_header(ecl_smspec_type * ecl_smspec, const char * h
ecl_util_get_file_type( header_file , &ecl_smspec->formatted , NULL );

{
int node_count = 0;
for (params_index=0; params_index < ecl_kw_get_size(wells); params_index++) {
float default_value = PARAMS_GLOBAL_DEFAULT;
int num = SMSPEC_NUMS_INVALID;
Expand All @@ -1230,8 +1250,11 @@ static bool ecl_smspec_fread_header(ecl_smspec_type * ecl_smspec, const char * h
} else
smspec_node = smspec_node_alloc( var_type , well , kw , unit , ecl_smspec->key_join_string , ecl_smspec->grid_dims , num , params_index , default_value);

if (smspec_node)
if (smspec_node) {
ecl_smspec_add_node( ecl_smspec , smspec_node );
ecl_smspec->inv_index_map.insert( std::make_pair(params_index, node_count) );
node_count++;
}

free( kw );
free( well );
Expand Down Expand Up @@ -1716,7 +1739,7 @@ void ecl_smspec_free(ecl_smspec_type *ecl_smspec) {
float_vector_free( ecl_smspec->params_default );
vector_free( ecl_smspec->smspec_nodes );
free( ecl_smspec->restart_case );
free( ecl_smspec );
delete ecl_smspec;
}


Expand Down
4 changes: 2 additions & 2 deletions lib/ecl/ecl_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ ecl_sum_type * ecl_sum_alloc_resample(const ecl_sum_type * ecl_sum, const char *
const int * grid_dims = ecl_smspec_get_grid_dims(ecl_sum->smspec);

bool time_in_days = false;
const smspec_node_type * node = ecl_smspec_iget_node(ecl_sum->smspec, 0);
const smspec_node_type * node = ecl_smspec_iget_node_w_node_index(ecl_sum->smspec, 0);
if ( util_string_equal(smspec_node_get_unit(node), "DAYS" ) )
time_in_days = true;

Expand All @@ -801,7 +801,7 @@ ecl_sum_type * ecl_sum_alloc_resample(const ecl_sum_type * ecl_sum, const char *


for (int i = 0; i < ecl_smspec_num_nodes(ecl_sum->smspec); i++) {
const smspec_node_type * node = ecl_smspec_iget_node(ecl_sum->smspec, i);
const smspec_node_type * node = ecl_smspec_iget_node_w_node_index(ecl_sum->smspec, i);
if (util_string_equal(smspec_node_get_gen_key1(node), "TIME"))
continue;

Expand Down
7 changes: 3 additions & 4 deletions lib/ecl/ecl_sum_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ double ecl_sum_data_iget( const ecl_sum_data_type * data , int time_index , int
if (params_map[params_index] >= 0)
return file_data->iget( time_index - index_node.offset, params_map[params_index] );
else {
const smspec_node_type * smspec_node = ecl_smspec_iget_node(data->smspec, params_index);
const smspec_node_type * smspec_node = ecl_smspec_iget_node_w_params_index(data->smspec, params_index);
return smspec_node_get_default(smspec_node);
}
}
Expand Down Expand Up @@ -1076,17 +1076,16 @@ static void ecl_sum_data_init_double_vector__(const ecl_sum_data_type * data, in
const auto& params_map = index_node.params_map;
int params_index = params_map[main_params_index];


if (report_only) {
const smspec_node_type * smspec_node = ecl_smspec_iget_node(data->smspec, main_params_index);
const smspec_node_type * smspec_node = ecl_smspec_iget_node_w_params_index(data->smspec, main_params_index);
double default_value = smspec_node_get_default(smspec_node);
offset += data_file->get_data_report(params_index, index_node.length, &output_data[offset], default_value);
} else {

if (params_index >= 0)
data_file->get_data(params_index, index_node.length, &output_data[offset]);
else {
const smspec_node_type * smspec_node = ecl_smspec_iget_node(data->smspec, main_params_index);
const smspec_node_type * smspec_node = ecl_smspec_iget_node_w_params_index(data->smspec, main_params_index);
for (int i=0; i < index_node.length; i++)
output_data[offset + i] = smspec_node_get_default(smspec_node);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ecl/ecl_sum_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ecl_sum_vector_type * ecl_sum_vector_alloc(const ecl_sum_type * ecl_sum, bool ad
if (add_keywords) {
const ecl_smspec_type * smspec = ecl_sum_get_smspec(ecl_sum);
for (int i=0; i < ecl_smspec_num_nodes(smspec); i++) {
const smspec_node_type * node = ecl_smspec_iget_node( smspec , i );
const smspec_node_type * node = ecl_smspec_iget_node_w_node_index( smspec , i );
const char * key = smspec_node_get_gen_key1(node);
/*
The TIME keyword is special case handled to not be included; that is
Expand Down
6 changes: 3 additions & 3 deletions lib/ecl/tests/ecl_smspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void test_sort( ecl_smspec_type * smspec )
test_assert_int_equal( num_nodes, ecl_smspec_num_nodes( smspec ));

for (int i=1; i < ecl_smspec_num_nodes( smspec ); i++) {
const smspec_node_type * node1 = ecl_smspec_iget_node( smspec, i - 1 );
const smspec_node_type * node2 = ecl_smspec_iget_node( smspec, i );
const smspec_node_type * node1 = ecl_smspec_iget_node_w_node_index( smspec, i - 1 );
const smspec_node_type * node2 = ecl_smspec_iget_node_w_node_index( smspec, i );
test_assert_true( smspec_node_cmp( node1 , node2 ) <= 0 );

test_assert_int_equal( smspec_node_get_params_index( node1 ) , i - 1 );
Expand All @@ -43,7 +43,7 @@ void test_copy(const ecl_smspec_type * smspec1) {
ecl_sum_type * ecl_sum2 = ecl_sum_alloc_writer("CASE", false, true, ":", 0, true, 100, 100, 100);
const ecl_smspec_type * smspec2 = ecl_sum_get_smspec(ecl_sum2);
for (int i=0; i < ecl_smspec_num_nodes(smspec1); i++) {
const smspec_node_type * node = ecl_smspec_iget_node(smspec1, i);
const smspec_node_type * node = ecl_smspec_iget_node_w_node_index(smspec1, i);
ecl_sum_add_smspec_node(ecl_sum2, node);
}
test_assert_true( ecl_smspec_equal(smspec1, smspec2));
Expand Down
6 changes: 3 additions & 3 deletions lib/ecl/tests/ecl_sum_alloc_resampled_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ void test_correct_time_vector() {
test_assert_int_equal( ecl_sum_get_report_time(ecl_sum_resampled, 2) , util_make_date_utc( 6,1,2010 ));

const ecl_smspec_type * smspec_resampled = ecl_sum_get_smspec(ecl_sum_resampled);
const smspec_node_type * node1 = ecl_smspec_iget_node(smspec_resampled, 1);
const smspec_node_type * node2 = ecl_smspec_iget_node(smspec_resampled, 2);
const smspec_node_type * node3 = ecl_smspec_iget_node(smspec_resampled, 3);
const smspec_node_type * node1 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 1);
const smspec_node_type * node2 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 2);
const smspec_node_type * node3 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 3);
test_assert_string_equal( "BPR" , smspec_node_get_keyword(node2) );
test_assert_string_equal( "BARS" , smspec_node_get_unit(node2) );

Expand Down
94 changes: 89 additions & 5 deletions lib/ecl/tests/ecl_sum_data_intermediate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
#include <ert/util/double_vector.h>
#include <ert/util/vector.h>

#include <ert/ecl/ecl_endian_flip.hpp>
#include <ert/ecl/ecl_type.hpp>
#include <ert/ecl/ecl_kw.hpp>
#include <ert/ecl/ecl_file.hpp>
#include <ert/ecl/ecl_sum.hpp>
#include <ert/ecl/ecl_sum_vector.hpp>


/*
Expand Down Expand Up @@ -175,7 +180,7 @@ void verify_CASE3() {
ieq(d,2,(2 - i)*10 + 300);

if (i == 0) {
const smspec_node_type * node = ecl_smspec_iget_node(ecl_sum_get_smspec(sum), i);
const smspec_node_type * node = ecl_smspec_iget_node_w_params_index(ecl_sum_get_smspec(sum), i);
double default_value = smspec_node_get_default(node);
ieq(d,3,default_value);
ieq(d,4,default_value);
Expand All @@ -190,12 +195,16 @@ void verify_CASE3() {
double_vector_free(d);
}

ecl_sum_vector_type * vector = ecl_sum_vector_alloc(sum, true);
double frame[27]; //3 vectors X 9 data points pr. vector
ecl_sum_init_double_frame(sum, vector, frame);
ecl_sum_vector_free(vector);

ecl_sum_free(sum);
}


void write_CASE3(bool unified) {
test_work_area_type * work_area = test_work_area_alloc("SMSPEC");
write_CASE2(unified);
{
time_t start_time = util_make_date_utc( 1,1,2010 );
Expand All @@ -221,12 +230,87 @@ void write_CASE3(bool unified) {
ecl_sum_free(ecl_sum);
verify_CASE3();
}
test_work_area_free(work_area);
}

void verify_CASE4() {
ecl_sum_type * sum = ecl_sum_fread_alloc_case("CASE4", ":");

double_vector_type * d;
d = ecl_sum_alloc_data_vector(sum, 0, false); double_vector_free(d);
d = ecl_sum_alloc_data_vector(sum, 1, false); double_vector_free(d);
d = ecl_sum_alloc_data_vector(sum, 2, false); double_vector_free(d);
d = ecl_sum_alloc_data_vector(sum, 4, false);
ieq(d, 0, -99);
ieq(d, 4, -99);
ieq(d, 5, 10000);
ieq(d, 8, 40000);
double_vector_free(d);

ecl_sum_vector_type * vector = ecl_sum_vector_alloc(sum, true);
double frame[27]; //3 vectors X 9 data points pr. vector
ecl_sum_init_double_frame(sum, vector, frame);
test_assert_double_equal(frame[26], 40000);
ecl_sum_vector_free(vector);

ecl_sum_free(sum);
}


void write_CASE4(bool unified) {
test_work_area_type * work_area = test_work_area_alloc("CASE4");
write_CASE3(unified);
{
ecl_file_type * sum_file = ecl_file_open("CASE3.UNSMRY", 0);
ecl_file_type * smspec_file = ecl_file_open("CASE3.SMSPEC", 0);

ecl_kw_type * keywords = ecl_file_iget_named_kw(smspec_file, "KEYWORDS", 0);
ecl_kw_resize(keywords, 5);
ecl_kw_iset_char_ptr(keywords, 3, "WTPRWI1");
ecl_kw_iset_char_ptr(keywords, 4, "BPR");

ecl_kw_type * nums = ecl_file_iget_named_kw(smspec_file, "NUMS", 0);
ecl_kw_resize(nums, 5);
unsigned int * nums_ptr = (unsigned int *)ecl_kw_get_int_ptr(nums);
nums_ptr[3] = 5;
nums_ptr[4] = 8; //a different

ecl_kw_type * wgnames = ecl_file_iget_named_kw(smspec_file, "WGNAMES", 0);
ecl_kw_resize(wgnames, 5);
ecl_kw_iset_char_ptr(wgnames, 4, ":+:+:+:+");

ecl_kw_type * units = ecl_file_iget_named_kw(smspec_file, "UNITS", 0);
ecl_kw_resize(units, 5);
ecl_kw_iset_char_ptr(units, 4, "BARS");

int num_params = ecl_file_get_num_named_kw(sum_file, "PARAMS");
for (int i = 0; i < num_params; i++) {
ecl_kw_type * params_kw = ecl_file_iget_named_kw(sum_file, "PARAMS", i);
ecl_kw_resize(params_kw, 5);
float * ptr = (float*)ecl_kw_get_void_ptr(params_kw);
ptr[4] = ptr[3];
ptr[3] = -1;
}

fortio_type * f;
const char * filename_sum = "CASE4.UNSMRY";
f = fortio_open_writer( filename_sum, false, ECL_ENDIAN_FLIP );
ecl_file_fwrite_fortio(sum_file, f, 0);
fortio_fclose( f );

const char * filename_smspec = "CASE4.SMSPEC";
f = fortio_open_writer( filename_smspec, false, ECL_ENDIAN_FLIP );
ecl_file_fwrite_fortio(smspec_file, f, 0);
fortio_fclose( f );

ecl_file_close(smspec_file);
ecl_file_close(sum_file);
verify_CASE4();
}
test_work_area_free(work_area);
}

int main() {
write_CASE3(true);
write_CASE3(false);
write_CASE4(true);
write_CASE4(false);
return 0;
}
6 changes: 4 additions & 2 deletions lib/include/ert/ecl/ecl_smspec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,17 @@ typedef struct ecl_smspec_struct ecl_smspec_type;
const int * ecl_smspec_get_grid_dims( const ecl_smspec_type * smspec );
int ecl_smspec_get_params_size( const ecl_smspec_type * smspec );
int ecl_smspec_num_nodes( const ecl_smspec_type * smspec);
const smspec_node_type * ecl_smspec_iget_node( const ecl_smspec_type * smspec , int index );

const smspec_node_type * ecl_smspec_iget_node_w_node_index( const ecl_smspec_type * smspec , int node_index );
const smspec_node_type * ecl_smspec_iget_node_w_params_index( const ecl_smspec_type * smspec , int params_index );
const smspec_node_type * ecl_smspec_iget_node(const ecl_smspec_type * smspec, int index);

char * ecl_smspec_alloc_well_key( const ecl_smspec_type * smspec , const char * keyword , const char * wgname);
bool ecl_smspec_equal( const ecl_smspec_type * self , const ecl_smspec_type * other);

void ecl_smspec_sort( ecl_smspec_type * smspec );
ert_ecl_unit_enum ecl_smspec_get_unit_system(const ecl_smspec_type * smspec);


#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 162d336

Please sign in to comment.