Skip to content

Commit

Permalink
tendencies working
Browse files Browse the repository at this point in the history
  • Loading branch information
peverwhee committed Aug 12, 2024
1 parent 58308e4 commit 922a4dd
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 43 deletions.
23 changes: 21 additions & 2 deletions scripts/constituents.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ def find_variable(self, standard_name=None, source_var=None,
var = source_var.clone({'dimensions' : newdims}, remove_intent=True,
source_type=self.__constituent_type)
self.add_variable(var, self.__run_env)
# Also add a nearly-identical tendency_of_{standard_name} variable
new_stdname = f"tendency_of_{standard_name}"
new_units = f"{source_var.get_prop_value('units')} s-1"
new_lname = f"{source_var.get_prop_value('local_name')}_tend"
var_tend = source_var.clone({'dimensions' : newdims, 'standard_name' : new_stdname,
'units': new_units, 'local_name': new_lname},
remove_intent=True, source_type=self.__constituent_type)
self.add_variable(var_tend, self.__run_env)
return var

@staticmethod
Expand Down Expand Up @@ -276,17 +284,28 @@ def write_constituent_routines(self, outfile, indent, suite_name, err_vars):
for evar in err_vars:
evar.write_def(outfile, indent+1, self, dummy=True)
# end for
# Figure out how many constituents variables we have
const_num = 0
for std_name, var in self.items():
if 'tendency_of' in std_name or 'index_of' in std_name:
continue
# end if
const_num += 1
# end for
if self:
outfile.write("! Local variables", indent+1)
outfile.write("integer :: index", indent+1)
stmt = f"allocate({self.constituent_prop_array_name()}({len(self)}))"
stmt = f"allocate({self.constituent_prop_array_name()}({const_num}))"
outfile.write(stmt, indent+1)
outfile.write("index = 0", indent+1)
# end if
for evar in err_vars:
self.__init_err_var(evar, outfile, indent+1)
# end for
for std_name, var in self.items():
if 'tendency_of' in std_name or 'index_of' in std_name:
continue
# end if
outfile.write("index = index + 1", indent+1)
long_name = var.get_prop_value('long_name')
units = var.get_prop_value('units')
Expand Down Expand Up @@ -336,7 +355,7 @@ def write_constituent_routines(self, outfile, indent, suite_name, err_vars):
outfile.write(stmt, indent+1)
outfile.write(f"call {self.constituent_prop_init_consts()}({local_call})", indent+2)
outfile.write("end if", indent+1)
outfile.write(f"{fname} = {len(self)}", indent+1)
outfile.write(f"{fname} = {const_num}", indent+1)
outfile.write(f"end function {fname}", indent)
outfile.write(f"\n! {border}\n", 1)
# Return the name of a constituent given an index
Expand Down
43 changes: 30 additions & 13 deletions scripts/host_cap.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def add_constituent_vars(cap, host_model, suite_list, run_env):
vert_layer_dim = "vertical_layer_dimension"
vert_interface_dim = "vertical_interface_dimension"
array_layer = "vars_layer"
tend_layer = "vars_layer_tend"
# Table preamble (leave off ccpp-table-properties header)
ddt_mdata = [
#"[ccpp-table-properties]",
Expand Down Expand Up @@ -340,19 +341,35 @@ def add_constituent_vars(cap, host_model, suite_list, run_env):
emsg = f"Unsupported 2-D variable, '{std_name}'"
raise CCPPError(emsg)
# end if
# First, create an index variable for <cvar>
ind_std_name = "index_of_{}".format(std_name)
loc_name = f"{cvar_array_name}(:,:,{ind_std_name})"
ddt_mdata.append(f"[ {loc_name} ]")
ddt_mdata.append(f" standard_name = {std_name}")
units = cvar.get_prop_value('units')
ddt_mdata.append(f" units = {units}")
dimstr = f"({', '.join(dims)})"
ddt_mdata.append(f" dimensions = {dimstr}")
vtype = cvar.get_prop_value('type')
vkind = cvar.get_prop_value('kind')
ddt_mdata.append(f" type = {vtype} | kind = {vkind}")
const_stdnames.add(std_name)
if "tendency_of" not in std_name:
# Create an index variable for <cvar>
ind_std_name = "index_of_{}".format(std_name)
loc_name = f"{cvar_array_name}(:,:,{ind_std_name})"
ddt_mdata.append(f"[ {loc_name} ]")
ddt_mdata.append(f" standard_name = {std_name}")
units = cvar.get_prop_value('units')
ddt_mdata.append(f" units = {units}")
dimstr = f"({', '.join(dims)})"
ddt_mdata.append(f" dimensions = {dimstr}")
vtype = cvar.get_prop_value('type')
vkind = cvar.get_prop_value('kind')
ddt_mdata.append(f" type = {vtype} | kind = {vkind}")
const_stdnames.add(std_name)
else:
# Create an index variable for tendency of constituent
const_std_name = std_name.split('tendency_of_')[1]
ind_std_name = "index_of_{}".format(const_std_name)
loc_name = f"{tend_layer}(:,:,{ind_std_name})"
ddt_mdata.append(f"[ {loc_name} ]")
ddt_mdata.append(f" standard_name = {std_name}")
units = cvar.get_prop_value('units')
ddt_mdata.append(f" units = {units} s-1")
dimstr = f"({', '.join(dims)})"
ddt_mdata.append(f" dimensions = {dimstr}")
vtype = cvar.get_prop_value('type')
vkind = cvar.get_prop_value('kind')
ddt_mdata.append(f" type = {vtype} | kind = {vkind}")
# end if
# end if
# end for
# end for
Expand Down
6 changes: 5 additions & 1 deletion scripts/suite_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,11 @@ def write_var_debug_check(self, var, internal_var, cldicts, outfile, errcode, er
# add_var_debug_check, therefore need to back out here,
# using the information from the scheme variable (call list).
svar = self.call_list.find_variable(standard_name=standard_name, any_scope=False)
intent = svar.get_prop_value('intent')
if svar:
intent = svar.get_prop_value('intent')
else:
intent = 'in'
# end if
if intent == 'out' and allocatable:
return

Expand Down
13 changes: 13 additions & 0 deletions src/ccpp_constituent_prop_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ module ccpp_constituent_prop_mod
! These fields are public to allow for efficient (i.e., no copying)
! usage even though it breaks object independence
real(kind_phys), allocatable :: vars_layer(:,:,:)
real(kind_phys), allocatable :: vars_layer_tend(:,:,:)
real(kind_phys), allocatable :: vars_minvalue(:)
! An array containing all the constituent metadata
! Each element contains a pointer to a constituent from the hash table
Expand Down Expand Up @@ -1465,12 +1466,21 @@ subroutine ccp_model_const_data_lock(this, ncols, num_layers, errcode, errmsg)
call handle_allocate_error(astat, 'vars_layer', &
subname, errcode=errcode, errmsg=errmsg)
errcode_local = astat
if (astat == 0) then
allocate(this%vars_layer_tend(ncols, num_layers, this%hash_table%num_values()), &
stat=astat)
call handle_allocate_error(astat, 'vars_layer_tend', &
subname, errcode=errcode, errmsg=errmsg)
errcode_local = astat
end if
if (astat == 0) then
allocate(this%vars_minvalue(this%hash_table%num_values()), stat=astat)
call handle_allocate_error(astat, 'vars_minvalue', &
subname, errcode=errcode, errmsg=errmsg)
errcode_local = astat
end if
! Initialize tendencies to 0
this%vars_layer_tend(:,:,:) = 0._kind_phys
if (errcode_local == 0) then
this%num_layers = num_layers
do index = 1, this%hash_table%num_values()
Expand Down Expand Up @@ -1521,6 +1531,9 @@ subroutine ccp_model_const_reset(this, clear_hash_table)
if (allocated(this%vars_minvalue)) then
deallocate(this%vars_minvalue)
end if
if (allocated(this%vars_layer_tend)) then
deallocate(this%vars_layer_tend)
end if
if (allocated(this%const_metadata)) then
if (clear_table) then
do index = 1, size(this%const_metadata, 1)
Expand Down
6 changes: 6 additions & 0 deletions src/ccpp_constituent_prop_mod.meta
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
state_variable = true
dimensions = (horizontal_dimension, vertical_layer_dimension, number_of_ccpp_constituents)
type = real | kind = kind_phys
[ vars_layer_tend ]
standard_name = ccpp_constituents_tendencies
long_name = Array of constituent tendencies managed by CCPP Framework
units = none
dimensions = (horizontal_dimension, vertical_layer_dimension, number_of_ccpp_constituents)
type = real | kind = kind_phys
[ const_metadata ]
standard_name = ccpp_constituent_properties
units = None
Expand Down
4 changes: 3 additions & 1 deletion test/advection_test/cld_liq.F90
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MODULE cld_liq
!! \htmlinclude arg_table_cld_liq_run.html
!!
subroutine cld_liq_run(ncol, timestep, tcld, temp, qv, ps, cld_liq_array, &
errmsg, errflg)
cld_liq_tend, errmsg, errflg)

integer, intent(in) :: ncol
real(kind_phys), intent(in) :: timestep
Expand All @@ -27,6 +27,8 @@ subroutine cld_liq_run(ncol, timestep, tcld, temp, qv, ps, cld_liq_array, &
real(kind_phys), intent(inout) :: qv(:,:)
real(kind_phys), intent(in) :: ps(:)
REAL(kind_phys), intent(inout) :: cld_liq_array(:,:)
REAL(kind_phys), intent(inout) :: cld_liq_tend(:,:)
! integer, intent(in) :: cld_liq_ind
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errflg
!----------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions test/advection_test/cld_liq.meta
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
dimensions = (horizontal_loop_extent, vertical_layer_dimension)
type = real | kind = kind_phys
intent = inout
[ cld_liq_tend ]
standard_name = tendency_of_cloud_liquid_dry_mixing_ratio
units = kg kg-1 s-1
dimensions = (horizontal_loop_extent, vertical_layer_dimension)
type = real | kind = kind_phys
intent = inout
[ errmsg ]
standard_name = ccpp_error_message
long_name = Error message for error handling in CCPP
Expand Down
3 changes: 3 additions & 0 deletions test/advection_test/run_test
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ required_vars="${required_vars},horizontal_loop_begin"
required_vars="${required_vars},horizontal_loop_end"
required_vars="${required_vars},surface_air_pressure"
required_vars="${required_vars},temperature"
required_vars="${required_vars},tendency_of_cloud_liquid_dry_mixing_ratio"
required_vars="${required_vars},time_step_for_physics"
required_vars="${required_vars},vertical_layer_dimension"
required_vars="${required_vars},water_temperature_at_freezing"
Expand All @@ -146,6 +147,7 @@ input_vars="${input_vars},horizontal_dimension"
input_vars="${input_vars},horizontal_loop_begin"
input_vars="${input_vars},horizontal_loop_end"
input_vars="${input_vars},surface_air_pressure,temperature"
input_vars="${input_vars},tendency_of_cloud_liquid_dry_mixing_ratio"
input_vars="${input_vars},time_step_for_physics"
input_vars="${input_vars},vertical_layer_dimension"
input_vars="${input_vars},water_temperature_at_freezing"
Expand All @@ -154,6 +156,7 @@ output_vars="ccpp_error_code,ccpp_error_message"
output_vars="${output_vars},cloud_ice_dry_mixing_ratio"
output_vars="${output_vars},cloud_liquid_dry_mixing_ratio"
output_vars="${output_vars},temperature"
output_vars="${output_vars},tendency_of_cloud_liquid_dry_mixing_ratio"
output_vars="${output_vars},water_vapor_specific_humidity"

##
Expand Down
55 changes: 29 additions & 26 deletions test/advection_test/test_host.F90
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module test_prog

! Public data and interfaces
integer, public, parameter :: cs = 16
integer, public, parameter :: cm = 36
integer, public, parameter :: cm = 41

!> \section arg_table_suite_info Argument Table
!! \htmlinclude arg_table_suite_info.html
Expand Down Expand Up @@ -1008,39 +1008,42 @@ program test
implicit none

character(len=cs), target :: test_parts1(1)
character(len=cm), target :: test_invars1(7)
character(len=cm), target :: test_outvars1(6)
character(len=cm), target :: test_reqvars1(9)
character(len=cm), target :: test_invars1(8)
character(len=cm), target :: test_outvars1(7)
character(len=cm), target :: test_reqvars1(10)

type(suite_info) :: test_suites(1)
logical :: run_okay

test_parts1 = (/ 'physics '/)
test_invars1 = (/ &
'cloud_ice_dry_mixing_ratio ', &
'cloud_liquid_dry_mixing_ratio ', &
'surface_air_pressure ', &
'temperature ', &
'time_step_for_physics ', &
'water_temperature_at_freezing ', &
'water_vapor_specific_humidity ' /)
'cloud_ice_dry_mixing_ratio ', &
'cloud_liquid_dry_mixing_ratio ', &
'tendency_of_cloud_liquid_dry_mixing_ratio', &
'surface_air_pressure ', &
'temperature ', &
'time_step_for_physics ', &
'water_temperature_at_freezing ', &
'water_vapor_specific_humidity ' /)
test_outvars1 = (/ &
'ccpp_error_message ', &
'ccpp_error_code ', &
'temperature ', &
'water_vapor_specific_humidity ', &
'cloud_liquid_dry_mixing_ratio ', &
'cloud_ice_dry_mixing_ratio ' /)
'ccpp_error_message ', &
'ccpp_error_code ', &
'temperature ', &
'water_vapor_specific_humidity ', &
'cloud_liquid_dry_mixing_ratio ', &
'tendency_of_cloud_liquid_dry_mixing_ratio', &
'cloud_ice_dry_mixing_ratio ' /)
test_reqvars1 = (/ &
'surface_air_pressure ', &
'temperature ', &
'time_step_for_physics ', &
'cloud_liquid_dry_mixing_ratio ', &
'cloud_ice_dry_mixing_ratio ', &
'water_temperature_at_freezing ', &
'water_vapor_specific_humidity ', &
'ccpp_error_message ', &
'ccpp_error_code ' /)
'surface_air_pressure ', &
'temperature ', &
'time_step_for_physics ', &
'cloud_liquid_dry_mixing_ratio ', &
'tendency_of_cloud_liquid_dry_mixing_ratio', &
'cloud_ice_dry_mixing_ratio ', &
'water_temperature_at_freezing ', &
'water_vapor_specific_humidity ', &
'ccpp_error_message ', &
'ccpp_error_code ' /)


! Setup expected test suite info
Expand Down
3 changes: 3 additions & 0 deletions test/advection_test/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def usage(errmsg=None):
_REQUIRED_VARS_CLD = ["ccpp_error_code", "ccpp_error_message",
"horizontal_loop_begin", "horizontal_loop_end",
"surface_air_pressure", "temperature",
"tendency_of_cloud_liquid_dry_mixing_ratio",
"time_step_for_physics", "water_temperature_at_freezing",
"water_vapor_specific_humidity",
"cloud_ice_dry_mixing_ratio",
Expand All @@ -83,11 +84,13 @@ def usage(errmsg=None):
"water_vapor_specific_humidity",
"cloud_ice_dry_mixing_ratio",
"cloud_liquid_dry_mixing_ratio",
"tendency_of_cloud_liquid_dry_mixing_ratio",
# Added by --debug option
"horizontal_dimension",
"vertical_layer_dimension"]
_OUTPUT_VARS_CLD = ["ccpp_error_code", "ccpp_error_message",
"water_vapor_specific_humidity", "temperature",
"tendency_of_cloud_liquid_dry_mixing_ratio",
"cloud_ice_dry_mixing_ratio",
"cloud_liquid_dry_mixing_ratio"]

Expand Down

0 comments on commit 922a4dd

Please sign in to comment.