From eb6543dcb1c6b63c53d44483284cf43ed184af28 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Sun, 18 Nov 2018 19:33:11 +0100 Subject: [PATCH] Use c++ value semantics for nodes in smspec --- CMakeLists.txt | 2 +- applications/ecl/sum_write.cpp | 10 +- bin/summary_resample | 1 - lib/ecl/ecl_smspec.cpp | 736 +++++++------- lib/ecl/ecl_sum.cpp | 151 ++- lib/ecl/ecl_sum_data.cpp | 35 +- lib/ecl/ecl_sum_file_data.cpp | 8 +- lib/ecl/ecl_sum_tstep.cpp | 20 +- lib/ecl/ecl_sum_vector.cpp | 12 +- lib/ecl/ecl_unsmry_loader.cpp | 2 +- lib/ecl/smspec_node.cpp | 913 +++++++++++------- lib/ecl/tests/ecl_smspec.cpp | 21 +- lib/ecl/tests/ecl_smspec_node.cpp | 131 ++- .../tests/ecl_sum_alloc_resampled_test.cpp | 45 +- .../tests/ecl_sum_data_intermediate_test.cpp | 40 +- lib/ecl/tests/ecl_sum_writer.cpp | 50 +- lib/ecl/tests/ecl_unsmry_loader_test.cpp | 12 +- lib/include/ert/ecl/ecl_smspec.hpp | 58 +- lib/include/ert/ecl/ecl_sum.hpp | 16 +- lib/include/ert/ecl/ecl_sum_data.hpp | 8 +- lib/include/ert/ecl/ecl_sum_tstep.hpp | 7 +- lib/include/ert/ecl/smspec_node.h | 15 +- lib/include/ert/ecl/smspec_node.hpp | 119 +-- .../detail/ecl/ecl_sum_file_data.hpp | 2 +- python/ecl/summary/ecl_sum.py | 5 +- python/ecl/util/test/ecl_mock/ecl_sum_mock.py | 2 + .../tests/bin_tests/test_summary_resample.py | 1 + python/tests/ecl_tests/test_sum.py | 6 +- 28 files changed, 1240 insertions(+), 1188 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b04b686d08..1a91c714d9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,7 +116,7 @@ endif() # Treat warnings as errors if not on Windows if (NOT ERT_WINDOWS) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wall -Wno-unknown-pragmas ") - set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unknown-pragmas -Wno-unused-result -Wno-unused-parameter" ) + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfatal-errors -Wall -Wno-unknown-pragmas -Wno-unused-result -Wno-unused-parameter" ) endif() if (MSVC) diff --git a/applications/ecl/sum_write.cpp b/applications/ecl/sum_write.cpp index 7e37cf2c53..47414bcced 100644 --- a/applications/ecl/sum_write.cpp +++ b/applications/ecl/sum_write.cpp @@ -223,7 +223,7 @@ int main( int argc , char ** argv) { /* The return value from the ecl_sum_add_var() function is an - ecl::smspec_node_type instance (implemented in file smspec_node.c); + ecl::smspec_node instance (implemented in file smspec_node.c); which is essentially a struct holding header information about this varible. There are several options on how to handle the return value from the ecl_sum_add_var() function; which affect how @@ -243,8 +243,8 @@ int main( int argc , char ** argv) { */ - ecl::smspec_node_type * wwct_wellx = ecl_sum_add_var( ecl_sum , "WWCT" , NULL , 0 , "(1)" , 0.0); - ecl::smspec_node_type * wopr_wellx = ecl_sum_add_var( ecl_sum , "WOPR" , NULL , 0 , "Barrels" , 0.0); + const ecl::smspec_node * wwct_wellx = ecl_sum_add_var( ecl_sum , "WWCT" , NULL , 0 , "(1)" , 0.0); + const ecl::smspec_node * wopr_wellx = ecl_sum_add_var( ecl_sum , "WOPR" , NULL , 0 , "Barrels" , 0.0); { @@ -297,8 +297,8 @@ int main( int argc , char ** argv) { We can use the smspec_node value from the ecl_sum_add_var() function directly: */ - ecl_sum_tstep_set_from_node( tstep , wwct_wellx , sim_days ); - ecl_sum_tstep_set_from_node( tstep, wopr_wellx, sim_days * 100); + ecl_sum_tstep_set_from_node( tstep , *wwct_wellx , sim_days ); + ecl_sum_tstep_set_from_node( tstep, *wopr_wellx, sim_days * 100); } } } diff --git a/bin/summary_resample b/bin/summary_resample index 619dff43a6..c032f0016d 100755 --- a/bin/summary_resample +++ b/bin/summary_resample @@ -23,7 +23,6 @@ else: end_time = input_case.get_end_time() time_points = TimeVector.create_linear(CTime(start_time), CTime(end_time), args.num_timestep) - sys.stderr.write("The summary_resample code is partly broken as of October 24.th 2018. Should be fixed\n") output_case = input_case.resample(args.output_case, time_points) output_case.fwrite( ) diff --git a/lib/ecl/ecl_smspec.cpp b/lib/ecl/ecl_smspec.cpp index bb508c7a6b..c2ffe11c7c 100644 --- a/lib/ecl/ecl_smspec.cpp +++ b/lib/ecl/ecl_smspec.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -57,7 +58,7 @@ ------------------------------- 1. The function smspec_node_alloc() must be updated to return a valid - ecl::smspec_node_type instance when called with the new var_type. + ecl::smspec_node instance when called with the new var_type. 2. Update the function ecl_smpec_install_gen_key() to install smpec_index instances of this particular type. The format of the general key is @@ -100,7 +101,7 @@ #define ECL_SMSPEC_ID 806647 #define PARAMS_GLOBAL_DEFAULT -99 -typedef std::map node_map; +typedef std::map node_map; struct ecl_smspec_struct { UTIL_TYPE_ID_DECLARATION; @@ -121,19 +122,18 @@ struct ecl_smspec_struct { std::map> well_completion_var_index; /* Indexes for completion indexes .*/ - std::vector smspec_nodes; + std::vector> smspec_nodes; bool write_mode; bool need_nums; std::vector index_map; std::map inv_index_map; - + int params_size; /*-----------------------------------------------------------------*/ int time_seconds; int grid_dims[3]; /* Grid dimensions - in DIMENS[1,2,3] */ int num_regions; int Nwells , param_offset; - int params_size; std::string key_join_string; /* The string used to join keys when building gen_key keys - typically ":" - but arbitrary - NOT necessary to be able to invert the joining. */ std::string header_file; /* FULL path to the currenbtly loaded header_file. */ @@ -209,33 +209,6 @@ Completion var: VAR_TYPE:WELL_NAME:NUM */ -/** - The special_vars list is used to associate keywords with special - types, when the kewyord name is in conflict with the default vector - naming convention; all the variables mentioned in the list below - are given the type ECL_SMSPEC_MISC_VAR. - - For instance the keyword 'NEWTON' starts with 'N' and is - classified as a NETWORK type variable. However it should rather - be classified as a MISC type variable. (What a fucking mess). - - The special_vars list is used in the functions - ecl_smspec_identify_special_var() and ecl_smspec_identify_var_type(). -*/ - -static const char* special_vars[] = {"NEWTON", - "NAIMFRAC", - "NLINEARS", - "NLINSMIN", - "NLINSMAX", - "ELAPSED", - "MAXDPR", - "MAXDSO", - "MAXDSG", - "MAXDSW", - "STEPTYPE", - "WNEWTON"}; - /* @@ -256,6 +229,52 @@ static const char* smspec_required_keywords[] = { DIMENS_KW }; +namespace { + +const ecl::smspec_node * ecl_smspec_get_var_node( const node_map& mp, const char * var) { + const auto it = mp.find(var); + if (it == mp.end()) + return nullptr; + + return it->second; + } + + const ecl::smspec_node * ecl_smspec_get_str_key_var_node( const std::map& mp , const char * key , const char * var) { + const auto key_it = mp.find(key); + if (key_it == mp.end()) + return nullptr; + + const node_map& var_map = key_it->second; + return ecl_smspec_get_var_node(var_map, var); + } + + const ecl::smspec_node * ecl_smspec_get_int_key_var_node(const std::map& mp , int key , const char * var) { + const auto key_it = mp.find(key); + if (key_it == mp.end()) + return nullptr; + + const auto& var_map = key_it->second; + return ecl_smspec_get_var_node(var_map, var); + } + +} //end namespace + +int ecl_smspec_num_nodes( const ecl_smspec_type * smspec) { + return smspec->smspec_nodes.size(); +} + +/* + When loading a summary case from file many of the nodes can be ignored, in + that case the size of PARAMS vector in the data files is larger than the + number of internalized nodes. Therefor we need to maintain the + params_size member. +*/ + +int ecl_smspec_get_params_size( const ecl_smspec_type * smspec ) { + return smspec->params_size; +} + + /*****************************************************************/ @@ -272,6 +291,7 @@ ecl_smspec_type * ecl_smspec_alloc_empty(bool write_mode , const char * key_join ecl_smspec->year_index = -1; ecl_smspec->month_index = -1; ecl_smspec->time_seconds = -1; + ecl_smspec->params_size = -1; /* The unit system is given as an integer in the INTEHEAD keyword. The INTEHEAD @@ -290,19 +310,19 @@ ecl_smspec_type * ecl_smspec_alloc_empty(bool write_mode , const char * key_join int * ecl_smspec_alloc_mapping( const ecl_smspec_type * self, const ecl_smspec_type * other) { int params_size = ecl_smspec_get_params_size( self ); - int * mapping = (int*)util_malloc( params_size * sizeof * mapping ); + int * mapping = (int*) util_malloc( params_size * sizeof * mapping ); for (int i = 0; i < params_size; i++) mapping[i] = -1; for (int i=0; i < ecl_smspec_num_nodes( self ); i++) { - const ecl::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 ); + const ecl::smspec_node& self_node = ecl_smspec_iget_node_w_node_index( self , i ); + int self_index = self_node.get_params_index(); + const char * key = self_node.get_gen_key1(); if (ecl_smspec_has_general_var( other , key)) { - const ecl::smspec_node_type * other_node = ecl_smspec_get_general_var_node( other , key); - int other_index = smspec_node_get_params_index(other_node); + const ecl::smspec_node& other_node = ecl_smspec_get_general_var_node( other , key); + int other_index = other_node.get_params_index(); mapping[ self_index ] = other_index; } } @@ -318,8 +338,9 @@ int * ecl_smspec_alloc_mapping( const ecl_smspec_type * self, const ecl_smspec_t */ -const ecl::smspec_node_type * ecl_smspec_iget_node_w_node_index( const ecl_smspec_type * smspec , int node_index ) { - return smspec->smspec_nodes[node_index]; +const ecl::smspec_node& ecl_smspec_iget_node_w_node_index( const ecl_smspec_type * smspec , int node_index ) { + const auto& node = smspec->smspec_nodes[node_index]; + return *node.get(); } @@ -328,20 +349,15 @@ const ecl::smspec_node_type * ecl_smspec_iget_node_w_node_index( const ecl_smspe replaced with calls to the more explicit: ecl_smspec_iget_node_w_node_index(). */ -const ecl::smspec_node_type * ecl_smspec_iget_node(const ecl_smspec_type * smspec, int index) { +const ecl::smspec_node& ecl_smspec_iget_node(const ecl_smspec_type * smspec, int index) { return ecl_smspec_iget_node_w_node_index(smspec, index); } -const ecl::smspec_node_type * ecl_smspec_iget_node_w_params_index( const ecl_smspec_type * smspec , int params_index ) { +const ecl::smspec_node& 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) { - return smspec->smspec_nodes.size(); -} - - /** * Returns an ecl data type for which all names will fit. If the maximum name * length is at most 8, an ECL_CHAR is returned and otherwise a large enough @@ -350,8 +366,8 @@ 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 ecl::smspec_node_type * node = ecl_smspec_iget_node_w_node_index(smspec, i); - const char * name = smspec_node_get_wgname( node ); + const ecl::smspec_node& 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)); } @@ -442,7 +458,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 ecl::smspec_node_type * smspec_node = ecl_smspec_iget_node_w_node_index( smspec , i ); + const ecl::smspec_node& 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 @@ -462,23 +478,23 @@ static void ecl_smspec_fortio_fwrite( const ecl_smspec_type * smspec , fortio_ty ignored when/if this smspec file is read in at a later stage. */ - if (smspec_node_get_var_type( smspec_node ) == ECL_SMSPEC_INVALID_VAR) { + if (smspec_node.get_var_type() == ECL_SMSPEC_INVALID_VAR) { ecl_kw_iset_string8( keywords_kw , i , "WWCT" ); ecl_kw_iset_string8( units_kw , i , "????????"); ecl_kw_iset_string_ptr( wgnames_kw , i , DUMMY_WELL); } else { - ecl_kw_iset_string8( keywords_kw , i , smspec_node_get_keyword( smspec_node )); - ecl_kw_iset_string8( units_kw , i , smspec_node_get_unit( smspec_node )); + ecl_kw_iset_string8( keywords_kw , i , smspec_node_get_keyword( &smspec_node )); + ecl_kw_iset_string8( units_kw , i , smspec_node_get_unit( &smspec_node )); { const char * wgname = DUMMY_WELL; - if (smspec_node_get_wgname( smspec_node )) - wgname = smspec_node_get_wgname( smspec_node ); + if (smspec_node_get_wgname( &smspec_node )) + wgname = smspec_node_get_wgname( &smspec_node ); ecl_kw_iset_string_ptr( wgnames_kw , i , wgname); } } if (nums_kw != NULL) - ecl_kw_iset_int( nums_kw , i , smspec_node_get_num( smspec_node )); + ecl_kw_iset_int( nums_kw , i , smspec_node.get_num()); } ecl_kw_fwrite( keywords_kw , fortio ); ecl_kw_fwrite( wgnames_kw , fortio ); @@ -531,33 +547,16 @@ static ecl_smspec_type * ecl_smspec_alloc_writer__( const char * key_join_string ecl_smspec->sim_start_time = sim_start; { - ecl::smspec_node_type * time_node; + const ecl::smspec_node * time_node; + if (time_in_days) { - time_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_MISC_VAR , - NULL , - "TIME" , - "DAYS" , - key_join_string , - ecl_smspec->grid_dims , - 0 , - -1 , - 0 ); ecl_smspec->time_seconds = 3600 * 24; + time_node = ecl_smspec_add_node(ecl_smspec, "TIME", "DAYS", 0); } else { - time_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_MISC_VAR , - NULL , - "TIME" , - "HOURS" , - key_join_string , - ecl_smspec->grid_dims , - 0 , - -1 , - 0 ); ecl_smspec->time_seconds = 3600; + time_node = ecl_smspec_add_node(ecl_smspec, "TIME", "HOURS", 0); } - - ecl_smspec_add_node( ecl_smspec , time_node ); - ecl_smspec->time_index = smspec_node_get_params_index( time_node ); + ecl_smspec->time_index = time_node->get_params_index(); } return ecl_smspec; } @@ -573,131 +572,8 @@ ecl_smspec_type * ecl_smspec_alloc_writer(const char * key_join_string, time_t s UTIL_SAFE_CAST_FUNCTION( ecl_smspec , ECL_SMSPEC_ID ) - -/** - Goes through the special_vars static table to check if @var is one - the special variables which does not follow normal naming - convention. If the test eavulates to true the function will return - ECL_SMSPEC_MISC_VAR, otherwise the function will return - ECL_SMSPEC_INVALID_VAR and the variable type will be determined - from the var name according to standard naming conventions. - - It is important that this function is called before the standard - method. -*/ - -static ecl_smspec_var_type ecl_smspec_identify_special_var( const char * var ) { - ecl_smspec_var_type var_type = ECL_SMSPEC_INVALID_VAR; - - int num_special = sizeof( special_vars ) / sizeof( special_vars[0] ); - int i; - for (i=0; i < num_special; i++) { - if (strcmp( var , special_vars[i]) == 0) { - var_type = ECL_SMSPEC_MISC_VAR; - break; - } - } - - return var_type; -} - - -/* - See table 3.4 in the ECLIPSE file format reference manual. - - Observe that the combined ecl_sum style keys like e.g. WWCT:OP1 - should be formatted with the keyword first, so that this function - will identify both 'WWCT' and 'WWCT:OP_1' as a ECL_SMSPEC_WELL_VAR - instance. -*/ - - ecl_smspec_var_type ecl_smspec_identify_var_type(const char * var) { - ecl_smspec_var_type var_type = ecl_smspec_identify_special_var( var ); - if (var_type == ECL_SMSPEC_INVALID_VAR) { - switch(var[0]) { - case('A'): - var_type = ECL_SMSPEC_AQUIFER_VAR; - break; - case('B'): - var_type = ECL_SMSPEC_BLOCK_VAR; - break; - case('C'): - var_type = ECL_SMSPEC_COMPLETION_VAR; - break; - case('F'): - var_type = ECL_SMSPEC_FIELD_VAR; - break; - case('G'): - var_type = ECL_SMSPEC_GROUP_VAR; - break; - case('L'): - switch(var[1]) { - case('B'): - var_type = ECL_SMSPEC_LOCAL_BLOCK_VAR; - break; - case('C'): - var_type = ECL_SMSPEC_LOCAL_COMPLETION_VAR; - break; - case('W'): - var_type = ECL_SMSPEC_LOCAL_WELL_VAR; - break; - default: - /* - The documentation explicitly mentions keywords starting with - LB, LC and LW as special types, but keywords starting with - L[^BCW] are also valid. These come in the misceallaneous - category; at least the LLINEAR keyword is an example of such - a keyword. - */ - var_type = ECL_SMSPEC_MISC_VAR; - } - break; - case('N'): - var_type = ECL_SMSPEC_NETWORK_VAR; - break; - case('R'): - { - /* - The distinction between region-to-region variables and plain - region variables is less than clear: The current - interpretation is that the cases: - - 1. Any variable matching: - - a) Starts with 'R' - b) Has 'F' as the third character - - 2. The variable "RNLF" - - Get variable type ECL_SMSPEC_REGION_2_REGION_VAR. The rest - get the type ECL_SMSPEC_REGION_VAR. - */ - - if (util_string_equal( var , "RNLF")) - var_type = ECL_SMSPEC_REGION_2_REGION_VAR; - else if (var[2] == 'F') - var_type = ECL_SMSPEC_REGION_2_REGION_VAR; - else - var_type = ECL_SMSPEC_REGION_VAR; - - } - break; - case('S'): - var_type = ECL_SMSPEC_SEGMENT_VAR; - break; - case('W'): - var_type = ECL_SMSPEC_WELL_VAR; - break; - default: - /* - It is unfortunately impossible to recognize an error situation - - the rest just goes in "other" variables. - */ - var_type = ECL_SMSPEC_MISC_VAR; - } - } - return var_type; + return ecl::smspec_node::identify_var_type(var); } @@ -798,23 +674,23 @@ static int ecl_smspec_get_global_grid_index(const ecl_smspec_type * smspec , int defined through the format strings used in this function. */ -static void ecl_smspec_install_gen_keys( ecl_smspec_type * smspec , ecl::smspec_node_type * smspec_node ) { +static void ecl_smspec_install_gen_keys( ecl_smspec_type * smspec , const ecl::smspec_node& smspec_node ) { /* Insert the default general mapping. */ { - const char * gen_key1 = smspec_node_get_gen_key1( smspec_node ); + const char * gen_key1 = smspec_node.get_gen_key1(); if (gen_key1) - smspec->gen_var_index[gen_key1] = smspec_node; + smspec->gen_var_index[gen_key1] = &smspec_node; } /* Insert the (optional) extra mapping for block related variables and region_2_region variables: */ { - const char * gen_key2 = smspec_node_get_gen_key2( smspec_node ); + const char * gen_key2 = smspec_node.get_gen_key2(); if (gen_key2) - smspec->gen_var_index[gen_key2] = smspec_node; + smspec->gen_var_index[gen_key2] = &smspec_node; } } -static void ecl_smspec_install_special_keys( ecl_smspec_type * ecl_smspec , ecl::smspec_node_type * smspec_node) { +static void ecl_smspec_install_special_keys( ecl_smspec_type * ecl_smspec , const ecl::smspec_node& smspec_node) { /** This large switch is for installing keys which have custom lookup paths, in addition to the lookup based on general keys. Examples @@ -823,35 +699,35 @@ static void ecl_smspec_install_special_keys( ecl_smspec_type * ecl_smspec , ecl: ecl_smspec_get_well_var_index( smspec , well_name , var ); */ - const char * well = smspec_node_get_wgname( smspec_node ); + const char * well = smspec_node_get_wgname( &smspec_node ); const char * group = well; - const int num = smspec_node_get_num(smspec_node); - const char * keyword = smspec_node_get_keyword(smspec_node); - ecl_smspec_var_type var_type = smspec_node_get_var_type( smspec_node ); + const int num = smspec_node_get_num(&smspec_node); + const char * keyword = smspec_node_get_keyword(&smspec_node); + ecl_smspec_var_type var_type = smspec_node_get_var_type(&smspec_node ); switch(var_type) { case(ECL_SMSPEC_COMPLETION_VAR): - ecl_smspec->well_completion_var_index[well][num][keyword] = smspec_node; + ecl_smspec->well_completion_var_index[well][num][keyword] = &smspec_node; break; case(ECL_SMSPEC_FIELD_VAR): - ecl_smspec->field_var_index[keyword] = smspec_node; + ecl_smspec->field_var_index[keyword] = &smspec_node; break; case(ECL_SMSPEC_GROUP_VAR): - ecl_smspec->group_var_index[group][keyword] = smspec_node; + ecl_smspec->group_var_index[group][keyword] = &smspec_node; break; case(ECL_SMSPEC_REGION_VAR): - ecl_smspec->region_var_index[num][keyword] = smspec_node; + ecl_smspec->region_var_index[num][keyword] = &smspec_node; ecl_smspec->num_regions = util_int_max(ecl_smspec->num_regions , num); break; case (ECL_SMSPEC_WELL_VAR): - ecl_smspec->well_var_index[well][keyword] = smspec_node; + ecl_smspec->well_var_index[well][keyword] = &smspec_node; break; case(ECL_SMSPEC_MISC_VAR): /* Misc variable - i.e. date or CPU time ... */ - ecl_smspec->misc_var_index[keyword] = smspec_node; + ecl_smspec->misc_var_index[keyword] = &smspec_node; break; case(ECL_SMSPEC_BLOCK_VAR): - ecl_smspec->block_var_index[num][keyword] = smspec_node; + ecl_smspec->block_var_index[num][keyword] = &smspec_node; break; /** The variables below are ONLY accesable through the gen_key @@ -872,9 +748,7 @@ static void ecl_smspec_install_special_keys( ecl_smspec_type * ecl_smspec , ecl: case(ECL_SMSPEC_AQUIFER_VAR): break; default: - smspec_node_fprintf(smspec_node, stderr); - util_abort("%: Internal error - should never be here ?? \n",__func__); - break; + throw std::invalid_argument("Internal error - should not be here \n"); } } @@ -969,10 +843,10 @@ bool ecl_smspec_equal(const ecl_smspec_type * self, return false; for (size_t i=0; i < self->smspec_nodes.size(); i++) { - const auto * node1 = self->smspec_nodes[i]; - const auto * node2 = other->smspec_nodes[i]; + const ecl::smspec_node* node1 = self->smspec_nodes[i].get(); + const ecl::smspec_node* node2 = other->smspec_nodes[i].get(); - if (!smspec_node_equal(node1, node2)) + if (node1->cmp(*node2) != 0) return false; } @@ -1037,60 +911,133 @@ static void ecl_smspec_load_restart( ecl_smspec_type * ecl_smspec , const ecl_fi -void ecl_smspec_index_node( ecl_smspec_type * ecl_smspec , ecl::smspec_node_type * smspec_node) { - ecl_smspec_install_gen_keys( ecl_smspec , smspec_node ); - ecl_smspec_install_special_keys( ecl_smspec , smspec_node ); - if (smspec_node_need_nums( smspec_node )) + + + + +static const ecl::smspec_node * ecl_smspec_insert_node(ecl_smspec_type * ecl_smspec, std::unique_ptr smspec_node){ + int params_index = smspec_node->get_params_index(); + + /* This indexing must be used when writing. */ + ecl_smspec->index_map.push_back(params_index); + ecl_smspec->params_default.resize( params_index+1, PARAMS_GLOBAL_DEFAULT ); + ecl_smspec->params_default[params_index] = smspec_node->get_default(); + ecl_smspec->inv_index_map.insert( std::make_pair(params_index, ecl_smspec->smspec_nodes.size())); + + ecl_smspec_install_gen_keys( ecl_smspec, *smspec_node.get() ); + ecl_smspec_install_special_keys( ecl_smspec, *smspec_node.get() ); + + if (smspec_node_need_nums( &smspec_node )) ecl_smspec->need_nums = true; + + ecl_smspec->smspec_nodes.push_back(std::move(smspec_node)); + + if (params_index > ecl_smspec->params_size) + ecl_smspec->params_size = params_index + 1; + + if (static_cast(ecl_smspec->smspec_nodes.size()) > ecl_smspec->params_size) + ecl_smspec->params_size = ecl_smspec->smspec_nodes.size(); + + const auto& node = ecl_smspec->smspec_nodes.back(); + return node.get(); } -static void ecl_smspec_set_params_size( ecl_smspec_type * ecl_smspec , int params_size) { - ecl_smspec->params_size = params_size; - ecl_smspec->params_default.resize( params_size, PARAMS_GLOBAL_DEFAULT ); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, int num, const char * unit, float default_value) { + int params_index = ecl_smspec->smspec_nodes.size(); + return ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + keyword, + num, + unit, + ecl_smspec->grid_dims, + default_value, + ecl_smspec->key_join_string.c_str()))); } -void ecl_smspec_insert_node(ecl_smspec_type * ecl_smspec, ecl::smspec_node_type * smspec_node){ - int internal_index = ecl_smspec->smspec_nodes.size(); - /* This IF test should only apply in write_mode. */ - if (smspec_node_get_params_index( smspec_node ) < 0) { - 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) ); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, const char * unit, float default_value) { + int params_index = ecl_smspec->smspec_nodes.size(); + return ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + keyword, + unit, + default_value))); +} - if (internal_index >= ecl_smspec->params_size) - ecl_smspec_set_params_size( ecl_smspec , internal_index + 1); - } - ecl_smspec->smspec_nodes.push_back(smspec_node); - { - int params_index = smspec_node_get_params_index( smspec_node ); - /* This indexing must be used when writing. */ - ecl_smspec->index_map.push_back(params_index); - ecl_smspec->params_default.resize( params_index+1, PARAMS_GLOBAL_DEFAULT ); - ecl_smspec->params_default[params_index] = smspec_node_get_default(smspec_node); - } +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, const char * wgname, const char * unit, float default_value) { + int params_index = ecl_smspec->smspec_nodes.size(); + return ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + keyword, + wgname, + unit, + default_value, + ecl_smspec->key_join_string.c_str()))); } -void ecl_smspec_add_node( ecl_smspec_type * ecl_smspec , ecl::smspec_node_type * smspec_node ) { - ecl_smspec_insert_node( ecl_smspec , smspec_node ); - ecl_smspec_index_node( ecl_smspec , smspec_node ); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, + const char * keyword, + const char * wgname, + int num, + const char * unit, + float default_value) +{ + int params_index = ecl_smspec->smspec_nodes.size(); + return ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + keyword, + wgname, + num, + unit, + ecl_smspec->grid_dims, + default_value, + ecl_smspec->key_join_string.c_str()))); } +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, + int params_index, + const char * keyword, + const char * wgname, + int num, + const char * unit, + float default_value) +{ + return ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + keyword, + wgname, + num, + unit, + ecl_smspec->grid_dims, + default_value, + ecl_smspec->key_join_string.c_str()))); +} -void ecl_smspec_init_var( ecl_smspec_type * ecl_smspec , ecl::smspec_node_type * smspec_node , const char * keyword , const char * wgname , int num, const char * unit ) { - smspec_node_init( smspec_node , ecl_smspec_identify_var_type( keyword ) , wgname , keyword , unit , ecl_smspec->key_join_string.c_str() , ecl_smspec->grid_dims , num ); - ecl_smspec_index_node( ecl_smspec , smspec_node ); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, + int params_index, + const char * keyword, + const char * wgname, + int num, + const char * unit, + const char * lgr, + int lgr_i, int lgr_j, int lgr_k, + float default_value) +{ + return ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + keyword, + wgname, + unit, + lgr, + lgr_i, lgr_j, lgr_k, + default_value, + ecl_smspec->key_join_string.c_str()))); } + + const int * ecl_smspec_get_index_map( const ecl_smspec_type * smspec ) { return smspec->index_map.data(); } @@ -1137,6 +1084,7 @@ static bool ecl_smspec_fread_header(ecl_smspec_type * ecl_smspec, const char * h int params_index; ecl_smspec->num_regions = 0; + ecl_smspec->params_size = ecl_kw_get_size(keywords); if (startdat == NULL) util_abort("%s: could not locate STARTDAT keyword in header - aborting \n",__func__); @@ -1173,47 +1121,60 @@ static bool ecl_smspec_fread_header(ecl_smspec_type * ecl_smspec, const char * h ecl_smspec->grid_dims[1] = ecl_kw_iget_int(dimens , DIMENS_SMSPEC_NY_INDEX ); ecl_smspec->grid_dims[2] = ecl_kw_iget_int(dimens , DIMENS_SMSPEC_NZ_INDEX ); ecl_smspec->restart_step = ecl_kw_iget_int(dimens , DIMENS_SMSPEC_RESTART_STEP_INDEX); - ecl_smspec_set_params_size( ecl_smspec , ecl_kw_get_size(keywords)); 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; char * well = (char*)util_alloc_strip_copy((const char*)ecl_kw_iget_ptr(wells , params_index)); char * kw = (char*)util_alloc_strip_copy((const char*)ecl_kw_iget_ptr(keywords , params_index)); char * unit = (char*)util_alloc_strip_copy((const char*)ecl_kw_iget_ptr(units , params_index)); - char * lgr_name = NULL; - ecl::smspec_node_type * smspec_node; - ecl_smspec_var_type var_type = ecl_smspec_identify_var_type( kw ); + ecl_smspec_var_type var_type; if (nums != NULL) num = ecl_kw_iget_int(nums , params_index); + var_type = ecl::smspec_node::valid_type(kw, well, num); + if (var_type == ECL_SMSPEC_INVALID_VAR) { + free( kw ); + free( well ); + free( unit ); + continue; + } + if (ecl_smspec_lgr_var_type( var_type )) { int lgr_i = ecl_kw_iget_int( numlx , params_index ); int lgr_j = ecl_kw_iget_int( numly , params_index ); int lgr_k = ecl_kw_iget_int( numlz , params_index ); - lgr_name = (char*)util_alloc_strip_copy( (const char*)ecl_kw_iget_ptr( lgrs , params_index )); - smspec_node = (ecl::smspec_node_type*)smspec_node_alloc_lgr( var_type , well , kw , unit , lgr_name , ecl_smspec->key_join_string.c_str() , lgr_i , lgr_j , lgr_k , params_index, default_value); + char * lgr_name = (char*)util_alloc_strip_copy( (const char*)ecl_kw_iget_ptr( lgrs , params_index )); + + ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + kw, + well, + unit, + lgr_name, + lgr_i, lgr_j, lgr_k, + default_value, + ecl_smspec->key_join_string.c_str()))); + free(lgr_name); } else - smspec_node = (ecl::smspec_node_type*)smspec_node_alloc( var_type , well , kw , unit , ecl_smspec->key_join_string.c_str() , ecl_smspec->grid_dims , num , params_index , default_value); - - 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++; - } + ecl_smspec_insert_node(ecl_smspec, std::unique_ptr( new ecl::smspec_node(params_index, + kw, + well, + num, + unit, + ecl_smspec->grid_dims, + default_value, + ecl_smspec->key_join_string.c_str()))); free( kw ); free( well ); free( unit ); - free( lgr_name ); } } char * header_str = util_alloc_realpath( header_file ); - ecl_smspec->header_file = header_str; + ecl_smspec->header_file = header_str; free(header_str); if (include_restart) ecl_smspec_load_restart( ecl_smspec , header ); @@ -1226,36 +1187,6 @@ static bool ecl_smspec_fread_header(ecl_smspec_type * ecl_smspec, const char * h } -namespace { - -const ecl::smspec_node_type * ecl_smspec_get_var_node( const node_map& mp, const char * var) { - const auto it = mp.find(var); - if (it == mp.end()) - return NULL; - - return it->second; -} - -const ecl::smspec_node_type * ecl_smspec_get_str_key_var_node( const std::map& mp , const char * key , const char * var) { - const auto key_it = mp.find(key); - if (key_it == mp.end()) - return NULL; - - const node_map& var_map = key_it->second; - return ecl_smspec_get_var_node(var_map, var); -} - -const ecl::smspec_node_type * ecl_smspec_get_int_key_var_node( const std::map& mp , int key , const char * var) { - const auto key_it = mp.find(key); - if (key_it == mp.end()) - return NULL; - - const auto& var_map = key_it->second; - return ecl_smspec_get_var_node(var_map, var); -} - -} //end namespace - ecl_smspec_type * ecl_smspec_fread_alloc(const char *header_file, const char * key_join_string , bool include_restart) { ecl_smspec_type *ecl_smspec; @@ -1269,10 +1200,10 @@ ecl_smspec_type * ecl_smspec_fread_alloc(const char *header_file, const char * k if (ecl_smspec_fread_header(ecl_smspec , header_file , include_restart)) { - const ecl::smspec_node_type * time_node = ecl_smspec_get_var_node(ecl_smspec->misc_var_index, "TIME"); - if (time_node != NULL) { - const char * time_unit = smspec_node_get_unit( time_node ); - ecl_smspec->time_index = smspec_node_get_params_index( time_node ); + const ecl::smspec_node * time_node = ecl_smspec_get_var_node(ecl_smspec->misc_var_index, "TIME"); + if (time_node) { + const char * time_unit = time_node->get_unit(); + ecl_smspec->time_index = time_node->get_params_index(); if (util_string_equal( time_unit , "DAYS")) ecl_smspec->time_seconds = 3600 * 24; @@ -1280,13 +1211,14 @@ ecl_smspec_type * ecl_smspec_fread_alloc(const char *header_file, const char * k ecl_smspec->time_seconds = 3600; else util_abort("%s: time_unit:%s not recognized \n",__func__ , time_unit); + } - const ecl::smspec_node_type * day_node = ecl_smspec_get_var_node(ecl_smspec->misc_var_index, "DAY"); + const ecl::smspec_node * day_node = ecl_smspec_get_var_node(ecl_smspec->misc_var_index, "DAY"); if (day_node != NULL) { ecl_smspec->day_index = smspec_node_get_params_index( day_node ); - ecl_smspec->month_index = smspec_node_get_params_index( ecl_smspec->misc_var_index["MONTH"] ); - ecl_smspec->year_index = smspec_node_get_params_index( ecl_smspec->misc_var_index["YEAR"] ); + ecl_smspec->month_index = smspec_node_get_params_index( &ecl_smspec->misc_var_index["MONTH"] ); + ecl_smspec->year_index = smspec_node_get_params_index( &ecl_smspec->misc_var_index["YEAR"] ); } if ((ecl_smspec->time_index == -1) && ( ecl_smspec->day_index == -1)) { @@ -1349,37 +1281,42 @@ int ecl_smspec_get_num_regions(const ecl_smspec_type * ecl_smspec) { /*****************************************************************/ -#define NODE_RETURN_INDEX(node) \ - if (node == NULL) \ +#define NODE_RETURN_INDEX(node_ptr) \ + if (!node_ptr) \ return -1; \ - else \ - return smspec_node_get_params_index( node ); + else \ + return smspec_node_get_params_index( node_ptr ); -#define NODE_RETURN_EXISTS(node) \ - if (node == NULL) \ +#define NODE_RETURN_EXISTS(node_ptr) \ + if (!node_ptr) \ return false; \ else \ return true; + /******************************************************************/ /* Well variables */ -const ecl::smspec_node_type * ecl_smspec_get_well_var_node( const ecl_smspec_type * smspec , const char * well , const char * var) { - return ecl_smspec_get_str_key_var_node( smspec->well_var_index, well, var ); +const ecl::smspec_node& ecl_smspec_get_well_var_node( const ecl_smspec_type * smspec , const char * well , const char * var) { + const auto node_ptr = ecl_smspec_get_str_key_var_node(smspec->well_var_index, well, var); + if (!node_ptr) + throw std::out_of_range("The well: " + std::string(well) + " variable: " + std::string(var) + " combination does not exist."); + + return *node_ptr; } int ecl_smspec_get_well_var_params_index(const ecl_smspec_type * ecl_smspec , const char * well , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_well_var_node( ecl_smspec , well , var ); - NODE_RETURN_INDEX(node); + const auto node_ptr = ecl_smspec_get_str_key_var_node(ecl_smspec->well_var_index, well, var); + NODE_RETURN_INDEX(node_ptr); } bool ecl_smspec_has_well_var(const ecl_smspec_type * ecl_smspec , const char * well , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_well_var_node(ecl_smspec , well ,var); - NODE_RETURN_EXISTS(node); + const auto node_ptr = ecl_smspec_get_str_key_var_node(ecl_smspec->well_var_index, well, var); + NODE_RETURN_EXISTS(node_ptr); } @@ -1387,43 +1324,50 @@ bool ecl_smspec_has_well_var(const ecl_smspec_type * ecl_smspec , const char * w /*****************************************************************/ /* Group variables */ +const ecl::smspec_node& ecl_smspec_get_group_var_node( const ecl_smspec_type * smspec , const char * group , const char * var) { + const auto node_ptr = ecl_smspec_get_str_key_var_node(smspec->group_var_index, group, var); + if (!node_ptr) + throw std::out_of_range("The group: " + std::string(group) + " variable: " + std::string(var) + " combination does not exist."); -const ecl::smspec_node_type * ecl_smspec_get_group_var_node( const ecl_smspec_type * smspec , const char * group , const char * var) { - return ecl_smspec_get_str_key_var_node( smspec->group_var_index, group, var ); + return *node_ptr; } int ecl_smspec_get_group_var_params_index(const ecl_smspec_type * ecl_smspec , const char * group , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_group_var_node( ecl_smspec , group , var ); - NODE_RETURN_INDEX(node); + const auto node_ptr = ecl_smspec_get_str_key_var_node(ecl_smspec->group_var_index, group, var); + NODE_RETURN_INDEX(node_ptr); } bool ecl_smspec_has_group_var(const ecl_smspec_type * ecl_smspec , const char * group , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_group_var_node(ecl_smspec , group ,var); - NODE_RETURN_EXISTS(node); + const auto node_ptr = ecl_smspec_get_str_key_var_node(ecl_smspec->group_var_index, group, var); + NODE_RETURN_EXISTS(node_ptr); } /*****************************************************************/ /* Field variables */ -const ecl::smspec_node_type * ecl_smspec_get_field_var_node(const ecl_smspec_type * ecl_smspec , const char *var) { - return ecl_smspec_get_var_node(ecl_smspec->field_var_index, var); +const ecl::smspec_node& ecl_smspec_get_field_var_node(const ecl_smspec_type * ecl_smspec , const char *var) { + const auto node_ptr = ecl_smspec_get_var_node( ecl_smspec->field_var_index, var); + if (!node_ptr) + throw std::out_of_range("The field variable: " + std::string(var) + " does not exist."); + + return *node_ptr; } int ecl_smspec_get_field_var_params_index(const ecl_smspec_type * ecl_smspec , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_field_var_node( ecl_smspec , var ); - NODE_RETURN_INDEX(node); + const auto node_ptr = ecl_smspec_get_var_node(ecl_smspec->field_var_index, var); + NODE_RETURN_INDEX(node_ptr); } bool ecl_smspec_has_field_var(const ecl_smspec_type * ecl_smspec , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_field_var_node( ecl_smspec , var ); - NODE_RETURN_EXISTS(node); + const auto node_ptr = ecl_smspec_get_var_node(ecl_smspec->field_var_index, var); + NODE_RETURN_EXISTS(node_ptr); } /*****************************************************************/ @@ -1438,37 +1382,39 @@ bool ecl_smspec_has_field_var(const ecl_smspec_type * ecl_smspec , const char *v */ -const ecl::smspec_node_type * ecl_smspec_get_block_var_node(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr) { - return ecl_smspec_get_int_key_var_node(ecl_smspec->block_var_index, block_nr, block_var); +const ecl::smspec_node& ecl_smspec_get_block_var_node(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr) { + const auto node_ptr = ecl_smspec_get_int_key_var_node(ecl_smspec->block_var_index, block_nr, block_var); + if (!node_ptr) + throw std::out_of_range("No such block variable"); + + return *node_ptr; } -const ecl::smspec_node_type * ecl_smspec_get_block_var_node_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k) { +const ecl::smspec_node& ecl_smspec_get_block_var_node_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k) { return ecl_smspec_get_block_var_node( ecl_smspec , block_var , ecl_smspec_get_global_grid_index( ecl_smspec , i,j,k) ); } bool ecl_smspec_has_block_var(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_block_var_node( ecl_smspec , block_var , block_nr ); - NODE_RETURN_EXISTS(node); + const auto node_ptr = ecl_smspec_get_int_key_var_node(ecl_smspec->block_var_index, block_nr, block_var); + NODE_RETURN_EXISTS(node_ptr); } bool ecl_smspec_has_block_var_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k) { - const ecl::smspec_node_type * node = ecl_smspec_get_block_var_node_ijk( ecl_smspec , block_var , i,j,k ); - NODE_RETURN_EXISTS(node); + return ecl_smspec_has_block_var(ecl_smspec, block_var, ecl_smspec_get_global_grid_index(ecl_smspec, i, j, k)); } int ecl_smspec_get_block_var_params_index(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_block_var_node( ecl_smspec , block_var , block_nr ); - NODE_RETURN_INDEX(node); + const auto node_ptr = ecl_smspec_get_int_key_var_node(ecl_smspec->block_var_index, block_nr, block_var); + NODE_RETURN_INDEX(node_ptr); } int ecl_smspec_get_block_var_params_index_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k) { - const ecl::smspec_node_type * node = ecl_smspec_get_block_var_node_ijk( ecl_smspec , block_var , i,j,k ); - NODE_RETURN_INDEX(node); + return ecl_smspec_get_block_var_params_index(ecl_smspec, block_var, ecl_smspec_get_global_grid_index(ecl_smspec, i, j, k)); } @@ -1480,38 +1426,46 @@ int ecl_smspec_get_block_var_params_index_ijk(const ecl_smspec_type * ecl_smspec -const ecl::smspec_node_type * ecl_smspec_get_region_var_node(const ecl_smspec_type * ecl_smspec , const char *region_var , int region_nr) { - return ecl_smspec_get_int_key_var_node(ecl_smspec->region_var_index, region_nr, region_var); +const ecl::smspec_node& ecl_smspec_get_region_var_node(const ecl_smspec_type * ecl_smspec , const char *region_var , int region_nr) { + const auto node_ptr = ecl_smspec_get_int_key_var_node(ecl_smspec->region_var_index, region_nr, region_var); + if (!node_ptr) + throw std::out_of_range("No such block variable"); + + return *node_ptr; } bool ecl_smspec_has_region_var(const ecl_smspec_type * ecl_smspec , const char *region_var, int region_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_region_var_node( ecl_smspec , region_var , region_nr ); - NODE_RETURN_EXISTS(node); + const auto node_ptr = ecl_smspec_get_int_key_var_node(ecl_smspec->region_var_index, region_nr, region_var); + NODE_RETURN_EXISTS(node_ptr); } int ecl_smspec_get_region_var_params_index(const ecl_smspec_type * ecl_smspec , const char *region_var, int region_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_region_var_node( ecl_smspec , region_var , region_nr ); - NODE_RETURN_INDEX(node); + const auto node_ptr = ecl_smspec_get_int_key_var_node(ecl_smspec->region_var_index, region_nr, region_var); + NODE_RETURN_INDEX(node_ptr); } /*****************************************************************/ /* Misc variables */ -const ecl::smspec_node_type * ecl_smspec_get_misc_var_node(const ecl_smspec_type * ecl_smspec , const char *var) { - return ecl_smspec_get_var_node(ecl_smspec->misc_var_index , var); +const ecl::smspec_node& ecl_smspec_get_misc_var_node(const ecl_smspec_type * ecl_smspec , const char *var) { + const auto node_ptr = ecl_smspec_get_var_node(ecl_smspec->misc_var_index, var); + if (!node_ptr) + throw std::out_of_range("No such misc variable"); + + return *node_ptr; } bool ecl_smspec_has_misc_var(const ecl_smspec_type * ecl_smspec , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_misc_var_node( ecl_smspec , var ); - NODE_RETURN_EXISTS(node); + const auto node_ptr = ecl_smspec_get_var_node(ecl_smspec->misc_var_index , var ); + NODE_RETURN_EXISTS(node_ptr); } int ecl_smspec_get_misc_var_params_index(const ecl_smspec_type * ecl_smspec , const char *var) { - const ecl::smspec_node_type * node = ecl_smspec_get_misc_var_node( ecl_smspec , var ); - NODE_RETURN_INDEX(node); + const auto node_ptr = ecl_smspec_get_var_node(ecl_smspec->misc_var_index , var ); + NODE_RETURN_INDEX(node_ptr); } @@ -1519,25 +1473,33 @@ int ecl_smspec_get_misc_var_params_index(const ecl_smspec_type * ecl_smspec , co /* Well completion - not fully implemented ?? */ -const ecl::smspec_node_type * ecl_smspec_get_well_completion_var_node(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr) { +const ecl::smspec_node * ecl_smspec_get_well_completion_var_node__(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr) { const auto well_iter = ecl_smspec->well_completion_var_index.find(well); if (well_iter == ecl_smspec->well_completion_var_index.end()) - return NULL; + return nullptr; const auto& num_map = well_iter->second; return ecl_smspec_get_int_key_var_node( num_map, cell_nr, var ); } +const ecl::smspec_node& ecl_smspec_get_well_completion_var_node(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr) { + const auto node_ptr = ecl_smspec_get_well_completion_var_node__(ecl_smspec, well, var, cell_nr); + if (!node_ptr) + throw std::out_of_range("No such well/var/completion"); + + return *node_ptr; +} + bool ecl_smspec_has_well_completion_var(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_well_completion_var_node( ecl_smspec , well , var , cell_nr ); - NODE_RETURN_EXISTS( node ); + const auto node_ptr = ecl_smspec_get_well_completion_var_node__( ecl_smspec , well , var , cell_nr ); + NODE_RETURN_EXISTS( node_ptr ); } int ecl_smspec_get_well_completion_var_params_index(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_well_completion_var_node( ecl_smspec , well , var , cell_nr ); - NODE_RETURN_INDEX( node ); + const auto node_ptr = ecl_smspec_get_well_completion_var_node__( ecl_smspec , well , var , cell_nr ); + NODE_RETURN_INDEX( node_ptr ); } @@ -1551,31 +1513,31 @@ int ecl_smspec_get_well_completion_var_params_index(const ecl_smspec_type * ecl -const ecl::smspec_node_type * ecl_smspec_get_general_var_node( const ecl_smspec_type * smspec , const char * lookup_kw ) { - const auto iter = smspec->gen_var_index.find(lookup_kw); - if (iter == smspec->gen_var_index.end()) - return NULL; +const ecl::smspec_node& ecl_smspec_get_general_var_node( const ecl_smspec_type * smspec , const char * lookup_kw ) { + const auto node_ptr = ecl_smspec_get_var_node(smspec->gen_var_index, lookup_kw); + if (!node_ptr) + throw std::out_of_range("No such variable: " + std::string(lookup_kw)); - return iter->second; + return *node_ptr; } int ecl_smspec_get_general_var_params_index(const ecl_smspec_type * ecl_smspec , const char * lookup_kw) { - const ecl::smspec_node_type * node = ecl_smspec_get_general_var_node( ecl_smspec , lookup_kw ); - NODE_RETURN_INDEX( node ); + const auto node_ptr = ecl_smspec_get_var_node(ecl_smspec->gen_var_index , lookup_kw ); + NODE_RETURN_INDEX( node_ptr ); } bool ecl_smspec_has_general_var(const ecl_smspec_type * ecl_smspec , const char * lookup_kw) { - const ecl::smspec_node_type * node = ecl_smspec_get_general_var_node( ecl_smspec , lookup_kw ); - NODE_RETURN_EXISTS( node ); + const auto node_ptr = ecl_smspec_get_var_node(ecl_smspec->gen_var_index , lookup_kw ); + NODE_RETURN_EXISTS( node_ptr ); } /** DIES if the lookup_kw is not present. */ const char * ecl_smspec_get_general_var_unit( const ecl_smspec_type * ecl_smspec , const char * lookup_kw) { const auto smspec_node = ecl_smspec_get_general_var_node(ecl_smspec, lookup_kw); - return smspec_node_get_unit( smspec_node ); + return smspec_node_get_unit( &smspec_node ); } @@ -1586,22 +1548,22 @@ const char * ecl_smspec_get_general_var_unit( const ecl_smspec_type * ecl_smspec */ //const char * ecl_smspec_iget_unit( const ecl_smspec_type * smspec , int node_index ) { -// const ecl::smspec_node_type * smspec_node = ecl_smspec_iget_node( smspec , node_index ); +// const ecl::smspec_node * smspec_node = ecl_smspec_iget_node( smspec , node_index ); // return smspec_node_get_unit( smspec_node ); //} // //int ecl_smspec_iget_num( const ecl_smspec_type * smspec , int node_index ) { -// const ecl::smspec_node_type * smspec_node = ecl_smspec_iget_node( smspec , node_index ); +// const ecl::smspec_node * smspec_node = ecl_smspec_iget_node( smspec , node_index ); // return smspec_node_get_num( smspec_node ); //} // //const char * ecl_smspec_iget_wgname( const ecl_smspec_type * smspec , int node_index ) { -// const ecl::smspec_node_type * smspec_node = ecl_smspec_iget_node( smspec , node_index ); +// const ecl::smspec_node * smspec_node = ecl_smspec_iget_node( smspec , node_index ); // return smspec_node_get_wgname( smspec_node ); //} // //const char * ecl_smspec_iget_keyword( const ecl_smspec_type * smspec , int index ) { -// const ecl::smspec_node_type * smspec_node = ecl_smspec_iget_node( smspec , index ); +// const ecl::smspec_node * smspec_node = ecl_smspec_iget_node( smspec , index ); // return smspec_node_get_keyword( smspec_node ); //} @@ -1653,10 +1615,6 @@ const std::vector& ecl_smspec_get_params_default( const ecl_smspec_type * } void ecl_smspec_free(ecl_smspec_type *ecl_smspec) { - - for (auto& node : ecl_smspec->smspec_nodes) - smspec_node_free(node); - delete ecl_smspec; } @@ -1689,8 +1647,8 @@ int ecl_smspec_get_date_year_index( const ecl_smspec_type * smspec ) { bool ecl_smspec_general_is_total( const ecl_smspec_type * smspec , const char * gen_key) { - const ecl::smspec_node_type * smspec_node = ecl_smspec_get_general_var_node(smspec, gen_key); - return smspec_node_is_total( smspec_node ); + const ecl::smspec_node& smspec_node = ecl_smspec_get_general_var_node(smspec, gen_key); + return smspec_node_is_total( &smspec_node ); } @@ -1826,10 +1784,6 @@ stringlist_type * ecl_smspec_alloc_well_var_list( const ecl_smspec_type * smspec -int ecl_smspec_get_params_size( const ecl_smspec_type * smspec ) { - return smspec->params_size; -} - const int * ecl_smspec_get_grid_dims( const ecl_smspec_type * smspec ) { @@ -1846,15 +1800,15 @@ char * ecl_smspec_alloc_well_key( const ecl_smspec_type * smspec , const char * } -void ecl_smspec_sort( ecl_smspec_type * smspec ) { +/*void ecl_smspec_sort( ecl_smspec_type * smspec ) { std::sort(smspec->smspec_nodes.begin(), smspec->smspec_nodes.end(), smspec_node_lt); for (int i=0; i < static_cast(smspec->smspec_nodes.size()); i++) { - ecl::smspec_node_type * node = smspec->smspec_nodes[i]; - smspec_node_set_params_index( node , i ); + ecl::smspec_node& node = *smspec->smspec_nodes[i].get(); + smspec_node_set_params_index( &node , i ); } - } +*/ ert_ecl_unit_enum ecl_smspec_get_unit_system(const ecl_smspec_type * smspec) { return smspec->unit_system; diff --git a/lib/ecl/ecl_sum.cpp b/lib/ecl/ecl_sum.cpp index 7b91acb9d8..b3fc416fe2 100644 --- a/lib/ecl/ecl_sum.cpp +++ b/lib/ecl/ecl_sum.cpp @@ -290,32 +290,21 @@ void ecl_sum_set_fmt_case( ecl_sum_type * ecl_sum , bool fmt_case ) { -ecl::smspec_node_type * ecl_sum_add_var( ecl_sum_type * ecl_sum , const char * keyword , const char * wgname , int num , const char * unit , float default_value) { +const ecl::smspec_node * ecl_sum_add_var( ecl_sum_type * ecl_sum , const char * keyword , const char * wgname , int num , const char * unit , float default_value) { if (ecl_sum_data_get_length(ecl_sum->data) > 0) throw std::invalid_argument("Can not interchange variable adding and timesteps.\n"); - - ecl::smspec_node_type * smspec_node = (ecl::smspec_node_type*)smspec_node_alloc( ecl_smspec_identify_var_type(keyword), - wgname, - keyword, - unit, - ecl_sum->key_join_string, - ecl_smspec_get_grid_dims(ecl_sum->smspec), - num, - -1, - default_value); - ecl_smspec_add_node(ecl_sum->smspec, smspec_node); - ecl_sum_data_reset_self_map( ecl_sum->data ); - return smspec_node; + return ecl_smspec_add_node( ecl_sum->smspec, keyword, wgname, num, unit, default_value); } -ecl::smspec_node_type * ecl_sum_add_smspec_node(ecl_sum_type * ecl_sum, const ecl::smspec_node_type * node) { + +const ecl::smspec_node * ecl_sum_add_smspec_node(ecl_sum_type * ecl_sum, const ecl::smspec_node * node) { return ecl_sum_add_var(ecl_sum, smspec_node_get_keyword(node), smspec_node_get_wgname(node), - smspec_node_get_num(node), + node->get_num(), smspec_node_get_unit(node), - smspec_node_get_default(node)); + node->get_default()); } @@ -335,7 +324,8 @@ ecl::smspec_node_type * ecl_sum_add_smspec_node(ecl_sum_type * ecl_sum, const ec */ ecl_sum_tstep_type * ecl_sum_add_tstep( ecl_sum_type * ecl_sum , int report_step , double sim_seconds) { - return ecl_sum_data_add_new_tstep( ecl_sum->data , report_step , sim_seconds ); + ecl_sum_tstep_type * new_tstep = ecl_sum_data_add_new_tstep( ecl_sum->data , report_step , sim_seconds ); + return new_tstep; } static ecl_sum_type * ecl_sum_alloc_writer__( const char * ecl_case , const char * restart_case , int restart_step, bool fmt_output , bool unified , const char * key_join_string , time_t sim_start , bool time_in_days , int nx , int ny , int nz) { @@ -516,12 +506,12 @@ bool ecl_sum_case_exists( const char * input_file ) { /*****************************************************************/ -double ecl_sum_get_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const ecl::smspec_node_type * node) { - return ecl_sum_data_get_from_sim_time( ecl_sum->data , sim_time , node ); +double ecl_sum_get_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const ecl::smspec_node * node) { + return ecl_sum_data_get_from_sim_time( ecl_sum->data , sim_time , *node ); } -double ecl_sum_get_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const ecl::smspec_node_type * node) { - return ecl_sum_data_get_from_sim_days( ecl_sum->data , sim_days , node ); +double ecl_sum_get_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const ecl::smspec_node * node) { + return ecl_sum_data_get_from_sim_days( ecl_sum->data , sim_days , *node ); } double ecl_sum_time2days( const ecl_sum_type * ecl_sum , time_t sim_time) { @@ -559,13 +549,13 @@ double ecl_sum_get_well_var(const ecl_sum_type * ecl_sum , int time_index , con } double ecl_sum_get_well_var_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const char * well , const char * var) { - const ecl::smspec_node_type * node = ecl_smspec_get_well_var_node( ecl_sum->smspec , well , var ); - return ecl_sum_get_from_sim_time( ecl_sum , sim_time , node ); + const ecl::smspec_node& node = ecl_smspec_get_well_var_node( ecl_sum->smspec , well , var ); + return ecl_sum_get_from_sim_time( ecl_sum , sim_time , &node ); } double ecl_sum_get_well_var_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const char * well , const char * var) { - const ecl::smspec_node_type * node = ecl_smspec_get_well_var_node( ecl_sum->smspec , well , var ); - return ecl_sum_get_from_sim_days( ecl_sum , sim_days , node ); + const ecl::smspec_node& node = ecl_smspec_get_well_var_node( ecl_sum->smspec , well , var ); + return ecl_sum_get_from_sim_days( ecl_sum , sim_days , &node ); } @@ -583,13 +573,13 @@ double ecl_sum_get_group_var(const ecl_sum_type * ecl_sum , int time_index , co double ecl_sum_get_group_var_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const char * group , const char * var) { - const ecl::smspec_node_type * node = ecl_smspec_get_group_var_node( ecl_sum->smspec , group , var ); - return ecl_sum_get_from_sim_time( ecl_sum , sim_time , node ); + const ecl::smspec_node& node = ecl_smspec_get_group_var_node( ecl_sum->smspec , group , var ); + return ecl_sum_get_from_sim_time( ecl_sum , sim_time , &node ); } double ecl_sum_get_group_var_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const char * group , const char * var) { - const ecl::smspec_node_type * node = ecl_smspec_get_group_var_node( ecl_sum->smspec , group , var ); - return ecl_sum_get_from_sim_days( ecl_sum , sim_days , node ); + const ecl::smspec_node& node = ecl_smspec_get_group_var_node( ecl_sum->smspec , group , var ); + return ecl_sum_get_from_sim_days( ecl_sum , sim_days , &node ); } @@ -605,13 +595,13 @@ double ecl_sum_get_field_var(const ecl_sum_type * ecl_sum , int time_index , con } double ecl_sum_get_field_var_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const char * var) { - const ecl::smspec_node_type * node = ecl_smspec_get_field_var_node( ecl_sum->smspec , var ); - return ecl_sum_get_from_sim_time( ecl_sum , sim_time , node ); + const ecl::smspec_node& node = ecl_smspec_get_field_var_node( ecl_sum->smspec , var ); + return ecl_sum_get_from_sim_time( ecl_sum , sim_time , &node ); } double ecl_sum_get_field_var_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const char * var) { - const ecl::smspec_node_type * node = ecl_smspec_get_field_var_node( ecl_sum->smspec , var ); - return ecl_sum_get_from_sim_days( ecl_sum , sim_days , node ); + const ecl::smspec_node& node = ecl_smspec_get_field_var_node( ecl_sum->smspec , var ); + return ecl_sum_get_from_sim_days( ecl_sum , sim_days , &node ); } @@ -641,13 +631,13 @@ double ecl_sum_get_block_var_ijk(const ecl_sum_type * ecl_sum , int time_index , } double ecl_sum_get_block_var_ijk_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const char * block_var, int i , int j , int k) { - const ecl::smspec_node_type * node = ecl_smspec_get_block_var_node_ijk( ecl_sum->smspec , block_var , i ,j , k); - return ecl_sum_get_from_sim_time( ecl_sum , sim_time , node ); + const ecl::smspec_node& node = ecl_smspec_get_block_var_node_ijk( ecl_sum->smspec , block_var , i ,j , k); + return ecl_sum_get_from_sim_time( ecl_sum , sim_time , &node ); } double ecl_sum_get_block_var_ijk_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const char * block_var, int i , int j , int k) { - const ecl::smspec_node_type * node = ecl_smspec_get_block_var_node_ijk( ecl_sum->smspec , block_var , i ,j , k); - return ecl_sum_get_from_sim_days( ecl_sum , sim_days , node ); + const ecl::smspec_node& node = ecl_smspec_get_block_var_node_ijk( ecl_sum->smspec , block_var , i ,j , k); + return ecl_sum_get_from_sim_days( ecl_sum , sim_days , &node ); } @@ -668,13 +658,13 @@ double ecl_sum_get_region_var(const ecl_sum_type * ecl_sum , int time_index , co } double ecl_sum_get_region_var_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const char * var , int region_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_region_var_node( ecl_sum->smspec , var , region_nr); - return ecl_sum_get_from_sim_time( ecl_sum , sim_time , node ); + const ecl::smspec_node& node = ecl_smspec_get_region_var_node( ecl_sum->smspec , var , region_nr); + return ecl_sum_get_from_sim_time( ecl_sum , sim_time , &node ); } double ecl_sum_get_region_var_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const char * var , int region_nr) { - const ecl::smspec_node_type * node = ecl_smspec_get_region_var_node( ecl_sum->smspec , var , region_nr); - return ecl_sum_get_from_sim_days( ecl_sum , sim_days , node ); + const ecl::smspec_node& node = ecl_smspec_get_region_var_node( ecl_sum->smspec , var , region_nr); + return ecl_sum_get_from_sim_days( ecl_sum , sim_days , &node ); } @@ -715,14 +705,9 @@ double ecl_sum_get_well_completion_var(const ecl_sum_type * ecl_sum , int time_i /*****************************************************************/ /* General variables - this means WWCT:OP_1 - i.e. composite variables*/ -const ecl::smspec_node_type * ecl_sum_get_general_var_node(const ecl_sum_type * ecl_sum , const char * lookup_kw) { - const ecl::smspec_node_type * node = ecl_smspec_get_general_var_node( ecl_sum->smspec , lookup_kw ); - if (node != NULL) - return node; - else { - util_abort("%s: summary case:%s does not contain key:%s\n",__func__ , ecl_sum_get_case( ecl_sum ) , lookup_kw ); - return NULL; - } +const ecl::smspec_node * ecl_sum_get_general_var_node(const ecl_sum_type * ecl_sum , const char * lookup_kw) { + const ecl::smspec_node& node = ecl_smspec_get_general_var_node( ecl_sum->smspec , lookup_kw ); + return &node; } int ecl_sum_get_general_var_params_index(const ecl_sum_type * ecl_sum , const char * lookup_kw) { @@ -761,18 +746,18 @@ void ecl_sum_fwrite_interp_csv_line(const ecl_sum_type * ecl_sum, time_t sim_tim double ecl_sum_get_general_var_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const char * var) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , var ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , var ); return ecl_sum_get_from_sim_time( ecl_sum , sim_time , node ); } double ecl_sum_get_general_var_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const char * var) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , var ); - return ecl_sum_data_get_from_sim_days( ecl_sum->data , sim_days , node ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , var ); + return ecl_sum_data_get_from_sim_days( ecl_sum->data , sim_days , *node ); } const char * ecl_sum_get_general_var_unit( const ecl_sum_type * ecl_sum , const char * var) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , var ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , var ); return smspec_node_get_unit( node ); } @@ -800,8 +785,8 @@ 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 ecl::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" ) ) + const ecl::smspec_node& 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; //create elc_sum_resampled with TIME node only @@ -809,11 +794,11 @@ ecl_sum_type * ecl_sum_alloc_resample(const ecl_sum_type * ecl_sum, const char * //add remaining nodes for (int i = 0; i < ecl_smspec_num_nodes(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_keyword(node), "TIME")) - continue; + const ecl::smspec_node& node = ecl_smspec_iget_node_w_node_index(ecl_sum->smspec, i); + if (util_string_equal(smspec_node_get_gen_key1(&node), "TIME")) + continue; - ecl_sum_add_smspec_node( ecl_sum_resampled, node ); + ecl_sum_add_smspec_node( ecl_sum_resampled, &node ); } /* @@ -830,18 +815,18 @@ ecl_sum_type * ecl_sum_alloc_resample(const ecl_sum_type * ecl_sum, const char * //clamping to the first value for t < start_time or if it is a rate than derivative is 0 for (int i=1; i < ecl_smspec_num_nodes(ecl_sum->smspec); i++) { double value = 0; - const smspec_node_type * node = ecl_smspec_iget_node_w_node_index(ecl_sum->smspec, i); - if (!smspec_node_is_rate(node)) - value = ecl_sum_iget_first_value(ecl_sum, smspec_node_get_params_index(node)); + const ecl::smspec_node& node = ecl_smspec_iget_node_w_node_index(ecl_sum->smspec, i); + if (!node.is_rate()) + value = ecl_sum_iget_first_value(ecl_sum, node.get_params_index()); double_vector_iset(data, i-1, value); } } else if (input_t > end_time) { //clamping to the last value for t > end_time or if it is a rate than derivative is 0 for (int i=1; i < ecl_smspec_num_nodes(ecl_sum->smspec); i++) { double value = 0; - const smspec_node_type * node = ecl_smspec_iget_node_w_node_index(ecl_sum->smspec, i); - if (!smspec_node_is_rate(node)) - value = ecl_sum_iget_last_value(ecl_sum, smspec_node_get_params_index(node)); + const ecl::smspec_node& node = ecl_smspec_iget_node_w_node_index(ecl_sum->smspec, i); + if (!node.is_rate()) + value = ecl_sum_iget_last_value(ecl_sum, node.get_params_index()); double_vector_iset(data, i-1, value); } } else { @@ -871,12 +856,12 @@ double ecl_sum_iget( const ecl_sum_type * ecl_sum , int time_index , int param_i /* Simple get functions which take a general var key as input */ bool ecl_sum_var_is_rate( const ecl_sum_type * ecl_sum , const char * gen_key) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return smspec_node_is_rate( node ); } bool ecl_sum_var_is_total( const ecl_sum_type * ecl_sum , const char * gen_key) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return smspec_node_is_total( node ); } @@ -886,27 +871,27 @@ ecl_smspec_var_type ecl_sum_identify_var_type( const char * var ) { } ecl_smspec_var_type ecl_sum_get_var_type( const ecl_sum_type * ecl_sum , const char * gen_key) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return smspec_node_get_var_type( node ); } const char * ecl_sum_get_unit( const ecl_sum_type * ecl_sum , const char * gen_key) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return smspec_node_get_unit( node ); } int ecl_sum_get_num( const ecl_sum_type * ecl_sum , const char * gen_key ) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return smspec_node_get_num( node ); } const char * ecl_sum_get_wgname( const ecl_sum_type * ecl_sum , const char * gen_key ) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return smspec_node_get_wgname( node ); } const char * ecl_sum_get_keyword( const ecl_sum_type * ecl_sum , const char * gen_key ) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return smspec_node_get_keyword( node ); } @@ -959,7 +944,7 @@ void ecl_sum_init_double_vector(const ecl_sum_type * ecl_sum, const char * gen_k } void ecl_sum_init_double_vector_interp(const ecl_sum_type * ecl_sum, const char * gen_key, const time_t_vector_type * time_points, double * data) { - const ecl::smspec_node_type * node = ecl_smspec_get_general_var_node( ecl_sum->smspec , gen_key); + const ecl::smspec_node& node = ecl_smspec_get_general_var_node( ecl_sum->smspec , gen_key); ecl_sum_data_init_double_vector_interp(ecl_sum->data, node, time_points, data); } @@ -1332,7 +1317,7 @@ stringlist_type * ecl_sum_alloc_well_var_list( const ecl_sum_type * ecl_sum ) { void ecl_sum_resample_from_sim_time( const ecl_sum_type * ecl_sum , const time_t_vector_type * sim_time , double_vector_type * value , const char * gen_key) { - const ecl::smspec_node_type * node = ecl_smspec_get_general_var_node( ecl_sum->smspec , gen_key); + const ecl::smspec_node& node = ecl_smspec_get_general_var_node( ecl_sum->smspec , gen_key); double_vector_reset( value ); { int i; @@ -1343,7 +1328,7 @@ void ecl_sum_resample_from_sim_time( const ecl_sum_type * ecl_sum , const time_t void ecl_sum_resample_from_sim_days( const ecl_sum_type * ecl_sum , const double_vector_type * sim_days , double_vector_type * value , const char * gen_key) { - const ecl::smspec_node_type * node = ecl_smspec_get_general_var_node( ecl_sum->smspec , gen_key); + const ecl::smspec_node& node = ecl_smspec_get_general_var_node( ecl_sum->smspec , gen_key); double_vector_reset( value ); { int i; @@ -1404,7 +1389,7 @@ int ecl_sum_get_report_step_from_days( const ecl_sum_type * sum , double sim_day /*****************************************************************/ -const ecl_smspec_type * ecl_sum_get_smspec( const ecl_sum_type * ecl_sum ) { +ecl_smspec_type * ecl_sum_get_smspec( const ecl_sum_type * ecl_sum ) { return ecl_sum->smspec; } @@ -1457,8 +1442,8 @@ bool ecl_sum_report_step_compatible( const ecl_sum_type * ecl_sum1 , const ecl_s double_vector_type * ecl_sum_alloc_seconds_solution( const ecl_sum_type * ecl_sum , const char * gen_key , double cmp_value , bool rates_clamp_lower) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key); - return ecl_sum_data_alloc_seconds_solution( ecl_sum->data , node , cmp_value , rates_clamp_lower); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key); + return ecl_sum_data_alloc_seconds_solution( ecl_sum->data , *node , cmp_value , rates_clamp_lower); } @@ -1494,11 +1479,11 @@ double ecl_sum_iget_last_value(const ecl_sum_type * ecl_sum, int param_index) { } double ecl_sum_get_last_value_gen_key(const ecl_sum_type * ecl_sum, const char * gen_key) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return ecl_sum_iget_last_value(ecl_sum, smspec_node_get_params_index(node)); } -double ecl_sum_get_last_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node_type *node) { +double ecl_sum_get_last_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node *node) { return ecl_sum_iget_last_value(ecl_sum, smspec_node_get_params_index(node)); } @@ -1507,10 +1492,10 @@ double ecl_sum_iget_first_value(const ecl_sum_type * ecl_sum, int param_index) { } double ecl_sum_get_first_value_gen_key(const ecl_sum_type * ecl_sum, const char * gen_key) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum , gen_key ); return ecl_sum_iget_first_value(ecl_sum, smspec_node_get_params_index(node)); } -double ecl_sum_get_first_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node_type *node) { +double ecl_sum_get_first_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node *node) { return ecl_sum_iget_first_value(ecl_sum, smspec_node_get_params_index(node)); } diff --git a/lib/ecl/ecl_sum_data.cpp b/lib/ecl/ecl_sum_data.cpp index 0e5d6c5b87..24bef5f536 100644 --- a/lib/ecl/ecl_sum_data.cpp +++ b/lib/ecl/ecl_sum_data.cpp @@ -577,9 +577,9 @@ void ecl_sum_data_init_interp_from_sim_days( const ecl_sum_data_type * data , do } -double_vector_type * ecl_sum_data_alloc_seconds_solution(const ecl_sum_data_type * data, const ecl::smspec_node_type * node, double cmp_value, bool rates_clamp_lower) { +double_vector_type * ecl_sum_data_alloc_seconds_solution(const ecl_sum_data_type * data, const ecl::smspec_node& node, double cmp_value, bool rates_clamp_lower) { double_vector_type * solution = double_vector_alloc(0, 0); - const int param_index = smspec_node_get_params_index(node); + const int param_index = smspec_node_get_params_index(&node); const int size = ecl_sum_data_get_length(data); if (size <= 1) @@ -601,7 +601,7 @@ double_vector_type * ecl_sum_data_alloc_seconds_solution(const ecl_sum_data_type double prev_time = ecl_sum_data_iget_sim_seconds(data, prev_index); double time = ecl_sum_data_iget_sim_seconds(data, index); - if (smspec_node_is_rate(node)) { + if (smspec_node_is_rate(&node)) { double_vector_append(solution, rates_clamp_lower ? prev_time + 1 : time); } else { double slope = (value - prev_value) / (time - prev_time); @@ -819,8 +819,8 @@ 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 ecl::smspec_node_type * smspec_node = ecl_smspec_iget_node_w_params_index(data->smspec, params_index); - return smspec_node_get_default(smspec_node); + const ecl::smspec_node& smspec_node = ecl_smspec_iget_node_w_params_index(data->smspec, params_index); + return smspec_node.get_default(); } } @@ -913,9 +913,9 @@ void ecl_sum_data_get_interp_vector( const ecl_sum_data_type * data , time_t sim } } -double ecl_sum_data_get_from_sim_time( const ecl_sum_data_type * data , time_t sim_time , const ecl::smspec_node_type * smspec_node) { - int params_index = smspec_node_get_params_index( smspec_node ); - if (smspec_node_is_rate( smspec_node )) { +double ecl_sum_data_get_from_sim_time( const ecl_sum_data_type * data , time_t sim_time , const ecl::smspec_node& smspec_node) { + int params_index = smspec_node_get_params_index( &smspec_node ); + if (smspec_node_is_rate( &smspec_node )) { /* In general the mapping from sim_time to index is based on half open intervals, which are closed in the upper end: @@ -994,7 +994,7 @@ double ecl_sum_data_time2days( const ecl_sum_data_type * data , time_t sim_time) return util_difftime_days( start_time , sim_time ); } -double ecl_sum_data_get_from_sim_days( const ecl_sum_data_type * data , double sim_days , const ecl::smspec_node_type * smspec_node) { +double ecl_sum_data_get_from_sim_days( const ecl_sum_data_type * data , double sim_days , const ecl::smspec_node& smspec_node) { time_t sim_time = ecl_smspec_get_start_time( data->smspec ); util_inplace_forward_days_utc( &sim_time , sim_days ); return ecl_sum_data_get_from_sim_time( data , sim_time , smspec_node ); @@ -1085,17 +1085,17 @@ static void ecl_sum_data_init_double_vector__(const ecl_sum_data_type * data, in int params_index = params_map[main_params_index]; if (report_only) { - const ecl::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); + const ecl::smspec_node& smspec_node = ecl_smspec_iget_node_w_params_index(data->smspec, main_params_index); + double default_value = smspec_node.get_default(); 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 ecl::smspec_node_type * smspec_node = ecl_smspec_iget_node_w_params_index(data->smspec, main_params_index); + const ecl::smspec_node& 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); + output_data[offset + i] = smspec_node.get_default(); } offset += index_node.length; } @@ -1122,6 +1122,9 @@ double_vector_type * ecl_sum_data_alloc_data_vector( const ecl_sum_data_type * d else output_data.resize( ecl_sum_data_get_length(data) ); + if (params_index >= ecl_smspec_get_params_size(data->smspec)) + throw std::out_of_range("Out of range"); + ecl_sum_data_init_double_vector__(data, params_index, output_data.data(), report_only); double_vector_type * data_vector = double_vector_alloc(output_data.size(), 0); { @@ -1133,11 +1136,11 @@ double_vector_type * ecl_sum_data_alloc_data_vector( const ecl_sum_data_type * d void ecl_sum_data_init_double_vector_interp(const ecl_sum_data_type * data, - const ecl::smspec_node_type * smspec_node, + const ecl::smspec_node& smspec_node, const time_t_vector_type * time_points, double * output_data) { - bool is_rate = smspec_node_is_rate(smspec_node); - int params_index = smspec_node_get_params_index(smspec_node); + bool is_rate = smspec_node_is_rate(&smspec_node); + int params_index = smspec_node_get_params_index(&smspec_node); time_t start_time = ecl_sum_data_get_data_start(data); time_t end_time = ecl_sum_data_get_sim_end(data); double start_value = 0; diff --git a/lib/ecl/ecl_sum_file_data.cpp b/lib/ecl/ecl_sum_file_data.cpp index 90b3c96a1b..19c6fa28cd 100644 --- a/lib/ecl/ecl_sum_file_data.cpp +++ b/lib/ecl/ecl_sum_file_data.cpp @@ -554,7 +554,7 @@ bool ecl_sum_file_data::check_file( ecl_file_type * ecl_file ) { calling routine will read the unified summary file partly. */ -void ecl_sum_file_data::add_ecl_file(int report_step, const ecl_file_view_type * summary_view, const ecl_smspec_type * smspec) { +void ecl_sum_file_data::add_ecl_file(int report_step, const ecl_file_view_type * summary_view) { int num_ministep = ecl_file_view_get_num_named_kw( summary_view , PARAMS_KW); if (num_ministep > 0) { @@ -570,7 +570,7 @@ void ecl_sum_file_data::add_ecl_file(int report_step, const ecl_file_view_type * ministep_nr , params_kw , ecl_file_view_get_src_file( summary_view ), - smspec ); + this->ecl_smspec ); if (tstep) append_tstep( tstep ); @@ -601,7 +601,7 @@ bool ecl_sum_file_data::fread(const stringlist_type * filelist, bool lazy_load, { ecl_file_type * ecl_file = ecl_file_open( data_file , 0); if (ecl_file && check_file( ecl_file )) { - add_ecl_file( report_step , ecl_file_get_global_view( ecl_file ) , ecl_smspec); + this->add_ecl_file( report_step , ecl_file_get_global_view( ecl_file )); ecl_file_close( ecl_file ); } } @@ -633,7 +633,7 @@ bool ecl_sum_file_data::fread(const stringlist_type * filelist, bool lazy_load, */ ecl_file_view_type * summary_view = ecl_file_get_summary_view(ecl_file , block_index); if (summary_view) { - add_ecl_file(block_index + first_report_step , summary_view , ecl_smspec); + this->add_ecl_file(block_index + first_report_step , summary_view); block_index++; } else break; } diff --git a/lib/ecl/ecl_sum_tstep.cpp b/lib/ecl/ecl_sum_tstep.cpp index a7045f5073..97121eefd1 100644 --- a/lib/ecl/ecl_sum_tstep.cpp +++ b/lib/ecl/ecl_sum_tstep.cpp @@ -1,4 +1,4 @@ - /* +/* Copyright (C) 2012 Statoil ASA, Norway. The file 'ecl_sum_tstep.c' is part of ERT - Ensemble based Reservoir Tool. @@ -216,7 +216,11 @@ ecl_sum_tstep_type * ecl_sum_tstep_alloc_from_file( int report_step , ecl_smspec_load_restart() function and the restart case discarded. */ - fprintf(stderr , "** Warning size mismatch between timestep loaded from:%s and header:%s - timestep discarded.\n" , src_file , ecl_smspec_get_header_file( smspec )); + fprintf(stderr , "** Warning size mismatch between timestep loaded from:%s(%d) and header:%s(%d) - timestep discarded.\n" , + src_file , + data_size, + ecl_smspec_get_header_file( smspec ), + ecl_smspec_get_params_size( smspec )); return NULL; } } @@ -315,24 +319,24 @@ void ecl_sum_tstep_ishift(ecl_sum_tstep_type * tstep, int index, float addend) { ecl_sum_tstep_iset(tstep, index, ecl_sum_tstep_iget(tstep, index) + addend); } -void ecl_sum_tstep_set_from_node( ecl_sum_tstep_type * tstep , const ecl::smspec_node_type * smspec_node , float value) { - int data_index = smspec_node_get_params_index( smspec_node ); +void ecl_sum_tstep_set_from_node( ecl_sum_tstep_type * tstep , const ecl::smspec_node& smspec_node , float value) { + int data_index = smspec_node_get_params_index( &smspec_node ); ecl_sum_tstep_iset( tstep , data_index , value); } -double ecl_sum_tstep_get_from_node( const ecl_sum_tstep_type * tstep , const ecl::smspec_node_type * smspec_node) { - int data_index = smspec_node_get_params_index( smspec_node ); +double ecl_sum_tstep_get_from_node( const ecl_sum_tstep_type * tstep , const ecl::smspec_node& smspec_node) { + int data_index = smspec_node_get_params_index( &smspec_node ); return ecl_sum_tstep_iget( tstep , data_index); } void ecl_sum_tstep_set_from_key( ecl_sum_tstep_type * tstep , const char * gen_key , float value) { - const ecl::smspec_node_type * smspec_node = ecl_smspec_get_general_var_node( tstep->smspec , gen_key ); + const ecl::smspec_node& smspec_node = ecl_smspec_get_general_var_node( tstep->smspec , gen_key ); ecl_sum_tstep_set_from_node( tstep , smspec_node , value); } double ecl_sum_tstep_get_from_key(const ecl_sum_tstep_type * tstep , const char * gen_key) { - const ecl::smspec_node_type * smspec_node = ecl_smspec_get_general_var_node( tstep->smspec , gen_key ); + const ecl::smspec_node& smspec_node = ecl_smspec_get_general_var_node( tstep->smspec , gen_key ); return ecl_sum_tstep_get_from_node(tstep , smspec_node ); } diff --git a/lib/ecl/ecl_sum_vector.cpp b/lib/ecl/ecl_sum_vector.cpp index b0eda4014e..62c5659a10 100644 --- a/lib/ecl/ecl_sum_vector.cpp +++ b/lib/ecl/ecl_sum_vector.cpp @@ -47,7 +47,7 @@ void ecl_sum_vector_free( ecl_sum_vector_type * ecl_sum_vector ){ UTIL_IS_INSTANCE_FUNCTION( ecl_sum_vector , ECL_SUM_VECTOR_TYPE_ID ) -static void ecl_sum_vector_add_node(ecl_sum_vector_type * vector, const ecl::smspec_node_type * node, const char * key ) { +static void ecl_sum_vector_add_node(ecl_sum_vector_type * vector, const ecl::smspec_node * node, const char * key ) { int params_index = smspec_node_get_params_index( node ); bool is_rate_key = smspec_node_is_rate( node); @@ -64,14 +64,14 @@ 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 ecl::smspec_node_type * node = ecl_smspec_iget_node_w_node_index( smspec , i ); - const char * key = smspec_node_get_gen_key1(node); + const ecl::smspec_node& 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 to match the same special casing in the key matching function. */ if (!util_string_equal(key, "TIME")) - ecl_sum_vector_add_node( ecl_sum_vector, node, key); + ecl_sum_vector_add_node( ecl_sum_vector, &node, key); } } return ecl_sum_vector; @@ -113,7 +113,7 @@ ecl_sum_vector_type * ecl_sum_vector_alloc_layout_copy(const ecl_sum_vector_type bool ecl_sum_vector_add_key( ecl_sum_vector_type * ecl_sum_vector, const char * key){ if (ecl_sum_has_general_var( ecl_sum_vector->ecl_sum , key)) { - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum_vector->ecl_sum , key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum_vector->ecl_sum , key ); ecl_sum_vector_add_node(ecl_sum_vector, node, key); return true; } else @@ -127,7 +127,7 @@ void ecl_sum_vector_add_keys( ecl_sum_vector_type * ecl_sum_vector, const char * int i; for(i = 0; i < num_keywords ;i++){ const char * key = stringlist_iget(keylist, i); - const ecl::smspec_node_type * node = ecl_sum_get_general_var_node( ecl_sum_vector->ecl_sum , key ); + const ecl::smspec_node * node = ecl_sum_get_general_var_node( ecl_sum_vector->ecl_sum , key ); ecl_sum_vector_add_node(ecl_sum_vector, node, key); } stringlist_free(keylist); diff --git a/lib/ecl/ecl_unsmry_loader.cpp b/lib/ecl/ecl_unsmry_loader.cpp index bd9eb1edde..e30b038d89 100644 --- a/lib/ecl/ecl_unsmry_loader.cpp +++ b/lib/ecl/ecl_unsmry_loader.cpp @@ -53,7 +53,7 @@ int unsmry_loader::length() const { std::vector unsmry_loader::get_vector(int pos) const { if (pos >= size) - throw std::invalid_argument("unsmry_loader::get_vector: argument 'pos' mst be less than size of PARAMS."); + throw std::out_of_range("unsmry_loader::get_vector pos: " + std::to_string(pos) + " PARAMS_SIZE: " + std::to_string(size)); std::vector data(this->length()); int_vector_type * index_map = int_vector_alloc( 1 , pos); diff --git a/lib/ecl/smspec_node.cpp b/lib/ecl/smspec_node.cpp index 8ff0cdd7b0..1d0843e6ee 100644 --- a/lib/ecl/smspec_node.cpp +++ b/lib/ecl/smspec_node.cpp @@ -43,6 +43,33 @@ #include "detail/util/string_util.hpp" +/** + The special_vars list is used to associate keywords with special + types, when the kewyord name is in conflict with the default vector + naming convention; all the variables mentioned in the list below + are given the type ECL_SMSPEC_MISC_VAR. + + For instance the keyword 'NEWTON' starts with 'N' and is + classified as a NETWORK type variable. However it should rather + be classified as a MISC type variable. (What a fucking mess). + + The special_vars list is used in the functions + ecl_smspec_identify_special_var() and ecl_smspec_identify_var_type(). +*/ + +static const char* special_vars[] = {"NEWTON", + "NAIMFRAC", + "NLINEARS", + "NLINSMIN", + "NLINSMAX", + "ELAPSED", + "MAXDPR", + "MAXDSO", + "MAXDSG", + "MAXDSW", + "STEPTYPE", + "WNEWTON"}; + /** This struct contains meta-information about one element in the smspec @@ -52,26 +79,134 @@ probably the most important field. */ +/** + Goes through the special_vars static table to check if @var is one + the special variables which does not follow normal naming + convention. If the test eavulates to true the function will return + ECL_SMSPEC_MISC_VAR, otherwise the function will return + ECL_SMSPEC_INVALID_VAR and the variable type will be determined + from the var name according to standard naming conventions. + + It is important that this function is called before the standard + method. +*/ +ecl_smspec_var_type ecl::smspec_node::identify_special_var( const char * var ) { + ecl_smspec_var_type var_type = ECL_SMSPEC_INVALID_VAR; -static bool smspec_node_need_wgname(ecl_smspec_var_type var_type) { - if (var_type == ECL_SMSPEC_COMPLETION_VAR || - var_type == ECL_SMSPEC_GROUP_VAR || - var_type == ECL_SMSPEC_WELL_VAR || - var_type == ECL_SMSPEC_SEGMENT_VAR) - return true; - else - return false; + int num_special = sizeof( special_vars ) / sizeof( special_vars[0] ); + int i; + for (i=0; i < num_special; i++) { + if (strcmp( var , special_vars[i]) == 0) { + var_type = ECL_SMSPEC_MISC_VAR; + break; + } + } + + return var_type; } -static bool smspec_node_type_supported(ecl_smspec_var_type var_type) { - if (var_type == ECL_SMSPEC_NETWORK_VAR) - return false; +/* + See table 3.4 in the ECLIPSE file format reference manual. + + Observe that the combined ecl_sum style keys like e.g. WWCT:OP1 + should be formatted with the keyword first, so that this function + will identify both 'WWCT' and 'WWCT:OP_1' as a ECL_SMSPEC_WELL_VAR + instance. +*/ + +ecl_smspec_var_type ecl::smspec_node::identify_var_type(const char * var) { + ecl_smspec_var_type var_type = ecl::smspec_node::identify_special_var(var); + if (var_type == ECL_SMSPEC_INVALID_VAR) { + switch(var[0]) { + case('A'): + var_type = ECL_SMSPEC_AQUIFER_VAR; + break; + case('B'): + var_type = ECL_SMSPEC_BLOCK_VAR; + break; + case('C'): + var_type = ECL_SMSPEC_COMPLETION_VAR; + break; + case('F'): + var_type = ECL_SMSPEC_FIELD_VAR; + break; + case('G'): + var_type = ECL_SMSPEC_GROUP_VAR; + break; + case('L'): + switch(var[1]) { + case('B'): + var_type = ECL_SMSPEC_LOCAL_BLOCK_VAR; + break; + case('C'): + var_type = ECL_SMSPEC_LOCAL_COMPLETION_VAR; + break; + case('W'): + var_type = ECL_SMSPEC_LOCAL_WELL_VAR; + break; + default: + /* + The documentation explicitly mentions keywords starting with + LB, LC and LW as special types, but keywords starting with + L[^BCW] are also valid. These come in the misceallaneous + category; at least the LLINEAR keyword is an example of such + a keyword. + */ + var_type = ECL_SMSPEC_MISC_VAR; + } + break; + case('N'): + var_type = ECL_SMSPEC_NETWORK_VAR; + break; + case('R'): + { + /* + The distinction between region-to-region variables and plain + region variables is less than clear: The current + interpretation is that the cases: + + 1. Any variable matching: - return true; + a) Starts with 'R' + b) Has 'F' as the third character + + 2. The variable "RNLF" + + Get variable type ECL_SMSPEC_REGION_2_REGION_VAR. The rest + get the type ECL_SMSPEC_REGION_VAR. + */ + + if (util_string_equal( var , "RNLF")) + var_type = ECL_SMSPEC_REGION_2_REGION_VAR; + else if (var[2] == 'F') + var_type = ECL_SMSPEC_REGION_2_REGION_VAR; + else + var_type = ECL_SMSPEC_REGION_VAR; + + } + break; + case('S'): + var_type = ECL_SMSPEC_SEGMENT_VAR; + break; + case('W'): + var_type = ECL_SMSPEC_WELL_VAR; + break; + default: + /* + It is unfortunately impossible to recognize an error situation - + the rest just goes in "other" variables. + */ + var_type = ECL_SMSPEC_MISC_VAR; + } + } + return var_type; } + + + /*****************************************************************/ /* The key formats for the combined keys like e.g. 'WWCT:OP_5' should @@ -339,84 +474,60 @@ bool smspec_node_identify_total(const char * keyword, ecl_smspec_var_type var_ty namespace ecl { -void smspec_node_type::set_keyword( const std::string& keyword_ ) { - // ECLIPSE Standard: Max eight characters - everything beyond is silently dropped - // This function can __ONLY__ be called on time; run-time chaning of keyword is not - // allowed. - if (keyword.size() == 0) - this->keyword = keyword_; - else - util_abort("%s: fatal error - attempt to change keyword runtime detected - aborting\n",__func__); -} - - -void smspec_node_type::set_invalid_flags() { - this->rate_variable = false; - this->total_variable = false; - this->historical = false; -} - -void smspec_node_type::set_flags() { - /* - Check if this is a rate variabel - that info is used when - interpolating results to true_time between ministeps. - */ - rate_variable = smspec_node_identify_rate(keyword.c_str()); - if (keyword.back() == 'H') - historical = true; - total_variable = smspec_node_identify_total(keyword.c_str(), var_type); -} - -float smspec_node_type::get_default() const { +float smspec_node::get_default() const { return this->default_value; } -void smspec_node_type::set_lgr_ijk( int lgr_i , int lgr_j , int lgr_k) { +void smspec_node::set_lgr_ijk( int lgr_i , int lgr_j , int lgr_k) { lgr_ijk[0] = lgr_i; lgr_ijk[1] = lgr_j; lgr_ijk[2] = lgr_k; } -void smspec_node_type::init_num( ecl_smspec_var_type var_type_) { - switch( var_type_ ) { - case(ECL_SMSPEC_WELL_VAR): - num = SMSPEC_NUMS_WELL; - break; - case(ECL_SMSPEC_GROUP_VAR): - num = SMSPEC_NUMS_GROUP; - break; - case(ECL_SMSPEC_FIELD_VAR): - num = SMSPEC_NUMS_FIELD; - break; - default: - num = SMSPEC_NUMS_INVALID; - } +/* + Observe that field vectors like 'FOPT' and 'FOPR' will have the string 'FIELD' + in the 'WGNAME' vector, that is not internalized here. +*/ + +void smspec_node::set_wgname(const char * wgname) { + if (!wgname) + return; + + if (IS_DUMMY_WELL(wgname)) + return; + + if (this->var_type == ECL_SMSPEC_WELL_VAR || + this->var_type == ECL_SMSPEC_GROUP_VAR || + this->var_type == ECL_SMSPEC_COMPLETION_VAR || + this->var_type == ECL_SMSPEC_SEGMENT_VAR || + this->var_type == ECL_SMSPEC_LOCAL_WELL_VAR) + this->wgname = wgname; } -void smspec_node_type::set_num( const int grid_dims[3] , int num_) { +void smspec_node::set_num( const int * grid_dims , int num_) { if (num_ == SMSPEC_NUMS_INVALID) util_abort("%s: explicitly trying to set nums == SMSPEC_NUMS_INVALID - seems like a bug?!\n",__func__); - num = num_; + this->num = num_; if ((var_type == ECL_SMSPEC_COMPLETION_VAR) || (var_type == ECL_SMSPEC_BLOCK_VAR)) { - int global_index = num - 1; - ijk[2] = global_index / ( grid_dims[0] * grid_dims[1] ); global_index -= ijk[2] * (grid_dims[0] * grid_dims[1]); - ijk[1] = global_index / grid_dims[0] ; global_index -= ijk[1] * grid_dims[0]; - ijk[0] = global_index; - - ijk[0] += 1; - ijk[1] += 1; - ijk[2] += 1; + int global_index = this->num - 1; + this->ijk[2] = global_index / ( grid_dims[0] * grid_dims[1] ); global_index -= this->ijk[2] * (grid_dims[0] * grid_dims[1]); + this->ijk[1] = global_index / grid_dims[0] ; global_index -= this->ijk[1] * grid_dims[0]; + this->ijk[0] = global_index; + + this->ijk[0] += 1; + this->ijk[1] += 1; + this->ijk[2] += 1; } } -void smspec_node_type::decode_R1R2(int * r1 , int * r2) const { +void smspec_node::decode_R1R2(int * r1 , int * r2) const { if (var_type == ECL_SMSPEC_REGION_2_REGION_VAR) { - *r1 = num % 32768; - *r2 = ((num - (*r1)) / 32768)-10; + *r1 = this->num % 32768; + *r2 = ((this->num - (*r1)) / 32768)-10; } else { *r1 = -1; *r2 = -1; @@ -435,7 +546,7 @@ void smspec_node_type::decode_R1R2(int * r1 , int * r2) const { */ -void smspec_node_type::set_gen_keys( const char * key_join_string_) { +void smspec_node::set_gen_keys( const char* key_join_string_) { switch( var_type) { case(ECL_SMSPEC_COMPLETION_VAR): // KEYWORD:WGNAME:NUM @@ -503,101 +614,67 @@ void smspec_node_type::set_gen_keys( const char * key_join_string_) { } +/* + Observe the following: -void smspec_node_type::common_init( ecl_smspec_var_type var_type_ , const char * keyword_ , const std::string& unit_ ) { - if (var_type == ECL_SMSPEC_INVALID_VAR) { - this->unit = unit_; - this->keyword = keyword_; - this->var_type = var_type_; - set_flags(); - init_num( var_type_ ); - } else - util_abort("%s: trying to re-init smspec node with keyword:%s - invalid \n",__func__ , keyword_ ); -} + 1. There are many legitimate value types here which we do not handle, then we + just return false. + 2. Observe that the LGR variables are not thoroughly checked; the only check + is that the well is not the dummy well. Experience has shown that there has + not been problems with SMSPEC files with invalid LGR and LGRIJK values; + that is therefor just assumed to be right. -bool smspec_node_type::init__( ecl_smspec_var_type var_type , - const char * wgname_ , - const char * keyword_ , - const char * unit , - const char * key_join_string , - const int grid_dims[3] , - int num) { +*/ - bool initOK = true; +ecl_smspec_var_type smspec_node::valid_type(const char * keyword, const char * wgname, int num) { + auto var_type = smspec_node::identify_var_type(keyword); - common_init( var_type , keyword_ , unit ); - switch (var_type) { - case(ECL_SMSPEC_COMPLETION_VAR): - /* Completion variable : WGNAME & NUM */ - set_num( grid_dims , num ); - wgname = wgname_; - if (num < 0) - initOK = false; - break; - case(ECL_SMSPEC_GROUP_VAR): - /* Group variable : WGNAME */ - wgname = wgname_; - break; - case(ECL_SMSPEC_WELL_VAR): - /* Well variable : WGNAME */ - wgname = wgname_; - break; - case(ECL_SMSPEC_SEGMENT_VAR): - wgname = wgname_; - set_num( grid_dims , num ); - if (num < 0) - initOK = false; - break; - case(ECL_SMSPEC_FIELD_VAR): - /* Field variable : */ - /* Fully initialized with the smspec_common_init() function */ - break; - case(ECL_SMSPEC_REGION_VAR): - /* Region variable : NUM */ - set_num( grid_dims , num ); - break; - case(ECL_SMSPEC_REGION_2_REGION_VAR): - /* Region 2 region variable : NUM */ - set_num( grid_dims , num ); - break; - case(ECL_SMSPEC_BLOCK_VAR): - /* A block variable : NUM*/ - set_num( grid_dims , num ); - break; - case(ECL_SMSPEC_MISC_VAR): - /* Misc variable : */ + if (var_type == ECL_SMSPEC_MISC_VAR) + return var_type; - /* - For some keywords the SMSPEC files generated by Eclipse have a - non zero NUMS value although; it seems that value is required - for the generatd summaryfiles to display nicely in - e.g. S3GRAF. - */ + if (var_type == ECL_SMSPEC_FIELD_VAR) + return var_type; - if (util_string_equal( keyword_ ,SMSPEC_TIME_KEYWORD)) - set_num( grid_dims , SMSPEC_TIME_NUMS_VALUE ); + if (var_type == ECL_SMSPEC_LOCAL_BLOCK_VAR) + return var_type; - if (util_string_equal( keyword_ ,SMSPEC_YEARS_KEYWORD)) - set_num( grid_dims , SMSPEC_YEARS_NUMS_VALUE ); + if (var_type == ECL_SMSPEC_WELL_VAR || + var_type == ECL_SMSPEC_GROUP_VAR || + var_type == ECL_SMSPEC_LOCAL_WELL_VAR || + var_type == ECL_SMSPEC_LOCAL_COMPLETION_VAR) { + if (IS_DUMMY_WELL(wgname)) + return ECL_SMSPEC_INVALID_VAR; + else + return var_type; + } - break; - case(ECL_SMSPEC_AQUIFER_VAR): - set_num( grid_dims , num ); - break; - default: - /* Lots of legitimate alternatives which are not internalized. */ - initOK = false; - break; + if (var_type == ECL_SMSPEC_COMPLETION_VAR || var_type == ECL_SMSPEC_SEGMENT_VAR) { + if (IS_DUMMY_WELL(wgname)) + return ECL_SMSPEC_INVALID_VAR; + + if (num < 0) + return ECL_SMSPEC_INVALID_VAR; + + return var_type; } - if (initOK) - set_gen_keys( key_join_string ); + if (var_type == ECL_SMSPEC_REGION_VAR || + var_type == ECL_SMSPEC_REGION_2_REGION_VAR || + var_type == ECL_SMSPEC_BLOCK_VAR || + var_type == ECL_SMSPEC_AQUIFER_VAR) { - return initOK; + if (num < 0) + return ECL_SMSPEC_INVALID_VAR; + + return var_type; + } + + return ECL_SMSPEC_INVALID_VAR; } + /** This function will allocate a smspec_node instance, and initialize all the elements. Observe that the function can return NULL, in the @@ -612,15 +689,77 @@ bool smspec_node_type::init__( ecl_smspec_var_type var_type , ecl_smspec_fread_header() functions in addition. UGGGLY */ -smspec_node_type::smspec_node_type() {} - -smspec_node_type::smspec_node_type( ecl_smspec_var_type var_type_ , - const char * wgname , - const char * keyword , - const char * unit , - const char * key_join_string , - const int grid_dims[3] , - int num , int param_index_, float default_value_) { +smspec_node::smspec_node(int param_index, const char * keyword, int num, const char * unit, const int grid_dims[3], float default_value, const char * key_join_string) + : smspec_node(param_index, + keyword, + nullptr, + num, + unit, + grid_dims, + default_value, + key_join_string) +{ +} + +smspec_node::smspec_node(int param_index, const char * keyword, int num, const char * unit, float default_value, const char * key_join_string) + : smspec_node(param_index, + keyword, + nullptr, + num, + unit, + nullptr, + default_value, + key_join_string) +{ +} + + + +smspec_node::smspec_node(int param_index, const char * keyword, const char * wgname, const char * unit, float default_value, const char * key_join_string) + : smspec_node(param_index, + keyword, + wgname, + 0, + unit, + nullptr, + default_value, + key_join_string) +{ +} + +smspec_node::smspec_node(int param_index, const char * keyword, const char * wgname, int num, const char * unit, float default_value, const char * key_join_string) + : smspec_node(param_index, + keyword, + wgname, + num, + unit, + nullptr, + default_value, + key_join_string) +{} + + +smspec_node::smspec_node(int param_index, const char * keyword, const char * unit, float default_value) + : smspec_node(param_index, + keyword, + nullptr, + 0, + unit, + nullptr, + default_value, + nullptr) +{ +} + + +smspec_node::smspec_node(int param_index, + const char * keyword, + const char * wgname , + int num, + const char * unit , + const int grid_dims[3] , + float default_value, + const char * key_join_string) { /* Well and group names in the wgname parameter is quite messy. The situation is as follows: @@ -651,102 +790,129 @@ smspec_node_type::smspec_node_type( ecl_smspec_var_type var_type_ , completely. */ - if (smspec_node_need_wgname(var_type_) && IS_DUMMY_WELL(wgname)) - throw std::invalid_argument("Wrong input params to smspec_node constructor."); + this->var_type = this->valid_type(keyword, wgname, num); + if (this->var_type == ECL_SMSPEC_INVALID_VAR) + throw std::invalid_argument("Could not construct smspec_node from this input."); - if (!smspec_node_type_supported(var_type_)) - throw std::invalid_argument("Wrong input params to smspec_node constructor."); + this->params_index = param_index; + this->default_value = default_value; + this->keyword = keyword; + this->unit = unit; + this->rate_variable = smspec_node_identify_rate(this->keyword.c_str()); + this->total_variable = smspec_node_identify_total(this->keyword.c_str(), this->var_type); + this->historical = (this->keyword.back() == 'H'); + this->set_wgname( wgname ); - /* - TODO: The init function should be joined in this functions. - */ + switch (this->var_type) { + case(ECL_SMSPEC_COMPLETION_VAR): + /* Completion variable : WGNAME & NUM */ + set_num( grid_dims , num ); + break; + case(ECL_SMSPEC_GROUP_VAR): + /* Group variable : WGNAME */ + break; + case(ECL_SMSPEC_WELL_VAR): + /* Well variable : WGNAME */ + break; + case(ECL_SMSPEC_SEGMENT_VAR): + set_num( grid_dims , num ); + break; + case(ECL_SMSPEC_FIELD_VAR): + /* Field variable : */ + /* Fully initialized with the smspec_common_init() function */ + break; + case(ECL_SMSPEC_REGION_VAR): + /* Region variable : NUM */ + set_num( grid_dims , num ); + break; + case(ECL_SMSPEC_REGION_2_REGION_VAR): + /* Region 2 region variable : NUM */ + set_num( grid_dims , num ); + break; + case(ECL_SMSPEC_BLOCK_VAR): + /* A block variable : NUM*/ + set_num( grid_dims , num ); + break; + case(ECL_SMSPEC_MISC_VAR): + /* Misc variable : */ - params_index = param_index_; - default_value = default_value_; + /* + For some keywords the SMSPEC files generated by Eclipse have a + non zero NUMS value although; it seems that value is required + for the generatd summaryfiles to display nicely in + e.g. S3GRAF. + */ - var_type = ECL_SMSPEC_INVALID_VAR; - set_invalid_flags(); + if (this->keyword == std::string(SMSPEC_TIME_KEYWORD)) + set_num( grid_dims , SMSPEC_TIME_NUMS_VALUE ); - bool initOK = init__( var_type_ , wgname , keyword , unit , key_join_string , grid_dims, num); - if (!initOK) - throw std::invalid_argument("Wrong input params to smspec_node constructor."); -} + if (this->keyword == std::string(SMSPEC_YEARS_KEYWORD)) + set_num( grid_dims , SMSPEC_YEARS_NUMS_VALUE ); + break; + case(ECL_SMSPEC_AQUIFER_VAR): + set_num( grid_dims , num ); + break; + default: + throw std::invalid_argument("Should not be here ... "); + break; + } -void smspec_node_type::init_lgr( ecl_smspec_var_type var_type , - const char * wgname_ , - const char * keyword_ , - const char * unit , - const char * lgr , - const char * key_join_string , - int lgr_i, int lgr_j , int lgr_k - ) { - bool initOK = true; - bool wgnameOK = true; - if ((wgname_ != NULL) && (IS_DUMMY_WELL(wgname_))) - wgnameOK = false; + set_gen_keys( key_join_string ); +} - common_init( var_type , keyword_ , unit ); - switch (var_type) { +smspec_node::smspec_node( int param_index_, + const char * keyword_ , + const char * wgname_ , + const char * unit_ , + const char * lgr_ , + int lgr_i, int lgr_j , int lgr_k, + float default_value_, + const char * key_join_string_) { + + this->var_type = this->valid_type(keyword_, wgname_, -1); + printf("keyword: %s\n",keyword_); + if (this->var_type == ECL_SMSPEC_INVALID_VAR) + throw std::invalid_argument("Could not construct smspec_node from this input."); + + this->params_index = param_index_; + this->default_value = default_value_; + this->keyword = keyword_; + this->wgname = wgname_; + this->unit = unit_; + this->rate_variable = smspec_node_identify_rate(this->keyword.c_str()); + this->total_variable = smspec_node_identify_total(this->keyword.c_str(), this->var_type); + this->historical = (this->keyword.back() == 'H'); + this->lgr_name = lgr_; + + switch (this->var_type) { case(ECL_SMSPEC_LOCAL_WELL_VAR): - wgname = wgname_; - lgr_name = lgr; - initOK = wgnameOK; break; case(ECL_SMSPEC_LOCAL_BLOCK_VAR): - lgr_name = lgr; set_lgr_ijk( lgr_i, lgr_j , lgr_k ); break; case(ECL_SMSPEC_LOCAL_COMPLETION_VAR): - lgr_name = lgr; - wgname = wgname_; set_lgr_ijk( lgr_i, lgr_j , lgr_k ); - initOK = wgnameOK; break; default: - util_abort("%s: internal error: in LGR function with non-LGR keyword:%s \n",__func__ , keyword_); + throw std::invalid_argument("Should not be here .... "); } - if (initOK) - set_gen_keys( key_join_string ); -} - -smspec_node_type::smspec_node_type( ecl_smspec_var_type var_type_ , - const char * wgname_ , - const char * keyword_ , - const char * unit_ , - const char * lgr_ , - const char * key_join_string_ , - int lgr_i, int lgr_j , int lgr_k, - int param_index_ , float default_value_) { - params_index = param_index_; - default_value = default_value_; - - var_type = ECL_SMSPEC_INVALID_VAR; - set_invalid_flags(); - - init_lgr( var_type_ , wgname_ , keyword_ , unit_ , lgr_ , key_join_string_ , lgr_i, lgr_j , lgr_k); - + set_gen_keys( key_join_string_ ); } -smspec_node_type * smspec_node_type::copy() const { - smspec_node_type * copy = new smspec_node_type(); - *copy = *this; - return copy; -} - /*****************************************************************/ -int smspec_node_type::get_params_index() const { +int smspec_node::get_params_index() const { return this->params_index; } -void smspec_node_type::set_params_index( int params_index_) { - this->params_index = params_index_; -} +// void smspec_node::set_params_index( int params_index_) { + // this->params_index = params_index_; +// } namespace { @@ -761,58 +927,58 @@ namespace { } -const char * smspec_node_type::get_gen_key1() const { +const char * smspec_node::get_gen_key1() const { return get_cstring( this->gen_key1 ); } -const char * smspec_node_type::get_gen_key2() const { +const char * smspec_node::get_gen_key2() const { return get_cstring( this->gen_key2 ); } -const char * smspec_node_type::get_wgname() const { +const char * smspec_node::get_wgname() const { return get_cstring( this->wgname ); } -const char * smspec_node_type::get_keyword() const { +const char * smspec_node::get_keyword() const { return get_cstring( this->keyword ); } -ecl_smspec_var_type smspec_node_type::get_var_type() const { +ecl_smspec_var_type smspec_node::get_var_type() const { return this->var_type; } -int smspec_node_type::get_num() const { +int smspec_node::get_num() const { return this->num; } -bool smspec_node_type::is_rate() const { +bool smspec_node::is_rate() const { return this->rate_variable; } -bool smspec_node_type::is_total() const { +bool smspec_node::is_total() const { return this->total_variable; } -bool smspec_node_type::is_historical() const { +bool smspec_node::is_historical() const { return this->historical; } -const char * smspec_node_type::get_unit() const { +const char * smspec_node::get_unit() const { return this->unit.c_str(); } // Will be garbage for smspec_nodes which do not have i,j,k -const std::array& smspec_node_type::get_ijk() const { +const std::array& smspec_node::get_ijk() const { return this->ijk; } // Will be NULL for smspec_nodes which are not related to an LGR. -const std::string& smspec_node_type::get_lgr_name() const { - return this->lgr_name; +const char * smspec_node::get_lgr_name() const { + return get_cstring(this->lgr_name); } // Will be garbage for smspec_nodes which are not related to an LGR. -const std::array& smspec_node_type::get_lgr_ijk() const { +const std::array& smspec_node::get_lgr_ijk() const { return this->lgr_ijk; } @@ -821,7 +987,7 @@ const std::array& smspec_node_type::get_lgr_ijk() const { of type ECL_SMSPEC_REGION_2_REGION_VAR. */ -int smspec_node_type::get_R1() const { +int smspec_node::get_R1() const { if (var_type == ECL_SMSPEC_REGION_2_REGION_VAR) { int r1,r2; decode_R1R2( &r1 , &r2); @@ -830,7 +996,7 @@ int smspec_node_type::get_R1() const { return -1; } -int smspec_node_type::get_R2() const { +int smspec_node::get_R2() const { if (var_type == ECL_SMSPEC_REGION_2_REGION_VAR) { int r1,r2; decode_R1R2( &r1 , &r2); @@ -840,7 +1006,7 @@ int smspec_node_type::get_R2() const { } -bool smspec_node_type::need_nums() const { +bool smspec_node::need_nums() const { /* Check if this node needs the nums field; if at least one of the nodes need the NUMS field must be stored when writing a SMSPEC @@ -864,7 +1030,7 @@ bool smspec_node_type::need_nums() const { } -void smspec_node_type::fprintf__( FILE * stream) const { +void smspec_node::fprintf__( FILE * stream) const { fprintf(stream, "KEYWORD: %s \n", this->keyword.c_str()); fprintf(stream, "WGNAME : %s \n", this->wgname.c_str()); fprintf(stream, "UNIT : %s \n", this->unit.c_str()); @@ -872,19 +1038,32 @@ void smspec_node_type::fprintf__( FILE * stream) const { fprintf(stream, "NUM : %d \n\n", this->num); } + +namespace { /* MISC variables are generally sorted to the end of the list, but some special case variables come at the very beginning. */ -int smspec_node_type::cmp_MISC__( const smspec_node_type * node2) const { +int int_cmp(int v1, int v2) { + if (v1 < v2) + return -1; + + if (v1 > v2) + return 1; + + return 0; +} + + +int cmp_MISC( const smspec_node& node1, const smspec_node& node2) { static const std::set early_vars = {"TIME", "DAYS", "DAY", "MONTH", "YEAR", "YEARS"}; - if (smspec_node_type::equal_MISC( this, node2) ) + if (&node1 == &node2) return 0; - bool node1_early = !( early_vars.find(this->keyword) == early_vars.end() ); - bool node2_early = !( early_vars.find(node2->keyword) == early_vars.end() ); + bool node1_early = !( early_vars.find(node1.get_keyword()) == early_vars.end() ); + bool node2_early = !( early_vars.find(node2.get_keyword()) == early_vars.end() ); if (node1_early && !node2_early) @@ -893,174 +1072,178 @@ int smspec_node_type::cmp_MISC__( const smspec_node_type * node2) const { if (!node1_early && node2_early) return 1; - return this->keyword.compare(node2->keyword); + return strcmp(node1.get_keyword(), node2.get_keyword()); } -int int_cmp(int v1, int v2) { - if (v1 < v2) - return -1; +int cmp_LGRIJK( const smspec_node& node1, const smspec_node& node2) { + const auto& ijk1 = node1.get_lgr_ijk(); + const auto& ijk2 = node2.get_lgr_ijk(); - if (v1 > v2) - return 1; - - return 0; -} - -int smspec_node_type::cmp_LGRIJK__( const smspec_node_type * node2) const { - int i_cmp = int_cmp( this->lgr_ijk[0] , node2->lgr_ijk[0]); + int i_cmp = int_cmp( ijk1[0] , ijk2[0]); if (i_cmp != 0) return i_cmp; - int j_cmp = int_cmp(this->lgr_ijk[1] , node2->lgr_ijk[1]); + int j_cmp = int_cmp(ijk1[1] , ijk2[1]); if (j_cmp != 0) return j_cmp; - return int_cmp( this->lgr_ijk[2] , node2->lgr_ijk[2]); + return int_cmp( ijk1[2] , ijk2[2]); } -int smspec_node_type::cmp_KEYWORD_LGR_LGRIJK__( const smspec_node_type * node2) const { - int keyword_cmp = this->keyword.compare(node2->keyword); +int cmp_KEYWORD_LGR_LGRIJK( const smspec_node& node1, const smspec_node& node2) { + int keyword_cmp = strcmp(node1.get_keyword(), node2.get_keyword()); if (keyword_cmp != 0) return keyword_cmp; - int lgr_cmp = this->lgr_name.compare( node2->lgr_name ); + int lgr_cmp = strcmp(node1.get_lgr_name(), node2.get_lgr_name()); if (lgr_cmp != 0) return lgr_cmp; - return smspec_node_type::cmp_LGRIJK( this, node2); + return cmp_LGRIJK( node1, node2); } -int smspec_node_type::cmp_KEYWORD_WGNAME_NUM__(const smspec_node_type * node2) const { - int keyword_cmp = this->keyword.compare(node2->keyword); +int cmp_KEYWORD_WGNAME_NUM(const smspec_node& node1, const smspec_node& node2) { + int keyword_cmp = strcmp(node1.get_keyword(), node2.get_keyword()); if (keyword_cmp != 0) return keyword_cmp; - int wgname_cmp = this->wgname.compare(node2->wgname); + int wgname_cmp = strcmp(node1.get_wgname(), node2.get_wgname()); if (wgname_cmp != 0) return wgname_cmp; - return int_cmp( this->num , node2->num); + return int_cmp( node1.get_num() , node2.get_num()); } -int smspec_node_type::cmp_KEYWORD_WGNAME_LGR__( const smspec_node_type * node2) const { - int keyword_cmp = this->keyword.compare(node2->keyword); +int cmp_KEYWORD_WGNAME_LGR( const smspec_node& node1, const smspec_node& node2) { + int keyword_cmp = strcmp(node1.get_keyword(), node2.get_keyword()); if (keyword_cmp != 0) return keyword_cmp; - int wgname_cmp = this->wgname.compare(node2->wgname); + int wgname_cmp = strcmp(node1.get_wgname(), node2.get_wgname()); if (wgname_cmp != 0) return wgname_cmp; - return this->lgr_name.compare(node2->lgr_name); + return strcmp(node1.get_lgr_name(), node2.get_lgr_name()); } -int smspec_node_type::cmp_KEYWORD_WGNAME_LGR_LGRIJK__( const smspec_node_type * node2) const { - int keyword_cmp = this->keyword.compare(node2->keyword); +int cmp_KEYWORD_WGNAME_LGR_LGRIJK( const smspec_node& node1, const smspec_node& node2) { + int keyword_cmp = strcmp(node1.get_keyword(), node2.get_keyword()); if (keyword_cmp != 0) return keyword_cmp; - int wgname_cmp = this->wgname.compare(node2->wgname); + int wgname_cmp = strcmp(node1.get_wgname(), node2.get_wgname()); if (wgname_cmp != 0) return wgname_cmp; - int lgr_cmp = this->lgr_name.compare(node2->lgr_name); + int lgr_cmp = strcmp(node1.get_lgr_name(), node2.get_lgr_name()); if (lgr_cmp != 0) return lgr_cmp; - return smspec_node_type::cmp_LGRIJK( this, node2); + return cmp_LGRIJK( node1, node2); } -int smspec_node_type::cmp_KEYWORD_WGNAME__( const smspec_node_type * node2) const { - int keyword_cmp = this->keyword.compare(node2->keyword); +int cmp_KEYWORD_WGNAME( const smspec_node& node1, const smspec_node& node2) { + int keyword_cmp = strcmp(node1.get_keyword(), node2.get_keyword()); if (keyword_cmp != 0) return keyword_cmp; - if (IS_DUMMY_WELL( this->wgname.c_str() )) { - if (IS_DUMMY_WELL( node2->wgname.c_str() )) + if (IS_DUMMY_WELL( node1.get_wgname() )) { + if (IS_DUMMY_WELL( node2.get_wgname() )) return 0; else return 1; } - if (IS_DUMMY_WELL( node2->wgname.c_str() )) + if (IS_DUMMY_WELL( node2.get_wgname() )) return -1; - return this->wgname.compare(node2->wgname); + return strcmp(node1.get_wgname(), node2.get_wgname()); } +int cmp_KEYWORD( const smspec_node& node1, const smspec_node& node2) { + return strcmp(node1.get_keyword(), node2.get_keyword()); +} + + -int smspec_node_type::cmp_KEYWORD_NUM__( const smspec_node_type * node2) const { - int keyword_cmp = this->keyword.compare(node2->keyword); +int cmp_KEYWORD_NUM( const smspec_node& node1, const smspec_node& node2) { + int keyword_cmp = strcmp(node1.get_keyword(), node2.get_keyword()); if (keyword_cmp != 0) return keyword_cmp; - return int_cmp( this->num , node2->num); + return int_cmp( node1.get_num() , node2.get_num()); } -int smspec_node_type::cmp_key1__( const smspec_node_type * node2) const { - if (this->gen_key1.empty()) { - if (node2->gen_key1.empty()) +int cmp_key1( const smspec_node& node1, const smspec_node& node2) { + if (node1.get_gen_key1() == NULL) { + if (node2.get_gen_key1() == NULL) return 0; else return -1; - } else if (node2->gen_key1.empty()) { + } else if (node2.get_gen_key1() == NULL) return 1; - } - return util_strcmp_int( this->gen_key1.c_str() , node2->gen_key1.c_str() ); + + return util_strcmp_int( node1.get_gen_key1() , node2.get_gen_key1() ); } +} + -int smspec_node_type::cmp__(const smspec_node_type * node2) const { +int smspec_node::cmp(const smspec_node& node1, const smspec_node& node2) { /* 1: Start with special casing for the MISC variables. */ - if ((this->var_type == ECL_SMSPEC_MISC_VAR) || (node2->var_type == ECL_SMSPEC_MISC_VAR)) - return smspec_node_type::cmp_MISC( this , node2 ); + if ((node1.var_type == ECL_SMSPEC_MISC_VAR) || (node2.var_type == ECL_SMSPEC_MISC_VAR)) + return cmp_MISC( node1 , node2 ); /* 2: Sort according to variable type */ - if (this->var_type < node2->var_type) + if (node1.var_type < node2.var_type) return -1; - if (this->var_type > node2->var_type) + if (node1.var_type > node2.var_type) return 1; /* 3: Internal sort of variables of the same type. */ - switch (this->var_type) { + switch (node1.var_type) { case( ECL_SMSPEC_FIELD_VAR): - return smspec_node_type::cmp_KEYWORD( this, node2); + return cmp_KEYWORD( node1, node2); case( ECL_SMSPEC_WELL_VAR): case( ECL_SMSPEC_GROUP_VAR): - return smspec_node_type::cmp_KEYWORD_WGNAME( this, node2); + return cmp_KEYWORD_WGNAME( node1, node2); case( ECL_SMSPEC_BLOCK_VAR): case( ECL_SMSPEC_REGION_VAR): case( ECL_SMSPEC_REGION_2_REGION_VAR): case( ECL_SMSPEC_AQUIFER_VAR): - return smspec_node_type::cmp_KEYWORD_NUM( this, node2); + return cmp_KEYWORD_NUM( node1, node2); case( ECL_SMSPEC_COMPLETION_VAR): case( ECL_SMSPEC_SEGMENT_VAR): - return smspec_node_type::cmp_KEYWORD_WGNAME_NUM( this, node2); + return cmp_KEYWORD_WGNAME_NUM( node1, node2); case (ECL_SMSPEC_NETWORK_VAR): - return smspec_node_type::cmp_key1( this, node2); + return cmp_key1( node1, node2); case( ECL_SMSPEC_LOCAL_BLOCK_VAR): - return smspec_node_type::cmp_KEYWORD_LGR_LGRIJK( this, node2); + return cmp_KEYWORD_LGR_LGRIJK( node1, node2); case( ECL_SMSPEC_LOCAL_WELL_VAR): - return smspec_node_type::cmp_KEYWORD_WGNAME_LGR( this, node2); + return cmp_KEYWORD_WGNAME_LGR( node1, node2); case( ECL_SMSPEC_LOCAL_COMPLETION_VAR): - return smspec_node_type::cmp_KEYWORD_WGNAME_LGR_LGRIJK( this, node2); + return cmp_KEYWORD_WGNAME_LGR_LGRIJK( node1, node2); default: /* Should not really end up here. */ - return smspec_node_type::cmp_key1( this, node2); + return cmp_key1( node1, node2); } } +int smspec_node::cmp(const smspec_node& node2) const { + return smspec_node::cmp(*this, node2); +} + } // end namespace ecl @@ -1068,7 +1251,7 @@ int smspec_node_type::cmp__(const smspec_node_type * node2) const { void smspec_node_free( void * index ) { - delete static_cast(index); + delete static_cast(index); } void smspec_node_free__( void * arg ) { @@ -1076,131 +1259,118 @@ void smspec_node_free__( void * arg ) { } float smspec_node_get_default( const void * smspec_node ) { - return static_cast(smspec_node)->get_default(); + return static_cast(smspec_node)->get_default(); } int smspec_node_get_params_index( const void * smspec_node ) { - return static_cast(smspec_node)->get_params_index(); + return static_cast(smspec_node)->get_params_index(); } -void smspec_node_set_params_index( void * smspec_node , int params_index) { - static_cast(smspec_node)->set_params_index( params_index ); -} +// void smspec_node_set_params_index( void * smspec_node , int params_index) { + // static_cast(smspec_node)->set_params_index( params_index ); +// } const char * smspec_node_get_gen_key1( const void * smspec_node) { - return static_cast(smspec_node)->get_gen_key1(); + return static_cast(smspec_node)->get_gen_key1(); } const char * smspec_node_get_gen_key2( const void * smspec_node) { - return static_cast(smspec_node)->get_gen_key2(); + return static_cast(smspec_node)->get_gen_key2(); } const char * smspec_node_get_wgname( const void * smspec_node) { - return static_cast(smspec_node)->get_wgname(); + return static_cast(smspec_node)->get_wgname(); } const char * smspec_node_get_keyword( const void * smspec_node) { - return static_cast(smspec_node)->get_keyword( ); + return static_cast(smspec_node)->get_keyword( ); } ecl_smspec_var_type smspec_node_get_var_type( const void * smspec_node) { - return static_cast(smspec_node)->get_var_type(); + return static_cast(smspec_node)->get_var_type(); } int smspec_node_get_num( const void * smspec_node) { - return static_cast(smspec_node)->get_num(); + return static_cast(smspec_node)->get_num(); } bool smspec_node_is_rate( const void * smspec_node ) { - return static_cast(smspec_node)->is_rate(); + return static_cast(smspec_node)->is_rate(); } bool smspec_node_is_total( const void * smspec_node ){ - return static_cast(smspec_node)->is_total(); + return static_cast(smspec_node)->is_total(); } bool smspec_node_is_historical( const void * smspec_node ){ - return static_cast(smspec_node)->is_historical(); + return static_cast(smspec_node)->is_historical(); } const char * smspec_node_get_unit( const void * smspec_node) { - return static_cast(smspec_node)->get_unit(); + return static_cast(smspec_node)->get_unit(); } // Will be garbage for smspec_nodes which do not have i,j,k const int* smspec_node_get_ijk( const void * smspec_node ) { - return static_cast(smspec_node)->get_ijk().data(); + return static_cast(smspec_node)->get_ijk().data(); } // Will be NULL for smspec_nodes which are not related to an LGR. const char* smspec_node_get_lgr_name( const void * smspec_node ) { - return static_cast(smspec_node)->get_lgr_name().c_str(); + return static_cast(smspec_node)->get_lgr_name(); } // Will be garbage for smspec_nodes which are not related to an LGR. const int* smspec_node_get_lgr_ijk( const void * smspec_node ) { - return static_cast(smspec_node)->get_lgr_ijk().data(); + return static_cast(smspec_node)->get_lgr_ijk().data(); } int smspec_node_get_R1( const void * smspec_node ) { - return static_cast(smspec_node)->get_R1(); + return static_cast(smspec_node)->get_R1(); } int smspec_node_get_R2( const void * smspec_node ) { - return static_cast(smspec_node)->get_R2(); + return static_cast(smspec_node)->get_R2(); } bool smspec_node_need_nums( const void * smspec_node ) { - return static_cast(smspec_node)->need_nums(); + return static_cast(smspec_node)->need_nums(); } void smspec_node_fprintf( const void * smspec_node , FILE * stream) { - static_cast(smspec_node)->fprintf__(stream); + static_cast(smspec_node)->fprintf__(stream); } int smspec_node_cmp( const void * node1, const void * node2) { - return ecl::smspec_node_type::cmp(static_cast(node1), static_cast(node2)); + return ecl::smspec_node::cmp(static_cast(node1), static_cast(node2)); } int smspec_node_cmp__( const void * node1, const void * node2) { return smspec_node_cmp(node1, node2); } -/* - This function should be removed from the public API. -*/ -void smspec_node_init( void * smspec_node, - ecl_smspec_var_type var_type , - const char * wgname , - const char * keyword , - const char * unit , - const char * key_join_string , - const int grid_dims[3] , - int num) { - - static_cast(smspec_node)->init__( var_type, - wgname, - keyword, - unit, - key_join_string, - grid_dims, - num ); - -} - - -void * smspec_node_alloc( ecl_smspec_var_type var_type , - const char * wgname , - const char * keyword , - const char * unit , - const char * key_join_string , - const int grid_dims[3] , - int num , int param_index, float default_value) { - ecl::smspec_node_type * node = NULL; + +void * smspec_node_alloc( int param_index, + const char * keyword , + const char * wgname, + int num, + const char * unit , + const int grid_dims[3] , + float default_value, + const char * key_join_string) { + + ecl::smspec_node * node = NULL; try { - node = new ecl::smspec_node_type(var_type, wgname, keyword, unit, key_join_string, grid_dims, num, param_index, default_value); + node = new ecl::smspec_node(param_index, + keyword, + wgname, + num, + unit, + grid_dims, + default_value, + key_join_string); } catch (const std::invalid_argument& e) { node = NULL; @@ -1218,26 +1388,21 @@ void * smspec_node_alloc_lgr( ecl_smspec_var_type var_type , int lgr_i, int lgr_j , int lgr_k, int param_index , float default_value) { - return new ecl::smspec_node_type( var_type, wgname, keyword, unit, lgr, key_join_string, lgr_i, lgr_j, lgr_k, param_index, default_value); + return new ecl::smspec_node( param_index, keyword, wgname, unit, lgr, lgr_i, lgr_j, lgr_k, default_value, key_join_string); } -void * smspec_node_alloc_copy( const void * node ) { - if( !node ) return NULL; - return static_cast(node)->copy(); -} - bool smspec_node_equal( const void * node1, const void * node2) { - return ecl::smspec_node_type::cmp( static_cast(node1) , static_cast(node2) ) == 0; + return ecl::smspec_node::cmp( static_cast(node1) , static_cast(node2) ) == 0; } bool smspec_node_gt( const void * node1, const void * node2) { - return ecl::smspec_node_type::cmp( static_cast(node1) , static_cast(node2) ) > 0; + return ecl::smspec_node::cmp( static_cast(node1) , static_cast(node2) ) > 0; } bool smspec_node_lt( const void * node1, const void * node2) { - return ecl::smspec_node_type::cmp( static_cast(node1) , static_cast(node2) ) < 0; + return ecl::smspec_node::cmp( static_cast(node1) , static_cast(node2) ) < 0; } diff --git a/lib/ecl/tests/ecl_smspec.cpp b/lib/ecl/tests/ecl_smspec.cpp index 997b060c80..ecfe60ce9e 100644 --- a/lib/ecl/tests/ecl_smspec.cpp +++ b/lib/ecl/tests/ecl_smspec.cpp @@ -23,28 +23,13 @@ #include #include -void test_sort( ecl_smspec_type * smspec ) -{ - int num_nodes = ecl_smspec_num_nodes( smspec ); - ecl_smspec_sort( smspec ); - test_assert_int_equal( num_nodes, ecl_smspec_num_nodes( smspec )); - - for (int i=1; i < ecl_smspec_num_nodes( smspec ); i++) { - const ecl::smspec_node_type * node1 = ecl_smspec_iget_node_w_node_index( smspec, i - 1 ); - const ecl::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 ); - } -} - 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 ecl::smspec_node_type * node = ecl_smspec_iget_node_w_node_index(smspec1, i); - ecl_sum_add_smspec_node(ecl_sum2, node); + const ecl::smspec_node& 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)); ecl_sum_free(ecl_sum2); @@ -63,8 +48,6 @@ int main(int argc, char ** argv) { test_assert_false( ecl_smspec_equal( smspec1 , smspec2 )); test_assert_false( ecl_smspec_equal( smspec2 , smspec1 )); - test_sort( smspec1 ); - test_sort( smspec2 ); ecl_smspec_free( smspec1 ); ecl_smspec_free( smspec2 ); } diff --git a/lib/ecl/tests/ecl_smspec_node.cpp b/lib/ecl/tests/ecl_smspec_node.cpp index dd5e5aef76..2c1647d224 100644 --- a/lib/ecl/tests/ecl_smspec_node.cpp +++ b/lib/ecl/tests/ecl_smspec_node.cpp @@ -95,92 +95,69 @@ static void test_identify_total_variable() { void test_cmp_types() { const int dims[3] = {10,10,10}; - ecl::smspec_node_type * field_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_FIELD_VAR , NULL , "FOPT" , "UNIT" , ":" , dims , 0 , 0 , 0 ); - ecl::smspec_node_type * region_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_REGION_VAR , NULL , "RPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * group_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_GROUP_VAR , "G1" , "GOPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * well_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_WELL_VAR , "W1" , "WOPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * block_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_BLOCK_VAR , NULL , "BPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * aquifer_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_AQUIFER_VAR , NULL , "AAQP" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * segment_node = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_SEGMENT_VAR , "W1" , "SGOR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * misc_node1 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_MISC_VAR , NULL , "TIME" , "UNIT" , ":", dims, 10 , 0, 0); - ecl::smspec_node_type * misc_node2 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_MISC_VAR , NULL , "TCPU" , "UNIT" , ":", dims, 10 , 0, 0); - - test_assert_int_equal( smspec_node_cmp( field_node , field_node ), 0); - test_assert_int_equal( smspec_node_cmp( region_node , region_node ), 0); - test_assert_int_equal( smspec_node_cmp( well_node , well_node ), 0); - test_assert_int_equal( smspec_node_cmp( group_node , group_node ), 0); - test_assert_int_equal( smspec_node_cmp( block_node , block_node ), 0); - - test_assert_true( smspec_node_cmp( misc_node1 , field_node ) < 0 ); - test_assert_true( smspec_node_cmp( field_node , region_node ) < 0 ); - test_assert_true( smspec_node_cmp( region_node , group_node ) < 0 ); - test_assert_true( smspec_node_cmp( group_node , well_node ) < 0 ); - test_assert_true( smspec_node_cmp( well_node , segment_node ) < 0 ); - test_assert_true( smspec_node_cmp( segment_node , block_node ) < 0 ); - test_assert_true( smspec_node_cmp( block_node , aquifer_node) < 0 ); - test_assert_true( smspec_node_cmp( aquifer_node , misc_node2 ) < 0 ); - - test_assert_true( smspec_node_cmp( field_node, misc_node1) > 0 ); - test_assert_true( smspec_node_cmp( misc_node2, aquifer_node) > 0 ); - test_assert_true( smspec_node_cmp( misc_node1, misc_node2) < 0 ); - test_assert_true( smspec_node_cmp( misc_node2, misc_node1) > 0 ); - - smspec_node_free( segment_node ); - smspec_node_free( aquifer_node ); - smspec_node_free( block_node ); - smspec_node_free( group_node ); - smspec_node_free( well_node ); - smspec_node_free( region_node ); - smspec_node_free( field_node ); - smspec_node_free( misc_node1 ); - smspec_node_free( misc_node2 ); + ecl::smspec_node field_node( 0, "FOPT" , "UNIT" , 0); + ecl::smspec_node region_node( 0, "RPR" , 10, "UNIT" , dims , 0 , ":"); + ecl::smspec_node group_node( 0, "GOPR" , "G1", "UNIT" , 0, ":"); + ecl::smspec_node well_node( 0, "WOPR" , "W1", "UNIT" , 0, ":"); + ecl::smspec_node block_node( 0, "BPR", 10, "UNIT", dims, 0, ":"); + ecl::smspec_node aquifer_node( 0, "AAQP" , 10, "UNIT" , dims, 0 , ":"); + ecl::smspec_node segment_node( 0, "SGOR" , "W1" , 10, "UNIT" , dims , 0 , ":"); + ecl::smspec_node misc_node1( 0, "TIME" , "UNIT", 0 ); + ecl::smspec_node misc_node2( 0, "TCPU", "UNIT", 0); + + test_assert_int_equal( field_node.cmp(field_node), 0); + test_assert_int_equal( region_node.cmp(region_node), 0); + test_assert_int_equal( well_node.cmp(well_node), 0); + test_assert_int_equal( group_node.cmp(group_node), 0); + test_assert_int_equal( block_node.cmp(block_node), 0); + + test_assert_true( misc_node1.cmp(field_node) < 0 ); + test_assert_true( field_node.cmp(region_node) < 0 ); + test_assert_true( region_node.cmp(group_node) < 0 ); + test_assert_true( group_node.cmp(well_node) < 0 ); + test_assert_true( well_node.cmp(segment_node) < 0 ); + test_assert_true( segment_node.cmp(block_node) < 0 ); + test_assert_true( block_node.cmp(aquifer_node)< 0 ); + test_assert_true( aquifer_node.cmp(misc_node2) < 0 ); + + test_assert_true( field_node.cmp(misc_node1) > 0 ); + test_assert_true( misc_node2.cmp(aquifer_node) > 0 ); + test_assert_true( misc_node1.cmp(misc_node2) < 0 ); + test_assert_true( misc_node2.cmp(misc_node1) > 0 ); } void test_cmp_well() { - const int dims[3] = {10,10,10}; - ecl::smspec_node_type * well_node1_1 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_WELL_VAR , "W1" , "WOPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * well_node1_2 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_WELL_VAR , "W2" , "WOPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * well_node2_1 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_WELL_VAR , "W1" , "WWCT" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * well_node2_2 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_WELL_VAR , "W2" , "WWWT" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * well_node_dummy = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_WELL_VAR , DUMMY_WELL , "WOPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - - test_assert_NULL( well_node_dummy); - test_assert_int_equal( smspec_node_cmp( well_node1_1 , well_node1_1 ), 0); - test_assert_int_equal( smspec_node_cmp( well_node2_2 , well_node2_2 ), 0); - - test_assert_true( smspec_node_cmp( well_node1_1, well_node1_2) < 0 ); - test_assert_true( smspec_node_cmp( well_node1_1, well_node2_1) < 0 ); - test_assert_true( smspec_node_cmp( well_node1_1, well_node2_2) < 0 ); - - test_assert_true( smspec_node_cmp( well_node1_2, well_node1_1) > 0 ); - test_assert_true( smspec_node_cmp( well_node1_2, well_node2_1) < 0 ); - - smspec_node_free( well_node1_1 ); - smspec_node_free( well_node2_1 ); - smspec_node_free( well_node1_2 ); - smspec_node_free( well_node2_2 ); + ecl::smspec_node well_node1_1( 0 , "WOPR" ,"W1" , "UNIT" , 10 ,":"); + ecl::smspec_node well_node1_2( 0 , "WOPR" ,"W2" , "UNIT" , 10 ,":"); + ecl::smspec_node well_node2_1( 0 , "WWCT" ,"W1" , "UNIT" , 10 ,":"); + ecl::smspec_node well_node2_2( 0 , "WWWT" ,"W2" , "UNIT" , 10 ,":"); + + test_assert_int_equal( well_node1_1.cmp(well_node1_1), 0); + test_assert_int_equal( well_node2_2.cmp(well_node2_2), 0); + + test_assert_true( well_node1_1.cmp(well_node1_2)< 0 ); + test_assert_true( well_node1_1.cmp(well_node2_1)< 0 ); + test_assert_true( well_node1_1.cmp(well_node2_2)< 0 ); + + test_assert_true( well_node1_2.cmp(well_node1_1)> 0 ); + test_assert_true( well_node1_2.cmp(well_node2_1)< 0 ); } void test_cmp_region() { const int dims[3] = {10,10,10}; - ecl::smspec_node_type * region_node1_1 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_REGION_VAR , NULL , "ROIP" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * region_node1_2 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_REGION_VAR , NULL , "ROIP" , "UNIT" , ":" , dims , 11 , 0 , 0 ); - ecl::smspec_node_type * region_node2_1 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_REGION_VAR , NULL , "RPR" , "UNIT" , ":" , dims , 10 , 0 , 0 ); - ecl::smspec_node_type * region_node2_2 = (ecl::smspec_node_type*)smspec_node_alloc( ECL_SMSPEC_REGION_VAR , NULL , "RPR" , "UNIT" , ":" , dims , 12 , 0 , 0 ); - - test_assert_true( smspec_node_cmp( region_node1_1, region_node1_2) < 0 ); - test_assert_true( smspec_node_cmp( region_node1_1, region_node2_1) < 0 ); - test_assert_true( smspec_node_cmp( region_node1_1, region_node2_2) < 0 ); - - test_assert_true( smspec_node_cmp( region_node1_2, region_node1_1) > 0 ); - test_assert_true( smspec_node_cmp( region_node1_2, region_node2_1) < 0 ); - - smspec_node_free( region_node1_1 ); - smspec_node_free( region_node2_1 ); - smspec_node_free( region_node1_2 ); - smspec_node_free( region_node2_2 ); + ecl::smspec_node region_node1_1( 0, "ROIP" , 10 ,"UNIT" , dims , 0 , ":" ); + ecl::smspec_node region_node1_2( 0, "ROIP" , 11 ,"UNIT" , dims , 0 , ":" ); + ecl::smspec_node region_node2_1( 0, "RPR" , 10 ,"UNIT" , dims , 0 , ":" ); + ecl::smspec_node region_node2_2( 0, "RPR" , 12 ,"UNIT" , dims , 0 , ":" ); + + test_assert_true( region_node1_1.cmp(region_node1_2)< 0 ); + test_assert_true( region_node1_1.cmp(region_node2_1)< 0 ); + test_assert_true( region_node1_1.cmp(region_node2_2)< 0 ); + + test_assert_true( region_node1_2.cmp(region_node1_1)> 0 ); + test_assert_true( region_node1_2.cmp(region_node2_1)< 0 ); } diff --git a/lib/ecl/tests/ecl_sum_alloc_resampled_test.cpp b/lib/ecl/tests/ecl_sum_alloc_resampled_test.cpp index 0a7a4411b8..ae98f6acb4 100644 --- a/lib/ecl/tests/ecl_sum_alloc_resampled_test.cpp +++ b/lib/ecl/tests/ecl_sum_alloc_resampled_test.cpp @@ -3,6 +3,7 @@ #include #include +#include ecl_sum_type * test_alloc_ecl_sum() { time_t start_time = util_make_date_utc( 1,1,2010 ); @@ -12,16 +13,16 @@ ecl_sum_type * test_alloc_ecl_sum() { int num_dates = 4; double ministep_length = 86400; // seconds in a day - ecl::smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "FOPT" , NULL , 0 , "Barrels" , 99.0 ); - ecl::smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 567 , "BARS" , 0.0 ); - ecl::smspec_node_type * node3 = ecl_sum_add_var( ecl_sum , "WWCT" , "OP-1" , 0 , "(1)" , 0.0 ); + const ecl::smspec_node * node1 = ecl_sum_add_var( ecl_sum , "FOPT" , NULL , 0 , "Barrels" , 99.0 ); + const ecl::smspec_node * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 567 , "BARS" , 0.0 ); + const ecl::smspec_node * node3 = ecl_sum_add_var( ecl_sum , "WWCT" , "OP-1" , 0 , "(1)" , 0.0 ); for (int report_step = 0; report_step < num_dates; report_step++) { { ecl_sum_tstep_type * tstep = ecl_sum_add_tstep( ecl_sum , report_step + 1 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node1 , report_step*2.0 ); - ecl_sum_tstep_set_from_node( tstep , node2 , report_step*4.0 + 2.0 ); - ecl_sum_tstep_set_from_node( tstep , node3 , report_step*6.0 + 4.0 ); + ecl_sum_tstep_set_from_node( tstep , *node1 , report_step*2.0 ); + ecl_sum_tstep_set_from_node( tstep , *node2 , report_step*4.0 + 2.0 ); + ecl_sum_tstep_set_from_node( tstep , *node3 , report_step*6.0 + 4.0 ); } sim_seconds += ministep_length * 3; @@ -41,15 +42,15 @@ 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 ecl::smspec_node_type * node1 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 1); - const ecl::smspec_node_type * node2 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 2); - const ecl::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) ); + const ecl::smspec_node& node1 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 1); + const ecl::smspec_node& node2 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 2); + const ecl::smspec_node& 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) ); - test_assert_double_equal(3.33333, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 6,1,2010 ), node1) ); - test_assert_double_equal(3.33333, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 2,1,2010 ), node2) ); - test_assert_double_equal(10.0000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 4,1,2010 ), node3) ); + test_assert_double_equal(3.33333, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 6,1,2010 ), &node1) ); + test_assert_double_equal(3.33333, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 2,1,2010 ), &node2) ); + test_assert_double_equal(10.0000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 4,1,2010 ), &node3) ); ecl_sum_free(ecl_sum_resampled); @@ -71,19 +72,19 @@ void test_resample_extrapolate_rate() { const ecl_smspec_type * smspec_resampled = ecl_sum_get_smspec(ecl_sum_resampled); - const smspec_node_type * node1 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 1); - const smspec_node_type * node3 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 3); + const ecl::smspec_node& node1 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 1); + const ecl::smspec_node& node3 = ecl_smspec_iget_node_w_params_index(smspec_resampled, 3); //testing extrapolation for rate wrt. 3 dates: lower, inside and upper - test_assert_double_equal(0, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 1, 1, 2009), node3)); - test_assert_double_equal(10.000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 4, 1, 2010), node3)); - test_assert_double_equal(0, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 12, 1, 2010), node3)); + test_assert_double_equal(0, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 1, 1, 2009), &node3)); + test_assert_double_equal(10.000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 4, 1, 2010), &node3)); + test_assert_double_equal(0, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 12, 1, 2010), &node3)); //testing extrapolation for variable wrt. 3 dates: lower, inside and upper - test_assert_double_equal(0, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 1, 1, 2009 ), node1) ); - test_assert_double_equal(2.000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 4, 1,2010 ), node1) ); - test_assert_double_equal(6.000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 12, 1,2010 ), node1) ); + test_assert_double_equal(0, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 1, 1, 2009 ), &node1) ); + test_assert_double_equal(2.000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 4, 1,2010 ), &node1) ); + test_assert_double_equal(6.000, ecl_sum_get_from_sim_time( ecl_sum_resampled, util_make_date_utc( 12, 1,2010 ), &node1) ); ecl_sum_free(ecl_sum_resampled); diff --git a/lib/ecl/tests/ecl_sum_data_intermediate_test.cpp b/lib/ecl/tests/ecl_sum_data_intermediate_test.cpp index f9305a0f52..69234d9bd8 100644 --- a/lib/ecl/tests/ecl_sum_data_intermediate_test.cpp +++ b/lib/ecl/tests/ecl_sum_data_intermediate_test.cpp @@ -81,16 +81,16 @@ void write_CASE1(bool unified) { int num_dates = 100; double ministep_length = 86400; // seconds in a day - ecl::smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 1 , "BARS" , 0.0 ); - ecl::smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 2 , "BARS" , 0.0 ); - ecl::smspec_node_type * node3 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 3 , "BARS" , 0.0 ); + const ecl::smspec_node * node1 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 1 , "BARS" , 0.0 ); + const ecl::smspec_node * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 2 , "BARS" , 0.0 ); + const ecl::smspec_node * node3 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 3 , "BARS" , 0.0 ); for (int report_step = 0; report_step < num_dates; report_step++) { { ecl_sum_tstep_type * tstep = ecl_sum_add_tstep( ecl_sum , report_step + 1 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node1 , (1 + report_step) * 100 ); - ecl_sum_tstep_set_from_node( tstep , node2 , (1 + report_step) * 100 + 10.0 ); - ecl_sum_tstep_set_from_node( tstep , node3 , (1 + report_step) * 100 + 20.0 ); + ecl_sum_tstep_set_from_node( tstep , *node1 , (1 + report_step) * 100 ); + ecl_sum_tstep_set_from_node( tstep , *node2 , (1 + report_step) * 100 + 10.0 ); + ecl_sum_tstep_set_from_node( tstep , *node3 , (1 + report_step) * 100 + 20.0 ); } sim_seconds += ministep_length * 3; } @@ -144,14 +144,14 @@ void write_CASE2(bool unified) { double sim_seconds = ministep_length * 2.5 * 3; ecl_sum_type * ecl_sum = ecl_sum_alloc_restart_writer( "CASE2" , "CASE1", false , true , ":" , start_time , true , 10 , 10 , 10 ); - ecl::smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 2 , "BARS" , 0.0 ); - ecl::smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 1 , "BARS" , 0.0 ); + const ecl::smspec_node * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 2 , "BARS" , 0.0 ); + const ecl::smspec_node * node1 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 1 , "BARS" , 0.0 ); for (int report_step = 1; report_step <= num_dates; report_step++) { { ecl_sum_tstep_type * tstep = ecl_sum_add_tstep( ecl_sum , report_step + 3 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node1 , report_step*1000); - ecl_sum_tstep_set_from_node( tstep , node2 , report_step*1000 + 100); + ecl_sum_tstep_set_from_node( tstep , *node1 , report_step*1000); + ecl_sum_tstep_set_from_node( tstep , *node2 , report_step*1000 + 100); } sim_seconds += ministep_length * 3; } @@ -183,8 +183,8 @@ void verify_CASE3(int length) { ieq(d,2,(2 - i)*10 + 300); if (i == 0) { - const ecl::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); + const ecl::smspec_node& node = ecl_smspec_iget_node_w_params_index(ecl_sum_get_smspec(sum), i); + double default_value = node.get_default(); ieq(d,3,default_value); ieq(d,4,default_value); } else { @@ -198,7 +198,7 @@ void verify_CASE3(int length) { double_vector_free(d); } - ecl_sum_vector_type * vector = ecl_sum_vector_alloc(sum, true); + 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); @@ -216,16 +216,16 @@ void write_CASE3(bool unified) { double sim_seconds = ministep_length * 4.0 * 3; ecl_sum_type * ecl_sum = ecl_sum_alloc_restart_writer( "CASE3" , "CASE2", false , true , ":" , start_time , true , 10 , 10 , 10 ); - ecl::smspec_node_type * node3 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 3 , "BARS" , 0.0 ); - ecl::smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 2 , "BARS" , 0.0 ); - ecl::smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 1 , "BARS" , 0.0 ); + const ecl::smspec_node * node3 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 3 , "BARS" , 0.0 ); + const ecl::smspec_node * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 2 , "BARS" , 0.0 ); + const ecl::smspec_node * node1 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 1 , "BARS" , 0.0 ); for (int report_step = 1; report_step <= num_dates; report_step++) { { ecl_sum_tstep_type * tstep = ecl_sum_add_tstep( ecl_sum , report_step + 5 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node1 , report_step*10000); - ecl_sum_tstep_set_from_node( tstep , node2 , report_step*10000 + 1000); - ecl_sum_tstep_set_from_node( tstep , node3 , report_step*10000 + 2000); + ecl_sum_tstep_set_from_node( tstep , *node1 , report_step*10000); + ecl_sum_tstep_set_from_node( tstep , *node2 , report_step*10000 + 1000); + ecl_sum_tstep_set_from_node( tstep , *node3 , report_step*10000 + 2000); } sim_seconds += ministep_length * 3; } @@ -249,7 +249,7 @@ void verify_CASE4() { ieq(d, 8, 40000); double_vector_free(d); - ecl_sum_vector_type * vector = ecl_sum_vector_alloc(sum, true); + 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); diff --git a/lib/ecl/tests/ecl_sum_writer.cpp b/lib/ecl/tests/ecl_sum_writer.cpp index 3746e4fdee..4b92655134 100644 --- a/lib/ecl/tests/ecl_sum_writer.cpp +++ b/lib/ecl/tests/ecl_sum_writer.cpp @@ -32,10 +32,13 @@ double write_summary( const char * name , time_t start_time , int nx , int ny , int nz , int num_dates, int num_ministep, double ministep_length) { ecl_sum_type * ecl_sum = ecl_sum_alloc_writer( name , false , true , ":" , start_time , true , nx , ny , nz ); double sim_seconds = 0; + ecl_smspec_type * smspec = ecl_sum_get_smspec( ecl_sum ); - ecl::smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "FOPT" , NULL , 0 , "Barrels" , 99.0 ); - ecl::smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 567 , "BARS" , 0.0 ); - ecl::smspec_node_type * node3 = ecl_sum_add_var( ecl_sum , "WWCT" , "OP-1" , 0 , "(1)" , 0.0 ); + test_assert_int_equal( ecl_smspec_get_params_size( smspec ), 1); + const ecl::smspec_node * node1 = ecl_smspec_add_node( smspec , "FOPT" , "Barrels", 99.0); + const ecl::smspec_node * node2 = ecl_smspec_add_node( smspec , "BPR" , 567 , "BARS", 0.0 ); + const ecl::smspec_node * node3 = ecl_smspec_add_node( smspec , "WWCT" , "OP-1" , "(1)" , 0.0 ); + test_assert_int_equal( ecl_smspec_get_params_size( smspec ), 4); for (int report_step = 0; report_step < num_dates; report_step++) { for (int step = 0; step < num_ministep; step++) { @@ -43,13 +46,13 @@ double write_summary( const char * name , time_t start_time , int nx , int ny , { ecl_sum_tstep_type * tstep = ecl_sum_add_tstep( ecl_sum , report_step + 1 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node1 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node2 , 10*sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node3 , 100*sim_seconds ); + ecl_sum_tstep_set_from_node( tstep , *node1 , sim_seconds ); + ecl_sum_tstep_set_from_node( tstep , *node2 , 10*sim_seconds ); + ecl_sum_tstep_set_from_node( tstep , *node3 , 100*sim_seconds ); - test_assert_double_equal( ecl_sum_tstep_get_from_node( tstep , node1 ), sim_seconds ); - test_assert_double_equal( ecl_sum_tstep_get_from_node( tstep , node2 ), sim_seconds*10 ); - test_assert_double_equal( ecl_sum_tstep_get_from_node( tstep , node3 ), sim_seconds*100 ); + test_assert_double_equal( ecl_sum_tstep_get_from_node( tstep , *node1 ), sim_seconds ); + test_assert_double_equal( ecl_sum_tstep_get_from_node( tstep , *node2 ), sim_seconds*10 ); + test_assert_double_equal( ecl_sum_tstep_get_from_node( tstep , *node3 ), sim_seconds*100 ); } sim_seconds += ministep_length; } @@ -61,20 +64,20 @@ double write_summary( const char * name , time_t start_time , int nx , int ny , int write_restart_summary(const char * name, const char * restart_name , int start_report_step, double sim_seconds, time_t start_time , int nx , int ny , int nz , int num_dates, int num_ministep, double ministep_length) { ecl_sum_type * ecl_sum = ecl_sum_alloc_restart_writer( name , restart_name, false , true , ":" , start_time , true , nx , ny , nz ); + ecl_smspec_type * smspec = ecl_sum_get_smspec( ecl_sum ); - - ecl::smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "FOPT" , NULL , 0 , "Barrels" , 99.0 ); - ecl::smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 567 , "BARS" , 0.0 ); - ecl::smspec_node_type * node3 = ecl_sum_add_var( ecl_sum , "WWCT" , "OP-1" , 0 , "(1)" , 0.0 ); + const ecl::smspec_node * node1 = ecl_smspec_add_node( smspec , "FOPT", "Barrels" , 99.0 ); + const ecl::smspec_node * node2 = ecl_smspec_add_node( smspec , "BPR" , 567 , "BARS" , 0.0 ); + const ecl::smspec_node * node3 = ecl_smspec_add_node( smspec , "WWCT" , "OP-1" , "(1)", 0.0 ); int num_report_steps = start_report_step + num_dates; for (int report_step = start_report_step; report_step < num_report_steps; report_step++) { for (int step = 0; step < num_ministep; step++) { { ecl_sum_tstep_type * tstep = ecl_sum_add_tstep( ecl_sum , report_step + 1 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node1 , sim_seconds); - ecl_sum_tstep_set_from_node( tstep , node2 , 10*sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node3 , 100*sim_seconds ); + ecl_sum_tstep_set_from_node( tstep , *node1 , sim_seconds); + ecl_sum_tstep_set_from_node( tstep , *node2 , 10*sim_seconds ); + ecl_sum_tstep_set_from_node( tstep , *node3 , 100*sim_seconds ); } sim_seconds += ministep_length; @@ -97,17 +100,25 @@ void test_write_read( ) { int num_ministep = 10; double ministep_length = 86400; // Seconds - numerical value chosen to avoid rounding problems when converting between seconds and days. { - test_work_area_type * work_area = test_work_area_alloc("sum/write"); + //test_work_area_type * work_area = test_work_area_alloc("sum/write"); ecl_sum_type * ecl_sum; - write_summary( name , start_time , nx , ny , nz , num_dates , num_ministep , ministep_length); + auto seconds = write_summary( name , start_time , nx , ny , nz , num_dates , num_ministep , ministep_length); ecl_sum = ecl_sum_fread_alloc_case( name , ":" ); test_assert_true( ecl_sum_is_instance( ecl_sum )); + printf("days: %g \n", seconds / 86400.0 ); + /* Time direction */ test_assert_time_t_equal( start_time , ecl_sum_get_start_time(ecl_sum)); test_assert_time_t_equal( start_time , ecl_sum_get_data_start(ecl_sum)); util_inplace_forward_seconds_utc(&end_time, (num_dates * num_ministep - 1) * ministep_length ); + { + time_t et = ecl_sum_get_end_time(ecl_sum); + printf("days: %g \n", 1.0 * (et - start_time) / 86400); + printf("days: %g \n", 1.0 * (end_time - start_time) / 86400); + printf("%ld %ld diff:%ld fraction:%g \n", end_time, et, end_time - et , 1.0 * et / end_time); + } test_assert_time_t_equal( end_time , ecl_sum_get_end_time(ecl_sum)); /* Keys */ @@ -126,7 +137,7 @@ void test_write_read( ) { } ecl_sum_free( ecl_sum ); - test_work_area_free( work_area ); + //test_work_area_free( work_area ); } } @@ -216,6 +227,7 @@ void test_long_restart_names() { } int main( int argc , char ** argv) { + util_install_signals(); test_write_read(); test_ecl_sum_alloc_restart_writer(); test_long_restart_names(); diff --git a/lib/ecl/tests/ecl_unsmry_loader_test.cpp b/lib/ecl/tests/ecl_unsmry_loader_test.cpp index b7d887275e..036d486975 100644 --- a/lib/ecl/tests/ecl_unsmry_loader_test.cpp +++ b/lib/ecl/tests/ecl_unsmry_loader_test.cpp @@ -17,16 +17,16 @@ ecl_sum_type * write_ecl_sum() { int num_dates = 4; double ministep_length = 86400; // seconds in a day - ecl::smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "FOPT" , NULL , 0 , "Barrels" , 99.0 ); - ecl::smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 567 , "BARS" , 0.0 ); - ecl::smspec_node_type * node3 = ecl_sum_add_var( ecl_sum , "WWCT" , "OP-1" , 0 , "(1)" , 0.0 ); + const ecl::smspec_node * node1 = ecl_sum_add_var( ecl_sum , "FOPT" , NULL , 0 , "Barrels" , 99.0 ); + const ecl::smspec_node * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 567 , "BARS" , 0.0 ); + const ecl::smspec_node * node3 = ecl_sum_add_var( ecl_sum , "WWCT" , "OP-1" , 0 , "(1)" , 0.0 ); for (int report_step = 0; report_step < num_dates; report_step++) { { ecl_sum_tstep_type * tstep = ecl_sum_add_tstep( ecl_sum , report_step + 1 , sim_seconds ); - ecl_sum_tstep_set_from_node( tstep , node1 , report_step*2.0 ); - ecl_sum_tstep_set_from_node( tstep , node2 , report_step*4.0 + 2.0 ); - ecl_sum_tstep_set_from_node( tstep , node3 , report_step*6.0 + 4.0 ); + ecl_sum_tstep_set_from_node( tstep , *node1 , report_step*2.0 ); + ecl_sum_tstep_set_from_node( tstep , *node2 , report_step*4.0 + 2.0 ); + ecl_sum_tstep_set_from_node( tstep , *node3 , report_step*6.0 + 4.0 ); } sim_seconds += ministep_length * 3; diff --git a/lib/include/ert/ecl/ecl_smspec.hpp b/lib/include/ert/ecl/ecl_smspec.hpp index dda2927190..9b95c77d15 100644 --- a/lib/include/ert/ecl/ecl_smspec.hpp +++ b/lib/include/ert/ecl/ecl_smspec.hpp @@ -34,6 +34,18 @@ typedef struct ecl_smspec_struct ecl_smspec_type; #ifdef __cplusplus #include const std::vector& ecl_smspec_get_params_default( const ecl_smspec_type * ecl_smspec ); +const ecl::smspec_node& ecl_smspec_get_well_var_node( const ecl_smspec_type * smspec , const char * well , const char * var); +const ecl::smspec_node& ecl_smspec_get_group_var_node( const ecl_smspec_type * smspec , const char * group , const char * var); +const ecl::smspec_node& ecl_smspec_get_field_var_node( const ecl_smspec_type * smspec , const char * var); +const ecl::smspec_node& ecl_smspec_get_region_var_node(const ecl_smspec_type * ecl_smspec , const char *region_var , int region_nr); +const ecl::smspec_node& ecl_smspec_get_misc_var_node(const ecl_smspec_type * ecl_smspec , const char *var); +const ecl::smspec_node& ecl_smspec_get_block_var_node(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr); +const ecl::smspec_node& ecl_smspec_get_block_var_node_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k); +const ecl::smspec_node& ecl_smspec_get_well_completion_var_node(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr); +const ecl::smspec_node& ecl_smspec_get_general_var_node( const ecl_smspec_type * smspec , const char * lookup_kw ); +const ecl::smspec_node& ecl_smspec_iget_node_w_node_index( const ecl_smspec_type * smspec , int node_index ); +const ecl::smspec_node& ecl_smspec_iget_node_w_params_index( const ecl_smspec_type * smspec , int params_index ); +const ecl::smspec_node& ecl_smspec_iget_node(const ecl_smspec_type * smspec, int index); #endif #ifdef __cplusplus @@ -53,9 +65,6 @@ extern "C" { int * ecl_smspec_alloc_mapping( const ecl_smspec_type * self, const ecl_smspec_type * other); const int * ecl_smspec_get_index_map( const ecl_smspec_type * smspec ); - void ecl_smspec_index_node( ecl_smspec_type * ecl_smspec , ecl::smspec_node_type * smspec_node); - void ecl_smspec_insert_node(ecl_smspec_type * ecl_smspec, ecl::smspec_node_type * smspec_node); - void ecl_smspec_add_node( ecl_smspec_type * ecl_smspec , ecl::smspec_node_type * smspec_node ); ecl_smspec_var_type ecl_smspec_iget_var_type( const ecl_smspec_type * smspec , int index ); bool ecl_smspec_needs_num( ecl_smspec_var_type var_type ); bool ecl_smspec_needs_wgname( ecl_smspec_var_type var_type ); @@ -76,39 +85,30 @@ extern "C" { int ecl_smspec_get_date_year_index( const ecl_smspec_type * smspec ); - const ecl::smspec_node_type * ecl_smspec_get_well_var_node( const ecl_smspec_type * smspec , const char * well , const char * var); int ecl_smspec_get_well_var_params_index(const ecl_smspec_type * ecl_smspec , const char * well , const char *var); bool ecl_smspec_has_well_var(const ecl_smspec_type * ecl_smspec , const char * well , const char *var); - const ecl::smspec_node_type * ecl_smspec_get_group_var_node( const ecl_smspec_type * smspec , const char * group , const char * var); int ecl_smspec_get_group_var_params_index(const ecl_smspec_type * ecl_smspec , const char * group , const char *var); bool ecl_smspec_has_group_var(const ecl_smspec_type * ecl_smspec , const char * group , const char *var); - const ecl::smspec_node_type * ecl_smspec_get_field_var_node( const ecl_smspec_type * smspec , const char * var); int ecl_smspec_get_field_var_params_index(const ecl_smspec_type * ecl_smspec , const char *var); bool ecl_smspec_has_field_var(const ecl_smspec_type * ecl_smspec , const char *var); - const ecl::smspec_node_type * ecl_smspec_get_region_var_node(const ecl_smspec_type * ecl_smspec , const char *region_var , int region_nr); int ecl_smspec_get_region_var_params_index(const ecl_smspec_type * ecl_smspec , const char * region_var , int region_nr); bool ecl_smspec_has_region_var(const ecl_smspec_type * ecl_smspec , const char * region_var , int region_nr); - const ecl::smspec_node_type * ecl_smspec_get_misc_var_node(const ecl_smspec_type * ecl_smspec , const char *var); int ecl_smspec_get_misc_var_params_index(const ecl_smspec_type * ecl_smspec , const char *var); bool ecl_smspec_has_misc_var(const ecl_smspec_type * ecl_smspec , const char *var); - const ecl::smspec_node_type * ecl_smspec_get_block_var_node(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr); int ecl_smspec_get_block_var_params_index(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr); bool ecl_smspec_has_block_var(const ecl_smspec_type * ecl_smspec , const char * block_var , int block_nr); - const ecl::smspec_node_type * ecl_smspec_get_block_var_node_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k); int ecl_smspec_get_block_var_params_index_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k); bool ecl_smspec_has_block_var_ijk(const ecl_smspec_type * ecl_smspec , const char * block_var , int i , int j , int k); - const ecl::smspec_node_type * ecl_smspec_get_well_completion_var_node(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr); int ecl_smspec_get_well_completion_var_params_index(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr); bool ecl_smspec_has_well_completion_var(const ecl_smspec_type * ecl_smspec , const char * well , const char *var, int cell_nr); - const ecl::smspec_node_type * ecl_smspec_get_general_var_node( const ecl_smspec_type * smspec , const char * lookup_kw ); int ecl_smspec_get_general_var_params_index(const ecl_smspec_type * ecl_smspec , const char * lookup_kw); bool ecl_smspec_has_general_var(const ecl_smspec_type * ecl_smspec , const char * lookup_kw); const char * ecl_smspec_get_general_var_unit( const ecl_smspec_type * ecl_smspec , const char * lookup_kw); @@ -125,7 +125,6 @@ extern "C" { - void ecl_smspec_init_var( ecl_smspec_type * ecl_smspec , ecl::smspec_node_type * smspec_node , const char * keyword , const char * wgname , int num, const char * unit ); void ecl_smspec_select_matching_general_var_list( const ecl_smspec_type * smspec , const char * pattern , stringlist_type * keys); stringlist_type * ecl_smspec_alloc_matching_general_var_list(const ecl_smspec_type * smspec , const char * pattern); @@ -147,18 +146,43 @@ extern "C" { 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 ecl::smspec_node_type * ecl_smspec_iget_node_w_node_index( const ecl_smspec_type * smspec , int node_index ); - const ecl::smspec_node_type * ecl_smspec_iget_node_w_params_index( const ecl_smspec_type * smspec , int params_index ); - const ecl::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 ); + // 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 + +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, int num, const char * unit, float default_value); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, const char * unit, float default_value); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, const char * wgname, const char * unit, float default_value); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, const char * wgname, int num, const char * unit, float default_value); +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, const char * keyword, int num, const char * unit, float default_value); + +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, + int params_index, + const char * keyword, + const char * wgname, + int num, + const char * unit, + float default_value); + +const ecl::smspec_node * ecl_smspec_add_node(ecl_smspec_type * ecl_smspec, + int params_index, + const char * keyword, + const char * wgname, + int num, + const char * unit, + const char * lgr, + int lgr_i, int lgr_j, int lgr_k, + float default_value); + + + + #endif diff --git a/lib/include/ert/ecl/ecl_sum.hpp b/lib/include/ert/ecl/ecl_sum.hpp index 2ca3a2d7b1..2e857220e1 100644 --- a/lib/include/ert/ecl/ecl_sum.hpp +++ b/lib/include/ert/ecl/ecl_sum.hpp @@ -58,8 +58,8 @@ typedef struct ecl_sum_vector_struct ecl_sum_vector_type; typedef struct ecl_sum_struct ecl_sum_type; void ecl_sum_fmt_init_summary_x( const ecl_sum_type * ecl_sum , ecl_sum_fmt_type * fmt ); - double ecl_sum_get_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const ecl::smspec_node_type * node); - double ecl_sum_get_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const ecl::smspec_node_type * node); + double ecl_sum_get_from_sim_time( const ecl_sum_type * ecl_sum , time_t sim_time , const ecl::smspec_node * node); + double ecl_sum_get_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const ecl::smspec_node * node); double ecl_sum_time2days( const ecl_sum_type * ecl_sum , time_t sim_time); void ecl_sum_set_unified( ecl_sum_type * ecl_sum , bool unified ); @@ -130,7 +130,7 @@ typedef struct ecl_sum_struct ecl_sum_type; double ecl_sum_get_general_var(const ecl_sum_type * ecl_sum , int time_index , const char * lookup_kw); int ecl_sum_get_general_var_params_index(const ecl_sum_type * ecl_sum , const char * lookup_kw); - const ecl::smspec_node_type * ecl_sum_get_general_var_node(const ecl_sum_type * ecl_sum , const char * lookup_kw); + const ecl::smspec_node * ecl_sum_get_general_var_node(const ecl_sum_type * ecl_sum , const char * lookup_kw); bool ecl_sum_has_general_var(const ecl_sum_type * ecl_sum , const char * lookup_kw); bool ecl_sum_has_key(const ecl_sum_type * ecl_sum , const char * lookup_kw); double ecl_sum_get_general_var_from_sim_days( const ecl_sum_type * ecl_sum , double sim_days , const char * var); @@ -190,7 +190,7 @@ typedef struct ecl_sum_struct ecl_sum_type; stringlist_type * ecl_sum_alloc_well_var_list( const ecl_sum_type * ecl_sum ); stringlist_type * ecl_sum_alloc_matching_general_var_list(const ecl_sum_type * ecl_sum , const char * pattern); void ecl_sum_select_matching_general_var_list( const ecl_sum_type * ecl_sum , const char * pattern , stringlist_type * keys); - const ecl_smspec_type * ecl_sum_get_smspec( const ecl_sum_type * ecl_sum ); + ecl_smspec_type * ecl_sum_get_smspec( const ecl_sum_type * ecl_sum ); ecl_smspec_var_type ecl_sum_identify_var_type(const char * var); ecl_smspec_var_type ecl_sum_get_var_type( const ecl_sum_type * ecl_sum , const char * gen_key); bool ecl_sum_var_is_rate( const ecl_sum_type * ecl_sum , const char * gen_key); @@ -230,8 +230,8 @@ typedef struct ecl_sum_struct ecl_sum_type; void ecl_sum_fwrite( const ecl_sum_type * ecl_sum ); bool ecl_sum_can_write( const ecl_sum_type * ecl_sum ); void ecl_sum_fwrite_smspec( const ecl_sum_type * ecl_sum ); - ecl::smspec_node_type * ecl_sum_add_smspec_node(ecl_sum_type * ecl_sum, const ecl::smspec_node_type * node); - ecl::smspec_node_type * ecl_sum_add_var(ecl_sum_type * ecl_sum , + const ecl::smspec_node * ecl_sum_add_smspec_node(ecl_sum_type * ecl_sum, const ecl::smspec_node * node); + const ecl::smspec_node * ecl_sum_add_var(ecl_sum_type * ecl_sum , const char * keyword , const char * wgname , int num , @@ -256,10 +256,10 @@ typedef struct ecl_sum_struct ecl_sum_type; double ecl_sum_iget_last_value(const ecl_sum_type * ecl_sum, int param_index); double ecl_sum_get_last_value_gen_key(const ecl_sum_type * ecl_sum, const char * gen_key); - double ecl_sum_get_last_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node_type *node); + double ecl_sum_get_last_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node *node); double ecl_sum_iget_first_value(const ecl_sum_type * ecl_sum, int param_index); double ecl_sum_get_first_value_gen_key(const ecl_sum_type * ecl_sum, const char * gen_key); - double ecl_sum_get_first_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node_type *node); + double ecl_sum_get_first_value_node(const ecl_sum_type * ecl_sum, const ecl::smspec_node *node); void ecl_sum_init_datetime64_vector(const ecl_sum_type * ecl_sum, int64_t * data, int multiplier); void ecl_sum_init_double_vector_interp(const ecl_sum_type * ecl_sum, const char * gen_key, const time_t_vector_type * time_points, double * data); diff --git a/lib/include/ert/ecl/ecl_sum_data.hpp b/lib/include/ert/ecl/ecl_sum_data.hpp index 9ee5dbf225..b88ac62be1 100644 --- a/lib/include/ert/ecl/ecl_sum_data.hpp +++ b/lib/include/ert/ecl/ecl_sum_data.hpp @@ -75,8 +75,8 @@ typedef struct ecl_sum_data_struct ecl_sum_data_type ; int ecl_sum_data_get_last_report_step( const ecl_sum_data_type * data ); int ecl_sum_data_get_first_report_step( const ecl_sum_data_type * data ); - double ecl_sum_data_get_from_sim_time( const ecl_sum_data_type * data , time_t sim_time , const ecl::smspec_node_type * smspec_node); - double ecl_sum_data_get_from_sim_days( const ecl_sum_data_type * data , double sim_days , const ecl::smspec_node_type * smspec_node); + double ecl_sum_data_get_from_sim_time( const ecl_sum_data_type * data , time_t sim_time , const ecl::smspec_node& smspec_node); + double ecl_sum_data_get_from_sim_days( const ecl_sum_data_type * data , double sim_days , const ecl::smspec_node& smspec_node); int ecl_sum_data_get_length( const ecl_sum_data_type * data ); int ecl_sum_data_iget_report_step(const ecl_sum_data_type * data , int internal_index); @@ -92,14 +92,14 @@ typedef struct ecl_sum_data_struct ecl_sum_data_type ; void ecl_sum_data_init_datetime64_vector(const ecl_sum_data_type * data, int64_t * output_data, int multiplier); void ecl_sum_data_init_double_frame(const ecl_sum_data_type * data, const ecl_sum_vector_type * keywords, double *output_data); - double_vector_type * ecl_sum_data_alloc_seconds_solution( const ecl_sum_data_type * data , const ecl::smspec_node_type * node , double value, bool rates_clamp_lower); + double_vector_type * ecl_sum_data_alloc_seconds_solution( const ecl_sum_data_type * data , const ecl::smspec_node& node , double value, bool rates_clamp_lower); void ecl_sum_data_init_double_frame_interp(const ecl_sum_data_type * data, const ecl_sum_vector_type * keywords, const time_t_vector_type * time_points, double * output_data); void ecl_sum_data_init_double_vector_interp(const ecl_sum_data_type * data, - const ecl::smspec_node_type * smspec_node, + const ecl::smspec_node& smspec_node, const time_t_vector_type * time_points, double * output_data); diff --git a/lib/include/ert/ecl/ecl_sum_tstep.hpp b/lib/include/ert/ecl/ecl_sum_tstep.hpp index 1471e175ab..e15ad1b2da 100644 --- a/lib/include/ert/ecl/ecl_sum_tstep.hpp +++ b/lib/include/ert/ecl/ecl_sum_tstep.hpp @@ -42,6 +42,9 @@ typedef struct ecl_sum_tstep_struct ecl_sum_tstep_type; ecl_sum_tstep_type * ecl_sum_tstep_alloc_new( int report_step , int ministep , float sim_seconds , const ecl_smspec_type * smspec ); + void ecl_sum_tstep_set_from_node( ecl_sum_tstep_type * tstep , const ecl::smspec_node& smspec_node , float value); + double ecl_sum_tstep_get_from_node( const ecl_sum_tstep_type * tstep , const ecl::smspec_node& smspec_node); + double ecl_sum_tstep_iget(const ecl_sum_tstep_type * ministep , int index); time_t ecl_sum_tstep_get_sim_time(const ecl_sum_tstep_type * ministep); double ecl_sum_tstep_get_sim_days(const ecl_sum_tstep_type * ministep); @@ -59,8 +62,6 @@ typedef struct ecl_sum_tstep_struct ecl_sum_tstep_type; /// adds addend to tstep[index]; equivalent to iset( iget() + addend) void ecl_sum_tstep_ishift(ecl_sum_tstep_type * tstep, int index, float addend); - void ecl_sum_tstep_set_from_node( ecl_sum_tstep_type * tstep , const ecl::smspec_node_type * smspec_node , float value); - double ecl_sum_tstep_get_from_node( const ecl_sum_tstep_type * tstep , const ecl::smspec_node_type * smspec_node); void ecl_sum_tstep_set_from_key( ecl_sum_tstep_type * tstep , const char * gen_key , float value); double ecl_sum_tstep_get_from_key( const ecl_sum_tstep_type * tstep , const char * gen_key); @@ -75,5 +76,7 @@ typedef struct ecl_sum_tstep_struct ecl_sum_tstep_type; #ifdef __cplusplus } + + #endif #endif diff --git a/lib/include/ert/ecl/smspec_node.h b/lib/include/ert/ecl/smspec_node.h index ddc0a56b93..b058fc8898 100644 --- a/lib/include/ert/ecl/smspec_node.h +++ b/lib/include/ert/ecl/smspec_node.h @@ -76,13 +76,14 @@ typedef enum {ECL_SMSPEC_INVALID_VAR = 0 , const int grid_dims[3] , int num); - void * smspec_node_alloc( ecl_smspec_var_type var_type , - const char * wgname , - const char * keyword , - const char * unit , - const char * key_join_string , - const int grid_dims[3] , - int num , int param_index, float default_value); + void * smspec_node_alloc( int param_index, + const char * keyword , + const char * wgname, + int num, + const char * unit , + const int grid_dims[3] , + float default_value, + const char * key_join_string); void * smspec_node_alloc_lgr( ecl_smspec_var_type var_type , const char * wgname , diff --git a/lib/include/ert/ecl/smspec_node.hpp b/lib/include/ert/ecl/smspec_node.hpp index 9e802d663b..fb3b6f871a 100644 --- a/lib/include/ert/ecl/smspec_node.hpp +++ b/lib/include/ert/ecl/smspec_node.hpp @@ -23,17 +23,17 @@ #include #include +#include +#include + #include #include -#ifdef __cplusplus -#include -#include namespace ecl { - class smspec_node_type { + class smspec_node { private: std::string wgname; @@ -55,102 +55,47 @@ namespace ecl { int params_index; /* The index of this variable (applies to all the vectors - in particular the PARAMS vectors of the summary files *.Snnnn / *.UNSMRY ). */ float default_value; /* Default value for this variable. */ - smspec_node_type(); - - void set_invalid_flags(); - void init_lgr( ecl_smspec_var_type var_type , - const char * wgname , - const char * keyword , - const char * unit , - const char * lgr , - const char * key_join_string , - int lgr_i, int lgr_j , int lgr_k ); - void common_init( ecl_smspec_var_type var_type_ , const char * keyword , const std::string& unit ); + static ecl_smspec_var_type identify_special_var( const char * var ); + void set_wgname(const char * wgname); void set_num( const int grid_dims[3] , int num_); - void set_keyword( const std::string& keyword_ ); - void set_flags(); - void init_num( ecl_smspec_var_type var_type_); void set_gen_keys( const char * key_join_string_); void decode_R1R2( int * r1 , int * r2) const; void set_lgr_ijk( int lgr_i , int lgr_j , int lgr_k); + public: - int cmp__(const smspec_node_type * node2) const; - int cmp_KEYWORD_WGNAME_NUM__(const smspec_node_type * node2) const; - static int cmp_KEYWORD_WGNAME_NUM( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_KEYWORD_WGNAME_NUM__(node2); - } - int cmp_KEYWORD_WGNAME_LGR__( const smspec_node_type * node2) const; - static int cmp_KEYWORD_WGNAME_LGR( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_KEYWORD_WGNAME_LGR__(node2); - } - int cmp_KEYWORD_WGNAME_LGR_LGRIJK__( const smspec_node_type * node2) const; - static int cmp_KEYWORD_WGNAME_LGR_LGRIJK( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_KEYWORD_WGNAME_LGR_LGRIJK__(node2); - } - int cmp_KEYWORD_WGNAME__( const smspec_node_type * node2) const; - static int cmp_KEYWORD_WGNAME( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_KEYWORD_WGNAME__(node2); - } - static bool equal_MISC( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->keyword == node2->keyword; - } - int cmp_MISC__( const smspec_node_type * node2) const; - static int cmp_MISC( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_MISC__(node2); - } - int cmp_LGRIJK__( const smspec_node_type * node2) const; - static int cmp_LGRIJK( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_LGRIJK__(node2); - } - int cmp_KEYWORD_LGR_LGRIJK__( const smspec_node_type * node2) const; - static int cmp_KEYWORD_LGR_LGRIJK( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_KEYWORD_LGR_LGRIJK__(node2); - } - int cmp_KEYWORD_NUM__( const smspec_node_type * node2) const; - static int cmp_KEYWORD_NUM( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_KEYWORD_NUM__(node2); - } - static int cmp_KEYWORD( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->keyword.compare(node2->keyword); - } - int cmp_key1__( const smspec_node_type * node2) const; - static int cmp_key1( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp_key1__(node2); - } - - public: + static ecl_smspec_var_type valid_type(const char * keyword, const char * wgname, int num); + int cmp(const smspec_node& node2) const; + static int cmp(const smspec_node& node1, const smspec_node& node2); - smspec_node_type(ecl_smspec_var_type var_type , - const char * wgname , - const char * keyword , + smspec_node(int param_index, + const char * keyword , + const char * wgname, + int num, const char * unit , - const char * key_join_string , const int grid_dims[3] , - int num , int param_index, float default_value); + float default_value, + const char * key_join_string); - smspec_node_type(ecl_smspec_var_type var_type , - const char * wgname , + smspec_node(int param_index, const char * keyword , + const char * wgname , const char * unit , - const char * lgr , - const char * key_join_string , + const char * lgr , int lgr_i, int lgr_j , int lgr_k, - int param_index , float default_value); + float default_value, + const char * key_join_string); - bool init__( ecl_smspec_var_type var_type , - const char * wgname , - const char * keyword , - const char * unit , - const char * key_join_string , - const int grid_dims[3] , - int num); + smspec_node(int param_index, const char * keyword, const char * unit, float default_value); + smspec_node(int param_index, const char * keyword, int num, const char * unit, const int grid_dims[3], float default_value, const char * key_join_string); + smspec_node(int param_index, const char * keyword, int num, const char * unit, float default_value, const char * key_join_string); + smspec_node(int param_index, const char * keyword, const char * wgname, const char * unit, float default_value, const char * key_join_string); + smspec_node(int param_index, const char * keyword, const char * wgname, int num, const char * unit, float default_value, const char * key_join_string); + static ecl_smspec_var_type identify_var_type(const char * var); - smspec_node_type * copy() const; - - static int cmp( const smspec_node_type * node1, const smspec_node_type * node2) { - return node1->cmp__(node2); + static int cmp( const smspec_node * node1, const smspec_node * node2) { + return node1->cmp(*node2); } int get_R1() const; @@ -168,17 +113,13 @@ namespace ecl { bool need_nums() const; void fprintf__( FILE * stream) const; int get_params_index() const; - void set_params_index( int params_index_); float get_default() const; const std::array& get_ijk() const; - const std::string& get_lgr_name() const; + const char * get_lgr_name() const; const std::array& get_lgr_ijk() const; }; } -typedef class ecl::smspec_node_type smspec_node_type; - -#endif #endif diff --git a/lib/private-include/detail/ecl/ecl_sum_file_data.hpp b/lib/private-include/detail/ecl/ecl_sum_file_data.hpp index 3435cc6017..e323a307f6 100644 --- a/lib/private-include/detail/ecl/ecl_sum_file_data.hpp +++ b/lib/private-include/detail/ecl/ecl_sum_file_data.hpp @@ -141,7 +141,7 @@ class ecl_sum_file_data { void build_index(); void fwrite_report( int report_step , fortio_type * fortio) const; bool check_file( ecl_file_type * ecl_file ); - void add_ecl_file(int report_step, const ecl_file_view_type * summary_view, const ecl_smspec_type * smspec); + void add_ecl_file(int report_step, const ecl_file_view_type * summary_view); }; diff --git a/python/ecl/summary/ecl_sum.py b/python/ecl/summary/ecl_sum.py index a0f851f835..af24261846 100644 --- a/python/ecl/summary/ecl_sum.py +++ b/python/ecl/summary/ecl_sum.py @@ -305,9 +305,8 @@ def add_t_step(self, report_step, sim_days): raise TypeError('Parameter sim_days should be float, was %r' % sim_days) sim_seconds = sim_days * 24 * 60 * 60 - - return self._add_tstep(report_step, sim_seconds).setParent(parent=self) - + tstep = self._add_tstep(report_step, sim_seconds).setParent(parent=self) + return tstep diff --git a/python/ecl/util/test/ecl_mock/ecl_sum_mock.py b/python/ecl/util/test/ecl_mock/ecl_sum_mock.py index 9fc8b7a8b1..3096791654 100644 --- a/python/ecl/util/test/ecl_mock/ecl_sum_mock.py +++ b/python/ecl/util/test/ecl_mock/ecl_sum_mock.py @@ -39,8 +39,10 @@ def createEclSum( case, days = time_offset + report_step * report_step_length + mini_step * mini_step_length t_step = ecl_sum.addTStep( report_step + 1 , sim_days = days ) + for var in var_list: key = var.getKey1( ) + if key in func_table: func = func_table[key] t_step[key] = func( days ) diff --git a/python/tests/bin_tests/test_summary_resample.py b/python/tests/bin_tests/test_summary_resample.py index c265382c00..8552449950 100644 --- a/python/tests/bin_tests/test_summary_resample.py +++ b/python/tests/bin_tests/test_summary_resample.py @@ -60,6 +60,7 @@ def test_run_default(self): with TestAreaContext(""): self.case.fwrite() + # Too few arguments with self.assertRaises(CallError): subprocess.check_call([self.script]) diff --git a/python/tests/ecl_tests/test_sum.py b/python/tests/ecl_tests/test_sum.py index 867d9267b0..08515c2500 100644 --- a/python/tests/ecl_tests/test_sum.py +++ b/python/tests/ecl_tests/test_sum.py @@ -25,7 +25,7 @@ import pandas from pandas.testing import assert_frame_equal from contextlib import contextmanager -from unittest import skipIf, skipUnless, skipIf +from unittest import skipIf, skipUnless from ecl import EclUnitTypeEnum from ecl import EclDataType @@ -131,6 +131,7 @@ def test_identify_var_type(self): node1 = case.smspec_node( "FOPT" ) self.assertEqual( node1.varType( ) , EclSumVarType.ECL_SMSPEC_FIELD_VAR ) + self.assertIsNone(node1.wgname) node2 = case.smspec_node( "AARQ:10" ) self.assertEqual( node2.varType( ) , EclSumVarType.ECL_SMSPEC_AQUIFER_VAR ) @@ -603,8 +604,6 @@ def test_load_case(self): for time_index,value in enumerate(fopr): self.assertEqual(fopr[time_index], value) - - def test_write_not_implemented(self): path = os.path.join(self.TESTDATA_ROOT, "local/ECLIPSE/cp_simple3/SIMPLE_SUMMARY3") case = EclSum( path, lazy_load=True ) @@ -613,7 +612,6 @@ def test_write_not_implemented(self): case.fwrite( ) - def test_directory_conflict(self): with TestAreaContext("dir_conflict"): case = create_case("UNITS")