Skip to content

Commit e115dff

Browse files
committed
refactor: ♻️ make upscale MEX-compatible
1 parent c1438d0 commit e115dff

File tree

5 files changed

+60
-21
lines changed

5 files changed

+60
-21
lines changed

src/CapPressure.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
obj (1,1) CapPressure
2727
sw (1,1) double
2828
poro double {mustBeNonnegative}
29-
perm double {mustBeNonnegative}
29+
perm (:,:,:,:) double {mustBeNonnegative}
3030
end
3131

3232
perm = obj.transform_perm(poro,perm);
@@ -43,7 +43,7 @@
4343
obj (1,1) CapPressure
4444
pc (1,1) double
4545
poro double {mustBeNonnegative}
46-
perm double {mustBeNonnegative}
46+
perm (:,:,:,:) double {mustBeNonnegative}
4747
end
4848

4949
lj = obj.inv_lj(pc,poro,perm);
@@ -58,7 +58,7 @@
5858
obj (1,1) CapPressure
5959
pc double
6060
poro double {mustBeNonnegative}
61-
perm double {mustBeNonnegative}
61+
perm (:,:,:,:) double {mustBeNonnegative}
6262
end
6363

6464
perm = obj.transform_perm(poro,perm);
@@ -74,7 +74,7 @@
7474
obj (1,1) CapPressure
7575
sw (1,1) double
7676
poro double {mustBeNonnegative}
77-
perm double {mustBePositive}
77+
perm (:,:,:,:) double {mustBePositive}
7878
end
7979

8080
perm = obj.transform_perm(poro,perm);
@@ -85,17 +85,24 @@
8585
dpc_dsw = obj.mult * obj.leverett_j.deriv(sw) * sqrt(poresize_mult);
8686
end
8787

88-
function perm = transform_perm(obj,poro,perm)
88+
function perm_transformed = transform_perm(obj,poro,perm)
89+
arguments
90+
obj (1,1) CapPressure
91+
poro double
92+
perm (:,:,:,:) double
93+
end
8994
dims_poro = size(poro);
9095
dims_perm = size(perm);
9196
if (length(dims_poro) == 3) && (length(dims_perm) == 4) && (dims_perm(4) == 3)
92-
perm = sum(perm .* reshape(obj.perm_weights, 1, 1, 1, 3), 4);
97+
perm_transformed = sum(perm .* reshape(obj.perm_weights, 1, 1, 1, 3), 4);
9398
return;
9499
end
95100

96101
if (length(dims_poro) == 2) && (length(dims_perm) == 2) && (dims_perm(2) == 3)
97-
perm = sum(perm .* reshape(obj.perm_weights, 1, 3), 2);
102+
perm_transformed = sum(perm .* reshape(obj.perm_weights, 1, 3), 2);
103+
return;
98104
end
105+
perm_transformed = perm;
99106
end
100107
end
101108
end

src/Options.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
sat_num_points (1,1) uint32 {mustBePositive} = 40
44
sat_tol (1,1) double {mustBeNonnegative} = 1e-4
55
hydrostatic_correction (1,1) logical = false
6-
m_save_mip_step (1,1) {mustBeOfClass(m_save_mip_step,'logical')} = false;
6+
m_save_mip_step (1,1) {mustBeA(m_save_mip_step,'logical')} = false;
77
m_perm_threshold_mD (1,3) double {mustBeNonnegative} = [0,0,0];
88
m_poro_threshold (1,1) double {mustBeNonnegative} = 0;
99
end

src/calc_percolation.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
h_ref = Lz*0.5;
55

66
Nz = size(p_entry,3);
7+
h = zeros(1,1,Nz);
78
h(1,1,1:Nz) = linspace(Lz/Nz/2,Lz - Lz/Nz/2,Nz);
89

910
hydrostatic_correction = 0;
@@ -28,10 +29,10 @@
2829
function [invasion,has_changed] = calc_percolation_iter(invasion,Idx)
2930
[Nx,Ny,Nz] = size(invasion);
3031
has_changed = false;
31-
for idx = Idx
32-
i = idx(1);
33-
j = idx(2);
34-
k = idx(3);
32+
for num_col = 1:size(Idx,2)
33+
i = Idx(1,num_col);
34+
j = Idx(2,num_col);
35+
k = Idx(3,num_col);
3536

3637
is_boundary = i==1 || i==Nx || j==1 || j==Ny || k==1 || k==Nz;
3738

src/upscale.m

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
function [perm_upscaled, pc_upscaled, krw, krg, mip] = upscale(...
22
dr, saturations, params, options, porosities, permeabilities)
3+
arguments
4+
dr (1,3) double
5+
saturations (1, :) double
6+
params (1,1) Params
7+
options (1,1) Options
8+
porosities (:,:,:) double
9+
permeabilities (:,:,:,3) double
10+
end
311

412
if max(porosities,[],'all') <= 0
513
error('inactive cell');
614
end
715

8-
mip(1:length(saturations)) = struct('sw',nan,'sub_sw',[]);
16+
mip = repmat(construct_mip_cell_data(),1,numel(saturations));
917

1018
% apply thresholds before proceeding
1119
small_poro = porosities < options.m_poro_threshold;
@@ -49,7 +57,10 @@
4957

5058
entry_pressures = params.cap_pressure.func(1,porosities,permeabilities);
5159
pc_max = params.cap_pressure.func(params.sw_resid,porosities,permeabilities);
52-
pc_points = linspace(max(pc_max(isfinite(pc_max))),min(entry_pressures(:)),length(saturations));
60+
is_pc_max_finite = isfinite(pc_max(:));
61+
pc_max_finite = pc_max(:);
62+
pc_max_finite = pc_max_finite(is_pc_max_finite);
63+
pc_points = linspace(max(pc_max_finite),min(entry_pressures(:)),length(saturations));
5364

5465
for index_saturation = 1:length(saturations)
5566

@@ -61,6 +72,8 @@
6172
calc_endpoint = index_saturation == 1 || index_saturation == length(saturations);
6273
max_iterations = calc_endpoint*1000 + ~calc_endpoint*100;
6374
err_prev = Inf;
75+
pc_mid_tot = 0;
76+
sub_sw = zeros(0,0,0); coder.varsize('sub_sw');
6477
for iteration_num=1:max_iterations
6578
[pc_mid_tot, sw_mid, pc_mid, sub_sw, converged, err] = mip_iteration(...
6679
sw_target, dr, entry_pressures, porosities, permeabilities, pc_mid, ...
@@ -109,12 +122,12 @@
109122
pc_upscaled = [pc_upscaled,pc_upscaled_extra];
110123

111124
sw_upscaled(end+1) = sw_extra(1);
112-
krw(:,end+1) = 0;
113-
krg(:,end+1) = max(params.krg.data(:,2));
125+
krw = extend_cols_with(krw,0);
126+
krg = extend_cols_with(krg,max(params.krg.data(:,2)));
114127

115128
sw_upscaled(end+1) = sw_extra(2);
116-
krw(:,end+1) = max(params.krw.data(:,2));
117-
krg(:,end+1) = 0;
129+
krw = extend_cols_with(krw,max(params.krw.data(:,2)));
130+
krg = extend_cols_with(krg,0);
118131

119132
[sw_upscaled,unique_idx] = unique(sw_upscaled);
120133
pc_upscaled = pc_upscaled(unique_idx);
@@ -205,3 +218,20 @@
205218
krw = K_phase_upscaled(4:6)';
206219

207220
end
221+
222+
function kr = extend_cols_with(kr,value)
223+
krx = kr(1,:);
224+
kry = kr(2,:);
225+
krz = kr(3,:);
226+
227+
krx(end+1) = value;
228+
kry(end+1) = value;
229+
krz(end+1) = value;
230+
kr = [krx;kry;krz];
231+
end
232+
233+
function mip_cell_data = construct_mip_cell_data()
234+
sub_sw = zeros(0,0,0);
235+
coder.varsize('sub_sw');
236+
mip_cell_data = struct('sw',nan,'sub_sw',sub_sw);
237+
end

src/upscale_permeability.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
idx_conductive = find(cond_sum>0);
4141

42+
idx_adj = zeros(numel(idx_conductive),3,2);
4243
idx_adj(:,1,1) = idx_conductive - 1;
4344
idx_adj(:,1,2) = idx_conductive + 1;
4445
idx_adj(:,2,1) = idx_conductive - x;
@@ -53,11 +54,11 @@
5354
A_diag(:,2) = 1:num_eqs;
5455

5556
num_cells_ext = x*y*z;
56-
57+
map_to_cond = zeros(num_cells_ext,1);
5758
map_to_cond(1:num_cells_ext,1)=0;
5859
map_to_cond(idx_conductive) = 1:length(idx_conductive);
5960

60-
A_ndiag{3,2} = [];
61+
A_ndiag = cell(3,2);
6162

6263
for dir=dirs
6364
cond_dir = cond(:,dir);
@@ -75,7 +76,7 @@
7576
end
7677

7778
A_ndiag = cell2mat(reshape(A_ndiag,[],1));
78-
79+
lin_to_sub = zeros(num_cells_ext,3);
7980
[lin_to_sub(:,1),lin_to_sub(:,2),lin_to_sub(:,3)] = ind2sub([x,y,z],1:num_cells_ext);
8081
is_inner = false(num_cells_ext,3);
8182

0 commit comments

Comments
 (0)