forked from ESCOMP/MOSART
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RtmMod.F90
2966 lines (2561 loc) · 112 KB
/
RtmMod.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
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 RtmMod
!-----------------------------------------------------------------------
!BOP
!
! !MODULE: RtmMod
!
! !DESCRIPTION:
! Mosart Routing Model
!
! !USES:
use shr_kind_mod , only : r8 => shr_kind_r8
use shr_sys_mod , only : shr_sys_flush
use shr_const_mod , only : SHR_CONST_PI, SHR_CONST_CDAY
use RtmVar , only : nt_rtm, rtm_tracers, nt_rtm_dom, rtm_tracers_dom
use RtmSpmd , only : masterproc, npes, iam, mpicom_rof, ROFID, mastertask, &
MPI_REAL8,MPI_INTEGER,MPI_CHARACTER,MPI_LOGICAL,MPI_MAX
use RtmVar , only : re, spval, rtmlon, rtmlat, iulog, ice_runoff, &
frivinp_rtm, finidat_rtm, nrevsn_rtm, &
nsrContinue, nsrBranch, nsrStartup, nsrest, &
inst_index, inst_suffix, inst_name, &
smat_option, decomp_option, &
bypass_routing_option, qgwl_runoff_option, &
barrier_timers
use RtmFileUtils , only : getfil, getavu, relavu
use RtmTimeManager , only : timemgr_init, get_nstep, get_curr_date
use RtmHistFlds , only : RtmHistFldsInit, RtmHistFldsSet
use RtmHistFile , only : RtmHistUpdateHbuf, RtmHistHtapesWrapup, RtmHistHtapesBuild, &
rtmhist_ndens, rtmhist_mfilt, rtmhist_nhtfrq, &
rtmhist_avgflag_pertape, rtmhist_avgflag_pertape, &
rtmhist_fincl1, rtmhist_fincl2, rtmhist_fincl3, &
rtmhist_fexcl1, rtmhist_fexcl2, rtmhist_fexcl3, &
max_tapes, max_namlen
use RtmRestFile , only : RtmRestTimeManager, RtmRestGetFile, RtmRestFileRead, &
RtmRestFileWrite, RtmRestFileName
use RunoffMod , only : RunoffInit, rtmCTL, Tctl, Tunit, TRunoff, Tpara, &
gsmap_r, &
SMatP_dnstrm, avsrc_dnstrm, avdst_dnstrm, &
SMatP_direct, avsrc_direct, avdst_direct, &
SMatP_eroutUp, avsrc_eroutUp, avdst_eroutUp, &
sMatP_domRUp, avsrc_domRUp, avdst_domRUp, &
Tdom
use MOSART_physics_mod, only : Euler
use MOSART_physics_mod, only : updatestate_hillslope, updatestate_subnetwork, &
updatestate_mainchannel
use RtmIO
use mct_mod
use perf_mod
use pio
!
! !PUBLIC TYPES:
implicit none
private
!
! !PUBLIC MEMBER FUNCTIONS:
public Rtmini ! Initialize MOSART grid
public Rtmrun ! River routing model
!
! !REVISION HISTORY:
! Author: Sam Levis
!
! !PRIVATE MEMBER FUNCTIONS:
private :: RtmFloodInit
! !PRIVATE TYPES:
! MOSART tracers
character(len=256) :: rtm_trstr ! tracer string
! MOSART namelists
integer, save :: coupling_period ! mosart coupling period
integer, save :: delt_mosart ! mosart internal timestep (->nsub)
! MOSART constants
real(r8) :: cfl_scale = 1.0_r8 ! cfl scale factor, must be <= 1.0
real(r8) :: river_depth_minimum = 1.e-4 ! gridcell average minimum river depth [m]
!global (glo)
integer , pointer :: ID0_global(:) ! local ID index
integer , pointer :: dnID_global(:) ! downstream ID based on ID0
real(r8), pointer :: area_global(:) ! area
integer , pointer :: IDkey(:) ! translation key from ID to gindex
!local (gdc)
real(r8), save, pointer :: evel(:,:) ! effective tracer velocity (m/s)
real(r8), save, pointer :: flow(:,:) ! mosart flow (m3/s)
real(r8), save, pointer :: erlateral2(:,:) ! mosart flow (m3/s)
real(r8), save, pointer :: etin(:,:) ! mosart flow (m3/s)
real(r8), save, pointer :: flowdom(:,:) ! mosart flow (kg/s)
real(r8), save, pointer :: Houtdom(:,:) ! mosart flow (kg/s)
real(r8), save, pointer :: Toutdom(:,:) ! mosart flow (kg/s)
real(r8), save, pointer :: erout_prev(:,:) ! erout previous timestep (m3/s)
real(r8), save, pointer :: eroutup_avg(:,:)! eroutup average over coupling period (m3/s)
real(r8), save, pointer :: erlat_avg(:,:) ! erlateral average over coupling period (m3/s)
! global MOSART grid
real(r8),pointer :: rlatc(:) ! latitude of 1d grid cell (deg)
real(r8),pointer :: rlonc(:) ! longitude of 1d grid cell (deg)
real(r8),pointer :: rlats(:) ! latitude of 1d south grid cell edge (deg)
real(r8),pointer :: rlatn(:) ! latitude of 1d north grid cell edge (deg)
real(r8),pointer :: rlonw(:) ! longitude of 1d west grid cell edge (deg)
real(r8),pointer :: rlone(:) ! longitude of 1d east grid cell edge (deg)
logical :: do_rtmflood
character(len=256) :: nlfilename_rof = 'mosart_in'
!
!EOP
!-----------------------------------------------------------------------
contains
!-----------------------------------------------------------------------
!BOP
!
! !IROUTINE: Rtmini
!
! !INTERFACE:
subroutine Rtmini(flood_active)
!
! !DESCRIPTION:
! Initialize MOSART grid, mask, decomp
!
! !USES:
!
! !ARGUMENTS:
implicit none
logical, intent(out) :: flood_active
!
! !CALLED FROM:
! subroutine initialize in module initializeMod
!
! !REVISION HISTORY:
! Author: Sam Levis
! Update: T Craig, Dec 2006
!
!
! !LOCAL VARIABLES:
!EOP
real(r8) :: effvel0 = 10.0_r8 ! default velocity (m/s)
real(r8) :: effvel(nt_rtm) ! downstream velocity (m/s)
real(r8) :: edgen ! North edge of the direction file
real(r8) :: edgee ! East edge of the direction file
real(r8) :: edges ! South edge of the direction file
real(r8) :: edgew ! West edge of the direction file
integer :: i,j,k,n,ng,g,n2,nt,nn,ntdom ! loop indices
integer :: i1,j1,i2,j2
integer :: im1,ip1,jm1,jp1,ir,jr,nr ! neighbor indices
real(r8) :: deg2rad ! pi/180
real(r8) :: dx,dx1,dx2,dx3 ! lon dist. betn grid cells (m)
real(r8) :: dy ! lat dist. betn grid cells (m)
real(r8) :: lrtmarea ! tmp local sum of area
real(r8),allocatable :: tempr(:,:) ! temporary buffer
integer ,allocatable :: itempr(:,:) ! temporary buffer
integer ,allocatable :: idxocn(:) ! downstream ocean outlet cell
integer ,allocatable :: nupstrm(:) ! number of upstream cells including own cell
integer ,allocatable :: pocn(:) ! pe number assigned to basin
integer ,allocatable :: nop(:) ! number of gridcells on a pe
integer ,allocatable :: nba(:) ! number of basins on each pe
integer ,allocatable :: nrs(:) ! begr on each pe
integer ,allocatable :: basin(:) ! basin to mosart mapping
integer :: nmos,nmos_chk ! number of mosart points
integer :: nout,nout_chk ! number of basin with outlets
integer :: nbas,nbas_chk ! number of basin/ocean points
integer :: nrof,nrof_chk ! num of active mosart points
integer :: baspe ! pe with min number of mosart cells
integer :: maxrtm ! max num of rtms per pe for decomp
integer :: minbas,maxbas ! used for decomp search
integer :: nl,nloops ! used for decomp search
integer :: ier ! error code
integer :: mon ! month (1, ..., 12)
integer :: day ! day of month (1, ..., 31)
integer :: numr ! tot num of roff pts on all pes
real(r8) :: dtover,dtovermax ! ts calc temporaries
type(file_desc_t) :: ncid ! netcdf file id
integer :: dimid ! netcdf dimension identifier
integer :: nroflnd ! local number of land runoff
integer :: nrofocn ! local number of ocn runoff
integer :: pid,np,npmin,npmax,npint ! log loop control
integer :: na,nb,ns ! mct sizes
integer :: ni,no,go ! tmps
integer ,pointer :: rgdc2glo(:) ! temporary for initialization
integer ,pointer :: rglo2gdc(:) ! temporary for initialization
integer ,pointer :: gmask(:) ! global mask
logical :: found ! flag
character(len=256):: fnamer ! name of netcdf restart file
character(len=256):: pnamer ! full pathname of netcdf restart file
character(len=256):: locfn ! local file name
character(len=16384) :: rList ! list of fields for SM multiply
integer :: unitn ! unit for namelist file
#ifdef NDEBUG
integer,parameter :: dbug = 0 ! 0 = none, 1=normal, 2=much, 3=max
#else
integer,parameter :: dbug = 3 ! 0 = none, 1=normal, 2=much, 3=max
#endif
logical :: lexist ! File exists
character(len= 7) :: runtyp(4) ! run type
integer ,allocatable :: gindex(:) ! global index
integer :: cnt, lsize, gsize ! counter
integer :: igrow,igcol,iwgt ! mct field indices
type(mct_avect) :: avtmp, avtmpG ! temporary avects
type(mct_sMat) :: sMat ! temporary sparse matrix, needed for sMatP
character(len=*),parameter :: subname = '(Rtmini) '
!-----------------------------------------------------------------------
!-------------------------------------------------------
! Read in mosart namelist
!-------------------------------------------------------
namelist /mosart_inparm / ice_runoff, do_rtmflood, &
frivinp_rtm, finidat_rtm, nrevsn_rtm, coupling_period, &
rtmhist_ndens, rtmhist_mfilt, rtmhist_nhtfrq, &
rtmhist_fincl1, rtmhist_fincl2, rtmhist_fincl3, &
rtmhist_fexcl1, rtmhist_fexcl2, rtmhist_fexcl3, &
rtmhist_avgflag_pertape, decomp_option, &
bypass_routing_option, qgwl_runoff_option, &
smat_option, delt_mosart
! Preset values
do_rtmflood = .false.
ice_runoff = .true.
finidat_rtm = ' '
nrevsn_rtm = ' '
coupling_period = -1
delt_mosart = 3600
decomp_option = 'basin'
bypass_routing_option = 'direct_in_place'
qgwl_runoff_option = 'threshold'
smat_option = 'opt'
nlfilename_rof = "mosart_in" // trim(inst_suffix)
inquire (file = trim(nlfilename_rof), exist = lexist)
if ( .not. lexist ) then
write(iulog,*) subname // ' ERROR: nlfilename_rof does NOT exist:'&
//trim(nlfilename_rof)
call shr_sys_abort(trim(subname)//' ERROR nlfilename_rof does not exist')
end if
if (masterproc) then
unitn = getavu()
write(iulog,*) 'Read in mosart_inparm namelist from: ', trim(nlfilename_rof)
open( unitn, file=trim(nlfilename_rof), status='old' )
ier = 1
do while ( ier /= 0 )
read(unitn, mosart_inparm, iostat=ier)
if (ier < 0) then
call shr_sys_abort( subname//' encountered end-of-file on mosart_inparm read' )
endif
end do
call relavu( unitn )
end if
call mpi_bcast (coupling_period, 1, MPI_INTEGER, 0, mpicom_rof, ier)
call mpi_bcast (delt_mosart , 1, MPI_INTEGER, 0, mpicom_rof, ier)
call mpi_bcast (finidat_rtm , len(finidat_rtm) , MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (frivinp_rtm , len(frivinp_rtm) , MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (nrevsn_rtm , len(nrevsn_rtm) , MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (decomp_option, len(decomp_option), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (smat_option , len(smat_option) , MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (bypass_routing_option, len(bypass_routing_option), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (qgwl_runoff_option, len(qgwl_runoff_option), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (do_rtmflood, 1, MPI_LOGICAL, 0, mpicom_rof, ier)
call mpi_bcast (ice_runoff, 1, MPI_LOGICAL, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_nhtfrq, size(rtmhist_nhtfrq), MPI_INTEGER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_mfilt , size(rtmhist_mfilt) , MPI_INTEGER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_ndens , size(rtmhist_ndens) , MPI_INTEGER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_fexcl1, (max_namlen+2)*size(rtmhist_fexcl1), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_fexcl2, (max_namlen+2)*size(rtmhist_fexcl2), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_fexcl3, (max_namlen+2)*size(rtmhist_fexcl3), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_fincl1, (max_namlen+2)*size(rtmhist_fincl1), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_fincl2, (max_namlen+2)*size(rtmhist_fincl2), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_fincl3, (max_namlen+2)*size(rtmhist_fincl3), MPI_CHARACTER, 0, mpicom_rof, ier)
call mpi_bcast (rtmhist_avgflag_pertape, size(rtmhist_avgflag_pertape), MPI_CHARACTER, 0, mpicom_rof, ier)
runtyp(:) = 'missing'
runtyp(nsrStartup + 1) = 'initial'
runtyp(nsrContinue + 1) = 'restart'
runtyp(nsrBranch + 1) = 'branch '
if (masterproc) then
write(iulog,*) 'define run:'
write(iulog,*) ' run type = ',runtyp(nsrest+1)
!write(iulog,*) ' case title = ',trim(ctitle)
!write(iulog,*) ' username = ',trim(username)
!write(iulog,*) ' hostname = ',trim(hostname)
write(iulog,*) ' coupling_period = ',coupling_period
write(iulog,*) ' delt_mosart = ',delt_mosart
write(iulog,*) ' decomp option = ',trim(decomp_option)
write(iulog,*) ' bypass_routing option = ',trim(bypass_routing_option)
write(iulog,*) ' qgwl runoff option = ',trim(qgwl_runoff_option)
write(iulog,*) ' smat option = ',trim(smat_option)
if (nsrest == nsrStartup .and. finidat_rtm /= ' ') then
write(iulog,*) ' MOSART initial data = ',trim(finidat_rtm)
end if
endif
flood_active = do_rtmflood
if (frivinp_rtm == ' ') then
call shr_sys_abort( subname//' ERROR: frivinp_rtm NOT set' )
else
if (masterproc) then
write(iulog,*) ' MOSART river data = ',trim(frivinp_rtm)
endif
end if
if (coupling_period <= 0) then
write(iulog,*) subname,' ERROR MOSART coupling_period invalid',coupling_period
call shr_sys_abort( subname//' ERROR: coupling_period invalid' )
endif
if (delt_mosart <= 0) then
write(iulog,*) subname,' ERROR MOSART delt_mosart invalid',delt_mosart
call shr_sys_abort( subname//' ERROR: delt_mosart invalid' )
endif
do i = 1, max_tapes
if (rtmhist_nhtfrq(i) == 0) then
rtmhist_mfilt(i) = 1
else if (rtmhist_nhtfrq(i) < 0) then
rtmhist_nhtfrq(i) = nint(-rtmhist_nhtfrq(i)*SHR_CONST_CDAY/(24._r8*coupling_period))
endif
end do
!-------------------------------------------------------
! Initialize MOSART time manager
!-------------------------------------------------------
! Intiialize MOSART pio
call ncd_pio_init()
! Obtain restart file if appropriate
if ((nsrest == nsrStartup .and. finidat_rtm /= ' ') .or. &
(nsrest == nsrContinue) .or. &
(nsrest == nsrBranch )) then
call RtmRestGetfile( file=fnamer, path=pnamer )
endif
! Initialize time manager
if (nsrest == nsrStartup) then
call timemgr_init(dtime_in=coupling_period)
else
call RtmRestTimeManager(file=fnamer)
end if
!-------------------------------------------------------
! Initialize rtm_trstr
!-------------------------------------------------------
rtm_trstr = trim(rtm_tracers(1))
do n = 2,nt_rtm
rtm_trstr = trim(rtm_trstr)//':'//trim(rtm_tracers(n))
enddo
if (masterproc) then
write(iulog,*)'MOSART tracers = ',nt_rtm,trim(rtm_trstr)
end if
rtm_trstr = trim(rtm_tracers_dom(1))
do n = 2,nt_rtm_dom
rtm_trstr = trim(rtm_trstr)//':'//trim(rtm_tracers_dom(n))
enddo
if (masterproc) then
write(iulog,*)'MOSART tracers dom = ',nt_rtm_dom,trim(rtm_trstr)
end if
!-------------------------------------------------------
! Read input data (river direction file)
!-------------------------------------------------------
! Useful constants and initial values
deg2rad = SHR_CONST_PI / 180._r8
call t_startf('mosarti_grid')
call getfil(frivinp_rtm, locfn, 0 )
if (masterproc) then
write(iulog,*) 'Read in MOSART file name: ',trim(frivinp_rtm)
call shr_sys_flush(iulog)
endif
call ncd_pio_openfile (ncid, trim(locfn), 0)
call ncd_inqdid(ncid,'lon',dimid)
call ncd_inqdlen(ncid,dimid,rtmlon)
call ncd_inqdid(ncid,'lat',dimid)
call ncd_inqdlen(ncid,dimid,rtmlat)
if (masterproc) then
write(iulog,*) 'Values for rtmlon/rtmlat: ',rtmlon,rtmlat
write(iulog,*) 'Successfully read MOSART dimensions'
call shr_sys_flush(iulog)
endif
! Allocate variables
allocate(rlonc(rtmlon), rlatc(rtmlat), &
rlonw(rtmlon), rlone(rtmlon), &
rlats(rtmlat), rlatn(rtmlat), &
rtmCTL%rlon(rtmlon), &
rtmCTL%rlat(rtmlat), &
stat=ier)
if (ier /= 0) then
write(iulog,*) subname,' : Allocation ERROR for rlon'
call shr_sys_abort(subname//' ERROR alloc for rlon')
end if
! reading the routing parameters
allocate ( &
ID0_global(rtmlon*rtmlat), area_global(rtmlon*rtmlat), &
dnID_global(rtmlon*rtmlat), &
stat=ier)
if (ier /= 0) then
write(iulog,*) subname, ' : Allocation error for ID0_global'
call shr_sys_abort(subname//' ERROR alloc for ID0')
end if
allocate(tempr(rtmlon,rtmlat))
allocate(itempr(rtmlon,rtmlat))
call ncd_io(ncid=ncid, varname='longxy', flag='read', data=tempr, readvar=found)
if ( .not. found ) call shr_sys_abort( trim(subname)//' ERROR: read MOSART longitudes')
if (masterproc) write(iulog,*) 'Read longxy ',minval(tempr),maxval(tempr)
do i=1,rtmlon
rtmCTL%rlon(i) = tempr(i,1)
rlonc(i) = tempr(i,1)
enddo
if (masterproc) write(iulog,*) 'rlonc ',minval(rlonc),maxval(rlonc)
call ncd_io(ncid=ncid, varname='latixy', flag='read', data=tempr, readvar=found)
if ( .not. found ) call shr_sys_abort( trim(subname)//' ERROR: read MOSART latitudes')
if (masterproc) write(iulog,*) 'Read latixy ',minval(tempr),maxval(tempr)
do j=1,rtmlat
rtmCTL%rlat(j) = tempr(1,j)
rlatc(j) = tempr(1,j)
end do
if (masterproc) write(iulog,*) 'rlatc ',minval(rlatc),maxval(rlatc)
call ncd_io(ncid=ncid, varname='area', flag='read', data=tempr, readvar=found)
if ( .not. found ) call shr_sys_abort( trim(subname)//' ERROR: read MOSART area')
if (masterproc) write(iulog,*) 'Read area ',minval(tempr),maxval(tempr)
do j=1,rtmlat
do i=1,rtmlon
n = (j-1)*rtmlon + i
area_global(n) = tempr(i,j)
end do
end do
if (masterproc) write(iulog,*) 'area ',minval(tempr),maxval(tempr)
call ncd_io(ncid=ncid, varname='ID', flag='read', data=itempr, readvar=found)
if ( .not. found ) call shr_sys_abort( trim(subname)//' ERROR: read MOSART ID')
if (masterproc) write(iulog,*) 'Read ID ',minval(itempr),maxval(itempr)
do j=1,rtmlat
do i=1,rtmlon
n = (j-1)*rtmlon + i
ID0_global(n) = itempr(i,j)
end do
end do
if (masterproc) write(iulog,*) 'ID ',minval(itempr),maxval(itempr)
call ncd_io(ncid=ncid, varname='dnID', flag='read', data=itempr, readvar=found)
if ( .not. found ) call shr_sys_abort( trim(subname)//' ERROR: read MOSART dnID')
if (masterproc) write(iulog,*) 'Read dnID ',minval(itempr),maxval(itempr)
do j=1,rtmlat
do i=1,rtmlon
n = (j-1)*rtmlon + i
dnID_global(n) = itempr(i,j)
end do
end do
if (masterproc) write(iulog,*) 'dnID ',minval(itempr),maxval(itempr)
deallocate(tempr)
deallocate(itempr)
call ncd_pio_closefile(ncid)
!-------------------------------------------------------
! RESET dnID indices based on ID0
! rename the dnID values to be consistent with global grid indexing.
! where 1 = lower left of grid and rtmlon*rtmlat is upper right.
! ID0 is the "key", modify dnID based on that. keep the IDkey around
! for as long as needed. This is a key that translates the ID0 value
! to the gindex value. compute the key, then apply the key to dnID_global.
! As part of this, check that each value of ID0 is unique and within
! the range of 1 to rtmlon*rtmlat.
!-------------------------------------------------------
allocate(IDkey(rtmlon*rtmlat))
IDkey = 0
do n=1,rtmlon*rtmlat
if (ID0_global(n) < 0 .or. ID0_global(n) > rtmlon*rtmlat) then
write(iulog,*) subname,' ERROR ID0 out of range',n,ID0_global(n)
call shr_sys_abort(subname//' ERROR error ID0 out of range')
endif
if (IDkey(ID0_global(n)) /= 0) then
write(iulog,*) subname,' ERROR ID0 value occurs twice',n,ID0_global(n)
call shr_sys_abort(subname//' ERROR ID0 value occurs twice')
endif
IDkey(ID0_global(n)) = n
enddo
if (minval(IDkey) < 1) then
write(iulog,*) subname,' ERROR IDkey incomplete'
call shr_sys_abort(subname//' ERROR IDkey incomplete')
endif
do n=1,rtmlon*rtmlat
if (dnID_global(n) > 0 .and. dnID_global(n) <= rtmlon*rtmlat) then
if (IDkey(dnID_global(n)) > 0 .and. IDkey(dnID_global(n)) <= rtmlon*rtmlat) then
dnID_global(n) = IDkey(dnID_global(n))
else
write(iulog,*) subname,' ERROR bad IDkey',n,dnID_global(n),IDkey(dnID_global(n))
call shr_sys_abort(subname//' ERROR bad IDkey')
endif
endif
enddo
deallocate(ID0_global)
!-------------------------------------------------------
! Derive gridbox edges
!-------------------------------------------------------
! assuming equispaced grid, calculate edges from rtmlat/rtmlon
! w/o assuming a global grid
edgen = maxval(rlatc) + 0.5*abs(rlatc(1) - rlatc(2))
edges = minval(rlatc) - 0.5*abs(rlatc(1) - rlatc(2))
edgee = maxval(rlonc) + 0.5*abs(rlonc(1) - rlonc(2))
edgew = minval(rlonc) - 0.5*abs(rlonc(1) - rlonc(2))
if ( edgen .ne. 90._r8 )then
if ( masterproc ) write(iulog,*) 'Regional grid: edgen = ', edgen
end if
if ( edges .ne. -90._r8 )then
if ( masterproc ) write(iulog,*) 'Regional grid: edges = ', edges
end if
if ( edgee .ne. 180._r8 )then
if ( masterproc ) write(iulog,*) 'Regional grid: edgee = ', edgee
end if
if ( edgew .ne.-180._r8 )then
if ( masterproc ) write(iulog,*) 'Regional grid: edgew = ', edgew
end if
! Set edge latitudes (assumes latitudes are constant for a given longitude)
rlats(:) = edges
rlatn(:) = edgen
do j = 2, rtmlat
if (rlatc(2) > rlatc(1)) then ! South to North grid
rlats(j) = (rlatc(j-1) + rlatc(j)) / 2._r8
rlatn(j-1) = rlats(j)
else ! North to South grid
rlatn(j) = (rlatc(j-1) + rlatc(j)) / 2._r8
rlats(j-1) = rlatn(j)
end if
end do
! Set edge longitudes
rlonw(:) = edgew
rlone(:) = edgee
dx = (edgee - edgew) / rtmlon
do i = 2, rtmlon
rlonw(i) = rlonw(i) + (i-1)*dx
rlone(i-1) = rlonw(i)
end do
call t_stopf ('mosarti_grid')
!-------------------------------------------------------
! Determine mosart ocn/land mask (global, all procs)
!-------------------------------------------------------
call t_startf('mosarti_decomp')
allocate (gmask(rtmlon*rtmlat), stat=ier)
if (ier /= 0) then
write(iulog,*) subname, ' : Allocation ERROR for gmask'
call shr_sys_abort(subname//' ERROR alloc for gmask')
end if
! 1=land,
! 2=ocean,
! 3=ocean outlet from land
gmask = 2 ! assume ocean point
do n=1,rtmlon*rtmlat ! mark all downstream points as outlet
nr = dnID_global(n)
if ((nr > 0) .and. (nr <= rtmlon*rtmlat)) then
gmask(nr) = 3 ! <- nr
end if
enddo
do n=1,rtmlon*rtmlat ! now mark all points with downstream points as land
nr = dnID_global(n)
if ((nr > 0) .and. (nr <= rtmlon*rtmlat)) then
gmask(n) = 1 ! <- n
end if
enddo
!-------------------------------------------------------
! Compute total number of basins and runoff points
!-------------------------------------------------------
nbas = 0
nrof = 0
nout = 0
nmos = 0
do nr=1,rtmlon*rtmlat
if (gmask(nr) == 3) then
nout = nout + 1
nbas = nbas + 1
nmos = nmos + 1
nrof = nrof + 1
elseif (gmask(nr) == 2) then
nbas = nbas + 1
nrof = nrof + 1
elseif (gmask(nr) == 1) then
nmos = nmos + 1
nrof = nrof + 1
endif
enddo
if (masterproc) then
write(iulog,*) 'Number of outlet basins = ',nout
write(iulog,*) 'Number of total basins = ',nbas
write(iulog,*) 'Number of mosart points = ',nmos
write(iulog,*) 'Number of runoff points = ',nrof
endif
!-------------------------------------------------------
! Compute river basins, actually compute ocean outlet gridcell
!-------------------------------------------------------
! idxocn = final downstream cell, index is global 1d ocean gridcell
! nupstrm = number of source gridcells upstream including self
allocate(idxocn(rtmlon*rtmlat),nupstrm(rtmlon*rtmlat),stat=ier)
if (ier /= 0) then
write(iulog,*) subname,' : Allocation ERROR for ',&
'idxocn,nupstrm'
call shr_sys_abort(subname//' ERROR alloc for idxocn nupstrm')
end if
call t_startf('mosarti_dec_basins')
idxocn = 0
nupstrm = 0
do nr=1,rtmlon*rtmlat
n = nr
if (abs(gmask(n)) == 1) then ! land
g = 0
do while (abs(gmask(n)) == 1 .and. g < rtmlon*rtmlat) ! follow downstream
nupstrm(n) = nupstrm(n) + 1
n = dnID_global(n)
g = g + 1
end do
if (gmask(n) == 3) then ! found ocean outlet
nupstrm(n) = nupstrm(n) + 1 ! one more land cell for n
idxocn(nr) = n ! set ocean outlet or nr to n
elseif (abs(gmask(n)) == 1) then ! no ocean outlet, warn user, ignore cell
write(iulog,*) subname,' ERROR closed basin found', &
g,nr,gmask(nr),dnID_global(nr), &
n,gmask(n),dnID_global(n)
call shr_sys_abort(subname//' ERROR closed basin found')
elseif (gmask(n) == 2) then
write(iulog,*) subname,' ERROR found invalid ocean cell ',nr
call shr_sys_abort(subname//' ERROR found invalid ocean cell')
else
write(iulog,*) subname,' ERROR downstream cell is unknown', &
g,nr,gmask(nr),dnID_global(nr), &
n,gmask(n),dnID_global(n)
call shr_sys_abort(subname//' ERROR downstream cell is unknown')
endif
elseif (gmask(n) >= 2) then ! ocean, give to self
nupstrm(n) = nupstrm(n) + 1
idxocn(nr) = n
endif
enddo
call t_stopf('mosarti_dec_basins')
! check
nbas_chk = 0
nrof_chk = 0
do nr=1,rtmlon*rtmlat
! !if (masterproc) write(iulog,*) 'nupstrm check ',nr,gmask(nr),nupstrm(nr),idxocn(nr)
if (gmask(nr) >= 2 .and. nupstrm(nr) > 0) then
nbas_chk = nbas_chk + 1
nrof_chk = nrof_chk + nupstrm(nr)
endif
enddo
if (nbas_chk /= nbas .or. nrof_chk /= nrof) then
write(iulog,*) subname,' ERROR nbas nrof check',nbas,nbas_chk,nrof,nrof_chk
call shr_sys_abort(subname//' ERROR nbas nrof check')
endif
!-------------------------------------------------------
!--- Now allocate those basins to pes
!-------------------------------------------------------
call t_startf('mosarti_dec_distr')
!--- this is the heart of the decomp, need to set pocn and nop by the end of this
!--- pocn is the pe that gets the basin associated with ocean outlet nr
!--- nop is a running count of the number of mosart cells/pe
allocate(pocn(rtmlon*rtmlat), & !global mosart array
nop(0:npes-1), &
nba(0:npes-1))
pocn = -99
nop = 0
nba = 0
if (trim(decomp_option) == 'basin') then
baspe = 0
maxrtm = int(float(nrof)/float(npes)*0.445) + 1
nloops = 3
minbas = nrof
do nl=1,nloops
maxbas = minbas - 1
minbas = maxval(nupstrm)/(2**nl)
if (nl == nloops) minbas = min(minbas,1)
do nr=1,rtmlon*rtmlat
if (gmask(nr) >= 2 .and. nupstrm(nr) > 0 .and. nupstrm(nr) >= minbas .and. nupstrm(nr) <= maxbas) then
! Decomp options
! find min pe (implemented but scales poorly)
! use increasing thresholds (implemented, ok load balance for l2r or calc)
! distribute basins using above methods but work from max to min basin size
!
!--------------
! find min pe
! baspe = 0
! do n = 1,npes-1
! if (nop(n) < nop(baspe)) baspe = n
! enddo
!--------------
! find next pe below maxrtm threshhold and increment
do while (nop(baspe) > maxrtm)
baspe = baspe + 1
if (baspe > npes-1) then
baspe = 0
maxrtm = max(maxrtm*1.5, maxrtm+1.0) ! 3 loop, .445 and 1.5 chosen carefully
endif
enddo
!--------------
if (baspe > npes-1 .or. baspe < 0) then
write(iulog,*) 'ERROR in decomp for MOSART ',nr,npes,baspe
call shr_sys_abort('ERROR mosart decomp')
endif
nop(baspe) = nop(baspe) + nupstrm(nr)
nba(baspe) = nba(baspe) + 1
pocn(nr) = baspe
endif
enddo ! nr
enddo ! nl
! set pocn for land cells, was set for ocean above
do nr=1,rtmlon*rtmlat
if (idxocn(nr) > 0) then
pocn(nr) = pocn(idxocn(nr))
if (pocn(nr) < 0 .or. pocn(nr) > npes-1) then
write(iulog,*) subname,' ERROR pocn lnd setting ',&
nr,idxocn(nr),idxocn(idxocn(nr)),pocn(idxocn(nr)),pocn(nr),npes
call shr_sys_abort(subname//' ERROR pocn lnd')
endif
endif
enddo
elseif (trim(decomp_option) == '1d') then
! distribute active points in 1d fashion to pes
! baspe is the pe assignment
! maxrtm is the maximum number of points to assign to each pe
baspe = 0
maxrtm = (nrof-1)/npes + 1
do nr=1,rtmlon*rtmlat
if (gmask(nr) >= 1) then
pocn(nr) = baspe
nop(baspe) = nop(baspe) + 1
if (nop(baspe) >= maxrtm) then
baspe = (mod(baspe+1,npes))
if (baspe < 0 .or. baspe > npes-1) then
write(iulog,*) subname,' ERROR basepe ',baspe,npes
call shr_sys_abort(subname//' ERROR pocn lnd')
endif
endif
endif
enddo
elseif (trim(decomp_option) == 'roundrobin') then
! distribute active points in roundrobin fashion to pes
! baspe is the pe assignment
! maxrtm is the maximum number of points to assign to each pe
baspe = 0
do nr=1,rtmlon*rtmlat
if (gmask(nr) >= 1) then
pocn(nr) = baspe
nop(baspe) = nop(baspe) + 1
baspe = (mod(baspe+1,npes))
if (baspe < 0 .or. baspe > npes-1) then
write(iulog,*) subname,' ERROR basepe ',baspe,npes
call shr_sys_abort(subname//' ERROR pocn lnd')
endif
endif
enddo
else
write(iulog,*) subname,' ERROR decomp option unknown ',trim(decomp_option)
call shr_sys_abort(subname//' ERROR pocn lnd')
endif ! decomp_option
if (masterproc) then
write(iulog,*) 'MOSART cells and basins total = ',nrof,nbas
write(iulog,*) 'MOSART cells per basin avg/max = ',nrof/nbas,maxval(nupstrm)
write(iulog,*) 'MOSART cells per pe min/max = ',minval(nop),maxval(nop)
write(iulog,*) 'MOSART basins per pe min/max = ',minval(nba),maxval(nba)
endif
deallocate(nupstrm)
!-------------------------------------------------------
!--- Count and distribute cells to rglo2gdc
!-------------------------------------------------------
rtmCTL%numr = 0
rtmCTL%lnumr = 0
do n = 0,npes-1
if (iam == n) then
rtmCTL%begr = rtmCTL%numr + 1
endif
rtmCTL%numr = rtmCTL%numr + nop(n)
if (iam == n) then
rtmCTL%lnumr = rtmCTL%lnumr + nop(n)
rtmCTL%endr = rtmCTL%begr + rtmCTL%lnumr - 1
endif
enddo
allocate(rglo2gdc(rtmlon*rtmlat), & !global mosart array
nrs(0:npes-1))
nrs = 0
rglo2gdc = 0
! nrs is begr on each pe
nrs(0) = 1
do n = 1,npes-1
nrs(n) = nrs(n-1) + nop(n-1)
enddo
! reuse nba for nop-like counter here
! pocn -99 is unused cell
nba = 0
do nr = 1,rtmlon*rtmlat
if (pocn(nr) >= 0) then
rglo2gdc(nr) = nrs(pocn(nr)) + nba(pocn(nr))
nba(pocn(nr)) = nba(pocn(nr)) + 1
endif
enddo
do n = 0,npes-1
if (nba(n) /= nop(n)) then
write(iulog,*) subname,' ERROR mosart cell count ',n,nba(n),nop(n)
call shr_sys_abort(subname//' ERROR mosart cell count')
endif
enddo
deallocate(nop,nba,nrs)
deallocate(pocn)
call t_stopf('mosarti_dec_distr')
!-------------------------------------------------------
!--- adjust area estimation from DRT algorithm for those outlet grids
!--- useful for grid-based representation only
!--- need to compute areas where they are not defined in input file
!-------------------------------------------------------
do n=1,rtmlon*rtmlat
if (area_global(n) <= 0._r8) then
i = mod(n-1,rtmlon) + 1
j = (n-1)/rtmlon + 1
dx = (rlone(i) - rlonw(i)) * deg2rad
dy = sin(rlatn(j)*deg2rad) - sin(rlats(j)*deg2rad)
area_global(n) = abs(1.e6_r8 * dx*dy*re*re)
if (masterproc .and. area_global(n) <= 0) then
write(iulog,*) 'Warning! Zero area for unit ', n, area_global(n),dx,dy,re
end if
end if
end do
call t_stopf('mosarti_decomp')
!-------------------------------------------------------
!--- Write per-processor runoff bounds depending on dbug level
!-------------------------------------------------------
call t_startf('mosarti_print')
call shr_sys_flush(iulog)
if (masterproc) then
write(iulog,*) 'total runoff cells numr = ',rtmCTL%numr
endif
call shr_sys_flush(iulog)
call mpi_barrier(mpicom_rof,ier)
npmin = 0
npmax = npes-1
npint = 1
if (dbug == 0) then
npmax = 0
elseif (dbug == 1) then
npmax = min(npes-1,4)
elseif (dbug == 2) then
npint = npes/8
elseif (dbug == 3) then
npint = 1
endif
do np = npmin,npmax,npint
pid = np
if (dbug == 1) then
if (np == 2) pid=npes/2-1
if (np == 3) pid=npes-2
if (np == 4) pid=npes-1
endif
pid = max(pid,0)
pid = min(pid,npes-1)
if (iam == pid) then
write(iulog,'(2a,i9,a,i9,a,i9,a,i9)') &
'MOSART decomp info',' proc = ',iam, &
' begr = ',rtmCTL%begr,&
' endr = ',rtmCTL%endr, &
' numr = ',rtmCTL%lnumr
endif
call shr_sys_flush(iulog)
call mpi_barrier(mpicom_rof,ier)
enddo
call t_stopf('mosarti_print')
!-------------------------------------------------------
! Allocate local flux variables
!-------------------------------------------------------
call t_startf('mosarti_vars')
allocate (evel (rtmCTL%begr:rtmCTL%endr,nt_rtm), &
flow (rtmCTL%begr:rtmCTL%endr,nt_rtm), &
flowdom (rtmCTL%begr:rtmCTL%endr,nt_rtm_dom), &
erlateral2 (rtmCTL%begr:rtmCTL%endr,nt_rtm), &
etin (rtmCTL%begr:rtmCTL%endr,nt_rtm), &
Houtdom (rtmCTL%begr:rtmCTL%endr,nt_rtm_dom), &
Toutdom (rtmCTL%begr:rtmCTL%endr,nt_rtm_dom), &
erout_prev(rtmCTL%begr:rtmCTL%endr,nt_rtm), &
eroutup_avg(rtmCTL%begr:rtmCTL%endr,nt_rtm), &
erlat_avg(rtmCTL%begr:rtmCTL%endr,nt_rtm), &
stat=ier)
if (ier /= 0) then
write(iulog,*) subname,' Allocation ERROR for flow'
call shr_sys_abort(subname//' Allocationt ERROR flow')
end if
flow(:,:) = 0._r8
erlateral2(:,:) = 0._r8
etin(:,:) = 0._r8
flowdom(:,:) = 0._r8
Houtdom(:,:) = 0._r8
Toutdom(:,:) = 0._r8
erout_prev(:,:) = 0._r8
eroutup_avg(:,:) = 0._r8
erlat_avg(:,:) = 0._r8
!-------------------------------------------------------
! Allocate runoff datatype
!-------------------------------------------------------
call RunoffInit(rtmCTL%begr, rtmCTL%endr, rtmCTL%numr)
!-------------------------------------------------------
! Initialize mosart flood - rtmCTL%fthresh and evel
!-------------------------------------------------------
if (do_rtmflood) then
write(iulog,*) subname,' Flood not validated in this version, abort'
call shr_sys_abort(subname//' Flood feature unavailable')
call RtmFloodInit (frivinp_rtm, rtmCTL%begr, rtmCTL%endr, rtmCTL%fthresh, evel)
else
effvel(:) = effvel0 ! downstream velocity (m/s)
rtmCTL%fthresh(:) = abs(spval)
do nt = 1,nt_rtm
do nr = rtmCTL%begr,rtmCTL%endr
evel(nr,nt) = effvel(nt)
enddo
enddo
end if
!-------------------------------------------------------
! Initialize runoff data type
!-------------------------------------------------------
allocate(rgdc2glo(rtmCTL%numr), stat=ier)
if (ier /= 0) then
write(iulog,*) subname,' ERROR allocation of rgdc2glo'
call shr_sys_abort(subname//' ERROR allocate of rgdc2glo')
end if
! Set map from local to global index space
numr = 0
do j = 1,rtmlat
do i = 1,rtmlon
n = (j-1)*rtmlon + i
nr = rglo2gdc(n)