-
Notifications
You must be signed in to change notification settings - Fork 0
/
EROS.py
2437 lines (1929 loc) · 86.9 KB
/
EROS.py
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
import numpy as np
import matplotlib.pyplot as plt
import geone
import geone.covModel as gcm
import pyvista as pv
import scipy
from skimage.measure import label # for connectivity
import time
from shapely.geometry import Polygon, LineString, MultiPolygon
import rasterio
import rasterio.features
from rasterio import Affine
class Graph:
def __init__(self, list_ids, directed=False):
self.list_ids = list_ids.astype(int)
self.directed = directed
self.list = {}
for i in self.list_ids:
self.list[i] = []
def add_edge(self, node1, node2, weight=1):
self.list[node1].append((node2, weight))
if not self.directed:
self.list[node2].append((node1, weight))
def print_list(self):
print(self.list)
# functions
def apply_facies(arr, dic_res):
res = arr.copy()
for k,v in dic_res.items():
res[arr==k]=v
return res
def plot_bh(w_logs, width=1):
for s in w_logs:
x,y, log = s
z_0 = log[0][1] - y
for unit in log:
np.random.seed(unit[0]+24)
plt.bar(x, unit[1] - z_0, bottom=z_0, linewidth=width, edgecolor="black", color=np.random.random(size=3), width=width)
## 3D functions
def compute_domain(z0, z1, nx, ny, nz, sz, s1, s2):
"""
Return a bool 2D array that define the domain where the units
exist (between two surfaces, s1 and s2)
s1, s2: 2D arrays, two given surfaces over simulation domain size: (ny, nx)),
s1 is top surface, s2 is bot surface
"""
s1[s1 < z0]=z0
s1[s1 > z1]=z1
s2[s2 < z0]=z0
s2[s2 > z1]=z1
idx_s1=(np.round((s1-z0)/sz)).astype(int)
idx_s2=(np.round((s2-z0)/sz)).astype(int)
diff = s1 - s2
list_iy, list_ix = np.where(diff > 0)
#domain
a=np.zeros([nz, ny, nx])
for iy, ix in zip(list_iy, list_ix):
a[idx_s2[iy, ix]: idx_s1[iy, ix], iy, ix]=1
return a
class Graph_3D:
def __init__(self, directed=False):
self.directed = directed
self.list = {}
# for i in self.list_ids:
# self.list[i] = []
def add_edge(self, node1, node2, weight=1):
if node1 not in self.list.keys():
self.list[node1] = []
if node2 not in self.list.keys():
self.list[node2] = []
self.list[node1].append((node2, weight))
if not self.directed:
self.list[node2].append((node1, weight))
def print_list(self):
print(self.list)
def plot_bhs_3D(w_logs, z0, plotter=None, v_ex=1):
"""
Plot boreholes in w_logs
#parameters#
w_logs : borehole logs
plotter: pyvista plotter
v_ex : float, vertical exaggeration
"""
def lines_from_points(points):
"""Given an array of points, make a line set"""
poly=pv.PolyData()
poly.points=points
cells=np.full((len(points)-1, 3), 2, dtype=np.int_)
cells[:, 1]=np.arange(0, len(points)-1, dtype=np.int_)
cells[:, 2]=np.arange(1, len(points), dtype=np.int_)
poly.lines=cells
return poly
if plotter is None:
p=pv.Plotter()
else:
p=plotter
for bh in w_logs:
for i in range(len(bh[3])):
l=[]
st=bh[3][i][0]
l.append(bh[3][i][1])
if i < len(bh[3])-1:
l.append(bh[3][i+1][1])
if i == len(bh[3])-1:
l.append(bh[3][0][1]-bh[2])
pts=np.array([np.ones([len(l)])*bh[0], np.ones([len(l)])*bh[1], l]).T
line=lines_from_points(pts)
line.points[:, -1]=(line.points[:, -1] - z0)*v_ex+z0
if st is not None:
np.random.seed(st+24)
color=np.random.random(size=3)
opacity=1
else:
color="white"
opacity=0
p.add_mesh(line, color=color, interpolate_before_map=True, render_lines_as_tubes=True, line_width=15, opacity=opacity)
if plotter is None:
p.add_bounding_box()
p.show_axes()
p.show()
def sim(N, covmodels, means_surf, dimension, spacing, origin, w_logs, nreal=1, bot=None, top=None, xi=0.2, grf_method = "sgs",
facies_ids = [1, 2, 3, 4], proba_cdf = [0.25, 0.25, 0.25, 0.25], alpha=1, seed = 5):
return
def sim_uncond_2D(N, covmodels, means, dimension, spacing, origin, bot=None, top=None, xi=0.5, grf_method = "sgs",
facies_ids = [1, 2, 3, 4], proba = [0.25, 0.25, 0.25, 0.25], alpha = 0.5, p_combi="log", seed = 5, verbose=1):
"""
##inputs##
N
covmodels
means
dimension : (nx, ny), dimension of the simulation grid
spacing : (sx, sy), spacing of the simulation grid
origin : origin of the simulation grid
xi : float btw 0 and 1, fraction of erosive surface
facies_ids : seq of int, facies ids to simulate
proba : proba target of the different facies
alpha : float btw 0 and 1, global proba fraction
(1 mean only global proba is taken into account
and 0 only proba of neighbours)
"""
np.random.seed(seed)
nx, nz = dimension
sx, sz = spacing
ox, oz = origin
z1 = oz + nz*sz
x1 = ox + nx*sx
if bot is None:
bot = oz*np.ones(nx)
if not isinstance(bot, np.ndarray):
bot = np.ones(nx)*bot
if top is None:
top = z1*np.ones(nx)
if not isinstance(top, np.ndarray):
top = np.ones(nx)*top
one_cm = False
if isinstance(covmodels, gcm.CovModel1D):
one_cm = True
# adjust surfaces
real_surf = np.ones([N, nx])
i = 0
while i < N:
erod_layer = np.random.uniform() < xi
# simulate surface
if one_cm:
cm = covmodels
else:
cm = covmodels[i]
if grf_method == "fft":
s1 = geone.grf.grf1D(cm, nx, sx, ox, mean=means[i])[0]
else:
s1 = geone.geosclassicinterface.simulate1D(cm, nx, sx, ox, nreal=1, mean=means[i], verbose=verbose,
searchRadiusRelative=1, nneighborMax=12)["image"].val[0, 0, 0, :]
s1[s1 > top] = top[s1 > top]
s1[s1 < bot] = bot[s1 < bot]
# loop over prexisting surfaces and apply erosion rules
for o in range(i):
s2 = real_surf[o]
if erod_layer:
s2[s2 > s1] = s1[s2 > s1]
else:
s1[s1 < s2] = s2[s1 < s2]
if i > 0 and erod_layer:
s1[s1 > real_surf[i-1]] = real_surf[i-1][s1 > real_surf[i-1]] # erode no deposition
if not erod_layer:
real_surf[i] = s1
i += 1
real_surf = np.concatenate((bot.reshape(1, nx), real_surf, top.reshape(1, nx)), axis=0) # add top and bot
real_surf[real_surf>z1]=z1
real_surf[real_surf<oz]=oz
# real_surf[real_surf>top]=top[real_surf>top]
# real_surf[real_surf<bot]=bot[real_surf<bot]
## polygons
list_p = []
list_ids = []
xg = np.linspace(ox, ox+sx*nx, nx)
ID = 0
for i in range(real_surf.shape[0]-1):
s1=real_surf[i]
s2=real_surf[i+1]
mask_g = s2>s1
mark = False
ia = 0
ib = 0
g_1 = mask_g[0]
g_2 = mask_g[-1]
idx_g = np.where(mask_g[1:] != mask_g[:-1])[0]
if len(idx_g) > 0:
if g_1:
start = 0
else:
start = 1
for o in range(start, len(idx_g)+1, 2):
if o == 0:
ia = 0
ib = idx_g[o]+2
elif o < len(idx_g):
ia = idx_g[o-1]
ib = idx_g[o]+2
else:
ia = idx_g[o-1]
ib = None
coord_l1 = [(x,y) for x,y in zip(xg[ia:ib], s1[ia:ib])]
coord_l2 = [(x,y) for x,y in zip(xg[ia:ib], s2[ia:ib])]
if len(coord_l1) > 1 or len(coord_l2) > 1:
l1 = LineString(coord_l1)
l2 = LineString(coord_l2)
p = Polygon([*list(l2.coords), *list(l1.coords)[::-1]])
list_ids.append(ID)
# p.ID = ID
ID += 1
list_p.append(p)
else:
pass
elif g_1:
coord_l1 = [(x,y) for x,y in zip(xg, s1)]
coord_l2 = [(x,y) for x,y in zip(xg, s2)]
if len(coord_l1) > 1 or len(coord_l2) > 1:
l1 = LineString(coord_l1)
l2 = LineString(coord_l2)
p = Polygon([*list(l2.coords), *list(l1.coords)[::-1]])
# p.ID = ID
list_ids.append(ID)
ID += 1
list_p.append(p)
# rasteriser les polygones
arr = rasterio.features.rasterize(shapes=zip(list_p, np.arange(len(list_p))), out_shape=(nz, nx),
transform=Affine(sx, 0.0, ox, 0.0, sz, oz), fill=-99)
list_ids = np.array(list_ids)
def create_graph():
# if necessary create graph
g = Graph(list_ids, False)
for o in range(len(list_p)):
p = list_p[o]
# for i in list_p[o:]:
for ip, i in enumerate(list_p[o:]):
if p.intersects(i) and p != i:
res = (list_ids[o], list_ids[o+ip], p.intersection(i).length)
# res = (p.ID, i.ID, p.intersection(i).length)
if res[2] > 0:
g.add_edge(res[0], res[1], res[2])
return g
## simulation of the facies
facies_ids = np.array(facies_ids)
proba_cdf = np.array(proba)
if proba_cdf.sum() != 1:
proba_cdf /= proba_cdf.sum()
if alpha < 1:
g = create_graph()
# set initial facies to polygon (0 mean unknown)
dic_res = {}
for i in list_ids:
dic_res[i] = 0
# dictionary of facies area
dic_area = {}
for i in facies_ids:
dic_area[i] = 0
area_sim = 0 # total area simulated
total_area = np.sum([p.area for p in list_p]) # total area
## algo with a graph
ids_to_sim = [i for i in dic_res.keys() if dic_res[i]==0] # cell id to simulate
proba = proba_cdf.copy()
while len(ids_to_sim) > 0:
if area_sim > 0:
# update proba according to area simulated
area_ratio = area_sim/total_area
for i in range(len(proba)):
p = proba_cdf[i]
facies_id = facies_ids[i]
new_p = (p - area_ratio*dic_area[facies_id]/area_sim)/(1- area_ratio)
if new_p < 0:
new_p = 0
proba[i] = new_p
proba = proba / proba.sum()
id_sim = np.random.choice(ids_to_sim) # select a volume to simulate
p_neig = proba
if alpha < 1:
neigs = g.list[id_sim] # neighbours
if len(neigs) == 1: # only 1 neighbour
fac = dic_res[neigs[0][0]]
if fac == 0:
p_neig = proba
else:
p_neig = facies_ids==fac
elif len(neigs) > 1:
sum_w = 0
p_neig = np.zeros(facies_ids.shape)
for n in neigs:
cell_id = n[0] # cell id
w = n[1] # weight
sum_w += w
fac = dic_res[cell_id] # value at the cell
if fac == 0: # no value, take proba
p_neig += proba*w
else:
p_neig += (fac==facies_ids)*w
p_neig /= sum_w
else:
p_neig=np.zeros(facies_ids.shape)
# restrict p interval between 0.001 and 0.999
p_neig[p_neig<0.001] = 0.001
p_neig[p_neig>0.999] = 0.999
p_neig[(p_neig > 0.001) & (p_neig < 0.999)].sum()/(1 - p_neig[(p_neig <= 0.001) | (p_neig >= 0.999)].sum())
proba[proba<0.001] = 0.001
proba[proba>0.999] = 0.999
proba[(proba > 0.001) & (proba < 0.999)].sum()/(1 - proba[(proba <= 0.001) | (proba >= 0.999)].sum())
## mix p with global p
if (p_neig * proba).sum() == 0:
p_combi = "linear"
if p_combi == "linear":
p = (1-alpha)*p_neig + alpha*proba
elif p_combi == "log":
p = p_neig**(1-alpha) * proba**alpha
else:
print("Invalid p_combi, use linear or log")
p = p / p.sum()
facies_choice = np.random.choice(facies_ids, p=p)
dic_res[id_sim] = facies_choice
poly_id = np.where(list_ids==id_sim)[0][0]
poly = list_p[poly_id]
# poly = [i for i in list_p if i.ID == id_sim]
area_sim += poly.area
dic_area[facies_choice] += poly.area
ids_to_sim.remove(id_sim) # remove id from list
arr_res = apply_facies(arr, dic_res)
return real_surf, arr_res, list_p
def sim_cond_2D(N, covmodels, means_surf, dimension, spacing, origin, w_logs, nreal=1, bot=None, top=None, xi=0.5,
facies_ids = [1, 2, 3, 4], proba_cdf = [0.25, 0.25, 0.25, 0.25], alpha=1, p_combi = "log", seed = 5, plots=False, verbose=1):
np.random.seed(seed)
def add_line():
global N_surf, means, erod_lst, real_surf
N_surf += 1
if len(means.shape) == 1:
means = np.concatenate((means, np.array(means[-1]).reshape(-1)))
elif len(means.shape) == 2:
means = np.concatenate((means, np.array(means[-1]).reshape(-1, nx)))
else:
raise ValueError ("Invalid shape {} for means_surf argument".format(means_surf.shape))
erod_lst = np.concatenate((erod_lst, np.array((np.random.random() < xi)).reshape(-1)))
real_surf = np.concatenate((real_surf, np.ones(nx).reshape(-1, nx)))
def check_dic_c(dic_c, prog_logs=None):
global N_surf, means, erod_lst, real_surf
if prog_logs is None: # prog is a dictionnary storing which intervals have been checked in each boreholes
prog={} # prog dic
for k, v in sorted(dic_c.items()):
for iv in v:
if iv not in prog.keys():
prog[iv] = -1
else:
prog={} # prog dic
for k, v in sorted(dic_c.items()):
for iv in v:
if iv not in prog.keys():
prog[iv] = prog_logs[iv]
for k, v in sorted(dic_c.items()):
v = np.copy(v)
if len(v) > 1: # if multiple intervals constrained by the same surface
prev = 0
for o in range(len(v)):
ov = v[o]
if o == 0:
prev = w_logs[ov][2][prog[ov]][0] # previous facies
else:
if prev != w_logs[ov][2][prog[ov]][0]: # if facies are different
dic_c[k].remove(ov) # remove the interval from the list
if not dic_c[k]: # if the list is empty delete
del(dic_c[k])
flag = True
inc = 1
while flag: # find new surface for removed interval
if k+np.abs(inc) >= N_surf:
add_line()
#raise ValueError("Error, difficulties to constrain, increase the number of lines (N)")
if k+inc not in dic_c.keys():
dic_c[k+inc] = [ov]
flag = False
inc += 1
# if k-inc not in dic_c.keys() and k-inc > i:
# dic_c[k-inc] = [ov]
# flag = False
# if inc < 0:
# inc -= 1
# inc *= -1
for iv in v:
if iv not in prog.keys():
prog[iv] = -1
else:
prog[iv] += -1
def check_bh_compa(i2_max, bh_id, plot=False):
"""
Check that a borehole is compatible with actual surfaces or not
Return correct if no there is no problem.
If there is, returns an altitude indicating a maximal bound for the next grf to simulate
"""
def plot_things():
plot_bh(w_logs)
new_shape = MultiPolygon(list_p)
for geom in new_shape.geoms:
xs, ys = geom.exterior.xy
plt.fill(xs, ys, alpha=.5, fc=np.random.random(3), ec='none')
plt.show()
list_p = []
if plot:
plt.figure(figsize=(12, 3), dpi=200)
fa_id_to_const = w_logs[bh_id][2][prog_logs[bh_id]][0] # facies id to constrained
idx_bh_x = np.round(((w_logs[bh_id][0] - ox - sx/2)/sx)).astype(int) # x_idx position of the borehole
# loop over surfaces until below facies to constrained
# this allows to keep only surfaces that are above what have already been constrained
# get index of lowest surfaces below facies to constrained
i2_min = i2_max
if prog_logs[bh_id]+1 != 0:
height_interface = w_logs[bh_id][2][prog_logs[bh_id]+1][1]
else:
height_interface = w_logs[bh_id][2][0][1] - w_logs[bh_id][1]
while np.abs(real_surf[i2_min, idx_bh_x] - height_interface) > 0.1 and real_surf[i2_min, idx_bh_x] > height_interface:
i2_min -= 1
if i2_min <= 0:
break
for i2 in range(i2_min, i2_max): # loop from lowest surfaces to highest
s1 = real_surf[i2]
s2 = real_surf[i2+1]
l1 = s1[idx_bh_x]
l2 = s2[idx_bh_x]
if l1 < l2 and l1 < w_logs[bh_id][2][prog_logs[bh_id]][1] :
if plot:
plt.plot(xgc, s1,c = "k", linewidth=0.5)
plt.plot(xgc, s2,c = "k", linewidth=0.5)
#polygons
mask_g = s2>s1
mark = False
ia = 0
ib = 0
g_1 = mask_g[0]
g_2 = mask_g[-1]
idx_g = np.where(mask_g[1:] != mask_g[:-1])[0]
if len(idx_g) > 0:
if g_1:
start = 0
else:
start = 1
for o in range(start, len(idx_g)+1, 2):
if o == 0:
ia = 0
ib = idx_g[o]+2
elif o < len(idx_g):
ia = idx_g[o-1]
ib = idx_g[o]+2
else:
ia = idx_g[o-1]
ib = len(s1)
if idx_bh_x > ia and idx_bh_x < ib: # if polygon touch the borehole where we are constraining
# make a polygon
coord_l1 = [(x,y) for x,y in zip(plot_xg[ia:ib], s1[ia:ib])]
coord_l2 = [(x,y) for x,y in zip(plot_xg[ia:ib], s2[ia:ib])]
elif g_1: # case polygon go through the whole domain
coord_l1 = [(x,y) for x,y in zip(plot_xg, s1)]
coord_l2 = [(x,y) for x,y in zip(plot_xg, s2)]
if len(coord_l1) > 1 or len(coord_l2) > 1:
l1 = LineString(coord_l1)
l2 = LineString(coord_l2)
p = Polygon([*list(l2.coords), *list(l1.coords)[::-1]])
list_p.append(p)
# now check if that these polygons intersect the others boreholes
for k,v in well_in_lines.items():
if k != bh_id: # check only other boreholes
for idx_k in range(-1, prog_logs[k], -1): # check only already constrained intervals
lin = v[idx_k][0]
# print(p, lin)
# if p.intersects(lin):
if p.intersection(lin).length > 0.05: # TO FIX
if v[idx_k][1] != fa_id_to_const:
if plot:
plot_things()
# return elevation up which there is a problem
return real_surf[i2, idx_bh_x] # incorrect connection
if plot:
plot_things()
return "correct"
# inputs
global N_surf
N_surf = N
# grid
nx, nz = dimension
sx, sz = spacing
ox, oz = origin
z1 = oz + nz*sz
x1 = ox + nx*sx
xgc = np.linspace(ox+sx/2, ox+sx*nx-sx/2, nx)
xg = np.arange(ox, ox+sx*(nx+1), sx)
plot_xg = np.linspace(ox, ox+nx*sx, nx)
if bot is None:
bot = oz*np.ones(nx)
if not isinstance(bot, np.ndarray):
bot = np.ones(nx)*bot
if top is None:
top = z1*np.ones(nx)
if not isinstance(top, np.ndarray):
top = np.ones(nx)*top
one_cm = False
if isinstance(covmodels, gcm.CovModel1D):
one_cm = True
elif isinstance(covmodels, list):
for cm in covmodels:
assert isinstance(cm, gcm.CovModel1D), "object in covmodels must be geone CovModel1D objects"
# adjust surfaces
global erod_lst, real_surf, means # global variables
# means
means = means_surf.copy()
mean_array = 0
if len(means.shape) == 1 and means.shape[0] == N:
mean_array = 1
elif len(means.shape) > 1 and means.shape == (N, nx):
mean_array = 2 # sequence of 1D arrays
else:
raise ValueError ("Invalid shape {} for means_surf argument".format(means_surf.shape))
erod_lst = np.random.uniform(size=N_surf) < xi # determine which layers will be erode
real_surf = np.ones([N_surf, nx])
# correct position of the boreholes
new_w_logs = []
for w in w_logs:
w =( xgc[np.round((w[0] - ox - sx/2)/sx).astype(int)], w[1], w[2]) # set x to the nearest center cell
new_w_logs.append(w)
w_logs = new_w_logs
# warning --> does not allow borehole on the same location, to do
for w in w_logs:
for w2 in w_logs:
if w != w2:
if w[0] == w2[0]: # same position
if w[1] > w2[1]:
w_logs.remove(w2)
else:
w_logs.remove(w)
nwells = len(w_logs)
## put boreholes into lines
well_in_lines = {} # dictionary that contains linestrings of boreholes
i = 0
for well in w_logs:
lines = []
xbh = well[0]
depth = well[1]
z0 = well[2][0][1]
for index in range(len(well[2])):
fa = well[2][index]
if index < len(well[2])-1:
fa2 = well[2][index+1]
line = LineString([(xbh, fa[1]-1e-3), (xbh, fa2[1]+1e-3)])
# line.id= fa[0]
else:
line = LineString([(xbh, fa[1]-1e-3), (xbh, z0-depth+1e-3)])
# line.id= fa[0]
lines.append((line, fa[0]))
well_in_lines[i] = lines
i += 1
# boreholes indexes
bh_idxs = [np.round(((bh[0] - ox - sx/2)/sx)).astype(int) for bh in w_logs]
# choose when to respect HD --> TO FINISH non-stationarity with multiple cm
# dictionary of constrained from boreholes
dic_c = {}
for o in range(nwells):
l = [i[1] for i in w_logs[o][-1]] # interfaces in borehole
ix = bh_idxs[o]
for i in l:
if one_cm:
for i in l:
if mean_array == 1:
dis = scipy.stats.norm(i, np.sqrt(covmodels.sill()))
probas = dis.pdf(means)
elif mean_array == 2: # non stationarity in mean
dis = scipy.stats.norm(i, np.sqrt(covmodels.sill()))
probas = dis.pdf([m[ix] for m in means])
p = np.random.choice(range(N_surf), p=probas/probas.sum())
# dis = scipy.stats.norm(i, np.sqrt(covmodels.sill()))
# probas = dis.pdf(means)
else: # TO FINISH
probas = np.ones(N_surf, dtype=np.float32)
for isurf in range(N_surf):
cm = covmodels[isurf]
dis = scipy.stats.norm(i, np.sqrt(cm.sill()))
probas[isurf] = dis.pdf(means[isurf])
p = np.random.choice(range(N_surf), p=probas/probas.sum())
if p not in dic_c.keys():
dic_c[p] = []
if o not in dic_c[p]:
dic_c[p].append(o)
else:
flag = True
while flag:
p = np.random.choice(range(N_surf), p=probas/probas.sum())
if p not in dic_c.keys():
dic_c[p] = [o]
flag=False
# some useful arrays
prog_logs = -1*np.ones([nwells], dtype=int) # progression of the constrained on the logs
idx_const = np.sort(list(dic_c.keys())) # idx of constrained
dic_ineq = {}
# simulation of the surfaces
i = 0
s1 = bot.copy()
plot_to_do = False
while i < N_surf:
if plot_to_do:
plt.plot(plot_xg, s1_org, c="r", linewidth=1, alpha=0.9, label="simulated surface")
plt.legend()
plt.show()
plot_to_do=False
if i in dic_c.keys() and plots:
print(i, dic_c[i])
fig, axs = plt.subplots(figsize=(10,5), dpi=200)
plt.plot(plot_xg, real_surf.T, c="k", linewidth=.5)
plot_bh(w_logs, 1)
np.random.seed(seed)
plt.ylim(0, 35)
plot_to_do = True
#return real_surf, prog_logs, dic_c
x_hd = [] # constraints on grf
z_hd = [] # constraints on grf
ineq_x = []
ineq_v = []
ineq_max_x = []
ineq_max_v = []
others_interfaces = None
# simulate surface
if one_cm:
cm = covmodels
else:
cm = covmodels[i]
sigma = np.sqrt(cm.sill())
# print(i)
# if i == 91:
# return real_surf, prog_logs, dic_c, well_in_lines, w_logs
check_dic_c(dic_c, prog_logs)
idx_const = np.sort(list(dic_c.keys()))
erod_layer = False
if np.random.random() < xi: # if erode
erod_layer = True
if i in idx_const and not erod_layer: # if a constraint must be respected
# check to correct a bug that can appear some times
if len(dic_c[i]) > 1:
l = []
for iwell in dic_c[i]:
test = check_bh_compa(i-1, iwell)
x, depth, log = w_logs[iwell]
s_max = real_surf[i-1, bh_idxs[iwell]] # maximum height of the surfaces previously simulated
height_log = log[prog_logs[iwell]][1]
if (s_max > height_log and prog_logs[iwell] != -len(log)) or test != "correct":
l.append("erode")
else:
l.append("onlap")
#print(l)
if "erode" in l and "onlap" in l: # this is a problem
l = np.array(l)
others_interfaces = list(np.array(dic_c[i])[l == "onlap"]).copy()
dic_c[i] = list(np.array(dic_c[i])[l == "erode"]) # keep interface that will be corrected by the erode surface
else:
pass
# determine constraints
to_remove = [] # list to interface to remove from dic_c
for iwell in dic_c[i]:
constraints = True # flag to apply constraints on the conditional surfaces to prevent some issues
test = check_bh_compa(i-1, iwell)
if test == "correct": # no problem of connexion with others bh
x, depth, log = w_logs[iwell]
s_max = real_surf[i-1, bh_idxs[iwell]] # maximum height of the surfaces previously simulated
height_log = log[prog_logs[iwell]][1]
if s_max > height_log and prog_logs[iwell] != -len(log): # if surfaces are above contact --> erosion has to be set
erod_layer = True
x_hd.append(x+1e-5)
z_hd.append(height_log)
elif s_max < height_log and prog_logs[iwell] == -len(log): # if top of the borehole
ineq_x = np.insert(ineq_x, 0, x+1e-5)
ineq_v = np.insert(ineq_v, 0, height_log)
elif s_max < height_log:
x_hd.append(x+1e-5)
z_hd.append(height_log)
elif s_max > height_log and prog_logs[iwell] == -len(log): # if surface are above contact but topest unit of borehole
constraints = False #disable constraints as intervals already good
dic_ineq[x] = height_log # update ineq
if not erod_layer and constraints:
### add more constraints to prevent that the surface cross cut other boreholes
s_bef = s1.copy()
h_sim = [s_bef[ibh] for ibh in bh_idxs] # height of simulation at bh positions
## select a bh where to simulate the surface
l_int = []
for bh_id in range(nwells):
bh = w_logs[bh_id]
pr = prog_logs[bh_id]
if -pr - 1 != len(bh[2]): # not completed bh
el = bh[2][pr] # (facies, altitude)
if h_sim[bh_id] < el[1]: # if surfaces are below facies to constrained
if pr == -1:
l_int.append((el[0], el[1], max(bh[2][0][1]-bh[1], h_sim[bh_id])))
else:
l_int.append((el[0], el[1], max(bh[2][pr+1][1], h_sim[bh_id])))
else:
l_int.append((None, h_sim[bh_id]))
else: # bh completed
l_int.append(None)
choice = iwell
facies = l_int[choice][0]
# create ineq
for iw in range(nwells):
ibh = bh_idxs[iw]
t = l_int[iw]
if t is not None:
if t[0] is None: # case where surfaces are above interface between two next facies of iw well
ineq_max_x.append(xgc[ibh])
ineq_max_v.append(t[1])
else:
if t[0] != facies:
ineq_max_x.append(xgc[ibh])
ineq_max_v.append(max(s_bef[ibh], t[2]))
prog_logs[iwell] -= 1 # update prog logs
to_remove.append(iwell)
else: # we have to fix that
print(i, "gne")
x, depth, log = w_logs[iwell]
height_log = log[prog_logs[iwell]][1]
ineq_max_x.append(x)
# un bout de scotch
good = True
for j in range(len(ineq_v)):
if ineq_x[j] == x:
if ineq_v[j] < test - 2*sz:
ineq_max_v.append(test - 2*sz)
good = False
if good:
ineq_max_v.append(test)
erod_layer = True
# now that we have fixed the problem we must simulate correctly the next surface, adapt dic_c
dic_c[i].remove(iwell)
if not dic_c[i]: # empty
del(dic_c[i])
# add a new entry
up = 1
flag = True
while flag:
if i+up not in dic_c.keys():
dic_c[i+up] = [iwell]
break
else:
if iwell not in dic_c[i+up]:
dic_c[i+up].append(iwell)
break
up += 1
if i + up > N_surf:
raise ValueError ("Error")
if i+1 == N_surf: # if no more surfaces availables, add a new one
add_line()
idx_const = np.sort(list(dic_c.keys()))
# remove in dic_c
for iwell in to_remove:
dic_c[i].remove(iwell)
if not dic_c[i]: # empty
del(dic_c[i]) # remove value from dic c when corrected
# min inequality constraints
if erod_layer :
temp = np.array([(k, v) for k,v in dic_ineq.items() if k not in x_hd])
if temp.shape[0] > 0:
ineq_x = temp[:, 0]
ineq_v = temp[:, 1]
if len(x_hd) == 0:
x_hd = None