-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathdyn_comp.F90
More file actions
1181 lines (950 loc) · 41.8 KB
/
dyn_comp.F90
File metadata and controls
1181 lines (950 loc) · 41.8 KB
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module dyn_comp
!-----------------------------------------------------------------------
!
! Eulerian dycore interface module
!
!-----------------------------------------------------------------------
use shr_kind_mod, only: r8=>shr_kind_r8
use spmd_utils, only: masterproc, npes, mpicom, mpir8
use physconst, only: pi
use pmgrid, only: plon, plat, plev, plevp, plnlv, beglat, endlat
use commap, only: clat, clon
use dyn_grid, only: ptimelevels
use prognostics, only: n3, ps, u3, v3, t3, q3, phis, pdeld, dpsm, dpsl, div, vort
use cam_control_mod, only: initial_run, moist_physics, adiabatic, simple_phys
use phys_control, only: phys_getopts
use constituents, only: pcnst, cnst_name, cnst_longname, sflxnam, tendnam, &
fixcnam, tottnam, hadvnam, vadvnam, cnst_get_ind, &
cnst_read_iv, qmin
use cam_initfiles, only: initial_file_get_id, topo_file_get_id, pertlim, scale_dry_air_mass
use inic_analytic, only: analytic_ic_active, analytic_ic_set_ic
use dyn_tests_utils, only: vc_moist_pressure
use cam_history, only: addfld, add_default, horiz_only
use eul_control_mod, only: dif2, hdif_order, kmnhdn, hdif_coef, divdampn, eps, &
kmxhdc, eul_nsplit
use scamMod, only: single_column, use_camiop, have_u, have_v, &
have_cldliq, have_cldice, loniop, latiop, scmlat, scmlon, &
qobs,tobs,scm_cambfb_mode
use cam_pio_utils, only: clean_iodesc_list, cam_pio_get_var
use pio, only: file_desc_t, pio_noerr, pio_inq_varid, pio_get_att, &
pio_inq_attlen, pio_inq_dimid, pio_inq_dimlen, &
pio_get_var,var_desc_t, pio_seterrorhandling, &
pio_bcast_error, pio_internal_error, pio_offset_kind
#if (defined SPMD)
use spmd_dyn, only: spmd_readnl
#endif
use cam_logfile, only: iulog
use cam_abortutils, only: endrun
implicit none
private
save
public :: &
dyn_import_t, &
dyn_export_t, &
dyn_readnl, &
dyn_register, &
dyn_init
! these structures are not used in this dycore, but are included
! for interface compatibility.
type dyn_import_t
integer :: placeholder
end type dyn_import_t
type dyn_export_t
integer :: placeholder
end type dyn_export_t
real(r8), allocatable :: ps_tmp (:,: )
real(r8), allocatable :: phis_tmp(:,: )
real(r8), allocatable :: q3_tmp (:,:,:)
real(r8), allocatable :: t3_tmp (:,:,:)
real(r8), allocatable :: arr3d_a (:,:,:)
real(r8), allocatable :: arr3d_b (:,:,:)
logical readvar ! inquiry flag: true => variable exists on netCDF file
!=========================================================================================
CONTAINS
!=========================================================================================
subroutine dyn_readnl(nlfile)
! Read dynamics namelist group.
use namelist_utils, only: find_group_name
use units, only: getunit, freeunit
use spmd_utils, only: mpicom, mstrid=>masterprocid, mpi_integer, mpi_real8
! args
character(len=*), intent(in) :: nlfile ! filepath for file containing namelist input
! local vars
integer :: unitn, ierr
real(r8) :: eul_dif2_coef ! del2 horizontal diffusion coeff.
integer :: eul_hdif_order ! Order of horizontal diffusion operator
integer :: eul_hdif_kmnhdn ! Nth order horizontal diffusion operator top level.
real(r8) :: eul_hdif_coef ! Nth order horizontal diffusion coefficient.
real(r8) :: eul_divdampn ! Number of days to invoke divergence damper
real(r8) :: eul_tfilt_eps ! Time filter coefficient. Defaults to 0.06.
integer :: eul_kmxhdc ! Number of levels to apply Courant limiter
namelist /dyn_eul_inparm/ eul_dif2_coef, eul_hdif_order, eul_hdif_kmnhdn, &
eul_hdif_coef, eul_divdampn, eul_tfilt_eps, eul_kmxhdc, eul_nsplit
character(len=*), parameter :: sub = 'dyn_readnl'
!-----------------------------------------------------------------------------
! Read namelist
if (masterproc) then
unitn = getunit()
open( unitn, file=trim(nlfile), status='old' )
call find_group_name(unitn, 'dyn_eul_inparm', status=ierr)
if (ierr == 0) then
read(unitn, dyn_eul_inparm, iostat=ierr)
if (ierr /= 0) then
call endrun(sub//': ERROR reading namelist')
end if
end if
close(unitn)
call freeunit(unitn)
end if
call mpi_bcast(eul_dif2_coef, 1, mpi_real8, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_dif2_coef")
call mpi_bcast(eul_hdif_order, 1, mpi_integer, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_hdif_order")
call mpi_bcast(eul_hdif_kmnhdn, 1, mpi_integer, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_hdif_kmnhdn")
call mpi_bcast(eul_hdif_coef, 1, mpi_real8, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_hdif_coef")
call mpi_bcast(eul_divdampn, 1, mpi_real8, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_divdampn")
call mpi_bcast(eul_tfilt_eps, 1, mpi_real8, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_tfilt_eps")
call mpi_bcast(eul_kmxhdc, 1, mpi_integer, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_kmxhdc")
call mpi_bcast(eul_nsplit, 1, mpi_integer, mstrid, mpicom, ierr)
if (ierr /= 0) call endrun(sub//": FATAL: mpi_bcast: eul_nsplit")
dif2 = eul_dif2_coef
hdif_order = eul_hdif_order
kmnhdn = eul_hdif_kmnhdn
hdif_coef = eul_hdif_coef
divdampn = eul_divdampn
eps = eul_tfilt_eps
kmxhdc = eul_kmxhdc
! Write namelist variables to logfile
if (masterproc) then
write(iulog,*) 'Eulerian Dycore Parameters:'
! Order of diffusion
if (hdif_order < 2 .or. mod(hdif_order, 2) /= 0) then
write(iulog,*) sub//': Order of diffusion must be greater than 0 and multiple of 2'
write(iulog,*) 'hdif_order = ', hdif_order
call endrun(sub//': ERROR: invalid eul_hdif_order specified')
end if
if (divdampn > 0._r8) then
write(iulog,*) ' Divergence damper for spectral dycore invoked for days 0. to ',divdampn,' of this case'
elseif (divdampn < 0._r8) then
call endrun (sub//': divdampn must be non-negative')
else
write(iulog,*) ' Divergence damper for spectral dycore NOT invoked'
endif
if (kmxhdc >= plev .or. kmxhdc < 0) then
call endrun (sub//': ERROR: KMXHDC must be between 0 and plev-1')
end if
write(iulog,9108) eps, hdif_order, kmnhdn, hdif_coef, kmxhdc, eul_nsplit
if (kmnhdn > 1) then
write(iulog,9109) dif2
end if
end if
#if (defined SPMD)
call spmd_readnl(nlfile)
#endif
9108 format(' Time filter coefficient (EPS) ',f10.3,/,&
' Horizontal diffusion order (N) ',i10/, &
' Top layer for Nth order horizontal diffusion ',i10/, &
' Nth order horizontal diffusion coefficient ',e10.3/, &
' Number of levels Courant limiter applied ',i10/, &
' Dynamics Subcycling ',i10)
9109 format(' DEL2 horizontal diffusion applied above Nth order diffusion',/,&
' DEL2 Horizontal diffusion coefficient (DIF2) ',e10.3)
end subroutine dyn_readnl
!=========================================================================================
subroutine dyn_register()
end subroutine dyn_register
!=========================================================================================
subroutine dyn_init(dyn_in, dyn_out)
use prognostics, only: initialize_prognostics
use scanslt, only: scanslt_alloc
use scamMod, only: single_column
#if (defined SPMD)
use spmd_dyn, only: spmdbuf
#endif
#if (defined BFB_CAM_SCAM_IOP )
use history_defaults, only: initialize_iop_history
#endif
use dyn_tests_utils, only: vc_dycore, vc_moist_pressure,string_vc, vc_str_lgth
! Arguments are not used in this dycore, included for compatibility
type(dyn_import_t), intent(out) :: dyn_in
type(dyn_export_t), intent(out) :: dyn_out
! Local workspace
integer :: m
integer :: ixcldice, ixcldliq ! constituent indices for cloud liquid and ice water.
logical :: history_amwg ! output for AMWG diagnostics
logical :: history_budget ! output tendencies and state variables for CAM4
! temperature, water vapor, cloud ice and cloud
! liquid budgets.
integer :: history_budget_histfile_num ! output history file number for budget fields
character (len=vc_str_lgth) :: str1
!----------------------------------------------------------------------------
vc_dycore = vc_moist_pressure
if (masterproc) then
call string_vc(vc_dycore,str1)
write(iulog,*)'dycore vertical coordinate : ',trim(str1)
end if
! Initialize prognostics variables
call initialize_prognostics
call scanslt_alloc()
#if (defined SPMD)
! Allocate communication buffers for collective communications in realloc
! routines and in dp_coupling. Call must come after phys_grid_init.
call spmdbuf ()
#endif
call set_phis()
if (initial_run) then
#if (defined BFB_CAM_SCAM_IOP )
call initialize_iop_history()
#endif
call read_inidat()
call clean_iodesc_list()
end if
call addfld ('ETADOT',(/ 'ilev' /),'A', '1/s','Vertical (eta) velocity', gridname='gauss_grid')
call addfld ('U&IC', (/ 'lev' /), 'I', 'm/s','Zonal wind', gridname='gauss_grid' )
call addfld ('V&IC', (/ 'lev' /), 'I', 'm/s','Meridional wind', gridname='gauss_grid' )
call add_default ('U&IC',0, 'I')
call add_default ('V&IC',0, 'I')
call addfld ('PS&IC',horiz_only,'I', 'Pa','Surface pressure', gridname='gauss_grid' )
call addfld ('T&IC',(/ 'lev' /),'I', 'K','Temperature', gridname='gauss_grid' )
call add_default ('PS&IC',0, 'I')
call add_default ('T&IC',0, 'I')
do m = 1, pcnst
call addfld (trim(cnst_name(m))//'&IC',(/ 'lev' /),'I', 'kg/kg',cnst_longname(m), gridname='gauss_grid' )
call add_default(trim(cnst_name(m))//'&IC',0, 'I')
call addfld (hadvnam(m), (/ 'lev' /), 'A', 'kg/kg/s',trim(cnst_name(m))//' horizontal advection tendency', &
gridname='gauss_grid')
call addfld (vadvnam(m), (/ 'lev' /), 'A', 'kg/kg/s',trim(cnst_name(m))//' vertical advection tendency', &
gridname='gauss_grid')
call addfld (tendnam(m), (/ 'lev' /), 'A', 'kg/kg/s',trim(cnst_name(m))//' total tendency', &
gridname='gauss_grid')
call addfld (tottnam(m), (/ 'lev' /), 'A', 'kg/kg/s',trim(cnst_name(m))//' horz + vert + fixer tendency', &
gridname='gauss_grid')
call addfld (fixcnam(m), (/ 'lev' /), 'A', 'kg/kg/s',trim(cnst_name(m))//' tendency due to slt fixer', &
gridname='gauss_grid')
end do
call addfld ('DUH ',(/ 'lev' /),'A', 'K/s ','U horizontal diffusive heating', gridname='gauss_grid')
call addfld ('DVH ',(/ 'lev' /),'A', 'K/s ','V horizontal diffusive heating', gridname='gauss_grid')
call addfld ('DTH ',(/ 'lev' /),'A', 'K/s ','T horizontal diffusive heating', gridname='gauss_grid')
call addfld ('ENGYCORR',(/ 'lev' /),'A', 'W/m2 ','Energy correction for over-all conservation', gridname='gauss_grid')
call addfld ('TFIX ',horiz_only ,'A', 'K/s ','T fixer (T equivalent of Energy correction)', gridname='gauss_grid')
call addfld ('FU ',(/ 'lev' /),'A', 'm/s2 ','Zonal wind forcing term', gridname='gauss_grid')
call addfld ('FV ',(/ 'lev' /),'A', 'm/s2 ','Meridional wind forcing term', gridname='gauss_grid')
call addfld ('UTEND ',(/ 'lev' /),'A', 'm/s2 ','U tendency', gridname='gauss_grid')
call addfld ('VTEND ',(/ 'lev' /),'A', 'm/s2 ','V tendency', gridname='gauss_grid')
call addfld ('TTEND ',(/ 'lev' /),'A', 'K/s ','T tendency', gridname='gauss_grid')
call addfld ('LPSTEN ',horiz_only ,'A', 'Pa/s ','Surface pressure tendency', gridname='gauss_grid')
call addfld ('VAT ',(/ 'lev' /),'A', 'K/s ','Vertical advective tendency of T',gridname='gauss_grid')
call addfld ('KTOOP ',(/ 'lev' /),'A', 'K/s ','(Kappa*T)*(omega/P)', gridname='gauss_grid')
call phys_getopts(history_amwg_out=history_amwg, &
history_budget_out = history_budget, &
history_budget_histfile_num_out = history_budget_histfile_num)
if (history_amwg) then
call add_default ('DTH ', 1, ' ')
end if
if ( history_budget ) then
if (.not.adiabatic) then
call cnst_get_ind('CLDLIQ', ixcldliq)
call cnst_get_ind('CLDICE', ixcldice)
end if
! The following variables are not defined for single column
if (.not. single_column) then
call add_default(hadvnam( 1), history_budget_histfile_num, ' ')
call add_default(vadvnam( 1), history_budget_histfile_num, ' ')
if (.not.adiabatic) then
call add_default(hadvnam(ixcldliq), history_budget_histfile_num, ' ')
call add_default(hadvnam(ixcldice), history_budget_histfile_num, ' ')
call add_default(vadvnam(ixcldliq), history_budget_histfile_num, ' ')
call add_default(vadvnam(ixcldice), history_budget_histfile_num, ' ')
end if
end if
call add_default(fixcnam( 1), history_budget_histfile_num, ' ')
call add_default(tottnam( 1), history_budget_histfile_num, ' ')
call add_default(tendnam( 1), history_budget_histfile_num, ' ')
if (.not.adiabatic) then
call add_default(fixcnam(ixcldliq), history_budget_histfile_num, ' ')
call add_default(fixcnam(ixcldice), history_budget_histfile_num, ' ')
call add_default(tottnam(ixcldliq), history_budget_histfile_num, ' ')
call add_default(tottnam(ixcldice), history_budget_histfile_num, ' ')
call add_default(tendnam(ixcldliq), history_budget_histfile_num, ' ')
call add_default(tendnam(ixcldice), history_budget_histfile_num, ' ')
end if
call add_default('TTEND', history_budget_histfile_num, ' ')
call add_default('TFIX', history_budget_histfile_num, ' ')
call add_default('KTOOP', history_budget_histfile_num, ' ')
call add_default('VAT', history_budget_histfile_num, ' ')
call add_default('DTH', history_budget_histfile_num, ' ')
end if
end subroutine dyn_init
!=========================================================================================
! Private routines
!=========================================================================================
subroutine read_inidat()
! Read initial dataset and spectrally truncate as appropriate.
! Read and process the fields one at a time to minimize
! memory usage.
use ppgrid, only: begchunk, endchunk, pcols
use phys_grid, only: clat_p, clon_p
use comspe, only: alp, dalp
use ncdio_atm, only: infld
use iop, only: setiopupdate,readiopdata
! Local variables
integer i,c,m,n,lat ! indices
integer ncol
integer ixcldice, ixcldliq ! indices into q3 array for cloud liq and cloud ice
integer :: ierr, pio_errtype
integer :: lonid, latid
integer :: mlon, morec ! lon/lat dimension lengths from IC file
type(file_desc_t), pointer :: fh_ini
real(r8), pointer, dimension(:,:,:) :: convptr_2d
real(r8), pointer, dimension(:,:,:,:) :: convptr_3d
real(r8), pointer, dimension(:,:,:,:) :: cldptr
real(r8), pointer, dimension(:,: ) :: arr2d_tmp
real(r8), pointer, dimension(:,: ) :: arr2d
character*16 fieldname ! field name
real(r8) :: clat2d(plon,plat),clon2d(plon,plat)
! variables for analytic initial conditions
integer, allocatable :: glob_ind(:)
integer :: m_cnst(1)
real(r8), allocatable :: q4_tmp(:,:,:,:)
integer londimid,dimlon,latdimid,dimlat,latvarid,lonvarid
integer strt(3),cnt(3)
character(len=3), parameter :: arraydims3(3) = (/ 'lon', 'lev', 'lat' /)
character(len=3), parameter :: arraydims2(2) = (/ 'lon', 'lat' /)
type(var_desc_t) :: varid
real(r8), allocatable :: tmp2d(:,:)
character(len=*), parameter :: sub='read_inidat'
!----------------------------------------------------------------------------
fh_ini => initial_file_get_id()
allocate ( ps_tmp (plon,plat ) )
allocate ( q3_tmp (plon,plev,plat) )
allocate ( t3_tmp (plon,plev,plat) )
allocate ( arr3d_a (plon,plev,plat) )
allocate ( arr3d_b (plon,plev,plat) )
if (analytic_ic_active()) then
allocate(glob_ind(plon * plat))
m = 1
do c = 1, plat
do i = 1, plon
! Create a global column index
glob_ind(m) = i + (c-1)*plon
m = m + 1
end do
end do
call analytic_ic_set_ic(vc_moist_pressure, clat(:), clon(:,1), &
glob_ind(:), U=arr3d_a, V=arr3d_b, T=t3_tmp, PS=ps_tmp, PHIS_IN=phis_tmp)
readvar = .false.
call process_inidat('PS')
call process_inidat('UV')
call process_inidat('T')
allocate(q4_tmp(plon,plev,plat,1))
do m = 1, pcnst
m_cnst(1) = m
call analytic_ic_set_ic(vc_moist_pressure, clat(:), clon(:,1), &
glob_ind(:), Q=q4_tmp, m_cnst=m_cnst)
arr3d_a(:,:,:) = q4_tmp(:,:,:,1)
call process_inidat('CONSTS', m_cnst=m, fh=fh_ini)
end do
deallocate(q4_tmp)
deallocate(glob_ind)
deallocate ( arr3d_a )
deallocate ( arr3d_b )
else
!---------------------
! Read required fields
!---------------------
call pio_seterrorhandling(fh_ini, PIO_BCAST_ERROR, pio_errtype)
ierr = pio_inq_dimid(fh_ini, 'lon', lonid)
ierr = pio_inq_dimid(fh_ini, 'lat', latid)
ierr = pio_inq_dimlen(fh_ini, lonid, mlon)
ierr = pio_inq_dimlen(fh_ini, latid, morec)
if (.not. single_column .and. (mlon /= plon .or. morec /= plat)) then
write(iulog,*) sub//': ERROR: model parameters do not match initial dataset parameters'
write(iulog,*)'Model Parameters: plon = ',plon,' plat = ',plat
write(iulog,*)'Dataset Parameters: dlon = ',mlon,' dlat = ',morec
call endrun(sub//': ERROR: model parameters do not match initial dataset parameters')
end if
call pio_seterrorhandling(fh_ini, pio_errtype)
!-----------
! 3-D fields
!-----------
fieldname = 'U'
call cam_pio_get_var(fieldname, fh_ini, arraydims3, arr3d_a, found=readvar)
if (.not. readvar) then
call endrun(sub//': ERROR: reading '//trim(fieldname))
end if
fieldname = 'V'
call cam_pio_get_var(fieldname, fh_ini, arraydims3, arr3d_b, found=readvar)
if (.not. readvar) then
call endrun(sub//': ERROR: reading '//trim(fieldname))
end if
call process_inidat('UV')
fieldname = 'T'
call cam_pio_get_var(fieldname, fh_ini, arraydims3, t3_tmp, found=readvar)
if (.not. readvar) then
call endrun(sub//': ERROR: reading '//trim(fieldname))
end if
call process_inidat('T')
! Constituents (read and process one at a time)
do m = 1,pcnst
readvar = .false.
fieldname = cnst_name(m)
if (cnst_read_iv(m)) then
call cam_pio_get_var(fieldname, fh_ini, arraydims3, arr3d_a, found=readvar)
end if
call process_inidat('CONSTS', m_cnst=m, fh=fh_ini)
end do
deallocate ( arr3d_a )
deallocate ( arr3d_b )
!-----------
! 2-D fields
!-----------
fieldname = 'PS'
call cam_pio_get_var(fieldname, fh_ini, arraydims2, ps_tmp, found=readvar)
if (.not. readvar) then
call endrun(sub//': ERROR: reading '//trim(fieldname))
end if
call process_inidat('PS')
end if
if (single_column) then
ps(:,:,1) = ps_tmp(:,:)
else
! Integrals of mass, moisture and geopotential height
! (fix mass of moisture as well)
call global_int
end if
! module data used in global_int
deallocate ( ps_tmp )
deallocate ( phis_tmp )
if (single_column) then
if ( scm_cambfb_mode ) then
fieldname = 'CLAT1'
call infld(fieldname, fh_ini, 'lon', 'lat', 1, pcols, begchunk, endchunk, &
clat2d, readvar, gridname='physgrid')
if (.not. readvar) then
call endrun('CLAT not on iop initial file')
else
clat(:) = clat2d(1,:)
clat_p(:)=clat(:)
end if
fieldname = 'CLON1'
call infld(fieldname, fh_ini, 'lon', 'lat', 1, pcols, begchunk, endchunk, &
clon2d, readvar, gridname='physgrid')
if (.not. readvar) then
call endrun('CLON not on iop initial file')
else
clon = clon2d
clon_p(:)=clon(:,1)
end if
! Get latdeg/londeg from initial file for bfb calculations
! needed for dyn_grid to determine bounding area and verticies
ierr = pio_inq_dimid (fh_ini, 'lon' , londimid)
ierr = pio_inq_dimlen (fh_ini, londimid, dimlon)
ierr = pio_inq_dimid (fh_ini, 'lat' , latdimid)
ierr = pio_inq_dimlen (fh_ini, latdimid, dimlat)
strt(:)=1
cnt(1)=dimlon
cnt(2)=dimlat
cnt(3)=1
allocate(latiop(dimlat))
allocate(loniop(dimlon))
allocate(tmp2d(dimlon,dimlat))
ierr = pio_inq_varid (fh_ini,'CLAT1', varid)
ierr = pio_get_var(fh_ini,varid,strt,cnt,tmp2d)
latiop(:)=tmp2d(1,:)
ierr = pio_inq_varid (fh_ini,'CLON1', varid)
ierr = pio_get_var(fh_ini,varid,strt,cnt,tmp2d)
loniop(:)=tmp2d(:,1)
deallocate(tmp2d)
else
! Using a standard iop - make the default grid size is
! 4x4 degree square for mo_drydep deposition.(standard ARM IOP area)
allocate(latiop(2))
allocate(loniop(2))
latiop(1)=(scmlat-2._r8)*pi/180_r8
latiop(2)=(scmlat+2._r8)*pi/180_r8
loniop(1)=(mod(scmlon-2.0_r8+360.0_r8,360.0_r8))*pi/180.0_r8
loniop(2)=(mod(scmlon+2.0_r8+360.0_r8,360.0_r8))*pi/180.0_r8
call setiopupdate()
! readiopdata will set all n1 level prognostics to iop value timestep 0
call readiopdata(timelevel=1)
! set t3, and q3(n1) values from iop on timestep 0
t3(1,:,1,1) = tobs
q3(1,:,1,1,1) = qobs
end if
end if
deallocate ( q3_tmp )
deallocate ( t3_tmp )
if (.not. single_column) then
deallocate ( alp )
deallocate ( dalp )
end if
call copytimelevels()
end subroutine read_inidat
!=========================================================================================
subroutine set_phis()
! Local variables
type(file_desc_t), pointer :: fh_topo
integer :: ierr, pio_errtype
integer :: lonid, latid
integer :: mlon, morec ! lon/lat dimension lengths from topo file
character(len=3), parameter :: arraydims2(2) = (/ 'lon', 'lat' /)
character(len=16) :: fieldname
integer :: c, i, m
integer, allocatable :: glob_ind(:)
character(len=*), parameter :: sub='set_phis'
!----------------------------------------------------------------------------
fh_topo => topo_file_get_id()
allocate( phis_tmp(plon,plat) )
readvar = .false.
if (associated(fh_topo)) then
call pio_seterrorhandling(fh_topo, PIO_BCAST_ERROR, pio_errtype)
ierr = pio_inq_dimid(fh_topo, 'lon', lonid)
ierr = pio_inq_dimid(fh_topo, 'lat', latid)
ierr = pio_inq_dimlen(fh_topo, lonid, mlon)
ierr = pio_inq_dimlen(fh_topo, latid, morec)
if (.not. single_column .and. (mlon /= plon .or. morec /= plat)) then
write(iulog,*) sub//': ERROR: model parameters do not match initial dataset parameters'
write(iulog,*)'Model Parameters: plon = ',plon,' plat = ',plat
write(iulog,*)'Dataset Parameters: dlon = ',mlon,' dlat = ',morec
call endrun(sub//': ERROR: model parameters do not match initial dataset parameters')
end if
call pio_seterrorhandling(fh_topo, pio_errtype)
fieldname = 'PHIS'
call cam_pio_get_var(fieldname, fh_topo, arraydims2, phis_tmp, found=readvar)
if (.not. readvar) then
call endrun(sub//': ERROR: reading '//trim(fieldname))
end if
else if (analytic_ic_active()) then
allocate(glob_ind(plon*plat))
m = 1
do c = 1, plat
do i = 1, plon
! Create a global column index
glob_ind(m) = i + (c-1)*plon
m = m + 1
end do
end do
call analytic_ic_set_ic(vc_moist_pressure, clat(:), clon(:,1), &
glob_ind(:), PHIS_OUT=phis_tmp)
deallocate(glob_ind)
else
phis_tmp(:,:) = 0._r8
end if
call process_inidat('PHIS', fh=fh_topo)
end subroutine set_phis
!=========================================================================================
subroutine process_inidat(fieldname, m_cnst, fh)
! Post-process input fields
use commap
use comspe
use spetru
use dyn_grid, only: get_horiz_grid_dim_d
use const_init, only: cnst_init_default
use qneg_module, only: qneg3
#if ( defined SPMD )
use spmd_dyn, only: compute_gsfactors
#endif
! arguments
character(len=*), intent(in) :: fieldname ! fields to be processed
integer, intent(in), optional :: m_cnst ! constituent index
type(file_desc_t), intent(inout), optional :: fh ! pio file handle
!---------------------------Local workspace-----------------------------
integer i,j,k,n,lat,irow ! grid and constituent indices
integer :: nglon, nglat, rndm_seed_sz ! For pertlim
integer, allocatable :: rndm_seed(:) ! For pertlim
real(r8) pertval ! perturbation value
integer varid ! netCDF variable id
integer ret
integer(pio_offset_kind) :: attlen ! netcdf return values
logical phis_hires ! true => PHIS came from hi res topo
character*256 text
character*256 trunits ! tracer untis
real(r8), pointer, dimension(:,:,:) :: q_tmp
real(r8), pointer, dimension(:,:,:) :: tmp3d_a, tmp3d_b, tmp3d_extend
real(r8), pointer, dimension(:,: ) :: tmp2d_a, tmp2d_b
#if ( defined BFB_CAM_SCAM_IOP )
real(r8), allocatable :: ps_sav(:,:)
real(r8), allocatable :: u3_sav(:,:,:)
real(r8), allocatable :: v3_sav(:,:,:)
#endif
#if ( defined SPMD )
integer :: numperlat ! number of values per latitude band
integer :: numsend(0:npes-1) ! number of items to be sent
integer :: numrecv ! number of items to be received
integer :: displs(0:npes-1) ! displacement array
#endif
character(len=*), parameter :: sub='process_inidat'
!----------------------------------------------------------------------------
select case (fieldname)
!------------
! Process U/V
!------------
case ('UV')
allocate ( tmp3d_a(plon,plev,plat) )
allocate ( tmp3d_b(plon,plev,plat) )
! Spectral truncation
if (single_column) then
tmp3d_a(:,:,:) = 0._r8
tmp3d_b(:,:,:) = 0._r8
else
#if (( defined BFB_CAM_SCAM_IOP ) && ( ! defined DO_SPETRU ))
allocate ( u3_sav (plon,plev,plat) )
allocate ( v3_sav (plon,plev,plat) )
u3_sav(:plon,:plev,:plat) = arr3d_a(:plon,:plev,:plat)
v3_sav(:plon,:plev,:plat) = arr3d_b(:plon,:plev,:plat)
call spetru_uv(u3_sav ,v3_sav ,vort=tmp3d_a, div=tmp3d_b)
deallocate ( u3_sav )
deallocate ( v3_sav )
#else
call spetru_uv(arr3d_a ,arr3d_b ,vort=tmp3d_a, div=tmp3d_b)
#endif
end if
#if ( defined SPMD )
numperlat = plnlv
call compute_gsfactors (numperlat, numrecv, numsend, displs)
call mpiscatterv (arr3d_a ,numsend, displs, mpir8,u3 (:,:,beglat:endlat,1) ,numrecv, mpir8,0,mpicom)
call mpiscatterv (arr3d_b ,numsend, displs, mpir8,v3 (:,:,beglat:endlat,1) ,numrecv, mpir8,0,mpicom)
call mpiscatterv (tmp3d_a ,numsend, displs, mpir8,vort(:,:,beglat:endlat,1) ,numrecv, mpir8,0,mpicom)
call mpiscatterv (tmp3d_b ,numsend, displs, mpir8,div (:,:,beglat:endlat,1) ,numrecv, mpir8,0,mpicom)
#else
u3 (:,:,:,1) = arr3d_a(:plon,:plev,:plat)
v3 (:,:,:,1) = arr3d_b(:plon,:plev,:plat)
vort (:,:,:,1) = tmp3d_a(:,:,:)
div (:,:,:,1) = tmp3d_b(:,:,:)
#endif
deallocate ( tmp3d_a )
deallocate ( tmp3d_b )
!----------
! Process T
!----------
case ('T')
! Add random perturbation to temperature if required
if (pertlim .ne. 0.0_r8) then
if (masterproc) write(iulog,*) sub//': INFO: Adding random perturbation bounded by +/-', &
pertlim,' to initial temperature field'
call get_horiz_grid_dim_d(nglon, nglat)
call random_seed(size=rndm_seed_sz)
allocate(rndm_seed(rndm_seed_sz))
do lat = 1, plat
do i = 1, plon
! seed random_number generator based on global column index
rndm_seed = i + (lat-1)*nglon
call random_seed(put=rndm_seed)
do k = 1, plev
call random_number (pertval)
pertval = 2._r8*pertlim*(0.5_r8 - pertval)
t3_tmp(i,k,lat) = t3_tmp(i,k,lat)*(1._r8 + pertval)
end do
end do
end do
deallocate(rndm_seed)
end if
! Spectral truncation
if (.not. single_column) then
#if ( ( ! defined BFB_CAM_SCAM_IOP ) || ( defined DO_SPETRU ) )
call spetru_3d_scalar(t3_tmp)
#endif
end if
#if ( defined SPMD )
numperlat = plnlv
call compute_gsfactors (numperlat, numrecv, numsend, displs)
call mpiscatterv (t3_tmp ,numsend, displs, mpir8,t3(:,:,beglat:endlat,1) ,numrecv, mpir8,0,mpicom)
#else
t3 (:,:,:,1) = t3_tmp(:plon,:plev,:plat)
#endif
!---------------------
! Process Constituents
!---------------------
case ('CONSTS')
if (.not. present(m_cnst)) then
call endrun(sub//': ERROR: m_cnst needs to be present in the'// &
' argument list')
end if
allocate(tmp3d_extend(plon,plev,beglat:endlat))
if (readvar) then
! Check that all tracer units are in mass mixing ratios
ret = pio_inq_varid(fh, cnst_name(m_cnst), varid)
ret = pio_get_att(fh, varid, 'units', trunits)
if (trunits(1:5) .ne. 'KG/KG' .and. trunits(1:5) .ne. 'kg/kg') then
!call endrun(sub//': ERROR: Units for tracer ' &
! //trim(cnst_name(m_cnst))//' must be in KG/KG')
end if
else if (.not. analytic_ic_active()) then
! Constituents not read from initial file are initialized by the
! package that implements them. Note that the analytic IC code calls
! cnst_init_default internally
if (m_cnst == 1 .and. moist_physics) then
call endrun(sub//': ERROR: Q must be on Initial File')
end if
call cnst_init_default(m_cnst, clat, clon(:,1), arr3d_a)
end if
!$omp parallel do private(lat)
do lat = 1,plat
call qneg3(sub, lat, plon, plon, plev , &
m_cnst, m_cnst, qmin(m_cnst) ,arr3d_a(1,1,lat))
end do
! if "Q", "CLDLIQ", or "CLDICE", save off for later use
if (m_cnst == 1) q3_tmp(:plon,:,:) = arr3d_a(:plon,:,:)
#if ( defined SPMD )
numperlat = plnlv
call compute_gsfactors(numperlat, numrecv, numsend, displs)
call mpiscatterv(arr3d_a, numsend, displs, mpir8, tmp3d_extend, numrecv, mpir8,0,mpicom)
q3(:,:,m_cnst,:,1) = tmp3d_extend(:,:,beglat:endlat)
#else
q3(:,:plev,m_cnst,:,1) = arr3d_a(:plon,:plev,:plat)
#endif
deallocate ( tmp3d_extend )
!-----------
! Process PS
!-----------
case ('PS')
allocate ( tmp2d_a(plon,plat) )
allocate ( tmp2d_b(plon,plat) )
! Spectral truncation
if (single_column) then
tmp2d_a(:,:) = 0._r8
tmp2d_b(:,:) = 0._r8
else
#if (( defined BFB_CAM_SCAM_IOP ) && ( ! defined DO_SPETRU ))
allocate ( ps_sav(plon,plat) )
ps_sav(:plon,:plat)=ps_tmp(:plon,:plat)
call spetru_ps(ps_sav, tmp2d_a, tmp2d_b)
deallocate ( ps_sav )
#else
call spetru_ps(ps_tmp, tmp2d_a, tmp2d_b)
#endif
end if
#if ( defined SPMD )
numperlat = plon
call compute_gsfactors (numperlat, numrecv, numsend, displs)
call mpiscatterv (tmp2d_a ,numsend, displs, mpir8,dpsl ,numrecv, mpir8,0,mpicom)
call mpiscatterv (tmp2d_b ,numsend, displs, mpir8,dpsm ,numrecv, mpir8,0,mpicom)
#else
dpsl(:,:) = tmp2d_a(:,:)
dpsm(:,:) = tmp2d_b(:,:)
#endif
deallocate ( tmp2d_a )
deallocate ( tmp2d_b )
!-------------
! Process PHIS
!-------------
case ('PHIS')
! Check for presence of 'from_hires' attribute to decide whether to filter
if (readvar) then
ret = pio_inq_varid (fh, 'PHIS', varid)
! Allow pio to return errors in case from_hires doesn't exist
call pio_seterrorhandling(fh, PIO_BCAST_ERROR)
ret = pio_inq_attlen (fh, varid, 'from_hires', attlen)
if (ret.eq.PIO_NOERR .and. attlen.gt.256) then
call endrun(sub//': ERROR: from_hires attribute length is too long')
end if
ret = pio_get_att(fh, varid, 'from_hires', text)
if (ret.eq.PIO_NOERR .and. text(1:4).eq.'true') then
phis_hires = .true.
if(masterproc) write(iulog,*) sub//': INFO: Will filter input PHIS: attribute from_hires is true'
else
phis_hires = .false.
if(masterproc) write(iulog,*) sub//': INFO: Will not filter input PHIS: attribute ', &
'from_hires is either false or not present'
end if
call pio_seterrorhandling(fh, PIO_INTERNAL_ERROR)
else
phis_hires = .false.
end if
! Spectral truncation
if (.not. single_column) then
#if (( ! defined BFB_CAM_SCAM_IOP ) || ( defined DO_SPETRU ))
call spetru_phis(phis_tmp, phis_hires)
#endif
end if
#if ( defined SPMD )
numperlat = plon
call compute_gsfactors (numperlat, numrecv, numsend, displs)
call mpiscatterv (phis_tmp ,numsend, displs, mpir8,phis ,numrecv, mpir8,0,mpicom)
#else
phis = phis_tmp
#endif
end select
end subroutine process_inidat
!=========================================================================================
subroutine global_int()
! Compute global integrals of mass, moisture and geopotential height
! and fix mass of atmosphere
use commap
use physconst, only: gravit
#if ( defined SPMD )
use mpishorthand
use spmd_dyn, only: compute_gsfactors
use spmd_utils, only: npes
#endif
use hycoef, only: hyai, ps0
use eul_control_mod, only: pdela, qmass1, tmassf, fixmas, &
tmass0, zgsint, qmass2, qmassf
use inic_analytic, only: analytic_ic_active
!---------------------------Local workspace-----------------------------
integer i,k,lat,ihem,irow ! grid indices
real(r8) pdelb(plon,plev) ! pressure diff between interfaces
! using "B" part of hybrid grid only
real(r8) pssum ! surface pressure sum
real(r8) dotproda ! dot product
real(r8) dotprodb ! dot product
real(r8) zgssum ! partial sums of phis
real(r8) hyad (plev) ! del (A)
real(r8) tmassf_tmp ! Global mass integral
real(r8) qmass1_tmp ! Partial Global moisture mass integral