-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathmo_tuvx.F90
1947 lines (1655 loc) · 87.8 KB
/
mo_tuvx.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
!----------------------------------------------------------------------
! Wrapper for TUV-x photolysis rate constant calculator
!----------------------------------------------------------------------
module mo_tuvx
use musica_map, only : map_t
use musica_string, only : string_t
use ppgrid, only : pver, & ! number of vertical layers
pverp ! number of vertical interfaces (pver + 1)
use shr_kind_mod, only : r8 => shr_kind_r8, cl=>shr_kind_cl
use tuvx_core, only : core_t
use tuvx_grid_from_host, only : grid_updater_t
use tuvx_profile_from_host, only : profile_updater_t
use tuvx_radiator_from_host, only : radiator_updater_t
implicit none
private
public :: tuvx_readnl
public :: tuvx_register
public :: tuvx_init
public :: tuvx_timestep_init
public :: tuvx_get_photo_rates
public :: tuvx_finalize
public :: tuvx_active
! Inidices for grid updaters
integer, parameter :: NUM_GRIDS = 2 ! number of grids that CAM will update at runtime
integer, parameter :: GRID_INDEX_HEIGHT = 1 ! Height grid index
integer, parameter :: GRID_INDEX_WAVELENGTH = 2 ! Wavelength grid index
! Indices for profile updaters
integer, parameter :: NUM_PROFILES = 8 ! number of profiles that CAM will update at runtime
integer, parameter :: PROFILE_INDEX_TEMPERATURE = 1 ! Temperature profile index
integer, parameter :: PROFILE_INDEX_ALBEDO = 2 ! Surface albedo profile index
integer, parameter :: PROFILE_INDEX_ET_FLUX = 3 ! Extraterrestrial flux profile index
integer, parameter :: PROFILE_INDEX_AIR = 4 ! Air density profile index
integer, parameter :: PROFILE_INDEX_O3 = 5 ! Ozone profile index
integer, parameter :: PROFILE_INDEX_O2 = 6 ! Molecular oxygen profile index
integer, parameter :: PROFILE_INDEX_SO2 = 7 ! Sulfur dioxide profile index
integer, parameter :: PROFILE_INDEX_NO2 = 8 ! Nitrogen dioxide profile index
! Indices for radiator updaters
integer, parameter :: NUM_RADIATORS = 2 ! number of radiators that CAM will update at runtime
integer, parameter :: RADIATOR_INDEX_AEROSOL = 1 ! Aerosol radiator index
integer, parameter :: RADIATOR_INDEX_CLOUDS = 2 ! Cloud radiator index
! Definition of the MS93 wavelength grid TODO add description of this
integer, parameter :: NUM_BINS_MS93 = 4
real(kind=r8), parameter :: WAVELENGTH_EDGES_MS93(NUM_BINS_MS93+1) = &
(/ 181.6_r8, 183.1_r8, 184.6_r8, 190.2_r8, 192.5_r8 /)
! Heating rate indices
integer :: number_of_heating_rates = 0 ! number of heating rates in TUV-x
integer :: index_cpe_jo2_a = -1 ! index for jo2_a in heating rate array
integer :: index_cpe_jo2_b = -1 ! index for jo2_b in heating rate array
integer :: index_cpe_jo3_a = -1 ! index for jo3_a in heating rate array
integer :: index_cpe_jo3_b = -1 ! index for jo3_b in heating rate array
integer :: cpe_jo2_a_pbuf_index = -1 ! index in physics buffer for jo2_a heating rate
integer :: cpe_jo2_b_pbuf_index = -1 ! index in physics buffer for jo2_b heating rate
integer :: cpe_jo3_a_pbuf_index = -1 ! index in physics buffer for jo3_a heating rate
integer :: cpe_jo3_b_pbuf_index = -1 ! index in physics buffer for jo3_b heating rate
! Information needed to access CAM species state data
logical :: is_fixed_N2 = .false. ! indicates whether N2 concentrations are fixed
logical :: is_fixed_O = .false. ! indicates whether O concentrations are fixed
logical :: is_fixed_O2 = .false. ! indicates whether O2 concentrations are fixed
logical :: is_fixed_O3 = .false. ! indicates whether O3 concentrations are fixed
logical :: is_fixed_NO = .false. ! indicates whether NO concentrations are fixed
integer :: index_N2 = 0 ! index for N2 in concentration array
integer :: index_O = 0 ! index for O in concentration array
integer :: index_O2 = 0 ! index for O2 in concentration array
integer :: index_O3 = 0 ! index for O3 in concentration array
integer :: index_NO = 0 ! index for NO in concentration array
! Information needed to access aerosol and cloud optical properties
logical :: do_aerosol = .false. ! indicates whether aerosol optical properties
! are available and should be used in radiative
! transfer calculations
logical :: do_clouds = .false. ! indicates whether cloud optical properties
! should be calculated and used in radiative
! transfer calculations
! Information needed to set extended-UV photo rates
logical :: do_euv = .false. ! Indicates whether to calculate
! extended-UV photo rates
integer :: ion_rates_pbuf_index = 0 ! Index in physics buffer for
! ionization rates
! Information needed to do special NO photolysis rate calculation
logical :: do_jno = .false. ! Indicates whether to calculate jno
integer :: jno_index = 0 ! Index in tuvx_ptr::photo_rates_ array for jno
! Cutoff solar zenith angle for doing photolysis rate calculations [degrees]
real(r8) :: max_sza = 0.0_r8
! TODO how should these paths be set and communicated to this wrapper?
character(len=*), parameter :: wavelength_config_path = &
"data/grids/wavelength/cam.csv"
logical, parameter :: enable_diagnostics = .true.
! TUV-x calculator for each OMP thread
type :: tuvx_ptr
type(core_t), pointer :: core_ => null( ) ! TUV-x calculator
integer :: n_photo_rates_ = 0 ! number of photo reactions in TUV-x
integer :: n_euv_rates_ = 0 ! number of extreme-UV rates
integer :: n_special_rates_ = 0 ! number of special photo rates
integer :: n_wavelength_bins_ = 0 ! number of wavelength bins in TUV-x
type(map_t) :: photo_rate_map_ ! map between TUV-x and CAM
! photo rate constant arrays
type(grid_updater_t) :: grids_(NUM_GRIDS) ! grid updaters
type(profile_updater_t) :: profiles_(NUM_PROFILES) ! profile updaters
type(radiator_updater_t) :: radiators_(NUM_RADIATORS) ! radiator updaters
real(r8) :: height_delta_(pver+1) ! change in height in each
! vertical layer (km)
real(r8), allocatable :: wavelength_edges_(:) ! TUV-x wavelength bin edges (nm)
! on the TUV-x wavelength grid
real(r8) :: et_flux_ms93_(NUM_BINS_MS93) ! extraterrestrial flux on the MS93 grid
! [photon cm-2 nm-1 s-1]
end type tuvx_ptr
type(tuvx_ptr), allocatable :: tuvx_ptrs(:)
! Diagnostic photolysis rate constant output
type :: diagnostic_t
character(len=:), allocatable :: name_ ! Name of the output field
integer :: index_ ! index of the photolysis rate constant from TUV-x
end type diagnostic_t
type(diagnostic_t), allocatable :: diagnostics(:)
! namelist options
character(len=cl) :: tuvx_config_path = 'NONE' ! absolute path to TUVX configuration file
logical, protected :: tuvx_active = .false.
!================================================================================================
contains
!================================================================================================
!-----------------------------------------------------------------------
! registers fields in the physics buffer
!-----------------------------------------------------------------------
subroutine tuvx_register( )
use mo_jeuv, only : nIonRates
use physics_buffer, only : pbuf_add_field, dtype_r8
use ppgrid, only : pcols ! maximum number of columns
if( .not. tuvx_active ) return
! add photo-ionization rates to physics buffer for WACCMX Ionosphere module
call pbuf_add_field( 'IonRates', 'physpkg', dtype_r8, (/ pcols, pver, nIonRates /), &
ion_rates_pbuf_index ) ! Ionization rates for O+, O2+, N+, N2+, NO+
call pbuf_add_field( 'CPE_jO2a', 'global', dtype_r8, (/ pcols, pver /), cpe_jo2_a_pbuf_index )
call pbuf_add_field( 'CPE_jO2b', 'global', dtype_r8, (/ pcols, pver /), cpe_jo2_b_pbuf_index )
call pbuf_add_field( 'CPE_jO3a', 'global', dtype_r8, (/ pcols, pver /), cpe_jo3_a_pbuf_index )
call pbuf_add_field( 'CPE_jO3b', 'global', dtype_r8, (/ pcols, pver /), cpe_jo3_b_pbuf_index )
end subroutine tuvx_register
!================================================================================================
!-----------------------------------------------------------------------
! read namelist options
!-----------------------------------------------------------------------
subroutine tuvx_readnl(nlfile)
#ifdef HAVE_MPI
use mpi
#endif
use cam_abortutils, only : endrun
use cam_logfile, only : iulog ! log file output unit
use namelist_utils, only : find_group_name
use spmd_utils, only : mpicom, is_main_task => masterproc, &
main_task => masterprocid
character(len=*), intent(in) :: nlfile ! filepath for file containing namelist input
integer :: unitn, ierr
character(len=*), parameter :: subname = 'tuvx_readnl'
! ===================
! Namelist definition
! ===================
namelist /tuvx_opts/ tuvx_active, tuvx_config_path
! =============
! Read namelist
! =============
if (is_main_task) then
open( newunit=unitn, file=trim(nlfile), status='old' )
call find_group_name(unitn, 'tuvx_opts', status=ierr)
if (ierr == 0) then
read(unitn, tuvx_opts, iostat=ierr)
if (ierr /= 0) then
call endrun(subname // ':: ERROR reading namelist')
end if
end if
close(unitn)
end if
! ============================
! Broadcast namelist variables
! ============================
#ifdef HAVE_MPI
call mpi_bcast(tuvx_config_path, len(tuvx_config_path), mpi_character, main_task, mpicom, ierr)
call mpi_bcast(tuvx_active, 1, mpi_logical, main_task, mpicom, ierr)
#endif
if (tuvx_active .and. tuvx_config_path == 'NONE') then
call endrun(subname // ' : must set tuvx_config_path when TUV-X is active')
end if
if (is_main_task) then
write(iulog,*) 'tuvx_readnl: tuvx_config_path = ', trim(tuvx_config_path)
write(iulog,*) 'tuvx_readnl: tuvx_active = ', tuvx_active
end if
end subroutine tuvx_readnl
!================================================================================================
!-----------------------------------------------------------------------
! Initializes TUV-x for photolysis calculations
!-----------------------------------------------------------------------
subroutine tuvx_init( photon_file, electron_file, max_solar_zenith_angle, pbuf2d )
#ifdef HAVE_MPI
use mpi
#endif
use cam_history, only : addfld
use cam_logfile, only : iulog ! log file output unit
use infnan, only : nan, assignment(=)
use mo_chem_utls, only : get_spc_ndx, get_inv_ndx
use mo_jeuv, only : neuv ! number of extreme-UV rates
use musica_assert, only : assert_msg, die_msg
use musica_config, only : config_t
use musica_mpi, only : musica_mpi_rank, &
musica_mpi_pack_size, &
musica_mpi_pack, &
musica_mpi_unpack
use musica_string, only : string_t, to_char
use physics_buffer, only : physics_buffer_desc
use physics_buffer, only : pbuf_set_field
use ppgrid, only : pcols ! maximum number of columns
use shr_const_mod, only : pi => shr_const_pi
use solar_irrad_data, only : has_spectrum
use spmd_utils, only : main_task => masterprocid, &
is_main_task => masterproc, &
mpicom
use tuvx_grid, only : grid_t
use tuvx_grid_warehouse, only : grid_warehouse_t
use tuvx_profile_warehouse, only : profile_warehouse_t
use tuvx_radiator_warehouse, only : radiator_warehouse_t
use time_manager, only : is_first_step
character(len=*), intent(in) :: photon_file ! photon file used in extended-UV module setup
character(len=*), intent(in) :: electron_file ! electron file used in extended-UV module setup
real(r8), intent(in) :: max_solar_zenith_angle ! cutoff solar zenith angle for
! photo rate calculations [degrees]
type(physics_buffer_desc), pointer :: pbuf2d(:,:) ! Physics buffer
character(len=*), parameter :: my_name = "TUV-x wrapper initialization"
class(core_t), pointer :: core
character, allocatable :: buffer(:)
type(string_t) :: config_path
type(config_t) :: tuvx_config, cam_config, map_config
type(map_t) :: map
class(grid_t), pointer :: height
class(grid_t), pointer :: wavelength
class(grid_warehouse_t), pointer :: cam_grids
class(profile_warehouse_t), pointer :: cam_profiles
class(radiator_warehouse_t), pointer :: cam_radiators
integer :: pack_size, pos, i_core, i_err
logical :: disable_aerosols, disable_clouds
type(string_t) :: required_keys(1), optional_keys(2)
logical, save :: is_initialized = .false.
type(string_t), allocatable :: labels(:)
character(len=16) :: label
integer :: i
real(r8) :: nanval
if( .not. tuvx_active ) return
if( is_initialized ) return
is_initialized = .true.
nanval=nan
call pbuf_set_field( pbuf2d, ion_rates_pbuf_index, nanval )
if( is_first_step( ) ) then
call pbuf_set_field( pbuf2d, cpe_jo2_a_pbuf_index, 0.0_r8 )
call pbuf_set_field( pbuf2d, cpe_jo2_b_pbuf_index, 0.0_r8 )
call pbuf_set_field( pbuf2d, cpe_jo3_a_pbuf_index, 0.0_r8 )
call pbuf_set_field( pbuf2d, cpe_jo3_b_pbuf_index, 0.0_r8 )
end if
if( is_main_task ) write(iulog,*) "Beginning TUV-x Initialization"
config_path = trim(tuvx_config_path)
! ===============================
! CAM TUV-x configuration options
! ===============================
required_keys(1) = "aliasing"
optional_keys(1) = "disable aerosols"
optional_keys(2) = "disable clouds"
#ifndef HAVE_MPI
call assert_msg( 113937299, is_main_task, "Multiple tasks present without " &
//"MPI support enabled for TUV-x" )
#endif
! ===============================================================
! set the maximum solar zenith angle to calculate photo rates for
! ===============================================================
max_sza = max_solar_zenith_angle
if( max_sza <= 0.0_r8 .or. max_sza > 180.0_r8 ) then
call die_msg( 723815691, "TUV-x max solar zenith angle must be between 0 and 180 degress" )
end if
! =================================
! initialize the extended-UV module
! =================================
call initialize_euv( photon_file, electron_file, do_euv )
! ==========================================================================
! create the set of TUV-x grids and profiles that CAM will update at runtime
! ==========================================================================
cam_grids => get_cam_grids( wavelength_config_path )
cam_profiles => get_cam_profiles( cam_grids )
cam_radiators => get_cam_radiators( cam_grids )
! ==================================================================
! construct a core and a map between TUV-x and CAM photolysis arrays
! on the primary process and pack them onto an MPI buffer
! ==================================================================
if( is_main_task ) then
call tuvx_config%from_file( config_path%to_char( ) )
call tuvx_config%get( "__CAM options", cam_config, my_name )
call assert_msg( 973680295, &
cam_config%validate( required_keys, optional_keys ), &
"Bad configuration for CAM TUV-x options." )
call cam_config%get( "disable aerosols", disable_aerosols, my_name, &
default = .false. )
call cam_config%get( "disable clouds", disable_clouds, my_name, &
default = .false. )
call cam_config%get( "aliasing", map_config, my_name )
core => core_t( config_path, cam_grids, cam_profiles, cam_radiators )
call set_photo_rate_map( core, map_config, do_euv, do_jno, jno_index, map )
pack_size = core%pack_size( mpicom ) + &
map%pack_size( mpicom ) + &
musica_mpi_pack_size( do_jno, mpicom ) + &
musica_mpi_pack_size( jno_index, mpicom ) + &
musica_mpi_pack_size( disable_aerosols, mpicom ) + &
musica_mpi_pack_size( disable_clouds, mpicom )
allocate( buffer( pack_size ) )
pos = 0
call core%mpi_pack( buffer, pos, mpicom )
call map%mpi_pack( buffer, pos, mpicom )
call musica_mpi_pack( buffer, pos, do_jno, mpicom )
call musica_mpi_pack( buffer, pos, jno_index, mpicom )
call musica_mpi_pack( buffer, pos, disable_aerosols, mpicom )
call musica_mpi_pack( buffer, pos, disable_clouds, mpicom )
deallocate( core )
end if
#ifdef HAVE_MPI
! ====================================================
! broadcast the core and map data to all MPI processes
! ====================================================
call mpi_bcast( pack_size, 1, MPI_INTEGER, main_task, mpicom, i_err )
if( i_err /= MPI_SUCCESS ) then
write(iulog,*) "TUV-x MPI int bcast error"
call mpi_abort( mpicom, 1, i_err )
end if
if( .not. is_main_task ) allocate( buffer( pack_size ) )
call mpi_bcast( buffer, pack_size, MPI_CHARACTER, main_task, mpicom, i_err )
if( i_err /= MPI_SUCCESS ) then
write(iulog,*) "TUV-x MPI char array bcast error"
call mpi_abort( mpicom, 1, i_err )
end if
#endif
! ================================================================
! unpack the core and map for each OMP thread on every MPI process
! ================================================================
allocate( tuvx_ptrs( max_threads( ) ) )
do i_core = 1, size( tuvx_ptrs )
associate( tuvx => tuvx_ptrs( i_core ) )
allocate( tuvx%core_ )
pos = 0
call tuvx%core_%mpi_unpack( buffer, pos, mpicom )
call tuvx%photo_rate_map_%mpi_unpack( buffer, pos, mpicom )
call musica_mpi_unpack( buffer, pos, do_jno, mpicom )
call musica_mpi_unpack( buffer, pos, jno_index, mpicom )
call musica_mpi_unpack( buffer, pos, disable_aerosols, mpicom )
call musica_mpi_unpack( buffer, pos, disable_clouds, mpicom )
! ===================================================================
! Set up connections between CAM and TUV-x input data for each thread
! ===================================================================
call create_updaters( tuvx, cam_grids, cam_profiles, cam_radiators, &
disable_aerosols, disable_clouds )
! ============================================
! Save the number of photolysis rate constants
! ============================================
tuvx%n_photo_rates_ = tuvx%core_%number_of_photolysis_reactions( )
if( do_euv ) tuvx%n_euv_rates_ = neuv
if( do_jno ) tuvx%n_special_rates_ = tuvx%n_special_rates_ + 1
end associate
end do
deallocate( cam_grids )
deallocate( cam_profiles )
deallocate( cam_radiators )
! =============================================
! Get index info for CAM species concentrations
! =============================================
index_N2 = get_inv_ndx( 'N2' )
is_fixed_N2 = index_N2 > 0
if( .not. is_fixed_N2 ) index_N2 = get_spc_ndx( 'N2' )
index_O = get_inv_ndx( 'O' )
is_fixed_O = index_O > 0
if( .not. is_fixed_O ) index_O = get_spc_ndx( 'O' )
index_O2 = get_inv_ndx( 'O2' )
is_fixed_O2 = index_O2 > 0
if( .not. is_fixed_O2 ) index_O2 = get_spc_ndx( 'O2' )
index_O3 = get_inv_ndx( 'O3' )
is_fixed_O3 = index_O3 > 0
if( .not. is_fixed_O3 ) index_O3 = get_spc_ndx( 'O3' )
index_NO = get_inv_ndx( 'NO' )
is_fixed_NO = index_NO > 0
if( .not. is_fixed_NO ) index_NO = get_spc_ndx( 'NO' )
! ====================================================
! make sure extraterrestrial flux values are available
! ====================================================
call assert_msg( 170693514, has_spectrum, &
"Solar irradiance spectrum needed for TUV-x" )
! ============================================
! set up diagnostic output of photolysis rates
! ============================================
call initialize_diagnostics( tuvx_ptrs( 1 ) )
! ===============================
! set up map to CAM heating rates
! ===============================
labels = tuvx_ptrs(1)%core_%heating_rate_labels( )
do i = 1, size( labels )
label = trim( labels( i )%to_char( ) )
select case( label )
case( 'jo2_a' )
index_cpe_jo2_a = i
number_of_heating_rates = number_of_heating_rates + 1
call addfld('CPE_jO2a',(/ 'lev' /), 'A', 'joules sec-1', &
trim(label)//' chemical potential energy')
case( 'jo2_b' )
index_cpe_jo2_b = i
number_of_heating_rates = number_of_heating_rates + 1
call addfld('CPE_jO2b',(/ 'lev' /), 'A', 'joules sec-1', &
trim(label)//' chemical potential energy')
case( 'jo3_a' )
index_cpe_jo3_a = i
number_of_heating_rates = number_of_heating_rates + 1
call addfld('CPE_jO3a',(/ 'lev' /), 'A', 'joules sec-1', &
trim(label)//' chemical potential energy')
case( 'jo3_b' )
index_cpe_jo3_b = i
number_of_heating_rates = number_of_heating_rates + 1
call addfld('CPE_jO3b',(/ 'lev' /), 'A', 'joules sec-1', &
trim(label)//' chemical potential energy')
end select
end do
call assert_msg( 398372957, &
number_of_heating_rates == size( labels ), &
"TUV-x heating rate mismatch. Expected "// &
trim( to_char( size( labels ) )// &
" rates, but only matched "// &
trim( to_char( number_of_heating_rates ) )//"." ) )
if( is_main_task ) call log_initialization( labels )
end subroutine tuvx_init
!================================================================================================
!-----------------------------------------------------------------------
! Updates TUV-x profiles that depend on time but not space
!-----------------------------------------------------------------------
subroutine tuvx_timestep_init( )
integer :: i_thread
if( .not. tuvx_active ) return
do i_thread = 1, size( tuvx_ptrs )
associate( tuvx => tuvx_ptrs( i_thread ) )
call set_et_flux( tuvx )
end associate
end do
end subroutine tuvx_timestep_init
!================================================================================================
!-----------------------------------------------------------------------
! Calculates and returns photolysis rate constants
!-----------------------------------------------------------------------
subroutine tuvx_get_photo_rates( state, pbuf, ncol, lchnk, height_mid, &
height_int, temperature_mid, surface_temperature, fixed_species_conc, &
species_vmr, exo_column_conc, surface_albedo, solar_zenith_angle, &
earth_sun_distance, pressure_delta, cloud_fraction, liquid_water_content, &
photolysis_rates )
use cam_history, only : outfld
use cam_logfile, only : iulog ! log info output unit
use chem_mods, only : phtcnt, & ! number of photolysis reactions
gas_pcnst, & ! number of non-fixed species
nfs, & ! number of fixed species
nabscol ! number of absorbing species (radiators)
use physics_types, only : physics_state
use physics_buffer, only : physics_buffer_desc
use physics_buffer, only : pbuf_get_field
use ppgrid, only : pcols ! maximum number of columns
use shr_const_mod, only : pi => shr_const_pi
use spmd_utils, only : main_task => masterprocid, &
is_main_task => masterproc, &
mpicom
type(physics_state), target, intent(in) :: state
type(physics_buffer_desc), pointer, intent(inout) :: pbuf(:)
integer, intent(in) :: ncol ! number of active columns on this thread
integer, intent(in) :: lchnk ! identifier for this thread
real(r8), intent(in) :: height_mid(ncol,pver) ! height at mid-points (km)
real(r8), intent(in) :: height_int(ncol,pver+1) ! height at interfaces (km)
real(r8), intent(in) :: temperature_mid(pcols,pver) ! midpoint temperature (K)
real(r8), intent(in) :: surface_temperature(pcols) ! surface temperature (K)
real(r8), intent(in) :: fixed_species_conc(ncol,pver,max(1,nfs)) ! fixed species densities
! (molecule cm-3)
real(r8), intent(in) :: species_vmr(ncol,pver,max(1,gas_pcnst)) ! species volume mixing
! ratios (mol mol-1)
real(r8), intent(in) :: exo_column_conc(ncol,0:pver,max(1,nabscol)) ! layer column densities
! (molecule cm-2)
real(r8), intent(in) :: surface_albedo(pcols) ! surface albedo (unitless)
real(r8), intent(in) :: solar_zenith_angle(ncol) ! solar zenith angle (radians)
real(r8), intent(in) :: earth_sun_distance ! Earth-Sun distance (AU)
real(r8), intent(in) :: pressure_delta(pcols,pver) ! pressure delta about midpoints (Pa)
real(r8), intent(in) :: cloud_fraction(ncol,pver) ! cloud fraction (unitless)
real(r8), intent(in) :: liquid_water_content(ncol,pver) ! liquid water content (kg/kg)
real(r8), intent(inout) :: photolysis_rates(ncol,pver,phtcnt) ! photolysis rate
! constants (1/s)
integer :: i_col ! column index
integer :: i_level ! vertical level index
real(r8) :: sza ! solar zenith angle [degrees]
real(r8) :: cpe_rates(ncol,pverp+1,number_of_heating_rates) ! heating rates from TUV-x
real(r8), pointer :: cpe_jo2_a(:,:) ! heating rate for jo2_a in physics buffer
real(r8), pointer :: cpe_jo2_b(:,:) ! heating rate for jo2_b in physics buffer
real(r8), pointer :: cpe_jo3_a(:,:) ! heating rate for jo3_a in physics buffer
real(r8), pointer :: cpe_jo3_b(:,:) ! heating rate for jo3_b in physics buffer
! working arrays
real(r8), allocatable :: photo_rates(:,:,:) ! calculated photo rate constants (column, level, reaction) [s-1]
real(r8), allocatable :: optical_depth(:,:,:) ! aerosol optical depth (column, level, wavelength) [unitless]
real(r8), allocatable :: single_scattering_albedo(:,:,:) ! aerosol single scattering albedo (column, level, wavelength) [unitless]
real(r8), allocatable :: asymmetry_factor(:,:,:) ! aerosol asymmetry factor (column, level, wavelength) [unitless]
if( .not. tuvx_active ) return
call pbuf_get_field(pbuf, cpe_jo2_a_pbuf_index, cpe_jo2_a)
call pbuf_get_field(pbuf, cpe_jo2_b_pbuf_index, cpe_jo2_b)
call pbuf_get_field(pbuf, cpe_jo3_a_pbuf_index, cpe_jo3_a)
call pbuf_get_field(pbuf, cpe_jo3_b_pbuf_index, cpe_jo3_b)
cpe_rates(:,:,:) = 0.0_r8
cpe_jo2_a(:,:) = 0.0_r8
cpe_jo2_b(:,:) = 0.0_r8
cpe_jo3_a(:,:) = 0.0_r8
cpe_jo3_b(:,:) = 0.0_r8
associate( tuvx => tuvx_ptrs( thread_id( ) ) )
allocate( photo_rates( pcols, pver+2, tuvx%n_photo_rates_ + tuvx%n_euv_rates_ &
+ tuvx%n_special_rates_ ) )
allocate( optical_depth( pcols, pver+1, tuvx%n_wavelength_bins_ ) )
allocate( single_scattering_albedo( pcols, pver+1, tuvx%n_wavelength_bins_ ) )
allocate( asymmetry_factor( pcols, pver+1, tuvx%n_wavelength_bins_ ) )
photo_rates(:,:,:) = 0.0_r8
! ==============================================
! set aerosol optical properties for all columns
! ==============================================
call get_aerosol_optical_properties( tuvx, state, pbuf, optical_depth, &
single_scattering_albedo, asymmetry_factor )
do i_col = 1, ncol
! ===================================
! skip columns in near total darkness
! ===================================
sza = solar_zenith_angle(i_col) * 180.0_r8 / pi
if( sza < 0.0_r8 .or. sza > max_sza ) cycle
! ===================
! update grid heights
! ===================
call set_heights( tuvx, i_col, ncol, height_mid, height_int )
! =======================================
! set conditions for this column in TUV-x
! =======================================
call set_temperatures( tuvx, i_col, temperature_mid, surface_temperature )
call set_surface_albedo( tuvx, i_col, surface_albedo )
call set_radiator_profiles( tuvx, i_col, ncol, fixed_species_conc, &
species_vmr, exo_column_conc, &
pressure_delta(1:ncol,:), cloud_fraction, &
liquid_water_content, optical_depth, &
single_scattering_albedo, asymmetry_factor)
! ===================================================
! Calculate photolysis rate constants for this column
! ===================================================
call tuvx%core_%run( solar_zenith_angle = sza, &
earth_sun_distance = earth_sun_distance, &
photolysis_rate_constants = &
photo_rates(i_col,:,1:tuvx%n_photo_rates_), &
heating_rates = cpe_rates(i_col,:,:) )
! ==============================
! Calculate the extreme-UV rates
! ==============================
if( do_euv ) then
associate( euv_begin => tuvx%n_photo_rates_ + 1, &
euv_end => tuvx%n_photo_rates_ + tuvx%n_euv_rates_ )
call calculate_euv_rates( sza, &
fixed_species_conc(i_col,:,:), &
species_vmr(i_col,:,:), &
height_mid(i_col,:), &
height_int(i_col,:), &
photo_rates(i_col,2:pver+1,euv_begin:euv_end) )
end associate
end if
! =============================
! Calculate special photo rates
! =============================
if( do_jno ) then
call calculate_jno( sza, &
tuvx%et_flux_ms93_, &
fixed_species_conc(i_col,:,:), &
species_vmr(i_col,:,:), &
height_int(i_col,:), &
photo_rates(i_col,2:pver+1,jno_index) )
end if
end do
! =====================
! Filter negative rates
! =====================
photo_rates(:,:,:) = max( 0.0_r8, photo_rates(:,:,:) )
! ============================================
! Return the photolysis rates on the CAM grids
! ============================================
do i_col = 1, ncol
do i_level = 1, pver
call tuvx%photo_rate_map_%apply( photo_rates(i_col,pver-i_level+2,:), &
photolysis_rates(i_col,i_level,:) )
end do
end do
call output_diagnostics( tuvx, ncol, lchnk, photo_rates )
end associate
if (index_cpe_jo2_a>0) then
do i_level = 1, pver
cpe_jo2_a(:ncol,i_level) = &
0.5_r8 * ( cpe_rates(:ncol,pver-i_level+2,index_cpe_jo2_a) &
+ cpe_rates(:ncol,pver-i_level+1,index_cpe_jo2_a) )
end do
call outfld('CPE_jO2a', cpe_jo2_a(:ncol,:), ncol, lchnk )
end if
if (index_cpe_jo2_b>0) then
do i_level = 1, pver
cpe_jo2_b(:ncol,i_level) = &
0.5_r8 * ( cpe_rates(:ncol,pver-i_level+2,index_cpe_jo2_b) &
+ cpe_rates(:ncol,pver-i_level+1,index_cpe_jo2_b) )
end do
call outfld('CPE_jO2b', cpe_jo2_b(:ncol,:), ncol, lchnk )
end if
if (index_cpe_jo3_a>0) then
do i_level = 1, pver
cpe_jo3_a(:ncol,i_level) = &
0.5_r8 * ( cpe_rates(:ncol,pver-i_level+2,index_cpe_jo3_a) &
+ cpe_rates(:ncol,pver-i_level+1,index_cpe_jo3_a) )
end do
call outfld('CPE_jO3a', cpe_jo3_a(:ncol,:), ncol, lchnk )
end if
if (index_cpe_jo3_b>0) then
do i_level = 1, pver
cpe_jo3_b(:ncol,i_level) = &
0.5_r8 * ( cpe_rates(:ncol,pver-i_level+2,index_cpe_jo3_b) &
+ cpe_rates(:ncol,pver-i_level+1,index_cpe_jo3_b) )
end do
call outfld('CPE_jO3b', cpe_jo3_b(:ncol,:), ncol, lchnk )
end if
end subroutine tuvx_get_photo_rates
!================================================================================================
!-----------------------------------------------------------------------
! Cleans up memory associated with TUV-x calculators
!-----------------------------------------------------------------------
subroutine tuvx_finalize( )
integer :: i_core
if( allocated( tuvx_ptrs ) ) then
do i_core = 1, size( tuvx_ptrs )
associate( tuvx => tuvx_ptrs( i_core ) )
if( associated( tuvx%core_ ) ) deallocate( tuvx%core_ )
end associate
end do
end if
end subroutine tuvx_finalize
!================================================================================================
!================================================================================================
!
! Support functions
!
!================================================================================================
!================================================================================================
!-----------------------------------------------------------------------
! Returns the id of the current OpenMP thread, or 1 if not
! using OpenMP (1 <= id <= max_threads())
!-----------------------------------------------------------------------
integer function thread_id( )
#ifdef _OPENMP
use omp_lib, only : omp_get_thread_num
#endif
#ifdef _OPENMP
thread_id = 1 + omp_get_thread_num( )
#else
thread_id = 1
#endif
end function thread_id
!================================================================================================
!-----------------------------------------------------------------------
! Returns the number of threads available for calculations at
! runtime
!-----------------------------------------------------------------------
integer function max_threads( )
#ifdef _OPENMP
use omp_lib, only : omp_get_max_threads
#endif
#ifdef _OPENMP
max_threads = omp_get_max_threads( )
#else
max_threads = 1
#endif
end function max_threads
!================================================================================================
!-----------------------------------------------------------------------
! Prints initialization conditions to the log file
!-----------------------------------------------------------------------
subroutine log_initialization( heating_rate_labels )
use cam_logfile, only : iulog ! log info output unit
use musica_string, only : to_char
use spmd_utils, only : main_task => masterprocid, &
is_main_task => masterproc
type(string_t), intent(in) :: heating_rate_labels(:) ! heating rate labels
integer :: i
if( is_main_task ) then
write(iulog,*) "Initialized TUV-x"
#ifdef HAVE_MPI
write(iulog,*) " - with MPI support on task "//trim( to_char( main_task ) )
#else
write(iulog,*) " - without MPI support"
#endif
#ifdef _OPENMP
write(iulog,*) " - with OpenMP support for "// &
trim( to_char( max_threads( ) ) )//" threads, on thread " &
//trim( to_char( thread_id( ) ) )
#else
write(iulog,*) " - without OpenMP support"
#endif
write(iulog,*) " - with configuration file: '"//trim( tuvx_config_path )//"'"
if( do_aerosol ) then
write(iulog,*) " - with on-line aerosols"
else
write(iulog,*) " - without on-line aerosols"
end if
if( do_clouds ) then
write(iulog,*) " - with on-line clouds"
else
write(iulog,*) " - without on-line clouds"
end if
if( index_N2 > 0 ) write(iulog,*) " - including N2"
if( index_O > 0 ) write(iulog,*) " - including O"
if( index_O2 > 0 ) write(iulog,*) " - including O2"
if( index_O3 > 0 ) write(iulog,*) " - including O3"
if( index_NO > 0 ) write(iulog,*) " - including NO"
if( do_euv ) write(iulog,*) " - doing Extreme-UV calculations"
if( do_jno ) write(iulog,*) " - including special jno rate calculation"
write(iulog,*) " - max solar zenith angle [degrees]:", max_sza
if( size( heating_rate_labels ) > 0 ) then
write(iulog,*) " - with heating rates:"
do i = 1, size( heating_rate_labels )
write(iulog,*) " - "//trim( heating_rate_labels( i )%to_char( ) )
end do
end if
end if
end subroutine log_initialization
!================================================================================================
!-----------------------------------------------------------------------
! initializes the external extreme-UV module
!-----------------------------------------------------------------------
subroutine initialize_euv( photon_file, electron_file, do_euv )
use chem_mods, only : phtcnt ! number of CAM-Chem photolysis reactions
use mo_jeuv, only : jeuv_init ! extreme-UV initialization
character(len=*), intent(in) :: photon_file ! photon file used in extended-UV module
! setup
character(len=*), intent(in) :: electron_file ! electron file used in extended-UV
! module setup
logical, intent(out) :: do_euv ! indicates whether extreme-UV
! calculations are needed
integer, allocatable :: euv_index_map(:)
allocate( euv_index_map( phtcnt ) )
euv_index_map(:) = 0
call jeuv_init( photon_file, electron_file, euv_index_map )
do_euv = any( euv_index_map(:) > 0 )
end subroutine initialize_euv
!================================================================================================
!-----------------------------------------------------------------------
! Registers fields for diagnostic output
!-----------------------------------------------------------------------
subroutine initialize_diagnostics( this )
use cam_history, only : addfld
use musica_assert, only : assert
use musica_string, only : string_t
type(tuvx_ptr), intent(in) :: this
type(string_t), allocatable :: labels(:), all_labels(:)
integer :: i_label
if( .not. enable_diagnostics ) then
allocate( diagnostics( 0 ) )
return
end if
! ==========================================================
! add output for specific photolysis reaction rate constants
! ==========================================================
labels = this%core_%photolysis_reaction_labels( )
allocate( all_labels( size( labels ) + this%n_special_rates_ ) )
all_labels( 1 : size( labels ) ) = labels(:)
i_label = size( labels ) + 1
if( do_jno ) then
all_labels( i_label ) = "jno"
i_label = i_label + 1
end if
call assert( 522515214, i_label == size( all_labels ) + 1 )
allocate( diagnostics( size( all_labels ) ) )
do i_label = 1, size( all_labels )
diagnostics( i_label )%name_ = trim( all_labels( i_label )%to_char( ) )
diagnostics( i_label )%index_ = i_label
call addfld( "tuvx_"//diagnostics( i_label )%name_, (/ 'lev' /), 'A', 'sec-1', &
'photolysis rate constant' )
end do
end subroutine initialize_diagnostics
!================================================================================================
!-----------------------------------------------------------------------
! Sets up a map between the TUV-x and CAM photolysis rate arrays
!-----------------------------------------------------------------------
subroutine set_photo_rate_map( core, config, do_euv, do_jno, jno_index, map )
use cam_logfile, only : iulog ! log info output unit
use chem_mods, only : phtcnt, & ! number of photolysis reactions
rxt_tag_lst ! labels for all chemical reactions
! NOTE photolysis reactions are
! expected to appear first
use mo_jeuv, only : neuv ! number of extreme-UV rates
use musica_config, only : config_t
use musica_string, only : string_t
type(core_t), intent(in) :: core ! TUV-x core
type(config_t), intent(inout) :: config ! CAM<->TUV-x map configuration
logical, intent(in) :: do_euv ! indicates whether to include
! extreme-UV rates in the mapping
logical, intent(out) :: do_jno ! indicates whether jno should be
! calculated
integer, intent(out) :: jno_index ! index for jno in source photo
! rate array
type(map_t), intent(out) :: map
integer :: i_label, i_start, i_end
type(string_t) :: str_label
type(string_t), allocatable :: tuvx_labels(:), euv_labels(:), special_labels(:), &
all_labels(:), cam_labels(:)
! ==================
! MOZART photo rates
! ==================
allocate( cam_labels( phtcnt ) )
do i_label = 1, phtcnt
cam_labels( i_label ) = trim( rxt_tag_lst( i_label ) )
end do
! =================
! TUV-x photo rates
! =================
tuvx_labels = core%photolysis_reaction_labels( )
! ======================
! Extreme-UV photo rates
! ======================
if( do_euv ) then
allocate( euv_labels( neuv ) )
do i_label = 1, neuv
str_label = i_label
euv_labels( i_label ) = "jeuv_"//str_label
end do
else
allocate( euv_labels(0) )
end if
! ===============================
! Special photo rate calculations
! ===============================
do_jno = .false.
jno_index = 0
do i_label = 1, size( cam_labels )
if( cam_labels( i_label ) == "jno" ) then
do_jno = .true.
exit
end if
end do
if( do_jno ) then
allocate( special_labels(1) )
special_labels(1) = "jno"
else
allocate( special_labels(0) )
end if
! ==========================
! Combine photo rate sources
! ==========================
allocate( all_labels( size( tuvx_labels ) + size( euv_labels ) + &
size( special_labels ) ) )
i_end = 0
if( size( tuvx_labels ) > 0 ) then
i_start = i_end + 1
i_end = i_start + size( tuvx_labels ) - 1
all_labels( i_start : i_end ) = tuvx_labels(:)
end if
if( size( euv_labels ) > 0 ) then
i_start = i_end + 1
i_end = i_start + size( euv_labels ) - 1
all_labels( i_start : i_end ) = euv_labels(:)
end if
if( size( special_labels ) > 0 ) then
i_start = i_end + 1