-
Notifications
You must be signed in to change notification settings - Fork 19
/
fft_ma_3d.m
310 lines (266 loc) · 9.15 KB
/
fft_ma_3d.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
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
% fft_ma_3d :
% Call :
% [out,z,options,logL]=fft_ma_3d(x,y,z,Va,options)
%
% x: array, ex : x=1:1:80:
% y: array, ex : y=1:1:50:
% z: array, ex : z=1:1:50:
% Va: variogram def, ex : Va="1 Sph (10,30,.25)";
%
% options.gmean
% options.gvar
% options.pad_x : Padding in x-direction (number of pixels [def=nx])
% options.pad_y : Padding in y-direction (number of pixels [def=ny])
% options.pad_z : Padding in z-direction (number of pixels [def=nz])
% options.wx,options.wy,options.wz : wraparound padding around the simulation area
% when using sequential Gibbs simulation.
% [def, options.wx=max(range)/dx]
% [def, options.wy=max(range)/dy]
% [def, options.wz=max(range)/dz]
%
%
% "
% Ravalec, M.L. and Noetinger, B. and Hu, L.Y.},
% Mathematical Geology 32(6), 2000, pp 701-723
% The FFT moving average (FFT-MA) generator: An efficient numerical
% method for generating and conditioning Gaussian simulations
% "
%
% Examples:
% % 1D
% x=1:1:512;y=0;z=0;
% Va='1 Gau(20)';
% [out,z]=fft_ma_3d(x,y,z,Va);
% plot(x,out);colorbar
%
% % 2D
% x=[1:1:50];y=1:1:80;z=0;
% direction=30; % 30 degrees from north
% h_max=10;
% h_min=5;
% aniso=h_min/h_max;
% Va='1 Sph(10,30,5/10)';
% [out,z]=fft_ma_3d(x,y,z,Va);
% imagesc(x,y,out);colorbar
%
%
% x=[1:1:50];y=1:1:80;z=0;
% Va='1 Sph(10,30,.25)';
% [out1,z_rand]=fft_ma_2d(x,y,Va);
% ii=10000:20000;
% z_rand(ii)=randn(size(z_rand(ii)));
% options.z_rand=z_rand;
% [out2,z_rand2]=fft_ma_3d(x,y,z,Va,options);
% subplot(1,3,1),imagesc(x,y,[out1]);colorbar;axis image;cax=caxis;
% subplot(1,3,2),imagesc(x,y,[out2]);caxis(cax);colorbar;axis image
% subplot(1,3,3),imagesc(x,y,[out2-out1]);colorbar;axis image
%
% % 3D
% x=[1:1:50];y=1:1:80;z=1:30;
% Va='1 Sph(10,30,.25)';
% [m,z]=fft_ma_3d(x,y,z,Va);
% isosurface(m,.6);isosurface(m,0);isosurface(m,-.6)
% view([30 40 10]);axis image
% Using proper semivariogram anisotropy specification (Feb, 2012)
% original (FFT_MA_2D) Knud S. Cordua (June 2009)
% Thomas M. Hansen (September, 2009)
% Jan Frydendall (April, 2011) Zero padding
% UPDATE TO WORK WITH RESIM
%
function [out,z_rand,options,logL]=fft_ma_3d(x,y,z,Va,options)
if nargin==0
x=[1:1:32];y=1:1:32;z=1:32;
Va='1 Sph(10,30,.25)';
options.wx=0;
options.wy=0;
options.wz=0;
options.pad_x=0;
options.pad_y=0;
options.pad_z=0;
[m,z_rand,options]=fft_ma_3d_new(x,y,z,Va);
isosurface(m,.6);isosurface(m,0);isosurface(m,-.6)
view([30 40 10]);axis image
return
end
options.null='';
if ~isfield(options,'resim_type');options.resim_type=2;end
if ~isstruct(Va);Va=deformat_variogram(Va);end
if ~isfield(options,'wrap_around');options.wrap_around=1;end
if ~isfield(options,'gmean');options.gmean=0;end
if ~isfield(options,'gvar');options.gvar=sum([Va.par1]);end
nx=length(x);
ny=length(y);
nz=length(z);
if nx>1; dx=x(2)-x(1); else dx=1; end
if ny>1; dy=y(2)-y(1); else dy=1; end
if nz>1; dz=z(2)-z(1); else dz=1; end
if isfield(options,'pad');
if length(options.pad)==1, options.pad=[1 1 1].*options.pad;end
try;options.pad_x=options.pad(1);end
try;options.pad_y=options.pad(2);end
try;options.pad_z=options.pad(3);end
end
if ~isfield(options,'pad_x');options.pad_x=nx-1;end
if ~isfield(options,'pad_y');options.pad_y=ny-1;end
if ~isfield(options,'pad_z');options.pad_z=nz-1;end
if ~isfield(options,'padpow2');options.padpow2=1;end
if isfield(options,'w');
if length(options.w)==1, options.w=[1 1 1].*options.w;end
try;options.wx=options.w(1);end
try;options.wy=options.w(2);end
try;options.wz=options.w(3);end
end
if ~isfield(options,'wx');
if options.resim_type==1
options.wx=0;
else
options.wx = 2*ceil(semivar_get_max_range(Va)./dx);
end
end
if ~isfield(options,'wy');
if options.resim_type==1
options.wy=0;
else
options.wy = 2*ceil(semivar_get_max_range(Va)./dy);
end
end
if ~isfield(options,'wz');
if options.resim_type==1
options.wz=0;
else
options.wz = 2*ceil(semivar_get_max_range(Va)./dz);
end
end
if length(x)==1; x=[x x+.0001]; end
if length(y)==1; y=[y y+.0001]; end
if length(z)==1; z=[z z+.0001]; end
org.nx=nx;
org.ny=ny;
org.nz=nz;
% padding size (before padding to size 2^n)
nx_c=nx+options.pad_x;
ny_c=ny+options.pad_y;
nz_c=nz+options.pad_z;
x_all=[0:1:(nx_c-1)].*dx;
y_all=[0:1:(ny_c-1)].*dy;
z_all=[0:1:(nz_c-1)].*dz;
%% REMOVE OLD COVARIANCE OF options.constant_C=0
if (isfield(options,'constant_C'));
if options.constant_C==0;
try;options=rmfield(options,'C');end
try;options=rmfield(options,'fftC');end
end
end
%% SETUP COVARIANCE MODEL
if (~isfield(options,'C'))&(~isfield(options,'fftC'));
if (options.padpow2==1)
nx_c=2.^nextpow2(nx_c);
ny_c=2.^nextpow2(ny_c);
nz_c=2.^nextpow2(nz_c);
end
x1=[0:1:(nx_c-1)].*dx;
y1=[0:1:(ny_c-1)].*dy;
z1=[0:1:(nz_c-1)].*dz;
if (~isfield(options,'X'))|(~isfield(options,'Y'))|(~isfield(options,'Z'));
[options.X options.Y options.Z]=meshgrid(x1,y1,z1);
end
if nx>1, h_x=options.X-x1(ceil(nx_c/2)+1);else;h_x=options.X;end
if ny>1, h_y=options.Y-y1(ceil(ny_c/2)+1);else;h_y=options.Y;end
if nz>1, h_z=options.Z-z1(ceil(nz_c/2)+1);else;h_z=options.Z;end
if nz==1;
C=precal_cov([0 0],[h_x(:) h_y(:)],Va);
elseif ny==0;
C=precal_cov([0 0],[h_x(:) h_z(:)],Va);
elseif nx==0;
C=precal_cov([0 0],[h_y(:) h_z(:)],Va);
else
C=precal_cov([0 0 0],[h_x(:) h_y(:) h_z(:)],Va);
end
options.C=reshape(C,ny_c,nx_c,nz_c);
end
%% COMPUTE FFT and PAD
if ~isfield(options,'fftC');
options.fftC=fftn(fftshift(options.C));
end
%% normal deviates
if isfield(options,'z_rand')
% use given set
z_rand=options.z_rand;
else
% create a new set
z_rand=randn(size(options.fftC));
end
%% RESIMULATION
if isfield(options,'lim');
if options.wx > (size(z_rand,2)-nx);options.wx=size(z_rand,2)-nx;end
if options.wy > (size(z_rand,1)-ny);options.wy=size(z_rand,1)-ny;end
if options.wz > (size(z_rand,3)-nz);options.wz=size(z_rand,3)-nz;end
if options.resim_type==1;
% BOX TYPE RESIMULATION
if isfield(options,'pos');
% NEXT LINE MAY BE PROBLEMATIC USING NEIGHBORHOODS
[options.used]=set_resim_data_3d(x_all,y_all,z_all,z_rand,options.lim,options.pos,options.wrap_around);
else
% CHOOSE CENTER OF BOX AUTOMATICALLY
x0=ceil((rand(1)*(nx+2*options.wx)))-ceil(options.wx);
y0=ceil((rand(1)*(ny+2*options.wy)))-ceil(options.wy);
z0=ceil((rand(1)*(nz+2*options.wz)))-ceil(options.wz);
if x0<1; x0=size(z_rand,2)+x0;end
if y0<1; y0=size(z_rand,1)+y0;end
if z0<1; z0=size(z_rand,3)+z0;end
if x0>size(z_rand,2); x0=x0-size(z_rand,2);end
if y0>size(z_rand,1); y0=y0-size(z_rand,1);end
if z0>size(z_rand,3); z0=z0-size(z_rand,3);end
options.pos_use=[x_all(x0) y_all(y0) z_all(y0)];
[options.used]=set_resim_data_3d([1:size(z_rand,2)]*dx,[1:size(z_rand,1)]*dy,[1:size(z_rand,3)]*dz,z_rand,options.lim,options.pos_use,options.wrap_around,options.X,options.Y,options.Z);
end
ii=find(options.used==0);
z_rand_new=randn(size(z_rand(ii)));
z_rand(ii) = z_rand_new;
else
% RANDOM SET TYPE RESIMULATION
z_rand_in=z_rand;
n_resim=options.lim(1);
nz_rand=prod(size(z_rand));
if n_resim<=1
% use n_resim as a proportion of all random deviates
n_resim=n_resim.*nz_rand;
end
if ((n_resim<2)&&(n_resim>1))
n_resim=1;
end
n_resim=floor(n_resim);
n_resim = min([n_resim nz_rand]);
% if (n_resim==nz:rand);
%end
% Select random set of nodes within simulation grid !
ii=randomsample(nz_rand,n_resim);
z_rand_new=z_rand;
z_rand_new(ii)=randn(1,n_resim);
z_rand=z_rand_new;
end
end
%% linear combinartion of the perturbed paramaters
if isfield(options,'gradual')&isfield(options,'z_rand')
if options.gradual<1
if exist('gaussian_linear_combine','file')
i_perturbed=find((options.z_rand-z_rand)~=0);
z_rand(i_perturbed) = gaussian_linear_combine(options.z_rand(i_perturbed),z_rand(i_perturbed),options.gradual,0);
end
end
end
%% inverse FFT
out=(ifftn( sqrt((options.fftC)).*fftn(z_rand) ));
options.out=out;
out=real(out(1:ny,1:nx,1:nz))+options.gmean;
if org.nx==1; out=out(:,1,:); end
if org.ny==1; out=out(1,:,:); end
if org.nz==1; out=out(:,:,1); end
% Prior Likelihood
logL = -.5*sum(z_rand(:).^2);
options.nx=nx;
options.ny=ny;
options.nz=nz;
options.nx_c=nx_c;
options.ny_c=ny_c;
options.nz_c=nz_c;