Skip to content

Commit

Permalink
refactor: ♻️ extract correlated field generator
Browse files Browse the repository at this point in the history
  • Loading branch information
djmaxus committed Oct 17, 2024
1 parent c849e6d commit 12019e8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
38 changes: 2 additions & 36 deletions src/downscale.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

%% generate fine-scale porosity

sub_porosity = rsgen3D(dr,subscale_dims,downscale_params.corr_lens,@(N)sub_porosity);
sub_porosity = gen_corr_field(dr,subscale_dims,downscale_params.corr_lens,@(N)sub_porosity);
sub_porosity = normalize(sub_porosity,porosity);
sub_porosity = max(sub_porosity,0);

%% calculate fine-scale permeability

sub_permeability_log = log(sub_perm_x);
sub_permeability_log = rsgen3D(dr,subscale_dims,downscale_params.corr_lens,@(N)sub_permeability_log);
sub_permeability_log = gen_corr_field(dr,subscale_dims,downscale_params.corr_lens,@(N)sub_permeability_log);
perm_x = perm_coarse(1);
sub_permeability_log = normalize(sub_permeability_log,log(perm_x));
sub_perm_x = exp(sub_permeability_log);
Expand All @@ -34,40 +34,6 @@
end
end

function [f] = rsgen3D(dr,dims,corr_lens,dist)
arguments
dr (1,3) double
dims (1,3) uint32
corr_lens (1,3) double
dist = @(N) randn(N)
end
x = linspace(-0.5,0.5,dims(1)).*dr(1)*(1 - 1/double(dims(1)));
y = linspace(-0.5,0.5,dims(2)).*dr(2)*(1 - 1/double(dims(2)));
z = linspace(-0.5,0.5,dims(3)).*dr(3)*(1 - 1/double(dims(3)));

[X,Y,Z] = meshgrid(x,y,z);

X = permute(X,[2 1 3]);
Y = permute(Y,[2 1 3]);
Z = permute(Z,[2 1 3]);

D = dist(dims.*2-1);

% Gaussian filter

function out = divide(a,b)
out = a./b;
out(a==0)=0;
end

K = divide(X,corr_lens(1)).^2 + divide(Y,corr_lens(2)).^2 + divide(Z,corr_lens(3)).^2;
K = exp(K.*(-2));
K = K./sum(K(:));

f = convn(D,K,'valid');

end

function [data] = normalize(data,new_norm)
arguments
data double
Expand Down
34 changes: 34 additions & 0 deletions src/gen_corr_field.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function [f] = gen_corr_field(dr,dims,corr_lens,dist)
arguments
dr (1,3) double
dims (1,3) uint32
corr_lens (1,3) double
dist = @(N) randn(N)
end
x = linspace(-0.5,0.5,dims(1)).*dr(1)*(1 - 1/double(dims(1)));
y = linspace(-0.5,0.5,dims(2)).*dr(2)*(1 - 1/double(dims(2)));
z = linspace(-0.5,0.5,dims(3)).*dr(3)*(1 - 1/double(dims(3)));

[X,Y,Z] = meshgrid(x,y,z);

X = permute(X,[2 1 3]);
Y = permute(Y,[2 1 3]);
Z = permute(Z,[2 1 3]);

D = dist(dims.*2-1);

% Gaussian filter

function out = divide(a,b)
out = a./b;
out(a==0)=0;
end

K = divide(X,corr_lens(1)).^2 + divide(Y,corr_lens(2)).^2 + divide(Z,corr_lens(3)).^2;
K = exp(K.*(-2));
K = K./sum(K(:));

f = convn(D,K,'valid');

end

0 comments on commit 12019e8

Please sign in to comment.