-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fully_Implicit.m
330 lines (264 loc) · 7.9 KB
/
Fully_Implicit.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%FULLY IMPLICIT METHOD%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% grid and initial properties%%%%%%%%%%%%%%%%%%%%
grid.nx=15;
grid.ny=15;
grid.nz=1;
grid.Pinit=3000;
grid.Swinit=0.25;
grid.Soinit=0.75;
load('permx_final.mat');
grid.kx=reshape(permx,15,15);
grid.kx=grid.kx';
grid.ky=grid.kx;
grid.dx=30;
grid.dy=30;
grid.dz=30;
%%%%%%%%%property allocation
dx=grid.dx;
dy=grid.dy;
dz=grid.dz;
nx=grid.nx;
ny=grid.ny;
nz=grid.nz;
kx=grid.kx;
ky=grid.ky;
Pinit=grid.Pinit;
Swinit=grid.Swinit;
Soinit=grid.Soinit;
rw=0.35;
Pbhp=2900;
Qi=300*5.615;
s=0;%%%%skin
%%%%%%geometric transmissibilities
%%% X -direction
Tx_g=zeros(ny,nx+1);
for i=1:ny
for j=2:nx
K=2*kx(i,j-1)*kx(i,j)/(kx(i,j-1)+kx(i,j));
Tx_g(i,j)=0.00633*K*dy*dz/dx;
end
end
%%% Y-direction
Ty_g=zeros(ny+1,nx);
for i=2:ny
for j=1:nx
K=2*ky(i-1,j)*ky(i,j)/(ky(i-1,j)+ky(i,j));
Ty_g(i,j)=0.00633*K*dx*dz/dy;
end
end
%%%%geometric well index
i=15;
j=15;
ro=0.2*dx;
WI_g=0.00633*(kx(i,j)*ky(i,j))^0.5*dz*(1/(log(ro/rw)+s));
%%%%%%initialize simulator
P_n=Pinit*ones(nx*ny,1);
Sw_n=Swinit*ones(nx*ny,1);
So_n=Soinit*ones(nx*ny,1);
PRESSURE(:,1)=P_n;
SATURATION(:,1)=Sw_n;
T_max=500;
dt=1;
t=0;
timestep=1;
e_p=1e-04;
e_s=1e-03;
Niter=10;
%%%%%%%%%%%
while t<T_max
P_n1=1.05*P_n;
Sw_n1=1.005*Sw_n;
P_n2d=reshape(P_n,nx,ny);
P_n2d=P_n2d';
Sw_n2d=reshape(Sw_n,nx,ny);
Sw_n2d=Sw_n2d';
P_v0=P_n1;%%%%%%assign the pressure and saturation at zero level of iteration
Sw_v0=Sw_n1;
iter=0;
disp('simulation time');
disp(t);
while iter<Niter
%%% iteration%%
P_v02d=reshape(P_v0,nx,ny);
P_v02d=P_v02d';
Sw_v02d=reshape(Sw_v0,nx,ny);
Sw_v02d=Sw_v02d';
%%%%%%Transmissibility calculation%%%%
[ Tx_o,Ty_o,Tx_w,Ty_w ] = Trans(P_v02d,Sw_v02d,Tx_g,Ty_g);
%%%%%%%%%%residual vector calculation%%%%%
for i=1:ny
for j=1:nx
Res_w = Rw( P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','none',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g);
Res_o = Ro( P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','none',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g);
Res_d=[Res_w;Res_o];
if i==1 &&j==1
Res=Res_d;
else
Res=[Res;Res_d];
end
end
end
%%%%%%Jacobian calculation
e1=1e-06;
%%% centre diagonal%%%%
for i=1:ny
for j=1:nx
c11= Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'ij','none',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
c12= Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','ij',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
c21= Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'ij','none',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
c22= Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','ij',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
c=(1/e1)*[c11 c12;c21 c22];
if i==1 &&j==1
C=c;
else
C=[C;c];
end
end
end
C=mat2cell(C,2*ones(nx*ny,1),2);
C=blkdiag(C{:,1});
%%%i+1 diagonal
for i=1:ny
for j=1:nx
if j==nx
b=zeros(2,2);
else
b11=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'+i','none',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
b12=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','+i',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
b21=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'+i','none',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
b22=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','+i',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
b=(1/e1)*[b11 b12; b21 b22];
end
if i==1 &&j==1
B=b;
else
B=[B;b];
end
end
end
B=mat2cell(B,2*ones(nx*ny,1),2);
B{1,1}=[zeros(2,2),B{1,1}];
B=blkdiag(B{1:end-1,1});
B=[B;zeros(2,2*nx*ny)];
%%i-1 diagonal
for i=1:ny
for j=1:nx
if j==1
a=zeros(2,2);
else
a11=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'-i','none',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
a12=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','-i',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
a21=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'-i','none',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
a22=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','-i',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
a=(1/e1)*[a11 a12; a21 a22];
end
if i==1 &&j==1
A=a;
else
A=[A;a];
end
end
end
A=mat2cell(A,2*ones(nx*ny,1),2);
A=blkdiag(A{2:end,1});
A=[zeros(2,2*nx*ny-2);A];
A=[A,zeros(2*nx*ny,2)];
%%%%%%j+1 diagonal
for i=1:ny
for j=1:nx
if i==ny
e=zeros(2,2);
else
e11=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'+j','none',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
e12=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','+j',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
e21=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'+j','none',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
e22=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','+j',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
e=(1/e1)*[e11 e12; e21 e22];
end
if i==1 &&j==1
E=e;
else
E=[E;e];
end
end
end
E=mat2cell(E,2*ones(nx*ny,1),2);
E{1,1}=[zeros(2,2*nx),E{1,1}];
E=blkdiag(E{1:end-nx,1});
E=[E;zeros(2*nx,2*nx*ny)];
%%%%%j-1 diagonal
for i=1:ny
for j=1:nx
if i==1
d=zeros(2,2);
else
d11=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'-j','none',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
d12=Rw(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','-j',Tx_w,Ty_w,Tx_g,Ty_g,dt,WI_g)-Res(2*j-1+2*(i-1)*nx);
d21=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'-j','none',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
d22=Ro(P_v02d,P_n2d,Sw_v02d,Sw_n2d,i,j,'none','-j',Tx_o,Ty_o,Tx_g,Ty_g,dt,WI_g)-Res(2*j+2*(i-1)*nx);
d=(1/e1)*[d11 d12; d21 d22];
end
if i==1 &&j==1
D=d;
else
D=[D;d];
end
end
end
D=mat2cell(D,2*ones(nx*ny,1),2);
D=blkdiag(D{nx+1:end,1});
D=[zeros(2*nx,2*nx*ny-2*nx);D];
D=[D,zeros(2*nx*ny,2*nx)];
J= C+B+A+E+D;%%%%%JAcobian matrix
tic;
delta=-sparse(J)\Res;
t2=toc;
% tic;
% tol=1e-10;
% setup.type = 'nofill'; % compute ILU(0)
% [L,U] = ilu(sparse(J), setup );
% maxit = 1000;
% [delta, flag, relres, iter_d] = bicgstab(sparse(J), Res, tol, maxit, L,U);
% t2=toc;
ctr=0;
for i=1:2:2*nx*ny-1
ctr=ctr+1;
deltaP_v1(ctr,1)=delta(i,1);
deltaSw_v1(ctr,1)=delta(i+1,1);
end
for i=1:225
P_v1(i,1)=P_v0(i,1)+deltaP_v1(i,1);
Sw_v1(i,1)=Sw_v0(i,1)+deltaSw_v1(i,1);
end
if norm(P_v1-P_v0)<=e_p && norm(Sw_v1-Sw_v0)<=e_s
P_n1=P_v1;
Sw_n1=Sw_v1;
break;
end
P_v0=P_v1;
Sw_v0=Sw_v1;
iter=iter+1;
disp('iterations')
disp(iter);
if iter>Niter
dt=dt/2;
iter=0;
end
disp('timestep size');
disp(dt);
end %%%%%%%%%%%%%%end of newton-raphson loop
t=t+dt;
timestep=timestep+1;
PRESSURE(:,timestep)=P_n1;
SATURATION(:,timestep)=Sw_n1;
uw=1;
WIf_w=Krw(Sw_n1(225,1))/(uw*Bw(P_n1(225,1)));
WIf_o=Kro(Sw_n1(225,1))/(Uo(P_n1(225,1))*Bo(P_n1(225,1)));
Rate_w(timestep-1)=WIf_w*WI_g*(P_n1(225,1)-Pbhp)/5.615;
Rate_o(timestep-1)=WIf_o*WI_g*(P_n1(225,1)-Pbhp)/5.615;
%%%Reassign pressure at n time level
P_n=P_n1;
Sw_n=Sw_n1;
end %% end of timestep loop