forked from shirangi/reservoir_simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
well.f90
539 lines (490 loc) · 32 KB
/
well.f90
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
!! 3D oil reservoir Simulator project
!! by Mehrdad Gharib Shirangi From Spring 2010 until Fall 2010
!! mehrdad.ghsh@gmail.com
!! Copy Right by Mehrdad Gharib Shirangi
!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Module Well
Use globalvar
Use Trans
!************************************************************************************************************************************
! Explanations
! This Madul contains some Subroutines, the common point of these subroutines is that they are all about Wells, or Well rates
!Subroutine 'wellIndex_cons' calculates the matrix of constant part of Well Index matrix,
! subroutine wellIndex , multiplies the constant term of Well Index matrix by (kro)/(muo)(Bo) and (krw)/(muw)(Bw) ,
! Needless to say, the Well Index is a matrix with Nwell columns and Nz rows, so for each layer that is not ptoducing 'oil' or 'water ' ,
! the entry of W_Index_O or W_Index_ w will be zero respectively. It is possible that a layer just produces oil or just Water, then kro or
! krw would be zero
! subroutine calc_rate, computes the oil rate and water rate of each well, and puts the value into a vector, the vectors Oilrate and Waterrate are
! Global vectors and are used in other subroutines like 'con_check' and Output
! In subroutine Calc_DeltaPwf, the value of DeltaPwf is calculated for each layer in the well and these values are in a matrix with Nwell columns and Nz rows
! The algorithm used in this sub is from Dr. Li's course Notes . There are 4 cases considered here : Production well, with q oil sp or BHP sp , and
! Injector Well with q water sp, or BHP sp.
! in case of constant rate production or injection, the value of BHP is determined using a loop. This loop is very exact and after BHP is determined, there
! is no need to improve it, as Fwell (would become)= 1d-12
! In case of BHP sp, the iterations aim to precisely calculate del_pwf and the convergence criteria is that oil rate or water rate is not changing with
! recalculating del_pwf
!************************************************************************************************************************************
! contains transmissibility functions
use globalvar
implicit none
Contains
subroutine wellIndex_cons ! calculates the matrix of constant part of Well Index
implicit none
integer:: i_well, i_k, alfa_well , counter , m , n
Do i_well = 1 , Nwell
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
alfa_well = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1 ) * Nx + (i_k - 1 ) * Nx * Ny
R_equ(i_well , i_k ) = 0.28 * sqrt( ky(alfa_well) * Deltax( Well_loc(i_well,1))**2 + kx(alfa_well) * Deltay( Well_loc(i_well,2))**2 ) / ( sqrt(kx(alfa_well)) + sqrt(ky(alfa_well)) )
KH(i_well , i_k) = ( kx(alfa_well) * ky(alfa_well) ) ** (0.5)
! the above terms are not a function of pressure or Saturation, but the below one is, so I can calculate the above terms for once and then just
! calculate the pressure and saturation dependent term during program Run
Well_index_cons(i_well , i_k ) = 2 * 3.14159265 * betac * KH(i_well , i_k) * Deltaz(alfa_well) / log( R_equ(i_well,i_k) / r_w(i_well) )
EndDo
EndDo
n = n_Kr
m = 1
do while (abs(n-m)>1)
if (SW_AtSor < Sw_mat(floor( m + (n-m+1)/2.0) ) ) then
n = floor(m + ( n - m + 1) / 2.0);
else
m = floor(m + ( n - m + 1) / 2.0);
end if
end do
kr_inj = krw(m)+ (SW_AtSor -Sw_mat(m))/(Sw_mat(n)-Sw_mat(m))*(krw(n)-krw(m))
Endsubroutine wellIndex_cons
!********************************************************************************************************
subroutine wellIndex ! this sub would be called at each time step from solver subroutine
integer:: i_well, i_k, alfa_well , counter
!counter = 0
W_index_o = 0.0
W_index_w = 0.0
Do i_well = 1 , Nwell
if ( W_type(i_well) == 'inje') then
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
alfa_well = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1 ) * Nx + ( i_k - 1) * Nx * Ny
W_index_o(i_well,i_k) = 0.0
if ( k_r(alfa_well , 'w' ) .GT. kr_inj ) then
W_index_w(i_well,i_k) = Well_index_cons(i_well,i_k) * k_r(alfa_well , 'w' ) / (mu_f( pressure(alfa_well),'w' ) * B_f( W_press(alfa_well) , 'w' ) )
Else
W_index_w(i_well,i_k) = Well_index_cons(i_well,i_k) * kr_inj / (mu_f( pressure(alfa_well),'w' ) * B_f( W_press(alfa_well) , 'w' ) )
EndIf
EndDo
Else
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
alfa_well = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1 ) * Nx + ( i_k - 1) * Nx * Ny
W_index_o(i_well,i_k) = Well_index_cons(i_well,i_k) * k_r(alfa_well , 'o' ) / (mu_f( pressure(alfa_well),'o' ) * B_f( pressure(alfa_well) , 'o' ) )
W_index_w(i_well,i_k) = Well_index_cons(i_well,i_k) * k_r(alfa_well , 'w' ) / (mu_f( pressure(alfa_well),'w' ) * B_f( W_press(alfa_well) , 'w' ) )
EndDo
EndIF
EndDO
Endsubroutine wellIndex
!********************************************************************************************************
subroutine calc_rate
Implicit none
integer :: i_well , alfa_w , alfa_wprim , alfa_wpp , i_k ,i , W_index
integer :: counter
real*8 :: qo, qw
! writing oil rates to a matrix
oilrate = 0.0
waterrate = 0.0
TOILP = 0
WOR = 0
If (Nz .NE. 1) then
call Calc_DeltaPwf
EndIf
counter = 0
WOR = 0
Do i_well = 1, Nwell
if ( ( W_type(i_well) == 'prod') .AND. ( Schedule(i_well , 2 , sche_N) .NE. 0) )then
qo = 0
qw = 0
W_index = Ntotal + i_well ! Pwf position in pressure vector
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
alfa_w = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1) * Nx * Ny
qo = qo + w_index_o(i_well, i_k ) * (pressure(alfa_w) - pressure(W_index ) - Del_Pwf(i_well, i_k))
qoL(i_well, i_k) = w_index_o(i_well, i_k ) * (pressure(alfa_w) - pressure(W_index ) - Del_Pwf(i_well, i_k))
EndDO
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
alfa_w = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1) * Nx * Ny
qw = qw + w_index_w(i_well, i_k ) * (pressure(alfa_w) - pressure(W_index ) - Del_Pwf(i_well, i_k))
qwL(i_well, i_k) = w_index_w(i_well, i_k ) * (pressure(alfa_w) - pressure(W_index ) - Del_Pwf(i_well, i_k))
EndDo
oilrate(i_well) = qo
waterRate(i_well) = qw
if ( abs(qo) .GE. 1 ) then
WOR(i_well) = qw / qo
EndIf
TOILP = TOILP + oilrate(i_well) * d_time !! Total OIL Production in that Time_step
! if ( Schedule(i_well , 3 ,sche_N) == 0.0 ) then
! oilrate (i_well) = 0
! waterRate(i_well) = 0
! EndIf
ElseIf ( Schedule(i_well , 2 , sche_N) .NE. 0) Then
oilrate (i_well) = 0
qw = 0
W_index = Ntotal + i_well ! Pwf position in pressure vector
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
alfa_w = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1) * Nx * Ny
qw = qw + w_index_w(i_well, i_k ) * (pressure(alfa_w) - pressure(W_index ) - Del_Pwf(i_well, i_k))
EndDO
waterRate(i_well) = qw
EndIf
EndDo
Endsubroutine calc_rate
!********************************************************************************************************
subroutine Calc_DeltaPwf ! calculates the matrix of Del_Pwf
implicit none
integer :: i_well, i_k, alfa_well , alfa_w , W_index , B_index , N_index , ii , counter
integer :: con ! convergence , when == 0 , not converged
Real, Allocatable, Dimension(:) :: O_Volume, W_Volume , mix_den ! O_Volume is the vector of Volume of oil in each completion ( for dt = 1 day , and dp = 1 psi )
Real, Allocatable, Dimension(:) :: qoN , qoP , qwN , qwP ! q oil New, q water New ... for convergence check in case of BHPsp
real*8 :: denom , eps_pwf , Fwell ,max_dif , c1 , c2
Del_Pwf = 0
Allocate ( O_Volume(Nz) )
Allocate ( W_Volume(Nz ) ) ! Well_k(i_well ,2) - Well_k(i_well ,1)
Allocate ( mix_den(Nz ) )
Allocate ( qoN(Nz) )
Allocate ( qwN(Nz) )
Allocate ( qoP(Nz) )
Allocate ( qwP(Nz) )
Do i_well = 1 , Nwell
W_index = Ntotal + i_well ! Pwf position in pressure vector
alfa_w = 2 * Ntotal + i_well
if ( W_type(i_well) == 'prod') then
if ( Schedule(i_well , 2 , sche_N) .EQ. 1 ) then ! oil rate is given
counter = 0
con = 0 ! convergence criteria
Do while (con == 0)
if ( counter .NE. 0 ) then
! checking convergence
max_dif = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
! finding the maximum difference
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
max_dif = max ( max_dif , abs( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) ) )
EndDo
if ( (max_dif < 1.0D-7 ) .OR. ( abs(eps_pwf / max_dif) < 0.01 ) ) then
con = 1
exit
EndIf
EndIf
counter = counter + 1
O_Volume = 0
W_Volume = 0
mix_den = 0
i_K = Well_k(i_well ,2)
! stage 1 : obtaining Oil Volume and Water Volume ,
Do while (i_k .GE. Well_k(i_well , 1) )
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
O_Volume(i_k) = w_index_o(i_well, i_k ) * 1.0 * ( pressure(B_index) - pressure(W_index) - Del_Pwf(i_well, i_k) )
W_Volume(i_k) = w_index_w(i_well, i_k ) * 1.0 * ( pressure(B_index) - pressure(W_index) - Del_Pwf(i_well, i_k) )
if (i_k .NE. Well_k(i_well ,2) ) then
O_Volume(i_k) = O_Volume(i_k) + O_Volume(i_k + 1)
W_Volume(i_k) = W_Volume(i_k) + W_Volume(i_k + 1)
EndIf
mix_den(i_k) = ( W_Volume(i_k) * den_w + O_Volume(i_k) * den_o ) / ( W_Volume(i_k) * den_w / density( pressure(W_index) + Del_Pwf(i_well, i_k),'w') + O_Volume(i_k) * den_o / density( pressure(W_index) + Del_Pwf(i_well, i_k),'o' ) )
! BHP or Pwf for oil and water are the same , so pressure( W_index) works for both
i_K = i_K - 1
EndDo
! stage 2 : obtaining Del_pwf
Do i_k = Well_k(i_well , 1) + 1 , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
N_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 - 1) * Nx * Ny
Del_Pwf(i_well , i_k ) = mix_den(i_k - 1) / 144.0 *( Deltaz(B_index) + Deltaz(N_index) ) / 2.0
!if (i_k .NE. Well_k(i_well , 1) + 1 ) then ! if this is not the first layer which has del_pwf
Del_Pwf(i_well , i_k ) = Del_Pwf(i_well , i_k ) + Del_Pwf(i_well , i_k - 1)
EndDO
! stage 3 : modifying pwf
Fwell = - Schedule(i_well , 3 , sche_N)
denom = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
Fwell = Fwell + w_index_o(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k))
denom = denom + w_index_o(i_well, i_k )
EndDO
! if ( Schedule(i_well , 3 , sche_N) .EQ. 0.0 ) then ! total liquid rate == 0
! Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
! B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
! Fwell = Fwell + w_index_w(i_well, i_k ) * ( W_press(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k))
! denom = denom + w_index_w(i_well, i_k )
! EndDo
! EndIf
eps_pwf = Fwell / denom
pressure(W_index ) = pressure(W_index ) + eps_pwf
YY(alfa_w) = YY(alfa_w) + eps_pwf
EndDO
Elseif ( Schedule(i_well , 2 , sche_N) .EQ. 3 ) then ! Total Liquid Rate Specified
counter = 0
con = 0 ! convergence criteria
Do while (con == 0)
if ( counter .NE. 0 ) then
! checking convergence
max_dif = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
! finding the maximum difference
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
max_dif = max ( max_dif , abs( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) ) )
EndDo
if ( (max_dif < 1.0D-7 ) .OR. abs(eps_pwf / max_dif) < 0.01) then
con = 1
exit
EndIf
EndIf
counter = counter + 1
O_Volume = 0
W_Volume = 0
mix_den = 0
i_K = Well_k(i_well ,2)
! stage 1 : obtaining Oil Volume and Water Volume ,
Do while (i_k .GE. Well_k(i_well , 1) )
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
O_Volume(i_k) = w_index_o(i_well, i_k ) * 1.0 * ( pressure(B_index) - pressure(W_index) - Del_Pwf(i_well, i_k) )
W_Volume(i_k) = w_index_w(i_well, i_k ) * 1.0 * ( pressure(B_index) - pressure(W_index) - Del_Pwf(i_well, i_k) )
if (i_k .NE. Well_k(i_well ,2) ) then
O_Volume(i_k) = O_Volume(i_k) + O_Volume(i_k + 1)
W_Volume(i_k) = W_Volume(i_k) + W_Volume(i_k + 1)
EndIf
mix_den(i_k) = ( W_Volume(i_k) * den_w + O_Volume(i_k) * den_o ) / ( W_Volume(i_k) * den_w / density( pressure(W_index) + Del_Pwf(i_well, i_k),'w') + O_Volume(i_k) * den_o / density( pressure(W_index) + Del_Pwf(i_well, i_k),'o' ) )
! BHP or Pwf for oil and water are the same , so pressure( W_index) works for both
i_K = i_K - 1
EndDo
! stage 2 : obtaining Del_pwf
Do i_k = Well_k(i_well , 1) + 1 , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
N_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 - 1) * Nx * Ny
Del_Pwf(i_well , i_k ) = mix_den(i_k - 1) / 144.0 *( Deltaz(B_index) + Deltaz(N_index) ) / 2.0
!if (i_k .NE. Well_k(i_well , 1) + 1 ) then ! if this is not the first layer which has del_pwf
Del_Pwf(i_well , i_k ) = Del_Pwf(i_well , i_k ) + Del_Pwf(i_well , i_k - 1)
EndDO
! stage 3 : modifying pwf
Fwell = - Schedule(i_well , 3 , sche_N)
denom = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
Fwell = Fwell + w_index_o(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k)) &
+ w_index_w(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k))
denom = denom + w_index_o(i_well, i_k ) + w_index_w(i_well, i_k )
EndDO
eps_pwf = Fwell / denom
pressure(W_index ) = pressure(W_index ) + eps_pwf
YY(alfa_w) = YY(alfa_w) + eps_pwf
EndDO
ElseIf ( Schedule(i_well , 2 , sche_N) .EQ. 2 ) then ! BHP specified case
qoN = 0
qoP = 0
qwN = 0
qwP = 0
counter = 0
con = 0 ! convergence criteria
Do while (con == 0)
counter = counter + 1
O_Volume = 0
W_Volume = 0
mix_den = 0
i_K = Well_k(i_well ,2)
! stage 1 : obtaining Oil Volume and Water Volume ,
Do while (i_k .GE. Well_k(i_well , 1) )
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
O_Volume(i_k) = w_index_o(i_well, i_k ) * 1.0 * ( pressure(B_index) - pressure(W_index) - Del_Pwf(i_well, i_k) )
W_Volume(i_k) = w_index_w(i_well, i_k ) * 1.0 * ( pressure(B_index) - pressure(W_index) - Del_Pwf(i_well, i_k) )
if (i_k .NE. Well_k(i_well ,2) ) then
O_Volume(i_k) = O_Volume(i_k) + O_Volume(i_k + 1)
W_Volume(i_k) = W_Volume(i_k) + W_Volume(i_k + 1)
EndIf
mix_den(i_k) = ( W_Volume(i_k) * den_w + O_Volume(i_k) * den_o ) / ( W_Volume(i_k) * den_w / density( pressure(W_index) + Del_Pwf(i_well, i_k),'w') + O_Volume(i_k) * den_o / density( pressure(W_index) + Del_Pwf(i_well, i_k),'o' ) )
! BHP or Pwf for oil and water are the same , so pressure( W_index) works for both
i_K = i_K - 1
EndDo
! stage 2 : obtaining Del_pwf
Do i_k = Well_k(i_well , 1) + 1 , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
N_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 - 1) * Nx * Ny
Del_Pwf(i_well , i_k ) = mix_den(i_k - 1) / 144.0 *( Deltaz(B_index) + Deltaz(N_index) ) / 2.0
!if (i_k .NE. Well_k(i_well , 1) + 1 ) then ! if this is not the first layer which has del_pwf
Del_Pwf(i_well , i_k ) = Del_Pwf(i_well , i_k ) + Del_Pwf(i_well , i_k - 1)
EndDO
! stage 3 : convergence
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
qoN(i_k) = w_index_o(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) )
qwN(i_k) = w_index_w(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) )
EndDo
if ( counter .NE. 1 ) then
! checking convergence
max_dif = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
if ( abs(qwN(i_k)) > 1 ) then
max_dif = max ( max_dif , abs(qwN(i_k) - qwP(i_k) ) /qwN(i_k) )
EndIf
if ( abs(qoN(i_k)) > 1 ) then
max_dif = max ( max_dif , abs(qoN(i_k) - qoP(i_k) ) /qoN(i_k) )
EndIf
EndDo
if (max_dif < 0.000001) then
con = 1
!exit
EndIf
EndIf
qoP = qoN
qwP = qwN
EndDO
EndIf
Else ! Injection Well
if ( Schedule(i_well , 2 , sche_N) .EQ. 1 ) then ! injection rate is given
counter = 0
con = 0 ! convergence criteria
Do while (con == 0)
if ( counter .NE. 0 ) then
! checking convergence
max_dif = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
! finding the maximum difference
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
max_dif = max ( max_dif , abs( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) ) )
EndDo
if (abs(eps_pwf / max_dif) < 0.01) then
con = 1
exit
EndIf
EndIf
counter = counter + 1
! obtaining Del_pwf
Do i_k = Well_k(i_well , 1) + 1 , Well_k(i_well ,2)
c1 = 0
c2 = 2
Do while ( abs(c1-c2) > 0.1)
c1 = Del_Pwf(i_well , i_k )
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
N_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 - 1) * Nx * Ny
Del_Pwf(i_well , i_k ) = density( pressure(W_index) + Del_Pwf(i_well, i_k),'w') / 144.0 *( Deltaz(B_index) + Deltaz(N_index) ) / 2.0
!if (i_k .NE. Well_k(i_well , 1) + 1 ) then ! if this is not the first layer which has del_pwf
Del_Pwf(i_well , i_k ) = Del_Pwf(i_well , i_k ) + Del_Pwf(i_well , i_k - 1)
c2 = Del_Pwf(i_well , i_k )
EndDo
EndDO
! stage 3 : modifying pwf
Fwell = Schedule(i_well , 3 , sche_N)
denom = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
Fwell = Fwell + w_index_w(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k))
denom = denom + w_index_w(i_well, i_k )
EndDO
eps_pwf = Fwell / denom
pressure(W_index ) = pressure(W_index ) + eps_pwf
YY(alfa_w) = YY(alfa_w) + eps_pwf
EndDO
Elseif ( Schedule(i_well , 2 , sche_N) .EQ. 2 ) then ! BHP of Injection specified case
qwN = 0
qwP = 0
counter = 0
con = 0 ! convergence criteria
Do while (con == 0)
counter = counter + 1
O_Volume = 0
W_Volume = 0
mix_den = 0
i_K = Well_k(i_well ,2)
! stage 2 : obtaining Del_pwf
Do i_k = Well_k(i_well , 1) + 1 , Well_k(i_well ,2)
c1 = 0
c2 = 2
Do while ( abs(c1-c2) > 0.1)
c1 = Del_Pwf(i_well , i_k )
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 ) * Nx * Ny
N_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 - 1) * Nx * Ny
Del_Pwf(i_well , i_k ) = density( pressure(W_index) + Del_Pwf(i_well, i_k),'w') / 144.0 *( Deltaz(B_index) + Deltaz(N_index) ) / 2.0
!if (i_k .NE. Well_k(i_well , 1) + 1 ) then ! if this is not the first layer which has del_pwf
Del_Pwf(i_well , i_k ) = Del_Pwf(i_well , i_k ) + Del_Pwf(i_well , i_k - 1)
c2 = Del_Pwf(i_well , i_k )
EndDo
EndDO ! stage 3 : convergence
if ( counter .NE. 1 ) then
! checking convergence
max_dif = 0
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
qwN(i_k) = w_index_w(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) )
max_dif = max ( max_dif , abs(qwN(i_k) - qwP(i_k) ) /qwN(i_k) )
EndDo
if (max_dif < 0.01) then
con = 1
!exit
EndIf
Else
Do i_k = Well_k(i_well , 1) , Well_k(i_well ,2)
B_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx + ( i_k - 1 )* Nx * Ny
qoN(i_k) = w_index_o(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) )
qwN(i_k) = w_index_w(i_well, i_k ) * ( pressure(B_index) - pressure(W_index ) - Del_Pwf(i_well, i_k) )
EndDo
EndIf
qoP = qoN
qwP = qwN
EndDO
EndIf
EndIf
EndDo
DeAllocate (O_Volume)
DeAllocate (W_Volume)
DeAllocate (mix_den)
Endsubroutine Calc_DeltaPwf
!********************************************************************************************************
! subroutine calc_fwell
!
! Endsubroutine calc_fwell
!********************************************************************************************************
subroutine check_BHP
integer :: i_well , W_index, alfa_w
integer :: iwell_sche ! Number of well that its schedule has changed
integer :: sche_type , b_index ! type of new schedule
real*8 :: sche_val ! value of new schdule
Logical :: change_Sche ! If a schdule has changed , it would be .True.
change_Sche = .False.
! If (time == 500) then
! time = 500
! endIf
Do i_well = 1 , Nwell
b_index = Well_loc(i_well,1) + ( Well_loc(i_well,2) - 1) * Nx
change_Sche = .False.
W_index = Ntotal + i_well ! Pwf position in pressure vector
alfa_w = 2 * Ntotal + i_well
if ( W_type(i_well) == 'prod') then
if ( WOR(i_well) > max_WOR) then ! shut in the well
change_Sche = .True.
Schedule(i_well , 2 , sche_N) = 1 ! switching to zero rate
Schedule(i_well , 3 , sche_N) = 0.0
iwell_sche = i_well ; sche_type = 1 ; sche_val = 0.0 ; change_Sche = .True. ; exit
EndIf
if ((( Schedule(i_well , 2 , sche_N) .EQ. 1 ) .OR. ( Schedule(i_well , 2 , sche_N) .EQ. 3 ) ) .AND. ( Schedule(i_well , 3 , sche_N) .NE. 0 ) )then ! oil rate is given
if ( pressure(W_index) < minBHP) then
change_Sche = .True.
Schedule(i_well , 2 , sche_N) = 2 ! switching to constant BHP
Schedule(i_well , 3 , sche_N) = minBHP
iwell_sche = i_well ; sche_type = 2 ; sche_val = minBHP ; change_Sche = .True. ; exit
EndIF
EndIf
Else
if ( Schedule(i_well , 2 , sche_N) .EQ. 1 ) then ! injection rate is given
if ( pressure(W_index) > maxBHP) then
change_Sche = .True.
Schedule(i_well , 2 , sche_N) = 2 ! switching to constant BHP
Schedule(i_well , 3 , sche_N) = maxBHP
iwell_sche = i_well ; sche_type = 2 ; sche_val = minBHP ; change_Sche = .True. ; exit
EndIF
EndIf
EndIF
If (change_Sche) then
Open(2,DEFAULTFILE='.\sim_output\' , FILE='WARNING_Change of Schedule.DAT', status='unknown' , access = 'append')
write(2,*) 'Time =' , Time
write(2,*) 'well # ', iwell_sche
write(2,*) 'New Schedule Type', Schedule(iwell_sche , 2 , sche_N)
write(2,*) 'New Schedule Value', Schedule(iwell_sche, 3 , sche_N)
close(2)
EndIf
EndDo
Endsubroutine check_BHP
!********************************************************************************************************
EndModule Well