From 7313bea5dd8f2580491362b0ed365f69eaa3b2c8 Mon Sep 17 00:00:00 2001 From: Courtney Peverley Date: Fri, 1 Nov 2024 11:21:13 -0600 Subject: [PATCH 1/4] add initialize_constituents scheme --- .../test_schemes/initialize_constituents.F90 | 140 ++++++++++++++++++ .../test_schemes/initialize_constituents.meta | 44 ++++++ 2 files changed, 184 insertions(+) create mode 100644 schemes/test_schemes/initialize_constituents.F90 create mode 100644 schemes/test_schemes/initialize_constituents.meta diff --git a/schemes/test_schemes/initialize_constituents.F90 b/schemes/test_schemes/initialize_constituents.F90 new file mode 100644 index 00000000..eeee6091 --- /dev/null +++ b/schemes/test_schemes/initialize_constituents.F90 @@ -0,0 +1,140 @@ +module initialize_constituents + +implicit none +private + +public :: initialize_constituents_register +public :: initialize_constituents_run + +contains + +!> \section arg_table_initialize_constituents_register Argument Table +!! \htmlinclude initialize_constituents_register.html +subroutine initialize_constituents_register(constituents, errmsg, errcode) + use ccpp_constituent_prop_mod, only: ccpp_constituent_properties_t + use cam_initfiles, only: initial_file_get_id + use pio, only: pio_inquire, file_desc_t, pio_inq_varname + use ccpp_kinds, only: kind_phys + ! Dummy variables + type(ccpp_constituent_properties_t), allocatable, intent(out) :: constituents(:) + character(len=512), intent(out) :: errmsg + integer, intent(out) :: errcode + ! Local variables + type(file_desc_t), pointer :: ncdata + integer :: num_variables + integer :: ierr + integer :: var_index + integer :: constituent_index + integer :: known_const_index + integer :: found_const_count + logical :: known_constituent + character(len=256) :: variable_name + character(len=256), allocatable :: constituent_names(:) + character(len=65), parameter :: const_std_names(6) = & + (/'water_vapor_mixing_ratio_wrt_moist_air_and_condensed_water ', & + 'cloud_liquid_water_mixing_ratio_wrt_moist_air_and_condensed_water', & + 'rain_mixing_ratio_wrt_moist_air_and_condensed_water ', & + 'cloud_ice_mixing_ratio_wrt_moist_air_and_condensed_water ', & + 'snow_mixing_ratio_wrt_moist_air_and_condensed_water ', & + 'graupel_water_mixing_ratio_wrt_moist_air_and_condensed_water '/) + + character(len=11), parameter :: const_file_names(6) = (/'cnst_Q ', & + 'cnst_CLDLIQ', & + 'cnst_RAINQM', & + 'cnst_CLDICE', & + 'cnst_SNOWQM', & + 'cnst_GRAUQM'/) + character(len=11), parameter :: water_species_number_concentrations(5) = & + (/'cnst_NUMLIQ', & + 'cnst_NUMRAI', & + 'cnst_NUMICE', & + 'cnst_NUMSNO', & + 'cnst_NUMGRA'/) + + errcode = 0 + errmsg = '' + constituent_index = 0 + found_const_count = 0 + + ncdata => initial_file_get_id() + ! See how many variables are present on the file + ierr = pio_inquire(ncdata, nVariables=num_variables) + allocate(constituent_names(num_variables), stat=ierr) + if (ierr /= 0) then + errcode = 1 + errmsg = 'Failed to allocate "constituent_names"' + return + end if + + ! Loop over all variables in the file and add each constituent to the + ! dynamic constituent array + do var_index = 1, num_variables + ierr = pio_inq_varname(ncdata, var_index, variable_name) + known_constituent = .false. + if (index(variable_name, 'cnst_') > 0) then + constituent_index = constituent_index + 1 + ! Replace with standard name if known, to avoid duplicates + if (found_const_count < size(const_std_names)) then + do known_const_index = 1, size(const_file_names) + if (trim(const_file_names(known_const_index)) == trim(variable_name)) then + constituent_names(constituent_index) = const_std_names(known_const_index) + found_const_count = found_const_count + 1 + known_constituent = .true. + exit + end if + end do + end if + if (.not. known_constituent) then + constituent_names(constituent_index) = variable_name + end if + end if + end do + + allocate(constituents(constituent_index), stat=ierr) + if (ierr /= 0) then + errcode = 1 + errmsg = 'Failed to allocate "constituents"' + return + end if + + do var_index = 1, size(constituents) + if (any(water_species_number_concentrations == trim(constituent_names(var_index)))) then + call constituents(var_index)%instantiate( & + std_name = constituent_names(var_index), & + long_name = constituent_names(var_index), & + units = 'kg kg-1', & + vertical_dim = 'vertical_layer_dimension', & + min_value = 0.0_kind_phys, & + advected = .true., & + water_species = .true., & + mixing_ratio_type = 'wet', & + errcode = errcode, & + errmsg = errmsg) + else + call constituents(var_index)%instantiate( & + std_name = constituent_names(var_index), & + long_name = constituent_names(var_index), & + units = 'kg kg-1', & + vertical_dim = 'vertical_layer_dimension', & + min_value = 0.0_kind_phys, & + advected = .true., & + errcode = errcode, & + errmsg = errmsg) + end if + if (errcode /= 0) then + exit + end if + end do + + end subroutine initialize_constituents_register + +!> \section arg_table_initialize_constituents_run Argument Table +!! \htmlinclude initialize_constituents_run.html + subroutine initialize_constituents_run(errmsg, errcode) + character(len=512), intent(out) :: errmsg + integer, intent(out) :: errcode + + errcode = 0 + errmsg = '' + end subroutine initialize_constituents_run +end module initialize_constituents diff --git a/schemes/test_schemes/initialize_constituents.meta b/schemes/test_schemes/initialize_constituents.meta new file mode 100644 index 00000000..0618c16d --- /dev/null +++ b/schemes/test_schemes/initialize_constituents.meta @@ -0,0 +1,44 @@ +[ccpp-table-properties] + name = initialize_constituents + type = scheme +[ccpp-arg-table] + name = initialize_constituents_register + type = scheme +[ constituents ] + standard_name = dynamic_constituents_for_initialize_constituents + units = none + dimensions = (:) + allocatable = True + type = ccpp_constituent_properties_t + intent = out +[ errmsg ] + standard_name = ccpp_error_message + long_name = Error message for error handling in CCPP + units = none + type = character | kind = len=512 + dimensions = () + intent = out +[ errcode ] + standard_name = ccpp_error_code + long_name = Error flag for error handling in CCPP + units = 1 + type = integer + dimensions = () + intent = out +[ccpp-arg-table] + name = initialize_constituents_run + type = scheme +[ errmsg ] + standard_name = ccpp_error_message + long_name = Error message for error handling in CCPP + units = none + type = character | kind = len=512 + dimensions = () + intent = out +[ errcode ] + standard_name = ccpp_error_code + long_name = Error flag for error handling in CCPP + units = 1 + type = integer + dimensions = () + intent = out From ac5f740bcf0cd152bff1afae110ca4db05331bdb Mon Sep 17 00:00:00 2001 From: Courtney Peverley Date: Wed, 6 Nov 2024 17:17:54 -0700 Subject: [PATCH 2/4] move test schemes to test directory --- {schemes => test}/test_schemes/initialize_constituents.F90 | 0 {schemes => test}/test_schemes/initialize_constituents.meta | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {schemes => test}/test_schemes/initialize_constituents.F90 (100%) rename {schemes => test}/test_schemes/initialize_constituents.meta (100%) diff --git a/schemes/test_schemes/initialize_constituents.F90 b/test/test_schemes/initialize_constituents.F90 similarity index 100% rename from schemes/test_schemes/initialize_constituents.F90 rename to test/test_schemes/initialize_constituents.F90 diff --git a/schemes/test_schemes/initialize_constituents.meta b/test/test_schemes/initialize_constituents.meta similarity index 100% rename from schemes/test_schemes/initialize_constituents.meta rename to test/test_schemes/initialize_constituents.meta From 29c7663a68d9b66bfe7926a56a595dbcbf2c385c Mon Sep 17 00:00:00 2001 From: peverwhee Date: Fri, 22 Nov 2024 10:41:06 -0700 Subject: [PATCH 3/4] add water species conditional --- test/test_schemes/initialize_constituents.F90 | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/test/test_schemes/initialize_constituents.F90 b/test/test_schemes/initialize_constituents.F90 index eeee6091..e838b704 100644 --- a/test/test_schemes/initialize_constituents.F90 +++ b/test/test_schemes/initialize_constituents.F90 @@ -29,8 +29,9 @@ subroutine initialize_constituents_register(constituents, errmsg, errcode) integer :: found_const_count logical :: known_constituent character(len=256) :: variable_name + character(len=512) :: alloc_err_msg character(len=256), allocatable :: constituent_names(:) - character(len=65), parameter :: const_std_names(6) = & + character(len=65), parameter :: water_species_std_names(6) = & (/'water_vapor_mixing_ratio_wrt_moist_air_and_condensed_water ', & 'cloud_liquid_water_mixing_ratio_wrt_moist_air_and_condensed_water', & 'rain_mixing_ratio_wrt_moist_air_and_condensed_water ', & @@ -59,10 +60,10 @@ subroutine initialize_constituents_register(constituents, errmsg, errcode) ncdata => initial_file_get_id() ! See how many variables are present on the file ierr = pio_inquire(ncdata, nVariables=num_variables) - allocate(constituent_names(num_variables), stat=ierr) + allocate(constituent_names(num_variables), stat=ierr, errmsg=alloc_err_msg) if (ierr /= 0) then errcode = 1 - errmsg = 'Failed to allocate "constituent_names"' + errmsg = alloc_err_msg return end if @@ -74,10 +75,10 @@ subroutine initialize_constituents_register(constituents, errmsg, errcode) if (index(variable_name, 'cnst_') > 0) then constituent_index = constituent_index + 1 ! Replace with standard name if known, to avoid duplicates - if (found_const_count < size(const_std_names)) then + if (found_const_count < size(water_species_std_names)) then do known_const_index = 1, size(const_file_names) if (trim(const_file_names(known_const_index)) == trim(variable_name)) then - constituent_names(constituent_index) = const_std_names(known_const_index) + constituent_names(constituent_index) = water_species_std_names(known_const_index) found_const_count = found_const_count + 1 known_constituent = .true. exit @@ -90,35 +91,47 @@ subroutine initialize_constituents_register(constituents, errmsg, errcode) end if end do - allocate(constituents(constituent_index), stat=ierr) + allocate(constituents(constituent_index), stat=ierr, errmsg=alloc_err_msg) if (ierr /= 0) then errcode = 1 - errmsg = 'Failed to allocate "constituents"' + errmsg = alloc_err_msg return end if do var_index = 1, size(constituents) if (any(water_species_number_concentrations == trim(constituent_names(var_index)))) then - call constituents(var_index)%instantiate( & - std_name = constituent_names(var_index), & - long_name = constituent_names(var_index), & - units = 'kg kg-1', & + call constituents(var_index)%instantiate( & + std_name = constituent_names(var_index), & + long_name = constituent_names(var_index), & + units = 'kg-1', & vertical_dim = 'vertical_layer_dimension', & - min_value = 0.0_kind_phys, & - advected = .true., & - water_species = .true., & - mixing_ratio_type = 'wet', & - errcode = errcode, & + min_value = 0.0_kind_phys, & + advected = .true., & + water_species = .true., & + mixing_ratio_type = 'wet', & + errcode = errcode, & + errmsg = errmsg) + else if (any(water_species_std_names == trim(constituent_names(var_index)))) then + call constituents(var_index)%instantiate( & + std_name = constituent_names(var_index), & + long_name = constituent_names(var_index), & + units = 'kg kg-1', & + vertical_dim = 'vertical_layer_dimension', & + min_value = 0.0_kind_phys, & + advected = .true., & + water_species = .true., & + mixing_ratio_type = 'wet', & + errcode = errcode, & errmsg = errmsg) else - call constituents(var_index)%instantiate( & - std_name = constituent_names(var_index), & - long_name = constituent_names(var_index), & - units = 'kg kg-1', & + call constituents(var_index)%instantiate( & + std_name = constituent_names(var_index), & + long_name = constituent_names(var_index), & + units = 'kg kg-1', & vertical_dim = 'vertical_layer_dimension', & - min_value = 0.0_kind_phys, & - advected = .true., & - errcode = errcode, & + min_value = 0.0_kind_phys, & + advected = .true., & + errcode = errcode, & errmsg = errmsg) end if if (errcode /= 0) then From 39b73888640d9437e9118f5c737268fb7181effc Mon Sep 17 00:00:00 2001 From: peverwhee Date: Fri, 22 Nov 2024 10:58:23 -0700 Subject: [PATCH 4/4] fix exner units in state_converters --- schemes/utilities/state_converters.meta | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schemes/utilities/state_converters.meta b/schemes/utilities/state_converters.meta index c0e57b15..170cc9e3 100644 --- a/schemes/utilities/state_converters.meta +++ b/schemes/utilities/state_converters.meta @@ -223,7 +223,7 @@ [ exner ] standard_name = dimensionless_exner_function type = real | kind = kind_phys - units = count + units = 1 dimensions = (horizontal_loop_extent, vertical_layer_dimension) intent = out [ errmsg ]