-
Notifications
You must be signed in to change notification settings - Fork 8
/
RIchard3d_Demo.m
320 lines (243 loc) · 8.56 KB
/
RIchard3d_Demo.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
311
312
313
314
315
316
function [] = RIchard3d_Demo()
% Richard3d solver demo funtion, Uisng Pircards iteration on Dirichlet and
% homogeneous Neumann Boundary condition. The permeability field is
% heterogeneous.
%
% Input parameters:
%
% Output parameters:
%
% Examples:
%
% See also:
% Author: Wei Xing
% History: 26/07/2017 Document and modification
%
% Log:
% Version1.0 initial filed
%% Setup
% Spatial setup
lengthZ=10;
deltaZ=1;
nZ=lengthZ/deltaZ+1;
lengthX=10;
deltaX=1;
nX=lengthX/deltaX+1;
lengthY=10;
deltaY=1;
nY=lengthY/deltaY+1;
% Temporal setup
lengthT=50;
deltaT=1;
nTime=lengthT/deltaT;
% Iteration solver setup
nMaxIteration=100;
maxIteError=1;
% Generate grid
[Z,X,Y] = ndgrid(0:deltaZ:lengthZ,0:deltaX:lengthX,0:deltaY:lengthY);
%% Define Boundary conditions
iBC=2;
switch iBC
case 1 % Unstable!!
% Option 1. Dirichlet boundary conditions on all faces.
% Make nodeIndex matrix. It indicate the sequence nodes are accessed and the type of node.
nodeIndex=zeros(nZ,nX,nY);
nodeIndex(2:end-1,2:end-1,2:end-1)=reshape(uint32(1:(nZ-2)*(nX-2)*(nY-2)), (nZ-2),(nX-2),(nY-2));
% nodeIndex indicates the node type.
% -nodeIndex= 0: node on Dirichlet boundary
% -nodeIndex= integer: free node
% -nodeIndex= - integer: node on Neumann boundary
% -code only accepts homogeneous Neumann BC
nodeInFieldIndex=find(nodeIndex);
%%%
%initial state
% H_init=zeros(nZ,nX,nY);
% H_init(2:end-1,2:end-1,2:end-1)=-61.5;
H_init=ones(nZ,nX,nY)*-21.5;
%BC values
bcFront=ones(nZ,nX)*-20.7;
bcBack =ones(nZ,nX)*-21.7;
bcLeft=ones(nZ,nY)*-20.7;
bcRight=ones(nZ,nY)*-21.7;
bcTop =ones(nX,nY)*-20.7;
bcBottom=ones(nX,nY)*-21.7;
H_init(1:end,1,1:end) =bcLeft;
H_init(1:end,end,1:end) =bcRight;
H_init(1:end,1:end,1) =bcFront;
H_init(1:end,1:end,end) =bcBack;
H_init(1,1:end,1:end) =bcTop;
H_init(end,1:end,1:end) =bcBottom;
case 2 %Recommended for Demo.
% Option 2 Neumann BC on sides and Dirichlet conditions on Top and bottom
nodeIndex=zeros(nZ,nX,nY);
nodeIndex(2:end-1,1:end,1:end)=reshape(uint32(1:(nZ-2)*nX*nY), (nZ-2),nX,nY);
% nodeIndex indicates the node type.
% -nodeIndex= 0: node on Dirichlet boundary
% -nodeIndex= integer: free node
% -nodeIndex= minus integer: node on Neumann boundary
% -code only accepts homogeneous Neumann BC
nodeInFieldIndex=find(nodeIndex);
nodeIndex(:,:,1)=-nodeIndex(:,:,1);
nodeIndex(:,:,end)=-nodeIndex(:,:,end);
nodeIndex(:,1,2:end-1)=-nodeIndex(:,1,2:end-1);
nodeIndex(:,end,2:end-1)=-nodeIndex(:,end,2:end-1);
% Define initial condition
H_init=ones(nZ,nX,nY)*-61.5;
H_init(1,:,:)=ones(nX,nY)*-20.7;
H_init(end,:,:)=ones(nX,nY)*-61.5;
end
%% Initilize mesh
mesh.deltaZ=deltaZ;
mesh.nZ=nZ;
mesh.deltaX=deltaX;
mesh.nX=nX;
mesh.deltaY=deltaY;
mesh.nY=nY;
mesh.nodeIndex=nodeIndex;
mesh.nNode=length(nodeIndex(nodeIndex~=0));
% mesh.nodeInFieldIndex=nodeInFieldIndex;
%% Define for C and K non-linear function
theata_s=0.287;
theata_r=0.075;
alpha=1.611e6;
beta=3.96;
rho=1.175e6;
r=4.74;
% kFromhKs @(h) Ks.*rho./(rho+abs(h).^r);
% K = @(h) Ks.*rho./(rho+abs(h).^r);
K = @(h,Ks) Ks.*rho./(rho+abs(h).^r);
theata = @(h) alpha.*(theata_s-theata_r)/(alpha+abs(h).^beta)+theata_r;
theataDif = @(h) -alpha.*(theata_s-theata_r).*-1.*(alpha+abs(h).^beta).^(-2).*abs(h).^(beta-1);
%% Define Permeability field input
iMethod=2;
switch iMethod
case 1 %directly give mean and covariance to X. Not recommended for realistic case.
scale=0.0094; % overall magnitude of the permeability field. decide the changing speed.
lengthScale=100; %larger number means less stochastic (more correlation as one zooms in the
%field) field. Thus gives smoother result.
nKl=100; %number of KL baisi/cofficients
[nAllNode,nDimension]=size([Z(:),X(:),Y(:)]);
% nKl=nAllNode;
%calculate distance matrix
distance = pdist([Z(:),X(:),Y(:)]);
distanceMatrix = squareform(distance);
%calculate covariance matrix
covMatrix=exp(-distanceMatrix./lengthScale);
% KL decomposition on covariance matrix via SVD/eigen decomposition
[klBasis,klEigenValue,~] = svds(covMatrix,nKl);
% Generate independent normal samples
seed=100;
rng(seed);
sample= randn(nKl,1);
%make multivariate Gaussian distributions with samples. zero mean.
%Covariance specified though KL basis.
% Ks=klBasis*sqrt(klEigenValue)*sample;
% Ks=reshape(Ks,nY,nX);
% a log (multi) normal permeability field
% Ks=exp(Ks);
Ks =exp(klBasis*sqrt(klEigenValue)*sample).*scale;
Ks=reshape(Ks,nZ,nX,nY);
%Plot
bubbleScale=100;
scatter3(X(:),Y(:),Z(:),Ks(:)*bubbleScale,Ks(:)*bubbleScale)
case 2 %Derive mean and covariance of X by given Y. recommended for realistic case.
lengthcale=4;
muY=0.0094;
DeviationRatio=0.4; %set DeviationRatio=10 to see dramatic results.
nSample=1;
nKL=100;
% Ks=permeaField([Z(:),X(:),Y(:)],lengthcale,muY,DeviationRatio,nSample);
Ks=permeaFieldApprox([Z(:),X(:),Y(:)],lengthcale,muY,DeviationRatio,nSample,nKL);
Ks=reshape(Ks,nZ,nX,nY,nSample);
%Plot
bubbleScale=100;
scatter3(X(:),Y(:),Z(:),Ks(:)*bubbleScale,Ks(:)*bubbleScale)
case 3 %interpolation for high resolution permeability. used for fine grid where permeability generation may fail
% WARMING: case 3 is wrong and not working now!!!
lengthcale=4;
muY=0.0094;
DeviationRatio=0.4; %set DeviationRatio=10 to see dramatic results.
nSample=1;
nKL=10;
[coarseZ,coarseX,coarseY] = ndgrid(0:deltaZ*2:lengthZ,0:deltaX*2:lengthX,0:deltaY*2:lengthY); %have to be just fine times
% Ks=permeaField([Z(:),X(:),Y(:)],lengthcale,muY,DeviationRatio,nSample);
Ks=permeaFieldApproxUpscale([coarseZ(:),coarseX(:),coarseY(:)],[Z(:),X(:),Y(:)],lengthcale,muY,DeviationRatio,nSample,nKL);
% Ks=reshape(Ks,nZ,nX,nY,nSample);
Ks=reshape(Ks,nZ,nX,nY,[]);
% Ks=reshape(Ks,nZ,nX,nY);
bubbleScale=100;
scatter3(X(:),Y(:),Z(:),Ks(:)*bubbleScale,Ks(:)*bubbleScale)
end
%% MAIN
mesh.H=H_init; %impose initial condition.
mesh.Ks=Ks;
tic
[hRecord,iteration1] = Richard3dPicardSolver(mesh,nTime,deltaT,nMaxIteration,maxIteError,theataDif,K);
toc
%% Plotting
% H=mesh.H;
figure(1)
plot(iteration1)
title(sprintf('Iteration at each time step'))
% end time pressure
% figure(2)
% scatter3(X(:),Y(:),Z(:),abs(H(:)),abs(H(:)))
% title('end time pressure')
% figure(1)
% p = patch(isosurface(H,-60));
% isonormals(H,p)
% p.FaceColor = 'red';
% p.EdgeColor = 'none';
% daspect([1 1 1])
% view(3);
% axis([nX,nY,nZ ])
% preasure head front surface propogation
figure(3)
for t=1:nTime
clf
Ht=abs(hRecord(:,:,:,t));
p=patch(isosurface(Ht,40));
p.FaceColor = 'red';
p.EdgeColor = 'none';
p=patch(isosurface(Ht,50));
p.FaceColor = 'yellow';
p.EdgeColor = 'none';
p=patch(isosurface(Ht,60));
p.FaceColor = 'blue';
p.EdgeColor = 'none';
view(-60,40)
title(sprintf('pressure front propogation. t=%i',t))
axis([1,nX,1,nY,1,nZ])
camlight
lighting gouraud
drawnow
frame(t)=getframe;
end
xslice = [nX];
yslice = [nY];
zslice = [0:nZ/2:nZ];
figure(3)
slice(mesh.H,nX,1:5:nZ,1)
% preasure head propogation in square volume
figure(4)
for t=1:nTime
% surf(X(:,:,sliceY),Z(:,:,sliceY),TheataRecord(:,:,sliceY,t))
% shading interp;
% slice(TheataRecord(:,:,:,t),nX,nY,0:nZ/4:nZ)
slice(hRecord(:,:,:,t),nX-3,1:5:nZ,1)
view(-60,40)
title(sprintf('pressure. t=%i',t))
drawnow
frame(t)=getframe;
end
% preasure head propogation in sphere volume
figure(5)
for t=1:4:nTime
hVector=(hRecord(:,:,:,t)+100)*10;
% scatter3(X(:),Y(:),Z(:),(hVector(:)),(Ks(:)*10000),'fill')
scatter3(X(:),Y(:),Z(:),(hVector(:)),(hVector(:)),'fill')
% shading interp;
title(sprintf('pressure. t=%i',t))
drawnow
frame(t)=getframe;
end