forked from NOAA-PSL/stochastic_physics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_ca.F90
491 lines (396 loc) · 10.9 KB
/
update_ca.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
module update_ca
use halo_exchange, only: atmosphere_scalar_field_halo
use mersenne_twister, only: random_gauss,random_stat,random_number
use mpi_wrapper, only: mp_reduce_sum,mp_bcst,mp_reduce_min,mp_reduce_max
use mpp_domains_mod, only: domain2D
implicit none
!L. Bengtsson 2017-06
!Evolve the cellular automata in time
contains
subroutine update_cells_sgs(kstep,nca,nxc,nyc,nxch,nych,nlon,nlat,isc,iec,jsc,jec, &
npx,npy,domain_for_coupler,CA,ca_plumes,iini,ilives, &
nlives,ncells,nfracseed,nseed,nthresh,nspinup,nf,nca_plumes)
implicit none
integer, intent(in) :: kstep,nxc,nyc,nlon,nlat,nxch,nych,nca,isc,iec,jsc,jec,npx,npy
integer, intent(in) :: iini(nxc,nyc,nca)
integer, intent(inout) :: ilives(nxc,nyc,nca)
real, intent(out) :: CA(nlon,nlat)
integer, intent(out) :: ca_plumes(nlon,nlat)
integer, intent(in) :: nlives, ncells, nseed, nspinup, nf
real, intent(in) :: nfracseed, nthresh
logical,intent(in) :: nca_plumes
type(domain2D), intent(inout) :: domain_for_coupler
real, dimension(nlon,nlat) :: frac
integer, dimension(nlon,nlat) :: maxlives
integer,allocatable,save :: board(:,:,:), lives(:,:,:)
integer,allocatable :: V(:),L(:),B(:)
integer,allocatable :: AG(:,:)
integer :: inci, incj, i, j, k,sub,spinup,it,halo,k_in,isize,jsize
integer :: ih, jh,kend
real, allocatable :: field_in(:,:),board_halo(:,:,:)
integer, dimension(nxc,nyc) :: neighbours, birth, newlives,thresh,maxliveshigh
integer, dimension(nxc,nyc) :: neg, newcell, oldlives, newval,temp,newseed
integer, dimension(ncells,ncells) :: onegrid
real, dimension(nxc,nyc) :: NOISE_B
real, dimension(nxc*nyc) :: noise1D2
!-------------------------------------------------------------------------------------------------
halo=1
isize=nlon+2*halo
jsize=nlat+2*halo
k_in=1
if (.not. allocated(board))then
allocate(board(nxc,nyc,nca))
endif
if (.not. allocated(lives))then
allocate(lives(nxc,nyc,nca))
endif
if (.not. allocated(field_in))then
allocate(field_in(nxc*nyc,1))
endif
if(.not. allocated(board_halo))then
allocate(board_halo(nxch,nych,1))
endif
noise1D2 = 0.0
!
call random_number(noise1D2)
!Put on 2D:
do j=1,nyc
do i=1,nxc
NOISE_B(i,j)=noise1D2(i+(j-1)*nxc)
enddo
enddo
if(kstep <= 1)then
do j=1,nyc
do i=1,nxc
board(i,j,nf) = 0
lives(i,j,nf) = 0
enddo
enddo
endif
if(kstep == 2)then !Initiate CA at kstep 2 as physics field is empty at 0 and 1.
do j=1,nyc
do i=1,nxc
board(i,j,nf) = iini(i,j,nf)
lives(i,j,nf) = ilives(i,j,nf)*iini(i,j,nf)
enddo
enddo
endif
!Seed with new CA cells at each nseed step
newseed = 0
if(mod(kstep,nseed) == 0 .and. kstep >= 2)then
do j=1,nyc
do i=1,nxc
if(board(i,j,nf) == 0 .and. NOISE_B(i,j)>0.95 )then
newseed(i,j) = 1
endif
board(i,j,nf) = board(i,j,nf) + newseed(i,j)
enddo
enddo
endif
if(kstep == 2)then
spinup=nspinup
else
spinup = 1
endif
do it = 1,spinup
!Step 1 - Initialize variables to 0 and extract the halo
CA=0
neighbours=0
birth=0
newlives=0
neg=0
newcell=0
oldlives=0
newval=0
frac=0
board_halo=0
field_in=0
maxlives = 0
maxliveshigh =0
!Step 4 - Compute the neighbourhood
do j=1,nyc
do i=1,nxc
field_in(i+(j-1)*nxc,1)=board(i,j,nf)
enddo
enddo
call atmosphere_scalar_field_halo(board_halo,halo,nxch,nych,k_in,field_in, &
isc,iec,jsc,jec,npx,npy,domain_for_coupler)
do j=1,nyc
do i=1,nxc
ih=i+halo
jh=j+halo
neighbours(i,j)=board_halo(ih-1,jh-1,1)+board_halo(ih-1,jh,1)+ &
board_halo(ih-1,jh+1,1)+board_halo(ih,jh+1,1)+board_halo(ih+1,jh+1,1)+&
board_halo(ih+1,jh,1)+board_halo(ih+1,jh-1,1)+board_halo(ih,jh-1,1)
enddo
enddo
! Step 5 - Check rules;
!birth
do j=1,nyc
do i=1,nxc
if(neighbours(i,j) == 3 .or. neighbours(i,j) ==2)then
birth(i,j)=1
endif
enddo
enddo
!death
do j=1,nyc
do i=1,nxc
if(neighbours(i,j) < 2 .or. neighbours(i,j) > 3)then
lives(i,j,nf)=lives(i,j,nf) - 1
endif
enddo
enddo
do j=1,nyc
do i=1,nxc
if(lives(i,j,nf) < 0)then
lives(i,j,nf)=0
endif
enddo
enddo
do j=1,nyc
do i=1,nxc
if(birth(i,j)==1 .and. lives(i,j,nf)==0)then
newcell(i,j)=1
endif
enddo
enddo
do j=1,nyc
do i=1,nxc
lives(i,j,nf)=lives(i,j,nf)+newcell(i,j)*ilives(i,j,nf)
enddo
enddo
do j=1,nyc
do i=1,nxc
if(neighbours(i,j)==3 .or. (board(i,j,nf)==1 .and. neighbours(i,j)==2))then
board(i,j,nf)=1
else
board(i,j,nf)=0
endif
enddo
enddo
enddo !spinup
!COARSE-GRAIN BACK TO NWP GRID
inci=ncells
incj=ncells
sub=ncells-1
DO j=1,nlat
DO i=1,nlon
CA(i,j)=(SUM(lives(inci-sub:inci,incj-sub:incj,nf)))/real(ncells*ncells)
inci=inci+ncells
ENDDO
inci=ncells
incj=incj+ncells
ENDDO
if(nca_plumes) then
!COMPUTE NUMBER OF CLUSTERS (CONVECTIVE PLUMES) IN EACH CA-CELL
!Note, at the moment we only use the count of the plumes found in a grid-cell
!In the future the routine "plumes" can also be used to give the size of
!each individual plume for better coupling to the convection scheme.
temp=0
do j=1,nyc
do i=1,nxc
if(lives(i,j,1) > 0)then
temp(i,j)=1
endif
enddo
enddo
kend=ceiling((ncells*ncells)/2.)
if (.not. allocated(V))then
allocate(V(kend))
endif
if (.not. allocated(L))then
allocate(L(kend))
endif
if (.not. allocated(B))then
allocate(B(kend))
endif
if (.not. allocated(AG))then
allocate(AG(ncells,ncells))
endif
ca_plumes(:,:)=0
inci=ncells
incj=ncells
sub=ncells-1
DO j=1,nlat
DO i=1,nlon
B(:)=0
L(:)=0
V(:)=0
onegrid(1:ncells,1:ncells)=temp(inci-sub:inci,incj-sub:incj)
call plumes(V,L,AG,onegrid,ncells,ncells,kend)
do k=1,kend
if(V(k)==1)then
B(k)=L(k) !to avoid considering clusters of 0
endif
enddo
ca_plumes(i,j)=MAXVAL(B(1:kend))
inci=inci+ncells
ENDDO
inci=ncells
incj=incj+ncells
ENDDO
else
ca_plumes(:,:)=0.
endif ! nca_plumes
end subroutine update_cells_sgs
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine update_cells_global(kstep,nca,nxc,nyc,nxch,nych,nlon,nlat,isc,iec,jsc,jec, &
npx,npy,domain_for_coupler,CA,iini_g,ilives_g, &
nlives,ncells,nfracseed,nseed,nthresh,nspinup,nf)
implicit none
integer, intent(in) :: kstep,nxc,nyc,nlon,nlat,nxch,nych,nca,isc,iec,jsc,jec,npx,npy
integer, intent(in) :: iini_g(nxc,nyc,nca), ilives_g(nxc,nyc)
real, intent(out) :: CA(nlon,nlat)
integer, intent(in) :: nlives, ncells, nseed, nspinup, nf
real, intent(in) :: nfracseed, nthresh
type(domain2D), intent(inout) :: domain_for_coupler
real, dimension(nlon,nlat) :: frac
integer,allocatable,save :: board_g(:,:,:), lives_g(:,:,:)
integer,allocatable :: V(:),L(:)
integer :: inci, incj, i, j, k ,sub,spinup,it,halo,k_in,isize,jsize
integer :: ih, jh
real, allocatable :: field_in(:,:),board_halo(:,:,:)
integer, dimension(nxc,nyc) :: neighbours, birth, newlives, thresh
integer, dimension(nxc,nyc) :: neg, newcell, oldlives, newval,temp,newseed
real, dimension(nxc,nyc) :: NOISE_B
real, dimension(nxc*nyc) :: noise1D2
!-------------------------------------------------------------------------------------------------
halo=1
isize=nlon+2*halo
jsize=nlat+2*halo
k_in=1
if (.not. allocated(board_g))then
allocate(board_g(nxc,nyc,nca))
endif
if (.not. allocated(lives_g))then
allocate(lives_g(nxc,nyc,nca))
endif
if (.not. allocated(field_in))then
allocate(field_in(nxc*nyc,1))
endif
if(.not. allocated(board_halo))then
allocate(board_halo(nxch,nych,1))
endif
!random numbers:
noise1D2 = 0.0
call random_number(noise1D2)
!Put on 2D:
do j=1,nyc
do i=1,nxc
NOISE_B(i,j)=noise1D2(i+(j-1)*nxc)
enddo
enddo
if(kstep == 0)then
do j=1,nyc
do i=1,nxc
board_g(i,j,nf) = iini_g(i,j,nf)
lives_g(i,j,nf) = ilives_g(i,j)*iini_g(i,j,nf)
enddo
enddo
endif
!Seed with new CA cells at each nseed step
newseed=0
if(mod(kstep,nseed) == 0 .and. kstep > 0.)then
do j=1,nyc
do i=1,nxc
if(board_g(i,j,nf) == 0 .and. NOISE_B(i,j)>0.95 )then
newseed(i,j)=1
endif
board_g(i,j,nf) = board_g(i,j,nf) + newseed(i,j)
enddo
enddo
endif
if(kstep == 0)then
spinup=nspinup
else
spinup = 1
endif
do it=1,spinup
!Step 2 - Initialize variables to 0 and extract the halo
neighbours=0
birth=0
newlives=0
neg=0
newcell=0
oldlives=0
newval=0
frac=0
board_halo=0
field_in=0
!The input to scalar_field_halo needs to be 1D.
!take the updated board_g fields and extract the halo
! in order to have updated values in the halo region.
do j=1,nyc
do i=1,nxc
field_in(i+(j-1)*nxc,1)=board_g(i,j,nf)
enddo
enddo
call atmosphere_scalar_field_halo(board_halo,halo,nxch,nych,k_in,field_in, &
isc,iec,jsc,jec,npx,npy,domain_for_coupler)
do j=1,nyc
do i=1,nxc
ih=i+halo
jh=j+halo
neighbours(i,j)=board_halo(ih-1,jh-1,1)+board_halo(ih-1,jh,1)+ &
board_halo(ih-1,jh+1,1)+board_halo(ih,jh+1,1)+board_halo(ih+1,jh+1,1)+&
board_halo(ih+1,jh,1)+board_halo(ih+1,jh-1,1)+board_halo(ih,jh-1,1)
enddo
enddo
do j=1,nyc
do i=1,nxc
if(neighbours(i,j)==2 .or. neighbours(i,j)==3)then
birth(i,j)=1
endif
enddo
enddo
do j=1,nyc
do i=1,nxc
if(neighbours(i,j)<2 .or. neighbours(i,j)>3)then
lives_g(i,j,nf)=lives_g(i,j,nf) - 1
endif
enddo
enddo
do j=1,nyc
do i=1,nxc
if(lives_g(i,j,nf)<0)then
lives_g(i,j,nf)=0
endif
enddo
enddo
do j=1,nyc
do i=1,nxc
if(birth(i,j)==1 .and. lives_g(i,j,nf)==0)then
newcell(i,j)=1
endif
enddo
enddo
do j=1,nyc
do i=1,nxc
lives_g(i,j,nf)=lives_g(i,j,nf)+newcell(i,j)*ilives_g(i,j)
enddo
enddo
do j=1,nyc
do i=1,nxc
if( (board_g(i,j,nf) ==1 .and. (neighbours(i,j)==3 .or. neighbours(i,j)==2) ).or. (board_g(i,j,nf)==0 .and. neighbours(i,j)==3) )then
board_g(i,j,nf)=1
else
board_g(i,j,nf)=0
endif
enddo
enddo
enddo !spinup
!COARSE-GRAIN BACK TO NWP GRID
inci=ncells
incj=ncells
sub=ncells-1
DO j=1,nlat
DO i=1,nlon
CA(i,j)=(SUM(lives_g(inci-sub:inci,incj-sub:incj,nf)))/(ncells*ncells)
inci=inci+ncells
ENDDO
inci=ncells
incj=incj+ncells
ENDDO
end subroutine update_cells_global
end module update_ca