mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Added missing files for ERT revision a39d4b4a9e597532b2dbf845751a70ffe1de3805
p4#: 20195
This commit is contained in:
parent
2cf0e32d4b
commit
bdfd9c543e
5
ThirdParty/Ert/devel/CMake/Tests/test_dlopen.c
vendored
Normal file
5
ThirdParty/Ert/devel/CMake/Tests/test_dlopen.c
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
#include <dlfcn.h>
|
||||
|
||||
int main( int argc , char ** argv) {
|
||||
dlopen( NULL , 0 );
|
||||
}
|
5
ThirdParty/Ert/devel/CMake/Tests/test_isfinite.c
vendored
Normal file
5
ThirdParty/Ert/devel/CMake/Tests/test_isfinite.c
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
#include <math.h>
|
||||
|
||||
int main( int argc , char ** argv) {
|
||||
isfinite(0);
|
||||
}
|
6
ThirdParty/Ert/devel/debian/README.Debian
vendored
Normal file
6
ThirdParty/Ert/devel/debian/README.Debian
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
libert for Debian
|
||||
-----------------
|
||||
|
||||
<possible notes regarding this package - if none, delete this file>
|
||||
|
||||
-- Arne Morten Kvarving <arne.morten.kvarving@sintef.no> Wed, 16 Jan 2013 11:21:17 +0100
|
9
ThirdParty/Ert/devel/debian/README.source
vendored
Normal file
9
ThirdParty/Ert/devel/debian/README.source
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
libert for Debian
|
||||
-----------------
|
||||
|
||||
<this file describes information about the source package, see Debian policy
|
||||
manual section 4.14. You WILL either need to modify or delete this file>
|
||||
|
||||
|
||||
|
||||
|
5
ThirdParty/Ert/devel/debian/changelog
vendored
Normal file
5
ThirdParty/Ert/devel/debian/changelog
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
libert (0.1-1) unstable; urgency=low
|
||||
|
||||
* Initial release
|
||||
|
||||
-- Arne Morten Kvarving <arne.morten.kvarving@sintef.no> Wed, 16 Jan 2013 11:21:17 +0100
|
1
ThirdParty/Ert/devel/debian/compat
vendored
Normal file
1
ThirdParty/Ert/devel/debian/compat
vendored
Normal file
@ -0,0 +1 @@
|
||||
8
|
25
ThirdParty/Ert/devel/debian/control
vendored
Normal file
25
ThirdParty/Ert/devel/debian/control
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
Source: libert
|
||||
Priority: extra
|
||||
Maintainer: Arne Morten Kvarving <arne.morten.kvarving@sintef.no>
|
||||
Build-Depends: debhelper (>= 8.0.0), cmake
|
||||
Standards-Version: 3.9.2
|
||||
Section: libs
|
||||
Homepage: <add me>
|
||||
Vcs-Git: git://github.com/Ensembles/ert.git
|
||||
Vcs-Browser: https://github.com/Ensembles/ert
|
||||
|
||||
Package: libert-dev
|
||||
Section: libdevel
|
||||
Architecture: any
|
||||
Depends: libert1 (= ${binary:Version})
|
||||
Description: The Ensemble based Reservoir Tool -- Development files
|
||||
ERT - Ensemble based Reservoir Tool is a tool for managing en ensemble
|
||||
of reservoir models.
|
||||
|
||||
Package: libert1
|
||||
Section: libs
|
||||
Architecture: any
|
||||
Depends: ${shlibs:Depends}, ${misc:Depends}
|
||||
Description: The Ensemble based Reservoir Tool
|
||||
ERT - Ensemble based Reservoir Tool is a tool for managing en ensemble
|
||||
of reservoir models.
|
36
ThirdParty/Ert/devel/debian/copyright
vendored
Normal file
36
ThirdParty/Ert/devel/debian/copyright
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
Format: http://dep.debian.net/deps/dep5
|
||||
Upstream-Name: libert
|
||||
Source: <url://example.com>
|
||||
|
||||
Files: *
|
||||
Copyright: <years> <put author's name and email here>
|
||||
<years> <likewise for another author>
|
||||
License: <special license>
|
||||
<Put the license of the package here indented by 1 space>
|
||||
<This follows the format of Description: lines in control file>
|
||||
.
|
||||
<Including paragraphs>
|
||||
|
||||
# If you want to use GPL v2 or later for the /debian/* files use
|
||||
# the following clauses, or change it to suit. Delete these two lines
|
||||
Files: debian/*
|
||||
Copyright: 2013 Arne Morten Kvarving <arne.morten.kvarving@sintef.no>
|
||||
License: GPL-2+
|
||||
This package 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 2 of the License, or
|
||||
(at your option) any later version.
|
||||
.
|
||||
This package 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 for more details.
|
||||
.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
.
|
||||
On Debian systems, the complete text of the GNU General
|
||||
Public License version 2 can be found in "/usr/share/common-licenses/GPL-2".
|
||||
|
||||
# Please also look if there are files or directories which have a
|
||||
# different copyright/license attached and list them here.
|
1
ThirdParty/Ert/devel/debian/docs
vendored
Normal file
1
ThirdParty/Ert/devel/debian/docs
vendored
Normal file
@ -0,0 +1 @@
|
||||
Windows.txt
|
2
ThirdParty/Ert/devel/debian/libert-dev.dirs
vendored
Normal file
2
ThirdParty/Ert/devel/debian/libert-dev.dirs
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
usr/lib
|
||||
usr/include
|
1
ThirdParty/Ert/devel/debian/libert-dev.install
vendored
Normal file
1
ThirdParty/Ert/devel/debian/libert-dev.install
vendored
Normal file
@ -0,0 +1 @@
|
||||
usr/include/*
|
1
ThirdParty/Ert/devel/debian/libert1.dirs
vendored
Normal file
1
ThirdParty/Ert/devel/debian/libert1.dirs
vendored
Normal file
@ -0,0 +1 @@
|
||||
usr/lib
|
1
ThirdParty/Ert/devel/debian/libert1.install
vendored
Normal file
1
ThirdParty/Ert/devel/debian/libert1.install
vendored
Normal file
@ -0,0 +1 @@
|
||||
usr/lib/lib*.so
|
16
ThirdParty/Ert/devel/debian/rules
vendored
Normal file
16
ThirdParty/Ert/devel/debian/rules
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
#!/usr/bin/make -f
|
||||
# -*- makefile -*-
|
||||
# Sample debian/rules that uses debhelper.
|
||||
# This file was originally written by Joey Hess and Craig Small.
|
||||
# As a special exception, when this file is copied by dh-make into a
|
||||
# dh-make output file, you may use that output file without restriction.
|
||||
# This special exception was added by Craig Small in version 0.37 of dh-make.
|
||||
|
||||
# Uncomment this to turn on verbose mode.
|
||||
#export DH_VERBOSE=1
|
||||
|
||||
%:
|
||||
dh $@
|
||||
|
||||
override_dh_auto_configure:
|
||||
dh_auto_configure -- -DSHARED_LIB=1
|
1
ThirdParty/Ert/devel/debian/source/format
vendored
Normal file
1
ThirdParty/Ert/devel/debian/source/format
vendored
Normal file
@ -0,0 +1 @@
|
||||
1.0
|
5
ThirdParty/Ert/devel/libconfig/CMakeLists.txt
vendored
Normal file
5
ThirdParty/Ert/devel/libconfig/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/src )
|
||||
include_directories( ${libert_util_build_path} )
|
||||
include_directories( ${libert_util_src_path} )
|
||||
|
||||
add_subdirectory( src )
|
14
ThirdParty/Ert/devel/libconfig/src/CMakeLists.txt
vendored
Normal file
14
ThirdParty/Ert/devel/libconfig/src/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
set( source_files config.c conf.c conf_util.c conf_data.c)
|
||||
set( header_files config.h conf.h conf_data.h)
|
||||
|
||||
add_library( config ${LIBRARY_TYPE} ${source_files} )
|
||||
|
||||
target_link_libraries( config ert_util )
|
||||
|
||||
if (CONFIG_INSTALL_PREFIX)
|
||||
install(TARGETS config DESTINATION ${CONFIG_INSTALL_PREFIX}/lib)
|
||||
install(FILES ${header_files} DESTINATION ${CONFIG_INSTALL_PREFIX}/include)
|
||||
else()
|
||||
install(TARGETS config DESTINATION lib)
|
||||
install(FILES ${header_files} DESTINATION include)
|
||||
endif()
|
93
ThirdParty/Ert/devel/libecl/applications/summary2csv.c
vendored
Normal file
93
ThirdParty/Ert/devel/libecl/applications/summary2csv.c
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
The file 'summary2csv.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <stringlist.h>
|
||||
|
||||
#include <ecl_sum.h>
|
||||
|
||||
|
||||
|
||||
|
||||
static void build_key_list( const ecl_sum_type * ecl_sum , stringlist_type * key_list ) {
|
||||
int iw;
|
||||
int last_step = ecl_sum_get_data_length( ecl_sum ) - 1;
|
||||
stringlist_type * well_list = ecl_sum_alloc_well_list( ecl_sum , NULL );
|
||||
for (iw = 0; iw < stringlist_get_size( well_list ); iw++) {
|
||||
const char * well = stringlist_iget( well_list , iw );
|
||||
char * wopt_key = ecl_sum_alloc_well_key( ecl_sum , "WOPR", well);
|
||||
if (ecl_sum_has_key( ecl_sum , wopt_key) && (ecl_sum_get_well_var( ecl_sum , last_step , well , "WOPT") > 0 )) {
|
||||
/*
|
||||
We add all the keys unconditionally here; and then let the
|
||||
ecl_sum_fprintf() function print a message on stderr if it is
|
||||
missing.
|
||||
*/
|
||||
stringlist_append_owned_ref( key_list , ecl_sum_alloc_well_key( ecl_sum , "WOPR", well) );
|
||||
stringlist_append_owned_ref( key_list , ecl_sum_alloc_well_key( ecl_sum , "WOPT", well) );
|
||||
|
||||
stringlist_append_owned_ref( key_list , ecl_sum_alloc_well_key( ecl_sum , "WGPR", well) );
|
||||
stringlist_append_owned_ref( key_list , ecl_sum_alloc_well_key( ecl_sum , "WGPT", well) );
|
||||
|
||||
stringlist_append_owned_ref( key_list , ecl_sum_alloc_well_key( ecl_sum , "WWPR", well) );
|
||||
stringlist_append_owned_ref( key_list , ecl_sum_alloc_well_key( ecl_sum , "WWPT", well) );
|
||||
} else
|
||||
printf("Ignoring well: %s \n",well);
|
||||
free( wopt_key );
|
||||
}
|
||||
stringlist_free( well_list );
|
||||
}
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
{
|
||||
ecl_sum_fmt_type fmt;
|
||||
bool include_restart = true;
|
||||
int arg_offset = 1;
|
||||
|
||||
if (argc != 2) {
|
||||
printf("You must supply the name of a case as:\n\n summary2csv.exe ECLIPSE_CASE\n\nThe case can optionally contain a leading path component.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
{
|
||||
char * data_file = argv[arg_offset];
|
||||
ecl_sum_type * ecl_sum;
|
||||
|
||||
ecl_sum_fmt_init_csv( &fmt );
|
||||
ecl_sum = ecl_sum_fread_alloc_case__( data_file , ":" , include_restart);
|
||||
if (ecl_sum != NULL) {
|
||||
stringlist_type * key_list = stringlist_alloc_new( );
|
||||
build_key_list( ecl_sum , key_list );
|
||||
{
|
||||
char * csv_file = util_alloc_filename( NULL , ecl_sum_get_base(ecl_sum) , "txt"); // Weill save to current path; can use ecl_sum_get_path() to save to target path instead.
|
||||
FILE * stream = util_fopen( csv_file , "w");
|
||||
ecl_sum_fprintf(ecl_sum , stream , key_list , false , &fmt);
|
||||
fclose( stream );
|
||||
free( csv_file );
|
||||
}
|
||||
stringlist_free( key_list );
|
||||
ecl_sum_free(ecl_sum);
|
||||
} else
|
||||
fprintf(stderr,"summary2csv: No summary data found for case:%s\n", data_file );
|
||||
}
|
||||
}
|
||||
}
|
317
ThirdParty/Ert/devel/libecl/src/ecl_coarse_cell.c
vendored
Normal file
317
ThirdParty/Ert/devel/libecl/src/ecl_coarse_cell.c
vendored
Normal file
@ -0,0 +1,317 @@
|
||||
/*
|
||||
Copyright (c) 2012 statoil asa, norway.
|
||||
|
||||
The file 'ecl_coarse_cell.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <type_macros.h>
|
||||
#include <int_vector.h>
|
||||
|
||||
#include <ecl_kw_magic.h>
|
||||
#include <ecl_coarse_cell.h>
|
||||
|
||||
/******************************************************************/
|
||||
/*
|
||||
|
||||
+---------+---------+---------+---------+---------+---------+
|
||||
| 24 | 25 | 26 | 27 | 28 | 29 |
|
||||
| [X] | [X] | [X] | [X] | | |
|
||||
| | | | | | |
|
||||
+---------+=========+=========+=========+=========+---------+
|
||||
| 18 | 19 . 20 . 21 . 22 | 23 |
|
||||
| [X] | . . [C] . | | j
|
||||
| | . . . | | /|\
|
||||
+---------+. . . . ... . . . ... . . . ... . . . .+---------+ |
|
||||
| 12 | 13 . 14 . 15 . 16 | 17 | |
|
||||
| [X] | [A] . . [B] . | [X] | |
|
||||
| | . . . | | |
|
||||
+---------+. . . . ... . . . ... . . . ... . . . .+---------+ +---------> i
|
||||
| 6 | 7 . 8 . 9 . 10 | 11 |
|
||||
| | . . . | |
|
||||
| | . . . | |
|
||||
+---------+=========+=========+=========+=========+---------+
|
||||
| 0 | 1 | 2 | 3 | 4 | 5 |
|
||||
| | [X] | [X] | [X] | | |
|
||||
| | | | | | |
|
||||
+---------+---------+---------+---------+---------+---------+
|
||||
|
||||
|
||||
- A global grid of nx = 6, ny = 5 cells. The running number inside
|
||||
the cells represents the cells global index.
|
||||
|
||||
- The grid contains a coarse cell of dimensions 4 x 3, the coarse
|
||||
cell is in the global region: i \in [1,4] and j \in [1,3].
|
||||
|
||||
- When solving the dynamical equations the properties of the coarse
|
||||
cell will enter as one entity, but when defining the grid initially
|
||||
one can in prinsiple have more than one active cell. The example
|
||||
above shows that the coarse region has three active cells, marked
|
||||
with [A], [B] and [C] respectively.
|
||||
|
||||
- In the global grid the cells marked with [X] are active, i.e. when
|
||||
counting from zero the coarse cell will be the fourth active
|
||||
cell. This particular grid has a total of ten normal active cells
|
||||
(i.e. 10 x [X]) and one coarse cell which is active.
|
||||
|
||||
|
||||
- The dynamic variables like PRESSURE and SWAT are only represented
|
||||
with one element for the entire coarse cell. Static properties like
|
||||
PERMX and PORO are typically defined for all the cells in the
|
||||
entire grid, the different active cells in the coarse cell are
|
||||
stored in the active_cells vector, and when we just want one
|
||||
representative property from the the coarse cell the first (often
|
||||
only ...) element in this vector is used.
|
||||
|
||||
----
|
||||
|
||||
|
||||
- When we introduce coarse cells the mapping between global cell
|
||||
indices and active cell indices is not unique any longer. In the
|
||||
current grid active_index == 3 will point to the coarse cell, and
|
||||
all the cells in the coarse region will:
|
||||
|
||||
- ecl_grid_cell_active1( grid , global_index) => True
|
||||
- ecl_grid_get_active_index1( grid , global_index) => 3
|
||||
|
||||
|
||||
|
||||
For the example above the ecl_coarse_cell datastructure would have the
|
||||
following content:
|
||||
|
||||
ijk = {0 , 5 , 0 , 4 , k1 , k2 }
|
||||
active_index = 3
|
||||
cell_list = {7,8,9,10,13,14,15,16,19,20,21,22}
|
||||
active_cells = {13,15,21}
|
||||
active_values = {1,1,1} <= This can 2/3 for dual porosity.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#define ECL_COARSE_CELL_TYPE_ID 91554106
|
||||
|
||||
struct ecl_coarse_cell_struct {
|
||||
UTIL_TYPE_ID_DECLARATION;
|
||||
int ijk[6]; // {i1, i2 , j1 , j2 , k1 , k2}
|
||||
int active_index;
|
||||
int active_fracture_index;
|
||||
|
||||
bool __cell_list_sorted;
|
||||
int_vector_type * cell_list;
|
||||
int_vector_type * active_cells;
|
||||
int_vector_type * active_values;
|
||||
};
|
||||
|
||||
|
||||
static UTIL_SAFE_CAST_FUNCTION( ecl_coarse_cell , ECL_COARSE_CELL_TYPE_ID )
|
||||
|
||||
static void ecl_coarse_cell_assert( ecl_coarse_cell_type * coarse_cell ) {
|
||||
int box_size =
|
||||
(1 + coarse_cell->ijk[1] - coarse_cell->ijk[0]) *
|
||||
(1 + coarse_cell->ijk[2] - coarse_cell->ijk[3]) *
|
||||
(1 + coarse_cell->ijk[4] - coarse_cell->ijk[5]);
|
||||
|
||||
if (box_size != int_vector_size( coarse_cell->cell_list ))
|
||||
util_abort("%s: using invalid coarse cell. Box size:%d cells. cell_list:%d cells \n",__func__ , box_size , int_vector_size( coarse_cell->cell_list ));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ecl_coarse_cell_type * ecl_coarse_cell_alloc() {
|
||||
const int LARGE = 1 << 30;
|
||||
ecl_coarse_cell_type * coarse_cell = util_malloc( sizeof * coarse_cell );
|
||||
UTIL_TYPE_ID_INIT( coarse_cell , ECL_COARSE_CELL_TYPE_ID );
|
||||
|
||||
coarse_cell->ijk[0] = LARGE;
|
||||
coarse_cell->ijk[2] = LARGE;
|
||||
coarse_cell->ijk[4] = LARGE;
|
||||
|
||||
coarse_cell->ijk[1] = -LARGE;
|
||||
coarse_cell->ijk[3] = -LARGE;
|
||||
coarse_cell->ijk[5] = -LARGE;
|
||||
|
||||
coarse_cell->active_index = -1;
|
||||
coarse_cell->active_fracture_index = -1;
|
||||
coarse_cell->__cell_list_sorted = false;
|
||||
coarse_cell->cell_list = int_vector_alloc(0,0);
|
||||
coarse_cell->active_cells = int_vector_alloc(0,0);
|
||||
coarse_cell->active_values = int_vector_alloc(0,0);
|
||||
return coarse_cell;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Should not be called more than once with the same arguments; that is
|
||||
not checked.
|
||||
*/
|
||||
|
||||
void ecl_coarse_cell_update( ecl_coarse_cell_type * coarse_cell , int i , int j , int k , int global_index ) {
|
||||
coarse_cell->ijk[0] = util_int_min( coarse_cell->ijk[0] , i );
|
||||
coarse_cell->ijk[2] = util_int_min( coarse_cell->ijk[2] , j );
|
||||
coarse_cell->ijk[4] = util_int_min( coarse_cell->ijk[4] , k );
|
||||
|
||||
coarse_cell->ijk[1] = util_int_max( coarse_cell->ijk[1] , i );
|
||||
coarse_cell->ijk[3] = util_int_max( coarse_cell->ijk[3] , j );
|
||||
coarse_cell->ijk[5] = util_int_max( coarse_cell->ijk[5] , k );
|
||||
|
||||
int_vector_append( coarse_cell->cell_list , global_index );
|
||||
}
|
||||
|
||||
|
||||
void ecl_coarse_cell_free( ecl_coarse_cell_type * coarse_cell ) {
|
||||
int_vector_free( coarse_cell->cell_list );
|
||||
int_vector_free( coarse_cell->active_cells );
|
||||
int_vector_free( coarse_cell->active_values );
|
||||
free( coarse_cell );
|
||||
}
|
||||
|
||||
|
||||
void ecl_coarse_cell_free__( void * arg ) {
|
||||
ecl_coarse_cell_type * coarse_cell = ecl_coarse_cell_safe_cast( arg );
|
||||
ecl_coarse_cell_free( coarse_cell );
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
static void ecl_coarse_cell_sort( ecl_coarse_cell_type * coarse_cell ) {
|
||||
if (!coarse_cell->__cell_list_sorted) {
|
||||
int_vector_sort( coarse_cell->cell_list );
|
||||
coarse_cell->__cell_list_sorted = true;
|
||||
}
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_size( const ecl_coarse_cell_type * coarse_cell) {
|
||||
return int_vector_size( coarse_cell->cell_list );
|
||||
}
|
||||
|
||||
|
||||
int ecl_coarse_cell_iget_cell_index( ecl_coarse_cell_type * coarse_cell , int group_index) {
|
||||
ecl_coarse_cell_sort( coarse_cell );
|
||||
return int_vector_iget( coarse_cell->cell_list , group_index );
|
||||
}
|
||||
|
||||
const int * ecl_coarse_cell_get_index_ptr( ecl_coarse_cell_type * coarse_cell ) {
|
||||
ecl_coarse_cell_sort( coarse_cell );
|
||||
return int_vector_get_const_ptr( coarse_cell->cell_list );
|
||||
}
|
||||
|
||||
|
||||
const int_vector_type * ecl_coarse_cell_get_index_vector( ecl_coarse_cell_type * coarse_cell ) {
|
||||
ecl_coarse_cell_sort( coarse_cell );
|
||||
return coarse_cell->cell_list;
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
int ecl_coarse_cell_get_i1( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->ijk[0];
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_i2( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->ijk[1];
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_j1( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->ijk[2];
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_j2( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->ijk[3];
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_k1( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->ijk[4];
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_k2( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->ijk[5];
|
||||
}
|
||||
|
||||
const int * ecl_coarse_cell_get_box_ptr( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->ijk;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
void ecl_coarse_cell_update_index( ecl_coarse_cell_type * coarse_cell , int global_index , int * active_index , int * active_fracture_index , int active_value) {
|
||||
if (active_value & ACTIVE_MATRIX) {
|
||||
if (coarse_cell->active_index == -1) {
|
||||
coarse_cell->active_index = *active_index;
|
||||
(*active_index) += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (active_value & ACTIVE_FRACTURE) {
|
||||
if (coarse_cell->active_fracture_index == -1) {
|
||||
coarse_cell->active_fracture_index = *active_fracture_index;
|
||||
(*active_fracture_index) += 1;
|
||||
}
|
||||
}
|
||||
|
||||
int_vector_append( coarse_cell->active_cells , global_index );
|
||||
int_vector_append( coarse_cell->active_values , active_value );
|
||||
|
||||
if (int_vector_size( coarse_cell->active_values ) > 1) {
|
||||
if (int_vector_reverse_iget( coarse_cell->active_values , -2 ) != active_value)
|
||||
util_abort("%s: Sorry - current coarse cell implementation requires that all active cells have the same active value\n",__func__);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int ecl_coarse_cell_get_active_index( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->active_index;
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_active_fracture_index( const ecl_coarse_cell_type * coarse_cell ) {
|
||||
return coarse_cell->active_fracture_index;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Will return the global index of the 'ith active cell in the coarse
|
||||
cell.
|
||||
*/
|
||||
int ecl_coarse_cell_iget_active_cell_index( const ecl_coarse_cell_type * coarse_cell , int index) {
|
||||
return int_vector_iget( coarse_cell->active_cells , index );
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_iget_active_value( const ecl_coarse_cell_type * coarse_cell , int index) {
|
||||
return int_vector_iget( coarse_cell->active_values , index );
|
||||
}
|
||||
|
||||
int ecl_coarse_cell_get_num_active( const ecl_coarse_cell_type * coarse_cell) {
|
||||
return int_vector_size( coarse_cell->active_cells );
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
void ecl_coarse_cell_fprintf( ecl_coarse_cell_type * coarse_cell , FILE * stream ) {
|
||||
fprintf(stream,"Coarse box: \n");
|
||||
fprintf(stream," i : %3d - %3d\n",coarse_cell->ijk[0] , coarse_cell->ijk[1]);
|
||||
fprintf(stream," j : %3d - %3d\n",coarse_cell->ijk[2] , coarse_cell->ijk[3]);
|
||||
fprintf(stream," k : %3d - %3d\n",coarse_cell->ijk[4] , coarse_cell->ijk[5]);
|
||||
fprintf(stream," active_cells : " ); int_vector_fprintf( coarse_cell->active_cells , stream , "" , "%5d ");
|
||||
fprintf(stream," active_values : " ); int_vector_fprintf( coarse_cell->active_values , stream , "" , "%5d ");
|
||||
fprintf(stream," Cells : " ); int_vector_fprintf( coarse_cell->cell_list , stream , "" , "%5d ");
|
||||
}
|
57
ThirdParty/Ert/devel/libecl/src/ecl_coarse_cell.h
vendored
Normal file
57
ThirdParty/Ert/devel/libecl/src/ecl_coarse_cell.h
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
Copyright (c) 2012 statoil asa, norway.
|
||||
|
||||
The file 'ecl_coarse_cell.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __ECL_COARSE_CELL_H__
|
||||
#define __ECL_COARSE_CELL_H__
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct ecl_coarse_cell_struct ecl_coarse_cell_type;
|
||||
|
||||
ecl_coarse_cell_type * ecl_coarse_cell_alloc( );
|
||||
void ecl_coarse_cell_update( ecl_coarse_cell_type * coarse_cell , int i , int j , int k , int global_index );
|
||||
void ecl_coarse_cell_free( ecl_coarse_cell_type * coarse_cell );
|
||||
void ecl_coarse_cell_free__( void * arg );
|
||||
|
||||
int ecl_coarse_cell_get_i1( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_get_j1( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_get_k1( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_get_i2( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_get_j2( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_get_k2( const ecl_coarse_cell_type * coarse_cell );
|
||||
const int * ecl_coarse_cell_get_box_ptr( const ecl_coarse_cell_type * coarse_cell );
|
||||
|
||||
int ecl_coarse_cell_get_size( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_iget_cell_index( ecl_coarse_cell_type * coarse_cell , int group_index);
|
||||
const int * ecl_coarse_cell_get_index_ptr( ecl_coarse_cell_type * coarse_cell );
|
||||
const int_vector_type * ecl_coarse_cell_get_index_vector( ecl_coarse_cell_type * coarse_cell );
|
||||
|
||||
void ecl_coarse_cell_update_index( ecl_coarse_cell_type * coarse_cell , int global_index , int * active_index , int * active_fracture_index , int active_value); int ecl_coarse_cell_get_active_index( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_get_active_fracture_index( const ecl_coarse_cell_type * coarse_cell );
|
||||
int ecl_coarse_cell_iget_active_cell_index( const ecl_coarse_cell_type * coarse_cell , int index);
|
||||
int ecl_coarse_cell_iget_active_value( const ecl_coarse_cell_type * coarse_cell , int index);
|
||||
int ecl_coarse_cell_get_num_active( const ecl_coarse_cell_type * coarse_cell);
|
||||
void ecl_coarse_cell_fprintf( ecl_coarse_cell_type * coarse_cell , FILE * stream );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
11
ThirdParty/Ert/devel/libecl/tests/CMakeLists.txt
vendored
Normal file
11
ThirdParty/Ert/devel/libecl/tests/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
add_executable( ecl_coarse_test ecl_coarse_test.c )
|
||||
target_link_libraries( ecl_coarse_test ecl )
|
||||
|
||||
add_executable( ecl_restart_test ecl_restart_test.c )
|
||||
target_link_libraries( ecl_restart_test ecl )
|
||||
|
||||
add_test( ecl_coarse_test ${EXECUTABLE_OUTPUT_PATH}/ecl_coarse_test ${PROJECT_SOURCE_DIR}/test-data/Statoil/ECLIPSE/LGCcase/LGC_TESTCASE2.EGRID )
|
||||
add_test( ecl_restart_test ${EXECUTABLE_OUTPUT_PATH}/ecl_restart_test ${PROJECT_SOURCE_DIR}/test-data/Statoil/ECLIPSE/Gurbat/ECLIPSE.UNRST )
|
||||
|
||||
set_property( TEST ecl_coarse_test PROPERTY LABELS Statoil )
|
||||
set_property( TEST ecl_restart_test PROPERTY LABELS Statoil )
|
43
ThirdParty/Ert/devel/libecl/tests/ecl_coarse_test.c
vendored
Normal file
43
ThirdParty/Ert/devel/libecl/tests/ecl_coarse_test.c
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'ecl_coarse_test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <ecl_grid.h>
|
||||
#include <ecl_coarse_cell.h>
|
||||
|
||||
void assert_equal( bool equal ) {
|
||||
if (!equal)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int test_grid( const char * filename) {
|
||||
ecl_grid_type * GRID = ecl_grid_alloc( filename );
|
||||
|
||||
assert_equal( ecl_grid_have_coarse_cells( GRID ));
|
||||
assert_equal( ecl_grid_get_num_coarse_groups( GRID ) == 3384 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
test_grid( argv[1] );
|
||||
}
|
58
ThirdParty/Ert/devel/libecl/tests/ecl_restart_test.c
vendored
Normal file
58
ThirdParty/Ert/devel/libecl/tests/ecl_restart_test.c
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'ecl_restart_test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <ecl_file.h>
|
||||
|
||||
|
||||
bool test_get( ecl_file_type * rst_file , int day , int month , int year , int expected_index) {
|
||||
time_t sim_time = util_make_datetime(0,0,0,day,month,year);
|
||||
int seqnum_index = ecl_file_get_restart_index( rst_file , sim_time );
|
||||
if (seqnum_index == expected_index)
|
||||
return true;
|
||||
else {
|
||||
printf("ERROR: Date: %02d/%02d/%4d Got:%d Expected:%d \n",day , month , year , seqnum_index , expected_index);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
bool OK = true;
|
||||
const char * unrst_file = argv[1];
|
||||
|
||||
ecl_file_type * rst_file = ecl_file_open( unrst_file );
|
||||
|
||||
OK = OK && test_get( rst_file , 1 , 1 , 1998 , -1 );
|
||||
OK = OK && test_get( rst_file , 17 , 9 , 2003 , -1 );
|
||||
OK = OK && test_get( rst_file , 1 , 1 , 2008 , -1 );
|
||||
|
||||
OK = OK && test_get( rst_file , 1 , 1 , 2000 , 0 );
|
||||
OK = OK && test_get( rst_file , 1 , 10 , 2000 , 10 );
|
||||
OK = OK && test_get( rst_file , 1 , 3 , 2003 , 40 );
|
||||
OK = OK && test_get( rst_file , 31 , 12 , 2004 , 62 );
|
||||
|
||||
|
||||
if (OK)
|
||||
exit(0);
|
||||
else
|
||||
exit(1);
|
||||
}
|
5
ThirdParty/Ert/devel/libenkf/applications/ert_gui/share/doc/kr�sj.txt
vendored
Normal file
5
ThirdParty/Ert/devel/libenkf/applications/ert_gui/share/doc/kr�sj.txt
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
Refcase <-> history source
|
||||
* Kanskje det er ERT som må sjekka om verdien er sett og så utsetta evt. kjøring?
|
||||
|
||||
Copy from case:
|
||||
* kræsj.
|
3
ThirdParty/Ert/devel/libenkf/applications/ert_tui/CMake/config/ert_build_config_file.h.in
vendored
Normal file
3
ThirdParty/Ert/devel/libenkf/applications/ert_tui/CMake/config/ert_build_config_file.h.in
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
#define SITE_CONFIG_FILE "@SITE_CONFIG_FILE@"
|
||||
#define COMPILE_TIME_STAMP "@COMPILE_TIME_STAMP@"
|
||||
#define SVN_VERSION "@SVN_VERSION@"
|
47
ThirdParty/Ert/devel/libenkf/applications/ert_tui/CMakeLists.txt
vendored
Normal file
47
ThirdParty/Ert/devel/libenkf/applications/ert_tui/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )
|
||||
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
|
||||
include_directories( ${libplot_src_path} )
|
||||
include_directories( ${PLPLOT_HEADER} )
|
||||
|
||||
set( SITE_CONFIG_FILE /project/res/etc/ERT/site-config CACHE FILEPATH "Path to global ERT Configuration file")
|
||||
|
||||
set( src_list main.c enkf_tui_main.c enkf_tui_fs.c enkf_tui_ranking.c enkf_tui_misc.c enkf_tui_table.c enkf_tui_plot.c enkf_tui_plot_rft.c enkf_tui_plot_util.c
|
||||
enkf_tui_run.c enkf_tui_util.c enkf_tui_init.c enkf_tui_export.c enkf_tui_analysis.c enkf_tui_QC.c enkf_tui_help.c enkf_tui_simple.c ert_tui_jobs.c)
|
||||
|
||||
#exec_program( svnversion ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE SVN_VERSION)
|
||||
#exec_program( date OUTPUT_VARIABLE COMPILE_TIME_STAMP)
|
||||
|
||||
#exec_program( svnversion ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE SVN_VERSION)
|
||||
#exec_program( date OUTPUT_VARIABLE COMPILE_TIME_STAMP)
|
||||
#file( WRITE ${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.h "#define SVN_VERSION \"${SVN_VERSION}\"\n#define COMPILE_TIME_STAMP \"${COMPILE_TIME_STAMP}\"\n" )
|
||||
|
||||
|
||||
#add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.h COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/timestamp.cmake )
|
||||
#add_custom_target( timestamp ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build_timestep.h )
|
||||
#set_source_files_properties( ${CMAKE_CURRENT_BINARY_DIR}/build_timestamp.h
|
||||
# PROPERTIES GENERATED TRUE
|
||||
# HEADER_FILE_ONLY TRUE)
|
||||
|
||||
|
||||
execute_process(COMMAND date "+%Y-%m-%d %H:%M:%S" OUTPUT_VARIABLE BUILD_TIME )
|
||||
string(STRIP ${BUILD_TIME} BUILD_TIME)
|
||||
|
||||
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/git_commit.sh ${CMAKE_CURRENT_SOURCE_DIR} OUTPUT_VARIABLE GIT_COMMIT)
|
||||
string(STRIP ${GIT_COMMIT} GIT_COMMIT)
|
||||
|
||||
set_source_files_properties( main.c PROPERTIES COMPILE_DEFINITIONS "COMPILE_TIME_STAMP=\"${BUILD_TIME}\";GIT_COMMIT=\"${GIT_COMMIT}\";SITE_CONFIG_FILE=\"${SITE_CONFIG_FILE}\"")
|
||||
|
||||
add_executable( ert ${src_list} )
|
||||
target_link_libraries( ert enkf sched rms ecl geometry config plot job_queue analysis util )
|
||||
|
||||
if (ERT_INSTALL_PREFIX)
|
||||
set (destination ${CMAKE_INSTALL_PREFIX}/${ERT_INSTALL_PREFIX}/bin)
|
||||
else()
|
||||
set (destination ${CMAKE_INSTALL_PREFIX}/bin)
|
||||
endif()
|
||||
|
||||
install(TARGETS ert DESTINATION ${destination})
|
||||
if (INSTALL_GROUP)
|
||||
install(CODE "EXECUTE_PROCESS(COMMAND chgrp ${INSTALL_GROUP} ${destination}/ert)")
|
||||
install(CODE "EXECUTE_PROCESS(COMMAND chmod g+w ${destination}/ert)")
|
||||
endif()
|
327
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_help.c
vendored
Normal file
327
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_help.c
vendored
Normal file
@ -0,0 +1,327 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'enkf_tui_QC.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <util.h>
|
||||
#include <ctype.h>
|
||||
#include <menu.h>
|
||||
#include <enkf_tui_run.h>
|
||||
#include <enkf_tui_export.h>
|
||||
#include <enkf_tui_plot.h>
|
||||
#include <enkf_tui_table.h>
|
||||
#include <enkf_tui_fs.h>
|
||||
#include <enkf_tui_ranking.h>
|
||||
#include <enkf_tui_QC.h>
|
||||
#include <enkf_tui_help.h>
|
||||
#include <enkf_tui_misc.h>
|
||||
#include <enkf_main.h>
|
||||
#include <enkf_sched.h>
|
||||
|
||||
|
||||
void enkf_tui_help_manual_main( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s &" , browser , manual_url);
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_main(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Main menu" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "Choose the different options from the main menu to read more about the different options.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_main , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_manual_cases( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s%s &" , browser , manual_url , "#Manage_cases");
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_cases(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Manage cases" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "Use this menu to navgate between cases and to initialize cases. A case has to be initialized before it can be used.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_cases , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_manual_run( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s%s &" , browser , manual_url , "#Run_or_restart_experiment");
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_run(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Run menu" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "This menu is used to do the main work in ERT. The first option, x: Ensemble run: history, will just run the case without any data conditioning. Options r and s will initiate classical enkf runs. The two options i and t invokes the ensemble kalman smoother.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_run , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_manual_plot( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s%s &" , browser , manual_url , "#Plot_Results");
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_plot(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Plot results" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "Use this option to plot results after the case has been run.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_plot , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_manual_rank( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s%s &" , browser , manual_url , "#Rank_Results");
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_rank(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Rank results" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "Use this option to rank results after the case has been run.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_rank , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_manual_export( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s%s &" , browser , manual_url , "#Export_data_to_other_formats");
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_export(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Export results" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "Use this option to export results after the case has been run.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_export , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_manual_table( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s%s &" , browser , manual_url , "#Table_of_results");
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_table(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Table of results" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "This option can generate a table of results after the case has been run.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_table , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_manual_misc( void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
const site_config_type * site_config = enkf_main_get_site_config ( enkf_main );
|
||||
const char * manual_url = site_config_get_manual_url( site_config );
|
||||
const char * browser = getenv("BROWSER");
|
||||
|
||||
if (browser == NULL)
|
||||
browser = site_config_get_default_browser( site_config );
|
||||
|
||||
{
|
||||
char * cmd = util_alloc_sprintf("%s %s%s &" , browser , manual_url , "#Miscellaneous");
|
||||
system(cmd);
|
||||
free( cmd );
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_misc(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Miscellanous" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "This option contains miscellaneous options.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_misc , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
||||
void enkf_tui_help_menu_simple(void * arg) {
|
||||
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( arg );
|
||||
plot_config_type * plot_config = enkf_main_get_plot_config( enkf_main );
|
||||
{
|
||||
const char * plot_path = plot_config_get_path( plot_config );
|
||||
util_make_path( plot_path );
|
||||
}
|
||||
|
||||
{
|
||||
menu_type * menu = menu_alloc("Help: Simple menu" , "Back" , "bB");
|
||||
menu_add_helptext(menu , "The top four options in the simple menu will run eclipse simulations. Option s: Sensitivity run: No data conditioning, will initialize all parameters and run one eclipse simulation for each set of different parameters. e: Assimilation run: EnKF updates, will initialize all parameters and run one eclipse simulation for each set of different parameters. The eclipse simulations will run until the first data time step is encountered and condition on data before continuing. a: Assimilation run: Smoother update, will do one pass of the sensitivity run, then condition the parameters to all the data and rerun all experiments. i: Assimilation run: Iterated smoother [RML-EnKF] will iterate the smoother run several times.");
|
||||
menu_add_item(menu , "Open manual (firefox)" , "mM" , enkf_tui_help_manual_main , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
}
|
||||
}
|
||||
|
33
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_help.h
vendored
Normal file
33
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_help.h
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'enkf_tui_QC.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __ENKF_TUI_HELP_H__
|
||||
#define __ENKF_TUI_HELP_H__
|
||||
|
||||
|
||||
|
||||
void enkf_tui_help_menu_main(void * );
|
||||
void enkf_tui_help_menu_cases(void * );
|
||||
void enkf_tui_help_menu_run(void * );
|
||||
void enkf_tui_help_menu_plot(void * );
|
||||
void enkf_tui_help_menu_rank(void * );
|
||||
void enkf_tui_help_menu_export(void * );
|
||||
void enkf_tui_help_menu_table(void * );
|
||||
void enkf_tui_help_menu_misc(void * );
|
||||
void enkf_tui_help_menu_simple(void * );
|
||||
#endif
|
74
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_simple.c
vendored
Normal file
74
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_simple.c
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'enkf_tui_init.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <util.h>
|
||||
#include <ctype.h>
|
||||
#include <menu.h>
|
||||
#include <enkf_main.h>
|
||||
#include <enkf_sched.h>
|
||||
#include <enkf_types.h>
|
||||
#include <enkf_tui_util.h>
|
||||
#include <enkf_tui_init.h>
|
||||
#include <enkf_state.h>
|
||||
#include <enkf_node.h>
|
||||
#include <enkf_fs.h>
|
||||
#include <msg.h>
|
||||
#include <ensemble_config.h>
|
||||
#include <enkf_tui_run.h>
|
||||
#include <enkf_tui_plot.h>
|
||||
#include <enkf_tui_help.h>
|
||||
#include <enkf_tui_main.h>
|
||||
#include <ecl_config.h>
|
||||
|
||||
void enkf_tui_simple_menu(void * arg) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast(arg);
|
||||
menu_type * menu = menu_alloc("Simple menu" , "Quit" , "qQ");
|
||||
menu_add_item(menu , "Sensitivity run: No data conditioning" , "sS" , enkf_tui_run_exp , enkf_main , NULL);
|
||||
const ecl_config_type * ecl_config = enkf_main_get_ecl_config( enkf_main );
|
||||
const model_config_type * model_config = enkf_main_get_model_config( enkf_main );
|
||||
menu_item_type * enkf_item = menu_add_item(menu , "Assimilation run: EnKF updates" , "eE" , enkf_tui_run_start , enkf_main , NULL);
|
||||
menu_item_type * ES_item = menu_add_item(menu , "Assimilation run: Smoother update" , "aA" , enkf_tui_run_smoother , enkf_main , NULL);
|
||||
menu_item_type * it_ES_item = menu_add_item(menu , "Assimilation run: Iterated smoother [RML-EnKF]" , "iI" , enkf_tui_run_iterated_ES , enkf_main , NULL);
|
||||
if (!ecl_config_has_schedule( ecl_config )) {
|
||||
menu_item_disable( enkf_item );
|
||||
}
|
||||
if (!model_config_has_history( model_config )) {
|
||||
menu_item_disable( it_ES_item );
|
||||
menu_item_disable( ES_item );
|
||||
}
|
||||
menu_add_separator( menu );
|
||||
menu_add_item(menu , "Plot results" , "pP" , enkf_tui_plot_simple_menu , enkf_main , NULL);
|
||||
{
|
||||
menu_item_type * menu_item = menu_add_item( menu , "Create pdf reports" , "rR" , enkf_tui_plot_reports , enkf_main , NULL );
|
||||
ert_report_list_type * report_list = enkf_main_get_report_list( enkf_main );
|
||||
|
||||
if (ert_report_list_get_num( report_list ) == 0)
|
||||
menu_item_disable( menu_item );
|
||||
|
||||
}
|
||||
menu_add_separator(menu);
|
||||
menu_add_item(menu , "Help" , "hH" , enkf_tui_help_menu_simple , enkf_main , NULL);
|
||||
menu_add_item(menu , "Advanced menu" , "dD" , enkf_tui_main_menu , enkf_main , NULL);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
|
||||
}
|
26
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_simple.h
vendored
Normal file
26
ThirdParty/Ert/devel/libenkf/applications/ert_tui/enkf_tui_simple.h
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'enkf_tui_main.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __ENKF_TUI_SIMPLE_H__
|
||||
#define __ENKF_TUI_SIMPLE_H__
|
||||
|
||||
|
||||
void enkf_tui_simple_menu(void * );
|
||||
|
||||
|
||||
#endif
|
35
ThirdParty/Ert/devel/libenkf/applications/ert_tui/ert_tui_jobs.c
vendored
Normal file
35
ThirdParty/Ert/devel/libenkf/applications/ert_tui/ert_tui_jobs.c
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'ert_tui_jobs.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stringlist.h>
|
||||
|
||||
#include <enkf_main.h>
|
||||
#include <enkf_tui_plot.h>
|
||||
|
||||
void enkf_tui_plot_all_summary_JOB(void * self , const stringlist_type * args ) {
|
||||
enkf_main_type * enkf_main = enkf_main_safe_cast( self );
|
||||
int iens1 , iens2 , step1 , step2;
|
||||
bool prediction_mode;
|
||||
iens1 = 0;
|
||||
iens2 = enkf_main_get_ensemble_size( enkf_main );
|
||||
step1 = 0;
|
||||
step2 = 0;
|
||||
prediction_mode = true;
|
||||
enkf_tui_plot_all_summary__( enkf_main , iens1 , iens2 , step1 , step2 , prediction_mode );
|
||||
}
|
||||
|
3
ThirdParty/Ert/devel/libenkf/applications/ert_tui/git_commit.sh
vendored
Normal file
3
ThirdParty/Ert/devel/libenkf/applications/ert_tui/git_commit.sh
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
cd $1
|
||||
git rev-parse HEAD
|
64
ThirdParty/Ert/devel/libenkf/src/qc_config.c
vendored
Normal file
64
ThirdParty/Ert/devel/libenkf/src/qc_config.c
vendored
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'ert_qc_config.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <util.h>
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <config_keys.h>
|
||||
#include <qc_config.h>
|
||||
|
||||
|
||||
struct qc_config_struct {
|
||||
char * qc_path;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
qc_config_type * qc_config_alloc( const char * qc_path ) {
|
||||
qc_config_type * qc_config = util_malloc( sizeof * qc_config );
|
||||
qc_config->qc_path = NULL;
|
||||
qc_config_set_path( qc_config , qc_path );
|
||||
return qc_config;
|
||||
}
|
||||
|
||||
|
||||
void qc_config_free( qc_config_type * qc_config ) {
|
||||
util_safe_free( qc_config->qc_path );
|
||||
free( qc_config );
|
||||
}
|
||||
|
||||
|
||||
void qc_config_set_path( qc_config_type * qc_config , const char * qc_path) {
|
||||
qc_config->qc_path = util_realloc_string_copy( qc_config->qc_path , qc_path );
|
||||
}
|
||||
|
||||
|
||||
const char * qc_config_get_path( const qc_config_type * qc_config ) {
|
||||
return qc_config->qc_path;
|
||||
}
|
||||
|
||||
|
||||
void qc_config_init( qc_config_type * qc_config , const config_type * config) {
|
||||
if (config_item_set( config , QC_PATH_KEY ))
|
||||
qc_config_set_path( qc_config , config_get_value( config , QC_PATH_KEY ));
|
||||
}
|
40
ThirdParty/Ert/devel/libenkf/src/qc_config.h
vendored
Normal file
40
ThirdParty/Ert/devel/libenkf/src/qc_config.h
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'qc_config.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
#ifndef __QC_CONFIG_H__
|
||||
#define __QC_CONFIG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <config.h>
|
||||
|
||||
typedef struct qc_config_struct qc_config_type;
|
||||
|
||||
qc_config_type * qc_config_alloc(const char * qc_path);
|
||||
void qc_config_free();
|
||||
|
||||
void qc_config_set_path( qc_config_type * qc_config , const char * qc_path);
|
||||
const char * qc_config_get_path( const qc_config_type * qc_config );
|
||||
void qc_config_init( qc_config_type * qc_config , const config_type * config);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
4
ThirdParty/Ert/devel/libert_util/CMakeLists.txt
vendored
Normal file
4
ThirdParty/Ert/devel/libert_util/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
set( UTIL_INSTALL_PREFIX "" CACHE STRING "Prefix for installation of libert_util")
|
||||
add_subdirectory( src )
|
||||
add_subdirectory( applications )
|
||||
add_subdirectory( tests )
|
4
ThirdParty/Ert/devel/libert_util/applications/CMakeLists.txt
vendored
Normal file
4
ThirdParty/Ert/devel/libert_util/applications/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
link_directories( ${ERT_BINARY_DIR}/libert_util/src )
|
||||
|
||||
add_executable( matrix_test matrix_test.c )
|
||||
target_link_libraries( matrix_test ert_util )
|
36
ThirdParty/Ert/devel/libert_util/applications/SConscript
vendored
Normal file
36
ThirdParty/Ert/devel/libert_util/applications/SConscript
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
import os
|
||||
import os.path
|
||||
import sys
|
||||
sys.path += ["../../scons-config"]
|
||||
import global_config
|
||||
from global_config import LIBUTIL
|
||||
from global_config import add_static_library
|
||||
from global_config import add_header
|
||||
from global_config import get_target
|
||||
from global_config import add_program
|
||||
|
||||
bin_path = "../bin"
|
||||
env = Environment()
|
||||
conf = global_config.get_conf(os.getcwd() , 2)
|
||||
conf.update_env( env , [ LIBUTIL ] , ext_liblist = ["pthread" , "m" , "lapack" , "blas" , "z"] , link = True)
|
||||
|
||||
add_program(env , conf , bin_path , "template_test" , "template_test.c" )
|
||||
add_program(env , conf , bin_path , "latex_test" , "latex_test.c" )
|
||||
add_program(env , conf , bin_path , "replace.x" , "replace.c" )
|
||||
add_program(env , conf , bin_path , "matrix_test" , "matrix_test.c" )
|
||||
add_program(env , conf , bin_path , "ens_tar.x" , "ens_tar.c" )
|
||||
add_program(env , conf , bin_path , "file_open.x" , "file_open.c" )
|
||||
add_program(env , conf , bin_path , "grane_fix" , "grane_fix.c" )
|
||||
add_program(env , conf , bin_path , "grdecl_to_ascii.x" , "grdecl_to_ascii.c" )
|
||||
add_program(env , conf , bin_path , "parser_test.x" , "parser_test.c" )
|
||||
add_program(env , conf , bin_path , "mem_free.x" , "mem_free.c" )
|
||||
add_program(env , conf , bin_path , "eol-fix.x" , "eol-fix.c" )
|
||||
add_program(env , conf , bin_path , "util-test" , "util-test.c" )
|
||||
add_program(env , conf , bin_path , "util_contains_test" , "util_contains_test.c" )
|
||||
add_program(env , conf , bin_path , "util_fmove_test" , "util_fmove_test.c" )
|
||||
|
||||
|
||||
Default( bin_path )
|
||||
|
||||
|
||||
|
85
ThirdParty/Ert/devel/libert_util/applications/block_fs/bcp.c
vendored
Normal file
85
ThirdParty/Ert/devel/libert_util/applications/block_fs/bcp.c
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'bcp.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <block_fs.h>
|
||||
#include <vector.h>
|
||||
#include <signal.h>
|
||||
#include <msg.h>
|
||||
|
||||
|
||||
static void install_SIGNALS(void) {
|
||||
signal(SIGSEGV , util_abort_signal); /* Segmentation violation, i.e. overwriting memory ... */
|
||||
signal(SIGINT , util_abort_signal); /* Control C */
|
||||
signal(SIGTERM , util_abort_signal); /* If killing the program with SIGTERM (the default kill signal) you will get a backtrace. Killing with SIGKILL (-9) will not give a backtrace.*/
|
||||
}
|
||||
|
||||
|
||||
static void usage() {
|
||||
printf("Usage: \n\n");
|
||||
printf("bcp src.mnt target.mnt file \n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
install_SIGNALS();
|
||||
if (argc < 4)
|
||||
usage();
|
||||
{
|
||||
const char * src_mount = argv[1];
|
||||
const char * target_mount = argv[2];
|
||||
if (block_fs_is_mount(src_mount)) {
|
||||
const char * pattern = NULL;
|
||||
int iarg;
|
||||
|
||||
for (iarg = 3; iarg < argc; iarg++) {
|
||||
if (argv[iarg][0] == '-') {
|
||||
/** OK - this is an option .. */
|
||||
}
|
||||
else pattern = argv[iarg];
|
||||
}
|
||||
|
||||
{
|
||||
block_fs_type * src_fs = block_fs_mount(src_mount , 1 , 0 , 1 , 0 , false , true );
|
||||
block_fs_type * target_fs = block_fs_mount(target_mount , 1 , 0 , 1 , 0 , false , false );
|
||||
vector_type * files = block_fs_alloc_filelist( src_fs , pattern , NO_SORT , false );
|
||||
buffer_type * buffer = buffer_alloc( 1024 );
|
||||
{
|
||||
int i;
|
||||
msg_type * msg = msg_alloc("Copying :" , false);
|
||||
msg_show( msg );
|
||||
for (i=0; i < vector_get_size( files ); i++) {
|
||||
const user_file_node_type * node = vector_iget_const( files , i );
|
||||
const char * filename = user_file_node_get_filename( node );
|
||||
msg_update( msg , filename );
|
||||
block_fs_fread_realloc_buffer( src_fs , filename , buffer );
|
||||
block_fs_fwrite_buffer( target_fs , filename , buffer );
|
||||
}
|
||||
msg_free( msg , true );
|
||||
}
|
||||
buffer_free( buffer );
|
||||
vector_free( files );
|
||||
block_fs_close( target_fs , true);
|
||||
block_fs_close( src_fs , false );
|
||||
}
|
||||
} else
|
||||
fprintf(stderr,"The files:%s/%s does not seem to be a block_fs mount files.\n" , src_mount , target_mount);
|
||||
}
|
||||
}
|
100
ThirdParty/Ert/devel/libert_util/applications/block_fs/bfs_extract.c
vendored
Normal file
100
ThirdParty/Ert/devel/libert_util/applications/block_fs/bfs_extract.c
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'bfs_extract.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <block_fs.h>
|
||||
#include <vector.h>
|
||||
#include <signal.h>
|
||||
#include <msg.h>
|
||||
|
||||
/*****************************************************************/
|
||||
/*
|
||||
This program is used to extract individual files from a block_fs
|
||||
file.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
void install_SIGNALS(void) {
|
||||
signal(SIGSEGV , util_abort_signal); /* Segmentation violation, i.e. overwriting memory ... */
|
||||
signal(SIGINT , util_abort_signal); /* Control C */
|
||||
signal(SIGTERM , util_abort_signal); /* If killing the enkf program with SIGTERM (the default kill signal) you will get a backtrace. Killing with SIGKILL (-9) will not give a backtrace.*/
|
||||
}
|
||||
|
||||
|
||||
static void usage() {
|
||||
fprintf(stderr,"\nThis program is used to extract individual files from a block_fs\n");
|
||||
fprintf(stderr,"file. The arguments to the program are:\n");
|
||||
fprintf(stderr,"\n");
|
||||
fprintf(stderr," 1. The block_fs mount file.\n");
|
||||
fprintf(stderr," 2. The name of directory (need not exist) where the extracted files will be put.\n");
|
||||
fprintf(stderr," 3. A list of files to extract - this can contain wildcards, but they MUST be \n");
|
||||
fprintf(stderr," quoted to avoid expansion by the shell.\n");
|
||||
fprintf(stderr,"\n");
|
||||
fprintf(stderr,"Example:\n\n");
|
||||
fprintf(stderr," bash%% bfs_extract block_fs_file.mnt DOGFolder \'DOG*\'\n\n");
|
||||
fprintf(stderr,"This will extract all files starting with \'DOG\' to folder \'DOGFolder\'.\n\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
install_SIGNALS();
|
||||
if (argc < 3)
|
||||
usage();
|
||||
{
|
||||
|
||||
const char * mount_file = argv[1];
|
||||
if (block_fs_is_mount(mount_file)) {
|
||||
const char * target_path = argv[2];
|
||||
int iarg;
|
||||
if (!util_is_directory( target_path )) {
|
||||
if (util_file_exists( target_path ))
|
||||
util_exit("The target:%s already exists - but it is not a directory.\n");
|
||||
else
|
||||
util_make_path( target_path );
|
||||
}
|
||||
{
|
||||
block_fs_type * block_fs = block_fs_mount(mount_file , 1 , 0 , 1 , 0 , false , true );
|
||||
buffer_type * buffer = buffer_alloc(1024);
|
||||
msg_type * msg = msg_alloc("Extracting: " , false);
|
||||
msg_show( msg );
|
||||
|
||||
for (iarg = 3; iarg < argc; iarg++) {
|
||||
vector_type * files = block_fs_alloc_filelist( block_fs , argv[iarg] , NO_SORT , false );
|
||||
{
|
||||
int i;
|
||||
for (i=0; i < vector_get_size( files ); i++) {
|
||||
const user_file_node_type * node = vector_iget_const( files , i );
|
||||
const char * filename = user_file_node_get_filename( node );
|
||||
const char * target_file = util_alloc_filename( target_path , filename , NULL );
|
||||
msg_update( msg , filename );
|
||||
block_fs_fread_realloc_buffer( block_fs , filename , buffer );
|
||||
buffer_store( buffer , target_file );
|
||||
}
|
||||
}
|
||||
vector_free( files );
|
||||
}
|
||||
block_fs_close( block_fs , false );
|
||||
msg_free( msg , true );
|
||||
}
|
||||
} else
|
||||
fprintf(stderr,"The file:%s does not seem to be a block_fs mount file.\n" , mount_file);
|
||||
}
|
||||
}
|
221
ThirdParty/Ert/devel/libert_util/applications/block_fs/block_fs_test.c
vendored
Normal file
221
ThirdParty/Ert/devel/libert_util/applications/block_fs/block_fs_test.c
vendored
Normal file
@ -0,0 +1,221 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <util.h>
|
||||
#include <block_fs.h>
|
||||
#include <hash.h>
|
||||
|
||||
|
||||
typedef enum {
|
||||
WRITE_FILE = 1,
|
||||
DELETE_FILE = 2,
|
||||
CHECK = 3,
|
||||
ROTATE = 4
|
||||
} test_action_enum;
|
||||
|
||||
#define WRITE_FILE_STRING "WRITE_FILE"
|
||||
#define DELETE_FILE_STRING "DELETE_FILE"
|
||||
#define CHECK_STRING "CHECK"
|
||||
#define ROATATE_STRING "ROTATE"
|
||||
|
||||
|
||||
typedef struct {
|
||||
test_action_enum action;
|
||||
char * filename;
|
||||
int length;
|
||||
} action_node_type;
|
||||
|
||||
|
||||
action_node_type * action_node_alloc_new() {
|
||||
action_node_type * node = util_malloc( sizeof * node );
|
||||
node->filename = NULL;
|
||||
return node;
|
||||
}
|
||||
|
||||
void action_node_free(action_node_type * node) {
|
||||
util_safe_free( node->filename );
|
||||
free( node );
|
||||
}
|
||||
|
||||
void action_node_update(action_node_type * node, test_action_enum action , char * filename) {
|
||||
util_safe_free( node->filename );
|
||||
node->filename = filename; /* Node takes ownership of filename. */
|
||||
node->action = action;
|
||||
}
|
||||
|
||||
|
||||
void apply_delete_file( const action_node_type * node , block_fs_type * fs , const char * file_path) {
|
||||
if (block_fs_has_file( fs , node->filename)) {
|
||||
char * filename = util_alloc_filename( file_path , node->filename , NULL );
|
||||
block_fs_unlink_file( fs , node->filename );
|
||||
unlink( filename );
|
||||
free( filename );
|
||||
}
|
||||
}
|
||||
|
||||
void apply_check( block_fs_type * fs , const char * file_path ) {
|
||||
char * buffer1 = NULL;
|
||||
char * buffer2 = NULL;
|
||||
|
||||
int current_size = 0;
|
||||
vector_type * files = block_fs_alloc_filelist( fs , NULL , NO_SORT , false );
|
||||
int i;
|
||||
for (i=0; i < vector_get_size( files ); i++) {
|
||||
const user_file_node_type * node = vector_iget_const( files , i );
|
||||
int size = user_file_node_get_data_size( node );
|
||||
if (size > current_size) {
|
||||
current_size = size;
|
||||
buffer1 = util_realloc( buffer1 , current_size );
|
||||
buffer2 = util_realloc( buffer2 , current_size );
|
||||
}
|
||||
block_fs_fread_file( fs , user_file_node_get_filename( node ) , buffer1 );
|
||||
{
|
||||
char * filename = util_alloc_filename( file_path , user_file_node_get_filename( node ) , NULL);
|
||||
FILE * stream = util_fopen( filename , "r");
|
||||
util_fread( buffer2 , 1 , size , stream , __func__);
|
||||
fclose( stream );
|
||||
free( filename );
|
||||
}
|
||||
if (memcmp( buffer1 , buffer2 , size ) != 0)
|
||||
fprintf(stderr,"Fatal error ..\n");
|
||||
}
|
||||
vector_free( files );
|
||||
free( buffer1 );
|
||||
free( buffer2 );
|
||||
printf("CHeck OK \n");
|
||||
}
|
||||
|
||||
|
||||
void apply_write_file( const action_node_type * node , block_fs_type * fs , const char * file_path) {
|
||||
const int short_min = 128;
|
||||
const int short_max = 256;
|
||||
const int long_min = 4096;
|
||||
const int long_max = 32000;
|
||||
const double p_short = 0.75;
|
||||
int length;
|
||||
|
||||
|
||||
{
|
||||
int min,max;
|
||||
double R = rand() * 1.0 / RAND_MAX;
|
||||
if (R < p_short) {
|
||||
min = short_min;
|
||||
max = short_max;
|
||||
} else {
|
||||
min = long_min;
|
||||
max = long_max;
|
||||
}
|
||||
length = min + (rand() % (max - min + 1));
|
||||
}
|
||||
{
|
||||
char * buffer = util_malloc( length * sizeof * buffer );
|
||||
int i;
|
||||
for (i=0; i < length; i++)
|
||||
buffer[i] = rand() % 256;
|
||||
block_fs_fwrite_file( fs , node->filename , buffer , length);
|
||||
{
|
||||
char * filename = util_alloc_filename( file_path , node->filename , NULL );
|
||||
FILE * stream = util_fopen( filename , "w");
|
||||
util_fwrite( buffer , 1 , length , stream , __func__ );
|
||||
fclose( stream );
|
||||
free(filename);
|
||||
}
|
||||
free( buffer );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void apply( const action_node_type ** action_list , int action_length, block_fs_type * fs , const char * file_path) {
|
||||
int i;
|
||||
for (i=0; i < action_length; i++) {
|
||||
const action_node_type * node = action_list[i];
|
||||
switch( node->action) {
|
||||
case WRITE_FILE:
|
||||
apply_write_file( node , fs , file_path );
|
||||
break;
|
||||
case DELETE_FILE:
|
||||
apply_delete_file( node , fs , file_path );
|
||||
break;
|
||||
case CHECK:
|
||||
apply_check( fs , file_path );
|
||||
break;
|
||||
case ROTATE:
|
||||
block_fs_rotate( fs , 0.00 );
|
||||
break;
|
||||
default:
|
||||
util_abort("Unrecognized enum value:%d \n", node->action );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
|
||||
const char * test_path = "/tmp/block_fs";
|
||||
const char * file_fmt = "file_%04d";
|
||||
char * file_path = util_alloc_filename( test_path , "files" , NULL );
|
||||
char * test_mnt = util_alloc_filename( test_path , "FORECAST" , "mnt");
|
||||
char * test_log = util_alloc_filename( test_path , "test" , "log");
|
||||
|
||||
const int test_run = 10;
|
||||
const int block_size = 1000;
|
||||
const int max_files = 1000;
|
||||
|
||||
const int fs_block_size = 32;
|
||||
const int cache_size = 2048;
|
||||
const float frag_limit = 0.25;
|
||||
const int fsync_interval = 100;
|
||||
const bool preload = false;
|
||||
int run = 0;
|
||||
|
||||
block_fs_type * block_fs = block_fs_mount( test_mnt , fs_block_size , cache_size , frag_limit , fsync_interval , preload , false );
|
||||
|
||||
action_node_type ** action_list = util_malloc( block_size * sizeof * action_list );
|
||||
{
|
||||
int i;
|
||||
for (i=0; i < block_size; i++) {
|
||||
action_list[i] = action_node_alloc_new( );
|
||||
}
|
||||
}
|
||||
|
||||
while ( run < test_run) {
|
||||
int action_nr = 0;
|
||||
|
||||
run++;
|
||||
while (action_nr < (block_size - 2)) {
|
||||
char * filename = NULL;
|
||||
int R = rand() % 3;
|
||||
if (R == 0) {
|
||||
/* Delete file */
|
||||
filename = util_alloc_sprintf( file_fmt , rand() % max_files );
|
||||
action_node_update( action_list[ action_nr ] , DELETE_FILE , filename );
|
||||
} else {
|
||||
/* Create file */
|
||||
filename = util_alloc_sprintf( file_fmt , rand() % max_files );
|
||||
action_node_update( action_list[ action_nr ] , WRITE_FILE , filename );
|
||||
}
|
||||
|
||||
action_nr++;
|
||||
}
|
||||
action_node_update( action_list[ block_size - 2] , ROTATE , NULL);
|
||||
action_node_update( action_list[ block_size - 1] , CHECK , NULL);
|
||||
|
||||
apply( (const action_node_type **) action_list , block_size , block_fs , file_path );
|
||||
//fprintf_log( log_stream , action_list , block_size );
|
||||
printf("*"); fflush( stdout );
|
||||
}
|
||||
|
||||
{
|
||||
int i;
|
||||
for (i=0; i < block_size; i++)
|
||||
action_node_free( action_list[i] );
|
||||
|
||||
}
|
||||
free( action_list );
|
||||
block_fs_close( block_fs , true);
|
||||
|
||||
free( file_path );
|
||||
free( test_mnt );
|
||||
free( test_log );
|
||||
}
|
77
ThirdParty/Ert/devel/libert_util/applications/block_fs/bls.c
vendored
Normal file
77
ThirdParty/Ert/devel/libert_util/applications/block_fs/bls.c
vendored
Normal file
@ -0,0 +1,77 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'bls.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <block_fs.h>
|
||||
#include <vector.h>
|
||||
#include <signal.h>
|
||||
|
||||
|
||||
void install_SIGNALS(void) {
|
||||
signal(SIGSEGV , util_abort_signal); /* Segmentation violation, i.e. overwriting memory ... */
|
||||
signal(SIGINT , util_abort_signal); /* Control C */
|
||||
signal(SIGTERM , util_abort_signal); /* If killing the enkf program with SIGTERM (the default kill signal) you will get a backtrace. Killing with SIGKILL (-9) will not give a backtrace.*/
|
||||
}
|
||||
|
||||
|
||||
static int usage( void ) {
|
||||
fprintf(stderr,"\n");
|
||||
fprintf(stderr,"Usage:\n\n");
|
||||
fprintf(stderr," bash%% block_fs BLOCK_FILE.mnt <pattern>\n\n");
|
||||
fprintf(stderr,"Will list all elements in BLOCK_FILE matching pattern - remember to quote wildcards.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
install_SIGNALS();
|
||||
if (argc == 1)
|
||||
usage();
|
||||
{
|
||||
const char * mount_file = argv[1];
|
||||
if (block_fs_is_mount(mount_file)) {
|
||||
block_fs_sort_type sort_mode = OFFSET_SORT;
|
||||
const char * pattern = NULL;
|
||||
int iarg;
|
||||
|
||||
for (iarg = 2; iarg < argc; iarg++) {
|
||||
if (argv[iarg][0] == '-') {
|
||||
/** OK - this is an option .. */
|
||||
}
|
||||
else
|
||||
pattern = argv[iarg];
|
||||
}
|
||||
|
||||
{
|
||||
block_fs_type * block_fs = block_fs_mount(mount_file , 1 , 0 , 1 , 0 , false , true );
|
||||
vector_type * files = block_fs_alloc_filelist( block_fs , pattern , sort_mode , false );
|
||||
{
|
||||
int i;
|
||||
for (i=0; i < vector_get_size( files ); i++) {
|
||||
const user_file_node_type * node = vector_iget_const( files , i );
|
||||
printf("%-40s %10d %ld \n",user_file_node_get_filename( node ), user_file_node_get_data_size( node ) , user_file_node_get_node_offset( node ));
|
||||
}
|
||||
}
|
||||
vector_free( files );
|
||||
block_fs_close( block_fs , false );
|
||||
}
|
||||
} else
|
||||
fprintf(stderr,"The file:%s does not seem to be a block_fs mount file.\n" , mount_file);
|
||||
}
|
||||
}
|
73
ThirdParty/Ert/devel/libert_util/applications/block_fs/brm.c
vendored
Normal file
73
ThirdParty/Ert/devel/libert_util/applications/block_fs/brm.c
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'brm.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <block_fs.h>
|
||||
#include <vector.h>
|
||||
#include <signal.h>
|
||||
#include <msg.h>
|
||||
|
||||
|
||||
void install_SIGNALS(void) {
|
||||
signal(SIGSEGV , util_abort_signal); /* Segmentation violation, i.e. overwriting memory ... */
|
||||
signal(SIGINT , util_abort_signal); /* Control C */
|
||||
signal(SIGTERM , util_abort_signal); /* If killing the enkf program with SIGTERM (the default kill signal) you will get a backtrace. Killing with SIGKILL (-9) will not give a backtrace.*/
|
||||
}
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
install_SIGNALS();
|
||||
const char * mount_file = argv[1];
|
||||
if (block_fs_is_mount(mount_file)) {
|
||||
block_fs_sort_type sort_mode = OFFSET_SORT;
|
||||
const char * pattern = NULL;
|
||||
int iarg;
|
||||
|
||||
for (iarg = 2; iarg < argc; iarg++) {
|
||||
if (argv[iarg][0] == '-') {
|
||||
/** OK - this is an option .. */
|
||||
}
|
||||
else pattern = argv[iarg];
|
||||
}
|
||||
|
||||
{
|
||||
block_fs_type * block_fs = block_fs_mount(mount_file , 1 , 0 , 1 , 0 , false , false );
|
||||
vector_type * files = block_fs_alloc_filelist( block_fs , pattern , sort_mode , false );
|
||||
{
|
||||
int i;
|
||||
msg_type * msg = msg_alloc("Deleting file: " , false);
|
||||
msg_show( msg );
|
||||
//for (i=0; i < vector_get_size( files ); i++) {
|
||||
// const user_file_node_type * node = vector_iget_const( files , i );
|
||||
// printf("%-40s %10d %ld \n",user_file_node_get_filename( node ), user_file_node_get_data_size( node ) , user_file_node_get_node_offset( node ));
|
||||
//}
|
||||
|
||||
for (i=0; i < vector_get_size( files ); i++) {
|
||||
const user_file_node_type * node = vector_iget_const( files , i );
|
||||
msg_update( msg , user_file_node_get_filename( node ) );
|
||||
block_fs_unlink_file( block_fs , user_file_node_get_filename( node ));
|
||||
}
|
||||
msg_free( msg , true );
|
||||
printf("Final fragmentation: %5.2f \n", block_fs_get_fragmentation( block_fs ));
|
||||
}
|
||||
vector_free( files );
|
||||
block_fs_close( block_fs , false );
|
||||
}
|
||||
} else
|
||||
fprintf(stderr,"The file:%s does not seem to be a block_fs mount file.\n" , mount_file);
|
||||
}
|
41
ThirdParty/Ert/devel/libert_util/applications/block_fs/brot.c
vendored
Normal file
41
ThirdParty/Ert/devel/libert_util/applications/block_fs/brot.c
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'brot.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <block_fs.h>
|
||||
#include <vector.h>
|
||||
#include <signal.h>
|
||||
#include <msg.h>
|
||||
|
||||
|
||||
void install_SIGNALS(void) {
|
||||
signal(SIGSEGV , util_abort_signal); /* Segmentation violation, i.e. overwriting memory ... */
|
||||
signal(SIGINT , util_abort_signal); /* Control C */
|
||||
signal(SIGTERM , util_abort_signal); /* If killing the enkf program with SIGTERM (the default kill signal) you will get a backtrace. Killing with SIGKILL (-9) will not give a backtrace.*/
|
||||
}
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
install_SIGNALS();
|
||||
const char * mount_file = argv[1];
|
||||
if (block_fs_is_mount(mount_file)) {
|
||||
block_fs_type * block_fs = block_fs_mount(mount_file , 1 , 0 , 1 , 0 , false , false );
|
||||
block_fs_rotate( block_fs , 0.00 );
|
||||
block_fs_close( block_fs , false );
|
||||
}
|
||||
}
|
81
ThirdParty/Ert/devel/libert_util/applications/ens_tar.c
vendored
Normal file
81
ThirdParty/Ert/devel/libert_util/applications/ens_tar.c
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'ens_tar.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <util.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <msg.h>
|
||||
|
||||
bool tar_and_remove_dir( const char * root_path , const char * current_dir , int current_depth , void * arg) {
|
||||
if (strncmp(current_dir , "mem" , 3) == 0) {
|
||||
msg_type * msg = msg_safe_cast( arg );
|
||||
{
|
||||
char * tar_file = util_alloc_filename(NULL , current_dir , "tar.gz");
|
||||
char * target_file = util_alloc_filename(root_path , current_dir , "tar.gz");
|
||||
char * text = util_alloc_sprintf("%s: Creating %s" , root_path , tar_file );
|
||||
msg_update( msg , text );
|
||||
util_fork_exec("tar" , 3 , (const char *[3]) {"-czf" , tar_file , current_dir} , true , target_file , root_path , NULL , NULL , NULL);
|
||||
free( text );
|
||||
free( tar_file );
|
||||
free( target_file );
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
char * full_path = util_alloc_filename( root_path , current_dir , NULL);
|
||||
char * text = util_alloc_sprintf("%s: Removing %s" , root_path , current_dir);
|
||||
msg_update( msg , text );
|
||||
util_clear_directory( full_path , true , true );
|
||||
free( text );
|
||||
free( full_path );
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main( int argc , char ** argv ) {
|
||||
const char * mount_file = "enkf_mount_info";
|
||||
if (util_file_exists( mount_file )) {
|
||||
msg_type * msg = msg_alloc("" , false);
|
||||
char * cwd = util_alloc_cwd();
|
||||
|
||||
msg_show( msg );
|
||||
|
||||
util_walk_directory( cwd , NULL , NULL , tar_and_remove_dir , msg);
|
||||
msg_free( msg , true );
|
||||
free( cwd );
|
||||
} else
|
||||
fprintf(stderr,"Hmmm - could not find file: \'%s\' - this does not seem to be a ENSPATH directory.\n",mount_file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
/d/proj/bg/enkf/abuf/Gullfaks/Gullfaks_sector_model/Experiments_Real_Ens_100/cgg_seismic_prod_local_start_2_new/Ensemble_old/cgg_4d_prod_local_start_2_new
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
49
ThirdParty/Ert/devel/libert_util/applications/eol-fix.c
vendored
Normal file
49
ThirdParty/Ert/devel/libert_util/applications/eol-fix.c
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'eol-fix.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <util.h>
|
||||
|
||||
|
||||
|
||||
void eol_fix_file(const char * filename) {
|
||||
char * tmp_file = util_alloc_tmp_file("/tmp" , "eol-fix" , true);
|
||||
util_copy_file(filename , tmp_file );
|
||||
{
|
||||
FILE * src = util_fopen(tmp_file , "r");
|
||||
FILE * target = util_fopen(filename , "w");
|
||||
|
||||
do {
|
||||
int c = fgetc(src);
|
||||
if (c != '\r')
|
||||
fputc(c , target);
|
||||
} while (!feof(src));
|
||||
|
||||
fclose(src);
|
||||
fclose(target);
|
||||
}
|
||||
free(tmp_file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main (int argc , char **argv) {
|
||||
eol_fix_file(argv[1]);
|
||||
return 0;
|
||||
}
|
50
ThirdParty/Ert/devel/libert_util/applications/file_open.c
vendored
Normal file
50
ThirdParty/Ert/devel/libert_util/applications/file_open.c
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'file_open.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <pwd.h>
|
||||
#include <util.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int main( int argc , char ** argv ) {
|
||||
char hostname[256];
|
||||
gethostname( hostname , 255 );
|
||||
printf("%s : " , hostname);
|
||||
for (int iarg = 1; iarg < argc; iarg++) {
|
||||
uid_t * uid_list;
|
||||
int num_users;
|
||||
|
||||
printf("%s :",argv[iarg]);
|
||||
uid_list = util_alloc_file_users( argv[iarg] , &num_users);
|
||||
for (int i = 0; i < num_users; i++) {
|
||||
struct passwd * pwd = getpwuid( uid_list[i] );
|
||||
if (pwd != NULL)
|
||||
printf(" %s", pwd->pw_name);
|
||||
else
|
||||
printf(" %d",uid_list[ i ]);
|
||||
}
|
||||
|
||||
if (num_users == 0)
|
||||
printf(" <Not open>");
|
||||
printf("\n");
|
||||
|
||||
util_safe_free( uid_list );
|
||||
}
|
||||
}
|
57
ThirdParty/Ert/devel/libert_util/applications/grane_fix.c
vendored
Normal file
57
ThirdParty/Ert/devel/libert_util/applications/grane_fix.c
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'grane_fix.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <time.h>
|
||||
#include <buffer.h>
|
||||
|
||||
|
||||
static void fix_file(const char * filename, const char * target_path , buffer_type * buffer) {
|
||||
int iens;
|
||||
char ** tmp;
|
||||
int num;
|
||||
util_split_string( filename , "." , &num , &tmp);
|
||||
util_sscanf_int( tmp[2] , &iens );
|
||||
|
||||
{
|
||||
char * new_file = util_alloc_sprintf("%s%c%s_%04d" , target_path , UTIL_PATH_SEP_CHAR , tmp[0] , iens);
|
||||
FILE * target_stream = util_fopen( new_file , "w");
|
||||
|
||||
buffer_fread_realloc( buffer , filename );
|
||||
{
|
||||
char * ptr = buffer_get_data( buffer );
|
||||
int elements = (buffer_get_size(buffer) - 12)/8;
|
||||
double * data = (double *) &ptr[12];
|
||||
for (int i=0; i < elements; i++)
|
||||
fprintf(target_stream , "%g\n",data[i]);
|
||||
}
|
||||
|
||||
fclose( target_stream );
|
||||
}
|
||||
util_free_stringlist( tmp , num );
|
||||
}
|
||||
|
||||
|
||||
int main( int argc , char ** argv) {
|
||||
int iarg;
|
||||
buffer_type * buffer = buffer_alloc(199);
|
||||
|
||||
for (iarg = 1; iarg < argc; iarg++)
|
||||
fix_file( argv[iarg] , "init_files" , buffer);
|
||||
|
||||
}
|
61
ThirdParty/Ert/devel/libert_util/applications/grdecl_to_ascii.c
vendored
Normal file
61
ThirdParty/Ert/devel/libert_util/applications/grdecl_to_ascii.c
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'grdecl_to_ascii.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <parser.h>
|
||||
#include <util.h>
|
||||
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
if(argc < 2)
|
||||
util_exit("usage: grdecl_to_ascii file_1.grdecl file_2.grdecl ... file_N.grdecl.\n");
|
||||
|
||||
parser_type * parser = parser_alloc(" \t\r\n", NULL , NULL , NULL , "--", "\n");
|
||||
|
||||
for(int i=1; i<argc; i++)
|
||||
{
|
||||
char * basename;
|
||||
char * filename;
|
||||
FILE * stream;
|
||||
|
||||
stringlist_type * tokens = parser_tokenize_file(parser , argv[i], false);
|
||||
int num_tokens = stringlist_get_size(tokens);
|
||||
|
||||
util_alloc_file_components( argv[i], NULL, &basename, NULL);
|
||||
assert(basename != NULL);
|
||||
filename = util_alloc_filename(NULL, basename, "ascii");
|
||||
stream = util_fopen(filename, "w");
|
||||
|
||||
for(int i=0; i<num_tokens; i++)
|
||||
{
|
||||
double value;
|
||||
const char * value_str = stringlist_iget(tokens, i);
|
||||
if( util_sscanf_double(value_str, &value) )
|
||||
fprintf(stream, "%f\n", value);
|
||||
}
|
||||
|
||||
fclose(stream);
|
||||
free(filename);
|
||||
free(basename);
|
||||
}
|
||||
|
||||
parser_free(parser);
|
||||
|
||||
return 0;
|
||||
}
|
17
ThirdParty/Ert/devel/libert_util/applications/latex_test.c
vendored
Normal file
17
ThirdParty/Ert/devel/libert_util/applications/latex_test.c
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <util.h>
|
||||
#include <latex.h>
|
||||
|
||||
|
||||
int main( int argc , char ** argv) {
|
||||
latex_type * latex = latex_alloc( argv[1] , false );
|
||||
|
||||
latex_link_path( latex , "include/figs");
|
||||
if (latex_compile( latex , false , false ))
|
||||
printf("Have created pdf: %s \n",latex_get_target_file( latex ));
|
||||
else
|
||||
printf("Compilation failed - check directory: %s \n",latex_get_runpath( latex ));
|
||||
|
||||
latex_free( latex );
|
||||
}
|
66
ThirdParty/Ert/devel/libert_util/applications/matrix_test.c
vendored
Normal file
66
ThirdParty/Ert/devel/libert_util/applications/matrix_test.c
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'matrix_test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <matrix.h>
|
||||
#include <rng.h>
|
||||
#ifdef WITH_LAPACK
|
||||
#include <matrix_lapack.h>
|
||||
#endif
|
||||
|
||||
int main( int argc, char ** argv) {
|
||||
rng_type * rng = rng_alloc( MZRAN , INIT_DEV_RANDOM );
|
||||
matrix_type * A = matrix_alloc( 12 , 12 );
|
||||
matrix_type * B = matrix_alloc( 12 , 12 );
|
||||
matrix_random_init( A , rng );
|
||||
matrix_assign( B , A );
|
||||
matrix_pretty_print( A , " A " , "%8.4f" );
|
||||
|
||||
|
||||
#ifdef WITH_LAPACK
|
||||
matrix_inv( B );
|
||||
printf("\n");
|
||||
matrix_pretty_print( B , "inv(A)" , "%8.4f" );
|
||||
matrix_inplace_matmul( B , A );
|
||||
printf("\n");
|
||||
matrix_pretty_print( B , " I " , "%8.4f" );
|
||||
{
|
||||
matrix_type * A3 = matrix_alloc(3,3);
|
||||
matrix_random_init( A3 , rng );
|
||||
matrix_iset( A3 , 0 , 0 , sin(0.98));
|
||||
printf("matrix_det3:%g ",matrix_det3( A3 ));
|
||||
printf("matrix_det:%g \n",matrix_det( A3 ));
|
||||
}
|
||||
|
||||
{
|
||||
matrix_type * A4 = matrix_alloc(4,4);
|
||||
matrix_random_init( A4 , rng );
|
||||
matrix_iset( A4 , 0 , 0 , sin(0.98));
|
||||
printf("matrix_det4:%g ",matrix_det4( A4 ));
|
||||
printf("matrix_det:%g \n",matrix_det( A4 ));
|
||||
}
|
||||
#endif
|
||||
|
||||
matrix_free( A );
|
||||
matrix_free( B );
|
||||
rng_free( rng );
|
||||
}
|
||||
|
35
ThirdParty/Ert/devel/libert_util/applications/mem_free.c
vendored
Normal file
35
ThirdParty/Ert/devel/libert_util/applications/mem_free.c
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'mem_free.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <util.h>
|
||||
#include <msg.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
|
||||
int main(void) {
|
||||
|
||||
while (true) {
|
||||
printf("Ledig minne: %8d kB \n",util_proc_mem_free());
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
41
ThirdParty/Ert/devel/libert_util/applications/parser_test.c
vendored
Normal file
41
ThirdParty/Ert/devel/libert_util/applications/parser_test.c
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'parser_test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <parser.h>
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
parser_type * parser = parser_alloc(" \t\n\r,", "'\"", "[]{}=", NULL , "--", "\n");
|
||||
|
||||
if(argc < 2 )
|
||||
{
|
||||
printf("Usage: parser_test.x file.txt\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
stringlist_type * tokens = parser_tokenize_file(parser, argv[1] , true);
|
||||
|
||||
int num_tokens = stringlist_get_size(tokens);
|
||||
|
||||
for(int i = 0; i < num_tokens; i++)
|
||||
printf("token[%d] : %s\n", i, stringlist_iget(tokens, i) );
|
||||
|
||||
stringlist_free( tokens );
|
||||
parser_free( parser );
|
||||
return 0;
|
||||
}
|
36
ThirdParty/Ert/devel/libert_util/applications/replace.c
vendored
Normal file
36
ThirdParty/Ert/devel/libert_util/applications/replace.c
vendored
Normal file
@ -0,0 +1,36 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'replace.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <subst_list.h>
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
if(argc == 1 || argc % 2 != 0 )
|
||||
util_exit("Usage: replace.x from1 to1 from2 to2 ... fromN toN filename\n");
|
||||
|
||||
{
|
||||
subst_list_type * subst_list = subst_list_alloc( NULL );
|
||||
for(int i=1; i < argc-1; i += 2)
|
||||
subst_list_append_ref(subst_list, argv[i], argv[i+1] , NULL);
|
||||
|
||||
subst_list_update_file(subst_list, argv[argc-1]);
|
||||
subst_list_free(subst_list);
|
||||
}
|
||||
return 0;
|
||||
}
|
48
ThirdParty/Ert/devel/libert_util/applications/template_test.c
vendored
Normal file
48
ThirdParty/Ert/devel/libert_util/applications/template_test.c
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'template_test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <template.h>
|
||||
|
||||
|
||||
|
||||
bool test_path( const char * src_file , const char * cmp_file ) {
|
||||
const char * target_file = "/tmp/target.txt";
|
||||
template_type * template = template_alloc( src_file , true , NULL );
|
||||
printf("Instantiating template: %s ...... " , src_file);
|
||||
template_instantiate( template , target_file , NULL , true );
|
||||
if (util_files_equal( target_file , cmp_file ))
|
||||
printf("OK\n");
|
||||
else {
|
||||
printf("ERROR \n");
|
||||
printf("Compare files: %s and %s \n",cmp_file , target_file );
|
||||
exit(1);
|
||||
}
|
||||
template_free( template );
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main( int argc , char ** argv) {
|
||||
test_path("template_test_data/template1.txt" , "template_test_data/result1.txt");
|
||||
test_path("template_test_data/template2.txt" , "template_test_data/result2.txt");
|
||||
test_path("template_test_data/template3.txt" , "template_test_data/result3.txt");
|
||||
}
|
2
ThirdParty/Ert/devel/libert_util/applications/template_test_data/result1.txt
vendored
Normal file
2
ThirdParty/Ert/devel/libert_util/applications/template_test_data/result1.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
xxxx1234yyyy
|
||||
[hiddenvar][hiddenvar][hiddenvar]
|
14
ThirdParty/Ert/devel/libert_util/applications/template_test_data/result2.txt
vendored
Normal file
14
ThirdParty/Ert/devel/libert_util/applications/template_test_data/result2.txt
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
1
|
||||
|
||||
2
|
||||
|
||||
3
|
||||
|
||||
4
|
||||
|
||||
---
|
||||
vara
|
||||
varb
|
||||
varc
|
||||
|
8
ThirdParty/Ert/devel/libert_util/applications/template_test_data/result3.txt
vendored
Normal file
8
ThirdParty/Ert/devel/libert_util/applications/template_test_data/result3.txt
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
Her comes some text
|
||||
|
||||
A1 A2 A3
|
||||
|
||||
B1 B2 B3
|
||||
|
||||
C1 C2 C3
|
||||
|
2
ThirdParty/Ert/devel/libert_util/applications/template_test_data/template1.txt
vendored
Normal file
2
ThirdParty/Ert/devel/libert_util/applications/template_test_data/template1.txt
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
xxxx{% for $x in [1,2,3,4] %}$x{% endfor %}yyyy
|
||||
{% for var in [A,B,C] %}[hiddenvar]{% endfor %}
|
6
ThirdParty/Ert/devel/libert_util/applications/template_test_data/template2.txt
vendored
Normal file
6
ThirdParty/Ert/devel/libert_util/applications/template_test_data/template2.txt
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
{% for $x in [1,2,3,4] %}
|
||||
$x
|
||||
{% endfor %}
|
||||
---
|
||||
{% for $x in [a,b,c] %}var$x
|
||||
{% endfor %}
|
4
ThirdParty/Ert/devel/libert_util/applications/template_test_data/template3.txt
vendored
Normal file
4
ThirdParty/Ert/devel/libert_util/applications/template_test_data/template3.txt
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
Her comes some text
|
||||
{% for $x in [A,B,C] %}
|
||||
{% for $y in [1,2,3] %}$x$y {% endfor %}
|
||||
{% endfor %}
|
66
ThirdParty/Ert/devel/libert_util/applications/util-test.c
vendored
Normal file
66
ThirdParty/Ert/devel/libert_util/applications/util-test.c
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'util-test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <util.h>
|
||||
#include <string.h>
|
||||
#include <path_fmt.h>
|
||||
#include <stdarg.h>
|
||||
#include <hash.h>
|
||||
#include <unistd.h>
|
||||
#include <thread_pool.h>
|
||||
#include <stringlist.h>
|
||||
#include <menu.h>
|
||||
#include <arg_pack.h>
|
||||
#include <vector.h>
|
||||
#include <double_vector.h>
|
||||
#include <matrix.h>
|
||||
#include <matrix_lapack.h>
|
||||
#include <matrix_blas.h>
|
||||
#include <parser.h>
|
||||
#include <block_fs.h>
|
||||
#include <thread_pool.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <subst_list.h>
|
||||
#include <subst_func.h>
|
||||
#include <buffer.h>
|
||||
#include <mzran.h>
|
||||
#include <statistics.h>
|
||||
#include <double_vector.h>
|
||||
#include <lookup_table.h>
|
||||
|
||||
|
||||
|
||||
int main(int argc , char ** argv) {
|
||||
int a = 1;
|
||||
int b = 1;
|
||||
for (int i=0; i < 24; i++) {
|
||||
matrix_type * m = matrix_alloc( a , b );
|
||||
printf(" m: [%d,%d] \n",a,b);
|
||||
a *= 2;
|
||||
b *= 2;
|
||||
matrix_free( m );
|
||||
}
|
||||
}
|
||||
|
||||
|
44
ThirdParty/Ert/devel/libert_util/applications/util_contains_test.c
vendored
Normal file
44
ThirdParty/Ert/devel/libert_util/applications/util_contains_test.c
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'util_contains_test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
|
||||
#include <util.h>
|
||||
|
||||
|
||||
void test_contains(const int * data , int len , int value , bool target_value , const char * msg) {
|
||||
if (util_sorted_contains_int( data , len , value ) == target_value)
|
||||
printf("%60s : OK \n",msg);
|
||||
else
|
||||
printf("%60s : FAILED \n",msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main( int argc , char ** argv) {
|
||||
test_contains( (const int[6]) {0 , 10 , 11 , 12 , 16 , 20} , 6 , 0 , true , "Test start equal" );
|
||||
test_contains( (const int[6]) {0 , 10 , 11 , 12 , 16 , 20} , 6 , 20 , true , "Test end equal" );
|
||||
|
||||
test_contains( (const int[6]) {0 , 10 , 11 , 12 , 16 , 20} , 6 , 22 , false , "Test too large" );
|
||||
test_contains( (const int[6]) {0 , 10 , 11 , 12 , 16 , 20} , 6 , -22 , false , "Test too small" );
|
||||
|
||||
test_contains( (const int[6]) {0 , 10 , 11 , 12 , 16 , 20} , 6 , 11 , true , "Test contains - even" );
|
||||
test_contains( (const int[6]) {0 , 10 , 11 , 12 , 16 , 20} , 6 , 9 , false , "Test !contains - even" );
|
||||
|
||||
test_contains( (const int[7]) {0 , 7, 10 , 11 , 12 , 16 , 20} , 7 , 11 , true , "Test contains - odd" );
|
||||
test_contains( (const int[7]) {0 , 7 , 10 , 11 , 12 , 16 , 20} , 7 , 9 , false , "Test !contains - odd" );
|
||||
}
|
111
ThirdParty/Ert/devel/libert_util/applications/util_fmove_test.c
vendored
Normal file
111
ThirdParty/Ert/devel/libert_util/applications/util_fmove_test.c
vendored
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'util_fmove_test.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <util.h>
|
||||
|
||||
|
||||
|
||||
|
||||
void test_init_txt( const char * name1 , const char * name2 ) {
|
||||
FILE * stream1 = util_fopen(name1 , "w");
|
||||
FILE * stream2 = util_fopen(name2 , "w");
|
||||
fprintf(stream1 , "01456789");
|
||||
fprintf(stream2 , "0123456789");
|
||||
fclose( stream1 );
|
||||
fclose( stream2 );
|
||||
}
|
||||
|
||||
|
||||
void test_init( const char * name1 , const char * name2 , int size , int offset , int shift ) {
|
||||
FILE * stream1 = util_fopen(name1 , "w");
|
||||
FILE * stream2 = util_fopen(name2 , "w");
|
||||
{
|
||||
int i;
|
||||
if (shift > 0) {
|
||||
for (i=0; i < size; i++) {
|
||||
int c = random( ) % 256;
|
||||
fputc( c , stream2 );
|
||||
|
||||
if (i == offset) {
|
||||
for (int j=0; j < shift; j++)
|
||||
fputc( 0 , stream1 );
|
||||
}
|
||||
fputc( c , stream1 );
|
||||
}
|
||||
if (i == offset) {
|
||||
for (int j=0; j < shift; j++)
|
||||
fputc( 0 , stream1 );
|
||||
}
|
||||
} else {
|
||||
for (i=0; i < size; i++) {
|
||||
int c = random( ) % 256;
|
||||
fputc( c , stream2 );
|
||||
|
||||
if ((i >= offset) || (i < (offset + shift)))
|
||||
fputc( c , stream1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose( stream1 );
|
||||
fclose( stream2 );
|
||||
}
|
||||
|
||||
|
||||
int test_fmove( const char * name1 , const char * name2 , int size , int offset , int shift ) {
|
||||
|
||||
int fmove_value;
|
||||
FILE * stream = util_fopen( name2 , "r+");
|
||||
fmove_value = util_fmove( stream , offset , shift );
|
||||
fclose( stream );
|
||||
|
||||
return fmove_value;
|
||||
}
|
||||
|
||||
|
||||
void run_test( int size , int offset , int shift , int target_value ) {
|
||||
const char * name1 = "/tmp/test1";
|
||||
const char * name2 = "/tmp/test2";
|
||||
|
||||
test_init( name1 , name2 , size , offset , shift);
|
||||
if (test_fmove(name1 , name2 , size , offset , shift) == target_value) {
|
||||
if (target_value == 0) {
|
||||
if (util_files_equal( name1 , name2))
|
||||
printf("Files equal OK \n");
|
||||
else
|
||||
printf("ERROR: Files different \n");
|
||||
} else
|
||||
printf("Return value OK \n");
|
||||
}
|
||||
unlink( name1 );
|
||||
unlink( name2 );
|
||||
}
|
||||
|
||||
|
||||
int main( int argc , char ** argv) {
|
||||
run_test( 10000000 , 10000 , -1000 , 0 ); // Normal negative shift
|
||||
run_test( 10000000 , 10000 , 1000 , 0 ); // Normal positive shift
|
||||
run_test( 10000000 , 10000 , -1000 , 0 ); // Normal negative shift
|
||||
run_test( 10000000 , 10000 , -90000 , EINVAL ); // Negative shift before start of file
|
||||
run_test( 1000 , 10000 , -90000 , EINVAL ); // Offset beyond end of file
|
||||
run_test( 1000 , 1000 , 1000 , 0 ); // Positove shift at end of file
|
||||
run_test( 1000 , 1000 , -100 , 0 ); // Negative shift at end of file
|
||||
}
|
92
ThirdParty/Ert/devel/libert_util/src/CMakeLists.txt
vendored
Normal file
92
ThirdParty/Ert/devel/libert_util/src/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,92 @@
|
||||
set(source_files rng.c lookup_table.c statistics.c mzran.c set.c hash_node.c hash_sll.c hash.c node_data.c node_ctype.c util.c thread_pool.c msg.c arg_pack.c path_fmt.c menu.c subst_list.c subst_func.c vector.c parser.c stringlist.c matrix.c buffer.c log.c template.c timer.c)
|
||||
|
||||
|
||||
set(header_files ssize_t.h type_macros.h rng.h lookup_table.h statistics.h mzran.h set.h hash.h hash_node.h hash_sll.h node_data.h node_ctype.h util.h thread_pool.h msg.h arg_pack.h path_fmt.h stringlist.h menu.h subst_list.h subst_func.h vector.h parser.h matrix.h buffer.h log.h template.h timer.h )
|
||||
|
||||
if (WITH_LATEX)
|
||||
add_definitions( -DWITH_LATEX )
|
||||
add_definitions( -DLATEX_CMD=\"${LATEX_PATH}\")
|
||||
|
||||
list( APPEND source_files latex.c )
|
||||
list( APPEND header_files latex.h )
|
||||
endif()
|
||||
|
||||
if (WITH_LAPACK)
|
||||
list( APPEND source_files matrix_lapack.c matrix_blas.c regression.c lars.c stepwise.c)
|
||||
list( APPEND header_files matrix_lapack.h matrix_blas.h regression.h lars.h stepwise.h)
|
||||
endif()
|
||||
|
||||
if (MSVC)
|
||||
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/msvc_stdbool.h ${CMAKE_CURRENT_BINARY_DIR}/stdbool.h)
|
||||
list( APPEND header_files ${CMAKE_CURRENT_BINARY_DIR}/stdbool.h)
|
||||
endif()
|
||||
|
||||
foreach (type int double bool long time_t size_t float)
|
||||
set(TYPE ${type} )
|
||||
set(src_target ${CMAKE_CURRENT_BINARY_DIR}/${type}_vector.c)
|
||||
set(header_target ${CMAKE_CURRENT_BINARY_DIR}/${type}_vector.h)
|
||||
|
||||
configure_file( vector_template.c ${src_target})
|
||||
configure_file( vector_template.h ${header_target})
|
||||
|
||||
list( APPEND source_files ${src_target} )
|
||||
list( APPEND header_files ${header_target} )
|
||||
endforeach( type )
|
||||
|
||||
|
||||
#if (WITH_ZLIB)
|
||||
# list( APPEND source_files util_zlib.c )
|
||||
# list( APPEND header_files util_zlib.h )
|
||||
#endif()
|
||||
|
||||
# The block_fs filesystem is so heavily dependant on pthreads that it is not
|
||||
# built if de not have pthreads.
|
||||
if (WITH_PTHREAD)
|
||||
list( APPEND source_files block_fs.c )
|
||||
list( APPEND header_files block_fs.h )
|
||||
|
||||
list( APPEND header_files thread_pool_posix.h )
|
||||
endif()
|
||||
|
||||
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
|
||||
include_directories( ${CMAKE_CURRENT_SOURCE_DIR} )
|
||||
|
||||
add_library( ert_util ${LIBRARY_TYPE} ${source_files} )
|
||||
|
||||
if (WITH_PTHREAD)
|
||||
target_link_libraries( ert_util ${PTHREAD_LIBRARY} )
|
||||
include_directories( ${PTHREAD_HEADER} )
|
||||
endif()
|
||||
|
||||
if (WITH_ZLIB)
|
||||
target_link_libraries( ert_util ${ZLIB_LIBRARY} )
|
||||
include_directories( ${ZLIB_HEADER} )
|
||||
endif()
|
||||
|
||||
if ( ERT_WINDOWS )
|
||||
target_link_libraries( ert_util ${SHWLAPI_LIBRARY} )
|
||||
endif()
|
||||
|
||||
if (WITH_LAPACK)
|
||||
target_link_libraries( ert_util ${LAPACK_LIBRARY} )
|
||||
if (NEED_BLAS)
|
||||
target_link_libraries( ert_util ${BLAS_LIBRARY} )
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (NEED_LIBM)
|
||||
target_link_libraries( ert_util m )
|
||||
endif()
|
||||
|
||||
#-----------------------------------------------------------------
|
||||
|
||||
if (UTIL_INSTALL_PREFIX)
|
||||
install(TARGETS ert_util DESTINATION ${CMAKE_INSTALL_PREFIX}/${UTIL_INSTALL_PREFIX}/lib)
|
||||
install(FILES ${header_files} DESTINATION ${CMAKE_INSTALL_PREFIX}/${UTIL_INSTALL_PREFIX}/include)
|
||||
else()
|
||||
install(TARGETS ert_util DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
|
||||
install(FILES ${header_files} DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
|
||||
endif()
|
||||
|
||||
|
||||
|
44
ThirdParty/Ert/devel/libert_util/src/all_include.h
vendored
Normal file
44
ThirdParty/Ert/devel/libert_util/src/all_include.h
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'all_include.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <set.h>
|
||||
#include <hash.h>
|
||||
#include <hash_node.h>
|
||||
#include <hash_sll.h>
|
||||
#include <list_node.h>
|
||||
#include <list.h>
|
||||
#include <node_data.h>
|
||||
#include <node_ctype.h>
|
||||
#include <util.h>
|
||||
#include <thread_pool.h>
|
||||
#include <msg.h>
|
||||
#include <arg_pack.h>
|
||||
#include <str_buffer.h>
|
||||
#include <path_fmt.h>
|
||||
#include <stringlist.h>
|
||||
#include <menu.h>
|
||||
#include <subst.h
|
||||
#include <vector.h>
|
||||
#include <matrix.h>
|
||||
#include <matrix_lapack.h>
|
||||
#include <matrix_blas.h>
|
||||
#include <buffer.h>
|
||||
#include <block_fs.h>
|
||||
#include <log.h>
|
||||
#include <template.h>
|
||||
#include <timer.h>
|
643
ThirdParty/Ert/devel/libert_util/src/arg_pack.c
vendored
Normal file
643
ThirdParty/Ert/devel/libert_util/src/arg_pack.c
vendored
Normal file
@ -0,0 +1,643 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'arg_pack.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <arg_pack.h>
|
||||
#include <node_ctype.h>
|
||||
|
||||
|
||||
/**
|
||||
This file implements a arg_pack structure which is a small
|
||||
convienence utility to pack several arguments into one
|
||||
argument. The generic use situtation is when calling functions like
|
||||
e.g. pthread_create() which take one (void *) as argument. You can
|
||||
then pack several arguments into one arg_pack instance, and then
|
||||
unpack them at the other end.
|
||||
|
||||
The content of the arg_pack is mainly inserted by appending, in
|
||||
addition it is possible to insert new items by using _iset()
|
||||
functions, however these functions will fail hard if the resulting
|
||||
call sequence will lead to holes in the structure, i.e.
|
||||
|
||||
arg_pack_type * arg_pack = arg_pack_alloc()
|
||||
arg_pack_append_int( arg_pack , 1);
|
||||
arg_pack_iset_int( arg_pack , 3 , 0); <--- Will fail hard because
|
||||
elements 1,2,3 have not been set.
|
||||
|
||||
|
||||
When you take them out again that is done with indexed get.
|
||||
|
||||
When elements are inserted into the arg_pack, they are inserted
|
||||
with a (limited) type information (implictly given by the function
|
||||
invoked to insert the argument), and the corresponding typed get
|
||||
must be used to unpack the argument again afterwards. The
|
||||
excepetion is with the function arg_pack_iget_adress() which can be
|
||||
used to extract the reference of a scalar.
|
||||
|
||||
|
||||
|
||||
Example:
|
||||
--------
|
||||
|
||||
void some_function(const char * arg1 , int arg2 , double arg3) {
|
||||
.....
|
||||
}
|
||||
|
||||
|
||||
void some_function__(void * __arg_pack) {
|
||||
arg_pack_type * arg_pack = arg_pack_safe_cast( __arg_pack );
|
||||
const char * arg1 = arg_pack_iget_ptr( arg_pack , 0);
|
||||
int arg2 = arg_pack_iget_int( arg_pack , 1);
|
||||
double arg3 = arg_pack_iget_double(arg_pack , 2);
|
||||
|
||||
some_function( arg1 , arg2 , arg3 );
|
||||
}
|
||||
|
||||
|
||||
.....
|
||||
arg_pack_type * arg_pack = arg_pack_alloc();
|
||||
arg_pack_append_ptr(arg_pack , "ARG1");
|
||||
arg_pack_append_int(arg_pack , 1);
|
||||
arg_pack_append_double(arg_pack , 3.14159265);
|
||||
|
||||
pthread_create( , , some_function__ , arg_pack);
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
#define ARG_PACK_TYPE_ID 668268
|
||||
|
||||
|
||||
typedef struct {
|
||||
void * buffer; /* This is the actual content - can either point to a remote object, or to storage managed by the arg_pack instance. */
|
||||
node_ctype ctype; /* The type of the data which is stored. */
|
||||
arg_node_free_ftype * destructor; /* destructor called on buffer - can be NULL. */
|
||||
arg_node_copyc_ftype * copyc; /* copy constructor - will typically be NULL. */
|
||||
} arg_node_type;
|
||||
|
||||
|
||||
|
||||
struct arg_pack_struct {
|
||||
UTIL_TYPE_ID_DECLARATION;
|
||||
int size; /* The number of arguments appended to this arg_pack instance. */
|
||||
int alloc_size; /* The number of nodes allocated to this arg_pack - will in general be greater than size. */
|
||||
bool locked; /* To insure against unwaranted modifictaions - you can explicitly lock the arg_pack instance. This only */
|
||||
arg_node_type **nodes; /* Vector of nodes */
|
||||
};
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/* First comes the arg_node functions. These are all fully static.*/
|
||||
|
||||
static arg_node_type * arg_node_alloc_empty() {
|
||||
arg_node_type * node = util_malloc( sizeof * node );
|
||||
node->buffer = NULL;
|
||||
node->destructor = NULL;
|
||||
node->ctype = CTYPE_INVALID;
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_realloc_buffer(arg_node_type * node , int new_size) {
|
||||
node->buffer = util_realloc(node->buffer , new_size );
|
||||
}
|
||||
|
||||
|
||||
static void __arg_node_assert_type(const arg_node_type * node , node_ctype arg_type) {
|
||||
if (arg_type != node->ctype)
|
||||
util_abort("%s: asked for type:\'%s\' inserted as:\'%s\' - aborting \n" , __func__ , node_ctype_name(arg_type) , node_ctype_name(node->ctype));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
#define ARG_NODE_GET_RETURN(type) \
|
||||
{ \
|
||||
type value; \
|
||||
memcpy(&value , node->buffer , sizeof value); \
|
||||
return value; \
|
||||
}
|
||||
|
||||
|
||||
static int arg_node_get_int( const arg_node_type * node) {
|
||||
__arg_node_assert_type( node , CTYPE_INT_VALUE );
|
||||
ARG_NODE_GET_RETURN( int )
|
||||
}
|
||||
|
||||
static char arg_node_get_char( const arg_node_type * node) {
|
||||
__arg_node_assert_type( node , CTYPE_CHAR_VALUE );
|
||||
ARG_NODE_GET_RETURN( char )
|
||||
}
|
||||
|
||||
static double arg_node_get_double( const arg_node_type * node) {
|
||||
__arg_node_assert_type( node , CTYPE_DOUBLE_VALUE );
|
||||
ARG_NODE_GET_RETURN( double )
|
||||
}
|
||||
|
||||
static float arg_node_get_float( const arg_node_type * node) {
|
||||
__arg_node_assert_type( node , CTYPE_FLOAT_VALUE );
|
||||
ARG_NODE_GET_RETURN( float )
|
||||
}
|
||||
|
||||
static bool arg_node_get_bool( const arg_node_type * node) {
|
||||
__arg_node_assert_type( node , CTYPE_BOOL_VALUE );
|
||||
ARG_NODE_GET_RETURN( bool )
|
||||
}
|
||||
|
||||
static size_t arg_node_get_size_t( const arg_node_type * node) {
|
||||
__arg_node_assert_type( node , CTYPE_SIZE_T_VALUE );
|
||||
ARG_NODE_GET_RETURN( size_t )
|
||||
}
|
||||
#undef ARG_NODE_GET_RETURN
|
||||
|
||||
/**
|
||||
If the argument is inserted as a pointer, you must use get_ptr ==
|
||||
true, otherwise you must use get_ptr == false, and this will give
|
||||
you the adress of the scalar.
|
||||
|
||||
Observe that if you call XX_get_ptr() on a pointer which is still
|
||||
owned by the arg_pack, you must be careful when freeing the
|
||||
arg_pack, as that will delete the pointer you are using as well.
|
||||
*/
|
||||
|
||||
|
||||
static void * arg_node_get_ptr(const arg_node_type * node , bool get_ptr) {
|
||||
if (get_ptr) {
|
||||
if (node->ctype != CTYPE_VOID_POINTER)
|
||||
util_abort("%s: tried to get pointer from something not a pointer\n",__func__);
|
||||
} else {
|
||||
if (node->ctype == CTYPE_VOID_POINTER)
|
||||
util_abort("%s: tried to get adress to something already a ponter\n",__func__);
|
||||
}
|
||||
return node->buffer;
|
||||
}
|
||||
|
||||
|
||||
static node_ctype arg_node_get_ctype( const arg_node_type * arg_node ) {
|
||||
return arg_node->ctype;
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
/* SET functions. */
|
||||
|
||||
|
||||
#define ARG_NODE_SET(node , value) \
|
||||
arg_node_realloc_buffer(node , sizeof value); \
|
||||
memcpy(node->buffer , &value , sizeof value); \
|
||||
node->destructor = NULL;
|
||||
|
||||
|
||||
static void arg_node_set_int( arg_node_type * node , int value) {
|
||||
ARG_NODE_SET( node , value );
|
||||
node->ctype = CTYPE_INT_VALUE;
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_set_char( arg_node_type * node , char value) {
|
||||
ARG_NODE_SET( node , value );
|
||||
node->ctype = CTYPE_CHAR_VALUE;
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_set_float( arg_node_type * node , float value) {
|
||||
ARG_NODE_SET( node , value );
|
||||
node->ctype = CTYPE_FLOAT_VALUE;
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_set_double( arg_node_type * node , double value) {
|
||||
ARG_NODE_SET( node , value );
|
||||
node->ctype = CTYPE_DOUBLE_VALUE;
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_set_bool( arg_node_type * node , bool value) {
|
||||
ARG_NODE_SET( node , value );
|
||||
node->ctype = CTYPE_BOOL_VALUE;
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_set_size_t( arg_node_type * node , size_t value) {
|
||||
ARG_NODE_SET( node , value );
|
||||
node->ctype = CTYPE_SIZE_T_VALUE;
|
||||
}
|
||||
|
||||
#undef ARG_NODE_SET
|
||||
|
||||
|
||||
static void arg_node_set_ptr(arg_node_type * node , void * ptr , arg_node_copyc_ftype * copyc , arg_node_free_ftype * destructor) {
|
||||
node->ctype = CTYPE_VOID_POINTER;
|
||||
node->destructor = destructor;
|
||||
node->copyc = copyc;
|
||||
if (copyc != NULL)
|
||||
node->buffer = copyc( ptr );
|
||||
else
|
||||
node->buffer = ptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
static void arg_node_clear(arg_node_type * node) {
|
||||
if (node->ctype == CTYPE_VOID_POINTER) {
|
||||
if (node->destructor != NULL)
|
||||
node->destructor( node->buffer );
|
||||
/* When you have cleared - must not reuse the thing. */
|
||||
node->destructor = NULL;
|
||||
node->buffer = NULL;
|
||||
node->copyc = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_free(arg_node_type * node) {
|
||||
arg_node_clear(node);
|
||||
util_safe_free(node->buffer);
|
||||
free(node);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static const char * arg_node_fmt(const arg_node_type *node) {
|
||||
switch (node->ctype) {
|
||||
case(CTYPE_INT_VALUE):
|
||||
return " %d";
|
||||
break;
|
||||
case(CTYPE_DOUBLE_VALUE):
|
||||
return " %lg";
|
||||
break;
|
||||
case(CTYPE_FLOAT_VALUE):
|
||||
return " %g";
|
||||
break;
|
||||
case(CTYPE_BOOL_VALUE):
|
||||
return " %d";
|
||||
break;
|
||||
case(CTYPE_CHAR_VALUE):
|
||||
return " %d";
|
||||
break;
|
||||
case(CTYPE_SIZE_T_VALUE):
|
||||
return " %d";
|
||||
break;
|
||||
default:
|
||||
util_abort("%s: arg_type:%d not recognized for scanning \n",__func__ , node->ctype);
|
||||
return ""; /* Dummy to shut up compiler */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void arg_node_fprintf(const arg_node_type * node , FILE * stream) {
|
||||
switch (node->ctype) {
|
||||
case(CTYPE_INT_VALUE):
|
||||
fprintf(stream , "int:%d",arg_node_get_int(node));
|
||||
break;
|
||||
case(CTYPE_DOUBLE_VALUE):
|
||||
fprintf(stream , "double:%g",arg_node_get_double(node));
|
||||
break;
|
||||
case(CTYPE_VOID_POINTER):
|
||||
fprintf(stream , "pointer:<%p>",arg_node_get_ptr( node , true ));
|
||||
break;
|
||||
case(CTYPE_BOOL_VALUE):
|
||||
fprintf(stream , "bool:%d", arg_node_get_bool( node ));
|
||||
break;
|
||||
default:
|
||||
util_abort("%s: - not implemented for type:%d \n",__func__ , node->ctype);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/* Ending node node functions - starting on functons for the whole pack. */
|
||||
/*****************************************************************/
|
||||
|
||||
UTIL_SAFE_CAST_FUNCTION( arg_pack , ARG_PACK_TYPE_ID)
|
||||
|
||||
static void __arg_pack_assert_index(const arg_pack_type * arg , int iarg) {
|
||||
if (iarg < 0 || iarg >= arg->size)
|
||||
util_abort("%s: arg_pack() object filled with %d arguments - %d invalid argument number - aborting \n",__func__ , arg->size , iarg);
|
||||
}
|
||||
|
||||
|
||||
static void arg_pack_realloc_nodes(arg_pack_type * arg_pack , int new_size) {
|
||||
arg_pack->nodes = util_realloc(arg_pack->nodes , new_size * sizeof * arg_pack->nodes );
|
||||
{
|
||||
int i;
|
||||
for (i = arg_pack->alloc_size; i < new_size; i++)
|
||||
arg_pack->nodes[i] = arg_node_alloc_empty();
|
||||
}
|
||||
arg_pack->alloc_size = new_size;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
The name of this function is QUITE MISLEADING; the function will
|
||||
create a new node, with index @index, and return it possibly
|
||||
freeing the existing with this index. If index == arg_pack->size a
|
||||
new node will be created at the end of the arg_pack; if index >
|
||||
arg_pack->size the function will fail hard.
|
||||
*/
|
||||
static arg_node_type * arg_pack_iget_new_node( arg_pack_type * arg_pack , int index) {
|
||||
if (index < 0 || index > arg_pack->size)
|
||||
util_abort("%s: index:%d invalid. Valid interval: [0,%d) \n",__func__ , index , arg_pack->size);
|
||||
{
|
||||
if (index < arg_pack->size) {
|
||||
arg_node_free( arg_pack->nodes[index] ); /* Free the existing current node. */
|
||||
arg_pack->nodes[index] = arg_node_alloc_empty( ); /* Allocate a new fresh instance. */
|
||||
}
|
||||
|
||||
if (arg_pack->size == arg_pack->alloc_size)
|
||||
arg_pack_realloc_nodes(arg_pack , 1 + arg_pack->alloc_size * 2); /* We have to grow the vector of nodes. */
|
||||
|
||||
if (index == arg_pack->size)
|
||||
arg_pack->size++; /* We are asking for the first element beyond the current length of the vector, i.e. append. */
|
||||
return arg_pack->nodes[index];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static arg_node_type * arg_pack_get_append_node(arg_pack_type * arg_pack) {
|
||||
if (arg_pack->locked) {
|
||||
util_abort("%s: tryng to append to a locked arg_pack instance \n",__func__);
|
||||
return NULL;
|
||||
}
|
||||
{
|
||||
arg_node_type * new_node = arg_pack_iget_new_node( arg_pack , arg_pack->size );
|
||||
return new_node;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void arg_pack_lock(arg_pack_type * arg_pack) {
|
||||
arg_pack->locked = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
arg_pack_type * arg_pack_alloc() {
|
||||
arg_pack_type * arg_pack = util_malloc(sizeof * arg_pack );
|
||||
UTIL_TYPE_ID_INIT( arg_pack , ARG_PACK_TYPE_ID);
|
||||
arg_pack->nodes = NULL;
|
||||
arg_pack->alloc_size = 0;
|
||||
arg_pack->locked = false;
|
||||
arg_pack_realloc_nodes(arg_pack , 4);
|
||||
arg_pack_clear(arg_pack);
|
||||
return arg_pack;
|
||||
}
|
||||
|
||||
|
||||
void arg_pack_free(arg_pack_type * arg_pack) {
|
||||
int i;
|
||||
|
||||
for (i=0; i < arg_pack->alloc_size; i++)
|
||||
arg_node_free( arg_pack->nodes[i] );
|
||||
|
||||
free(arg_pack->nodes);
|
||||
free(arg_pack);
|
||||
}
|
||||
|
||||
|
||||
void arg_pack_free__(void * __arg_pack) {
|
||||
arg_pack_type * arg_pack = arg_pack_safe_cast( __arg_pack );
|
||||
arg_pack_free( arg_pack );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void arg_pack_clear(arg_pack_type * arg_pack) {
|
||||
if (arg_pack->locked)
|
||||
util_abort("%s: arg_pack has been locked - abortng \n",__func__);
|
||||
{
|
||||
int i;
|
||||
for ( i=0; i < arg_pack->size; i++)
|
||||
arg_node_clear(arg_pack->nodes[i]);
|
||||
arg_pack->size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************/
|
||||
/* Access functions:
|
||||
|
||||
1. Append
|
||||
2. iget
|
||||
3. iset (can NOT create holes in the vector)
|
||||
|
||||
******************************************************************/
|
||||
|
||||
#define APPEND_TYPED(type) \
|
||||
void arg_pack_append_ ## type (arg_pack_type *pack , type value) { \
|
||||
arg_node_type * node = arg_pack_get_append_node( pack ); \
|
||||
arg_node_set_ ## type(node , value); \
|
||||
}
|
||||
|
||||
|
||||
#define ISET_TYPED(type)\
|
||||
void arg_pack_iset_ ## type(arg_pack_type * pack, int index, type value) { \
|
||||
arg_node_type * node = arg_pack_iget_new_node( pack , index); \
|
||||
arg_node_set_ ## type(node , value); \
|
||||
}
|
||||
|
||||
|
||||
#define IGET_TYPED(type)\
|
||||
type arg_pack_iget_ ## type(const arg_pack_type * pack, int index) { \
|
||||
__arg_pack_assert_index( pack , index); \
|
||||
{ \
|
||||
arg_node_type * node = pack->nodes[index]; \
|
||||
return arg_node_get_ ## type ( node ); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
APPEND_TYPED(int);
|
||||
APPEND_TYPED(bool);
|
||||
APPEND_TYPED(float);
|
||||
APPEND_TYPED(double);
|
||||
APPEND_TYPED(char);
|
||||
APPEND_TYPED(size_t);
|
||||
|
||||
IGET_TYPED(int);
|
||||
IGET_TYPED(bool);
|
||||
IGET_TYPED(float);
|
||||
IGET_TYPED(double);
|
||||
IGET_TYPED(char);
|
||||
IGET_TYPED(size_t);
|
||||
|
||||
ISET_TYPED(int);
|
||||
ISET_TYPED(bool);
|
||||
ISET_TYPED(float);
|
||||
ISET_TYPED(double);
|
||||
ISET_TYPED(char);
|
||||
ISET_TYPED(size_t);
|
||||
|
||||
#undef APPEND_TYPED
|
||||
#undef IGET_TYPED
|
||||
#undef ISET_TYPED
|
||||
|
||||
|
||||
void * arg_pack_iget_ptr(const arg_pack_type * arg , int iarg) {
|
||||
__arg_pack_assert_index(arg , iarg);
|
||||
return arg_node_get_ptr(arg->nodes[iarg] , true);
|
||||
}
|
||||
|
||||
|
||||
void * arg_pack_iget_adress(const arg_pack_type * arg , int iarg) {
|
||||
__arg_pack_assert_index(arg , iarg);
|
||||
return arg_node_get_ptr(arg->nodes[iarg] , false);
|
||||
}
|
||||
|
||||
|
||||
node_ctype arg_pack_iget_ctype(const arg_pack_type * arg_pack ,int index) {
|
||||
__arg_pack_assert_index(arg_pack , index);
|
||||
return arg_node_get_ctype( arg_pack->nodes[index] );
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
void arg_pack_iset_copy(arg_pack_type * arg_pack , int index , void * ptr, arg_node_copyc_ftype * copyc , arg_node_free_ftype * freef) {
|
||||
arg_node_type * node = arg_pack_iget_new_node( arg_pack , index );
|
||||
arg_node_set_ptr(node , ptr , copyc , freef);
|
||||
}
|
||||
|
||||
|
||||
void arg_pack_iset_ptr(arg_pack_type * arg_pack, int index , const void * ptr) {
|
||||
arg_pack_iset_copy(arg_pack , index , ptr , NULL , NULL);
|
||||
}
|
||||
|
||||
void arg_pack_iset_owned_ptr(arg_pack_type * arg_pack, int index , void * ptr, arg_node_free_ftype * freef) {
|
||||
arg_pack_iset_copy(arg_pack , index , ptr , NULL , freef );
|
||||
}
|
||||
|
||||
|
||||
void arg_pack_append_copy(arg_pack_type * arg_pack , void * ptr, arg_node_copyc_ftype * copyc , arg_node_free_ftype * freef) {
|
||||
arg_pack_iset_copy( arg_pack , arg_pack->size , ptr , copyc , freef);
|
||||
}
|
||||
|
||||
|
||||
void arg_pack_append_ptr(arg_pack_type * arg_pack, const void * ptr) {
|
||||
arg_pack_iset_ptr( arg_pack , arg_pack->size , ptr );
|
||||
}
|
||||
|
||||
void arg_pack_append_owned_ptr(arg_pack_type * arg_pack, void * ptr, arg_node_free_ftype * freef) {
|
||||
arg_pack_iset_owned_ptr( arg_pack , arg_pack->size , ptr , freef);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************/
|
||||
/* Functions for formatted reading/writing of arg_pack instances. */
|
||||
|
||||
|
||||
|
||||
void arg_pack_fscanf(arg_pack_type * arg , FILE * stream) {
|
||||
int scan_count = 0;
|
||||
int iarg;
|
||||
char * fmt = NULL;
|
||||
for (iarg = 0; iarg < arg->size; iarg++) {
|
||||
arg_node_type * node = arg->nodes[iarg];
|
||||
fmt = util_strcat_realloc(fmt , arg_node_fmt(node));
|
||||
}
|
||||
|
||||
switch(arg->size) {
|
||||
case(0):
|
||||
break;
|
||||
case(1):
|
||||
{
|
||||
void *arg0;
|
||||
arg0 = arg_pack_iget_adress(arg , 0);
|
||||
scan_count = fscanf(stream , fmt , arg0);
|
||||
break;
|
||||
}
|
||||
case(2):
|
||||
{
|
||||
void *arg0, *arg1;
|
||||
arg0 = arg_pack_iget_adress(arg , 0);
|
||||
arg1 = arg_pack_iget_adress(arg , 1);
|
||||
|
||||
scan_count = fscanf(stream , fmt , arg0 , arg1);
|
||||
break;
|
||||
}
|
||||
case(3):
|
||||
{
|
||||
void *arg0, *arg1 , *arg2;
|
||||
arg0 = arg_pack_iget_adress(arg , 0);
|
||||
arg1 = arg_pack_iget_adress(arg , 1);
|
||||
arg2 = arg_pack_iget_adress(arg , 2);
|
||||
|
||||
scan_count = fscanf(stream , fmt , arg0 , arg1 , arg2);
|
||||
break;
|
||||
}
|
||||
case(4):
|
||||
{
|
||||
void *arg0, *arg1 , *arg2 , *arg3;
|
||||
arg0 = arg_pack_iget_adress(arg , 0);
|
||||
arg1 = arg_pack_iget_adress(arg , 1);
|
||||
arg2 = arg_pack_iget_adress(arg , 2);
|
||||
arg3 = arg_pack_iget_adress(arg , 3);
|
||||
|
||||
scan_count = fscanf(stream , fmt , arg0 , arg1 , arg2 , arg3);
|
||||
break;
|
||||
}
|
||||
case(5):
|
||||
{
|
||||
void *arg0, *arg1 , *arg2 , *arg3, *arg4;
|
||||
arg0 = arg_pack_iget_adress(arg , 0);
|
||||
arg1 = arg_pack_iget_adress(arg , 1);
|
||||
arg2 = arg_pack_iget_adress(arg , 2);
|
||||
arg3 = arg_pack_iget_adress(arg , 3);
|
||||
arg4 = arg_pack_iget_adress(arg , 4);
|
||||
|
||||
scan_count = fscanf(stream , fmt , arg0 , arg1 , arg2 , arg3 , arg4);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
util_abort("%s: sorry %s not allocated for %d arguments - pathetic ehhh?? \n",__func__ , __func__ , arg->size);
|
||||
}
|
||||
|
||||
if (scan_count != arg->size)
|
||||
util_abort("%s: wanted %d arguments - only found: %d \n", __func__ , arg->size , scan_count);
|
||||
|
||||
free(fmt);
|
||||
}
|
||||
|
||||
|
||||
void arg_pack_fprintf(const arg_pack_type * arg_pack , FILE * stream) {
|
||||
int iarg;
|
||||
fprintf(stream," [");
|
||||
for (iarg = 0; iarg < arg_pack->size; iarg++) {
|
||||
arg_node_type * node = arg_pack->nodes[iarg];
|
||||
arg_node_fprintf(node , stream);
|
||||
if (iarg < (arg_pack->size - 1))
|
||||
fprintf(stream,", ");
|
||||
}
|
||||
fprintf(stream, "]\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
91
ThirdParty/Ert/devel/libert_util/src/arg_pack.h
vendored
Normal file
91
ThirdParty/Ert/devel/libert_util/src/arg_pack.h
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'arg_pack.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __ARG_PACK_H__
|
||||
#define __ARG_PACK_H__
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <node_ctype.h>
|
||||
#include <type_macros.h>
|
||||
|
||||
typedef struct arg_pack_struct arg_pack_type;
|
||||
typedef void (arg_node_free_ftype) (void *);
|
||||
typedef void * (arg_node_copyc_ftype) (void *);
|
||||
|
||||
arg_pack_type * arg_pack_alloc();
|
||||
UTIL_SAFE_CAST_HEADER( arg_pack );
|
||||
|
||||
void arg_pack_free(arg_pack_type * );
|
||||
void arg_pack_free__(void *);
|
||||
void arg_pack_clear(arg_pack_type *);
|
||||
void arg_pack_lock(arg_pack_type *);
|
||||
void arg_pack_fscanf(arg_pack_type * arg , FILE * stream);
|
||||
void arg_pack_fprintf(const arg_pack_type * , FILE * );
|
||||
|
||||
void arg_pack_append_ptr(arg_pack_type * , const void *);
|
||||
void arg_pack_append_owned_ptr(arg_pack_type * , void * , arg_node_free_ftype *);
|
||||
void arg_pack_append_copy(arg_pack_type * , void * , arg_node_copyc_ftype * , arg_node_free_ftype *);
|
||||
|
||||
void arg_pack_iset_copy(arg_pack_type * arg_pack , int index , void * ptr, arg_node_copyc_ftype * copyc , arg_node_free_ftype * freef);
|
||||
void arg_pack_iset_ptr(arg_pack_type * arg_pack, int index , const void * ptr);
|
||||
void arg_pack_iset_owned_ptr(arg_pack_type * arg_pack, int index , void * ptr, arg_node_free_ftype * freef);
|
||||
|
||||
void * arg_pack_iget_ptr(const arg_pack_type * , int);
|
||||
void * arg_pack_iget_adress(const arg_pack_type * , int);
|
||||
node_ctype arg_pack_iget_ctype(const arg_pack_type * arg_pack ,int index);
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
#define APPEND_TYPED_HEADER(type) void arg_pack_append_ ## type (arg_pack_type * , type);
|
||||
#define IGET_TYPED_HEADER(type) type arg_pack_iget_ ## type( const arg_pack_type * , int );
|
||||
#define ISET_TYPED_HEADER(type) void arg_pack_iset_ ## type( arg_pack_type * , int , type value);
|
||||
|
||||
APPEND_TYPED_HEADER(int)
|
||||
APPEND_TYPED_HEADER(bool)
|
||||
APPEND_TYPED_HEADER(char)
|
||||
APPEND_TYPED_HEADER(float)
|
||||
APPEND_TYPED_HEADER(double)
|
||||
APPEND_TYPED_HEADER(size_t)
|
||||
|
||||
IGET_TYPED_HEADER(int)
|
||||
IGET_TYPED_HEADER(bool)
|
||||
IGET_TYPED_HEADER(char)
|
||||
IGET_TYPED_HEADER(float)
|
||||
IGET_TYPED_HEADER(double)
|
||||
IGET_TYPED_HEADER(size_t)
|
||||
|
||||
ISET_TYPED_HEADER(int)
|
||||
ISET_TYPED_HEADER(bool)
|
||||
ISET_TYPED_HEADER(char)
|
||||
ISET_TYPED_HEADER(float)
|
||||
ISET_TYPED_HEADER(double)
|
||||
ISET_TYPED_HEADER(size_t)
|
||||
|
||||
#undef APPEND_TYPED_HEADER
|
||||
#undef GET_TYPED_HEADER
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
183
ThirdParty/Ert/devel/libert_util/src/atomic.h
vendored
Normal file
183
ThirdParty/Ert/devel/libert_util/src/atomic.h
vendored
Normal file
@ -0,0 +1,183 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'atomic.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
/*
|
||||
This whole file was something I found on the internet - it was
|
||||
posted as beeing in the public domain. The essential functions
|
||||
__sync_add_and_fetch() and do on are (as I understand it) built in
|
||||
gcc functions. Only available in reasonably new gcc versions
|
||||
(4.1???).
|
||||
*/
|
||||
|
||||
#ifndef _ATOMIC_H
|
||||
#define _ATOMIC_H
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Atomic type.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
volatile int counter;
|
||||
} atomic_t;
|
||||
|
||||
#define ATOMIC_INIT(i) { (i) }
|
||||
|
||||
/**
|
||||
* Read atomic variable
|
||||
* @param v pointer of type atomic_t
|
||||
*
|
||||
* Atomically reads the value of @v.
|
||||
*/
|
||||
#define atomic_read(v) ((v)->counter)
|
||||
|
||||
/**
|
||||
* Set atomic variable
|
||||
* @param v pointer of type atomic_t
|
||||
* @param i required value
|
||||
*/
|
||||
#define atomic_set(v,i) (((v)->counter) = (i))
|
||||
|
||||
/**
|
||||
* Add to the atomic variable
|
||||
* @param i integer value to add
|
||||
* @param v pointer of type atomic_t
|
||||
*/
|
||||
static inline void atomic_add( int i, atomic_t *v )
|
||||
{
|
||||
(void)__sync_add_and_fetch(&v->counter, i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract the atomic variable
|
||||
* @param i integer value to subtract
|
||||
* @param v pointer of type atomic_t
|
||||
*
|
||||
* Atomically subtracts @i from @v.
|
||||
*/
|
||||
static inline void atomic_sub( int i, atomic_t *v )
|
||||
{
|
||||
(void)__sync_sub_and_fetch(&v->counter, i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract value from variable and test result
|
||||
* @param i integer value to subtract
|
||||
* @param v pointer of type atomic_t
|
||||
*
|
||||
* Atomically subtracts @i from @v and returns
|
||||
* true if the result is zero, or false for all
|
||||
* other cases.
|
||||
*/
|
||||
static inline int atomic_sub_and_test( int i, atomic_t *v )
|
||||
{
|
||||
return !(__sync_sub_and_fetch(&v->counter, i));
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment atomic variable
|
||||
* @param v pointer of type atomic_t
|
||||
*
|
||||
* Atomically increments @v by 1.
|
||||
*/
|
||||
static inline void atomic_inc( atomic_t *v )
|
||||
{
|
||||
(void)__sync_fetch_and_add(&v->counter, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief decrement atomic variable
|
||||
* @param v: pointer of type atomic_t
|
||||
*
|
||||
* Atomically decrements @v by 1. Note that the guaranteed
|
||||
* useful range of an atomic_t is only 24 bits.
|
||||
*/
|
||||
static inline void atomic_dec( atomic_t *v )
|
||||
{
|
||||
(void)__sync_fetch_and_sub(&v->counter, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Decrement and test
|
||||
* @param v pointer of type atomic_t
|
||||
*
|
||||
* Atomically decrements @v by 1 and
|
||||
* returns true if the result is 0, or false for all other
|
||||
* cases.
|
||||
*/
|
||||
static inline int atomic_dec_and_test( atomic_t *v )
|
||||
{
|
||||
return !(__sync_sub_and_fetch(&v->counter, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Increment and test
|
||||
* @param v pointer of type atomic_t
|
||||
*
|
||||
* Atomically increments @v by 1
|
||||
* and returns true if the result is zero, or false for all
|
||||
* other cases.
|
||||
*/
|
||||
static inline int atomic_inc_and_test( atomic_t *v )
|
||||
{
|
||||
return !(__sync_add_and_fetch(&v->counter, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief add and test if negative
|
||||
* @param v pointer of type atomic_t
|
||||
* @param i integer value to add
|
||||
*
|
||||
* Atomically adds @i to @v and returns true
|
||||
* if the result is negative, or false when
|
||||
* result is greater than or equal to zero.
|
||||
*/
|
||||
static inline int atomic_add_negative( int i, atomic_t *v )
|
||||
{
|
||||
return (__sync_add_and_fetch(&v->counter, i) < 0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*****************************************************************/
|
||||
/*****************************************************************/
|
||||
/*****************************************************************/
|
||||
|
||||
///* ALternative implementations: */
|
||||
////Pretty straight forward isn't it? It could be even more powerful and simpler if you don't need precise compatibility with atomic.h. For example, atomic_add could easily return the result values:
|
||||
//static inline int atomic_add( int i, atomic_t *v )
|
||||
//{
|
||||
// return __sync_add_and_fetch(&v->counter, i);
|
||||
//}
|
||||
//
|
||||
////As a second example, consider a compare and swap operation, frequently used in lock-free algorithms. Once again, it's trivially:
|
||||
///**
|
||||
// * @brief compare and swap
|
||||
// * @param v pointer of type atomic_t
|
||||
// *
|
||||
// * If the current value of @b v is @b oldval,
|
||||
// * then write @b newval into @b v. Returns #TRUE if
|
||||
// * the comparison is successful and @b newval was
|
||||
// * written.
|
||||
// */
|
||||
//static inline int atomic_cas( atomic_t *v, int oldval, int newval )
|
||||
//{
|
||||
// return __sync_bool_compare_and_swap(&v->counter, oldval, newval);
|
||||
//}
|
||||
|
1789
ThirdParty/Ert/devel/libert_util/src/block_fs.c
vendored
Normal file
1789
ThirdParty/Ert/devel/libert_util/src/block_fs.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
67
ThirdParty/Ert/devel/libert_util/src/block_fs.h
vendored
Normal file
67
ThirdParty/Ert/devel/libert_util/src/block_fs.h
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'block_fs.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __BLOCK_FS__
|
||||
#define __BLOCK_FS__
|
||||
#include <buffer.h>
|
||||
#include <vector.h>
|
||||
#include <type_macros.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct block_fs_struct block_fs_type;
|
||||
typedef struct user_file_node_struct user_file_node_type;
|
||||
|
||||
typedef enum {
|
||||
NO_SORT = 0,
|
||||
STRING_SORT = 1,
|
||||
OFFSET_SORT = 2
|
||||
} block_fs_sort_type;
|
||||
|
||||
size_t block_fs_get_cache_usage( const block_fs_type * block_fs );
|
||||
double block_fs_get_fragmentation( const block_fs_type * block_fs );
|
||||
bool block_fs_rotate( block_fs_type * block_fs , double fragmentation_limit);
|
||||
void block_fs_fsync( block_fs_type * block_fs );
|
||||
bool block_fs_is_mount( const char * mount_file );
|
||||
block_fs_type * block_fs_mount( const char * mount_file , int block_size , int max_cache_size , float fragmentation_limit , int fsync_interval , bool preload , bool read_only);
|
||||
void block_fs_close( block_fs_type * block_fs , bool unlink_empty);
|
||||
void block_fs_fwrite_file(block_fs_type * block_fs , const char * filename , const void * ptr , size_t byte_size);
|
||||
void block_fs_fwrite_buffer(block_fs_type * block_fs , const char * filename , const buffer_type * buffer);
|
||||
void block_fs_fread_file( block_fs_type * block_fs , const char * filename , void * ptr);
|
||||
int block_fs_get_filesize( block_fs_type * block_fs , const char * filename);
|
||||
void block_fs_fread_realloc_buffer( block_fs_type * block_fs , const char * filename , buffer_type * buffer);
|
||||
void block_fs_sync( block_fs_type * block_fs );
|
||||
void block_fs_unlink_file( block_fs_type * block_fs , const char * filename);
|
||||
bool block_fs_has_file( block_fs_type * block_fs , const char * filename);
|
||||
vector_type * block_fs_alloc_filelist( block_fs_type * block_fs , const char * pattern , block_fs_sort_type sort_mode , bool include_free_nodes );
|
||||
void block_fs_defrag( block_fs_type * block_fs );
|
||||
|
||||
long int user_file_node_get_node_offset( const user_file_node_type * user_file_node );
|
||||
long int user_file_node_get_data_offset( const user_file_node_type * user_file_node );
|
||||
int user_file_node_get_node_size( const user_file_node_type * user_file_node );
|
||||
int user_file_node_get_data_size( const user_file_node_type * user_file_node );
|
||||
bool user_file_node_in_use( const user_file_node_type * user_file_node );
|
||||
const char * user_file_node_get_filename( const user_file_node_type * user_file_node );
|
||||
|
||||
UTIL_IS_INSTANCE_HEADER( block_fs );
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
840
ThirdParty/Ert/devel/libert_util/src/buffer.c
vendored
Normal file
840
ThirdParty/Ert/devel/libert_util/src/buffer.c
vendored
Normal file
@ -0,0 +1,840 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'buffer.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <util.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <buffer.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <ssize_t.h>
|
||||
/**
|
||||
This function implements a small buffer type. The whole point of
|
||||
this type is that it should work (more-or-less) as a drop in
|
||||
replacement of FILE * instances (at least for unformatted
|
||||
read/write).
|
||||
|
||||
I.e. instead of
|
||||
|
||||
int * p = util_malloc( sizeof * p * 10 );
|
||||
fread( p , sizeof * p , 10 , stream);
|
||||
|
||||
To read ten integers from a FILE * instance we should be able to
|
||||
call
|
||||
|
||||
int * p = util_malloc( sizeof * p * 10 );
|
||||
buffer_fread( buffer , p , sizeof * p , 10);
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#define BUFFER_TYPE_ID 661043
|
||||
|
||||
|
||||
struct buffer_struct {
|
||||
size_t __id;
|
||||
char * data; /* The actual storage. */
|
||||
size_t alloc_size; /* The total byte size of the buffer. */
|
||||
size_t content_size; /* The extent of initialized data in the buffer - i.e. the meaningful content in the buffer. */
|
||||
size_t pos; /* The current byte position in the buffer.*/
|
||||
};
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
abort_on_error == true:
|
||||
-----------------------
|
||||
The function abort with util_abort() if the allocation fails.
|
||||
|
||||
|
||||
abort_on_error == false:
|
||||
------------------------
|
||||
The function will SILENTLY fail if you ask for more memory than
|
||||
the system can provide.
|
||||
*/
|
||||
|
||||
|
||||
static void buffer_resize__(buffer_type * buffer , size_t new_size, bool abort_on_error) {
|
||||
if (abort_on_error) {
|
||||
buffer->data = util_realloc(buffer->data , new_size );
|
||||
buffer->alloc_size = new_size;
|
||||
} else {
|
||||
void * tmp = realloc(buffer->data , new_size);
|
||||
if (tmp != NULL) {
|
||||
buffer->data = tmp;
|
||||
buffer->alloc_size = new_size;
|
||||
}
|
||||
}
|
||||
buffer->content_size = util_size_t_min( buffer->content_size , new_size ); /* If the buffer has actually shrinked. */
|
||||
buffer->pos = util_size_t_min( buffer->pos , new_size); /* If the buffer has actually shrinked. */
|
||||
}
|
||||
|
||||
|
||||
static buffer_type * buffer_alloc_empty( ) {
|
||||
buffer_type * buffer = util_malloc( sizeof * buffer );
|
||||
buffer->__id = BUFFER_TYPE_ID;
|
||||
buffer->data = NULL;
|
||||
|
||||
buffer->alloc_size = 0;
|
||||
buffer->content_size = 0;
|
||||
buffer->pos = 0;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
buffer_type * buffer_alloc( size_t buffer_size ) {
|
||||
buffer_type * buffer = buffer_alloc_empty();
|
||||
buffer_resize__( buffer , buffer_size , true);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
Will resize the buffer storage to exactly fit the amount of content.
|
||||
*/
|
||||
void buffer_shrink_to_fit( buffer_type * buffer ) {
|
||||
buffer_resize__( buffer , buffer->content_size , true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This function will allocate a buffer instance based on the input
|
||||
data. Observe that the buffer will 'steal' the input data pointer,
|
||||
in the process the data pointer might very well be realloced()
|
||||
leaving the original pointer invalid.
|
||||
|
||||
All the content of the input data pointer will be assumed to be
|
||||
valid, i.e. the fields content_size and pos will be set to the
|
||||
value @buffer_size.
|
||||
|
||||
When calling buffer_free() at a later stage the hijacked data will
|
||||
also be freed.
|
||||
*/
|
||||
|
||||
buffer_type * buffer_alloc_private_wrapper(void * data , size_t buffer_size ) {
|
||||
buffer_type * buffer = buffer_alloc_empty();
|
||||
|
||||
buffer->data = data; /* We have stolen the data pointer. */
|
||||
buffer->content_size = buffer_size;
|
||||
buffer->pos = buffer_size;
|
||||
buffer->alloc_size = buffer_size;
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This function will free the buffer data structure, but NOT the
|
||||
actual storage. Can typically be used when some other pointer has
|
||||
taken posession of the buffer content:
|
||||
|
||||
buffer_type * buffer = buffer_alloc( );
|
||||
|
||||
// Do thing with the buffer
|
||||
|
||||
{
|
||||
void * thief_ptr = buffer_get_data( buffer );
|
||||
buffer_free_container( buffer );
|
||||
|
||||
// Do stuff with thief_ptr
|
||||
// ....
|
||||
|
||||
free( thief_ptr);
|
||||
}
|
||||
*/
|
||||
|
||||
void buffer_free_container( buffer_type * buffer ) {
|
||||
free( buffer );
|
||||
}
|
||||
|
||||
|
||||
void buffer_free( buffer_type * buffer) {
|
||||
free( buffer->data );
|
||||
buffer_free_container( buffer );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This will reposition all the pointers to the start of the buffer.
|
||||
The actual data of the buffer will not be touched.
|
||||
*/
|
||||
void buffer_clear( buffer_type * buffer ) {
|
||||
buffer->content_size = 0;
|
||||
buffer->pos = 0;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/**
|
||||
Observe that it is the functions with _safe_ in the name which
|
||||
most closely mimicks the behaviour of fread(), and fwrite() -
|
||||
these functions will *NOT* abort if the buffer is to small,
|
||||
instead they will just return the number of items read/written,
|
||||
and it is the responsability of the calling scope to check the
|
||||
return values.
|
||||
|
||||
The functions buffer_fread() and buffer_fwrite() will abort if
|
||||
read/write to buffer failed.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
static size_t buffer_fread__(buffer_type * buffer , void * target_ptr , size_t item_size , size_t items, bool abort_on_error) {
|
||||
size_t remaining_size = buffer->content_size - buffer->pos;
|
||||
size_t remaining_items = remaining_size / item_size;
|
||||
size_t read_items = util_size_t_min( items , remaining_items );
|
||||
size_t read_bytes = read_items * item_size;
|
||||
|
||||
memcpy( target_ptr , &buffer->data[buffer->pos] , read_bytes );
|
||||
buffer->pos += read_bytes;
|
||||
|
||||
if (read_items < items) {
|
||||
/* The buffer was not large enough - what to do now???? */
|
||||
if (abort_on_error)
|
||||
util_abort("%s: tried to read beyond the length of the buffer: Wanted:%ld Size:%ld \n",__func__ , items , read_items);
|
||||
else
|
||||
/* OK we emulate fread() behaviour - setting errno to EOVERFLOW*/
|
||||
errno = ENOMEM;//EOVERFLOW;
|
||||
}
|
||||
|
||||
return read_items;
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_safe_fread(buffer_type * buffer , void * target_ptr , size_t item_size , size_t items) {
|
||||
return buffer_fread__(buffer , target_ptr , item_size , items , false);
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_fread(buffer_type * buffer , void * target_ptr , size_t item_size , size_t items) {
|
||||
return buffer_fread__(buffer , target_ptr , item_size , items , true);
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
static size_t buffer_fwrite__(buffer_type * buffer , const void * src_ptr , size_t item_size , size_t items, bool abort_on_error) {
|
||||
size_t remaining_size = buffer->alloc_size - buffer->pos;
|
||||
size_t target_size = item_size * items;
|
||||
|
||||
if (target_size > remaining_size) {
|
||||
buffer_resize__(buffer , buffer->pos + 2 * (item_size * items) , abort_on_error);
|
||||
/**
|
||||
OK - now we have the buffer size we are going to get.
|
||||
*/
|
||||
remaining_size = buffer->alloc_size - buffer->pos;
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
size_t remaining_items = remaining_size / item_size;
|
||||
size_t write_items = util_size_t_min( items , remaining_items );
|
||||
size_t write_bytes = write_items * item_size;
|
||||
|
||||
memcpy( &buffer->data[buffer->pos] , src_ptr , write_bytes );
|
||||
buffer->pos += write_bytes;
|
||||
|
||||
if (write_items < items) {
|
||||
/* The buffer was not large enough - what to do now???? */
|
||||
if (abort_on_error)
|
||||
util_abort("%s: failed to write %d elements to the buffer \n",__func__ , items); /* This code is never executed - abort is in resize__(); */
|
||||
else
|
||||
/* OK we emulate fwrite() behaviour - setting errno to ENOMEM */
|
||||
errno = ENOMEM;
|
||||
}
|
||||
buffer->content_size = util_size_t_max(buffer->content_size , buffer->pos);
|
||||
return write_items;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_safe_fwrite(buffer_type * buffer , const void * src_ptr , size_t item_size , size_t items) {
|
||||
return buffer_fwrite__(buffer , src_ptr , item_size , items , false);
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_fwrite(buffer_type * buffer , const void * src_ptr , size_t item_size , size_t items) {
|
||||
return buffer_fwrite__(buffer , src_ptr , item_size , items , true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/* Various (slighly) higher level functions */
|
||||
|
||||
|
||||
void buffer_rewind(buffer_type * buffer ) {
|
||||
buffer_fseek( buffer , 0 , SEEK_SET);
|
||||
}
|
||||
|
||||
|
||||
void buffer_fseek(buffer_type * buffer , ssize_t offset , int whence) {
|
||||
ssize_t new_pos = 0;
|
||||
|
||||
if (whence == SEEK_SET)
|
||||
new_pos = offset;
|
||||
else if (whence == SEEK_CUR)
|
||||
new_pos = buffer->pos + offset;
|
||||
else if (whence == SEEK_END)
|
||||
new_pos = buffer->content_size + offset;
|
||||
else
|
||||
util_abort("%s: unrecognized whence indicator - aborting \n",__func__);
|
||||
|
||||
/**
|
||||
Observe that we can seek to the very end of the buffer. I.e. for
|
||||
a buffer with content_size == 20 we can seek to position 20.
|
||||
*/
|
||||
|
||||
if ((new_pos >= 0) && (new_pos <= buffer->content_size))
|
||||
buffer->pos = new_pos;
|
||||
else
|
||||
util_abort("%s: tried to seek to position:%ld - outside of bounds: [0,%d) \n",__func__ , new_pos , buffer->content_size);
|
||||
}
|
||||
|
||||
|
||||
void buffer_fskip(buffer_type * buffer, ssize_t offset) {
|
||||
buffer_fseek( buffer , offset , SEEK_CUR );
|
||||
}
|
||||
|
||||
|
||||
int buffer_fread_int(buffer_type * buffer) {
|
||||
int value;
|
||||
buffer_fread(buffer , &value , sizeof value , 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
bool buffer_fread_bool(buffer_type * buffer) {
|
||||
bool value;
|
||||
buffer_fread(buffer , &value , sizeof value , 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
long int buffer_fread_long(buffer_type * buffer) {
|
||||
long value;
|
||||
buffer_fread(buffer , &value , sizeof value , 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
int buffer_fgetc( buffer_type * buffer ) {
|
||||
if (buffer->pos == buffer->content_size)
|
||||
return EOF;
|
||||
else {
|
||||
unsigned char byte;
|
||||
buffer_fread( buffer , &byte , sizeof byte , 1 );
|
||||
return byte;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
This function writes all the elements in the string __NOT__
|
||||
including the terminating \0 character into the buffer. This should
|
||||
not be confused with buffer_fwrite_string() function which both
|
||||
prepends the string with an integer length specifier and also
|
||||
includes the terminating \0.
|
||||
*/
|
||||
|
||||
void buffer_fwrite_char_ptr(buffer_type * buffer , const char * string_ptr ) {
|
||||
buffer_fwrite(buffer , string_ptr , sizeof * string_ptr , strlen( string_ptr ));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Will append a \0 to the buffer; before appending the last character
|
||||
of the buffer will be checked, and no new \0 will be added if the
|
||||
buffer is already \0 terminated.
|
||||
*/
|
||||
void buffer_terminate_char_ptr( buffer_type * buffer ) {
|
||||
if (buffer->data[ buffer->content_size - 1] != '\0')
|
||||
buffer_fwrite_char( buffer , '\0');
|
||||
}
|
||||
|
||||
|
||||
void buffer_fwrite_int(buffer_type * buffer , int value) {
|
||||
buffer_fwrite(buffer , &value , sizeof value , 1);
|
||||
}
|
||||
|
||||
|
||||
void buffer_fwrite_bool(buffer_type * buffer , bool value) {
|
||||
buffer_fwrite(buffer , &value , sizeof value , 1);
|
||||
}
|
||||
|
||||
|
||||
void buffer_fskip_time_t(buffer_type * buffer) {
|
||||
buffer_fseek( buffer , sizeof(time_t) , SEEK_CUR );
|
||||
}
|
||||
|
||||
|
||||
void buffer_fskip_int(buffer_type * buffer) {
|
||||
buffer_fseek( buffer , sizeof( int ) , SEEK_CUR );
|
||||
}
|
||||
|
||||
void buffer_fskip_long(buffer_type * buffer) {
|
||||
buffer_fseek( buffer , sizeof( long ) , SEEK_CUR );
|
||||
}
|
||||
|
||||
void buffer_fskip_bool(buffer_type * buffer) {
|
||||
buffer_fseek( buffer , sizeof( bool ) , SEEK_CUR );
|
||||
}
|
||||
|
||||
|
||||
time_t buffer_fread_time_t(buffer_type * buffer) {
|
||||
time_t value;
|
||||
buffer_fread(buffer , &value , sizeof value , 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
void buffer_fwrite_time_t(buffer_type * buffer , time_t value) {
|
||||
buffer_fwrite(buffer , &value , sizeof value , 1);
|
||||
}
|
||||
|
||||
|
||||
char buffer_fread_char(buffer_type * buffer) {
|
||||
char value;
|
||||
buffer_fread(buffer , &value , sizeof value , 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
void buffer_fwrite_char(buffer_type * buffer , char value) {
|
||||
buffer_fwrite(buffer , &value , sizeof value , 1);
|
||||
}
|
||||
|
||||
|
||||
double buffer_fread_double(buffer_type * buffer) {
|
||||
double value;
|
||||
buffer_fread(buffer , &value , sizeof value , 1);
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
void buffer_fwrite_double(buffer_type * buffer , double value) {
|
||||
buffer_fwrite(buffer , &value , sizeof value , 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Storing strings:
|
||||
----------------
|
||||
|
||||
When storing a string (\0 terminated char pointer) what is actually
|
||||
written to the buffer is
|
||||
|
||||
1. The length of the string - as returned from strlen().
|
||||
2. The string content INCLUDING the terminating \0.
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
This function will return a pointer to the current position in the
|
||||
buffer, and advance the buffer position forward until a \0
|
||||
terminater is found. If \0 is not found the thing will abort().
|
||||
|
||||
Observe that the return value will point straight into the buffer,
|
||||
this is highly volatile memory, and in general it will be safer to
|
||||
use buffer_fread_alloc_string() to get a copy of the string.
|
||||
*/
|
||||
|
||||
const char * buffer_fread_string(buffer_type * buffer) {
|
||||
int string_length = buffer_fread_int( buffer );
|
||||
char * string_ptr = &buffer->data[buffer->pos];
|
||||
char c;
|
||||
buffer_fskip( buffer , string_length );
|
||||
c = buffer_fread_char( buffer );
|
||||
if (c != '\0')
|
||||
util_abort("%s: internal error - malformed string representation in buffer \n",__func__);
|
||||
return string_ptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char * buffer_fread_alloc_string(buffer_type * buffer) {
|
||||
return util_alloc_string_copy( buffer_fread_string( buffer ));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Observe that this function writes a leading integer string length.
|
||||
*/
|
||||
void buffer_fwrite_string(buffer_type * buffer , const char * string) {
|
||||
buffer_fwrite_int( buffer , strlen( string )); /* Writing the length of the string */
|
||||
buffer_fwrite(buffer , string , 1 , strlen( string ) + 1); /* Writing the string content ** WITH ** the terminating \0 */
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/*****************************************************************/
|
||||
|
||||
size_t buffer_get_offset(const buffer_type * buffer) {
|
||||
return buffer->pos;
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_get_size(const buffer_type * buffer) {
|
||||
return buffer->content_size;
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_get_string_size( const buffer_type * buffer ) {
|
||||
return strlen( buffer->data );
|
||||
}
|
||||
|
||||
size_t buffer_get_alloc_size(const buffer_type * buffer) {
|
||||
return buffer->alloc_size;
|
||||
}
|
||||
|
||||
|
||||
size_t buffer_get_remaining_size(const buffer_type * buffer) {
|
||||
return buffer->content_size - buffer->pos;
|
||||
}
|
||||
|
||||
/**
|
||||
Returns a pointer to the internal storage of the buffer. Observe
|
||||
that this storage is volatile, and the return value from this
|
||||
function should not be kept around; alternatively you can use
|
||||
buffer_alloc_data_copy().
|
||||
*/
|
||||
void * buffer_get_data(const buffer_type * buffer) {
|
||||
return buffer->data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Returns a copy of the initialized (i.e. buffer->content_size)
|
||||
buffer content.
|
||||
*/
|
||||
void * buffer_alloc_data_copy(const buffer_type * buffer) {
|
||||
return util_alloc_copy(buffer->data , buffer->content_size );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This function will shift parts of the buffer data, either creating
|
||||
a hole in the buffer, or overwriting parts of the internal buffer.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
The buffer has content_size of 8, and allocated size of 12
|
||||
elements.
|
||||
|
||||
-------------------------------------------------
|
||||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | x | x | x | x |
|
||||
-------------------------------------------------
|
||||
|
||||
|
||||
buffer_memshift(buffer , 4 , 3)
|
||||
-------------------------------
|
||||
The part from offset four is moved three bytes to the right. The
|
||||
newly created 'hole' in the storage area has undefined values.
|
||||
|
||||
-------------------------------------------------
|
||||
| 0 | 1 | 2 | 3 | x | x | x | 4 | 5 | 6 | 7 | x |
|
||||
-------------------------------------------------
|
||||
|
||||
If you are shifting beyound the end of the buffer, it will be
|
||||
automatically resized.
|
||||
|
||||
|
||||
buffer_memshift(buffer , 2 , -4)
|
||||
--------------------------------
|
||||
|
||||
-------------------------------------------------
|
||||
| 4 | 5 | 6 | 7 | 8 | x | x | x | x | x | x | x |
|
||||
-------------------------------------------------
|
||||
|
||||
|
||||
When shifting to the left, content is lost (without warning/error)
|
||||
when it is shifted beyond the start of the buffer.
|
||||
|
||||
|
||||
The current position in the buffer is not updated, unless it
|
||||
corresponds to a point beyond the (new) end of the buffer, in which
|
||||
case it is set to the new end of the buffer.
|
||||
*/
|
||||
|
||||
void buffer_memshift(buffer_type * buffer , size_t offset, ssize_t shift) {
|
||||
/* Do we need to grow the buffer? */
|
||||
if (shift > 0) {
|
||||
if (buffer->alloc_size <= (buffer->content_size + shift)) {
|
||||
size_t new_size = 2 * (buffer->content_size + shift);
|
||||
buffer_resize__(buffer , new_size , true );
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
size_t move_size;
|
||||
if (shift < 0)
|
||||
if (abs(shift) > offset)
|
||||
offset = abs(shift); /* We are 'trying' to left shift beyond the start of the buffer. */
|
||||
|
||||
move_size = buffer->content_size - offset;
|
||||
memmove( &buffer->data[offset + shift] , &buffer->data[offset] , move_size );
|
||||
buffer->content_size += shift;
|
||||
buffer->pos = util_size_t_min( buffer->pos , buffer->content_size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void buffer_replace_data(buffer_type * buffer , size_t offset , size_t old_size , const void * new_data , size_t new_size) {
|
||||
ssize_t shift = new_size - old_size;
|
||||
buffer_memshift( buffer , offset , shift );
|
||||
buffer_fseek( buffer , offset , SEEK_SET );
|
||||
buffer_fwrite( buffer , new_data , 1 , new_size );
|
||||
}
|
||||
|
||||
|
||||
void buffer_replace_string( buffer_type * buffer , size_t offset , size_t old_size , const char * new_string) {
|
||||
|
||||
buffer_replace_data( buffer , offset , old_size , new_string , strlen(new_string));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This function will use the stdlib function strstr() to search for
|
||||
the string @expr in @buffer. The search will start at the current
|
||||
position in the buffer, if the string is found true is returned AND
|
||||
the internal pos is updated to point at the match.
|
||||
|
||||
If the string is NOT found the function will return false, without
|
||||
touching internal state.
|
||||
*/
|
||||
|
||||
|
||||
bool buffer_strstr( buffer_type * buffer , const char * expr ) {
|
||||
/**
|
||||
If this condition is satisfied the assumption that buffer->data
|
||||
is a \0 terminated string certainly breaks down.
|
||||
*/
|
||||
if ((buffer->content_size == 0) || (buffer->pos == buffer->content_size))
|
||||
return false;
|
||||
|
||||
{
|
||||
char * match = NULL;
|
||||
|
||||
match = strstr( &buffer->data[buffer->pos] , expr);
|
||||
if (match != NULL)
|
||||
buffer->pos = match - buffer->data;
|
||||
|
||||
return (match != NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool buffer_strchr( buffer_type * buffer , int c) {
|
||||
/**
|
||||
If this condition is satisfied the assumption that buffer->data
|
||||
is a \0 terminated string certainly breaks down.
|
||||
*/
|
||||
if ((buffer->content_size == 0) || (buffer->pos == buffer->content_size))
|
||||
return false;
|
||||
|
||||
{
|
||||
char * match = NULL;
|
||||
|
||||
match = strchr( &buffer->data[buffer->pos] , c);
|
||||
if (match != NULL)
|
||||
buffer->pos = match - buffer->data;
|
||||
|
||||
return (match != NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool buffer_search_replace( buffer_type * buffer , const char * old_string , const char * new_string) {
|
||||
const int shift = strlen( new_string ) - strlen( old_string );
|
||||
bool match = buffer_strstr( buffer , old_string );
|
||||
if (match) {
|
||||
size_t offset = buffer_get_offset( buffer ) + strlen( old_string );
|
||||
if (shift != 0)
|
||||
buffer_memshift( buffer , offset , shift );
|
||||
|
||||
/** Search continues at the end of the newly inserted string - i.e. no room for recursions. */
|
||||
buffer_fwrite( buffer , new_string , strlen( new_string ) , sizeof * new_string );
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void buffer_summarize(const buffer_type * buffer , const char * header) {
|
||||
printf("-----------------------------------------------------------------\n");
|
||||
if (header != NULL)
|
||||
printf("%s \n",header);
|
||||
printf(" Allocated size .....: %zd10 bytes \n",buffer->alloc_size);
|
||||
printf(" Content size .......: %zd10 bytes \n",buffer->content_size);
|
||||
printf(" Current position ...: %zd10 bytes \n",buffer->pos);
|
||||
printf("-----------------------------------------------------------------\n");
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/*****************************************************************/
|
||||
/*
|
||||
Here comes a couple of functions for loading/storing a buffer
|
||||
instance to a stream. Observe that when the buffer is stored to file
|
||||
it does not store any metadata (i.e. not even size) - only the raw
|
||||
buffer content.
|
||||
*/
|
||||
|
||||
/**
|
||||
This is the lowest level: 'read buffer content from file'
|
||||
function. It will read 'byte_size' bytes from stream and fill the
|
||||
buffer with the data.
|
||||
|
||||
When the function completes the buffer position is at the end of
|
||||
the buffer, i.e. it is ready for more calls to buffer_stream_fread;
|
||||
this is in contrast to the higher level functions
|
||||
buffer_fread_alloc() / buffer_fread_realloc() which reposition the
|
||||
buffer position to the beginning of the buffer.
|
||||
|
||||
Before reading from the buffer with e.g. buffer_fread_int() the
|
||||
buffer must be repositioned with buffer_rewind().
|
||||
*/
|
||||
|
||||
|
||||
void buffer_stream_fread( buffer_type * buffer , size_t byte_size , FILE * stream) {
|
||||
size_t min_size = byte_size + buffer->pos;
|
||||
if (buffer->alloc_size < min_size)
|
||||
buffer_resize__(buffer , min_size , true);
|
||||
|
||||
util_fread( &buffer->data[buffer->pos] , 1 , byte_size , stream , __func__);
|
||||
|
||||
buffer->content_size += byte_size;
|
||||
buffer->pos += byte_size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This file will read in the full content of file, and allocate a
|
||||
buffer instance with that content. When the function completes the
|
||||
position is at the beginning of the buffer.
|
||||
*/
|
||||
|
||||
void buffer_fread_realloc(buffer_type * buffer , const char * filename) {
|
||||
size_t file_size = util_file_size( filename );
|
||||
FILE * stream = util_fopen( filename , "r");
|
||||
|
||||
buffer_clear( buffer ); /* Setting: content_size = 0; pos = 0; */
|
||||
buffer_stream_fread( buffer , file_size , stream );
|
||||
buffer_rewind( buffer ); /* Setting: pos = 0; */
|
||||
fclose( stream );
|
||||
}
|
||||
|
||||
|
||||
|
||||
buffer_type * buffer_fread_alloc(const char * filename) {
|
||||
buffer_type * buffer = buffer_alloc( 0 );
|
||||
buffer_fread_realloc( buffer , filename );
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Will write parts of the buffer to the stream. Will start at buffer
|
||||
position @offset and write @write_size bytes.
|
||||
|
||||
o If @offset is invalid, i.e. less than zero or greater than
|
||||
buffer->content_size the function will fail hard.
|
||||
|
||||
o If @write_size is greater than storage in the buffer the
|
||||
function will just write all the available data, but not complain
|
||||
any more.
|
||||
|
||||
o @write_size == 0 that is interpreted as "write everything from offset".
|
||||
|
||||
o @write_size < 0 is interpreted as : "Write everything except the
|
||||
abs(@write_size) last bytes.
|
||||
|
||||
The return value is the number of bytes actually written.
|
||||
*/
|
||||
|
||||
size_t buffer_stream_fwrite_n( const buffer_type * buffer , size_t offset , ssize_t write_size , FILE * stream ) {
|
||||
if (offset < 0 || offset > buffer->content_size)
|
||||
util_abort("%s: invalid offset:%ld - valid range: [0,%ld) \n",__func__ , offset , offset);
|
||||
{
|
||||
ssize_t len;
|
||||
|
||||
if (write_size > 0) /* Normal - write @write_size bytes from offset */
|
||||
len = write_size;
|
||||
else if (write_size == 0) /* Write everything from the offset */
|
||||
len = buffer->content_size - offset;
|
||||
else /* @write_size < 0 - write everything excluding the last abs(write_size) bytes. */
|
||||
len = buffer->content_size - offset - abs( write_size );
|
||||
|
||||
if (len < 0)
|
||||
util_abort("%s: invalid length spesifier - tried to write %ld bytes \n",__func__ , len);
|
||||
|
||||
util_fwrite( &buffer->data[offset] , 1 , len , stream , __func__);
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void buffer_stream_fwrite( const buffer_type * buffer , FILE * stream ) {
|
||||
buffer_stream_fwrite_n( buffer , 0 , buffer->content_size , stream );
|
||||
}
|
||||
|
||||
|
||||
/* Assumes that the buffer contains a \0 terminated string - that is the resoponsability of the caller. */
|
||||
void buffer_stream_fprintf( const buffer_type * buffer , FILE * stream ) {
|
||||
fprintf(stream , "%s" , buffer->data );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Dumps buffer content to a stream - without any metadata.
|
||||
*/
|
||||
void buffer_store(const buffer_type * buffer , const char * filename) {
|
||||
FILE * stream = util_fopen(filename , "w");
|
||||
buffer_stream_fwrite( buffer , stream );
|
||||
fclose( stream );
|
||||
}
|
||||
|
||||
|
||||
#ifdef WITH_ZLIB
|
||||
#include "buffer_zlib.c"
|
||||
#endif
|
||||
|
103
ThirdParty/Ert/devel/libert_util/src/buffer.h
vendored
Normal file
103
ThirdParty/Ert/devel/libert_util/src/buffer.h
vendored
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'buffer.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __BUFFER_H__
|
||||
#define __BUFFER_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <ssize_t.h>
|
||||
|
||||
|
||||
typedef struct buffer_struct buffer_type;
|
||||
|
||||
buffer_type * buffer_alloc( size_t buffer_size );
|
||||
buffer_type * buffer_alloc_private_wrapper(void * data , size_t buffer_size );
|
||||
bool buffer_search_replace( buffer_type * buffer , const char * old_string , const char * new_string);
|
||||
void buffer_shrink_to_fit( buffer_type * buffer );
|
||||
void buffer_memshift(buffer_type * buffer , size_t offset, ssize_t shift);
|
||||
bool buffer_strstr( buffer_type * buffer , const char * expr );
|
||||
bool buffer_strchr( buffer_type * buffer , int c);
|
||||
void buffer_replace_string( buffer_type * buffer , size_t offset , size_t old_size , const char * new_string);
|
||||
void buffer_replace_data(buffer_type * buffer , size_t offset , size_t old_size , const void * new_data , size_t new_size);
|
||||
|
||||
void buffer_free_container( buffer_type * buffer );
|
||||
void buffer_free( buffer_type * buffer);
|
||||
size_t buffer_safe_fread(buffer_type * buffer , void * target_ptr , size_t item_size , size_t items);
|
||||
size_t buffer_fread(buffer_type * buffer , void * target_ptr , size_t item_size , size_t items);
|
||||
size_t buffer_safe_fwrite(buffer_type * buffer , const void * src_ptr , size_t item_size , size_t items);
|
||||
size_t buffer_fwrite(buffer_type * buffer , const void * src_ptr , size_t item_size , size_t items);
|
||||
const char * buffer_fread_string(buffer_type * buffer);
|
||||
char * buffer_fread_alloc_string(buffer_type * buffer);
|
||||
void buffer_fwrite_string(buffer_type * buffer , const char * string);
|
||||
void buffer_summarize(const buffer_type * buffer , const char *);
|
||||
|
||||
void buffer_fwrite_char_ptr(buffer_type * buffer , const char * string_ptr );
|
||||
void buffer_terminate_char_ptr( buffer_type * buffer );
|
||||
void buffer_fwrite_char(buffer_type * buffer , char value);
|
||||
void buffer_fwrite_int(buffer_type * buffer , int value);
|
||||
void buffer_fskip_bool(buffer_type * buffer);
|
||||
void buffer_fwrite_bool(buffer_type * buffer , bool value);
|
||||
int buffer_fread_int(buffer_type * buffer );
|
||||
bool buffer_fread_bool(buffer_type * buffer);
|
||||
long int buffer_fread_long(buffer_type * buffer);
|
||||
void buffer_fskip_long(buffer_type * buffer);
|
||||
void buffer_store(const buffer_type * buffer , const char * filename);
|
||||
size_t buffer_get_offset(const buffer_type * buffer);
|
||||
size_t buffer_get_alloc_size(const buffer_type * buffer);
|
||||
size_t buffer_get_size(const buffer_type * buffer);
|
||||
size_t buffer_get_string_size( const buffer_type * buffer );
|
||||
size_t buffer_get_remaining_size(const buffer_type * buffer);
|
||||
void * buffer_get_data(const buffer_type * buffer);
|
||||
void * buffer_alloc_data_copy(const buffer_type * buffer);
|
||||
void buffer_stream_fwrite( const buffer_type * buffer , FILE * stream );
|
||||
int buffer_fgetc( buffer_type * buffer );
|
||||
void buffer_fseek(buffer_type * buffer , ssize_t offset , int whence);
|
||||
void buffer_fskip(buffer_type * buffer, ssize_t offset);
|
||||
void buffer_clear( buffer_type * buffer );
|
||||
|
||||
void buffer_fskip_int(buffer_type * buffer);
|
||||
void buffer_fskip_time_t(buffer_type * buffer);
|
||||
time_t buffer_fread_time_t(buffer_type * buffer);
|
||||
void buffer_fwrite_time_t(buffer_type * buffer , time_t value);
|
||||
void buffer_rewind(buffer_type * buffer );
|
||||
|
||||
double buffer_fread_double(buffer_type * buffer);
|
||||
void buffer_fwrite_double(buffer_type * buffer , double value);
|
||||
|
||||
size_t buffer_stream_fwrite_n( const buffer_type * buffer , size_t offset , ssize_t write_size , FILE * stream );
|
||||
void buffer_stream_fprintf( const buffer_type * buffer , FILE * stream );
|
||||
void buffer_stream_fread( buffer_type * buffer , size_t byte_size , FILE * stream);
|
||||
buffer_type * buffer_fread_alloc(const char * filename);
|
||||
void buffer_fread_realloc(buffer_type * buffer , const char * filename);
|
||||
|
||||
#ifdef WITH_ZLIB
|
||||
size_t buffer_fwrite_compressed(buffer_type * buffer, const void * ptr , size_t byte_size);
|
||||
size_t buffer_fread_compressed(buffer_type * buffer , size_t compressed_size , void * target_ptr , size_t target_size);
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
74
ThirdParty/Ert/devel/libert_util/src/buffer_zlib.c
vendored
Normal file
74
ThirdParty/Ert/devel/libert_util/src/buffer_zlib.c
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
This file is compiled as part of the buffer.c file; if the symbol
|
||||
HAVE_ZLIB is defined.
|
||||
*/
|
||||
#include <zlib.h>
|
||||
/**
|
||||
Unfortunately the old RedHat3 computers have a zlib version which
|
||||
does not have the compressBound function. For that reason the
|
||||
compressBound function from a 1.2xx version of zlib is pasted in
|
||||
here verbatim:
|
||||
*/
|
||||
|
||||
|
||||
/* Snipped from zlib source code: */
|
||||
static size_t __compress_bound (size_t sourceLen)
|
||||
{
|
||||
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Return value is the size (in bytes) of the compressed buffer.
|
||||
*/
|
||||
size_t buffer_fwrite_compressed(buffer_type * buffer, const void * ptr , size_t byte_size) {
|
||||
size_t compressed_size = 0;
|
||||
bool abort_on_error = true;
|
||||
buffer->content_size = buffer->pos; /* Invalidating possible buffer content coming after the compressed content; that is uninterpretable anyway. */
|
||||
|
||||
if (byte_size > 0) {
|
||||
size_t remaining_size = buffer->alloc_size - buffer->pos;
|
||||
size_t compress_bound = __compress_bound( byte_size );
|
||||
if (compress_bound > remaining_size)
|
||||
buffer_resize__(buffer , remaining_size + compress_bound , abort_on_error);
|
||||
|
||||
compressed_size = buffer->alloc_size - buffer->pos;
|
||||
util_compress_buffer( ptr , byte_size , &buffer->data[buffer->pos] , &compressed_size);
|
||||
buffer->pos += compressed_size;
|
||||
buffer->content_size += compressed_size;
|
||||
}
|
||||
|
||||
return compressed_size;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Return value is the size of the uncompressed buffer.
|
||||
*/
|
||||
size_t buffer_fread_compressed(buffer_type * buffer , size_t compressed_size , void * target_ptr , size_t target_size) {
|
||||
size_t remaining_size = buffer->content_size - buffer->pos;
|
||||
size_t uncompressed_size = target_size;
|
||||
if (remaining_size < compressed_size)
|
||||
util_abort("%s: trying to read beyond end of buffer\n",__func__);
|
||||
|
||||
|
||||
if (compressed_size > 0) {
|
||||
int uncompress_result = uncompress(target_ptr , &uncompressed_size , &buffer->data[buffer->pos] , compressed_size);
|
||||
if (uncompress_result != Z_OK) {
|
||||
fprintf(stderr,"%s: ** Warning uncompress result:%d != Z_OK.\n" , __func__ , uncompress_result);
|
||||
/**
|
||||
According to the zlib documentation:
|
||||
|
||||
1. Values > 0 are not errors - just rare events?
|
||||
2. The value Z_BUF_ERROR is not fatal - we let that pass?!
|
||||
*/
|
||||
if (uncompress_result < 0 && uncompress_result != Z_BUF_ERROR)
|
||||
util_abort("%s: fatal uncompress error: %d \n",__func__ , uncompress_result);
|
||||
}
|
||||
} else
|
||||
uncompressed_size = 0;
|
||||
|
||||
buffer->pos += compressed_size;
|
||||
return uncompressed_size;
|
||||
}
|
||||
|
803
ThirdParty/Ert/devel/libert_util/src/conf_new.c
vendored
Normal file
803
ThirdParty/Ert/devel/libert_util/src/conf_new.c
vendored
Normal file
@ -0,0 +1,803 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'conf_new.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <util.h>
|
||||
#include <hash.h>
|
||||
#include <set.h>
|
||||
#include <vector.h>
|
||||
#include <int_vector.h>
|
||||
#include <conf_new.h>
|
||||
#include <parser.h>
|
||||
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
Defining parameters for the file inclusion and tokenizing.
|
||||
*/
|
||||
#define __CONF_INCLUDE "include"
|
||||
#define __CONF_WHITESPACE " \t\n\r,;"
|
||||
#define __CONF_COM_START "--"
|
||||
#define __CONF_COM_STOP "\n"
|
||||
#define __CONF_SPECIAL "[]{}="
|
||||
#define __CONF_QUOTERS "'\""
|
||||
#define __CONF_DELETE NULL
|
||||
|
||||
#define __CONF_VEC_START "["
|
||||
#define __CONF_VEC_STOP "]"
|
||||
#define __CONF_EXP_START "{"
|
||||
#define __CONF_EXP_STOP "}"
|
||||
#define __CONF_ASSIGN "="
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Defining numbers for safe run time casting.
|
||||
*/
|
||||
#define __CONF_SPEC_ID 132489012
|
||||
#define __CONF_ITEM_ID 342349032
|
||||
#define __CONF_ID 314234239
|
||||
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
TODO
|
||||
|
||||
Data structures should have some parsing info etc.
|
||||
Should have a special structure for the error messages.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
struct conf_spec_struct
|
||||
{
|
||||
int __id; /** Used for safe run time casting. */
|
||||
char * name; /** Name used to identify this class. */
|
||||
const conf_spec_type * super; /** Mother. Can be NULL for the root. */
|
||||
hash_type * specs; /** Hash of conf_spec_type's. */
|
||||
validator_ftype * validator; /** Function used to validate a conf_type.*/
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct conf_struct
|
||||
{
|
||||
int __id; /** Used for safe run time casting. */
|
||||
conf_type * super; /** Mother. Can be NULL for the root. */
|
||||
char * type; /** Name used to identify the class. */
|
||||
char * name; /** Name used to identify the instance. */
|
||||
vector_type * confs; /** Vector of conf_type's. */
|
||||
hash_type * items; /** Hash of conf_item_type's. */
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct conf_item_struct
|
||||
{
|
||||
int __id; /** Used for safe run time casting. */
|
||||
conf_type * super; /** Mother. Can not be NULL. */
|
||||
stringlist_type * values; /** Raw tokens read from file. */
|
||||
};
|
||||
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
|
||||
static
|
||||
bool strings_are_equal(
|
||||
const char * string_a,
|
||||
const char * string_b)
|
||||
{
|
||||
if( strcmp(string_a, string_b) == 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Check if a string is equal one of the characters in the special set.
|
||||
*/
|
||||
static
|
||||
bool string_is_special(
|
||||
const char * str)
|
||||
{
|
||||
int num_special_chars = strlen(__CONF_SPECIAL);
|
||||
if( num_special_chars != 1 )
|
||||
{
|
||||
/**
|
||||
Cannot be in the special set if it's not a single character.
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i=0; i<num_special_chars; i++)
|
||||
{
|
||||
if( str[0] == __CONF_SPECIAL[i])
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Find the realpath of file included from another file.
|
||||
Returns CONF_OK and stores the realpath in *include_file_realpath
|
||||
if the realpath can be resolved. If the realopath cannot be resolved,
|
||||
the function returns CONF_UNABLE_TO_OPEN_FILE and *include_file_realpath
|
||||
is set to NULL.
|
||||
|
||||
The calling scope is responsible for free'ing *include_file_realpath if
|
||||
CONF_OK is returned.
|
||||
*/
|
||||
static
|
||||
int alloc_included_realpath(
|
||||
const char * current_file_realpath,
|
||||
const char * include_file,
|
||||
char ** include_file_realpath
|
||||
)
|
||||
{
|
||||
assert(current_file_realpath != NULL);
|
||||
assert(include_file != NULL);
|
||||
assert(include_file_realpath != NULL);
|
||||
|
||||
bool realpath_ok;
|
||||
char * path;
|
||||
|
||||
if( util_is_abs_path(include_file) )
|
||||
path = util_alloc_string_copy(include_file);
|
||||
else
|
||||
{
|
||||
util_alloc_file_components(current_file_realpath, &path, NULL, NULL);
|
||||
if( path == NULL )
|
||||
path = util_alloc_string_copy("/");
|
||||
path = util_strcat_realloc(path, "/");
|
||||
path = util_strcat_realloc(path, include_file);
|
||||
}
|
||||
|
||||
realpath_ok = util_try_alloc_realpath(path);
|
||||
if( realpath_ok )
|
||||
*include_file_realpath = util_alloc_realpath(path);
|
||||
else
|
||||
*include_file_realpath = NULL;
|
||||
|
||||
free(path);
|
||||
|
||||
if( realpath_ok )
|
||||
return CONF_OK;
|
||||
else
|
||||
return CONF_UNABLE_TO_OPEN_FILE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Recursively builds a stringlist of tokens from files, and an associated
|
||||
stringlist of the source file for each token.
|
||||
|
||||
Do not use this function directly, use create_token_buffer.
|
||||
|
||||
NOTE: This function wants the realpath of the file to tokenize!
|
||||
*/
|
||||
static
|
||||
int create_token_buffer__(
|
||||
stringlist_type ** tokens, /** Memory to the created pointer. */
|
||||
stringlist_type ** src_files, /** Memory to the created pointer. */
|
||||
const set_type * sources, /** Real path of files already sourced. */
|
||||
const parser_type * parser , /** Parser to use. */
|
||||
const char * filename , /** Real path of file to parse. */
|
||||
stringlist_type * errors /** Storage for error messages. */
|
||||
)
|
||||
{
|
||||
int status;
|
||||
|
||||
stringlist_type * tokens__;
|
||||
stringlist_type * src_files__;
|
||||
|
||||
/**
|
||||
Assert that we've got a realpath.
|
||||
*/
|
||||
assert( util_is_abs_path(filename) );
|
||||
|
||||
/**
|
||||
Check if the file has been parsed before.
|
||||
*/
|
||||
if( !set_has_key(sources, filename) )
|
||||
status = CONF_OK;
|
||||
else
|
||||
{
|
||||
char * err = util_alloc_sprintf("Circular inclusion of \"%s\".", filename);
|
||||
stringlist_append_owned_ref(errors, err);
|
||||
status = CONF_CIRCULAR_INCLUDE_ERROR;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Check that we can open the file.
|
||||
*/
|
||||
if( status == CONF_OK && !util_fopen_test(filename, "r") )
|
||||
{
|
||||
char * err = util_alloc_sprintf("Unable to open file \"%s\".", filename);
|
||||
stringlist_append_owned_ref(errors, err);
|
||||
status = CONF_UNABLE_TO_OPEN_FILE;
|
||||
}
|
||||
|
||||
|
||||
if( status != CONF_OK )
|
||||
{
|
||||
/**
|
||||
Create empty tokens__ and src_files__ list to return.
|
||||
*/
|
||||
tokens__ = stringlist_alloc_new();
|
||||
src_files__ = stringlist_alloc_new();
|
||||
}
|
||||
else
|
||||
{
|
||||
set_type * sources__ = set_copyc(sources);
|
||||
set_add_key(sources__, filename);
|
||||
|
||||
tokens__ = parser_tokenize_file(parser , filename, true);
|
||||
src_files__ = stringlist_alloc_new();
|
||||
|
||||
/**
|
||||
Set the sources. Let the first token own the memory.
|
||||
*/
|
||||
for(int i=0; i<stringlist_get_size(tokens__); i++)
|
||||
{
|
||||
if(i == 0)
|
||||
stringlist_append_copy(src_files__, filename);
|
||||
else
|
||||
stringlist_append_ref(src_files__, stringlist_iget(src_files__, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
Handle any include statements by recursion.
|
||||
*/
|
||||
{
|
||||
int i = stringlist_find_first(tokens__, __CONF_INCLUDE);
|
||||
while( i != -1)
|
||||
{
|
||||
const char * include_file;
|
||||
char * include_file_realpath;
|
||||
stringlist_type * included_tokens = NULL;
|
||||
stringlist_type * included_src_files = NULL;
|
||||
|
||||
/**
|
||||
Check that the next token actually exists.
|
||||
*/
|
||||
if( i+1 == stringlist_get_size(tokens__) )
|
||||
{
|
||||
char * err = util_alloc_sprintf("Unexpected end of file in \"%s\". "
|
||||
"Expected a file name after \"%s\".",
|
||||
filename, __CONF_INCLUDE);
|
||||
stringlist_append_owned_ref(errors, err);
|
||||
status = CONF_UNEXPECTED_EOF_ERROR;
|
||||
|
||||
/**
|
||||
Delete the last tokens, so that the output is still meaningful.
|
||||
*/
|
||||
stringlist_idel(tokens__, i);
|
||||
stringlist_idel(src_files__, i);
|
||||
break;
|
||||
}
|
||||
|
||||
include_file = stringlist_iget(tokens__, i+1);
|
||||
|
||||
/**
|
||||
Check that the file exists.
|
||||
*/
|
||||
status = alloc_included_realpath(filename,
|
||||
include_file,
|
||||
&include_file_realpath);
|
||||
if( status == CONF_OK )
|
||||
{
|
||||
/**
|
||||
Recursive call.
|
||||
*/
|
||||
int last_status = create_token_buffer__(&included_tokens,
|
||||
&included_src_files,
|
||||
sources__, parser,
|
||||
include_file_realpath,
|
||||
errors);
|
||||
if( last_status != CONF_OK)
|
||||
status = last_status;
|
||||
|
||||
free(include_file_realpath);
|
||||
}
|
||||
else
|
||||
{
|
||||
char * err = util_alloc_sprintf("Unable to open file \"%s\" included "
|
||||
"from file \"%s\".",
|
||||
include_file, filename);
|
||||
stringlist_append_owned_ref(errors, err);
|
||||
status = CONF_UNABLE_TO_OPEN_FILE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Delete the "include" and filename token. Do the same for src_files__.
|
||||
*/
|
||||
stringlist_idel(tokens__, i+1);
|
||||
stringlist_idel(tokens__, i);
|
||||
stringlist_idel(src_files__, i+1);
|
||||
stringlist_idel(src_files__, i);
|
||||
|
||||
if(included_tokens != NULL && included_src_files != NULL)
|
||||
{
|
||||
/**
|
||||
Replace the deleted items.
|
||||
*/
|
||||
stringlist_insert_stringlist_copy(tokens__, included_tokens, i);
|
||||
stringlist_free(included_tokens);
|
||||
stringlist_insert_stringlist_copy(src_files__, included_src_files, i);
|
||||
stringlist_free(included_src_files);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Find next include statement.
|
||||
*/
|
||||
i = stringlist_find_first(tokens__, __CONF_INCLUDE);
|
||||
}
|
||||
}
|
||||
|
||||
set_free(sources__);
|
||||
}
|
||||
|
||||
*tokens = tokens__;
|
||||
*src_files = src_files__;
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Creates a stringlist of tokens suitable for parsing with the config
|
||||
parser from a file. All include statements are resolved, and circular
|
||||
include statements are detected.
|
||||
|
||||
If no errors are detected CONF_OK will be returned. Otherwise, the last
|
||||
detected error will be returned. Error messages are available in *errors.
|
||||
|
||||
Note that the recursion does not halt on errors. Thus, the tokens are fully
|
||||
useable even though errors may have been encountered.
|
||||
|
||||
The calling scope is responsible for free'ing *tokens, *src_files and
|
||||
*errors when they are no longer needed.
|
||||
*/
|
||||
static
|
||||
int create_token_buffer(
|
||||
stringlist_type ** tokens, /** Memory for the tokens. */
|
||||
stringlist_type ** src_files, /** Memory for source file for each token. */
|
||||
stringlist_type ** errors, /** Memory to return error messages in. */
|
||||
const char * filename /** Filename to create tokens from. */
|
||||
)
|
||||
{
|
||||
int status;
|
||||
stringlist_type * tokens__ ;
|
||||
stringlist_type * src_files__;
|
||||
|
||||
stringlist_type * errors__ = stringlist_alloc_new();
|
||||
|
||||
if( util_fopen_test(filename, "r") )
|
||||
{
|
||||
parser_type * parser = parser_alloc(__CONF_WHITESPACE,
|
||||
__CONF_QUOTERS,
|
||||
__CONF_SPECIAL,
|
||||
__CONF_DELETE,
|
||||
__CONF_COM_START,
|
||||
__CONF_COM_STOP);
|
||||
|
||||
set_type * sources = set_alloc_empty();
|
||||
char * realpath_filename = util_alloc_realpath(filename);
|
||||
|
||||
status = create_token_buffer__(&tokens__, &src_files__, sources,
|
||||
parser, realpath_filename, errors__);
|
||||
|
||||
free(realpath_filename);
|
||||
set_free(sources);
|
||||
parser_free(parser);
|
||||
}
|
||||
else
|
||||
{
|
||||
tokens__ = stringlist_alloc_new();
|
||||
src_files__ = stringlist_alloc_new();
|
||||
|
||||
char * err = util_alloc_sprintf("Unable to open file \"%s\".", filename);
|
||||
stringlist_append_owned_ref(errors__, err);
|
||||
|
||||
status = CONF_UNABLE_TO_OPEN_FILE;
|
||||
}
|
||||
|
||||
*tokens = tokens__;
|
||||
*src_files = src_files__;
|
||||
*errors = errors__;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
|
||||
static
|
||||
conf_type * conf_alloc_root()
|
||||
{
|
||||
|
||||
conf_type * conf = util_malloc(sizeof * conf);
|
||||
|
||||
conf->__id = __CONF_ID;
|
||||
conf->super = NULL;
|
||||
conf->type = util_alloc_string_copy("root");
|
||||
conf->name = util_alloc_string_copy("root");
|
||||
|
||||
conf->confs = vector_alloc_new();
|
||||
conf->items = hash_alloc();
|
||||
|
||||
return conf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
conf_type * conf_safe_cast(
|
||||
void * conf
|
||||
)
|
||||
{
|
||||
conf_type * __conf = (conf_type *) conf;
|
||||
if( __conf->__id != __CONF_ID )
|
||||
util_abort("%s: Internal error. Run time cast failed.\n", __func__);
|
||||
return __conf;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void conf_free(
|
||||
conf_type * conf
|
||||
)
|
||||
{
|
||||
hash_free(conf->items);
|
||||
vector_free(conf->confs);
|
||||
|
||||
free(conf->type);
|
||||
free(conf->name);
|
||||
free(conf);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
void conf_free__(
|
||||
void * conf
|
||||
)
|
||||
{
|
||||
conf_type * conf__ = conf_safe_cast(conf);
|
||||
conf_free(conf__);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
conf_item_type * conf_item_safe_cast(
|
||||
void * item
|
||||
)
|
||||
{
|
||||
conf_item_type * __item = (conf_item_type *) item;
|
||||
if( __item->__id != __CONF_ITEM_ID)
|
||||
util_abort("%s: Internal error. Run time cast failed.\n", __func__);
|
||||
return __item;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
void conf_item_free(
|
||||
conf_item_type * item
|
||||
)
|
||||
{
|
||||
stringlist_free(item->values);
|
||||
free(item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
void conf_item_free__(
|
||||
void * item
|
||||
)
|
||||
{
|
||||
conf_item_type * item__ = conf_item_safe_cast(item);
|
||||
conf_item_free(item__);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
|
||||
static
|
||||
conf_item_type * conf_insert_item(
|
||||
conf_type * conf,
|
||||
const char * name
|
||||
)
|
||||
{
|
||||
assert(conf != NULL);
|
||||
assert(name != NULL);
|
||||
|
||||
conf_item_type * item = util_malloc(sizeof * item );
|
||||
item->__id = __CONF_ITEM_ID;
|
||||
item->values = stringlist_alloc_new();
|
||||
|
||||
hash_insert_hash_owned_ref(conf->items, name, item, conf_item_free__);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
conf_type * conf_append_child(
|
||||
conf_type * conf,
|
||||
const char * type,
|
||||
const char * name
|
||||
)
|
||||
{
|
||||
assert(conf != NULL);
|
||||
assert(type != NULL );
|
||||
assert(name != NULL );
|
||||
|
||||
conf_type * child = util_malloc(sizeof * child );
|
||||
|
||||
child->__id = __CONF_ID;
|
||||
child->super = conf;
|
||||
child->type = util_alloc_string_copy(type);
|
||||
child->name = util_alloc_string_copy(name);
|
||||
|
||||
child->confs = vector_alloc_new();
|
||||
child->items = hash_alloc();
|
||||
|
||||
vector_append_owned_ref(conf->confs, child, conf_free__);
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
conf_type * conf_get_super(
|
||||
conf_type * conf
|
||||
)
|
||||
{
|
||||
return conf->super;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
void conf_item_append_data_item(
|
||||
conf_item_type * item,
|
||||
const char * data
|
||||
)
|
||||
{
|
||||
stringlist_append_copy(item->values, data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
void conf_item_append_data(
|
||||
conf_item_type * item,
|
||||
const stringlist_type * data
|
||||
)
|
||||
{
|
||||
stringlist_append_stringlist_copy(item->values, data);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Create a shallow copy stringlist of tokens that belong to an item
|
||||
starting at *position__. The memory pointed by position__ is updated
|
||||
so that it contains the position of the first token AFTER the tokens
|
||||
belonging to the item.
|
||||
*/
|
||||
static
|
||||
int get_item_tokens(
|
||||
const stringlist_type * tokens,
|
||||
stringlist_type ** item_tokens,
|
||||
int * position__
|
||||
)
|
||||
{
|
||||
int status = CONF_OK;
|
||||
|
||||
int item_size = 0;
|
||||
int start = *position__;
|
||||
int position = *position__;
|
||||
int num_tokens = stringlist_get_size(tokens);
|
||||
|
||||
|
||||
const char * current_token = stringlist_iget(tokens, position);
|
||||
if( strings_are_equal(current_token, __CONF_VEC_START) )
|
||||
{
|
||||
position++;
|
||||
start = position;
|
||||
bool matched_delimiters = false;
|
||||
while( position < num_tokens && !matched_delimiters )
|
||||
{
|
||||
current_token = stringlist_iget(tokens, position);
|
||||
if( strings_are_equal(current_token, __CONF_VEC_STOP) )
|
||||
{
|
||||
matched_delimiters = true;
|
||||
position++;
|
||||
}
|
||||
else
|
||||
{
|
||||
item_size++;
|
||||
position++;
|
||||
}
|
||||
}
|
||||
|
||||
if( !matched_delimiters )
|
||||
{
|
||||
/**
|
||||
TODO
|
||||
|
||||
This is an error. Should add a message.
|
||||
*/
|
||||
status = CONF_PARSE_ERROR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
item_size = 1;
|
||||
position++;
|
||||
}
|
||||
|
||||
*item_tokens = stringlist_alloc_shallow_copy_with_limits(tokens, start,
|
||||
item_size);
|
||||
*position__ = position;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
int get_conf_tokens(
|
||||
const stringlist_type * tokens,
|
||||
stringlist_type ** conf_tokens,
|
||||
int * position__
|
||||
)
|
||||
{
|
||||
int status = CONF_OK;
|
||||
|
||||
int conf_size = 0;
|
||||
int start = *position__;
|
||||
int position = *position__;
|
||||
int num_tokens = stringlist_get_size(tokens);
|
||||
|
||||
|
||||
const char * current_token = stringlist_iget(tokens, position);
|
||||
if( strings_are_equal(current_token, __CONF_EXP_START) )
|
||||
{
|
||||
position++;
|
||||
start = position;
|
||||
int depth = 1;
|
||||
while( position < num_tokens && depth > 0 )
|
||||
{
|
||||
current_token = stringlist_iget(tokens, position);
|
||||
if( strings_are_equal(current_token, __CONF_EXP_START) )
|
||||
{
|
||||
position++;
|
||||
conf_size++;
|
||||
depth++;
|
||||
}
|
||||
else if( strings_are_equal(current_token, __CONF_EXP_STOP) )
|
||||
{
|
||||
position++;
|
||||
depth--;
|
||||
if(depth > 0)
|
||||
conf_size++;
|
||||
}
|
||||
else
|
||||
{
|
||||
position++;
|
||||
conf_size++;
|
||||
}
|
||||
}
|
||||
|
||||
if( depth > 0 )
|
||||
{
|
||||
/**
|
||||
TODO
|
||||
|
||||
This is an error. Should add a message.
|
||||
*/
|
||||
status = CONF_PARSE_ERROR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
conf_size = 0;
|
||||
position++;
|
||||
}
|
||||
|
||||
*conf_tokens = stringlist_alloc_shallow_copy_with_limits(tokens, start,
|
||||
conf_size);
|
||||
*position__ = position;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
int conf_alloc_from_tokens(
|
||||
const stringlist_type * tokens,
|
||||
stringlist_type * errors,
|
||||
conf_type ** conf__
|
||||
)
|
||||
{
|
||||
assert(tokens != NULL);
|
||||
assert(errors != NULL);
|
||||
|
||||
typedef enum {IN_ROOT, IN_CLASS, IN_ITEM} PARSER_STATE;
|
||||
PARSER_STATE state = IN_ROOT;
|
||||
|
||||
int position = 0;
|
||||
int num_tokens = stringlist_get_size(tokens);
|
||||
|
||||
conf_type * root = conf_alloc_root();
|
||||
|
||||
conf_type * current_conf = root;
|
||||
conf_item_type * current_item;
|
||||
|
||||
while(position < num_tokens)
|
||||
{
|
||||
/**
|
||||
On entering this loop, stringlist_iget(tokens, position)
|
||||
shall always be a non-special token.
|
||||
*/
|
||||
|
||||
const char * current_token = stringlist_iget(tokens, position);
|
||||
const char * look_ahead_one;
|
||||
const char * look_ahead_two;
|
||||
}
|
||||
}
|
||||
|
49
ThirdParty/Ert/devel/libert_util/src/conf_new.h
vendored
Normal file
49
ThirdParty/Ert/devel/libert_util/src/conf_new.h
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'conf_new.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __CONF_NEW_H__
|
||||
#define __CONF_NEW_H__
|
||||
|
||||
#include <stringlist.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define CONF_OK 0
|
||||
#define CONF_PARSE_ERROR 1
|
||||
#define CONF_CIRCULAR_INCLUDE_ERROR 2
|
||||
#define CONF_UNEXPECTED_EOF_ERROR 3
|
||||
#define CONF_UNABLE_TO_OPEN_FILE 4
|
||||
|
||||
|
||||
|
||||
typedef struct conf_struct conf_type;
|
||||
typedef struct conf_item_struct conf_item_type;
|
||||
typedef struct conf_spec_struct conf_spec_type;
|
||||
|
||||
typedef int (validator_ftype)(conf_type * conf);
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
11
ThirdParty/Ert/devel/libert_util/src/config_test_input
vendored
Normal file
11
ThirdParty/Ert/devel/libert_util/src/config_test_input
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
KEY1 ARG1 ARG2 ARG3
|
||||
KEY2 emacs /tmp/Gurbat/Ensemble/enkf_mount_info 1 F
|
||||
KEY1 PISS OFF
|
||||
KEY2 __RESET__
|
||||
KEY2 ssXX
|
||||
FATHER
|
||||
--CHILD1 1
|
||||
--CHILD2 2
|
||||
INCLUDE Path/config_test_input2
|
||||
|
||||
|
26
ThirdParty/Ert/devel/libert_util/src/dependencies
vendored
Normal file
26
ThirdParty/Ert/devel/libert_util/src/dependencies
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
list.o : list.c list.h list_node.o
|
||||
block_fs.o : block_fs.c block_fs.h hash.o util.o vector.o buffer.o
|
||||
hash_node.o : hash_node.c hash_node.h util.o node_data.o
|
||||
menu.o : menu.c menu.h util.o vector.o
|
||||
buffer.o : buffer.c buffer.h util.o
|
||||
util.o : util.c util.h util_path.c
|
||||
stringlist.o : stringlist.c stringlist.h util.o vector.o buffer.o
|
||||
hash.o : hash.c hash.h hash_sll.o hash_node.o node_data.o util.o stringlist.o
|
||||
timer.o : timer.c timer.h util.o
|
||||
arg_pack.o : arg_pack.c arg_pack.h util.o node_ctype.o
|
||||
node_data.o : node_data.c node_data.h util.o node_ctype.o
|
||||
matrix.o : matrix.c matrix.h util.o thread_pool.o arg_pack.o
|
||||
vector.o : vector.c vector.h util.o node_data.o
|
||||
msg.o : msg.c msg.h util.o
|
||||
hash_sll.o : hash_sll.c hash_sll.h hash_node.o
|
||||
subst.o : subst.c subst.h util.o vector.o node_data.o
|
||||
matrix_blas.o : matrix_blas.c matrix_blas.h util.o matrix.o
|
||||
matrix_lapack.o : matrix_lapack.c matrix_lapack.h matrix.o util.o
|
||||
template.o : template.c template.h subst.o
|
||||
list_node.o : list_node.c list_node.h util.o node_data.o
|
||||
set.o : set.c set.h hash.o util.o
|
||||
node_ctype.o : node_ctype.c node_ctype.h
|
||||
path_fmt.o : path_fmt.c path_fmt.h util.o node_ctype.o
|
||||
str_buffer.o : str_buffer.c str_buffer.h
|
||||
thread_pool.o : thread_pool.c thread_pool.h util.o
|
||||
log.o : log.c log.h util.o
|
960
ThirdParty/Ert/devel/libert_util/src/hash.c
vendored
Normal file
960
ThirdParty/Ert/devel/libert_util/src/hash.c
vendored
Normal file
@ -0,0 +1,960 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'hash.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE /* Must define this to get access to pthread_rwlock_t */
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <hash.h>
|
||||
#include <hash_sll.h>
|
||||
#include <hash_node.h>
|
||||
#include <node_data.h>
|
||||
#include <util.h>
|
||||
#include <stringlist.h>
|
||||
|
||||
#ifdef HAVE_PTHREAD
|
||||
#include <pthread.h>
|
||||
typedef pthread_rwlock_t lock_type;
|
||||
#else
|
||||
typedef int lock_type;
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define HASH_DEFAULT_SIZE 16
|
||||
#define HASH_TYPE_ID 771065
|
||||
|
||||
/**
|
||||
This is **THE** hash function - which actually does the hashing.
|
||||
*/
|
||||
|
||||
static uint32_t hash_index(const char *key, size_t len) {
|
||||
uint32_t hash = 0;
|
||||
size_t i;
|
||||
|
||||
for (i=0; i < len; i++) {
|
||||
hash += key[i];
|
||||
hash += (hash << 10);
|
||||
hash ^= (hash >> 6);
|
||||
}
|
||||
hash += (hash << 3);
|
||||
hash ^= (hash >> 11);
|
||||
hash += (hash << 15);
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
struct hash_struct {
|
||||
UTIL_TYPE_ID_DECLARATION;
|
||||
uint32_t size; /* This is the size of the internal table **NOT**NOT** the number of elements in the table. */
|
||||
uint32_t elements; /* The number of elements in the hash table. */
|
||||
double resize_fill;
|
||||
hash_sll_type **table;
|
||||
hashf_type *hashf;
|
||||
|
||||
lock_type rwlock;
|
||||
};
|
||||
|
||||
|
||||
|
||||
typedef struct hash_sort_node {
|
||||
char *key;
|
||||
int cmp_value;
|
||||
} hash_sort_type;
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/* locking */
|
||||
/*****************************************************************/
|
||||
#ifdef HAVE_PTHREAD
|
||||
static void __hash_deadlock_abort(hash_type * hash) {
|
||||
util_abort("%s: A deadlock condition has been detected in the hash routine - and the program will abort.\n", __func__);
|
||||
}
|
||||
|
||||
|
||||
static void __hash_rdlock(hash_type * hash) {
|
||||
int lock_error = pthread_rwlock_tryrdlock( &hash->rwlock );
|
||||
if (lock_error != 0) {
|
||||
/* We did not get the lock - let us check why: */
|
||||
if (lock_error == EDEADLK)
|
||||
/* A deadlock is detected - we just abort. */
|
||||
__hash_deadlock_abort(hash);
|
||||
else
|
||||
/* We ignore all other error conditions than DEADLOCK and just try again. */
|
||||
pthread_rwlock_rdlock( &hash->rwlock );
|
||||
}
|
||||
/* Ok - when we are here - we are guranteed to have the lock. */
|
||||
}
|
||||
|
||||
|
||||
static void __hash_wrlock(hash_type * hash) {
|
||||
int lock_error = pthread_rwlock_trywrlock( &hash->rwlock );
|
||||
if (lock_error != 0) {
|
||||
/* We did not get the lock - let us check why: */
|
||||
if (lock_error == EDEADLK)
|
||||
/* A deadlock is detected - we just abort. */
|
||||
__hash_deadlock_abort(hash);
|
||||
else
|
||||
/* We ignore all other error conditions than DEADLOCK and just try again. */
|
||||
pthread_rwlock_wrlock( &hash->rwlock );
|
||||
}
|
||||
/* Ok - when we are here - we are guranteed to have the lock. */
|
||||
}
|
||||
|
||||
|
||||
static void __hash_unlock( hash_type * hash) {
|
||||
pthread_rwlock_unlock( &hash->rwlock );
|
||||
}
|
||||
|
||||
|
||||
static void LOCK_DESTROY( lock_type * rwlock ) {
|
||||
pthread_rwlock_destroy( rwlock );
|
||||
}
|
||||
|
||||
static void LOCK_INIT( lock_type * rwlock ) {
|
||||
pthread_rwlock_init( rwlock , NULL);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static void __hash_rdlock(hash_type * hash) {}
|
||||
static void __hash_wrlock(hash_type * hash) {}
|
||||
static void __hash_unlock(hash_type * hash) {}
|
||||
static void LOCK_DESTROY(lock_type * rwlock) {}
|
||||
static void LOCK_INIT(lock_type * rwlock) {}
|
||||
|
||||
#endif
|
||||
|
||||
/*****************************************************************/
|
||||
/* Low level access functions */
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
static void * __hash_get_node_unlocked(const hash_type *__hash , const char *key, bool abort_on_error) {
|
||||
hash_type * hash = (hash_type *) __hash; /* The net effect is no change - but .... ?? */
|
||||
hash_node_type * node = NULL;
|
||||
{
|
||||
const uint32_t global_index = hash->hashf(key , strlen(key));
|
||||
const uint32_t table_index = (global_index % hash->size);
|
||||
|
||||
node = hash_sll_get(hash->table[table_index] , global_index , key);
|
||||
if (node == NULL && abort_on_error)
|
||||
util_abort("%s: tried to get from key:%s which does not exist - aborting \n",__func__ , key);
|
||||
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
This function looks up a hash_node from the hash. This is the common
|
||||
low-level function to get content from the hash. The function takes
|
||||
read-lock which is held during execution.
|
||||
|
||||
Would strongly preferred that the hash_type * was const - but that is
|
||||
difficult due to locking requirements.
|
||||
*/
|
||||
|
||||
static void * __hash_get_node(hash_type *hash , const char *key, bool abort_on_error) {
|
||||
hash_node_type * node;
|
||||
__hash_rdlock( hash );
|
||||
node = __hash_get_node_unlocked(hash , key , abort_on_error);
|
||||
__hash_unlock( hash );
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
static node_data_type * hash_get_node_data(hash_type *hash , const char *key) {
|
||||
hash_node_type * node = __hash_get_node(hash , key , true);
|
||||
return hash_node_get_data(node);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This function resizes the hash table when it has become to full.
|
||||
The table only grows - this function is called from
|
||||
__hash_insert_node().
|
||||
|
||||
If you know in advance (roughly) how large the hash table will be
|
||||
it can be advantageous to call hash_resize() manually, to avoid
|
||||
repeated internal calls to hash_resize().
|
||||
*/
|
||||
|
||||
void hash_resize(hash_type *hash, int new_size) {
|
||||
hash_sll_type ** new_table = hash_sll_alloc_table( new_size );
|
||||
hash_node_type * node;
|
||||
int i;
|
||||
|
||||
for (i=0; i < hash->size; i++) {
|
||||
node = hash_sll_get_head(hash->table[i]);
|
||||
while (node != NULL) {
|
||||
uint32_t new_table_index = hash_node_set_table_index(node , new_size);
|
||||
hash_node_type *next_node = hash_node_get_next(node);
|
||||
hash_sll_add_node(new_table[new_table_index] , node);
|
||||
node = next_node;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Only freeing the table structure, *NOT* calling the node_free()
|
||||
functions, which happens when hash_sll_free() is called.
|
||||
*/
|
||||
|
||||
{
|
||||
for (i=0; i < hash->size; i++)
|
||||
free( hash->table[i] );
|
||||
free( hash->table );
|
||||
}
|
||||
|
||||
hash->size = new_size;
|
||||
hash->table = new_table;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This is the low-level function for inserting a hash node. This
|
||||
function takes a write-lock which is held during the execution of
|
||||
the function.
|
||||
*/
|
||||
|
||||
static void __hash_insert_node(hash_type *hash , hash_node_type *node) {
|
||||
__hash_wrlock( hash );
|
||||
{
|
||||
uint32_t table_index = hash_node_get_table_index(node);
|
||||
{
|
||||
/*
|
||||
If a node with the same key already exists in the table
|
||||
it is removed.
|
||||
*/
|
||||
hash_node_type *existing_node = __hash_get_node_unlocked(hash , hash_node_get_key(node) , false);
|
||||
if (existing_node != NULL) {
|
||||
hash_sll_del_node(hash->table[table_index] , existing_node);
|
||||
hash->elements--;
|
||||
}
|
||||
}
|
||||
|
||||
hash_sll_add_node(hash->table[table_index] , node);
|
||||
hash->elements++;
|
||||
if ((1.0 * hash->elements / hash->size) > hash->resize_fill)
|
||||
hash_resize(hash , hash->size * 2);
|
||||
}
|
||||
__hash_unlock( hash );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This function deletes a node from the hash_table. Observe that this
|
||||
function does *NOT* do any locking - it is the repsonsibility of
|
||||
the calling functions: hash_del() and hash_clear() to take the
|
||||
necessary write lock.
|
||||
*/
|
||||
|
||||
|
||||
static void hash_del_unlocked__(hash_type *hash , const char *key) {
|
||||
const uint32_t global_index = hash->hashf(key , strlen(key));
|
||||
const uint32_t table_index = (global_index % hash->size);
|
||||
hash_node_type *node = hash_sll_get(hash->table[table_index] , global_index , key);
|
||||
|
||||
if (node == NULL)
|
||||
util_abort("%s: hash does not contain key:%s - aborting \n",__func__ , key);
|
||||
else
|
||||
hash_sll_del_node(hash->table[table_index] , node);
|
||||
|
||||
hash->elements--;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This functions takes a hash_node and finds the "next" hash node by
|
||||
traversing the internal hash structure. Should NOT be confused with
|
||||
the other functions providing iterations to user-space.
|
||||
*/
|
||||
|
||||
static hash_node_type * hash_internal_iter_next(const hash_type *hash , const hash_node_type * node) {
|
||||
hash_node_type *next_node = hash_node_get_next(node);
|
||||
if (next_node == NULL) {
|
||||
const uint32_t table_index = hash_node_get_table_index(node);
|
||||
if (table_index < hash->size) {
|
||||
uint32_t i = table_index + 1;
|
||||
while (i < hash->size && hash_sll_empty(hash->table[i]))
|
||||
i++;
|
||||
|
||||
if (i < hash->size)
|
||||
next_node = hash_sll_get_head(hash->table[i]);
|
||||
}
|
||||
}
|
||||
return next_node;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This is the low level function which traverses a hash table and
|
||||
allocates a char ** list of keys.
|
||||
|
||||
It takes a read-lock which is held during the execution of the
|
||||
function. The locking guarantees that the list of keys is valid
|
||||
when this function is exited, but the the hash table can be
|
||||
subsequently updated.
|
||||
|
||||
If the hash table is empty NULL is returned.
|
||||
*/
|
||||
|
||||
static char ** hash_alloc_keylist__(hash_type *hash , bool lock) {
|
||||
char **keylist;
|
||||
if (lock) __hash_rdlock( hash );
|
||||
{
|
||||
if (hash->elements > 0) {
|
||||
int i = 0;
|
||||
hash_node_type *node = NULL;
|
||||
keylist = calloc(hash->elements , sizeof *keylist);
|
||||
{
|
||||
uint32_t i = 0;
|
||||
while (i < hash->size && hash_sll_empty(hash->table[i]))
|
||||
i++;
|
||||
|
||||
if (i < hash->size)
|
||||
node = hash_sll_get_head(hash->table[i]);
|
||||
}
|
||||
|
||||
while (node != NULL) {
|
||||
const char *key = hash_node_get_key(node);
|
||||
keylist[i] = util_alloc_string_copy(key);
|
||||
node = hash_internal_iter_next(hash , node);
|
||||
i++;
|
||||
}
|
||||
} else keylist = NULL;
|
||||
}
|
||||
if (lock) __hash_unlock( hash );
|
||||
return keylist;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/**
|
||||
The fundamental functions above relate the hash_node
|
||||
structure. Here comes a list of functions for inserting managed
|
||||
copies of various types.
|
||||
*/
|
||||
|
||||
void hash_insert_string(hash_type * hash , const char * key , const char * value) {
|
||||
node_data_type * node_data = node_data_alloc_string( value );
|
||||
hash_node_type * hash_node = hash_node_alloc_new(key , node_data , hash->hashf , hash->size);
|
||||
__hash_insert_node(hash , hash_node);
|
||||
}
|
||||
|
||||
|
||||
char * hash_get_string(hash_type * hash , const char * key) {
|
||||
node_data_type * node_data = hash_get_node_data(hash , key);
|
||||
return node_data_get_string( node_data );
|
||||
}
|
||||
|
||||
|
||||
void hash_insert_int(hash_type * hash , const char * key , int value) {
|
||||
node_data_type * node_data = node_data_alloc_int( value );
|
||||
hash_node_type * hash_node = hash_node_alloc_new(key , node_data , hash->hashf , hash->size);
|
||||
__hash_insert_node(hash , hash_node);
|
||||
}
|
||||
|
||||
|
||||
int hash_get_int(hash_type * hash , const char * key) {
|
||||
node_data_type * node_data = hash_get_node_data(hash , key);
|
||||
return node_data_get_int( node_data );
|
||||
}
|
||||
|
||||
/**
|
||||
Small wrapper around hash_get_int() / hash_insert_int() which
|
||||
implements a zero based counter.
|
||||
|
||||
hash_inc_counter()
|
||||
|
||||
Will increment the integer value stored in the node_data instance,
|
||||
and return the updated value. If the key is not present in the hash
|
||||
it will be inserted as an integer with value 0, and 0 will be
|
||||
returned.
|
||||
*/
|
||||
|
||||
int hash_inc_counter(hash_type * hash , const char * counter_key) {
|
||||
if (hash_has_key( hash , counter_key)) {
|
||||
node_data_type * node_data = hash_get_node_data(hash , counter_key);
|
||||
return node_data_fetch_and_inc_int( node_data );
|
||||
} else {
|
||||
hash_insert_int(hash , counter_key , 0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Will return 0 if the key is not in the hash.
|
||||
*/
|
||||
|
||||
int hash_get_counter(hash_type * hash , const char * key) {
|
||||
if (hash_has_key( hash , key ))
|
||||
return hash_get_int( hash , key );
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void hash_insert_double(hash_type * hash , const char * key , double value) {
|
||||
node_data_type * node_data = node_data_alloc_double( value );
|
||||
hash_node_type * hash_node = hash_node_alloc_new(key , node_data , hash->hashf , hash->size);
|
||||
__hash_insert_node(hash , hash_node);
|
||||
}
|
||||
|
||||
double hash_get_double(hash_type * hash , const char * key) {
|
||||
node_data_type * node_data = hash_get_node_data(hash , key);
|
||||
return node_data_get_double( node_data );
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
void hash_del(hash_type *hash , const char *key) {
|
||||
__hash_wrlock( hash );
|
||||
hash_del_unlocked__(hash , key);
|
||||
__hash_unlock( hash );
|
||||
}
|
||||
|
||||
/**
|
||||
This function will delete the key if it exists in the hash, but it
|
||||
will NOT fail if the key is not already in the hash table.
|
||||
*/
|
||||
|
||||
void hash_safe_del(hash_type * hash , const char * key) {
|
||||
__hash_wrlock( hash );
|
||||
if (__hash_get_node_unlocked(hash , key , false))
|
||||
hash_del_unlocked__(hash , key);
|
||||
__hash_unlock( hash );
|
||||
}
|
||||
|
||||
|
||||
void hash_clear(hash_type *hash) {
|
||||
__hash_wrlock( hash );
|
||||
{
|
||||
int old_size = hash_get_size(hash);
|
||||
if (old_size > 0) {
|
||||
char **keyList = hash_alloc_keylist__( hash , false);
|
||||
int i;
|
||||
for (i=0; i < old_size; i++) {
|
||||
hash_del_unlocked__(hash , keyList[i]);
|
||||
free(keyList[i]);
|
||||
}
|
||||
free(keyList);
|
||||
}
|
||||
}
|
||||
__hash_unlock( hash );
|
||||
}
|
||||
|
||||
|
||||
void * hash_get(const hash_type *hash , const char *key) {
|
||||
hash_node_type * hash_node = __hash_get_node(hash , key , true);
|
||||
node_data_type * data_node = hash_node_get_data( hash_node );
|
||||
return node_data_get_ptr( data_node );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This function will return NULL if the hash does not
|
||||
contain 'key'.
|
||||
*/
|
||||
void * hash_safe_get( const hash_type * hash , const char * key ) {
|
||||
hash_node_type * hash_node = __hash_get_node(hash , key , false);
|
||||
if (hash_node != NULL) {
|
||||
node_data_type * data_node = hash_node_get_data( hash_node );
|
||||
return node_data_get_ptr( data_node );
|
||||
} else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This function will:
|
||||
|
||||
1. Return an object from the hash table.
|
||||
2. Remove it from the table.
|
||||
|
||||
Observe that if the object has been installed with a destructor,
|
||||
the object will be destroyed by the hash_del() operation, and the
|
||||
return value is complete gibberish - i.e. this function can NOT be
|
||||
used on hash-owned references.
|
||||
*/
|
||||
|
||||
void * hash_pop( hash_type * hash , const char * key) {
|
||||
void * value = hash_get( hash , key);
|
||||
hash_del( hash , key );
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/******************************************************************/
|
||||
/* Allocators */
|
||||
/******************************************************************/
|
||||
|
||||
|
||||
static hash_type * __hash_alloc(int size, double resize_fill , hashf_type *hashf) {
|
||||
hash_type* hash;
|
||||
hash = util_malloc(sizeof *hash );
|
||||
UTIL_TYPE_ID_INIT(hash , HASH_TYPE_ID);
|
||||
hash->size = size;
|
||||
hash->hashf = hashf;
|
||||
hash->table = hash_sll_alloc_table(hash->size);
|
||||
hash->elements = 0;
|
||||
hash->resize_fill = resize_fill;
|
||||
LOCK_INIT( &hash->rwlock );
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
hash_type * hash_alloc() {
|
||||
return __hash_alloc(HASH_DEFAULT_SIZE , 0.50 , hash_index);
|
||||
}
|
||||
|
||||
// Purely a helper in the process of removing the internal locking
|
||||
// in the hash implementation.
|
||||
hash_type * hash_alloc_unlocked() {
|
||||
return __hash_alloc(HASH_DEFAULT_SIZE , 0.50 , hash_index);
|
||||
}
|
||||
|
||||
|
||||
UTIL_SAFE_CAST_FUNCTION( hash , HASH_TYPE_ID)
|
||||
UTIL_IS_INSTANCE_FUNCTION(hash , HASH_TYPE_ID)
|
||||
|
||||
void hash_free(hash_type *hash) {
|
||||
int i;
|
||||
for (i=0; i < hash->size; i++)
|
||||
hash_sll_free(hash->table[i]);
|
||||
free(hash->table);
|
||||
LOCK_DESTROY( &hash->rwlock );
|
||||
free(hash);
|
||||
}
|
||||
|
||||
|
||||
void hash_free__(void * void_hash) {
|
||||
hash_free(hash_safe_cast( void_hash ));
|
||||
}
|
||||
|
||||
|
||||
char ** hash_alloc_keylist(hash_type *hash) {
|
||||
return hash_alloc_keylist__(hash , true);
|
||||
}
|
||||
|
||||
stringlist_type * hash_alloc_stringlist(hash_type * hash) {
|
||||
stringlist_type * stringlist = stringlist_alloc_new();
|
||||
char ** keylist = hash_alloc_keylist__(hash , true);
|
||||
int i;
|
||||
for (i = 0; i < hash_get_size( hash ); i++)
|
||||
stringlist_append_owned_ref( stringlist , keylist[i] );
|
||||
|
||||
free( keylist );
|
||||
return stringlist;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/**
|
||||
The standard functions for inserting an entry in the hash table:
|
||||
|
||||
hash_insert_copy(): The hash table uses copyc() to make a copy of
|
||||
value, which is inserted in the table. This means that the
|
||||
calling scope is free to do whatever it wants with value; and
|
||||
is also responsible for freeing it. The hash_table takes
|
||||
responsibility for freeing it's own copy.
|
||||
|
||||
hash_insert_hash_owned_ref(): The hash table takes ownership of
|
||||
'value', in the sense that the hash table will call the
|
||||
'destructor' del() on value when the node is deleted.
|
||||
|
||||
hash_insert_ref(): The hash table ONLY contains a pointer to
|
||||
value; the calling scope retains full ownership to
|
||||
value. When the hash node is deleted, the hash implementation
|
||||
will just drop the reference on the floor.
|
||||
*/
|
||||
|
||||
|
||||
void hash_insert_copy(hash_type *hash , const char *key , const void *value , copyc_ftype *copyc , free_ftype *del) {
|
||||
hash_node_type *hash_node;
|
||||
if (copyc == NULL || del == NULL)
|
||||
util_abort("%s: must provide copy constructer and delete operator for insert copy - aborting \n",__func__);
|
||||
{
|
||||
node_data_type * data_node = node_data_alloc_ptr( value , copyc , del );
|
||||
hash_node = hash_node_alloc_new(key , data_node , hash->hashf , hash->size);
|
||||
__hash_insert_node(hash , hash_node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This function will insert a reference "value" with key "key"; when
|
||||
the key is deleted - either by an explicit call to hash_del(), or
|
||||
when the complete hash table is free'd with hash_free(), the
|
||||
destructur 'del' is called with 'value' as argument.
|
||||
|
||||
It is importand to realize that when elements are inserted into a
|
||||
hash table with this function the calling scope gives up
|
||||
responsibility of freeing the memory pointed to by value.
|
||||
*/
|
||||
|
||||
void hash_insert_hash_owned_ref(hash_type *hash , const char *key , const void *value , free_ftype *del) {
|
||||
hash_node_type *hash_node;
|
||||
if (del == NULL)
|
||||
util_abort("%s: must provide delete operator for insert hash_owned_ref - aborting \n",__func__);
|
||||
{
|
||||
node_data_type * data_node = node_data_alloc_ptr( value , NULL , del );
|
||||
hash_node = hash_node_alloc_new(key , data_node , hash->hashf , hash->size);
|
||||
__hash_insert_node(hash , hash_node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void hash_insert_ref(hash_type *hash , const char *key , const void *value) {
|
||||
hash_node_type *hash_node;
|
||||
{
|
||||
node_data_type * data_node = node_data_alloc_ptr( value , NULL , NULL);
|
||||
hash_node = hash_node_alloc_new(key , data_node , hash->hashf , hash->size);
|
||||
__hash_insert_node(hash , hash_node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool hash_has_key(const hash_type *hash , const char *key) {
|
||||
if (__hash_get_node(hash , key , false) == NULL)
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int hash_get_size(const hash_type *hash) {
|
||||
return hash->elements;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************/
|
||||
/**
|
||||
Here comes a list of functions for allocating keylists which have
|
||||
been sorted in various ways.
|
||||
*/
|
||||
|
||||
|
||||
static hash_sort_type * hash_alloc_sort_list(const hash_type *hash ,
|
||||
const char **keylist) {
|
||||
|
||||
int i; hash_sort_type * sort_list = calloc(hash_get_size(hash) , sizeof * sort_list);
|
||||
for (i=0; i < hash_get_size(hash); i++)
|
||||
sort_list[i].key = util_alloc_string_copy(keylist[i]);
|
||||
|
||||
return sort_list;
|
||||
}
|
||||
|
||||
static void hash_free_sort_list(const hash_type *hash , hash_sort_type *sort_list) {
|
||||
int i;
|
||||
for (i=0; i < hash_get_size(hash); i++)
|
||||
free(sort_list[i].key);
|
||||
free(sort_list);
|
||||
}
|
||||
|
||||
|
||||
static int hash_sortlist_cmp(const void *_p1 , const void *_p2) {
|
||||
const hash_sort_type *p1 = (const hash_sort_type *) _p1;
|
||||
const hash_sort_type *p2 = (const hash_sort_type *) _p2;
|
||||
|
||||
if (p1->cmp_value == p2->cmp_value)
|
||||
return 0;
|
||||
else if (p1->cmp_value < p2->cmp_value)
|
||||
return -1;
|
||||
else
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static char ** __hash_alloc_ordered_keylist(hash_type *hash , int ( hash_get_cmp_value) (const void * )) {
|
||||
int i;
|
||||
char **sorted_keylist;
|
||||
char **tmp_keylist = hash_alloc_keylist(hash);
|
||||
hash_sort_type * sort_list = hash_alloc_sort_list(hash , (const char **) tmp_keylist);
|
||||
|
||||
for (i = 0; i < hash_get_size(hash); i++)
|
||||
sort_list[i].cmp_value = hash_get_cmp_value( hash_get(hash , sort_list[i].key) );
|
||||
|
||||
qsort(sort_list , hash_get_size(hash) , sizeof *sort_list , &hash_sortlist_cmp);
|
||||
sorted_keylist = calloc(hash_get_size(hash) , sizeof *sorted_keylist);
|
||||
for (i = 0; i < hash_get_size(hash); i++) {
|
||||
sorted_keylist[i] = util_alloc_string_copy(sort_list[i].key);
|
||||
free(tmp_keylist[i]);
|
||||
}
|
||||
free(tmp_keylist);
|
||||
hash_free_sort_list(hash , sort_list);
|
||||
return sorted_keylist;
|
||||
}
|
||||
|
||||
|
||||
char ** hash_alloc_sorted_keylist(hash_type *hash , int ( hash_get_cmp_value) (const void *)) {
|
||||
char ** key_list;
|
||||
|
||||
key_list = __hash_alloc_ordered_keylist(hash , hash_get_cmp_value);
|
||||
|
||||
return key_list;
|
||||
}
|
||||
|
||||
|
||||
static int key_cmp(const void *_s1 , const void *_s2) {
|
||||
const char * s1 = *((const char **) _s1);
|
||||
const char * s2 = *((const char **) _s2);
|
||||
|
||||
return strcmp(s1 , s2);
|
||||
}
|
||||
|
||||
|
||||
static char ** __hash_alloc_key_sorted_list(hash_type *hash, int (*cmp) (const void * , const void *)) {
|
||||
char **keylist = hash_alloc_keylist(hash);
|
||||
|
||||
qsort(keylist , hash_get_size(hash) , sizeof *keylist , cmp);
|
||||
return keylist;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char ** hash_alloc_key_sorted_list(hash_type * hash, int (*cmp) (const void *, const void *))
|
||||
{
|
||||
char ** key_list;
|
||||
|
||||
key_list =__hash_alloc_key_sorted_list(hash , cmp);
|
||||
|
||||
return key_list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool hash_key_list_compare(hash_type * hash1, hash_type * hash2)
|
||||
{
|
||||
bool has_equal_keylist;
|
||||
int i,size1, size2;
|
||||
char **keylist1, **keylist2;
|
||||
|
||||
size1 = hash_get_size(hash1);
|
||||
size2 = hash_get_size(hash2);
|
||||
|
||||
if(size1 != size2) return false;
|
||||
|
||||
keylist1 = hash_alloc_key_sorted_list(hash1, &key_cmp);
|
||||
keylist2 = hash_alloc_key_sorted_list(hash2, &key_cmp);
|
||||
|
||||
has_equal_keylist = true;
|
||||
for(i=0; i<size1; i++)
|
||||
{
|
||||
if(strcmp(keylist1[i],keylist2[i]) != 0)
|
||||
{
|
||||
has_equal_keylist = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(i=0; i<size1; i++) {
|
||||
free( keylist1[i] );
|
||||
free( keylist2[i] );
|
||||
}
|
||||
free( keylist1 );
|
||||
free( keylist2 );
|
||||
return has_equal_keylist;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/**
|
||||
This function will take a list of strings of type:
|
||||
|
||||
["OPT1:Value1" , "MIN:0.0001" , "MAX:1.00" , "FILE:XX"]
|
||||
|
||||
and build a hash table where the element in front of ':' is used as
|
||||
key, and the element behind the ':' is used as value. The value is
|
||||
internalized as a (char *) pointer with no type conversion. In the
|
||||
calling scope the values should be extracted with hash_get().
|
||||
|
||||
Strings which can not be interpreted as KEY:VALUE are simply
|
||||
ignored.
|
||||
*/
|
||||
|
||||
|
||||
hash_type * hash_alloc_from_options(const stringlist_type * options) {
|
||||
int num_options = stringlist_get_size( options );
|
||||
hash_type * opt_hash = hash_alloc();
|
||||
int iopt;
|
||||
|
||||
for (iopt = 0; iopt < num_options; iopt++) {
|
||||
char * option;
|
||||
char * value;
|
||||
|
||||
util_binary_split_string( stringlist_iget(options , iopt) , ":" , true , &option , &value);
|
||||
if ((option != NULL) && (value != NULL))
|
||||
hash_insert_hash_owned_ref( opt_hash , option , util_alloc_string_copy(value) , free);
|
||||
// Warning: could not interpret string as KEY:VALUE - ignored
|
||||
|
||||
|
||||
util_safe_free(option);
|
||||
util_safe_free(value);
|
||||
}
|
||||
|
||||
return opt_hash;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
This is a **VERY** simple iteration object.
|
||||
|
||||
Do **NOT** use with multi-threading.
|
||||
*/
|
||||
struct hash_iter_struct {
|
||||
const hash_type * hash; /* The hash we are iterating over. */
|
||||
char ** keylist; /* The keys in the hash table - at the moment of hash_iter_alloc(). */
|
||||
int num_keys;
|
||||
int current_key_num; /* This integer retains the state. */
|
||||
};
|
||||
|
||||
|
||||
void hash_iter_restart( hash_iter_type * iter ) {
|
||||
iter->current_key_num = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
hash_iter_type * hash_iter_alloc(const hash_type * hash) {
|
||||
hash_iter_type * iter = util_malloc(sizeof * iter );
|
||||
|
||||
iter->hash = hash;
|
||||
iter->num_keys = hash_get_size(hash);
|
||||
iter->keylist = hash_alloc_keylist( (hash_type *) hash);
|
||||
hash_iter_restart( iter );
|
||||
return iter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void hash_iter_free(hash_iter_type * iter) {
|
||||
util_free_stringlist(iter->keylist, iter->num_keys);
|
||||
free(iter);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool hash_iter_is_complete(const hash_iter_type * iter) {
|
||||
if(iter->current_key_num == iter->num_keys)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Get the next key.
|
||||
|
||||
Returns NULL if the iteration has ended.
|
||||
*/
|
||||
const char * hash_iter_get_next_key(hash_iter_type * iter) {
|
||||
const char * key;
|
||||
|
||||
if(iter->current_key_num == iter->num_keys)
|
||||
return NULL;
|
||||
|
||||
key = iter->keylist[iter->current_key_num];
|
||||
iter->current_key_num++;
|
||||
|
||||
if(!hash_has_key(iter->hash, key))
|
||||
util_abort("%s: Programming error. Using hash_iter with multi-threading??\n", __func__);
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void * hash_iter_get_next_value(hash_iter_type * iter) {
|
||||
const char * key = hash_iter_get_next_key(iter);
|
||||
|
||||
if(key != NULL)
|
||||
return hash_get(iter->hash, key);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
/**
|
||||
This function will iterate through the hash table, and call the
|
||||
function func on each of the elements in the table inplace:
|
||||
|
||||
....
|
||||
value = hash_get( hash , key );
|
||||
func( value ); <-- The call is inplace - with no arguments,
|
||||
.... and no return value. The content of 'value'
|
||||
can obviously change, but 'value' itself
|
||||
must still be a valid reference!
|
||||
*/
|
||||
|
||||
|
||||
void hash_apply( hash_type * hash , hash_apply_ftype * func) {
|
||||
hash_iter_type * iter = hash_iter_alloc( hash );
|
||||
while (!hash_iter_is_complete( iter )) {
|
||||
const char * key = hash_iter_get_next_key( iter );
|
||||
void * value = hash_get( hash , key );
|
||||
|
||||
func( value );
|
||||
}
|
||||
hash_iter_free( iter );
|
||||
}
|
||||
|
||||
|
||||
|
||||
#undef HASH_GET_SCALAR
|
||||
#undef HASH_INSERT_SCALAR
|
||||
#undef HASH_INSERT_ARRAY
|
||||
#undef HASH_GET_ARRAY_PTR
|
||||
#undef HASH_NODE_AS
|
||||
#undef HASH_DEFAULT_SIZE
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
85
ThirdParty/Ert/devel/libert_util/src/hash.h
vendored
Normal file
85
ThirdParty/Ert/devel/libert_util/src/hash.h
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'hash.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __HASH_H__
|
||||
#define __HASH_H__
|
||||
#ifdef __cplusplus
|
||||
extern"C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stringlist.h>
|
||||
#include <type_macros.h>
|
||||
typedef struct hash_struct hash_type;
|
||||
typedef struct hash_iter_struct hash_iter_type;
|
||||
typedef void (hash_apply_ftype) (void * );
|
||||
#include <hash_node.h>
|
||||
|
||||
void hash_lock (hash_type * );
|
||||
void hash_unlock(hash_type * );
|
||||
hash_type * hash_alloc();
|
||||
hash_type * hash_alloc_unlocked();
|
||||
hash_type * hash_safe_cast( void * arg);
|
||||
void hash_iter_complete(hash_type * );
|
||||
void hash_free(hash_type *);
|
||||
void hash_free__(void *);
|
||||
void hash_insert_ref(hash_type * , const char * , const void *);
|
||||
void hash_insert_copy(hash_type *, const char * , const void *, copyc_ftype *, free_ftype *);
|
||||
void hash_insert_string(hash_type *, const char *, const char *);
|
||||
bool hash_has_key(const hash_type *, const char *);
|
||||
void * hash_pop( hash_type * hash , const char * key);
|
||||
void * hash_safe_get( const hash_type * hash , const char * key );
|
||||
void * hash_get(const hash_type *, const char *);
|
||||
char * hash_get_string(hash_type * , const char *);
|
||||
void hash_del(hash_type *, const char *);
|
||||
void hash_safe_del(hash_type * , const char * );
|
||||
void hash_clear(hash_type *);
|
||||
int hash_get_size(const hash_type *);
|
||||
void hash_set_keylist(const hash_type * , char **);
|
||||
char ** hash_alloc_keylist(hash_type *);
|
||||
stringlist_type * hash_alloc_stringlist(hash_type * );
|
||||
|
||||
char ** hash_alloc_sorted_keylist (hash_type *hash , int ( hash_get_cmp_value ) (const void *));
|
||||
char ** hash_alloc_key_sorted_list(hash_type *hash, int (*cmp)(const void *, const void *));
|
||||
bool hash_key_list_compare( hash_type * hash1, hash_type * hash2);
|
||||
void hash_insert_hash_owned_ref(hash_type *, const char * , const void *, free_ftype *);
|
||||
void hash_resize(hash_type *hash, int new_size);
|
||||
|
||||
hash_iter_type * hash_iter_alloc(const hash_type *);
|
||||
void hash_iter_free(hash_iter_type *);
|
||||
bool hash_iter_is_complete(const hash_iter_type *);
|
||||
const char * hash_iter_get_next_key(hash_iter_type *);
|
||||
void * hash_iter_get_next_value(hash_iter_type *);
|
||||
void hash_iter_restart( hash_iter_type * iter );
|
||||
|
||||
hash_type * hash_alloc_from_options(const stringlist_type *);
|
||||
|
||||
int hash_inc_counter(hash_type * hash , const char * counter_key);
|
||||
int hash_get_counter(hash_type * hash , const char * key);
|
||||
void hash_insert_int(hash_type * , const char * , int);
|
||||
int hash_get_int(hash_type * , const char *);
|
||||
void hash_insert_double(hash_type * , const char * , double);
|
||||
double hash_get_double(hash_type * , const char *);
|
||||
void hash_apply( hash_type * hash , hash_apply_ftype * func);
|
||||
|
||||
UTIL_IS_INSTANCE_HEADER(hash);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
116
ThirdParty/Ert/devel/libert_util/src/hash_node.c
vendored
Normal file
116
ThirdParty/Ert/devel/libert_util/src/hash_node.c
vendored
Normal file
@ -0,0 +1,116 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'hash_node.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <util.h>
|
||||
#include <stdint.h>
|
||||
#include <node_data.h>
|
||||
#include <hash_node.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hash_node_struct {
|
||||
char *key;
|
||||
uint32_t global_index;
|
||||
uint32_t table_index;
|
||||
node_data_type *data;
|
||||
hash_node_type *next_node;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
bool hash_node_key_eq(const hash_node_type * node , uint32_t global_index , const char *key) {
|
||||
bool eq;
|
||||
eq = true;
|
||||
if (global_index != node->global_index)
|
||||
eq = false;
|
||||
else if (strcmp(node->key , key) != 0)
|
||||
eq = false;
|
||||
return eq;
|
||||
}
|
||||
|
||||
|
||||
uint32_t hash_node_get_table_index(const hash_node_type * node) {
|
||||
return node->table_index;
|
||||
}
|
||||
|
||||
uint32_t hash_node_get_global_index(const hash_node_type * node) {
|
||||
return node->global_index;
|
||||
}
|
||||
|
||||
const char * hash_node_get_key(const hash_node_type * node) {
|
||||
return node->key;
|
||||
}
|
||||
|
||||
hash_node_type * hash_node_get_next(const hash_node_type * node) {
|
||||
return node->next_node;
|
||||
}
|
||||
|
||||
|
||||
void hash_node_set_next(hash_node_type *node , const hash_node_type *next_node) {
|
||||
node->next_node = (hash_node_type *) next_node;
|
||||
}
|
||||
|
||||
|
||||
uint32_t hash_node_set_table_index(hash_node_type *node , uint32_t table_size) {
|
||||
node->table_index = (node->global_index % table_size);
|
||||
return node->table_index;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/* The three functions below here are the only functions accessing the
|
||||
data field of the hash_node.
|
||||
*/
|
||||
|
||||
node_data_type * hash_node_get_data(const hash_node_type * node) {
|
||||
return node->data;
|
||||
}
|
||||
|
||||
|
||||
hash_node_type * hash_node_alloc_new(const char *key, node_data_type * data, hashf_type *hashf , uint32_t table_size) {
|
||||
hash_node_type *node;
|
||||
node = util_malloc(sizeof *node );
|
||||
node->key = util_alloc_string_copy( key );
|
||||
node->data = data;
|
||||
node->next_node = NULL;
|
||||
|
||||
node->global_index = hashf(node->key , strlen(node->key));
|
||||
hash_node_set_table_index(node , table_size);
|
||||
return node;
|
||||
}
|
||||
|
||||
void hash_node_free(hash_node_type * node) {
|
||||
free(node->key);
|
||||
node_data_free( node->data );
|
||||
free(node);
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
49
ThirdParty/Ert/devel/libert_util/src/hash_node.h
vendored
Normal file
49
ThirdParty/Ert/devel/libert_util/src/hash_node.h
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'hash_node.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __HASH_NODE_H__
|
||||
#define __HASH_NODE_H__
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <node_data.h>
|
||||
|
||||
typedef struct hash_node_struct hash_node_type;
|
||||
typedef uint32_t (hashf_type) (const char *key, size_t len);
|
||||
typedef enum {hash_ref_data , hash_int_data , hash_double_data , hash_string_data} hash_data_type;
|
||||
|
||||
|
||||
bool hash_node_key_eq(const hash_node_type * , uint32_t , const char *);
|
||||
hash_node_type * hash_node_get_next(const hash_node_type * );
|
||||
uint32_t hash_node_get_insert_nr(const hash_node_type * );
|
||||
void hash_node_set_next(hash_node_type * , const hash_node_type * );
|
||||
hash_node_type * hash_node_alloc_new(const char *, node_data_type * , hashf_type *, uint32_t);
|
||||
void hash_node_set_insert_nr(hash_node_type *, uint32_t );
|
||||
uint32_t hash_node_get_table_index(const hash_node_type * );
|
||||
uint32_t hash_node_get_global_index(const hash_node_type * );
|
||||
const char * hash_node_get_key(const hash_node_type * );
|
||||
node_data_type * hash_node_get_data(const hash_node_type *);
|
||||
void hash_node_free(hash_node_type *);
|
||||
uint32_t hash_node_set_table_index(hash_node_type *, uint32_t );
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
136
ThirdParty/Ert/devel/libert_util/src/hash_sll.c
vendored
Normal file
136
ThirdParty/Ert/devel/libert_util/src/hash_sll.c
vendored
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'hash_sll.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <hash_node.h>
|
||||
#include <hash_sll.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hash_sll_struct {
|
||||
int length;
|
||||
hash_node_type * head;
|
||||
};
|
||||
|
||||
|
||||
static hash_sll_type * hash_sll_alloc( void ) {
|
||||
hash_sll_type * hash_sll = util_malloc(sizeof * hash_sll );
|
||||
hash_sll->length = 0;
|
||||
hash_sll->head = NULL;
|
||||
return hash_sll;
|
||||
}
|
||||
|
||||
|
||||
hash_sll_type ** hash_sll_alloc_table(int size) {
|
||||
hash_sll_type ** table = util_malloc(size * sizeof * table );
|
||||
int i;
|
||||
for (i=0; i<size; i++)
|
||||
table[i] = hash_sll_alloc();
|
||||
return table;
|
||||
}
|
||||
|
||||
|
||||
void hash_sll_del_node(hash_sll_type *hash_sll , hash_node_type *del_node) {
|
||||
if (del_node == NULL)
|
||||
util_abort("%s: tried to delete NULL node - aborting \n",__func__);
|
||||
|
||||
{
|
||||
hash_node_type *node, *p_node;
|
||||
p_node = NULL;
|
||||
node = hash_sll->head;
|
||||
while (node != NULL && node != del_node) {
|
||||
p_node = node;
|
||||
node = hash_node_get_next(node);
|
||||
}
|
||||
|
||||
if (node == del_node) {
|
||||
if (p_node == NULL)
|
||||
/*
|
||||
We are attempting to delete the first element in the list.
|
||||
*/
|
||||
hash_sll->head = hash_node_get_next(del_node);
|
||||
else
|
||||
hash_node_set_next(p_node , hash_node_get_next(del_node));
|
||||
hash_node_free(del_node);
|
||||
hash_sll->length--;
|
||||
} else
|
||||
util_abort("%s: tried to delete node not in list - aborting \n",__func__);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
hash_node_type * hash_sll_get_head(const hash_sll_type *hash_sll) { return hash_sll->head; }
|
||||
|
||||
void hash_sll_add_node(hash_sll_type *hash_sll , hash_node_type *new_node) {
|
||||
hash_node_set_next(new_node, hash_sll->head);
|
||||
hash_sll->head = new_node;
|
||||
hash_sll->length++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void hash_sll_free(hash_sll_type *hash_sll) {
|
||||
if (hash_sll->head != NULL) {
|
||||
hash_node_type *node , *next_node;
|
||||
node = hash_sll->head;
|
||||
while (node != NULL) {
|
||||
next_node = hash_node_get_next(node);
|
||||
hash_node_free(node);
|
||||
node = next_node;
|
||||
}
|
||||
}
|
||||
free( hash_sll );
|
||||
}
|
||||
|
||||
|
||||
bool hash_sll_empty(const hash_sll_type * hash_sll) {
|
||||
if (hash_sll->length == 0)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
hash_node_type * hash_sll_get(const hash_sll_type *hash_sll, uint32_t global_index , const char *key) {
|
||||
hash_node_type * node = hash_sll->head;
|
||||
|
||||
while ((node != NULL) && (!hash_node_key_eq(node , global_index , key)))
|
||||
node = hash_node_get_next(node);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
bool hash_sll_has_key(const hash_sll_type *hash_sll , uint32_t global_index , const char *key) {
|
||||
if (hash_sll_get(hash_sll , global_index , key))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
41
ThirdParty/Ert/devel/libert_util/src/hash_sll.h
vendored
Normal file
41
ThirdParty/Ert/devel/libert_util/src/hash_sll.h
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'hash_sll.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __HASH_SLL_H__
|
||||
#define __HASH_SLL_H__
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <hash_node.h>
|
||||
|
||||
typedef struct hash_sll_struct hash_sll_type;
|
||||
|
||||
hash_sll_type **hash_sll_alloc_table(int );
|
||||
/*hash_sll_type * hash_sll_alloc(void);*/
|
||||
void hash_sll_del_node(hash_sll_type * , hash_node_type *);
|
||||
void hash_sll_add_node(hash_sll_type *, hash_node_type *);
|
||||
void hash_sll_free(hash_sll_type *);
|
||||
bool hash_sll_has_key(const hash_sll_type *, uint32_t , const char *);
|
||||
bool hash_sll_empty(const hash_sll_type * hash_sll);
|
||||
hash_node_type * hash_sll_get(const hash_sll_type *, uint32_t , const char *);
|
||||
hash_node_type * hash_sll_get_head(const hash_sll_type *);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
448
ThirdParty/Ert/devel/libert_util/src/lars.c
vendored
Normal file
448
ThirdParty/Ert/devel/libert_util/src/lars.c
vendored
Normal file
@ -0,0 +1,448 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'lars.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <int_vector.h>
|
||||
#include <matrix.h>
|
||||
#include <matrix_blas.h>
|
||||
#include <matrix_lapack.h>
|
||||
#include <regression.h>
|
||||
#include <lars.h>
|
||||
|
||||
#define LARS_TYPE_ID 77125439
|
||||
|
||||
struct lars_struct {
|
||||
UTIL_TYPE_ID_DECLARATION;
|
||||
matrix_type * X;
|
||||
matrix_type * Y;
|
||||
bool data_owner;
|
||||
double Y0;
|
||||
matrix_type * beta0;
|
||||
|
||||
matrix_type * beta;
|
||||
matrix_type * X_norm;
|
||||
matrix_type * X_mean;
|
||||
double Y_mean;
|
||||
};
|
||||
|
||||
|
||||
|
||||
static lars_type * lars_alloc__() {
|
||||
lars_type * lars = util_malloc( sizeof * lars );
|
||||
UTIL_TYPE_ID_INIT( lars , LARS_TYPE_ID );
|
||||
lars->X = NULL;
|
||||
lars->Y = NULL;
|
||||
|
||||
lars->X_norm = NULL;
|
||||
lars->X_mean = NULL;
|
||||
lars->beta = NULL;
|
||||
lars->beta0 = NULL;
|
||||
lars->Y = 0;
|
||||
|
||||
return lars;
|
||||
}
|
||||
|
||||
lars_type * lars_alloc1( int nsample , int nvars) {
|
||||
lars_type * lars = lars_alloc__();
|
||||
lars->X = matrix_alloc( nsample , nvars );
|
||||
lars->Y = matrix_alloc( nsample , 1 );
|
||||
lars->data_owner = true;
|
||||
return lars;
|
||||
}
|
||||
|
||||
|
||||
lars_type * lars_alloc2( matrix_type * X , matrix_type * Y , bool internal_copy ) {
|
||||
lars_type * lars = lars_alloc__();
|
||||
if (internal_copy) {
|
||||
lars->X = matrix_alloc_copy( X );
|
||||
lars->Y = matrix_alloc_copy( Y );
|
||||
lars->data_owner = true;
|
||||
} else {
|
||||
lars->X = X;
|
||||
lars->Y = Y;
|
||||
lars->data_owner = false;
|
||||
}
|
||||
return lars;
|
||||
}
|
||||
|
||||
int lars_get_sample( const lars_type * lars ) {
|
||||
return matrix_get_rows( lars->X );
|
||||
}
|
||||
|
||||
|
||||
int lars_get_nvar( const lars_type * lars ) {
|
||||
return matrix_get_columns( lars->X );
|
||||
}
|
||||
|
||||
|
||||
void lars_isetX( lars_type * lars, int sample, int var , double value) {
|
||||
matrix_iset( lars->X , sample , var , value );
|
||||
}
|
||||
|
||||
void lars_isetY( lars_type * lars, int sample, double value) {
|
||||
matrix_iset( lars->Y , sample , 0 , value );
|
||||
}
|
||||
|
||||
#define MATRIX_SAFE_FREE( m ) if (m != NULL) matrix_free( m )
|
||||
void lars_free( lars_type * lars ) {
|
||||
|
||||
if (lars->data_owner) {
|
||||
MATRIX_SAFE_FREE( lars->X );
|
||||
MATRIX_SAFE_FREE( lars->Y );
|
||||
}
|
||||
MATRIX_SAFE_FREE( lars->X_norm );
|
||||
MATRIX_SAFE_FREE( lars->X_mean );
|
||||
MATRIX_SAFE_FREE( lars->beta );
|
||||
MATRIX_SAFE_FREE( lars->beta0 );
|
||||
|
||||
free( lars );
|
||||
}
|
||||
#undef MATRIX_SAFE_FREE
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
static double sgn(double x) {
|
||||
return copysign(1 , x); // C99
|
||||
}
|
||||
|
||||
|
||||
static void lars_estimate_init( lars_type * lars, matrix_type * X , matrix_type * Y) {
|
||||
int nvar = matrix_get_columns( lars->X );
|
||||
|
||||
matrix_assign( X , lars->X );
|
||||
matrix_assign( Y , lars->Y );
|
||||
if (lars->X_norm != NULL)
|
||||
matrix_free( lars->X_norm );
|
||||
lars->X_norm = matrix_alloc(1 , nvar );
|
||||
|
||||
if (lars->X_mean != NULL)
|
||||
matrix_free( lars->X_mean );
|
||||
lars->X_mean = matrix_alloc(1 , nvar );
|
||||
|
||||
if (lars->beta != NULL)
|
||||
matrix_free( lars->beta );
|
||||
lars->beta = matrix_alloc( nvar , nvar );
|
||||
lars->Y_mean = regression_scale( X , Y , lars->X_mean , lars->X_norm);
|
||||
}
|
||||
|
||||
|
||||
double lars_getY0( const lars_type * lars) {
|
||||
return lars->Y0;
|
||||
}
|
||||
|
||||
double lars_iget_beta( const lars_type * lars , int index) {
|
||||
return matrix_iget( lars->beta0 , index , 0 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void lars_select_beta( lars_type * lars , int beta_index) {
|
||||
int nvars = matrix_get_rows( lars->beta );
|
||||
if (lars->beta0 == NULL)
|
||||
lars->beta0 = matrix_alloc( nvars , 1 );
|
||||
{
|
||||
matrix_type * beta_vector = matrix_alloc( nvars , 1 );
|
||||
matrix_copy_column( beta_vector , lars->beta , 0 , beta_index );
|
||||
lars->Y0 = regression_unscale( beta_vector , lars->X_norm , lars->X_mean , lars->Y_mean , lars->beta0 );
|
||||
matrix_free( beta_vector );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
The algorithm can very briefly be summarized as:
|
||||
|
||||
1. Determine the covariate with greatest correlation to the data and
|
||||
add this covariate to the model. In the current implementation the
|
||||
correlations are stored in the matrix @C and the greatest
|
||||
correlation is stored in the scalar variable @maxC.
|
||||
|
||||
2. Determine an update direction in the set of active covariates
|
||||
which is equiangular to all covariates.
|
||||
|
||||
3. Determine the step length @gamma - which is the exact step length
|
||||
before a new covariate will enter the active set at the current
|
||||
correlation level.
|
||||
|
||||
4. Update the beta estimate and the current 'location' mu.
|
||||
*/
|
||||
|
||||
void lars_estimate(lars_type * lars , int max_vars , double max_beta , bool verbose) {
|
||||
int nvars = matrix_get_columns( lars->X );
|
||||
int nsample = matrix_get_rows( lars->X );
|
||||
matrix_type * X = matrix_alloc( nsample, nvars ); // Allocate local X and Y variables
|
||||
matrix_type * Y = matrix_alloc( nsample, 1 ); // which will hold the normalized data
|
||||
lars_estimate_init( lars , X , Y); // during the estimation process.
|
||||
{
|
||||
matrix_type * G = matrix_alloc_gram( X , true );
|
||||
matrix_type * mu = matrix_alloc( nsample , 1 );
|
||||
matrix_type * C = matrix_alloc( nvars , 1 );
|
||||
matrix_type * Y_mu = matrix_alloc_copy( Y );
|
||||
int_vector_type * active_set = int_vector_alloc(0,0);
|
||||
int_vector_type * inactive_set = int_vector_alloc(0,0);
|
||||
int active_size;
|
||||
|
||||
|
||||
if ((max_vars <= 0) || (max_vars > nvars))
|
||||
max_vars = nvars;
|
||||
|
||||
{
|
||||
int i;
|
||||
for (i=0; i < nvars; i++)
|
||||
int_vector_iset( inactive_set , i , i );
|
||||
}
|
||||
matrix_set( mu , 0 );
|
||||
|
||||
while (true) {
|
||||
double maxC = 0;
|
||||
|
||||
/*
|
||||
The first step is to calculate the correlations between the
|
||||
covariates, and the current residual. All the currently inactive
|
||||
covariates are searched; the covariate with the greatest
|
||||
correlation with (Y - mu) is selected and added to the active set.
|
||||
*/
|
||||
matrix_sub( Y_mu , Y , mu ); // Y_mu = Y - mu
|
||||
matrix_dgemm( C , X , Y_mu , true , false , 1.0 , 0); // C = X' * Y_mu
|
||||
{
|
||||
int i;
|
||||
int max_set_index = 0;
|
||||
|
||||
for (i=0; i < int_vector_size( inactive_set ); i++) {
|
||||
int set_index = i;
|
||||
int var_index = int_vector_iget( inactive_set , set_index );
|
||||
double value = fabs( matrix_iget(C , var_index , 0) );
|
||||
if (value > maxC) {
|
||||
maxC = value;
|
||||
max_set_index = set_index;
|
||||
}
|
||||
}
|
||||
/*
|
||||
Remove element corresponding to max_set_index from the
|
||||
inactive set and add it to the active set:
|
||||
*/
|
||||
int_vector_append( active_set , int_vector_idel( inactive_set , max_set_index ));
|
||||
}
|
||||
active_size = int_vector_size( active_set );
|
||||
/*
|
||||
Now we have calculated the correlations between all the
|
||||
covariates and the current residual @Y_mu. The correlations are
|
||||
stored in the matrix @C. The value of the maximum correlation is
|
||||
stored in @maxC.
|
||||
|
||||
Based on the value of @maxC we have added one new covariate to
|
||||
the model, technically by moving the index from @inactive_set to
|
||||
@active_set.
|
||||
*/
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
{
|
||||
matrix_type * weights = matrix_alloc( active_size , 1);
|
||||
double scale;
|
||||
|
||||
/*****************************************************************/
|
||||
/* This scope should compute and initialize the variables
|
||||
@weights and @scale. */
|
||||
{
|
||||
matrix_type * subG = matrix_alloc( active_size , active_size );
|
||||
matrix_type * STS = matrix_alloc( active_size , active_size );
|
||||
matrix_type * sign_vector = matrix_alloc( active_size , 1);
|
||||
int i , j;
|
||||
|
||||
/*
|
||||
STS = S' o S where 'o' is the Schur product and S is given
|
||||
by:
|
||||
|
||||
[ s1 s2 s3 s4 ]
|
||||
S = [ s1 s2 s3 s4 ]
|
||||
[ s1 s2 s3 s4 ]
|
||||
[ s1 s2 s3 s4 ]
|
||||
|
||||
Where si is the sign of the correlation between (active)
|
||||
variable 'i' and Y_mu.
|
||||
*/
|
||||
|
||||
|
||||
for (i=0; i < active_size ; i++) {
|
||||
int vari = int_vector_iget( active_set , i );
|
||||
double signi = sgn( matrix_iget( C , vari , 0));
|
||||
matrix_iset( sign_vector , i , 0 , signi );
|
||||
for (j=0; j < active_size; j++) {
|
||||
int varj = int_vector_iget( active_set , j );
|
||||
double signj = sgn( matrix_iget( C , varj , 0));
|
||||
|
||||
matrix_iset( STS , i , j , signi * signj );
|
||||
}
|
||||
}
|
||||
|
||||
// Extract the elements from G corresponding to active indices and
|
||||
// copy to the matrix subG:
|
||||
for (i=0; i < active_size ; i++) {
|
||||
int ii = int_vector_iget( active_set , i );
|
||||
for (j=0; j < active_size; j++) {
|
||||
int jj = int_vector_iget( active_set , j );
|
||||
|
||||
matrix_iset( subG , i , j , matrix_iget(G , ii , jj));
|
||||
}
|
||||
}
|
||||
|
||||
// Weights
|
||||
matrix_inplace_mul( subG , STS );
|
||||
matrix_inv( subG );
|
||||
|
||||
{
|
||||
matrix_type * ones = matrix_alloc( active_size , 1 );
|
||||
matrix_type * GA1 = matrix_alloc( active_size , 1 );
|
||||
|
||||
matrix_set( ones , 1.0 );
|
||||
matrix_matmul( GA1 , subG , ones );
|
||||
scale = 1.0 / sqrt( matrix_get_column_sum( GA1 , 0 ));
|
||||
|
||||
matrix_mul( weights , GA1 , sign_vector );
|
||||
matrix_scale( weights , scale );
|
||||
|
||||
matrix_free( GA1 );
|
||||
matrix_free( ones );
|
||||
}
|
||||
|
||||
matrix_free( sign_vector );
|
||||
matrix_free( subG );
|
||||
matrix_free( STS );
|
||||
}
|
||||
|
||||
/******************************************************************/
|
||||
/* The variables weight and scale have been calculated, proceed
|
||||
to calculate the step length @gamma. */
|
||||
{
|
||||
int i;
|
||||
double gamma;
|
||||
|
||||
{
|
||||
matrix_type * u = matrix_alloc( nsample , 1 );
|
||||
int j;
|
||||
|
||||
for (i=0; i < nsample; i++) {
|
||||
double row_sum = 0;
|
||||
for (j =0; j < active_size; j++)
|
||||
row_sum += matrix_iget( X , i , int_vector_iget( active_set , j)) * matrix_iget(weights , j , 0 );
|
||||
|
||||
matrix_iset( u , i , 0 , row_sum );
|
||||
}
|
||||
|
||||
gamma = maxC / scale;
|
||||
if (active_size < matrix_get_columns( X )) {
|
||||
matrix_type * equi_corr = matrix_alloc( nvars , 1 );
|
||||
matrix_dgemm( equi_corr , X , u , true , false , 1.0 , 0); // equi_corr = X'·u
|
||||
for (i=0; i < (nvars - active_size); i++) {
|
||||
int var_index = int_vector_iget( inactive_set , i );
|
||||
double gamma1 = (maxC - matrix_iget(C , var_index , 0 )) / ( scale - matrix_iget( equi_corr , var_index , 0));
|
||||
double gamma2 = (maxC + matrix_iget(C , var_index , 0 )) / ( scale + matrix_iget( equi_corr , var_index , 0));
|
||||
|
||||
if ((gamma1 > 0) && (gamma1 < gamma))
|
||||
gamma = gamma1;
|
||||
|
||||
if ((gamma2 > 0) && (gamma2 < gamma))
|
||||
gamma = gamma2;
|
||||
|
||||
}
|
||||
matrix_free( equi_corr );
|
||||
}
|
||||
/* Update the current estimated 'location' mu. */
|
||||
matrix_scale( u , gamma );
|
||||
matrix_inplace_add( mu , u );
|
||||
matrix_free( u );
|
||||
}
|
||||
|
||||
/*
|
||||
We have calculated the step length @gamma, and the @weights. Update the @beta matrix.
|
||||
*/
|
||||
for (i=0; i < active_size; i++)
|
||||
matrix_iset( lars->beta , int_vector_iget( active_set , i ) , active_size - 1 , gamma * matrix_iget( weights , i , 0));
|
||||
|
||||
if (active_size > 1)
|
||||
for (i=0; i < nvars; i++)
|
||||
matrix_iadd( lars->beta , i , active_size - 1 , matrix_iget( lars->beta , i , active_size - 2));
|
||||
|
||||
matrix_free( weights );
|
||||
}
|
||||
}
|
||||
|
||||
if (active_size == max_vars)
|
||||
break;
|
||||
|
||||
if (max_beta > 0) {
|
||||
double beta_norm2 = matrix_get_column_abssum( lars->beta , active_size - 1 );
|
||||
if (beta_norm2 > max_beta) {
|
||||
// We stop - we will use an interpolation between this beta estimate and
|
||||
// the previous, to ensure that the |beta| = max_beta criteria is satisfied.
|
||||
if (active_size >= 2) {
|
||||
double beta_norm1 = matrix_get_column_abssum( lars->beta , active_size - 2 );
|
||||
double s = (max_beta - beta_norm1)/(beta_norm2 - beta_norm1);
|
||||
{
|
||||
int j;
|
||||
for (j=0; j < nvars; j++) {
|
||||
double beta1 = matrix_iget( lars->beta , j , active_size - 2 );
|
||||
double beta2 = matrix_iget( lars->beta , j , active_size - 1 );
|
||||
matrix_iset( lars->beta , j , active_size - 1 , beta1 + s*(beta2 - beta1));
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
matrix_free( G );
|
||||
matrix_free( mu );
|
||||
matrix_free( C );
|
||||
matrix_free( Y_mu );
|
||||
int_vector_free( active_set );
|
||||
int_vector_free( inactive_set );
|
||||
matrix_resize( lars->beta , nvars , active_size , true );
|
||||
if (verbose)
|
||||
matrix_pretty_fprint( lars->beta , "beta" , "%12.5f" , stdout );
|
||||
lars_select_beta( lars , active_size - 1);
|
||||
}
|
||||
matrix_free( X );
|
||||
matrix_free( Y );
|
||||
}
|
||||
|
||||
|
||||
double lars_eval1( const lars_type * lars , const matrix_type * x) {
|
||||
return lars->Y0 + matrix_row_column_dot_product( x , 0 , lars->beta0 , 0 );
|
||||
}
|
||||
|
||||
|
||||
double lars_eval2( const lars_type * lars , double * x) {
|
||||
matrix_type * x_view = matrix_alloc_view( x , 1 , matrix_get_columns( lars->X_mean ));
|
||||
double y = lars_eval1( lars , x_view );
|
||||
matrix_free( x_view );
|
||||
return y;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
49
ThirdParty/Ert/devel/libert_util/src/lars.h
vendored
Normal file
49
ThirdParty/Ert/devel/libert_util/src/lars.h
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'lars.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __LARS_H__
|
||||
#define __LARS_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <matrix.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct lars_struct lars_type;
|
||||
|
||||
int lars_get_sample( const lars_type * lars );
|
||||
int lars_get_nvar( const lars_type * lars );
|
||||
lars_type * lars_alloc1( int nsample , int nvars);
|
||||
lars_type * lars_alloc2( matrix_type * X , matrix_type * Y , bool internal_copy );
|
||||
void lars_estimate(lars_type * lars , int max_vars , double max_beta , bool verbose);
|
||||
void lars_isetX( lars_type * lars, int sample, int var , double value);
|
||||
void lars_isetY( lars_type * lars, int sample, double value);
|
||||
void lars_select_beta( lars_type * lars , int beta_index);
|
||||
void lars_free( lars_type * lars );
|
||||
double lars_eval1( const lars_type * lars , const matrix_type * x);
|
||||
double lars_eval2( const lars_type * lars , double * x);
|
||||
double lars_getY0( const lars_type * lars);
|
||||
double lars_iget_beta( const lars_type * lars , int index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
384
ThirdParty/Ert/devel/libert_util/src/latex.c
vendored
Normal file
384
ThirdParty/Ert/devel/libert_util/src/latex.c
vendored
Normal file
@ -0,0 +1,384 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'latex.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <latex.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef LATEX_CMD
|
||||
#define __LATEX_CMD LATEX_CMD
|
||||
#else
|
||||
#define __LATEX_CMD "/usr/bin/pdflatex"
|
||||
#endif
|
||||
|
||||
|
||||
#define TARGET_EXTENSION "pdf"
|
||||
#define RUNPATH_FMT "/tmp/latex-XXXXXX"
|
||||
#define DEFAULT_TIMEOUT 60
|
||||
|
||||
static const char* delete_extensions[] = { "log",
|
||||
"aux",
|
||||
"nav",
|
||||
"out",
|
||||
"snm",
|
||||
"toc" };
|
||||
|
||||
struct latex_struct {
|
||||
char * latex_cmd;
|
||||
int timeout; // Seconds to wait for the compilation to complete.
|
||||
bool in_place;
|
||||
|
||||
char * run_path;
|
||||
char * src_file;
|
||||
char * result_file;
|
||||
|
||||
char * basename;
|
||||
char * target_extension;
|
||||
char * target_file;
|
||||
char * target_path;
|
||||
};
|
||||
|
||||
|
||||
|
||||
void latex_set_command( latex_type * latex , const char * latex_cmd) {
|
||||
latex->latex_cmd = util_realloc_string_copy( latex->latex_cmd , latex_cmd );
|
||||
}
|
||||
|
||||
static void latex_set_target_extension( latex_type * latex , const char * target_extension) {
|
||||
latex->target_extension = util_realloc_string_copy( latex->target_extension , target_extension );
|
||||
}
|
||||
|
||||
static void latex_set_target_path( latex_type * latex , const char * target_path) {
|
||||
latex->target_path = util_realloc_string_copy( latex->target_path , target_path );
|
||||
}
|
||||
|
||||
|
||||
void latex_set_target_file( latex_type * latex , const char * target_file ) {
|
||||
latex->target_file = util_realloc_string_copy( latex->target_file , target_file );
|
||||
{
|
||||
char * full_path = util_alloc_abs_path( target_file );
|
||||
free( latex->target_path );
|
||||
util_safe_free( latex->target_extension );
|
||||
|
||||
util_alloc_file_components( full_path , &latex->target_path , NULL , &latex->target_extension );
|
||||
free( full_path );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
latex_type * latex_alloc( const char * input_file , bool in_place ) {
|
||||
char * input_path;
|
||||
char * input_extension;
|
||||
latex_type * latex = util_malloc( sizeof * latex );
|
||||
|
||||
latex->in_place = in_place;
|
||||
util_alloc_file_components( input_file , &input_path , &latex->basename , &input_extension );
|
||||
if (input_path == NULL)
|
||||
input_path = util_alloc_cwd( );
|
||||
else {
|
||||
char * tmp = input_path;
|
||||
input_path = util_alloc_abs_path( tmp );
|
||||
free( tmp );
|
||||
}
|
||||
|
||||
if (!util_string_equal( input_extension , LATEX_EXTENSION ))
|
||||
util_abort("%s: sorry - the input file must have extension .tex \n",__func__);
|
||||
|
||||
if (in_place)
|
||||
latex->run_path = util_alloc_abs_path( input_path );
|
||||
else {
|
||||
latex->run_path = util_alloc_string_copy( RUNPATH_FMT );
|
||||
if (mkdtemp( latex->run_path ) == NULL)
|
||||
util_abort("%s: failed to create temporary directory with template:%s\n",__func__ , RUNPATH_FMT);
|
||||
{
|
||||
char * src_file = util_alloc_filename( latex->run_path , latex->basename , LATEX_EXTENSION );
|
||||
util_copy_file( input_file , src_file );
|
||||
free( src_file );
|
||||
}
|
||||
latex_link_directory_content( latex , input_path );
|
||||
}
|
||||
latex->src_file = util_alloc_filename( NULL , latex->basename , LATEX_EXTENSION );
|
||||
/*
|
||||
At this stage the following path variables should be correctly set:
|
||||
|
||||
latex->basename : The basename of the file we are compiling.
|
||||
|
||||
latex->src_file : The name of the file we are compiling; with a .tex extension,
|
||||
but without any leading path part.
|
||||
|
||||
latex->run_path : The directory where the compilation will be performed.
|
||||
*/
|
||||
|
||||
/*
|
||||
Setting various default values.
|
||||
*/
|
||||
latex->target_file = NULL;
|
||||
latex->latex_cmd = NULL;
|
||||
latex->target_extension = NULL;
|
||||
latex->target_path = NULL;
|
||||
latex->result_file = NULL;
|
||||
|
||||
latex_set_target_extension( latex , TARGET_EXTENSION );
|
||||
latex_set_target_path( latex , input_path );
|
||||
latex_set_command( latex , __LATEX_CMD );
|
||||
latex_set_timeout( latex , DEFAULT_TIMEOUT );
|
||||
|
||||
free( input_extension );
|
||||
free( input_path );
|
||||
return latex;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Whether the run was successfull or not is only checked by looking
|
||||
at the mtime of the result file. If this is greater than the time
|
||||
of the compilation start the compilation is deemed a success.
|
||||
*/
|
||||
|
||||
static bool latex_inspect_run( latex_type * latex , time_t compile_start ) {
|
||||
bool OK = false;
|
||||
|
||||
{
|
||||
char * result_file = util_alloc_filename( latex->run_path , latex->basename , latex->target_extension );
|
||||
if (util_file_exists( result_file )) {
|
||||
if (!util_file_older( result_file , compile_start ))
|
||||
OK = true;
|
||||
}
|
||||
free( result_file );
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
static void latex_copy_target( latex_type * latex ) {
|
||||
if (!util_same_file(latex->target_file , latex->result_file))
|
||||
util_copy_file( latex->result_file , latex->target_file );
|
||||
}
|
||||
|
||||
|
||||
static void latex_cleanup( latex_type * latex ) {
|
||||
if (latex->in_place) {
|
||||
int num_extensions = sizeof( delete_extensions ) / sizeof( delete_extensions[0] );
|
||||
for (int iext = 0; iext < num_extensions; iext++) {
|
||||
char * filename = util_alloc_filename( latex->run_path , latex->basename , delete_extensions[iext]);
|
||||
unlink(filename);
|
||||
free( filename );
|
||||
}
|
||||
} else
|
||||
util_clear_directory(latex->run_path , true , true );
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void latex_ensure_target_file( latex_type * latex) {
|
||||
if (latex->target_file == NULL)
|
||||
latex->target_file = util_alloc_filename( latex->target_path , latex->basename , latex->target_extension);
|
||||
}
|
||||
|
||||
static bool latex_compile__( latex_type * latex , bool ignore_errors) {
|
||||
bool normal_exit = true;
|
||||
char ** argv;
|
||||
int argc;
|
||||
char * stderr_file = util_alloc_filename( latex->run_path , "latex" , "stderr");
|
||||
char * stdout_file = util_alloc_filename( latex->run_path , "latex" , "stdout");
|
||||
int usleep_time = 500000; /* 1/2 second. */
|
||||
|
||||
argc = 2;
|
||||
argv = util_malloc( argc * sizeof * argv );
|
||||
if (ignore_errors)
|
||||
argv[0] = "-interaction=nonstopmode";
|
||||
else
|
||||
argv[0] = "-halt-on-error";
|
||||
argv[0] = "-halt-on-error";//latex->src_file;
|
||||
|
||||
argv[1] = latex->src_file;
|
||||
{
|
||||
pid_t child_pid = util_fork_exec( latex->latex_cmd , argc , (const char **) argv , false , NULL , latex->run_path , NULL , stdout_file , stderr_file );
|
||||
double total_wait = 0;
|
||||
int status;
|
||||
|
||||
while (true) {
|
||||
if (waitpid(child_pid , &status , WNOHANG) == 0) {
|
||||
util_usleep( usleep_time );
|
||||
total_wait += usleep_time / 1000000.0;
|
||||
|
||||
if (total_wait > latex->timeout) {
|
||||
// Exit due to excessive time usage.
|
||||
normal_exit = false;
|
||||
kill( child_pid , SIGKILL );
|
||||
}
|
||||
} else
|
||||
// The child has exited - succesfull or not?
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free( stderr_file );
|
||||
free( stdout_file );
|
||||
free( argv );
|
||||
|
||||
return normal_exit;
|
||||
}
|
||||
|
||||
/**
|
||||
Unfortunately it is very difficult to determine whether a latex
|
||||
compilation has been successfull or not.
|
||||
*/
|
||||
|
||||
bool latex_compile( latex_type * latex , bool ignore_errors , bool with_xref) {
|
||||
int num_compile = 1;
|
||||
time_t compile_start;
|
||||
|
||||
time( &compile_start );
|
||||
latex_ensure_target_file( latex );
|
||||
latex->result_file = util_alloc_filename( latex->run_path , latex->basename , latex->target_extension );
|
||||
|
||||
/* If we are compiling out of place we might have created a symlink
|
||||
from the compile path to an old version of the target file. */
|
||||
if (util_is_link(latex->result_file))
|
||||
unlink( latex->result_file );
|
||||
|
||||
if (with_xref)
|
||||
num_compile = 2;
|
||||
|
||||
{
|
||||
bool normal_exit = true;
|
||||
int compile_num = 0;
|
||||
while ((normal_exit) && (compile_num < num_compile)) {
|
||||
normal_exit = latex_compile__( latex , ignore_errors );
|
||||
compile_num++;
|
||||
}
|
||||
|
||||
/*
|
||||
OK - the compilation is finished; currently we do not have a clue
|
||||
whether it was a success or not.
|
||||
*/
|
||||
|
||||
{
|
||||
bool success = false;
|
||||
if (normal_exit)
|
||||
success = latex_inspect_run( latex , compile_start );
|
||||
|
||||
if (success) {
|
||||
latex_copy_target( latex );
|
||||
latex_cleanup( latex );
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int latex_get_timeout( const latex_type * latex ) {
|
||||
return latex->timeout;
|
||||
}
|
||||
|
||||
void latex_set_timeout( latex_type * latex , int timeout) {
|
||||
latex->timeout = timeout;
|
||||
}
|
||||
|
||||
|
||||
void latex_free( latex_type * latex ) {
|
||||
|
||||
free( latex->latex_cmd );
|
||||
free( latex->run_path );
|
||||
free( latex->src_file );
|
||||
free( latex->basename );
|
||||
free( latex->target_extension );
|
||||
free( latex->target_path );
|
||||
|
||||
util_safe_free( latex->target_file );
|
||||
util_safe_free( latex->result_file );
|
||||
|
||||
free( latex );
|
||||
}
|
||||
|
||||
|
||||
const char * latex_get_runpath( const latex_type * latex ) {
|
||||
return latex->run_path;
|
||||
}
|
||||
|
||||
const char * latex_get_target_file( const latex_type * latex ) {
|
||||
return latex->target_file;
|
||||
}
|
||||
|
||||
|
||||
void latex_link_path( const latex_type * latex , const char * path) {
|
||||
char * target_name;
|
||||
{
|
||||
char * tmp_copy = util_alloc_string_copy( path );
|
||||
target_name = util_alloc_string_copy( basename( tmp_copy ));
|
||||
free( tmp_copy );
|
||||
}
|
||||
{
|
||||
char * link = util_alloc_filename( latex->run_path , target_name , NULL);
|
||||
char * target = util_alloc_abs_path( path );
|
||||
|
||||
if (!util_entry_exists( link ))
|
||||
util_make_slink( target , link );
|
||||
|
||||
free( link );
|
||||
free( target );
|
||||
}
|
||||
util_safe_free( target_name );
|
||||
}
|
||||
|
||||
|
||||
void latex_link_directory_content(const latex_type * latex , const char * path) {
|
||||
if (util_is_directory( path )) {
|
||||
DIR * dirH = opendir( path );
|
||||
struct dirent * dentry;
|
||||
|
||||
while ( true ) {
|
||||
dentry = readdir( dirH );
|
||||
if (dentry != NULL) {
|
||||
if ((strcmp(dentry->d_name , ".") != 0) && (strcmp(dentry->d_name , "..") != 0)) {
|
||||
char * full_path = util_alloc_filename( path , dentry->d_name , NULL );
|
||||
|
||||
latex_link_path( latex , full_path );
|
||||
|
||||
free( full_path );
|
||||
}
|
||||
} else
|
||||
break;
|
||||
}
|
||||
closedir( dirH );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
50
ThirdParty/Ert/devel/libert_util/src/latex.h
vendored
Normal file
50
ThirdParty/Ert/devel/libert_util/src/latex.h
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Copyright (C) 2012 Statoil ASA, Norway.
|
||||
|
||||
The file 'latex.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __LATEX_H__
|
||||
#define __LATEX_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#define LATEX_EXTENSION "tex"
|
||||
|
||||
typedef struct latex_struct latex_type;
|
||||
|
||||
|
||||
latex_type * latex_alloc( const char * input_file , bool in_place);
|
||||
void latex_free( latex_type * latex );
|
||||
void latex_set_target_file( latex_type * latex , const char * target_file );
|
||||
bool latex_compile( latex_type * latex , bool ignore_errors , bool with_xref);
|
||||
void latex_set_timeout( latex_type * latex , int timeout);
|
||||
int latex_get_timeout( const latex_type * latex );
|
||||
const char * latex_get_runpath( const latex_type * latex );
|
||||
const char * latex_get_target_file( const latex_type * latex );
|
||||
|
||||
void latex_link_path( const latex_type * latex , const char * path);
|
||||
void latex_link_directory_content(const latex_type * latex , const char * path);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
220
ThirdParty/Ert/devel/libert_util/src/log.c
vendored
Normal file
220
ThirdParty/Ert/devel/libert_util/src/log.c
vendored
Normal file
@ -0,0 +1,220 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'log.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#ifdef HAVE_FSYNC
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#ifdef HAVE_PTHREAD
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
#include <util.h>
|
||||
#include <log.h>
|
||||
|
||||
struct log_struct {
|
||||
char * filename;
|
||||
FILE * stream;
|
||||
int fd;
|
||||
int log_level;
|
||||
#ifdef HAVE_PTHREAD
|
||||
pthread_mutex_t mutex;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
|
||||
void log_reset_filename(log_type *logh , const char *filename) {
|
||||
if (logh->stream != NULL) { /* Close the existing file descriptor. */
|
||||
size_t file_size;
|
||||
fclose( logh->stream );
|
||||
file_size = util_file_size( logh->filename );
|
||||
if (file_size == 0)
|
||||
remove( logh->filename ); /* Unlink the old log file if it had zero size. */
|
||||
}
|
||||
|
||||
logh->filename = util_realloc_string_copy( logh->filename , filename );
|
||||
#ifdef HAVE_PTHREAD
|
||||
pthread_mutex_lock( &logh->mutex );
|
||||
#endif
|
||||
|
||||
if (filename != NULL) {
|
||||
logh->stream = util_mkdir_fopen( filename , "a+");
|
||||
logh->fd = fileno( logh->stream );
|
||||
} else { /* It is ~OK to open a log with NULL filename, but then
|
||||
log_reset_filename() with a VALID filename must be
|
||||
called before it is actually used. */
|
||||
logh->stream = NULL;
|
||||
logh->fd = -1;
|
||||
}
|
||||
|
||||
#ifdef HAVE_PTHREAD
|
||||
pthread_mutex_unlock( &logh->mutex );
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
const char * log_get_filename( const log_type * logh ) {
|
||||
return logh->filename;
|
||||
}
|
||||
|
||||
int log_get_level( const log_type * logh) {
|
||||
return logh->log_level;
|
||||
}
|
||||
|
||||
|
||||
void log_set_level( log_type * logh , int log_level) {
|
||||
logh->log_level = log_level;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static log_type *log_alloc_internal(const char *filename , bool new, int log_level) {
|
||||
log_type *logh;
|
||||
logh = util_malloc(sizeof *logh );
|
||||
|
||||
logh->log_level = log_level;
|
||||
logh->filename = NULL;
|
||||
logh->stream = NULL;
|
||||
#ifdef HAVE_PTHREAD
|
||||
pthread_mutex_init( &logh->mutex , NULL );
|
||||
#endif
|
||||
log_reset_filename( logh ,filename );
|
||||
|
||||
return logh;
|
||||
}
|
||||
|
||||
|
||||
log_type * log_alloc_new(const char *filename, int log_level) {
|
||||
log_type *logh = log_alloc_internal(filename , true , log_level);
|
||||
return logh;
|
||||
}
|
||||
|
||||
|
||||
|
||||
log_type *log_alloc_existing(const char *filename, int log_level) {
|
||||
return log_alloc_internal(filename , false , log_level);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static bool log_include_message(const log_type *logh , int message_level) {
|
||||
if (message_level <= logh->log_level)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
If dup_stream != NULL the message (without the date/time header) is duplicated on this stream.
|
||||
*/
|
||||
void log_add_message(log_type *logh, int message_level , FILE * dup_stream , char* message, bool free_message) {
|
||||
if (logh->stream == NULL)
|
||||
util_abort("%s: logh->stream == NULL - must call log_reset_filename() first \n",__func__);
|
||||
|
||||
#ifdef HAVE_PTHREAD
|
||||
pthread_mutex_lock( &logh->mutex );
|
||||
#endif
|
||||
{
|
||||
struct tm time_fields;
|
||||
time_t epoch_time;
|
||||
|
||||
if (log_include_message(logh,message_level)) {
|
||||
time(&epoch_time);
|
||||
util_localtime(&epoch_time , &time_fields);
|
||||
|
||||
if (message != NULL)
|
||||
fprintf(logh->stream,"%02d/%02d - %02d:%02d:%02d %s\n",time_fields.tm_mday, time_fields.tm_mon + 1, time_fields.tm_hour , time_fields.tm_min , time_fields.tm_sec , message);
|
||||
else
|
||||
fprintf(logh->stream,"%02d/%02d - %02d:%02d:%02d \n",time_fields.tm_mday, time_fields.tm_mon + 1, time_fields.tm_hour , time_fields.tm_min , time_fields.tm_sec);
|
||||
|
||||
/** We duplicate the message to the stream 'dup_stream'. */
|
||||
if ((dup_stream != NULL) && (message != NULL))
|
||||
fprintf(dup_stream , "%s\n", message);
|
||||
|
||||
log_sync( logh );
|
||||
}
|
||||
}
|
||||
#ifdef HAVE_PTHREAD
|
||||
pthread_mutex_unlock( &logh->mutex );
|
||||
#endif
|
||||
if (free_message)
|
||||
free( message );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void log_add_fmt_message(log_type * logh , int message_level , FILE * dup_stream , const char * fmt , ...) {
|
||||
if (log_include_message(logh,message_level)) {
|
||||
char * message;
|
||||
va_list ap;
|
||||
va_start(ap , fmt);
|
||||
message = util_alloc_sprintf_va( fmt , ap );
|
||||
log_add_message( logh , message_level , dup_stream , message , true);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This function can be used to get low level to the FILE pointer of
|
||||
the stream. To 'ensure' that the data actually hits the disk
|
||||
you should call log_sync() after writing.
|
||||
|
||||
It is your responsabiulity to avoid racing++ when using the
|
||||
log_get_stream() function.
|
||||
*/
|
||||
|
||||
FILE * log_get_stream(log_type * logh ) {
|
||||
return logh->stream;
|
||||
}
|
||||
|
||||
|
||||
void log_sync(log_type * logh) {
|
||||
#ifdef HAVE_FSYNC
|
||||
fsync( logh->fd );
|
||||
#endif
|
||||
fseek( logh->stream , 0 , SEEK_END );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void log_close( log_type * logh ) {
|
||||
if ((logh->stream != stdout) && (logh->stream != stderr))
|
||||
fclose( logh->stream ); /* This closes BOTH the FILE * stream and the integer file descriptor. */
|
||||
|
||||
free( logh->filename );
|
||||
free( logh );
|
||||
}
|
50
ThirdParty/Ert/devel/libert_util/src/log.h
vendored
Normal file
50
ThirdParty/Ert/devel/libert_util/src/log.h
vendored
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'log.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __LOG_H__
|
||||
#define __LOG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct log_struct log_type;
|
||||
|
||||
FILE * log_get_stream(log_type * logh );
|
||||
void log_reset_filename( log_type * logh , const char * filename );
|
||||
void log_set_file(log_type * , const char *);
|
||||
log_type * log_alloc_new(const char *filename, int log_level);
|
||||
log_type * log_alloc_existing(const char *filename, int log_level);
|
||||
void log_add_message(log_type *logh, int message_level , FILE * dup_stream , char* message, bool free_message);
|
||||
void log_add_fmt_message(log_type * logh , int message_level , FILE * dup_stream , const char * fmt , ...);
|
||||
int log_get_level( const log_type * logh);
|
||||
void log_set_level( log_type * logh , int new_level);
|
||||
void log_close( log_type * logh );
|
||||
void log_sync(log_type * logh);
|
||||
const char * log_get_filename( const log_type * logh );
|
||||
int log_get_level( const log_type * logh);
|
||||
void log_set_level( log_type * logh , int log_level);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
176
ThirdParty/Ert/devel/libert_util/src/lookup_table.c
vendored
Normal file
176
ThirdParty/Ert/devel/libert_util/src/lookup_table.c
vendored
Normal file
@ -0,0 +1,176 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'lookup_table.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <double_vector.h>
|
||||
#include <stdbool.h>
|
||||
#include <util.h>
|
||||
#include <lookup_table.h>
|
||||
|
||||
struct lookup_table_struct {
|
||||
bool data_owner;
|
||||
double_vector_type * x_vector;
|
||||
double_vector_type * y_vector;
|
||||
const double * x_data;
|
||||
const double * y_data;
|
||||
int size;
|
||||
double xmin,ymin;
|
||||
double xmax,ymax;
|
||||
int prev_index;
|
||||
bool sorted;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
static void lookup_table_sort_data( lookup_table_type * lt) {
|
||||
if (double_vector_size( lt->x_vector ) > 0) {
|
||||
if (double_vector_get_read_only( lt->x_vector ))
|
||||
if (!double_vector_is_sorted( lt->x_vector , false))
|
||||
util_abort("%s: x vector is not sorted and read-only - this will not fly\n",__func__);
|
||||
|
||||
{
|
||||
int * sort_perm = double_vector_alloc_sort_perm( lt->x_vector );
|
||||
double_vector_permute( lt->x_vector , sort_perm );
|
||||
double_vector_permute( lt->y_vector , sort_perm );
|
||||
free( sort_perm );
|
||||
}
|
||||
lt->ymax = double_vector_get_max( lt->y_vector );
|
||||
lt->ymin = double_vector_get_min( lt->y_vector );
|
||||
lt->xmin = double_vector_get_min( lt->x_vector );
|
||||
lt->xmax = double_vector_get_max( lt->x_vector );
|
||||
lt->size = double_vector_size( lt->x_vector);
|
||||
lt->prev_index = -1;
|
||||
lt->x_data = double_vector_get_const_ptr( lt->x_vector );
|
||||
lt->y_data = double_vector_get_const_ptr( lt->y_vector );
|
||||
}
|
||||
lt->sorted = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
IFF the @read_only flag is set to true; the x vector MUST be
|
||||
sorted.
|
||||
*/
|
||||
|
||||
void lookup_table_set_data( lookup_table_type * lt , double_vector_type * x , double_vector_type * y , bool data_owner ) {
|
||||
|
||||
if (lt->data_owner) {
|
||||
double_vector_free( lt->x_vector );
|
||||
double_vector_free( lt->y_vector );
|
||||
}
|
||||
|
||||
lt->x_vector = x;
|
||||
lt->y_vector = y;
|
||||
lt->data_owner = data_owner;
|
||||
lt->sorted = false;
|
||||
lookup_table_sort_data( lt );
|
||||
}
|
||||
|
||||
|
||||
|
||||
lookup_table_type * lookup_table_alloc( double_vector_type * x , double_vector_type * y , bool data_owner) {
|
||||
lookup_table_type * lt = util_malloc( sizeof * lt);
|
||||
lt->data_owner = false;
|
||||
if ((x == NULL) && (y == NULL)) {
|
||||
x = double_vector_alloc(0 , 0);
|
||||
y = double_vector_alloc(0 , 0);
|
||||
data_owner = true;
|
||||
}
|
||||
lookup_table_set_data( lt , x , y , false );
|
||||
lt->data_owner = data_owner;
|
||||
|
||||
return lt;
|
||||
}
|
||||
|
||||
lookup_table_type * lookup_table_alloc_empty( ) {
|
||||
return lookup_table_alloc( NULL , NULL , true );
|
||||
}
|
||||
|
||||
|
||||
int lookup_table_get_size( const lookup_table_type * lt ) {
|
||||
return double_vector_size( lt->x_vector );
|
||||
}
|
||||
|
||||
|
||||
void lookup_table_append( lookup_table_type * lt , double x , double y) {
|
||||
double_vector_append( lt->x_vector , x );
|
||||
double_vector_append( lt->y_vector , y );
|
||||
lt->sorted = false;
|
||||
}
|
||||
|
||||
|
||||
void lookup_table_free( lookup_table_type * lt ) {
|
||||
if (lt->data_owner) {
|
||||
double_vector_free( lt->x_vector );
|
||||
double_vector_free( lt->y_vector );
|
||||
}
|
||||
free( lt );
|
||||
}
|
||||
|
||||
|
||||
static void lookup_table_assert_sorted( lookup_table_type * lt) {
|
||||
if (!lt->sorted)
|
||||
lookup_table_sort_data( lt );
|
||||
}
|
||||
|
||||
|
||||
double lookup_table_interp( lookup_table_type * lt , double x) {
|
||||
lookup_table_assert_sorted( lt );
|
||||
{
|
||||
if ((x >= lt->xmin) && (x < lt->xmax)) {
|
||||
int index = double_vector_lookup_bin__( lt->x_vector , x , lt->prev_index );
|
||||
{
|
||||
double x1 = lt->x_data[ index ];
|
||||
double x2 = lt->x_data[ index + 1];
|
||||
double y1 = lt->y_data[ index ];
|
||||
double y2 = lt->y_data[ index + 1];
|
||||
|
||||
lt->prev_index = index;
|
||||
return (( x - x1 ) * y2 + (x2 - x) * y1) / (x2 - x1 );
|
||||
}
|
||||
} else {
|
||||
util_abort("%s: out of bounds \n",__func__);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
double lookup_table_get_max_value( lookup_table_type * lookup_table ) {
|
||||
lookup_table_assert_sorted( lookup_table );
|
||||
return lookup_table->ymax;
|
||||
}
|
||||
|
||||
|
||||
double lookup_table_get_min_value( lookup_table_type * lookup_table ) {
|
||||
lookup_table_assert_sorted( lookup_table );
|
||||
return lookup_table->ymax;
|
||||
}
|
||||
|
||||
|
||||
double lookup_table_get_max_arg( lookup_table_type * lookup_table ) {
|
||||
lookup_table_assert_sorted( lookup_table );
|
||||
return lookup_table->xmax;
|
||||
}
|
||||
|
||||
|
||||
double lookup_table_get_min_arg( lookup_table_type * lookup_table ) {
|
||||
lookup_table_assert_sorted( lookup_table );
|
||||
return lookup_table->xmin;
|
||||
}
|
46
ThirdParty/Ert/devel/libert_util/src/lookup_table.h
vendored
Normal file
46
ThirdParty/Ert/devel/libert_util/src/lookup_table.h
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'lookup_table.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __LOOKUP_TABLE_H__
|
||||
#define __LOOKUP_TABLE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <double_vector.h>
|
||||
|
||||
typedef struct lookup_table_struct lookup_table_type;
|
||||
|
||||
|
||||
void lookup_table_set_data( lookup_table_type * lt , double_vector_type * x , double_vector_type * y , bool data_owner );
|
||||
lookup_table_type * lookup_table_alloc( double_vector_type * x , double_vector_type * y , bool data_owner);
|
||||
lookup_table_type * lookup_table_alloc_empty();
|
||||
void lookup_table_append( lookup_table_type * lt , double x , double y);
|
||||
void lookup_table_free( lookup_table_type * lt );
|
||||
double lookup_table_interp( lookup_table_type * lt , double x);
|
||||
double lookup_table_get_max_value( lookup_table_type * lookup_table );
|
||||
double lookup_table_get_min_value( lookup_table_type * lookup_table );
|
||||
double lookup_table_get_max_arg( lookup_table_type * lookup_table );
|
||||
double lookup_table_get_max_arg( lookup_table_type * lookup_table );
|
||||
int lookup_table_get_size( const lookup_table_type * lt );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
1521
ThirdParty/Ert/devel/libert_util/src/matrix.c
vendored
Normal file
1521
ThirdParty/Ert/devel/libert_util/src/matrix.c
vendored
Normal file
File diff suppressed because it is too large
Load Diff
146
ThirdParty/Ert/devel/libert_util/src/matrix.h
vendored
Normal file
146
ThirdParty/Ert/devel/libert_util/src/matrix.h
vendored
Normal file
@ -0,0 +1,146 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'matrix.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __MATRIX_H__
|
||||
#define __MATRIX_H__
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <rng.h>
|
||||
#include <type_macros.h>
|
||||
#ifdef HAVE_THREAD_POOL
|
||||
#include <thread_pool.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct matrix_struct matrix_type;
|
||||
|
||||
void matrix_fscanf_data( matrix_type * matrix , bool row_major_order , FILE * stream );
|
||||
void matrix_fprintf( const matrix_type * matrix , const char * fmt , FILE * stream );
|
||||
void matrix_pretty_fprint(const matrix_type * matrix , const char * name , const char * fmt , FILE * stream);
|
||||
matrix_type * matrix_alloc(int rows, int columns);
|
||||
matrix_type * matrix_safe_alloc(int rows, int columns);
|
||||
bool matrix_resize(matrix_type * matrix , int rows , int columns , bool copy_content);
|
||||
bool matrix_safe_resize(matrix_type * matrix , int rows , int columns , bool copy_content);
|
||||
matrix_type * matrix_alloc_copy(const matrix_type * src);
|
||||
matrix_type * matrix_safe_alloc_copy(const matrix_type * src);
|
||||
matrix_type * matrix_realloc_copy(matrix_type * T , const matrix_type * src);
|
||||
|
||||
matrix_type * matrix_alloc_shared(const matrix_type * src , int row , int column , int rows , int columns);
|
||||
void matrix_free(matrix_type * matrix);
|
||||
void matrix_safe_free( matrix_type * matrix );
|
||||
void matrix_pretty_print(const matrix_type * matrix , const char * name , const char * fmt);
|
||||
void matrix_set(matrix_type * matrix, double value);
|
||||
void matrix_set_name( matrix_type * matrix , const char * name);
|
||||
void matrix_scale(matrix_type * matrix, double value);
|
||||
void matrix_shift(matrix_type * matrix, double value);
|
||||
|
||||
void matrix_assign(matrix_type * A , const matrix_type * B);
|
||||
void matrix_inplace_add(matrix_type * A , const matrix_type * B);
|
||||
void matrix_inplace_sub(matrix_type * A , const matrix_type * B);
|
||||
void matrix_inplace_mul(matrix_type * A , const matrix_type * B);
|
||||
void matrix_inplace_div(matrix_type * A , const matrix_type * B);
|
||||
void matrix_sub(matrix_type * A , const matrix_type * B , const matrix_type * C);
|
||||
void matrix_mul( matrix_type * A , const matrix_type * B , const matrix_type * C);
|
||||
void matrix_transpose(const matrix_type * A , matrix_type * T);
|
||||
|
||||
void matrix_iset_safe(matrix_type * matrix , int i , int j, double value);
|
||||
void matrix_iset(matrix_type * matrix , int i , int j, double value);
|
||||
double matrix_iget(const matrix_type * matrix , int i , int j);
|
||||
double matrix_iget_safe(const matrix_type * matrix , int i , int j);
|
||||
void matrix_iadd(matrix_type * matrix , int i , int j , double value);
|
||||
void matrix_isub(matrix_type * matrix , int i , int j , double value);
|
||||
void matrix_imul(matrix_type * matrix , int i , int j , double value);
|
||||
|
||||
|
||||
void matrix_inplace_matmul(matrix_type * A, const matrix_type * B);
|
||||
void matrix_inplace_matmul_mt1(matrix_type * A, const matrix_type * B , int num_threads);
|
||||
#ifdef HAVE_THREAD_POOL
|
||||
void matrix_inplace_matmul_mt2(matrix_type * A, const matrix_type * B , thread_pool_type * thread_pool);
|
||||
#endif
|
||||
|
||||
void matrix_shift_column(matrix_type * matrix , int column, double shift);
|
||||
void matrix_shift_row(matrix_type * matrix , int row , double shift);
|
||||
double matrix_get_column_sum(const matrix_type * matrix , int column);
|
||||
double matrix_get_row_sum(const matrix_type * matrix , int column);
|
||||
double matrix_get_column_sum2(const matrix_type * matrix , int column);
|
||||
double matrix_get_row_abssum(const matrix_type * matrix , int row);
|
||||
double matrix_get_column_abssum(const matrix_type * matrix , int column);
|
||||
double matrix_get_row_sum2(const matrix_type * matrix , int column);
|
||||
void matrix_subtract_row_mean(matrix_type * matrix);
|
||||
void matrix_subtract_and_store_row_mean(matrix_type * matrix, matrix_type * row_mean);
|
||||
void matrix_scale_column(matrix_type * matrix , int column , double scale_factor);
|
||||
void matrix_scale_row(matrix_type * matrix , int row , double scale_factor);
|
||||
void matrix_set_const_column(matrix_type * matrix , const double value , int column);
|
||||
void matrix_copy_column(matrix_type * target_matrix, const matrix_type * src_matrix , int src_column, int target_column);
|
||||
void matrix_set_const_row(matrix_type * matrix , const double value , int row);
|
||||
|
||||
double * matrix_get_data(const matrix_type * matrix);
|
||||
double matrix_orthonormality( const matrix_type * matrix );
|
||||
|
||||
matrix_type * matrix_alloc_steal_data(int rows , int columns , double * data , int data_size);
|
||||
void matrix_set_column(matrix_type * matrix , const double * data , int column);
|
||||
void matrix_set_many_on_column(matrix_type * matrix , int row_offset , int elements , const double * data , int column);
|
||||
void matrix_ensure_rows(matrix_type * matrix, int rows, bool copy_content);
|
||||
void matrix_shrink_header(matrix_type * matrix , int rows , int columns);
|
||||
void matrix_full_size( matrix_type * matrix );
|
||||
int matrix_get_rows(const matrix_type * matrix);
|
||||
int matrix_get_columns(const matrix_type * matrix);
|
||||
int matrix_get_row_stride(const matrix_type * matrix);
|
||||
int matrix_get_column_stride(const matrix_type * matrix);
|
||||
void matrix_get_dims(const matrix_type * matrix , int * rows , int * columns , int * row_stride , int * column_stride);
|
||||
bool matrix_is_quadratic(const matrix_type * matrix);
|
||||
bool matrix_equal( const matrix_type * m1 , const matrix_type * m2);
|
||||
|
||||
void matrix_diag_set_scalar(matrix_type * matrix , double value);
|
||||
void matrix_diag_set(matrix_type * matrix , const double * diag);
|
||||
void matrix_random_init(matrix_type * matrix , rng_type * rng);
|
||||
void matrix_matlab_dump(const matrix_type * matrix, const char * filename);
|
||||
|
||||
void matrix_imul_col( matrix_type * matrix , int column , double factor);
|
||||
double matrix_column_column_dot_product(const matrix_type * m1 , int col1 , const matrix_type * m2 , int col2);
|
||||
double matrix_row_column_dot_product(const matrix_type * m1 , int row1 , const matrix_type * m2 , int col2);
|
||||
matrix_type * matrix_alloc_view(double * data , int rows , int columns);
|
||||
matrix_type * matrix_alloc_transpose( const matrix_type * A);
|
||||
void matrix_copy_row(matrix_type * target_matrix, const matrix_type * src_matrix , int target_row, int src_row);
|
||||
void matrix_copy_block( matrix_type * target_matrix , int target_row , int target_column , int rows , int columns,
|
||||
const matrix_type * src_matrix , int src_row , int src_column);
|
||||
|
||||
void matrix_scalar_set( matrix_type * matrix , double value);
|
||||
void matrix_inplace_diag_sqrt(matrix_type *Cd);
|
||||
void matrix_create_identiy(int n,matrix_type *Id);
|
||||
double matrix_trace(const matrix_type *matrix);
|
||||
double matrix_diag_std(const matrix_type * Sk,double mean);
|
||||
double matrix_det3( const matrix_type * A);
|
||||
double matrix_det4( const matrix_type * A);
|
||||
|
||||
#ifdef HAVE_ISFINITE
|
||||
bool matrix_is_finite(const matrix_type * matrix);
|
||||
void matrix_assert_finite( const matrix_type * matrix );
|
||||
#endif
|
||||
|
||||
UTIL_SAFE_CAST_HEADER( matrix );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
263
ThirdParty/Ert/devel/libert_util/src/matrix_blas.c
vendored
Normal file
263
ThirdParty/Ert/devel/libert_util/src/matrix_blas.c
vendored
Normal file
@ -0,0 +1,263 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'matrix_blas.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <matrix.h>
|
||||
#include <matrix_blas.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*****************************************************************/
|
||||
void dgemm_(char * , char * , int * , int * , int * , double * , double * , int * , double * , int * , double * , double * , int *);
|
||||
void dgemv_(char * , int * , int * , double * , double * , int * , const double * , int * , double * , double * , int * );
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
y = alpha * op(A)*x + beta * y
|
||||
|
||||
alpha,beta: scalars
|
||||
x,y : vectors
|
||||
A : matrix
|
||||
|
||||
|
||||
x and y are entered as (double * ).
|
||||
*/
|
||||
|
||||
|
||||
void matrix_dgemv(const matrix_type * A , const double *x , double * y, bool transA , double alpha , double beta) {
|
||||
int m = matrix_get_rows( A );
|
||||
int n = matrix_get_columns( A );
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int incx = 1;
|
||||
int incy = 1;
|
||||
|
||||
char transA_c;
|
||||
if (transA)
|
||||
transA_c = 'T';
|
||||
else
|
||||
transA_c = 'N';
|
||||
|
||||
dgemv_(&transA_c , &m , &n , &alpha , matrix_get_data( A ) , &lda , x , &incx , &beta , y , &incy);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
y = A*x
|
||||
*/
|
||||
|
||||
void matrix_mul_vector(const matrix_type * A , const double * x , double * y) {
|
||||
matrix_dgemv(A , x , y , false , 1 , 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void dgemm_debug(const matrix_type *C , const matrix_type *A , const matrix_type * B , bool transA, bool transB) {
|
||||
printf("\nC = [%d , %d]\n",matrix_get_rows( C ) , matrix_get_columns(C));
|
||||
|
||||
printf("A: [%d , %d]", matrix_get_rows( A ) , matrix_get_columns(A));
|
||||
if (transA)
|
||||
printf("^T");
|
||||
|
||||
printf("\nB: [%d , %d]", matrix_get_rows( B ) , matrix_get_columns(B));
|
||||
if (transB)
|
||||
printf("^T");
|
||||
|
||||
printf("\n\n");
|
||||
printf("[%d ,%d] = ",matrix_get_rows( C ) , matrix_get_columns(C));
|
||||
if (transA)
|
||||
printf("[%d ,%d] x ",matrix_get_rows( A ) , matrix_get_columns(A));
|
||||
else
|
||||
printf("[%d ,%d] x ",matrix_get_columns( A ) , matrix_get_rows(A));
|
||||
|
||||
|
||||
if (transB)
|
||||
printf("[%d ,%d]\n",matrix_get_rows( B ) , matrix_get_columns(B));
|
||||
else
|
||||
printf("[%d ,%d]\n",matrix_get_columns( B ) , matrix_get_rows(B));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
C = alpha * op(A) * op(B) + beta * C
|
||||
|
||||
op(·) can either be unity or Transpose.
|
||||
*/
|
||||
|
||||
void matrix_dgemm(matrix_type *C , const matrix_type *A , const matrix_type * B , bool transA, bool transB , double alpha , double beta) {
|
||||
int m = matrix_get_rows( C );
|
||||
int n = matrix_get_columns( C );
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int ldb = matrix_get_column_stride( B );
|
||||
int ldc = matrix_get_column_stride( C );
|
||||
char transA_c;
|
||||
char transB_c;
|
||||
int k , innerA, innerB , outerA , outerB;
|
||||
|
||||
if (transA)
|
||||
k = matrix_get_rows( A );
|
||||
else
|
||||
k = matrix_get_columns( A );
|
||||
|
||||
|
||||
if (transA) {
|
||||
innerA = matrix_get_rows(A);
|
||||
outerA = matrix_get_columns(A);
|
||||
transA_c = 'T';
|
||||
} else {
|
||||
innerA = matrix_get_columns(A);
|
||||
outerA = matrix_get_rows(A);
|
||||
transA_c = 'N';
|
||||
}
|
||||
|
||||
|
||||
if (transB) {
|
||||
innerB = matrix_get_columns( B );
|
||||
outerB = matrix_get_rows( B );
|
||||
transB_c = 'T';
|
||||
} else {
|
||||
transB_c = 'N';
|
||||
innerB = matrix_get_rows( B );
|
||||
outerB = matrix_get_columns( B );
|
||||
}
|
||||
|
||||
/*
|
||||
This is the dimension check which must pass:
|
||||
|
||||
--------------------------------------------------
|
||||
A | B | Columns(A) = Rows(B)
|
||||
Trans(A) | Trans(B) | Rows(A) = Columns(B)
|
||||
A | Trans(B) | Columns(A) = Columns(B)
|
||||
Trans(A) | B | Rows(A) = Rows(B)
|
||||
--------------------------------------------------
|
||||
|
||||
--------------------------------------------------
|
||||
A | Rows(A) = Rows(C)
|
||||
Trans(A) | Columns(A) = Rows(C)
|
||||
B | Columns(B) = Columns(C)
|
||||
Trans(B) | Rows(B) = Columns(B)
|
||||
--------------------------------------------------
|
||||
|
||||
*/
|
||||
|
||||
if (innerA != innerB) {
|
||||
dgemm_debug(C,A,B,transA , transB);
|
||||
util_abort("%s: matrix size mismatch between A and B \n", __func__);
|
||||
}
|
||||
|
||||
|
||||
if (outerA != matrix_get_rows( C )) {
|
||||
dgemm_debug(C,A,B,transA , transB);
|
||||
printf("outerA:%d rows(C):%d \n",outerA , matrix_get_rows( C ));
|
||||
util_abort("%s: matrix size mismatch between A and C \n",__func__);
|
||||
}
|
||||
|
||||
|
||||
if (outerB != matrix_get_columns( C )) {
|
||||
dgemm_debug(C,A,B,transA , transB);
|
||||
util_abort("%s: matrix size mismatch between B and C \n",__func__);
|
||||
}
|
||||
|
||||
|
||||
dgemm_(&transA_c , &transB_c , &m ,&n , &k , &alpha , matrix_get_data( A ) , &lda , matrix_get_data( B ) , &ldb , &beta , matrix_get_data( C ) , &ldc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
This function does a general matrix multiply of A * B, and stores
|
||||
the result in C.
|
||||
*/
|
||||
|
||||
void matrix_matmul(matrix_type * C, const matrix_type * A , const matrix_type * B) {
|
||||
matrix_dgemm( C , A , B , false , false , 1 , 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Allocates new matrix C = A·B
|
||||
*/
|
||||
|
||||
matrix_type * matrix_alloc_matmul(const matrix_type * A, const matrix_type * B) {
|
||||
matrix_type * C = matrix_alloc( matrix_get_rows( A ) , matrix_get_columns( B ));
|
||||
matrix_matmul( C , A , B );
|
||||
return C;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/**
|
||||
Will calculate the Gram matrix: G = X'*X
|
||||
*/
|
||||
|
||||
void matrix_gram_set( const matrix_type * X , matrix_type * G, bool col) {
|
||||
int G_rows = matrix_get_rows( G );
|
||||
int G_cols = matrix_get_columns( G );
|
||||
int X_rows = matrix_get_rows( X );
|
||||
int X_cols = matrix_get_columns( X );
|
||||
if (col) {
|
||||
// Calculate X' · X
|
||||
if ((G_rows == G_cols) && (X_cols == G_rows))
|
||||
matrix_dgemm( G , X , X , true , false , 1 , 0);
|
||||
else
|
||||
util_abort("%s: dimension mismatch \n",__func__);
|
||||
} else {
|
||||
// Calculate X · X'
|
||||
if ((G_rows == G_cols) && (X_rows == G_rows))
|
||||
matrix_dgemm( G , X , X , false , true , 1 , 0);
|
||||
else
|
||||
util_abort("%s: dimension mismatch \n",__func__);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
If col == true: G = X' · X
|
||||
col == false: G = X · X'
|
||||
*/
|
||||
|
||||
|
||||
matrix_type * matrix_alloc_gram( const matrix_type * X , bool col) {
|
||||
int X_rows = matrix_get_rows( X );
|
||||
int X_columns = matrix_get_columns( X );
|
||||
matrix_type * G;
|
||||
|
||||
if (col)
|
||||
G = matrix_alloc( X_columns , X_columns );
|
||||
else
|
||||
G = matrix_alloc( X_rows , X_rows );
|
||||
|
||||
matrix_gram_set( X , G , col);
|
||||
return G;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
40
ThirdParty/Ert/devel/libert_util/src/matrix_blas.h
vendored
Normal file
40
ThirdParty/Ert/devel/libert_util/src/matrix_blas.h
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'matrix_blas.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <matrix.h>
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
void matrix_dgemm(matrix_type *C , const matrix_type *A , const matrix_type * B , bool transA, bool transB , double alpha , double beta);
|
||||
void matrix_matmul(matrix_type * A, const matrix_type *B , const matrix_type * C);
|
||||
matrix_type * matrix_alloc_matmul(const matrix_type * A, const matrix_type * B);
|
||||
void matrix_dgemv(const matrix_type * A , const double * x , double * y , bool transA , double alpha , double beta);
|
||||
void matrix_mul_vector(const matrix_type * A , const double * x , double * y);
|
||||
void matrix_gram_set( const matrix_type * X , matrix_type * G, bool col);
|
||||
matrix_type * matrix_alloc_gram( const matrix_type * X , bool col);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
566
ThirdParty/Ert/devel/libert_util/src/matrix_lapack.c
vendored
Normal file
566
ThirdParty/Ert/devel/libert_util/src/matrix_lapack.c
vendored
Normal file
@ -0,0 +1,566 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'matrix_lapack.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <matrix.h>
|
||||
#include <matrix_lapack.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
The external lapack routines
|
||||
*/
|
||||
/*****************************************************************/
|
||||
void dgesv_(int * n, int * nrhs , double * A , int * lda , long int * ipivot , double * B , int * ldb , int * info);
|
||||
void dgesvd_(char * jobu , char * jobvt , int * m , int * n , double * A, int * lda , double * S , double * U , int * ldu , double * VT , int * ldvt, double * work , int * worksize , int * info);
|
||||
void dsyevx_(char * jobz, char * range , char * uplo , int *n , double * A , int * lda , double * vl , double * vu , int * il , int * iu , double * abstol , int * m , double * w , double *z , int * ldz , double * work, int * lwork , int * iwork , int * ifail , int * info);
|
||||
void dgeqrf_(int * m , int * n , double * A , int * lda , double * tau , double * work , int * lwork, int * info);
|
||||
void dorgqr_(int * m, int * n , int * k , double * A , int * lda , double * tau , double * work , int * lwork, int * info);
|
||||
void dgetrf_(int * M , int * n , double * A , int * lda , int * ipiv, int * info);
|
||||
void dgedi_(double * A , int * lda , int * n , int * ipiv , double * det , double * work , int * job);
|
||||
void dgetri_( int * n , double * A , int * lda , int * ipiv , double * work , int * work_size , int * info);
|
||||
/*****************************************************************/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This file implements (very thin) interfaces for the matrix_type to
|
||||
some lapack functions. The file does not contain any data
|
||||
structures, only functions.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
static void matrix_lapack_assert_fortran_layout( const matrix_type * matrix ) {
|
||||
int rows, columns, row_stride , column_stride;
|
||||
matrix_get_dims( matrix , &rows , &columns , &row_stride , &column_stride);
|
||||
if (!( (column_stride >= rows) && (row_stride == 1)))
|
||||
util_abort("%s: lapack routines require Fortran layout of memory - aborting \n",__func__);
|
||||
}
|
||||
|
||||
|
||||
static void matrix_lapack_assert_square(const matrix_type * matrix) {
|
||||
matrix_lapack_assert_fortran_layout(matrix );
|
||||
{
|
||||
int rows, columns, row_stride , column_stride;
|
||||
matrix_get_dims( matrix , &rows , &columns , &row_stride , &column_stride);
|
||||
if (rows != columns)
|
||||
util_abort("%s: must have square matrices \n",__func__);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/**
|
||||
Solves the linear equations Ax = B. The solution is stored in B on
|
||||
return.
|
||||
*/
|
||||
|
||||
|
||||
void matrix_dgesv(matrix_type * A , matrix_type * B) {
|
||||
matrix_lapack_assert_square( A );
|
||||
matrix_lapack_assert_fortran_layout( B );
|
||||
{
|
||||
int n = matrix_get_rows( A );
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int ldb = matrix_get_column_stride( B );
|
||||
int nrhs = matrix_get_columns( B );
|
||||
long int * ipivot = util_calloc( n , sizeof * ipivot );
|
||||
int info;
|
||||
|
||||
dgesv_(&n , &nrhs , matrix_get_data( A ) , &lda , ipivot , matrix_get_data( B ), &ldb , &info);
|
||||
if (info != 0)
|
||||
util_abort("%s: low level lapack routine: dgesv() failed with info:%d \n",__func__ , info);
|
||||
free(ipivot);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/**
|
||||
Singular Value Decomposition
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
This little function translates between an integer identifier
|
||||
(i.e. and enum instance) to one of the characters used by the low
|
||||
level lapack routine to indicate how the singular vectors should be
|
||||
returned to the calling scope.
|
||||
|
||||
The meaning of the different enum values is documented in the enum
|
||||
definition in the header file matrix_lapack.h.
|
||||
*/
|
||||
|
||||
static char dgesvd_get_vector_job( dgesvd_vector_enum vector_job) {
|
||||
char job = 'X';
|
||||
switch (vector_job) {
|
||||
case(DGESVD_ALL):
|
||||
job = 'A';
|
||||
break;
|
||||
case(DGESVD_MIN_RETURN):
|
||||
job = 'S';
|
||||
break;
|
||||
case(DGESVD_MIN_OVERWRITE):
|
||||
job = 'O';
|
||||
break;
|
||||
case(DGESVD_NONE):
|
||||
job = 'N';
|
||||
break;
|
||||
default:
|
||||
util_abort("%s: internal error - unrecognized code:%d \n",vector_job);
|
||||
}
|
||||
return job;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
If jobu == DGSEVD_NONE the U matrix can be NULL, same for jobvt.
|
||||
*/
|
||||
|
||||
void matrix_dgesvd(dgesvd_vector_enum jobu , dgesvd_vector_enum jobvt , matrix_type * A , double * S , matrix_type * U , matrix_type * VT) {
|
||||
char _jobu = dgesvd_get_vector_job( jobu );
|
||||
char _jobvt = dgesvd_get_vector_job( jobvt );
|
||||
int m = matrix_get_rows( A );
|
||||
int n = matrix_get_columns( A );
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int ldu, ldvt;
|
||||
double * VT_data , *U_data;
|
||||
int info = 0;
|
||||
int min_worksize = util_int_max(3* util_int_min(m , n) + util_int_max(m , n) , 5 * util_int_min(m , n));
|
||||
double * work;
|
||||
int worksize;
|
||||
|
||||
|
||||
if (U == NULL) {
|
||||
ldu = 1;
|
||||
U_data = NULL;
|
||||
if (jobu != DGESVD_NONE)
|
||||
util_abort("%s: internal error \n",__func__);
|
||||
} else {
|
||||
ldu = matrix_get_column_stride( U );
|
||||
U_data = matrix_get_data( U );
|
||||
if (jobu == DGESVD_NONE)
|
||||
util_abort("%s: internal error \n",__func__);
|
||||
}
|
||||
|
||||
if (VT == NULL) {
|
||||
ldvt = 1; /* Will fail if set to zero */
|
||||
VT_data = NULL;
|
||||
if (jobvt != DGESVD_NONE)
|
||||
util_abort("%s: internal error \n",__func__);
|
||||
} else {
|
||||
ldvt = matrix_get_column_stride( VT );
|
||||
VT_data = matrix_get_data( VT );
|
||||
if (jobvt == DGESVD_NONE)
|
||||
util_abort("%s: internal error \n",__func__);
|
||||
}
|
||||
|
||||
/*
|
||||
Query the routine for optimal worksize.
|
||||
*/
|
||||
|
||||
work = util_calloc( 1 , sizeof * work );
|
||||
worksize = -1;
|
||||
dgesvd_(&_jobu , /* 1 */
|
||||
&_jobvt , /* 2 */
|
||||
&m , /* 3 */
|
||||
&n , /* 4 */
|
||||
matrix_get_data( A ) , /* 5 */
|
||||
&lda , /* 6 */
|
||||
S , /* 7 */
|
||||
U_data , /* 8 */
|
||||
&ldu , /* 9 */
|
||||
VT_data , /* 10 */
|
||||
&ldvt , /* 11 */
|
||||
work , /* 12 */
|
||||
&worksize , /* 13 */
|
||||
&info); /* 14 */
|
||||
|
||||
|
||||
/* Try to allocate optimal worksize. */
|
||||
worksize = (int) work[0];
|
||||
work = realloc( work , sizeof * work * worksize );
|
||||
if (work == NULL) {
|
||||
/* Could not allocate optimal worksize - settle for the minimum. This can not fail. */
|
||||
worksize = min_worksize;
|
||||
work = util_calloc( worksize , sizeof * work );
|
||||
}
|
||||
|
||||
dgesvd_(&_jobu , &_jobvt , &m , &n , matrix_get_data( A ) , &lda , S , U_data , &ldu , VT_data , &ldvt , work , &worksize , &info);
|
||||
free( work );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************/
|
||||
/* Eigenvalues of a symmetric matrix */
|
||||
/* Return value is the number of eigenvalues found. */
|
||||
/******************************************************************/
|
||||
|
||||
int matrix_dsyevx(bool compute_eig_vectors ,
|
||||
dsyevx_eig_enum which_values , /* DSYEVX | DSYEVX_VALUE_INTERVAL | DSYEVX_INDEX_INTERVAL */
|
||||
dsyevx_uplo_enum uplo,
|
||||
matrix_type * A , /* The input matrix - is modified by the dsyevx() function. */
|
||||
double VL , /* Lower limit when using DSYEVX_VALUE_INTERVAL */
|
||||
double VU , /* Upper limit when using DSYEVX_VALUE_INTERVAL */
|
||||
int IL , /* Lower index when using DSYEVX_INDEX_INTERVAL */
|
||||
int IU , /* Upper index when using DSYEVX_INDEX_INTERVAL */
|
||||
double *eig_values , /* The calcualated eigenvalues */
|
||||
matrix_type * Z ) { /* The eigenvectors as columns vectors */
|
||||
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int n = matrix_get_rows( A );
|
||||
char jobz;
|
||||
char range;
|
||||
char uplo_c;
|
||||
|
||||
if (compute_eig_vectors)
|
||||
jobz = 'V';
|
||||
else
|
||||
jobz = 'N';
|
||||
|
||||
switch(which_values) {
|
||||
case(DSYEVX_ALL):
|
||||
range = 'A';
|
||||
break;
|
||||
case(DSYEVX_VALUE_INTERVAL):
|
||||
range = 'V';
|
||||
break;
|
||||
case(DSYEVX_INDEX_INTERVAL):
|
||||
range = 'I';
|
||||
break;
|
||||
default:
|
||||
util_abort("%s: internal error \n",__func__);
|
||||
}
|
||||
|
||||
if (uplo == DSYEVX_AUPPER)
|
||||
uplo_c = 'U';
|
||||
else if (uplo == DSYEVX_ALOWER)
|
||||
uplo_c = 'L';
|
||||
else
|
||||
util_abort("%s: internal error \n",__func__);
|
||||
|
||||
|
||||
if (!matrix_is_quadratic( A ))
|
||||
util_abort("%s: matrix A must be quadratic \n",__func__);
|
||||
|
||||
{
|
||||
int num_eigenvalues , ldz, info , worksize;
|
||||
int * ifail = util_calloc( n , sizeof * ifail );
|
||||
int * iwork = util_calloc( 5 * n , sizeof * iwork );
|
||||
double * work = util_calloc( 1 , sizeof * work );
|
||||
double * z_data;
|
||||
double abstol = 0.0; /* SHopuld */
|
||||
|
||||
|
||||
if (compute_eig_vectors) {
|
||||
ldz = matrix_get_column_stride( Z );
|
||||
z_data = matrix_get_data( Z );
|
||||
} else {
|
||||
/* In this case we can accept that Z == NULL */
|
||||
ldz = 1;
|
||||
z_data = NULL;
|
||||
}
|
||||
|
||||
/* First call to determine optimal worksize. */
|
||||
worksize = -1;
|
||||
info = 0;
|
||||
dsyevx_( &jobz, /* 1 */
|
||||
&range, /* 2 */
|
||||
&uplo_c, /* 3 */
|
||||
&n, /* 4 */
|
||||
matrix_get_data( A ), /* 5 */
|
||||
&lda , /* 6 */
|
||||
&VL , /* 7 */
|
||||
&VU , /* 8 */
|
||||
&IL , /* 9 */
|
||||
&IU , /* 10 */
|
||||
&abstol , /* 11 */
|
||||
&num_eigenvalues , /* 12 */
|
||||
eig_values , /* 13 */
|
||||
z_data , /* 14 */
|
||||
&ldz , /* 15 */
|
||||
work , /* 16 */
|
||||
&worksize , /* 17 */
|
||||
iwork , /* 18 */
|
||||
ifail , /* 19 */
|
||||
&info); /* 20 */
|
||||
|
||||
|
||||
worksize = (int) work[0];
|
||||
{
|
||||
double * tmp = realloc(work , sizeof * work * worksize );
|
||||
if (tmp == NULL) {
|
||||
/*
|
||||
OK - we could not get the optimal worksize,
|
||||
try again with the minimum.
|
||||
*/
|
||||
worksize = 8 * n;
|
||||
work = util_realloc(work , sizeof * work * worksize );
|
||||
} else
|
||||
work = tmp; /* The request for optimal worksize succeeded */
|
||||
}
|
||||
/* Second call: do the job */
|
||||
info = 0;
|
||||
dsyevx_( &jobz,
|
||||
&range,
|
||||
&uplo_c,
|
||||
&n,
|
||||
matrix_get_data( A ),
|
||||
&lda ,
|
||||
&VL ,
|
||||
&VU ,
|
||||
&IL ,
|
||||
&IU ,
|
||||
&abstol ,
|
||||
&num_eigenvalues ,
|
||||
eig_values ,
|
||||
z_data ,
|
||||
&ldz ,
|
||||
work ,
|
||||
&worksize ,
|
||||
iwork ,
|
||||
ifail ,
|
||||
&info);
|
||||
|
||||
free( ifail );
|
||||
free( work );
|
||||
free( iwork );
|
||||
return num_eigenvalues;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Wrapper function to compute all eigenvalues + eigenvectors with the
|
||||
matrix_dsyevx() function.
|
||||
*/
|
||||
|
||||
int matrix_dsyevx_all(dsyevx_uplo_enum uplo,
|
||||
matrix_type * A , /* The input matrix - is modified by the dsyevx() function. */
|
||||
double *eig_values , /* The calcualated eigenvalues */
|
||||
matrix_type * Z ) { /* The eigenvectors as columns vectors */
|
||||
int num_eigenvalues;
|
||||
num_eigenvalues = matrix_dsyevx(true , DSYEVX_ALL , uplo , A , 0,0,0,0, eig_values , Z);
|
||||
return num_eigenvalues;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/* Function to compute QR factorization withe the routine dgeqrf */
|
||||
|
||||
void matrix_dgeqrf(matrix_type * A , double * tau) {
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int m = matrix_get_rows( A );
|
||||
int n = matrix_get_columns( A );
|
||||
double * work = util_calloc(1 , sizeof * work );
|
||||
int worksize;
|
||||
int info;
|
||||
|
||||
|
||||
/* Determine optimal worksize. */
|
||||
worksize = -1;
|
||||
dgeqrf_(&m , &n , matrix_get_data( A ), &lda , tau , work , &worksize , &info);
|
||||
if (info != 0)
|
||||
util_abort("%s: dgerqf routine failed with info:%d \n",__func__ , info);
|
||||
worksize = ( int ) work[0];
|
||||
{
|
||||
double * tmp = realloc(work , sizeof * work * worksize );
|
||||
if (tmp == NULL) {
|
||||
/*
|
||||
OK - we could not get the optimal worksize,
|
||||
try again with the minimum.
|
||||
*/
|
||||
worksize = n;
|
||||
work = util_realloc(work , sizeof * work * worksize );
|
||||
} else
|
||||
work = tmp; /* The request for optimal worksize succeeded */
|
||||
}
|
||||
|
||||
|
||||
/* Second call - do the actual computation. */
|
||||
dgeqrf_(&m , &n , matrix_get_data( A ), &lda , tau , work , &worksize , &info);
|
||||
if (info != 0)
|
||||
util_abort("%s: dgerqf routine failed with info:%d \n",__func__ , info);
|
||||
free( work );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Typically to be used after the matrix_dgeqrf() function to construct a orthormal matrix.
|
||||
*/
|
||||
|
||||
void matrix_dorgqr(matrix_type * A , double * tau, int num_reflectors) { /* num_reflectors == length of tau. */
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int m = matrix_get_rows( A );
|
||||
int n = matrix_get_columns( A );
|
||||
double * work = util_malloc(sizeof * work );
|
||||
int worksize;
|
||||
int info;
|
||||
|
||||
|
||||
/* Determine optimal worksize. */
|
||||
worksize = -1;
|
||||
dorgqr_(&m , &n , &num_reflectors , matrix_get_data( A ), &lda , tau , work , &worksize , &info);
|
||||
if (info != 0)
|
||||
util_abort("%s: dorgqf routine failed with info:%d \n",__func__ , info);
|
||||
worksize = ( int ) work[0];
|
||||
{
|
||||
double * tmp = realloc(work , sizeof * work * worksize );
|
||||
if (tmp == NULL) {
|
||||
/*
|
||||
OK - we could not get the optimal worksize,
|
||||
try again with the minimum.
|
||||
*/
|
||||
worksize = n;
|
||||
work = util_realloc(work , sizeof * work * worksize );
|
||||
} else
|
||||
work = tmp; /* The request for optimal worksize succeeded */
|
||||
}
|
||||
|
||||
|
||||
/* Second call - do the actual computation. */
|
||||
dorgqr_(&m , &n , &num_reflectors , matrix_get_data( A ), &lda , tau , work , &worksize , &info);
|
||||
if (info != 0)
|
||||
util_abort("%s: dorqf routine failed with info:%d \n",__func__ , info);
|
||||
free( work );
|
||||
}
|
||||
|
||||
/*****************************************************************/
|
||||
|
||||
/*****************************************************************/
|
||||
/* Factorization */
|
||||
|
||||
/* Currently only used as 'support' function for the matrix_det function. */
|
||||
static void matrix_dgetrf__( matrix_type * A, int * ipiv, int * info) {
|
||||
int lda = matrix_get_column_stride( A );
|
||||
int m = matrix_get_rows( A );
|
||||
int n = matrix_get_columns( A );
|
||||
|
||||
dgetrf_( &m , &n , matrix_get_data( A ) , &lda , ipiv , info);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Calculated the determinant of A. The matrix content will be
|
||||
destroyed.
|
||||
*/
|
||||
|
||||
double matrix_det( matrix_type *A ) {
|
||||
matrix_lapack_assert_square( A );
|
||||
{
|
||||
|
||||
int dgetrf_info;
|
||||
double det = 1;
|
||||
double det_scale = 0;
|
||||
int n = matrix_get_columns( A );
|
||||
int * ipiv = util_malloc( n * sizeof * ipiv );
|
||||
matrix_dgetrf__( A , ipiv , &dgetrf_info );
|
||||
{
|
||||
int i;
|
||||
for (i=0; i < n; i++) {
|
||||
det *= matrix_iget(A , i , i);
|
||||
if (det == 0) return 0; /* Holy fuck - a float == comparison ?? */
|
||||
|
||||
if (ipiv[i] != (i + 1)) /* A permutation has taken place. */
|
||||
det *= -1;
|
||||
|
||||
|
||||
/* Try to avoid overflow/underflow by factoring out the order of magnitude. */
|
||||
while (fabs(det) > 10.0) {
|
||||
det /= 10;
|
||||
det_scale += 1;
|
||||
}
|
||||
|
||||
while (fabs(det) < 1.0) {
|
||||
det *= 10;
|
||||
det_scale -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free( ipiv );
|
||||
return det * pow(10 , det_scale );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*****************************************************************/
|
||||
/* The matrix will be inverted in-place, the inversion is based on LU
|
||||
factorisation in the routine matrix_dgetrf__( ).
|
||||
|
||||
The return value:
|
||||
|
||||
=0 : Success
|
||||
>0 : Singular matrix
|
||||
<0 : Invalid input
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
int matrix_inv( matrix_type * A ) {
|
||||
matrix_lapack_assert_square( A );
|
||||
{
|
||||
int dgetrf_info;
|
||||
int info;
|
||||
int n = matrix_get_columns( A );
|
||||
int * ipiv = util_malloc( n * sizeof * ipiv );
|
||||
matrix_dgetrf__( A , ipiv , &dgetrf_info );
|
||||
{
|
||||
int lda = matrix_get_column_stride( A );
|
||||
double * work = util_malloc( sizeof * work );
|
||||
int work_size;
|
||||
|
||||
/* First call: determine optimal worksize: */
|
||||
work_size = -1;
|
||||
dgetri_( &n , matrix_get_data( A ), &lda , ipiv , work , &work_size , &info);
|
||||
|
||||
if (info == 0) {
|
||||
work_size = (int) work[0];
|
||||
work = util_realloc( work , sizeof * work * work_size );
|
||||
dgetri_( &n , matrix_get_data( A ), &lda , ipiv , work , &work_size , &info);
|
||||
} else
|
||||
util_abort("%s: dgetri_ returned info:%d \n",__func__ , info);
|
||||
|
||||
free( work );
|
||||
}
|
||||
free( ipiv );
|
||||
return info;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
93
ThirdParty/Ert/devel/libert_util/src/matrix_lapack.h
vendored
Normal file
93
ThirdParty/Ert/devel/libert_util/src/matrix_lapack.h
vendored
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'matrix_lapack.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
#ifndef __MATRIX_LAPACK_H__
|
||||
#define __MATRIX_LAPACK_H__
|
||||
|
||||
#include <matrix.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
This enum is just a simple way to label the different ways the
|
||||
singular vectors in U and VT are returned to the calling scope. The
|
||||
low level lapack routine uses a character variable, indicated
|
||||
below.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
typedef enum {
|
||||
/* A */ DGESVD_ALL, /* Returns all the singular vectors in U/VT. */
|
||||
/* S */ DGESVD_MIN_RETURN, /* Return the first min(m,n) vectors in U/VT. */
|
||||
/* O */ DGESVD_MIN_OVERWRITE, /* Return the first min(m,n) vectors of U/VT by overwriteing in A. */
|
||||
/* N */ DGESVD_NONE} /* Do not compute any singular vectors for U/VT */
|
||||
dgesvd_vector_enum;
|
||||
|
||||
|
||||
typedef enum {
|
||||
/* A */ DSYEVX_ALL, /* Compute all the eigenvalues */
|
||||
/* V */ DSYEVX_VALUE_INTERVAL, /* Computes eigenvalues in half open interval <VL , VU] */
|
||||
/* I */ DSYEVX_INDEX_INTERVAL} /* The IL-th through IU'th eigenvalue will be found. */
|
||||
dsyevx_eig_enum;
|
||||
|
||||
|
||||
typedef enum {
|
||||
/* U */ DSYEVX_AUPPER, /* The data is stored in the upper right part of the A matrix. */
|
||||
/* L */ DSYEVX_ALOWER} /* The data is stored in the lower left part of the A matrix. */
|
||||
dsyevx_uplo_enum;
|
||||
|
||||
|
||||
|
||||
|
||||
void matrix_dgesv(matrix_type * A , matrix_type * B);
|
||||
void matrix_dgesvd(dgesvd_vector_enum jobv, dgesvd_vector_enum jobvt , matrix_type * A , double * S , matrix_type * U , matrix_type * VT);
|
||||
|
||||
int matrix_dsyevx(bool compute_eig_vectors ,
|
||||
dsyevx_eig_enum which_values ,
|
||||
dsyevx_uplo_enum uplo,
|
||||
matrix_type * A ,
|
||||
double VL ,
|
||||
double VU ,
|
||||
int IL ,
|
||||
int IU ,
|
||||
double *eig_values ,
|
||||
matrix_type * Z ) ;
|
||||
|
||||
|
||||
int matrix_dsyevx_all(dsyevx_uplo_enum uplo ,
|
||||
matrix_type * A ,
|
||||
double *eig_values ,
|
||||
matrix_type * Z );
|
||||
|
||||
void matrix_dgeqrf(matrix_type * A , double * tau);
|
||||
void matrix_dorgqr(matrix_type * A , double * tau, int num_reflectors);
|
||||
|
||||
double matrix_det( matrix_type *A );
|
||||
|
||||
int matrix_inv( matrix_type * A );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
462
ThirdParty/Ert/devel/libert_util/src/menu.c
vendored
Normal file
462
ThirdParty/Ert/devel/libert_util/src/menu.c
vendored
Normal file
@ -0,0 +1,462 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'menu.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <menu.h>
|
||||
#include <vector.h>
|
||||
|
||||
/**
|
||||
This file implements a simple character based menu system. The menu
|
||||
consists of a list of items, where each item has a description, a
|
||||
function to call, an argument to send to this function, and a set
|
||||
of keys which will invoke this function.
|
||||
|
||||
|
||||
Example:
|
||||
|
||||
menu_type * menu = menu_alloc("Tittel paa menyen" , "qQ");
|
||||
menu_add_item(menu , "Alternativ 1" , "1aA" , func1 , arg1);
|
||||
menu_add_item(menu , "Alternativ 2" , 2Bb" , func2 , arg2);
|
||||
menu_run(menu);
|
||||
menu_free(menu);
|
||||
|
||||
In this case we will get a menu looking like this:
|
||||
|
||||
|
||||
Tittel paa menyen
|
||||
1: Alternativ 1
|
||||
2: Alternativ 2
|
||||
q: Quit
|
||||
==> __
|
||||
|
||||
Now typing '1' will invoke the function func1() with argument
|
||||
arg1. In addition the characters 'a' and 'A' can also be used to
|
||||
invoke the same function. Function func2(arg2) is invoked with '2',
|
||||
'b' or 'B'; finally the menu is exited with 'q' or 'Q' (the
|
||||
arguments to the alloc call).
|
||||
|
||||
|
||||
Observe that (in the current implementation) only single characters
|
||||
are allowed as activator keys for the various items. I.e. 'save' to
|
||||
save is not an option'
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct menu_item_struct {
|
||||
bool separator; /* If this is a separator - in that case all the following fields are moot. */
|
||||
bool helptext; /* If this is help text - Only label is present */
|
||||
char * key_set; /* The characters which will activate this item , e.g. "sS" - must be a \0 terminated string */
|
||||
char * label; /* The label/description of this menu item */
|
||||
menu_func_type * func; /* The function called when this item is activated. */
|
||||
void * arg; /* The argument passed to func. */
|
||||
int label_length; /* The length of the label - zero for separators. */
|
||||
arg_free_ftype * free_arg; /* Destructor for the argument - will typically be NULL */
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
struct menu_struct {
|
||||
vector_type * items; /* Vector of menu_item_type instances */
|
||||
char * quit_keys; /* The keys which can be used to quit from this menu - typically "qQ" */
|
||||
char * title; /* The title of this menu */
|
||||
char * quit_label; /* The text printed on the back/quit item at the bottom. */
|
||||
char * complete_key_set; /* A string containing all the allowed characters - to validata input */
|
||||
};
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Free's the menu occupied by one menu item.
|
||||
*/
|
||||
static void menu_item_free(menu_item_type * item) {
|
||||
if (!item->separator) {
|
||||
free(item->label);
|
||||
}
|
||||
if (!item->separator && !item->helptext){
|
||||
free(item->key_set);
|
||||
}
|
||||
|
||||
|
||||
if (item->free_arg != NULL)
|
||||
item->free_arg(item->arg);
|
||||
|
||||
free(item);
|
||||
}
|
||||
|
||||
static void menu_item_free__(void * arg) {
|
||||
menu_item_free( (menu_item_type *) arg );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This function returns true if the first argument contains _any_ of the characters in
|
||||
the second argument. It is implemented by repeated calss to strchr();
|
||||
*/
|
||||
|
||||
static bool __string_contains(const char * string , const char * char_set) {
|
||||
bool contains = false;
|
||||
int i = 0;
|
||||
do {
|
||||
if (strchr(string , char_set[i]) != NULL)
|
||||
contains = true;
|
||||
i++;
|
||||
} while (!contains && i < strlen(char_set));
|
||||
return contains;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
This function allocates an empty menu, with title 'title'. This
|
||||
particular menu will exit when one the keys in quit_keys is
|
||||
entered. */
|
||||
|
||||
|
||||
menu_type * menu_alloc(const char * title , const char * quit_label , const char * quit_keys) {
|
||||
menu_type * menu = util_malloc(sizeof * menu );
|
||||
|
||||
menu->title = util_alloc_string_copy( title );
|
||||
menu->quit_keys = util_alloc_string_copy( quit_keys );
|
||||
menu->items = vector_alloc_new();
|
||||
menu->complete_key_set = util_alloc_string_copy( quit_keys );
|
||||
menu->quit_label = util_alloc_string_copy( quit_label );
|
||||
|
||||
return menu;
|
||||
}
|
||||
|
||||
static menu_item_type * menu_item_alloc_empty() {
|
||||
menu_item_type * item = util_malloc(sizeof * item );
|
||||
|
||||
item->label = NULL;
|
||||
item->key_set = NULL;
|
||||
item->func = NULL;
|
||||
item->arg = NULL;
|
||||
item->separator = false;
|
||||
item->helptext = false;
|
||||
item->label_length = 0;
|
||||
item->free_arg = NULL;
|
||||
item->enabled = true;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Low level function doing the actual append of a complete item.
|
||||
*/
|
||||
static void menu_append_item__(menu_type * menu , menu_item_type * item) {
|
||||
vector_append_owned_ref( menu->items , item , menu_item_free__);
|
||||
}
|
||||
|
||||
|
||||
void menu_set_title(menu_type * menu, const char * title) {
|
||||
menu->title = util_realloc_string_copy(menu->title, title);
|
||||
}
|
||||
|
||||
|
||||
void menu_item_disable( menu_item_type * item ) {
|
||||
item->enabled = false;
|
||||
}
|
||||
|
||||
void menu_item_enable( menu_item_type * item ) {
|
||||
item->enabled = true;
|
||||
}
|
||||
|
||||
|
||||
char menu_item_get_key( const menu_item_type * item ) {
|
||||
if (item->enabled)
|
||||
return item->key_set[0];
|
||||
else
|
||||
return '-';
|
||||
}
|
||||
|
||||
|
||||
void menu_item_call( const menu_item_type * item ) {
|
||||
if (item->enabled)
|
||||
item->func(item->arg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void menu_item_set_label(menu_item_type * item , const char * label) {
|
||||
item->label = util_realloc_string_copy(item->label , label);
|
||||
item->label_length = strlen(item->label);
|
||||
}
|
||||
|
||||
/**
|
||||
Adds (appends) an item to the menu.
|
||||
|
||||
|
||||
Obsereve that several keys can be used to invoke a particular
|
||||
function, however *only* the first key in the key_set is displayed
|
||||
when the menu is printed on stdout.
|
||||
*/
|
||||
|
||||
menu_item_type * menu_add_item(menu_type * menu , const char * label , const char * key_set , menu_func_type * func, void * arg , arg_free_ftype * free_arg) {
|
||||
if (__string_contains(menu->complete_key_set , key_set))
|
||||
util_abort("%s:fatal error when building menu - key(s) in:%s already in use \n",__func__ , key_set);
|
||||
{
|
||||
menu_item_type * item = menu_item_alloc_empty();
|
||||
item->key_set = util_alloc_string_copy(key_set);
|
||||
item->func = func;
|
||||
item->arg = arg;
|
||||
item->separator = false;
|
||||
item->helptext = false;
|
||||
item->free_arg = free_arg;
|
||||
menu_append_item__(menu , item);
|
||||
menu_item_set_label(item , label);
|
||||
menu->complete_key_set = util_strcat_realloc(menu->complete_key_set , key_set);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Will add a '-------------' line to the menu.
|
||||
*/
|
||||
void menu_add_separator(menu_type * menu) {
|
||||
menu_item_type * item = menu_item_alloc_empty();
|
||||
item->separator = true;
|
||||
menu_append_item__(menu , item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Will print helptext to screen
|
||||
*/
|
||||
|
||||
void menu_add_helptext(menu_type * menu, const char * label ) {
|
||||
menu_item_type * item = menu_item_alloc_empty();
|
||||
item->helptext = true;
|
||||
menu_append_item__(menu , item);
|
||||
menu_item_set_label(item , label);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** level == 0 : top line
|
||||
level == 1 : separator line
|
||||
level == 2 : bottom line
|
||||
*/
|
||||
|
||||
static void __print_line(int l , int level) {
|
||||
int i;
|
||||
if (level == 0)
|
||||
fputc('/' , stdout);
|
||||
else if (level == 1)
|
||||
fputc('|' , stdout);
|
||||
else
|
||||
fputc('\\' , stdout);
|
||||
|
||||
|
||||
for (i=1; i < (l - 1); i++)
|
||||
fputc('-' , stdout);
|
||||
|
||||
|
||||
if (level == 0)
|
||||
fputc('\\' , stdout);
|
||||
else if (level == 1)
|
||||
fputc('|' , stdout);
|
||||
else
|
||||
fputc('/' , stdout);
|
||||
|
||||
|
||||
fputc('\n' , stdout);
|
||||
}
|
||||
|
||||
static void __print_sep(int l) {
|
||||
int i;
|
||||
printf("| ");
|
||||
for (i=0; i < l; i++)
|
||||
fputc('-' , stdout);
|
||||
printf(" |\n");
|
||||
}
|
||||
|
||||
|
||||
static void __print_helptext(char * label, int l){
|
||||
bool end_reached = false;
|
||||
char * label_copy = util_alloc_string_copy( label );
|
||||
char * first_part = "Dummy3";
|
||||
char * second_part = "Dummy4";
|
||||
while(!end_reached){
|
||||
int i;
|
||||
if(strlen(label_copy) > l){
|
||||
util_binary_split_string_from_max_length(label_copy , " ", l , &first_part , &second_part);
|
||||
printf("| %s",first_part);
|
||||
for (i=strlen(first_part); i < l; i++)
|
||||
fputc(' ' , stdout);
|
||||
printf(" |\n");
|
||||
label_copy = util_realloc_string_copy(label_copy, second_part);
|
||||
}
|
||||
else{
|
||||
printf("| %s",label_copy);
|
||||
for (i=strlen(label_copy); i < l; i++)
|
||||
fputc(' ' , stdout);
|
||||
printf(" |\n");
|
||||
end_reached = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static void menu_display(const menu_type * menu) {
|
||||
int i;
|
||||
int length = strlen(menu->title);
|
||||
for (i = 0; i < vector_get_size(menu->items); i++) {
|
||||
const menu_item_type * item = vector_iget_const( menu->items , i);
|
||||
if(!item->helptext)
|
||||
length = util_int_max(length , item->label_length);
|
||||
if(item->helptext)
|
||||
length = util_int_max(length , 60); /* Hardcoded length for helptext*/
|
||||
}
|
||||
|
||||
|
||||
printf("\n");
|
||||
__print_line(length + 10 , 0);
|
||||
printf("| ");
|
||||
util_fprintf_string(menu->title , length + 6 , center_pad , stdout); printf(" |\n");
|
||||
__print_line(length + 10 , 1);
|
||||
for (i=0; i < vector_get_size(menu->items); i++) {
|
||||
const menu_item_type * item = vector_iget_const( menu->items , i);
|
||||
if (item->separator)
|
||||
__print_sep(length + 6);
|
||||
else if (item->helptext)
|
||||
__print_helptext(item->label,length);
|
||||
else {
|
||||
printf("| %c: ", menu_item_get_key( item ));
|
||||
util_fprintf_string(item->label , length + 3 , right_pad , stdout);
|
||||
printf(" |\n");
|
||||
}
|
||||
}
|
||||
__print_sep(length + 6);
|
||||
printf("| %c: ",menu->quit_keys[0]);
|
||||
util_fprintf_string(menu->quit_label , length + 3 , right_pad , stdout);
|
||||
printf(" |\n");
|
||||
__print_line(length + 10 , 2);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Reads a string from stdin: If the string is longer than one single
|
||||
character it is discarded, if it is exactly one character long we
|
||||
check if it is in the menus set of available command characters,
|
||||
and return it *IF* it is a valid character. I.e. the return value
|
||||
from this function is *GUARANTEED* to correspond to a menu item.
|
||||
|
||||
Observe that the function ends with a call to getchar() - this
|
||||
should remove the traling <RETURN> from the stdin input buffer.
|
||||
*/
|
||||
|
||||
static int menu_read_cmd(const menu_type * menu) {
|
||||
char cmd[256];
|
||||
|
||||
do {
|
||||
printf("==> ");
|
||||
fflush(stdout); fscanf(stdin , "%s" , cmd); /* We read a full string -
|
||||
but we only consider it if it is exactly *ONE* character long. */
|
||||
} while ((strchr(menu->complete_key_set , cmd[0]) == NULL) || strlen(cmd) > 1);
|
||||
|
||||
getchar(); /* Discards trailing <RETURN> from standard input buffer? */
|
||||
return cmd[0];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
menu_item_type * menu_get_item(const menu_type * menu, char cmd) {
|
||||
int item_index = 0;
|
||||
menu_item_type * item = NULL;
|
||||
while (item_index < vector_get_size(menu->items)) {
|
||||
menu_item_type * current_item = vector_iget(menu->items , item_index);
|
||||
if (!current_item->separator || !current_item->helptext) {
|
||||
if (strchr(current_item->key_set , cmd) != NULL) {
|
||||
item = current_item;
|
||||
break;
|
||||
}
|
||||
item_index++;
|
||||
}
|
||||
}
|
||||
|
||||
if (item == NULL)
|
||||
util_abort("%s: could not locate item with key: %c \n",__func__ , cmd);
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void menu_run(const menu_type * menu) {
|
||||
while (1) {
|
||||
int cmd;
|
||||
|
||||
|
||||
menu_display(menu);
|
||||
cmd = menu_read_cmd(menu);
|
||||
if (strchr(menu->quit_keys , cmd) != NULL) /* We have recieved a quit command - leave the building. */
|
||||
break;
|
||||
|
||||
/*
|
||||
OK - we start looking through all the available commands to see
|
||||
which this is. */
|
||||
{
|
||||
int item_index = 0;
|
||||
while (1) {
|
||||
const menu_item_type * item = vector_iget_const(menu->items , item_index);
|
||||
if (!item->separator) {
|
||||
if(!item->helptext) {
|
||||
if (strchr(item->key_set , cmd) != NULL) {
|
||||
/* Calling the function ... */
|
||||
menu_item_call( item );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
item_index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void menu_free(menu_type * menu) {
|
||||
free(menu->quit_keys);
|
||||
free(menu->title);
|
||||
free(menu->complete_key_set);
|
||||
free(menu->quit_label);
|
||||
vector_free(menu->items);
|
||||
free(menu);
|
||||
}
|
43
ThirdParty/Ert/devel/libert_util/src/menu.h
vendored
Normal file
43
ThirdParty/Ert/devel/libert_util/src/menu.h
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'menu.h' 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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#ifndef __MENU_H__
|
||||
#define __MENU_H__
|
||||
|
||||
typedef struct menu_struct menu_type;
|
||||
typedef struct menu_item_struct menu_item_type;
|
||||
|
||||
typedef void (menu_func_type) (void *);
|
||||
typedef void (arg_free_ftype) (void *);
|
||||
|
||||
menu_type * menu_alloc(const char * , const char * , const char *);
|
||||
void menu_run(const menu_type * );
|
||||
void menu_free(menu_type * );
|
||||
menu_item_type * menu_get_item(const menu_type * , char );
|
||||
menu_item_type * menu_add_item(menu_type *, const char * , const char * , menu_func_type * , void * , arg_free_ftype * );
|
||||
void menu_add_separator(menu_type * );
|
||||
void menu_add_helptext(menu_type * , const char * );
|
||||
menu_item_type * menu_get_item(const menu_type * , char );
|
||||
void menu_set_title(menu_type *, const char *);
|
||||
|
||||
void menu_item_set_label( menu_item_type * , const char *);
|
||||
void menu_item_disable( menu_item_type * item );
|
||||
void menu_item_enable( menu_item_type * item );
|
||||
|
||||
|
||||
#endif
|
161
ThirdParty/Ert/devel/libert_util/src/msg.c
vendored
Normal file
161
ThirdParty/Ert/devel/libert_util/src/msg.c
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
/*
|
||||
Copyright (C) 2011 Statoil ASA, Norway.
|
||||
|
||||
The file 'msg.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 <http://www.gnu.org/licenses/gpl.html>
|
||||
for more details.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <util.h>
|
||||
#include <msg.h>
|
||||
#include <type_macros.h>
|
||||
|
||||
#define MSG_TYPE_ID 1999867
|
||||
|
||||
struct msg_struct {
|
||||
UTIL_TYPE_ID_DECLARATION;
|
||||
char * prompt;
|
||||
char * msg;
|
||||
int msg_len;
|
||||
bool visible;
|
||||
bool debug;
|
||||
};
|
||||
|
||||
|
||||
static void __msg_assert_visible(const msg_type * msg) {
|
||||
if (!msg->visible)
|
||||
util_abort("%s: you must call msg_show() first - aborting.\n",__func__);
|
||||
}
|
||||
|
||||
|
||||
static void __blank_string(int len) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++)
|
||||
fputc('\b' , stdout);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
fputc(' ' , stdout);
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
fputc('\b' , stdout);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void msg_clear_msg(msg_type * msg) {
|
||||
__msg_assert_visible(msg);
|
||||
__blank_string(msg->msg_len);
|
||||
if (msg->msg != NULL) {
|
||||
free(msg->msg);
|
||||
msg->msg_len = 0;
|
||||
msg->msg = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void msg_clear_prompt(const msg_type * msg) {
|
||||
__blank_string(strlen(msg->prompt));
|
||||
}
|
||||
|
||||
|
||||
void msg_hide(msg_type * msg) {
|
||||
msg_clear_msg(msg);
|
||||
msg_clear_prompt(msg);
|
||||
msg->visible = false;
|
||||
}
|
||||
|
||||
|
||||
void msg_set_prompt(msg_type * msg , const char * prompt) {
|
||||
msg->prompt = util_realloc_string_copy(msg->prompt , prompt);
|
||||
}
|
||||
|
||||
|
||||
void msg_print_msg(const msg_type * msg) {
|
||||
if (msg->msg != NULL)
|
||||
printf("%s" , msg->msg);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
|
||||
void msg_show(msg_type * msg) {
|
||||
if (!msg->visible) {
|
||||
printf("%s" , msg->prompt);
|
||||
msg_print_msg(msg);
|
||||
msg->visible = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void msg_update(msg_type * msg , const char * new_msg) {
|
||||
__msg_assert_visible(msg);
|
||||
if (!msg->debug)
|
||||
msg_clear_msg(msg);
|
||||
|
||||
{
|
||||
msg->msg = util_realloc_string_copy(msg->msg , new_msg);
|
||||
if (new_msg == NULL)
|
||||
msg->msg_len = 0;
|
||||
else
|
||||
msg->msg_len = strlen(new_msg);
|
||||
}
|
||||
|
||||
if (msg->debug)
|
||||
printf("%s\n",msg->msg);
|
||||
else
|
||||
msg_print_msg(msg);
|
||||
}
|
||||
|
||||
|
||||
void msg_update_int(msg_type * msg , const char * fmt , int value) {
|
||||
char buffer[16];
|
||||
sprintf(buffer , fmt , value);
|
||||
msg_update(msg , buffer);
|
||||
}
|
||||
|
||||
|
||||
UTIL_SAFE_CAST_FUNCTION( msg , MSG_TYPE_ID )
|
||||
|
||||
msg_type * msg_alloc(const char * prompt, bool debug) {
|
||||
msg_type * msg = util_malloc(sizeof * msg );
|
||||
UTIL_TYPE_ID_INIT( msg , MSG_TYPE_ID);
|
||||
msg->prompt = util_alloc_string_copy(prompt);
|
||||
|
||||
msg->msg = NULL;
|
||||
msg->msg_len = 0;
|
||||
msg->visible = false;
|
||||
msg->debug = debug;
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
void msg_free(msg_type * msg , bool clear) {
|
||||
if (clear)
|
||||
msg_hide(msg);
|
||||
else
|
||||
printf("\n");
|
||||
|
||||
free(msg->prompt);
|
||||
if (msg->msg != NULL)
|
||||
free(msg->msg);
|
||||
|
||||
free(msg);
|
||||
}
|
||||
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user