forked from equinor/resdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecl_smspec.c
1890 lines (1475 loc) · 63.6 KB
/
ecl_smspec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2011 Statoil ASA, Norway.
The file 'ecl_smspec.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 <stdbool.h>
#include <math.h>
#include <time.h>
#include <ert/util/hash.h>
#include <ert/util/util.h>
#include <ert/util/vector.h>
#include <ert/util/int_vector.h>
#include <ert/util/float_vector.h>
#include <ert/util/stringlist.h>
#include <ert/ecl/ecl_smspec.h>
#include <ert/ecl/ecl_file.h>
#include <ert/ecl/ecl_kw_magic.h>
#include <ert/ecl/ecl_kw.h>
#include <ert/ecl/ecl_util.h>
#include <ert/ecl/smspec_node.h>
#include <ert/ecl/ecl_endian_flip.h>
#include <ert/ecl/ecl_type.h>
#ifdef HAVE_FNMATCH
#include <fnmatch.h>
#endif
/**
This file implements the indexing into the ECLIPSE summary files.
*/
/*
Supporting a new variable type:
-------------------------------
1. The function smspec_node_alloc() must be updated to return a valid
smspec_node_type instance when called with the new var_type.
2. Update the function ecl_smpec_install_gen_key() to install smpec_index
instances of this particular type. The format of the general key is
implicitly defined in this function.
3. The ecl_smspec structure supports two different types of lookup:
a) Lookup based on general key like e.g. WWCT:OP6
b) Specific lookup based on the variable type, i.e. :
ecl_smspec_get_well_var( xxx , well_name , var).
Historically everything started with specific lookup as in case b);
however the general lookup proved to be very convenient, and the
specfic lookup method has seen less use[*]. The final step in
supporting a new variable is to update the function
ecl_smspec_fread_header().
If you want to support specific lookup of the new variable type you
must add the necessary datastructures to the ecl_smspec_struct
structure and then subsequently fill that structure in the big
switch() in ecl_smspec_fread_header() - if you do not care about
specific lookup you just have to add an empty case() slot to the
switch in ecl_smspec_fread_header(). The LGR variables, and also
ECL_SMSPEC_SEGMENT_VAR do not support specific lookup.
[*]: The advantage of the specific lookup is that it is possible
to supply better error messages (The well 'XX' does not
exist, instead of just unknown key: 'WWCT:XX'), and it is
also possible to support queries like: give me all the
well names.
4. Mark the variable type as supported with a 'X' in the defintion of
ecl_smspec_var_type in ecl_smspec.h.
*/
#define ECL_SMSPEC_ID 806647
#define PARAMS_GLOBAL_DEFAULT -99
struct ecl_smspec_struct {
UTIL_TYPE_ID_DECLARATION;
/*
All the hash tables listed below here are different ways to access
smspec_node instances. The actual smspec_node instances are
owned by the smspec_nodes vector;
*/
hash_type * well_var_index; /* Indexes for all well variables: {well1: {var1: index1 , var2: index2} , well2: {var1: index1 , var2: index2}} */
hash_type * well_completion_var_index; /* Indexes for completion indexes .*/
hash_type * group_var_index; /* Indexes for group variables.*/
hash_type * field_var_index;
hash_type * region_var_index; /* The stored index is an offset. */
hash_type * misc_var_index; /* Variables like 'TCPU' and 'NEWTON'. */
hash_type * block_var_index; /* Block variables like BPR */
hash_type * gen_var_index; /* This is "everything" - things can either be found as gen_var("WWCT:OP_X") or as well_var("WWCT" , "OP_X") */
vector_type * smspec_nodes;
bool write_mode;
bool need_nums;
bool locked;
int_vector_type * index_map;
/*-----------------------------------------------------------------*/
int time_seconds;
int grid_dims[3]; /* Grid dimensions - in DIMENS[1,2,3] */
int num_regions;
int Nwells , param_offset;
int params_size;
const char * key_join_string; /* The string used to join keys when building gen_key keys - typically ":" -
but arbitrary - NOT necessary to be able to invert the joining. */
char * header_file; /* FULL path to the currenbtly loaded header_file. */
bool formatted; /* Has this summary instance been loaded from a formatted (i.e. FSMSPEC file) or unformatted (i.e. SMSPEC) file. */
time_t sim_start_time; /* When did the simulation start - worldtime. */
int time_index; /* The fields time_index, day_index, month_index and year_index */
int day_index; /* are used by the ecl_sum_data object to locate per. timestep */
int month_index; /* time information. */
int year_index;
bool has_lgr;
float_vector_type * params_default;
char * restart_case;
};
/**
About indexing:
---------------
The ECLISPE summary files are organised (roughly) like this:
1. A header-file called xxx.SMPSEC is written, which is common to
every timestep.
2. For each timestep the summary vector is written in the form of a
vector 'PARAMS' of length N with floats. In the PARAMS vector all
types of data are stacked togeheter, and one must use the header
info in the SMSPEC file to disentangle the summary data.
Here I will try to describe how the header in SMSPEC is organised, and
how that support is imlemented here. The SMSPEC header is organized
around three character vectors, of length N. To find the position in
the PARAMS vector of a certain quantity, you must consult one, two or
all three of these vectors. The most important vecor - which must
always be consulted is the KEYWORDS vector, then it is the WGNAMES and
NUMS (integer) vectors whcih must be consulted for some variable
types.
Let us a consider a system consisting of:
* Two wells: P1 and P2 - for each well we have variables WOPR, WWCT
and WGOR.
* Three regions: For each region we have variables RPR and RXX(??)
* We have stored field properties FOPT and FWPT
KEYWORDS = ['TIME','FOPR','FPR','FWCT','WOPR','WOPR,'WWCT','WWCT]
....
general_var:
------------
VAR_TYPE:(WELL_NAME|GROUP_NAME|NUMBER):NUMBER
Field var: VAR_TYPE
Misc var: VAR_TYPE
Well var: VAR_TYPE:WELL_NAME
Group var: VAR_TYPE:GROUP_NAME
Block var: VAR_TYPE:i,j,k (where i,j,k is calculated form NUM)
Region var VAR_TYPE:index (where index is NOT from the nums vector, it it is just an offset).
Completion var: VAR_TYPE:WELL_NAME:NUM
....
*/
/**
The special_vars list is used to associate keywords with special
types, when the kewyord name is in conflict with the default vector
naming convention; all the variables mentioned in the list below
are given the type ECL_SMSPEC_MISC_VAR.
For instance the keyword 'NEWTON' starts with 'N' and is
classified as a NETWORK type variable. However it should rather
be classified as a MISC type variable. (What a fucking mess).
The special_vars list is used in the functions
ecl_smspec_identify_special_var() and ecl_smspec_identify_var_type().
*/
static const char* special_vars[] = {"NEWTON",
"NLINEARS",
"ELAPSED",
"MAXDPR",
"MAXDSO",
"MAXDSG",
"MAXDSW",
"STEPTYPE",
"WNEWTON"};
/*
The smspec_required_keywords variable contains a list of keywords
which are *absolutely* required in the SMSPEC file, but observe that
depending on the content of the "KEYWORDS" array other keywords
might bre requred as well - this typically includes the NUMS
keyword. Such 'second-order' dependencies are not accounted for with
this simple list.
*/
static const size_t num_req_keywords = 5;
static const char* smspec_required_keywords[] = {
WGNAMES_KW,
KEYWORDS_KW,
STARTDAT_KW,
UNITS_KW,
DIMENS_KW
};
/*****************************************************************/
ecl_smspec_type * ecl_smspec_alloc_empty(bool write_mode , const char * key_join_string) {
ecl_smspec_type *ecl_smspec;
ecl_smspec = util_malloc(sizeof *ecl_smspec );
UTIL_TYPE_ID_INIT(ecl_smspec , ECL_SMSPEC_ID);
ecl_smspec->well_var_index = hash_alloc();
ecl_smspec->well_completion_var_index = hash_alloc();
ecl_smspec->group_var_index = hash_alloc();
ecl_smspec->field_var_index = hash_alloc();
ecl_smspec->region_var_index = hash_alloc();
ecl_smspec->misc_var_index = hash_alloc();
ecl_smspec->block_var_index = hash_alloc();
ecl_smspec->gen_var_index = hash_alloc();
ecl_smspec->sim_start_time = -1;
ecl_smspec->key_join_string = key_join_string;
ecl_smspec->header_file = NULL;
ecl_smspec->smspec_nodes = vector_alloc_new();
ecl_smspec->time_index = -1;
ecl_smspec->day_index = -1;
ecl_smspec->year_index = -1;
ecl_smspec->month_index = -1;
ecl_smspec->locked = false;
ecl_smspec->time_seconds = -1;
ecl_smspec->index_map = int_vector_alloc(0,0);
ecl_smspec->restart_case = NULL;
ecl_smspec->params_default = float_vector_alloc(0 , PARAMS_GLOBAL_DEFAULT);
ecl_smspec->write_mode = write_mode;
ecl_smspec->need_nums = false;
return ecl_smspec;
}
int * ecl_smspec_alloc_mapping( const ecl_smspec_type * self, const ecl_smspec_type * other) {
int params_size = ecl_smspec_get_params_size( self );
int * mapping = util_malloc( params_size * sizeof * mapping );
for (int i = 0; i < params_size; i++)
mapping[i] = -1;
for (int i=0; i < ecl_smspec_num_nodes( self ); i++) {
const smspec_node_type * self_node = ecl_smspec_iget_node( self , i );
if (smspec_node_is_valid( self_node )) {
int self_index = smspec_node_get_params_index( self_node );
const char * key = smspec_node_get_gen_key1( self_node );
if (ecl_smspec_has_general_var( other , key)) {
const smspec_node_type * other_node = ecl_smspec_get_general_var_node( other , key);
int other_index = smspec_node_get_params_index(other_node);
mapping[ self_index ] = other_index;
}
}
}
return mapping;
}
/**
Observe that the index here is into the __INTERNAL__ indexing in
the smspec_nodes vector; and in general widely different from the
params_index of the returned smspec_node instance.
*/
const smspec_node_type * ecl_smspec_iget_node( const ecl_smspec_type * smspec , int index ) {
return vector_iget_const( smspec->smspec_nodes , index );
}
int ecl_smspec_num_nodes( const ecl_smspec_type * smspec) {
return vector_get_size( smspec->smspec_nodes );
}
/*
In the current implementation it is impossible to mix calls to
ecl_sum_add_var() and ecl_sum_add_tstep() - i.e. one must first add
*all* the variables with ecl_sum_add_var() calls, and then
subsequently add timesteps with ecl_sum_add_tstep().
The locked property of the smspec structure is to ensure that no new
variables are added to the ecl_smspec structure after the first
timestep has been added.
*/
void ecl_smspec_lock( ecl_smspec_type * smspec ) {
smspec->locked = true;
}
/**
* Returns an ecl data type for which all names will fit. If the maximum name
* length is at most 8, an ECL_CHAR is returned and otherwise a large enough
* ECL_STRING.
*/
static ecl_data_type get_wgnames_type(const ecl_smspec_type * smspec) {
size_t max_len = 0;
for(int i = 0; i < ecl_smspec_num_nodes(smspec); ++i) {
const smspec_node_type * node = ecl_smspec_iget_node(smspec, i);
if (smspec_node_is_valid( node )) {
const char * name = smspec_node_get_wgname( node );
if(name)
max_len = util_size_t_max(max_len, strlen(name));
}
}
return max_len <= ECL_STRING8_LENGTH ? ECL_CHAR : ECL_STRING(max_len);
}
// DIMENS
// KEYWORDS
// WGNAMES
// NUMS - optional
// UNITS
// STARTDAT
static void ecl_smspec_fortio_fwrite( const ecl_smspec_type * smspec , fortio_type * fortio) {
int num_nodes = ecl_smspec_num_nodes( smspec );
{
ecl_kw_type * restart_kw = ecl_kw_alloc( RESTART_KW , SUMMARY_RESTART_SIZE , ECL_CHAR );
for (int i=0; i < SUMMARY_RESTART_SIZE; i++)
ecl_kw_iset_string8( restart_kw , i , "");
if (smspec->restart_case != NULL) {
int restart_case_len = strlen(smspec->restart_case);
int offset = 0;
for (int i = 0; i < SUMMARY_RESTART_SIZE ; i++) {
if (offset < restart_case_len)
ecl_kw_iset_string8( restart_kw , i , &smspec->restart_case[ offset ]);
offset += ECL_STRING8_LENGTH;
}
}
ecl_kw_fwrite( restart_kw , fortio );
ecl_kw_free( restart_kw );
}
{
ecl_kw_type * dimens_kw = ecl_kw_alloc( DIMENS_KW , DIMENS_SIZE , ECL_INT );
ecl_kw_iset_int( dimens_kw , DIMENS_SMSPEC_SIZE_INDEX , num_nodes );
ecl_kw_iset_int( dimens_kw , DIMENS_SMSPEC_NX_INDEX , smspec->grid_dims[0] );
ecl_kw_iset_int( dimens_kw , DIMENS_SMSPEC_NY_INDEX , smspec->grid_dims[1] );
ecl_kw_iset_int( dimens_kw , DIMENS_SMSPEC_NZ_INDEX , smspec->grid_dims[2] );
/* Do not know what these two last items are for. */
ecl_kw_iset_int( dimens_kw , 4 , 0 );
ecl_kw_iset_int( dimens_kw , 5 , -1 );
ecl_kw_fwrite( dimens_kw , fortio );
ecl_kw_free( dimens_kw );
}
{
ecl_kw_type * keywords_kw = ecl_kw_alloc( KEYWORDS_KW , num_nodes , ECL_CHAR );
ecl_kw_type * units_kw = ecl_kw_alloc( UNITS_KW , num_nodes , ECL_CHAR );
ecl_kw_type * nums_kw = NULL;
// If the names_type is an ECL_STRING we expect this to be an INTERSECT
// summary, otherwise an ECLIPSE summary.
ecl_data_type names_type = get_wgnames_type(smspec);
ecl_kw_type * wgnames_kw = ecl_kw_alloc(
ecl_type_is_char(names_type) ? WGNAMES_KW : NAMES_KW,
num_nodes,
names_type
);
if (smspec->need_nums)
nums_kw = ecl_kw_alloc( NUMS_KW , num_nodes , ECL_INT);
{
int i;
for (i=0; i < ecl_smspec_num_nodes( smspec ); i++) {
const smspec_node_type * smspec_node = ecl_smspec_iget_node( smspec , i );
/*
It is possible to add variables with deferred initialisation
with the ecl_sum_add_blank_var() function. Before these
variables can be actually used for anything interesting they
must be initialized with the ecl_sum_init_var() function.
If a call to save the smspec file comes before all the
variable have been initialized things will potentially go
belly up. This is solved with the following uber-hack:
o One of the well related keywords is chosen; in
particular 'WWCT' in this case.
o The wgname value is set to DUMMY_WELL
The use of DUMMY_WELL ensures that this field will be
ignored when/if this smspec file is read in at a later
stage.
*/
if (smspec_node_get_var_type( smspec_node ) == ECL_SMSPEC_INVALID_VAR) {
ecl_kw_iset_string8( keywords_kw , i , "WWCT" );
ecl_kw_iset_string8( units_kw , i , "????????");
ecl_kw_iset_string_ptr( wgnames_kw , i , DUMMY_WELL);
} else {
ecl_kw_iset_string8( keywords_kw , i , smspec_node_get_keyword( smspec_node ));
ecl_kw_iset_string8( units_kw , i , smspec_node_get_unit( smspec_node ));
{
const char * wgname = DUMMY_WELL;
if (smspec_node_get_wgname( smspec_node ))
wgname = smspec_node_get_wgname( smspec_node );
ecl_kw_iset_string_ptr( wgnames_kw , i , wgname);
}
}
if (nums_kw != NULL)
ecl_kw_iset_int( nums_kw , i , smspec_node_get_num( smspec_node ));
}
}
ecl_kw_fwrite( keywords_kw , fortio );
ecl_kw_fwrite( wgnames_kw , fortio );
if (nums_kw != NULL)
ecl_kw_fwrite( nums_kw , fortio );
ecl_kw_fwrite( units_kw , fortio );
ecl_kw_free( keywords_kw );
ecl_kw_free( wgnames_kw );
ecl_kw_free( units_kw );
if (nums_kw != NULL)
ecl_kw_free( nums_kw );
}
{
ecl_kw_type * startdat_kw = ecl_kw_alloc( STARTDAT_KW , STARTDAT_SIZE , ECL_INT );
int day,month,year;
ecl_util_set_date_values( smspec->sim_start_time , &day, &month , &year);
ecl_kw_iset_int( startdat_kw , STARTDAT_DAY_INDEX , day );
ecl_kw_iset_int( startdat_kw , STARTDAT_MONTH_INDEX , month );
ecl_kw_iset_int( startdat_kw , STARTDAT_YEAR_INDEX , year );
ecl_kw_fwrite( startdat_kw , fortio );
ecl_kw_free( startdat_kw );
}
}
void ecl_smspec_fwrite( const ecl_smspec_type * smspec , const char * ecl_case , bool fmt_file ) {
char * filename = ecl_util_alloc_filename( NULL , ecl_case , ECL_SUMMARY_HEADER_FILE , fmt_file , 0 );
fortio_type * fortio = fortio_open_writer( filename , fmt_file , ECL_ENDIAN_FLIP);
ecl_smspec_fortio_fwrite( smspec , fortio );
fortio_fclose( fortio );
free( filename );
}
ecl_smspec_type * ecl_smspec_alloc_writer( const char * key_join_string , const char * restart_case, time_t sim_start , bool time_in_days , int nx , int ny , int nz) {
ecl_smspec_type * ecl_smspec = ecl_smspec_alloc_empty( true , key_join_string );
if (restart_case != NULL) {
if (strlen(restart_case) <= (SUMMARY_RESTART_SIZE * ECL_STRING8_LENGTH))
ecl_smspec->restart_case = util_alloc_string_copy( restart_case );
else
return NULL;
}
ecl_smspec->grid_dims[0] = nx;
ecl_smspec->grid_dims[1] = ny;
ecl_smspec->grid_dims[2] = nz;
ecl_smspec->sim_start_time = sim_start;
{
smspec_node_type * time_node;
if (time_in_days) {
time_node = smspec_node_alloc( ECL_SMSPEC_MISC_VAR ,
NULL ,
"TIME" ,
"DAYS" ,
key_join_string ,
ecl_smspec->grid_dims ,
0 ,
-1 ,
0 );
ecl_smspec->time_seconds = 3600 * 24;
} else {
time_node = smspec_node_alloc( ECL_SMSPEC_MISC_VAR ,
NULL ,
"TIME" ,
"HOURS" ,
key_join_string ,
ecl_smspec->grid_dims ,
0 ,
-1 ,
0 );
ecl_smspec->time_seconds = 3600;
}
ecl_smspec_add_node( ecl_smspec , time_node );
ecl_smspec->time_index = smspec_node_get_params_index( time_node );
}
return ecl_smspec;
}
UTIL_SAFE_CAST_FUNCTION( ecl_smspec , ECL_SMSPEC_ID )
/**
Goes through the special_vars static table to check if @var is one
the special variables which does not follow normal naming
convention. If the test eavulates to true the function will return
ECL_SMSPEC_MISC_VAR, otherwise the function will return
ECL_SMSPEC_INVALID_VAR and the variable type will be determined
from the var name according to standard naming conventions.
It is important that this function is called before the standard
method.
*/
static ecl_smspec_var_type ecl_smspec_identify_special_var( const char * var ) {
ecl_smspec_var_type var_type = ECL_SMSPEC_INVALID_VAR;
int num_special = sizeof( special_vars ) / sizeof( special_vars[0] );
int i;
for (i=0; i < num_special; i++) {
if (strcmp( var , special_vars[i]) == 0) {
var_type = ECL_SMSPEC_MISC_VAR;
break;
}
}
return var_type;
}
/*
See table 3.4 in the ECLIPSE file format reference manual.
Observe that the combined ecl_sum style keys like e.g. WWCT:OP1
should be formatted with the keyword first, so that this function
will identify both 'WWCT' and 'WWCT:OP_1' as a ECL_SMSPEC_WELL_VAR
instance.
*/
ecl_smspec_var_type ecl_smspec_identify_var_type(const char * var) {
ecl_smspec_var_type var_type = ecl_smspec_identify_special_var( var );
if (var_type == ECL_SMSPEC_INVALID_VAR) {
switch(var[0]) {
case('A'):
var_type = ECL_SMSPEC_AQUIFER_VAR;
break;
case('B'):
var_type = ECL_SMSPEC_BLOCK_VAR;
break;
case('C'):
var_type = ECL_SMSPEC_COMPLETION_VAR;
break;
case('F'):
var_type = ECL_SMSPEC_FIELD_VAR;
break;
case('G'):
var_type = ECL_SMSPEC_GROUP_VAR;
break;
case('L'):
switch(var[1]) {
case('B'):
var_type = ECL_SMSPEC_LOCAL_BLOCK_VAR;
break;
case('C'):
var_type = ECL_SMSPEC_LOCAL_COMPLETION_VAR;
break;
case('W'):
var_type = ECL_SMSPEC_LOCAL_WELL_VAR;
break;
default:
/*
The documentation explicitly mentions keywords starting with
LB, LC and LW as special types, but keywords starting with
L[^BCW] are also valid. These come in the misceallaneous
category; at least the LLINEAR keyword is an example of such
a keyword.
*/
var_type = ECL_SMSPEC_MISC_VAR;
}
break;
case('N'):
var_type = ECL_SMSPEC_NETWORK_VAR;
break;
case('R'):
{
/*
The distinction between region-to-region variables and plain
region variables is less than clear: The current
interpretation is that the cases:
1. Any variable matching:
a) Starts with 'R'
b) Has 'F' as the third character
2. The variable "RNLF"
Get variable type ECL_SMSPEC_REGION_2_REGION_VAR. The rest
get the type ECL_SMSPEC_REGION_VAR.
*/
if (util_string_equal( var , "RNLF"))
var_type = ECL_SMSPEC_REGION_2_REGION_VAR;
else if (var[2] == 'F')
var_type = ECL_SMSPEC_REGION_2_REGION_VAR;
else
var_type = ECL_SMSPEC_REGION_VAR;
}
break;
case('S'):
var_type = ECL_SMSPEC_SEGMENT_VAR;
break;
case('W'):
var_type = ECL_SMSPEC_WELL_VAR;
break;
default:
/*
It is unfortunately impossible to recognize an error situation -
the rest just goes in "other" variables.
*/
var_type = ECL_SMSPEC_MISC_VAR;
}
}
return var_type;
}
static bool ecl_smspec_lgr_var_type( ecl_smspec_var_type var_type) {
if ((var_type == ECL_SMSPEC_LOCAL_BLOCK_VAR) ||
(var_type == ECL_SMSPEC_LOCAL_WELL_VAR) ||
(var_type == ECL_SMSPEC_LOCAL_COMPLETION_VAR))
return true;
else
return false;
}
/**
Takes a ecl_smspec_var_type variable as input, and return a string
representation of this var_type. Suitable for debug messages +++
*/
const char * ecl_smspec_get_var_type_name( ecl_smspec_var_type var_type ) {
switch(var_type) {
case(ECL_SMSPEC_INVALID_VAR):
return "INVALID_VAR";
break;
case(ECL_SMSPEC_AQUIFER_VAR):
return "AQUIFER_VAR";
break;
case(ECL_SMSPEC_WELL_VAR):
return "WELL_VAR";
break;
case(ECL_SMSPEC_REGION_VAR):
return "REGION_VAR";
break;
case(ECL_SMSPEC_FIELD_VAR):
return "FIELD_VAR";
break;
case(ECL_SMSPEC_GROUP_VAR):
return "GROUP_VAR";
break;
case(ECL_SMSPEC_BLOCK_VAR):
return "BLOCK_VAR";
break;
case(ECL_SMSPEC_COMPLETION_VAR):
return "COMPLETION_VAR";
break;
case(ECL_SMSPEC_LOCAL_BLOCK_VAR):
return "LOCAL_BLOCK_VAR";
break;
case(ECL_SMSPEC_LOCAL_COMPLETION_VAR):
return "LOCAL_COMPLETION_VAR";
break;
case(ECL_SMSPEC_LOCAL_WELL_VAR):
return "LOCAL_WELL_VAR";
break;
case(ECL_SMSPEC_NETWORK_VAR):
return "NETWORK_VAR";
break;
case(ECL_SMSPEC_REGION_2_REGION_VAR):
return "REGION_2_REGION_VAR";
break;
case(ECL_SMSPEC_SEGMENT_VAR):
return "SEGMENT_VAR";
break;
case(ECL_SMSPEC_MISC_VAR):
return "MISC_VAR";
break;
default:
util_abort("%s: Unrecognized variable type:%d \n",__func__ , var_type);
return NULL;
}
}
/**
Input i,j,k are assumed to be in the interval [1..nx] , [1..ny],
[1..nz], return value is a global index which can be used in the
xxx_block_xxx routines.
*/
static int ecl_smspec_get_global_grid_index(const ecl_smspec_type * smspec , int i , int j , int k) {
return i + (j - 1) * smspec->grid_dims[0] + (k - 1) * smspec->grid_dims[0] * smspec->grid_dims[1];
}
/**
This function takes a fully initialized smspec_node instance, generates the
corresponding key and inserts smspec_node instance in the main hash table
smspec->gen_var_index.
The format strings used, i.e. VAR:WELL for well based variables is implicitly
defined through the format strings used in this function.
*/
static void ecl_smspec_install_gen_keys( ecl_smspec_type * smspec , smspec_node_type * smspec_node ) {
/* Insert the default general mapping. */
{
const char * gen_key1 = smspec_node_get_gen_key1( smspec_node );
if (gen_key1 != NULL)
hash_insert_ref(smspec->gen_var_index , gen_key1 , smspec_node);
}
/* Insert the (optional) extra mapping for block related variables and region_2_region variables: */
{
const char * gen_key2 = smspec_node_get_gen_key2( smspec_node );
if (gen_key2 != NULL)
hash_insert_ref(smspec->gen_var_index , gen_key2 , smspec_node);
}
}
static void ecl_smspec_install_special_keys( ecl_smspec_type * ecl_smspec , smspec_node_type * smspec_node) {
/**
This large switch is for installing keys which have custom lookup
paths, in addition to the lookup based on general keys. Examples
of this is e.g. well variables which can be looked up through:
ecl_smspec_get_well_var_index( smspec , well_name , var );
*/
const char * well = smspec_node_get_wgname( smspec_node );
const char * group = well;
const int num = smspec_node_get_num(smspec_node);
const char * keyword = smspec_node_get_keyword(smspec_node);
ecl_smspec_var_type var_type = smspec_node_get_var_type( smspec_node );
switch(var_type) {
case(ECL_SMSPEC_COMPLETION_VAR):
/* Three level indexing: variable -> well -> string(cell_nr)*/
if (!hash_has_key(ecl_smspec->well_completion_var_index , well))
hash_insert_hash_owned_ref(ecl_smspec->well_completion_var_index , well , hash_alloc() , hash_free__);
{
hash_type * cell_hash = hash_get(ecl_smspec->well_completion_var_index , well);
char cell_str[16];
sprintf(cell_str , "%d" , num);
if (!hash_has_key(cell_hash , cell_str))
hash_insert_hash_owned_ref(cell_hash , cell_str , hash_alloc() , hash_free__);
{
hash_type * var_hash = hash_get(cell_hash , cell_str);
hash_insert_ref(var_hash , keyword , smspec_node );
}
}
break;
case(ECL_SMSPEC_FIELD_VAR):
/*
Field variable
*/
hash_insert_ref( ecl_smspec->field_var_index , keyword , smspec_node );
break;
case(ECL_SMSPEC_GROUP_VAR):
if (!hash_has_key(ecl_smspec->group_var_index , group))
hash_insert_hash_owned_ref(ecl_smspec->group_var_index , group, hash_alloc() , hash_free__);
{
hash_type * var_hash = hash_get(ecl_smspec->group_var_index , group);
hash_insert_ref(var_hash , keyword , smspec_node );
}
break;
case(ECL_SMSPEC_REGION_VAR):
if (!hash_has_key(ecl_smspec->region_var_index , keyword))
hash_insert_hash_owned_ref( ecl_smspec->region_var_index , keyword , hash_alloc() , hash_free__);
{
hash_type * var_hash = hash_get(ecl_smspec->region_var_index , keyword);
char num_str[16];
sprintf( num_str , "%d" , num);
hash_insert_ref(var_hash , num_str , smspec_node);
}
ecl_smspec->num_regions = util_int_max(ecl_smspec->num_regions , num);
break;
case (ECL_SMSPEC_WELL_VAR):
if (!hash_has_key(ecl_smspec->well_var_index , well))
hash_insert_hash_owned_ref(ecl_smspec->well_var_index , well , hash_alloc() , hash_free__);
{
hash_type * var_hash = hash_get(ecl_smspec->well_var_index , well);
hash_insert_ref(var_hash , keyword , smspec_node );
}
break;
case(ECL_SMSPEC_MISC_VAR):
/* Misc variable - i.e. date or CPU time ... */
hash_insert_ref(ecl_smspec->misc_var_index , keyword , smspec_node );
break;
case(ECL_SMSPEC_BLOCK_VAR):
/* A block variable */
if (!hash_has_key(ecl_smspec->block_var_index , keyword))
hash_insert_hash_owned_ref(ecl_smspec->block_var_index , keyword , hash_alloc() , hash_free__);
{
hash_type * block_hash = hash_get(ecl_smspec->block_var_index , keyword);
char block_nr[16];
sprintf( block_nr , "%d" , num );
hash_insert_ref(block_hash , block_nr , smspec_node);
}
break;
/**
The variables below are ONLY accesable through the gen_key
setup; but the must be mentioned in this switch statement,
otherwise they will induce a hard failure in the default: target
below.
*/
case(ECL_SMSPEC_LOCAL_BLOCK_VAR):
break;
case(ECL_SMSPEC_LOCAL_COMPLETION_VAR):
break;
case(ECL_SMSPEC_LOCAL_WELL_VAR):
break;
case(ECL_SMSPEC_SEGMENT_VAR):
break;
case(ECL_SMSPEC_REGION_2_REGION_VAR):
break;
case(ECL_SMSPEC_AQUIFER_VAR):
break;
default:
util_abort("%: Internal error - should never be here ?? \n",__func__);
break;
}
}
/**
The usage of this functon breaks down completely if LGR's are involved.
*/
bool ecl_smspec_needs_wgname( ecl_smspec_var_type var_type ) {
switch( var_type ) {
case(ECL_SMSPEC_COMPLETION_VAR):
return true;
break;
case(ECL_SMSPEC_FIELD_VAR):
return false;
break;
case(ECL_SMSPEC_GROUP_VAR):
return true;
break;
case(ECL_SMSPEC_WELL_VAR):
return true;
break;
case(ECL_SMSPEC_REGION_VAR):
return false;
break;
case(ECL_SMSPEC_REGION_2_REGION_VAR):
return false;
break;
case(ECL_SMSPEC_MISC_VAR):
return false;
break;
case(ECL_SMSPEC_BLOCK_VAR):
return false;
break;
case(ECL_SMSPEC_AQUIFER_VAR):
return false;
break;
case(ECL_SMSPEC_SEGMENT_VAR):
return true;
break;
default:
util_exit("Sorry: support for variables of type:%s is not implemented in %s.\n",ecl_smspec_get_var_type_name( var_type ), __FILE__);
}
/* Really should not be here. */
return false;
}
/**
The usage of this functon breaks down completely if LGR's are involved.
*/
bool ecl_smspec_needs_num( ecl_smspec_var_type var_type ) {
switch( var_type ) {
case(ECL_SMSPEC_COMPLETION_VAR):
return true;
break;
case(ECL_SMSPEC_AQUIFER_VAR):
return true;
break;
case(ECL_SMSPEC_FIELD_VAR):
return false;
break;
case(ECL_SMSPEC_GROUP_VAR):
return false;
break;
case(ECL_SMSPEC_WELL_VAR):
return false;
break;
case(ECL_SMSPEC_REGION_VAR):
return true;
break;
case(ECL_SMSPEC_REGION_2_REGION_VAR):
return true;
break;
case(ECL_SMSPEC_MISC_VAR):
return false;
break;
case(ECL_SMSPEC_BLOCK_VAR):
return true;
break;
default:
util_exit("Sorry: support for variables of type:%s is not implemented in %s.\n",ecl_smspec_get_var_type_name( var_type ), __FILE__);
}
return false;
}
bool ecl_smspec_equal(const ecl_smspec_type * self,
const ecl_smspec_type * other) {
if (vector_get_size( self->smspec_nodes ) != vector_get_size( other->smspec_nodes))
return false;
for (int i=0; i < vector_get_size( self->smspec_nodes ); i++) {
const smspec_node_type * node1 = vector_iget_const(self->smspec_nodes, i);
const smspec_node_type * node2 = vector_iget_const(other->smspec_nodes, i);
if (!smspec_node_equal(node1, node2))
return false;
}
return true;
}