Skip to content

Commit

Permalink
Fixed bug with too long restart names
Browse files Browse the repository at this point in the history
  • Loading branch information
joakim-hove committed Feb 7, 2018
1 parent 67e9beb commit 67ee923
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 26 deletions.
20 changes: 11 additions & 9 deletions lib/ecl/ecl_smspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,14 @@ static void ecl_smspec_fortio_fwrite( const ecl_smspec_type * smspec , fortio_ty
ecl_kw_iset_string8( restart_kw , i , "");

if (smspec->restart_case != NULL) {
int restart_case_len = strlen(smspec->restart_case);
int restart_case_len = strlen(smspec->restart_case);

int offset = 0;
for (int i = 0; i < SUMMARY_RESTART_SIZE ; i++) {
if (offset < restart_case_len)
ecl_kw_iset_string8( restart_kw , i , &smspec->restart_case[ offset ]);
offset += ECL_STRING8_LENGTH;
}
}
}

ecl_kw_fwrite( restart_kw , fortio );
Expand Down Expand Up @@ -503,12 +503,14 @@ void ecl_smspec_fwrite( const ecl_smspec_type * smspec , const char * ecl_case ,

ecl_smspec_type * ecl_smspec_alloc_writer( const char * key_join_string , const char * restart_case, time_t sim_start , bool time_in_days , int nx , int ny , int nz) {
ecl_smspec_type * ecl_smspec = ecl_smspec_alloc_empty( true , key_join_string );

if (restart_case != NULL) {
if (strlen(restart_case) <= (SUMMARY_RESTART_SIZE * ECL_STRING8_LENGTH))
ecl_smspec->restart_case = util_alloc_string_copy( restart_case );
else
return NULL;

/*
Only a total of 9 * 8 characters is set aside for the restart keyword, if
the supplied restart case is longer than that we silently ignore it.
*/
if (restart_case) {
if (strlen(restart_case) <= (SUMMARY_RESTART_SIZE * ECL_STRING8_LENGTH))
ecl_smspec->restart_case = util_alloc_string_copy( restart_case );
}
ecl_smspec->grid_dims[0] = nx;
ecl_smspec->grid_dims[1] = ny;
Expand Down
4 changes: 4 additions & 0 deletions lib/ecl/ecl_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,10 @@ void ecl_sum_export_csv(const ecl_sum_type * ecl_sum , const char * filename ,
}


const char * ecl_sum_get_restart_case(const ecl_sum_type * ecl_sum) {
return ecl_smspec_get_restart_case(ecl_sum->smspec);
}


const char * ecl_sum_get_case(const ecl_sum_type * ecl_sum) {
return ecl_sum->ecl_case;
Expand Down
28 changes: 16 additions & 12 deletions lib/ecl/tests/ecl_sum_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ 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 );


smspec_node_type * node1 = ecl_sum_add_var( ecl_sum , "FOPT" , NULL , 0 , "Barrels" , 99.0 );
smspec_node_type * node2 = ecl_sum_add_var( ecl_sum , "BPR" , NULL , 567 , "BARS" , 0.0 );
Expand All @@ -70,8 +70,6 @@ int write_restart_summary(const char * name, const char * restart_name , int sta
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);
Expand Down Expand Up @@ -147,7 +145,7 @@ void test_ecl_sum_alloc_restart_writer() {
int num_ministep = 10;
double ministep_length = 36000; // Seconds

int sim_seconds = write_summary( name1 , start_time , nx , ny , nz , num_dates , num_ministep , ministep_length);
int sim_seconds = write_summary( name1 , start_time , nx , ny , nz , num_dates , num_ministep , ministep_length);
sim_seconds = write_restart_summary( name2 , name1 , num_dates, sim_seconds, start_time , nx , ny , nz , num_dates , num_ministep , ministep_length);

ecl_sum_type * case1 = ecl_sum_fread_alloc_case( name1 , ":" );
Expand All @@ -157,20 +155,20 @@ void test_ecl_sum_alloc_restart_writer() {
test_assert_true( ecl_sum_has_key( case2 , "FOPT" ));

ecl_file_type * restart_file = ecl_file_open( "CASE2.SMSPEC" , 0 );
ecl_file_view_type * view_file = ecl_file_get_global_view( restart_file );
ecl_file_view_type * view_file = ecl_file_get_global_view( restart_file );
test_assert_true( ecl_file_view_has_kw(view_file, RESTART_KW));
ecl_kw_type * kw = ecl_file_view_iget_kw(view_file, 0);
test_assert_int_equal(8, ecl_kw_get_size(kw));
test_assert_string_equal( "CASE1 ", ecl_kw_iget_ptr( kw , 0 ) );
test_assert_string_equal( " ", ecl_kw_iget_ptr( kw , 1 ) );

for (int time_index=0; time_index < ecl_sum_get_data_length( case1 ); time_index++)
for (int time_index=0; time_index < ecl_sum_get_data_length( case1 ); time_index++)
test_assert_double_equal( ecl_sum_get_general_var( case1 , time_index , "FOPT"), ecl_sum_get_general_var( case2 , time_index , "FOPT"));

ecl_sum_free(case2);
ecl_sum_free(case1);
ecl_file_close(restart_file);

}
test_work_area_free( work_area );
}
Expand All @@ -190,9 +188,9 @@ void test_long_restart_names() {
ecl_sum_type * ecl_sum = ecl_sum_alloc_restart_writer( name , restart_case , false , true , ":" , start_time , true , 3, 3, 3);
ecl_sum_fwrite( ecl_sum );
ecl_sum_free(ecl_sum);

ecl_file_type * smspec_file = ecl_file_open( "THE_CASE.SMSPEC" , 0 );
ecl_file_view_type * view_file = ecl_file_get_global_view( smspec_file );
ecl_file_view_type * view_file = ecl_file_get_global_view( smspec_file );
test_assert_true( ecl_file_view_has_kw(view_file, RESTART_KW));
ecl_kw_type * kw = ecl_file_view_iget_kw(view_file, 0);
test_assert_int_equal(8, ecl_kw_get_size(kw));
Expand All @@ -201,12 +199,18 @@ void test_long_restart_names() {
char s[9]; sprintf(s, "WWWWGGG%d", n);
test_assert_string_equal(s, ecl_kw_iget_char_ptr(kw, n) );
}

test_assert_NULL( ecl_smspec_alloc_writer( ":" , "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ", start_time, true, 3, 3 ,3) );
{
ecl_smspec_type * smspec = ecl_smspec_alloc_writer( ":" , "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ", start_time, true, 3, 3 ,3);
/*
Restart case is too long - it is ignored.
*/
test_assert_NULL( ecl_smspec_get_restart_case( smspec));
ecl_smspec_free( smspec);
}
}

test_work_area_free( work_area );

}

int main( int argc , char ** argv) {
Expand Down
1 change: 1 addition & 0 deletions lib/include/ert/ecl/ecl_sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ typedef struct ecl_sum_struct ecl_sum_type;
const char * ecl_sum_get_base(const ecl_sum_type * ecl_sum );
const char * ecl_sum_get_path(const ecl_sum_type * ecl_sum );
const char * ecl_sum_get_abs_path(const ecl_sum_type * ecl_sum );
const char * ecl_sum_get_restart_case(const ecl_sum_type * ecl_sum);
const char * ecl_sum_get_case(const ecl_sum_type * );
bool ecl_sum_same_case( const ecl_sum_type * ecl_sum , const char * input_file );

Expand Down
6 changes: 6 additions & 0 deletions python/ecl/summary/ecl_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class EclSum(BaseCClass):
_get_first_day = EclPrototype("double ecl_sum_get_first_day(ecl_sum)")
_get_data_start = EclPrototype("time_t ecl_sum_get_data_start(ecl_sum)")
_get_unit = EclPrototype("char* ecl_sum_get_unit(ecl_sum, char*)")
_get_restart_case = EclPrototype("char* ecl_sum_get_restart_case(ecl_sum)")
_get_simcase = EclPrototype("char* ecl_sum_get_case(ecl_sum)")
_get_base = EclPrototype("char* ecl_sum_get_base(ecl_sum)")
_get_path = EclPrototype("char* ecl_sum_get_path(ecl_sum)")
Expand Down Expand Up @@ -767,6 +768,11 @@ def case(self):
return self._get_simcase()


@property
def restart_case(self):
return self._get_restart_case()


@property
def path(self):
"""
Expand Down
30 changes: 25 additions & 5 deletions python/tests/ecl_tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,29 @@ def test_restart_abs_path(self):
create_prediction(history, pred_path)

pred = EclSum(os.path.join(pred_path, "PREDICTION"))
length = pred.sim_length
pred_times = pred.alloc_time_vector(False)
hist_times = history.alloc_time_vector(False)
# The restart case has a maximum length of 72 characters, depending
# on the path used for $TMP and so on we do not really know here if
# the restart_case has been set or not.
if pred.restart_case:
self.assertEqual(pred.restart_case, os.path.join(os.getcwd(), history.case))

for index in range(len(hist_times)):
self.assertEqual(hist_times[index], pred_times[index])
length = pred.sim_length
pred_times = pred.alloc_time_vector(False)
hist_times = history.alloc_time_vector(False)

for index in range(len(hist_times)):
self.assertEqual(hist_times[index], pred_times[index])




def test_restart_too_long_history_path(self):
with TestAreaContext("restart_test_too_fucking_long_path_for_the_eclipse_restart_keyword_1234567890123456789012345678901234567890"):
history = create_case(case = "HISTORY")
history.fwrite()

pred_path = "prediction"
create_prediction(history, pred_path)

pred = EclSum(os.path.join(pred_path, "PREDICTION"))
self.assertIsNone(pred.restart_case)

0 comments on commit 67ee923

Please sign in to comment.