-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.f90
410 lines (300 loc) · 13.1 KB
/
main.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
program crm
! Main module.
use vars
use hbuffer
use params, only: dompiensemble
use microphysics
use sgs
use tracers
use movies, only: init_movies
use mse, only: init_MSE_tendency, compute_and_increment_MSE_tendency, &
init_momentum_tendency, compute_and_increment_momentum_tendency, &
initializeMSE
implicit none
integer k, icyc, nn, nstatsteps
double precision cputime, oldtime, init_time !bloss wallclocktime
double precision usrtime, systime
integer itmp1(1), oldstep
logical :: log_Exists
!-------------------------------------------------------------------
! determine the rank of the current task and of the neighbour's ranks
call task_init()
!-------------------------------------------------------------------
!bloss: Make sure that the run will not restart if it does not stop cleanly
if(masterproc) then
inquire(file='ReadyForRestart',exist=log_Exists)
if(.NOT.log_Exists) then
open(unit=47,file='ReadyForRestart',form='FORMATTED',status='NEW')
close(47)
end if
open(unit=47,file='ReadyForRestart',form='FORMATTED',status='REPLACE')
rewind(47)
write(47,991)
991 format('FALSE')
close(47)
end if
!------------------------------------------------------------------
! print time, version, etc
if(masterproc) call header()
!------------------------------------------------------------------
! Initialize timing library. 2nd arg 0 means disable, 1 means enable
call t_setoptionf (1, 0)
call t_initializef ()
call t_startf ('total')
call t_startf ('initialize')
!------------------------------------------------------------------
! Get initial time of job
call t_stampf(init_time,usrtime,systime)
!------------------------------------------------------------------
call init() ! initialize some statistics arrays
call setparm() ! set all parameters and constants
!------------------------------------------------------------------
! Initialize or restart from the save-dataset:
if(nrestart.eq.0) then
day=day0
call setgrid() ! initialize vertical grid structure
call setdata() ! initialize all variables
elseif(nrestart.eq.1) then
call read_all()
call setgrid() ! initialize vertical grid structure
call diagnose()
call sgs_init()
call micro_init() !initialize microphysics
if(do_chunked_energy_budgets) call initializeMSE()
elseif(nrestart.eq.2) then ! branch run
call read_all()
call setgrid() ! initialize vertical grid structure
call diagnose()
call setparm() ! overwrite the parameters
call sgs_init()
call micro_init() !initialize microphysics
if(do_chunked_energy_budgets) call initializeMSE()
nstep = 0
day0 = day
else
print *,'Error: confused by value of NRESTART'
call task_abort()
endif
call init_movies()
call stat_2Dinit(1) ! argument of 1 means storage terms in stats are reset
call tracers_init() ! initialize tracers
call setforcing()
call printout()
call write_fields3D() ! output initial condition
!------------------------------------------------------------------
! Initialize statistics buffer:
call hbuf_init()
!------------------------------------------------------------------
nstatis = nstat/nstatfrq
nstat = nstatis * nstatfrq
nstatsteps = 0
call t_stopf ('initialize')
!------------------------------------------------------------------
! Main time loop
!------------------------------------------------------------------
call t_stampf(oldtime,usrtime,systime)
oldstep = nstep
do while(nstep.lt.nstop.and.nelapse.gt.0)
nstep = nstep + 1
time = time + dt
day = day0 + nstep*dt/86400.
nelapse = nelapse - 1
!------------------------------------------------------------------
! Check if the dynamical time step should be decreased
! to handle the cases when the flow being locally linearly unstable
!------------------------------------------------------------------
ncycle = 1
! MPI Ensemble run: turn off mpi entering each loop (Song Qiyu, 2022)
if(dompiensemble) dompi = .false.
call kurant()
total_water_before = total_water()
total_water_evap = 0.
total_water_prec = 0.
total_water_ls = 0.
do icyc=1,ncycle
icycle = icyc
dtn = dt/ncycle
dt3(na) = dtn
dtfactor = dtn/dt
if(mod(nstep,nstatis).eq.0.and.icycle.eq.ncycle) then
nstatsteps = nstatsteps + 1
dostatis = .true.
if(masterproc) print *,'Collecting statistics...'
else
dostatis = .false.
endif
!bloss:make special statistics flag for radiation,since it's only updated at icycle==1.
dostatisrad = .false.
if(mod(nstep,nstatis).eq.0.and.icycle.eq.1) dostatisrad = .true.
!---------------------------------------------
! the Adams-Bashforth scheme in time
call abcoefs()
!---------------------------------------------
! initialize stuff:
call zero()
if(do_chunked_momentum_budgets) call init_momentum_tendency() !bloss: see mse.f90
!-----------------------------------------------------------
! Buoyancy term:
call buoyancy()
if(do_chunked_momentum_budgets) call compute_and_increment_momentum_tendency('buoy') !bloss: see mse.f90
!------------------------------------------------------------
total_water_ls = total_water_ls - total_water()
!------------------------------------------------------------
! for mse.f90, store s_li, h_f and q_tot before forcing.
if(do_chunked_energy_budgets) call init_MSE_tendency()
if(do_chunked_momentum_budgets) call init_momentum_tendency() !bloss: see mse.f90
!------------------------------------------------------------
! Large-scale and surface forcing:
call forcing()
!------------------------------------------------------------
! get increment from forcing and add it to chunk averages.
if(do_chunked_energy_budgets) call compute_and_increment_MSE_tendency('lsf')
if(do_chunked_momentum_budgets) call compute_and_increment_momentum_tendency('lsf') !bloss: see mse.f90
!------------------------------------------------------------
! for mse.f90, store s_li, h_f and q_tot before nudging/damping/upperbound
if(do_chunked_energy_budgets) call init_MSE_tendency()
if(do_chunked_momentum_budgets) call init_momentum_tendency() !bloss: see mse.f90
!----------------------------------------------------------
! Nadging to sounding:
call nudging()
!----------------------------------------------------------
! spange-layer damping near the upper boundary:
if(dodamping) call damping()
!------------------------------------------------------------
! get increment from nudging/damping and add it to chunk averages.
if(do_chunked_energy_budgets) call compute_and_increment_MSE_tendency('misc')
if(do_chunked_momentum_budgets) call compute_and_increment_momentum_tendency('misc') !bloss: see mse.f90
!----------------------------------------------------------
total_water_ls = total_water_ls + total_water()
!---------------------------------------------------------
! Ice fall-out
if(docloud) then
call ice_fall()
end if
!----------------------------------------------------------
! Update scalar boundaries after large-scale processes:
call boundaries(3)
!---------------------------------------------------------
! Update boundaries for velocities:
call boundaries(0)
!-----------------------------------------------
! surface fluxes:
if(dosurface) call surface()
call micro_flux() !bloss: moved here for consistency of isotopic fluxes
!-----------------------------------------------------------
! SGS physics:
if (dosgs) call sgs_proc()
!----------------------------------------------------------
! Fill boundaries for SGS diagnostic fields:
call boundaries(4)
if(do_chunked_momentum_budgets) call init_momentum_tendency() !bloss: see mse.f90
!-----------------------------------------------
! advection of momentum:
call advect_mom()
!----------------------------------------------------------
! SGS effects on momentum:
if(dosgs) call sgs_mom()
if(do_chunked_momentum_budgets) call compute_and_increment_momentum_tendency('eddy') !bloss: see mse.f90
if(do_chunked_momentum_budgets) call init_momentum_tendency() !bloss: see mse.f90
!-----------------------------------------------------------
! Coriolis force:
if(docoriolis) call coriolis()
if(do_chunked_momentum_budgets) call compute_and_increment_momentum_tendency('lsf') !bloss: see mse.f90
if(do_chunked_momentum_budgets) call init_momentum_tendency() !bloss: see mse.f90
!---------------------------------------------------------
! compute rhs of the Poisson equation and solve it for pressure.
call pressure()
if(do_chunked_momentum_budgets) call compute_and_increment_momentum_tendency('pgrad') !bloss: see mse.f90
!---------------------------------------------------------
! find velocity field at n+1/2 timestep needed for advection of scalars:
! Note that at the end of the call, the velocities are in nondimensional form.
call adams()
!----------------------------------------------------------
! Update boundaries for all prognostic scalar fields for advection:
call boundaries(2)
!---------------------------------------------------------
! advection of scalars :
call advect_all_scalars()
!-----------------------------------------------------------
! Convert velocity back from nondimensional form:
call uvw()
!----------------------------------------------------------
! Update boundaries for scalars to prepare for SGS effects:
call boundaries(3)
!------------------------------------------------------------
! for mse.f90, store s_li, h_f and q_tot before SGS transport
if(do_chunked_energy_budgets) call init_MSE_tendency()
!---------------------------------------------------------
! SGS effects on scalars :
if (dosgs) call sgs_scalars()
!------------------------------------------------------------
! get increment from advection/diffusion and add it to chunk averages.
if(do_chunked_energy_budgets) call compute_and_increment_MSE_tendency('eddy')
!------------------------------------------------------------
! for mse.f90, store s_li, h_f and q_tot before upperbound
if(do_chunked_energy_budgets) call init_MSE_tendency()
!-----------------------------------------------------------
! Handle upper boundary for scalars
total_water_ls = total_water_ls - total_water() !bloss: Include any water changes in large-scale diagnostic
if(doupperbound) call upperbound()
total_water_ls = total_water_ls + total_water()
!------------------------------------------------------------
! get increment from upperbound and add it to chunk averages.
if(do_chunked_energy_budgets) call compute_and_increment_MSE_tendency('misc')
!-----------------------------------------------------------
! Cloud condensation/evaporation and precipitation processes:
if(do_chunked_energy_budgets) call init_MSE_tendency() ! for mse.f90, store fields before micro_proc()
if(docloud.or.dosmoke) call micro_proc()
if(do_chunked_energy_budgets) call compute_and_increment_MSE_tendency('mphy')
!----------------------------------------------------------
! Tracers' physics:
call tracers_physics()
!-----------------------------------------------------------
! Radiation
if(dolongwave.or.doshortwave) then
call radiation()
end if
!-----------------------------------------------------------
! Compute diagnostic fields:
call diagnose()
!----------------------------------------------------------
! Rotate the dynamic tendency arrays for Adams-bashforth scheme:
nn=na
na=nc
nc=nb
nb=nn
end do ! icycle
total_water_after = total_water()
!----------------------------------------------------------
! collect statistics, write save-file, etc.
call stepout(nstatsteps)
! MPI Ensemble run: turn on mpi after each loop (Song Qiyu, 2022)
if(dompiensemble) dompi = .true.
!----------------------------------------------------------
!----------------------------------------------------------
! bloss: only stop when stat and 2D fields are output.
!bloss(2016-09-07): if saving local MSE budgets, only stop when they are output
if ( (nstep.lt.nstop).and.(mod(nstep,nstat).eq.0) .AND. &
( (.NOT.save2Davg) .OR. (mod(nstep,nsave2D).eq.0) ) .AND. &
( (.NOT.do_chunked_energy_budgets) .OR. (mod(nstep,nsaveMSE).eq.0) ) )then
if(masterproc) then
call t_stampf(cputime,usrtime,systime)
write(*,999) cputime-oldtime, float(nstep-oldstep)*dt/3600.
999 format('CPU TIME = ',f12.4, ' OVER ', f8.2,' MODEL HOURS')
oldtime = cputime
oldstep = nstep
end if
if(dompi) then
if(masterproc) itmp1 = nelapse
call task_bcast_integer(0,itmp1,1)
nelapse = itmp1(1)
end if
end if
end do ! main loop
!----------------------------------------------------------
!----------------------------------------------------------
call t_stopf('total')
if(masterproc) call t_prf(rank)
if(masterproc) write(*,*) 'Finished with SAM, exiting...'
call task_stop()
end program crm