-
Notifications
You must be signed in to change notification settings - Fork 0
/
strata_trapper.m
120 lines (93 loc) · 3.28 KB
/
strata_trapper.m
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
function strata_trapped = strata_trapper(G, mask, params, options, enable_waitbar)
arguments
G (1,1) struct
mask (:,1) logical
params (1,1) struct
options (1,1) struct
enable_waitbar (1,1) logical = false;
end
perm_upscaled = zeros(G.cells.num, 3);
poro_upscaled = zeros(G.cells.num, 1);
saturations = unique([linspace(options.sat_min,1,options.sat_num_points),params.rel_perm.sw_resid]);
cap_pres_upscaled = zeros(G.cells.num,length(saturations));
krw = zeros(G.cells.num, 3,length(saturations));
krg = zeros(G.cells.num,3,length(saturations));
cells_num = min(length(mask),G.cells.num);
if enable_waitbar
wb_queue = parallel.pool.DataQueue;
parforWaitbar(0,sum(mask));
afterEach(wb_queue,@parforWaitbar);
end
DR = [G.DX,G.DY,G.DZ];
parfor cell_index = 1:cells_num
if ~mask(cell_index)
continue;
end
[porosity, Kabs, sw_upscaled, pc_upscaled, krg_cell, krw_cell] = ...
downscale_upscale(DR(cell_index,:), saturations , params, options);
if porosity <= 0
error('Resulted in non-positive upscaled porosity')
end
poro_upscaled(cell_index) = porosity;
perm_upscaled(cell_index,:) = Kabs;
cap_pres_upscaled(cell_index,:) = interp1(sw_upscaled,pc_upscaled,saturations,"linear","extrap");
for i = 1:3
krw_cell(i,:) = monotonize(sw_upscaled, krw_cell(i,:), 1);
krg_cell(i,:) = monotonize(sw_upscaled, krg_cell(i,:), -1);
end
krw(cell_index,:,:) = interp1(sw_upscaled, krw_cell', saturations, "linear")';
krg(cell_index,:,:) = interp1(sw_upscaled, krg_cell', saturations, "linear")';
if enable_waitbar
send(wb_queue,cell_index);
end
end
krw(:,:,saturations<=params.rel_perm.sw_resid) = 0;
krg(:,:,saturations<=params.rel_perm.sw_resid) = 1;
krg(:,:,end) = 0;
strata_trapped = struct(...
'porosity', poro_upscaled, ...
'permeability', perm_upscaled, ...
'saturation', saturations,...
'capillary_pressure', cap_pres_upscaled, ...
'rel_perm_wat', krw, ...
'rel_perm_gas', krg ...
);
if enable_waitbar
parforWaitbar(0,0,'ready');
end
end
function parforWaitbar(~,max_iterations,~)
persistent state wb final_state timer_val last_reported_state last_reported_time
if nargin == 2
state = 0;
final_state = max_iterations;
wb = waitbar(state,sprintf('0/%u iterations', final_state),'Name','StrataTrapper');
timer_val = tic();
last_reported_state = state;
last_reported_time = timer_val;
return;
end
if ~isvalid(wb)
return;
end
if nargin == 3
elapsed = duration(seconds(toc(timer_val)),'Format','hh:mm:ss');
message = sprintf('%u coarse cells in %s',final_state,elapsed);
waitbar(1,wb,message);
return;
end
state = state + 1;
elapsed = toc(timer_val);
time_to_report = (elapsed - last_reported_time) > 1;
state_to_report = (state - last_reported_state) > final_state * 0.001;
if and(~time_to_report,~state_to_report)
return;
end
last_reported_state = state;
last_reported_time = elapsed;
t_expected = elapsed * final_state / state;
eta = duration(seconds(t_expected - elapsed),'Format','hh:mm:ss');
elapsed = duration(seconds(elapsed),'Format','hh:mm:ss');
message = sprintf('%u/%u coarse cells\n passed: %s | ETA: %s',state,final_state,elapsed,eta);
waitbar(state/final_state,wb,message);
end