forked from NOAA-PSL/stochastic_physics
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cellular_automata_sgs.F90
393 lines (337 loc) · 12.3 KB
/
cellular_automata_sgs.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
module cellular_automata_sgs_mod
use update_ca, only : domain_global,domain_sgs,iscnx,iecnx,jscnx,jecnx,isdnx,iednx,jsdnx,jednx,nxncells,nyncells,cold_start_ca_sgs
implicit none
contains
subroutine cellular_automata_sgs(kstep,dtf,restart,first_time_step,sst,lsmsk,lake,uwind,vwind,height,dx,condition_cpl, &
ca_deep_cpl,ca_turb_cpl,ca_shal_cpl,domain_in, &
nblks,isc,iec,jsc,jec,npx,npy,nlev,nthresh, mytile, &
nca,ncells,nlives,nfracseed,nseed,iseed_ca,ca_advect, &
nspinup,ca_trigger,blocksize,mpiroot,mpicomm)
use mpi_f08
use kinddef, only: kind_phys,kind_dbl_prec
use update_ca, only: update_cells_sgs, define_ca_domain
use random_numbers, only: random_01_CB
use mpp_domains_mod, only: domain2D,mpp_get_global_domain,CENTER, mpp_get_data_domain, mpp_get_compute_domain,&
mpp_define_io_domain,mpp_get_io_domain_layout,mpp_get_ntile_count
use block_control_mod, only: block_control_type, define_blocks_packed
use time_manager_mod, only: time_type
use mpi_wrapper, only: mype,mp_reduce_max, &
mpi_wrapper_initialize
implicit none
!L.Bengtsson, 2017-06
!L.Bengtsson, 2021-05
!Significant cleaning of old ideas
!Inclusion of restart capability
!Setting control variables as a function of dx and dt
!for scale adaptation.
!P.Pegion, 2021-09
! swtich to new random number generator and improve computational efficiency
! and remove unsued code
!L.Bengtsson, 2023-05
!Add horizontal advection of CA cells
!This routine produces an output field CA_DEEP for coupling to convection (saSAS).
!CA_DEEP can be either number of plumes in a cluster (nca_plumes=true) or updraft
!area fraction (nca_plumes=false)
integer,intent(in) :: kstep,ncells,nca,nlives,nseed,nspinup,mpiroot,mytile
type(MPI_Comm),intent(in) :: mpicomm
integer(kind=kind_dbl_prec), intent(in) :: iseed_ca
real(kind=kind_phys), intent(in) :: nfracseed,dtf,nthresh
logical,intent(in) :: restart,ca_trigger,first_time_step,ca_advect
integer, intent(in) :: nblks,isc,iec,jsc,jec,npx,npy,nlev,blocksize
real(kind=kind_phys), intent(in) :: sst(:,:),lsmsk(:,:),lake(:,:),uwind(:,:,:),dx(:,:),vwind(:,:,:),height(:,:,:)
real(kind=kind_phys), intent(inout) :: condition_cpl(:,:)
real(kind=kind_phys), intent(inout) :: ca_deep_cpl(:,:)
real(kind=kind_phys), intent(inout) :: ca_turb_cpl(:,:)
real(kind=kind_phys), intent(inout) :: ca_shal_cpl(:,:)
type(domain2D), intent(inout) :: domain_in
type(block_control_type) :: Atm_block
integer :: nlon, nlat, isize,jsize,nf,nn
integer :: inci, incj, nxc, nyc, nxch, nych, nx, ny
integer :: halo, k_in, i, j, k
integer :: seed, ierr7,blk, ix, iix, count4,ih,jh
integer :: blocksz,levs,u200,u850
integer, save :: initialize_ca
integer(8) :: count, count_rate, count_max, count_trunc,nx_full
integer(8) :: iscale = 10000000000
integer, allocatable :: iini(:,:,:),ilives_in(:,:,:),ca_plumes(:,:),io_layout(:)
real(kind=kind_phys), allocatable :: ssti(:,:),lsmski(:,:),lakei(:,:),uwindi(:,:),vwindi(:,:),dxi(:,:),heighti(:,:,:)
real(kind=kind_phys), allocatable :: CA(:,:),condition(:,:),uhigh(:,:),vhigh(:,:),dxhigh(:,:),conditiongrid(:,:)
real(kind=kind_phys), allocatable :: CA_DEEP(:,:),zi(:,:,:),sumx(:,:)
real*8 , allocatable :: noise(:,:,:)
real(kind=kind_phys) :: condmax,condmaxinv,livesmax,livesmaxinv,factor,pi,re
logical,save :: block_message=.true.
logical :: nca_plumes
logical,save :: first_flag
integer*8 :: i1,j1
integer :: ct,ntiles
real :: dz,invgrav
!nca :: switch for number of cellular automata to be used.
! :: for the moment only 1 CA can be used
!nfracseed :: switch for number of random cells initially seeded
!nlives :: switch for time scale (s)
!nspinup :: switch for number of itterations to spin up the ca
!ncells :: switch for CA cell size (m)
!nca_plumes :: compute number of CA-cells ("plumes") within a NWP gridbox.
if (nca .LT. 1) return
! Initialize MPI and OpenMP
if (first_time_step) then
call mpi_wrapper_initialize(mpiroot,mpicomm)
end if
halo=3
k_in=1
!Right now these values are experimental:
u200=56
u850=13
!Gravitational acceleration:
invgrav=1./9.81
nca_plumes = .true.
if(first_time_step)then
first_flag = .false.
initialize_ca = 100000
endif
!----------------------------------------------------------------------------
! Get information about the compute domain, allocate fields on this
! domain
! Some security checks for namelist combinations:
if(nca > 1)then
write(0,*)'When ca_sgs=.True., nca has to be 1 - exiting'
stop
endif
nlon=iec-isc+1
nlat=jec-jsc+1
isize=nlon+2*halo
jsize=nlat+2*halo
!Set time and length scales:
call mpp_get_global_domain(domain_in,xsize=nx,ysize=ny,position=CENTER)
ntiles = mpp_get_ntile_count(domain_in)
if(mype == 1)then
write(*,*)'ncells=',ncells
write(*,*)'nlives=',nlives
write(*,*)'nthresh=',nthresh
endif
inci=ncells
incj=ncells
!--- get params from domain_sgs for building board and board_halo
if(first_time_step)then
!Get CA domain
if (.not.restart) then
allocate(io_layout(2))
io_layout=mpp_get_io_domain_layout(domain_in)
call define_ca_domain(domain_in,domain_sgs,halo,ncells,nxncells,nyncells)
call mpp_define_io_domain(domain_sgs, io_layout)
endif
call mpp_get_data_domain (domain_sgs,isdnx,iednx,jsdnx,jednx)
call mpp_get_compute_domain (domain_sgs,iscnx,iecnx,jscnx,jecnx)
endif
nxc = iecnx-iscnx+1
nyc = jecnx-jscnx+1
nxch = iednx-isdnx+1
nych = jednx-jsdnx+1
!Allocate fields:
allocate(ssti(nlon,nlat))
allocate(lsmski(nlon,nlat))
allocate(lakei(nlon,nlat))
allocate(uwindi(nlon,nlat))
allocate(vwindi(nlon,nlat))
allocate(heighti(nlon,nlat,nlev))
allocate(zi(nlon,nlat,nlev))
allocate(sumx(nlon,nlat))
allocate(dxi(nlon,nlat))
allocate(iini(nxc,nyc,nca))
allocate(ilives_in(nxc,nyc,nca))
allocate(condition(nxc,nyc))
allocate(uhigh(nxc,nyc))
allocate(dxhigh(nxc,nyc))
allocate(vhigh(nxc,nyc))
allocate(conditiongrid(nlon,nlat))
allocate(CA(nlon,nlat))
allocate(ca_plumes(nlon,nlat))
allocate(CA_DEEP(nlon,nlat))
!Initialize:
ilives_in(:,:,:) = 0
iini(:,:,:) = 0
sumx(:,:) = 0.
uwindi(:,:) = 0.
vwindi(:,:) =0.
!Put the blocks of model fields into a 2d array - can't use nlev and blocksize directly,
!because the arguments to define_blocks_packed are intent(inout) and not intent(in).
levs=nlev
blocksz=blocksize
call define_blocks_packed('cellular_automata', Atm_block, isc, iec, jsc, jec, levs, &
blocksz, block_message)
do blk = 1,Atm_block%nblks
do ix = 1, Atm_block%blksz(blk)
i = Atm_block%index(blk)%ii(ix) - isc + 1
j = Atm_block%index(blk)%jj(ix) - jsc + 1
conditiongrid(i,j) = condition_cpl(blk,ix)
ssti(i,j) = sst(blk,ix)
lsmski(i,j) = lsmsk(blk,ix)
lakei(i,j) = lake(blk,ix)
dxi(i,j) = dx(blk,ix)
do k = 1,levs
heighti(i,j,k) = height(blk,ix,k)*invgrav
enddo
do k = 2,levs
zi(i,j,k)=0.5*(heighti(i,j,k)+heighti(i,j,k-1))
enddo
do k = u850,u200
dz=zi(i,j,k)-zi(i,j,k-1)
uwindi(i,j) = uwindi(i,j) + uwind(blk,ix,k)*dz
vwindi(i,j) = vwindi(i,j) + vwind(blk,ix,k)*dz
sumx(i,j) = sumx(i,j) + dz
enddo
enddo
enddo
do blk = 1,Atm_block%nblks
do ix = 1, Atm_block%blksz(blk)
i = Atm_block%index(blk)%ii(ix) - isc + 1
j = Atm_block%index(blk)%jj(ix) - jsc + 1
uwindi(i,j)=uwindi(i,j)/sumx(i,j)
vwindi(i,j)=vwindi(i,j)/sumx(i,j)
enddo
enddo
!Initialize the CA when the condition field is populated
do j=1,nyc
do i=1,nxc
condition(i,j)=conditiongrid(inci/ncells,incj/ncells)
uhigh(i,j)=uwindi(inci/ncells,incj/ncells)
vhigh(i,j)=vwindi(inci/ncells,incj/ncells)
dxhigh(i,j)=dxi(inci/ncells,incj/ncells)/ncells !dx on the finer grid
if(i.eq.inci)then
inci=inci+ncells
endif
enddo
inci=ncells
if(j.eq.incj)then
incj=incj+ncells
endif
enddo
condmax=maxval(condition)
condmaxinv=0.0
call mp_reduce_max(condmax)
if(condmax > 0.)then
if(.not. first_flag)then
first_flag = .true.
initialize_ca = kstep
endif
condmaxinv=1.0/condmax
endif
if(kstep >=initialize_ca)then
do nf=1,nca
do j = 1,nyc
do i = 1,nxc
ilives_in(i,j,nf)=int(real(nlives)*(condition(i,j)*condmaxinv))
enddo
enddo
enddo
else
do nf=1,nca
do j = 1,nyc
do i = 1,nxc
ilives_in(i,j,nf)=0
enddo
enddo
enddo
endif
!Generate random number, following stochastic physics code:
if (cold_start_ca_sgs) then
if(kstep == initialize_ca) then
nx_full=int(ncells,kind=8)*int(npx-1,kind=8)
allocate(noise(nxc,nyc,nca))
do j=1,nyc
j1=j+(jsc-1)*ncells
do i=1,nxc
i1=i+(isc-1)*ncells
if (iseed_ca <= 0) then
! generate a random seed from system clock and ens member number
call system_clock(count, count_rate, count_max)
! iseed is elapsed time since unix epoch began (secs)
! truncate to 4 byte integer
count_trunc = iscale*(count/iscale)
count4 = count - count_trunc + mytile *( i1+nx_full*(j1-1)) ! no need to multply by 7 since time will be different in sgs
else
! don't rely on compiler to truncate integer(8) to integer(4) on
! overflow, do wrap around explicitly.
count4 = mod((iseed_ca+mytile)*(i1+nx_full*(j1-1))+ 2147483648_8, 4294967296_8) - 2147483648_8
endif
ct=1
do nf=1,nca
noise(i,j,nf)=real(random_01_CB(ct,count4),kind=8)
ct=ct+1
enddo
enddo
enddo
!Initiate the cellular automaton with random numbers larger than nfracseed
do nf=1,nca
do j = 1,nyc
do i = 1,nxc
if (noise(i,j,nf) > nfracseed ) then
iini(i,j,nf)=1
else
iini(i,j,nf)=0
endif
enddo
enddo
enddo !nf
deallocate(noise)
endif !
endif ! cold_start_ca_sgs
!Calculate neighbours and update the automata
do nf=1,nca
call update_cells_sgs(kstep,halo,dtf,initialize_ca,iseed_ca,first_flag,restart,first_time_step,nca,nxc,nyc, &
nxch,nych,nlon,nlat,isc,iec,jsc,jec,ca_advect, &
npx,npy,CA,ca_plumes,iini,ilives_in,uhigh,vhigh,dxhigh, &
nlives,nfracseed,nseed,nspinup,nf,nca_plumes,ncells,mytile)
if(nca_plumes)then
do j=1,nlat
do i=1,nlon
CA_DEEP(i,j)=ca_plumes(i,j)
enddo
enddo
else
livesmax=maxval(ilives_in)
call mp_reduce_max(livesmax)
livesmaxinv=1.0/livesmax
do j=1,nlat
do i=1,nlon
CA_DEEP(i,j)=CA(i,j)*livesmaxinv
enddo
enddo
endif
enddo !nf (nca)
!Limit CA activity to the Tropical Ocean
if(ntiles==6)then
do j=1,nlat
do i=1,nlon
if(ssti(i,j) < 300. .or. lsmski(i,j) /= 0. .or. lakei(i,j) > 0.0)then
CA_DEEP(i,j)=0.
endif
enddo
enddo
endif
!Put back into blocks 1D array to be passed to physics
!or diagnostics output
do blk = 1, Atm_block%nblks
do ix = 1,Atm_block%blksz(blk)
i = Atm_block%index(blk)%ii(ix) - isc + 1
j = Atm_block%index(blk)%jj(ix) - jsc + 1
ca_deep_cpl(blk,ix)=CA_DEEP(i,j)
enddo
enddo
deallocate(conditiongrid)
deallocate(ssti)
deallocate(lsmski)
deallocate(lakei)
deallocate(iini)
deallocate(ilives_in)
deallocate(uwindi)
deallocate(vwindi)
deallocate(uhigh)
deallocate(vhigh)
deallocate(dxhigh)
deallocate(condition)
deallocate(CA)
deallocate(ca_plumes)
deallocate(CA_DEEP)
end subroutine cellular_automata_sgs
end module cellular_automata_sgs_mod