From ec86fc91cfffce0109aa3f000afacd95fec4077f Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Thu, 16 Aug 2018 21:39:59 +0200 Subject: [PATCH] Read gridunit from grid file --- lib/CMakeLists.txt | 1 + lib/ecl/ecl_grid.cpp | 75 ++++++++++++++++++++------ lib/ecl/tests/ecl_grid_unit_system.cpp | 63 ++++++++++++++++++++++ 3 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 lib/ecl/tests/ecl_grid_unit_system.cpp diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index db4f1a1aee..f8345bc7fe 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -349,6 +349,7 @@ foreach (name ecl_alloc_cpgrid ecl_grid_copy ecl_grid_create ecl_grid_DEPTHZ + ecl_grid_unit_system ecl_grid_export ecl_grid_init_fwrite ecl_grid_reset_actnum diff --git a/lib/ecl/ecl_grid.cpp b/lib/ecl/ecl_grid.cpp index 9821b3793d..4469d153f7 100644 --- a/lib/ecl/ecl_grid.cpp +++ b/lib/ecl/ecl_grid.cpp @@ -699,7 +699,7 @@ struct ecl_cell_struct { }; - +static ert_ecl_unit_enum ecl_grid_check_unit_system(const ecl_kw_type * gridunit_kw); static void ecl_grid_init_mapaxes_data_float( const ecl_grid_type * grid , float * mapaxes); float * ecl_grid_alloc_coord_data( const ecl_grid_type * grid ); static const float * ecl_grid_get_mapaxes( const ecl_grid_type * grid ); @@ -1527,6 +1527,7 @@ static bool ecl_grid_alloc_cells( ecl_grid_type * grid , bool init_valid) { */ static ecl_grid_type * ecl_grid_alloc_empty(ecl_grid_type * global_grid, + ert_ecl_unit_enum unit_system, int dualp_flag, int nx, int ny, @@ -1553,7 +1554,7 @@ static ecl_grid_type * ecl_grid_alloc_empty(ecl_grid_type * global_grid, grid->index_map = NULL; grid->fracture_index_map = NULL; grid->inv_fracture_index_map = NULL; - grid->unit_system = ECL_METRIC_UNITS; + grid->unit_system = unit_system; if (global_grid != NULL) { @@ -2416,6 +2417,7 @@ void ecl_grid_init_GRDECL_data(ecl_grid_type * ecl_grid, */ static ecl_grid_type * ecl_grid_alloc_GRDECL_data__(ecl_grid_type * global_grid, + ert_ecl_unit_enum unit_system, int dualp_flag, bool apply_mapaxes, int nx, @@ -2428,7 +2430,7 @@ static ecl_grid_type * ecl_grid_alloc_GRDECL_data__(ecl_grid_type * global_grid, const int * corsnum, int lgr_nr) { - ecl_grid_type * ecl_grid = ecl_grid_alloc_empty(global_grid , dualp_flag , nx,ny,nz,lgr_nr,true); + ecl_grid_type * ecl_grid = ecl_grid_alloc_empty(global_grid , unit_system, dualp_flag , nx,ny,nz,lgr_nr,true); if (ecl_grid) { if (mapaxes != NULL) ecl_grid_init_mapaxes( ecl_grid , apply_mapaxes, mapaxes ); @@ -2452,6 +2454,7 @@ static ecl_grid_type * ecl_grid_alloc_GRDECL_data__(ecl_grid_type * global_grid, } static ecl_grid_type * ecl_grid_alloc_GRDECL_data__(ecl_grid_type * global_grid, + ert_ecl_unit_enum unit_system, int dualp_flag, bool apply_mapaxes, int nx, @@ -2464,7 +2467,7 @@ static ecl_grid_type * ecl_grid_alloc_GRDECL_data__(ecl_grid_type * global_grid, const int * corsnum, int lgr_nr) { - ecl_grid_type * ecl_grid = ecl_grid_alloc_empty(global_grid , dualp_flag , nx,ny,nz,lgr_nr,true); + ecl_grid_type * ecl_grid = ecl_grid_alloc_empty(global_grid, unit_system, dualp_flag , nx,ny,nz,lgr_nr,true); if (ecl_grid) { if (mapaxes != NULL) ecl_grid_init_mapaxes( ecl_grid , apply_mapaxes, mapaxes ); @@ -2521,6 +2524,7 @@ static void ecl_grid_copy_content( ecl_grid_type * target_grid , const ecl_grid_ static ecl_grid_type * ecl_grid_alloc_copy__( const ecl_grid_type * src_grid, ecl_grid_type * main_grid ) { ecl_grid_type * copy_grid = ecl_grid_alloc_empty( main_grid , + src_grid->unit_system, src_grid->dualp_flag , ecl_grid_get_nx( src_grid ) , ecl_grid_get_ny( src_grid ) , @@ -2625,7 +2629,10 @@ ecl_grid_type * ecl_grid_alloc_GRDECL_data(int nx, const int * actnum, bool apply_mapaxes, const float * mapaxes) { + + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; return ecl_grid_alloc_GRDECL_data__(NULL, + unit_system, FILEHEAD_SINGLE_POROSITY, apply_mapaxes, nx, @@ -2649,8 +2656,9 @@ namespace ecl { const int * actnum, bool apply_mapaxes, const float * mapaxes) { - + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; return ecl_grid_alloc_GRDECL_data__(NULL, + unit_system, FILEHEAD_SINGLE_POROSITY, apply_mapaxes, nx, @@ -2690,11 +2698,12 @@ static ecl_grid_type * ecl_grid_alloc_GRDECL_kw__(ecl_grid_type * global_grid , const ecl_kw_type * gridhead_kw , const ecl_kw_type * zcorn_kw , const ecl_kw_type * coord_kw , + const ecl_kw_type * gridunit_kw, /* Can be NULL */ const ecl_kw_type * mapaxes_kw , /* Can be NULL */ const ecl_kw_type * corsnum_kw, /* Can be NULL */ const int * actnum_data) { /* Can be NULL */ - int gtype, nx,ny,nz, lgr_nr; - + int gtype, nx,ny,nz, lgr_nr; + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; gtype = ecl_kw_iget_int(gridhead_kw , GRIDHEAD_TYPE_INDEX); nx = ecl_kw_iget_int(gridhead_kw , GRIDHEAD_NX_INDEX); ny = ecl_kw_iget_int(gridhead_kw , GRIDHEAD_NY_INDEX); @@ -2717,13 +2726,17 @@ static ecl_grid_type * ecl_grid_alloc_GRDECL_kw__(ecl_grid_type * global_grid , const float * mapaxes_data = NULL; const int * corsnum_data = NULL; - if (mapaxes_kw != NULL) + if (mapaxes_kw) mapaxes_data = ecl_grid_get_mapaxes_from_kw__(mapaxes_kw); - if (corsnum_kw != NULL) + if (corsnum_kw) corsnum_data = ecl_kw_get_int_ptr( corsnum_kw ); + if (gridunit_kw) + unit_system = ecl_grid_check_unit_system(gridunit_kw); + return ecl_grid_alloc_GRDECL_data__(global_grid , + unit_system, dualp_flag , apply_mapaxes, nx , ny , nz , @@ -2756,13 +2769,14 @@ ecl_grid_type * ecl_grid_alloc_GRDECL_kw( int nx, int ny , int nz , bool apply_mapaxes = true; ecl_kw_type * gridhead_kw = ecl_grid_alloc_gridhead_kw( nx, ny, nz, 0); - + ecl_kw_type * gridunit_kw = NULL; ecl_grid_type * ecl_grid = ecl_grid_alloc_GRDECL_kw__(NULL, FILEHEAD_SINGLE_POROSITY, apply_mapaxes, gridhead_kw, zcorn_kw, coord_kw, + gridunit_kw, mapaxes_kw, NULL, actnum_data); @@ -2994,6 +3008,7 @@ static ecl_grid_type * ecl_grid_alloc_EGRID__( ecl_grid_type * main_grid , const ecl_kw_type * coord_kw = ecl_file_iget_named_kw( ecl_file , COORD_KW , grid_nr); ecl_kw_type * corsnum_kw = NULL; ecl_kw_type * actnum_kw = NULL; + ecl_kw_type * gridunit_kw = NULL; ecl_kw_type * mapaxes_kw = NULL; int dualp_flag; int eclipse_version; @@ -3031,6 +3046,9 @@ static ecl_grid_type * ecl_grid_alloc_EGRID__( ecl_grid_type * main_grid , const if (ecl_file_has_kw( ecl_file , CORSNUM_KW)) corsnum_kw = ecl_file_iget_named_kw( ecl_file , CORSNUM_KW , 0); + + if (ecl_file_has_kw(ecl_file, GRIDUNIT_KW)) + gridunit_kw = ecl_file_iget_named_kw( ecl_file, GRIDUNIT_KW, 0); } { @@ -3040,8 +3058,9 @@ static ecl_grid_type * ecl_grid_alloc_EGRID__( ecl_grid_type * main_grid , const gridhead_kw , zcorn_kw , coord_kw , + gridunit_kw, mapaxes_kw , - corsnum_kw, + corsnum_kw, actnum_data); if (ECL_GRID_MAINGRID_LGR_NR != grid_nr) ecl_grid_set_lgr_name_EGRID(ecl_grid , ecl_file , grid_nr); @@ -3105,11 +3124,11 @@ ecl_grid_type * ecl_grid_alloc_EGRID(const char * grid_file, bool apply_mapaxes) -static ecl_grid_type * ecl_grid_alloc_GRID_data__(ecl_grid_type * global_grid , int num_coords , int dualp_flag , bool apply_mapaxes, int nx, int ny , int nz , int grid_nr , int coords_size , int ** coords , float ** corners , const float * mapaxes) { +static ecl_grid_type * ecl_grid_alloc_GRID_data__(ecl_grid_type * global_grid , int num_coords , ert_ecl_unit_enum unit_system, int dualp_flag , bool apply_mapaxes, int nx, int ny , int nz , int grid_nr , int coords_size , int ** coords , float ** corners , const float * mapaxes) { if (dualp_flag != FILEHEAD_SINGLE_POROSITY) nz = nz / 2; { - ecl_grid_type * grid = ecl_grid_alloc_empty( global_grid , dualp_flag , nx , ny , nz , grid_nr, false); + ecl_grid_type * grid = ecl_grid_alloc_empty( global_grid , unit_system, dualp_flag , nx , ny , nz , grid_nr, false); if (grid) { if (mapaxes != NULL) ecl_grid_init_mapaxes( grid , apply_mapaxes , mapaxes); @@ -3135,8 +3154,10 @@ static ecl_grid_type * ecl_grid_alloc_GRID_data__(ecl_grid_type * global_grid , */ ecl_grid_type * ecl_grid_alloc_GRID_data(int num_coords , int nx , int ny , int nz , int coords_size , int ** coords , float ** corners , bool apply_mapaxes, const float * mapaxes) { + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; return ecl_grid_alloc_GRID_data__( NULL , num_coords , + unit_system, FILEHEAD_SINGLE_POROSITY , /* Does currently not support to determine dualp_flag from inspection. */ apply_mapaxes, nx , ny , nz , 0 , coords_size , coords , corners , mapaxes); @@ -3190,6 +3211,7 @@ static ecl_grid_type * ecl_grid_alloc_GRID__(ecl_grid_type * global_grid , const int nx,ny,nz; const float * mapaxes_data = NULL; ecl_grid_type * grid; + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; // 1: Fetching header data from the DIMENS keyword. { @@ -3209,6 +3231,8 @@ static ecl_grid_type * ecl_grid_alloc_GRID__(ecl_grid_type * global_grid , const } } + if ((grid_nr == 0) && (ecl_file_has_kw( ecl_file , GRIDUNIT_KW))) + unit_system = ecl_grid_check_unit_system( ecl_file_iget_named_kw(ecl_file, GRIDUNIT_KW, 0)); /* The number of COORDS/CORNERS blocks depends on the GRIDFILE option @@ -3281,7 +3305,7 @@ static ecl_grid_type * ecl_grid_alloc_GRID__(ecl_grid_type * global_grid , const coords_size = ecl_kw_get_size( coords_kw ); } // Create the grid: - grid = ecl_grid_alloc_GRID_data__( global_grid , num_coords , dualp_flag , apply_mapaxes, nx , ny , nz , grid_nr , coords_size , coords , corners , mapaxes_data ); + grid = ecl_grid_alloc_GRID_data__( global_grid , num_coords , unit_system, dualp_flag , apply_mapaxes, nx , ny , nz , grid_nr , coords_size , coords , corners , mapaxes_data ); free( coords ); free( corners ); @@ -3348,7 +3372,8 @@ ecl_grid_type * ecl_grid_alloc_GRID(const char * grid_file, bool apply_mapaxes) which case all cells will be active. */ ecl_grid_type * ecl_grid_alloc_regular( int nx, int ny , int nz , const double * ivec, const double * jvec , const double * kvec , const int * actnum) { - ecl_grid_type * grid = ecl_grid_alloc_empty(NULL , FILEHEAD_SINGLE_POROSITY , nx , ny , nz , 0, true); + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; + ecl_grid_type * grid = ecl_grid_alloc_empty(NULL , unit_system, FILEHEAD_SINGLE_POROSITY , nx , ny , nz , 0, true); if (grid) { const double grid_offset[3] = {0,0,0}; @@ -3405,7 +3430,9 @@ ecl_grid_type * ecl_grid_alloc_rectangular( int nx , int ny , int nz , double dx all cells will be active. */ ecl_grid_type * ecl_grid_alloc_dxv_dyv_dzv( int nx, int ny , int nz , const double * dxv , const double * dyv , const double * dzv , const int * actnum) { + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; ecl_grid_type* grid = ecl_grid_alloc_empty(NULL, + unit_system, FILEHEAD_SINGLE_POROSITY, nx, ny, nz, /*lgr_nr=*/0, /*init_valid=*/true); @@ -3448,7 +3475,9 @@ ecl_grid_type * ecl_grid_alloc_dxv_dyv_dzv( int nx, int ny , int nz , const doub ecl_grid_type * ecl_grid_alloc_dxv_dyv_dzv_depthz( int nx, int ny , int nz , const double * dxv , const double * dyv , const double * dzv , const double * depthz , const int * actnum) { + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; ecl_grid_type* grid = ecl_grid_alloc_empty(NULL, + unit_system, FILEHEAD_SINGLE_POROSITY, nx, ny, nz, /*lgr_nr=*/0, /*init_valid=*/true); @@ -3551,8 +3580,9 @@ ecl_grid_type * ecl_grid_alloc_dxv_dyv_dzv_depthz( int nx, int ny , int nz , con */ ecl_grid_type * ecl_grid_alloc_dx_dy_dz_tops( int nx, int ny , int nz , const double * dx , const double * dy , const double * dz , const double * tops , const int * actnum) { - + ert_ecl_unit_enum unit_system = ECL_METRIC_UNITS; ecl_grid_type* grid = ecl_grid_alloc_empty(NULL, + unit_system, FILEHEAD_SINGLE_POROSITY, nx, ny, nz, 0, true); @@ -6052,6 +6082,19 @@ static ecl_kw_type * ecl_grid_alloc_gridunits_kw( ert_ecl_unit_enum output_unit return gridunits_kw; } +static ert_ecl_unit_enum ecl_grid_check_unit_system(const ecl_kw_type * gridunit_kw) { + const char * length_unit = ecl_kw_iget_char_ptr(gridunit_kw, 0); + + if (strncmp(length_unit, "FEET", 4) == 0) + return ECL_FIELD_UNITS; + + if (strncmp(length_unit, "CM", 2) == 0) + return ECL_LAB_UNITS; + + return ECL_METRIC_UNITS; +} + + /*****************************************************************/ static float ecl_grid_output_scaling( const ecl_grid_type * grid , ert_ecl_unit_enum output_unit) { diff --git a/lib/ecl/tests/ecl_grid_unit_system.cpp b/lib/ecl/tests/ecl_grid_unit_system.cpp new file mode 100644 index 0000000000..2e75a1c59b --- /dev/null +++ b/lib/ecl/tests/ecl_grid_unit_system.cpp @@ -0,0 +1,63 @@ +/* + Copyright (C) 2018 Statoil ASA, Norway. + + The file 'ecl_grid_unit_system.c' is part of ERT - Ensemble based Reservoir Tool. + + ERT is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ERT is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU General Public License at + for more details. +*/ +#include +#include + +#include +#include + +#include +#include +#include + + +void test_EGRID(const char * filename, ert_ecl_unit_enum unit_system) { + ecl_grid_type * grid = ecl_grid_alloc_rectangular(4,4,2,1,1,1,NULL); + ecl_grid_fwrite_EGRID2( grid , filename, unit_system); + ecl_grid_free( grid ); + + + ecl_grid_type * grid2 = ecl_grid_alloc(filename); + test_assert_int_equal( ecl_grid_get_unit_system(grid2), unit_system); + ecl_grid_free(grid2); +} + + + +void test_GRID(const char * filename, ert_ecl_unit_enum unit_system) { + ecl_grid_type * grid = ecl_grid_alloc_rectangular(4,4,2,1,1,1,NULL); + ecl_grid_fwrite_GRID2( grid , filename, unit_system); + ecl_grid_free( grid ); + + + ecl_grid_type * grid2 = ecl_grid_alloc(filename); + test_assert_int_equal( ecl_grid_get_unit_system(grid2), unit_system); + ecl_grid_free(grid2); +} + + +int main(int argc, char **argv) { + test_work_area_type * work_area = test_work_area_alloc("grid_export"); + + test_EGRID("METRIC.EGRID", ECL_METRIC_UNITS); + test_EGRID("FIELD.EGRID", ECL_FIELD_UNITS); + test_GRID("METRIC.GRID", ECL_METRIC_UNITS); + test_GRID("FIELD.GRID", ECL_FIELD_UNITS); + + test_work_area_free(work_area); +}