Skip to content

Commit

Permalink
Merge pull request #172 from joakim-hove/ecl-sum-interp-multiple
Browse files Browse the repository at this point in the history
Added function to get multiple summary values.
  • Loading branch information
joakim-hove authored Sep 2, 2017
2 parents b61ecda + af5d75e commit 09d5ba9
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 17 deletions.
3 changes: 3 additions & 0 deletions lib/ecl/ecl_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@ double ecl_sum_get_general_var(const ecl_sum_type * ecl_sum , int time_index , c
return ecl_sum_data_iget( ecl_sum->data , time_index , params_index);
}

void ecl_sum_get_interp_vector(const ecl_sum_type * ecl_sum, time_t sim_time, const ecl_sum_vector_type * key_words, double_vector_type * data){
ecl_sum_data_get_interp_vector(ecl_sum->data, sim_time, key_words, data);
}

void ecl_sum_fwrite_interp_csv_line(const ecl_sum_type * ecl_sum, time_t sim_time, const ecl_sum_vector_type * key_words, FILE *fp){
ecl_sum_data_fwrite_interp_csv_line(ecl_sum->data, sim_time, key_words, fp);
Expand Down
42 changes: 29 additions & 13 deletions lib/ecl/ecl_sum_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,23 @@ double ecl_sum_data_interp_get(const ecl_sum_data_type * data , int time_index1
}


static double ecl_sum_data_vector_iget(const ecl_sum_data_type * data, time_t sim_time, const ecl_sum_vector_type * keylist, int key_index,
int time_index1 , int time_index2 , double weight1 , double weight2 ) {

int params_index = ecl_sum_vector_iget_param_index(keylist, key_index);
double value = 0.0;
bool is_rate = ecl_sum_vector_iget_is_rate(keylist, key_index);
if (is_rate) {
int time_index = ecl_sum_data_get_index_from_sim_time(data, sim_time);
// uses step function since it is a rate
value = ecl_sum_data_iget(data, time_index, params_index);
} else {
// uses interpolation between timesteps
value = ecl_sum_data_interp_get(data, time_index1, time_index2, weight1, weight2, params_index);
}
return value;
}

void ecl_sum_data_fwrite_interp_csv_line(const ecl_sum_data_type * data, time_t sim_time, const ecl_sum_vector_type * keylist, FILE *fp){
int num_keywords = ecl_sum_vector_get_size(keylist);
double weight1, weight2;
Expand All @@ -1188,18 +1205,7 @@ void ecl_sum_data_fwrite_interp_csv_line(const ecl_sum_data_type * data, time_t
ecl_sum_data_init_interp_from_sim_time(data, sim_time, &time_index1, &time_index2, &weight1, &weight2);

for (int i = 0; i < num_keywords; i++) {
int params_index = ecl_sum_vector_iget_param_index(keylist, i);

double value = 0.0;
bool is_rate = ecl_sum_vector_iget_is_rate(keylist, i);
if (is_rate) {
int time_index = ecl_sum_data_get_index_from_sim_time(data, sim_time);
// uses step function since it is a rate
value = ecl_sum_data_iget(data, time_index, params_index);
} else {
// uses interpolation between timesteps
value = ecl_sum_data_interp_get(data, time_index1, time_index2, weight1, weight2, params_index);
}
double value = ecl_sum_data_vector_iget( data, sim_time, keylist , i , time_index1, time_index2, weight1, weight2);

if (i == 0)
fprintf(fp, "%f", value);
Expand All @@ -1208,8 +1214,18 @@ void ecl_sum_data_fwrite_interp_csv_line(const ecl_sum_data_type * data, time_t
}
}

void ecl_sum_data_get_interp_vector( const ecl_sum_data_type * data , time_t sim_time, const ecl_sum_vector_type * keylist, double_vector_type * results){
int num_keywords = ecl_sum_vector_get_size(keylist);
double weight1, weight2;
int time_index1, time_index2;


ecl_sum_data_init_interp_from_sim_time(data, sim_time, &time_index1, &time_index2, &weight1, &weight2);
double_vector_reset( results );
for (int i = 0; i < num_keywords; i++) {
double value = ecl_sum_data_vector_iget( data, sim_time, keylist , i , time_index1, time_index2, weight1, weight2);
double_vector_iset( results, i , value );
}
}

double ecl_sum_data_get_from_sim_time( const ecl_sum_data_type * data , time_t sim_time , const smspec_node_type * smspec_node) {
int params_index = smspec_node_get_params_index( smspec_node );
Expand Down
10 changes: 10 additions & 0 deletions lib/ecl/ecl_sum_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ struct ecl_sum_vector_struct {
UTIL_TYPE_ID_DECLARATION;
int_vector_type * node_index_list;
bool_vector_type * is_rate_list;
stringlist_type * key_list;
const ecl_sum_type * ecl_sum;
};


void ecl_sum_vector_free( ecl_sum_vector_type * ecl_sum_vector ){
int_vector_free(ecl_sum_vector->node_index_list);
bool_vector_free(ecl_sum_vector->is_rate_list);
stringlist_free(ecl_sum_vector->key_list);
}


Expand All @@ -52,6 +54,7 @@ ecl_sum_vector_type * ecl_sum_vector_alloc(const ecl_sum_type * ecl_sum){
ecl_sum_vector->ecl_sum = ecl_sum;
ecl_sum_vector->node_index_list = int_vector_alloc(0,0);
ecl_sum_vector->is_rate_list = bool_vector_alloc(0,false);
ecl_sum_vector->key_list = stringlist_alloc_new( );
return ecl_sum_vector;
}

Expand All @@ -64,6 +67,7 @@ bool ecl_sum_vector_add_key( ecl_sum_vector_type * ecl_sum_vector, const char *

int_vector_append(ecl_sum_vector->node_index_list, params_index);
bool_vector_append(ecl_sum_vector->is_rate_list, is_rate_key);
stringlist_append_copy( ecl_sum_vector->key_list, key );
return true;
} else
return false;
Expand All @@ -84,6 +88,7 @@ void ecl_sum_vector_add_keys( ecl_sum_vector_type * ecl_sum_vector, const char *

int_vector_append(ecl_sum_vector->node_index_list, params_index);
bool_vector_append(ecl_sum_vector->is_rate_list, is_rate_key);
stringlist_append_copy( ecl_sum_vector->key_list, key );
}
stringlist_free(keylist);
}
Expand All @@ -99,3 +104,8 @@ bool ecl_sum_vector_iget_is_rate(const ecl_sum_vector_type * ecl_sum_vector, int
int ecl_sum_vector_iget_param_index(const ecl_sum_vector_type * ecl_sum_vector, int index){
return int_vector_iget(ecl_sum_vector->node_index_list, index);
}


const char* ecl_sum_vector_iget_key(const ecl_sum_vector_type * ecl_sum_vector, int index){
return stringlist_iget( ecl_sum_vector->key_list , index);
}
2 changes: 1 addition & 1 deletion lib/include/ert/ecl/ecl_sum_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ typedef struct ecl_sum_data_struct ecl_sum_data_type ;

double ecl_sum_data_iget_sim_days( const ecl_sum_data_type * , int );
time_t ecl_sum_data_iget_sim_time( const ecl_sum_data_type * , int );

void ecl_sum_data_get_interp_vector( const ecl_sum_data_type * data , time_t sim_time, const ecl_sum_vector_type * keylist, double_vector_type * results);

bool ecl_sum_data_has_report_step(const ecl_sum_data_type * , int );

Expand Down
9 changes: 9 additions & 0 deletions python/python/ecl/ecl/ecl_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,13 @@ def get_interp(self, key, days=None, date=None):
raise ValueError("Must supply either days or date")


def get_interp_row(self, key_list, sim_time):
ctime = CTime(sim_time)
data = DoubleVector( initial_size = len(key_list) )
EclSum._get_interp_vector(self, ctime, key_list, data)
return data


def time_range(self, start=None, end=None, interval="1Y", extend_end=True):
(num, timeUnit) = TimeVector.parseTimeUnit(interval)

Expand Down Expand Up @@ -1246,6 +1253,7 @@ def dump_csv_line(self, time, keywords, pfile):
EclSum._dump_csv_line(self, ctime, keywords, cfile)



def export_csv(self, filename, keys=None, date_format="%Y-%m-%d", sep=";"):
"""Will create a CSV file with summary data.
Expand Down Expand Up @@ -1273,6 +1281,7 @@ def export_csv(self, filename, keys=None, date_format="%Y-%m-%d", sep=";"):

import ecl.ecl.ecl_sum_keyword_vector
EclSum._dump_csv_line = EclPrototype("void ecl_sum_fwrite_interp_csv_line(ecl_sum, time_t, ecl_sum_vector, FILE)", bind=False)
EclSum._get_interp_vector = EclPrototype("void ecl_sum_get_interp_vector(ecl_sum, time_t, ecl_sum_vector, double_vector)", bind=False)

monkey_the_camel(EclSum, 'varType', EclSum.var_type, classmethod)
monkey_the_camel(EclSum, 'addVariable', EclSum.add_variable)
Expand Down
11 changes: 10 additions & 1 deletion python/python/ecl/ecl/ecl_sum_keyword_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@ class EclSumKeyWordVector(BaseCClass):
_add = EclPrototype("bool ecl_sum_vector_add_key(ecl_sum_vector, char*)")
_add_multiple = EclPrototype("void ecl_sum_vector_add_keys(ecl_sum_vector, char*)")
_get_size = EclPrototype("int ecl_sum_vector_get_size(ecl_sum_vector)")

_iget_key = EclPrototype("char* ecl_sum_vector_iget_key(ecl_sum_vector, int)")

def __init__(self, ecl_sum):
c_pointer = self._alloc(ecl_sum)
super(EclSumKeyWordVector, self).__init__(c_pointer)

def __getitem__(self, index):
if index < 0:
index += len(self)

if index >= len(self):
raise IndexError("Out of range")

return self._iget_key( index )

def __len__(self):
return self._get_size()

Expand Down
1 change: 0 additions & 1 deletion python/tests/ecl/test_ecl_sum_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def setUp(self):
self.test_file = self.createTestPath("Statoil/ECLIPSE/Gurbat/ECLIPSE.SMSPEC")
self.ecl_sum = EclSum(self.test_file)


def test_reportOnly_warns(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
Expand Down
31 changes: 30 additions & 1 deletion python/tests/ecl/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import shutil
from unittest import skipIf, skipUnless, skipIf

from ecl.ecl import EclSum, EclSumVarType, FortIO, openFortIO, EclKW, EclDataType
from ecl.ecl import EclSum, EclSumVarType, FortIO, openFortIO, EclKW, EclDataType, EclSumKeyWordVector
from ecl.test import ExtendedTestCase, TestAreaContext
from ecl.test.ecl_mock import createEclSum

Expand Down Expand Up @@ -284,3 +284,32 @@ def test_invalid(self):

with self.assertRaises( IOError ):
case2 = EclSum.load( "CASE.KW" , "CSV.UNSMRY" )


def test_kw_vector(self):
case = createEclSum("CSV" , [("FOPT", None , 0) , ("FOPR" , None , 0), ("FGPT" , None , 0)],
sim_length_days = 100,
num_report_step = 10,
num_mini_step = 10,
func_table = {"FOPT" : fopt,
"FOPR" : fopr ,
"FGPT" : fgpt })
kw_list = EclSumKeyWordVector( case )
kw_list.add_keyword("FOPT")
kw_list.add_keyword("FOPR")
kw_list.add_keyword("FGPT")

t = case.getDataStartTime( ) + datetime.timedelta( days = 43 );
data = case.get_interp_row( kw_list , t )
for d1,d2 in zip(data, [ case.get_interp("FOPT", date = t),
case.get_interp("FOPT", date = t),
case.get_interp("FOPT", date = t) ]):

self.assertFloatEqual(d1,d2)

tmp = []
for key in kw_list:
tmp.append(key)

for (k1,k2) in zip(kw_list,tmp):
self.assertEqual(k1,k2)

0 comments on commit 09d5ba9

Please sign in to comment.