Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand var compatibility test for active attribute #525

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 50 additions & 23 deletions scripts/suite_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,10 +1410,17 @@ def write_var_debug_check(self, var, internal_var, cldicts, outfile, errcode, er
if not dimensions:
if not intent == 'out':
internal_var_lname = internal_var.get_prop_value('local_name')
outfile.write(f"if ({conditional}) then", indent)
outfile.write(f"! Assign value of {local_name} to internal_var", indent+1)
outfile.write(f"{internal_var_lname} = {local_name}", indent+1)
outfile.write(f"end if", indent)
tmp_indent = indent
if conditional != '.true.':
tmp_indent = indent + 1
outfile.write(f"if {conditional} then", indent)
# end if
outfile.write(f"! Assign value of {local_name} to {internal_var}", tmp_indent)
outfile.write(f"{internal_var_lname} = {local_name}", tmp_indent)
outfile.write('',indent)
if conditional != '.true.':
outfile.write(f"end if", indent)
# end if
# For arrays, check size of array against dimensions in metadata, then assign
# the lower and upper bounds to the internal_var variable if the intent is in/inout
else:
Expand Down Expand Up @@ -1479,24 +1486,38 @@ def write_var_debug_check(self, var, internal_var, cldicts, outfile, errcode, er
ubound_string = '(' + ','.join(ubound_strings) + ')'

# Write size check
outfile.write(f"if ({conditional}) then", indent)
outfile.write(f"! Check size of array {local_name}", indent+1)
outfile.write(f"if (size({local_name}{dim_string}) /= {array_size}) then", indent+1)
outfile.write(f"write({errmsg}, '(a)') 'In group {self.__group.name} before {self.__subroutine_name}:'", indent+2)
outfile.write(f"write({errmsg}, '(2(a,i8))') 'for array {local_name}, expected size ', {array_size}, ' but got ', size({local_name})", indent+2)
outfile.write(f"{errcode} = 1", indent+2)
outfile.write(f"return", indent+2)
outfile.write(f"end if", indent+1)
outfile.write(f"end if", indent)
tmp_indent = indent
if conditional != '.true.':
dustinswales marked this conversation as resolved.
Show resolved Hide resolved
tmp_indent = indent + 1
outfile.write(f"if {conditional} then", indent)
# end if
outfile.write(f"! Check size of array {local_name}", tmp_indent)
outfile.write(f"if (size({local_name}{dim_string}) /= {array_size}) then", tmp_indent)
outfile.write(f"write({errmsg}, '(a)') 'In group {self.__group.name} before {self.__subroutine_name}:'", tmp_indent+1)
outfile.write(f"write({errmsg}, '(2(a,i8))') 'for array {local_name}, expected size ', {array_size}, ' but got ', size({local_name})", tmp_indent+1)
outfile.write(f"{errcode} = 1", tmp_indent+1)
outfile.write(f"return", tmp_indent+1)
outfile.write(f"end if", tmp_indent)
if conditional != '.true.':
outfile.write(f"end if", indent)
# end if
outfile.write('',indent)

# Assign lower/upper bounds to internal_var (scalar) if intent is not out
if not intent == 'out':
internal_var_lname = internal_var.get_prop_value('local_name')
outfile.write(f"if ({conditional}) then", indent)
outfile.write(f"! Assign lower/upper bounds of {local_name} to internal_var", indent+1)
outfile.write(f"{internal_var_lname} = {local_name}{lbound_string}", indent+1)
outfile.write(f"{internal_var_lname} = {local_name}{ubound_string}", indent+1)
outfile.write(f"end if", indent)
tmp_indent = indent
if conditional != '.true.':
dustinswales marked this conversation as resolved.
Show resolved Hide resolved
tmp_indent = indent + 1
outfile.write(f"if {conditional} then", indent)
# end if
outfile.write(f"! Assign lower/upper bounds of {local_name} to {internal_var}", tmp_indent+1)
outfile.write(f"{internal_var_lname} = {local_name}{lbound_string}", tmp_indent+1)
outfile.write(f"{internal_var_lname} = {local_name}{ubound_string}", tmp_indent+1)
if conditional != '.true.':
outfile.write(f"end if", indent)
# end if
outfile.write('',indent)

def add_var_transform(self, var, compat_obj, vert_dim):
"""Register any variable transformation needed by <var> for this Scheme.
Expand Down Expand Up @@ -1571,7 +1592,7 @@ def write_var_transform(self, var, dummy, rindices, lindices, compat_obj,
rvar_lname=dummy,
lvar_indices=rindices,
rvar_indices=lindices)
outfile.write(stmt, indent+1)
outfile.write(stmt, indent)

def write(self, outfile, errcode, errmsg, indent):
# Unused arguments are for consistent write interface
Expand All @@ -1585,23 +1606,29 @@ def write(self, outfile, errcode, errmsg, indent):
is_func_call=True,
subname=self.subroutine_name)

outfile.write('', indent)
outfile.write('if ({} == 0) then'.format(errcode), indent)

# Write debug checks (operating on variables
# coming from the group's call list)
for (var, internal_var) in self.__var_debug_checks:
stmt = self.write_var_debug_check(var, internal_var, cldicts, outfile, errcode, errmsg, indent)

stmt = self.write_var_debug_check(var, internal_var, cldicts, outfile, errcode, errmsg, indent+1)
# Write any reverse (pre-Scheme) transforms.
outfile.write('! Compute reverse (pre-scheme) transforms', indent+1)
for (dummy, var, rindices, lindices, compat_obj) in self.__reverse_transforms:
tstmt = self.write_var_transform(var, dummy, rindices, lindices, compat_obj, outfile, indent, False)
tstmt = self.write_var_transform(var, dummy, rindices, lindices, compat_obj, outfile, indent+1, False)
# Write the scheme call.
stmt = 'call {}({})'
outfile.write('',indent+1)
outfile.write('! Call scheme', indent+1)
outfile.write(stmt.format(self.subroutine_name, my_args), indent+1)
# Write any forward (post-Scheme) transforms.
outfile.write('',indent+1)
outfile.write('! Compute forward (post-scheme) transforms', indent+1)
for (var, dummy, lindices, rindices, compat_obj) in self.__forward_transforms:
tstmt = self.write_var_transform(var, dummy, rindices, lindices, compat_obj, outfile, indent, True)
tstmt = self.write_var_transform(var, dummy, rindices, lindices, compat_obj, outfile, indent+1, True)
#
outfile.write('', indent)
outfile.write('end if', indent)

def schemes(self):
Expand Down
8 changes: 6 additions & 2 deletions test/var_compatibility_test/effr_calc.F90
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ module effr_calc
!> \section arg_table_effr_calc_run Argument Table
!! \htmlinclude arg_table_effr_calc_run.html
!!
subroutine effr_calc_run(ncol, nlev, effrr_in, effrl_inout, &
effri_out, effrs_inout, errmsg, errflg)
subroutine effr_calc_run(ncol, nlev, effrr_in, effrg_in, effrl_inout, &
effri_out, effrs_inout, has_graupel, errmsg, errflg)

integer, intent(in) :: ncol
integer, intent(in) :: nlev
real(kind_phys), intent(in) :: effrr_in(:,:)
real(kind_phys), intent(in) :: effrg_in(:,:)
real(kind_phys), intent(inout) :: effrl_inout(:,:)
real(kind_phys), intent(out) :: effri_out(:,:)
real(8),intent(inout) :: effrs_inout(:,:)
logical, intent(in) :: has_graupel
character(len=512), intent(out) :: errmsg
integer, intent(out) :: errflg
!----------------------------------------------------------------
Expand All @@ -32,11 +34,13 @@ subroutine effr_calc_run(ncol, nlev, effrr_in, effrl_inout, &
real(kind_phys), parameter :: re_qc_max = 50. ! microns
real(kind_phys), parameter :: re_qi_avg = 75. ! microns
real(kind_phys) :: effrr_local(ncol,nlev)
real(kind_phys) :: effrg_local(ncol,nlev)

errmsg = ''
errflg = 0

effrr_local = effrr_in
if (has_graupel) effrg_local = effrg_in
effrl_inout = min(max(effrl_inout,re_qc_min),re_qc_max)
effri_out = re_qi_avg
effrs_inout = effrs_inout + 10.0 ! in micrometer
Expand Down
15 changes: 15 additions & 0 deletions test/var_compatibility_test/effr_calc.meta
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
kind = kind_phys
intent = in
top_at_one = .true.
[effrg_in]
standard_name = effective_radius_of_stratiform_cloud_graupel
long_name = effective radius of cloud graupel in micrometer
units = um
dimensions = (horizontal_loop_extent,vertical_layer_dimension)
type = real
kind = kind_phys
intent = in
[effrl_inout]
standard_name = effective_radius_of_stratiform_cloud_liquid_water_particle
long_name = effective radius of cloud liquid water particle in micrometer
Expand All @@ -51,6 +59,13 @@
kind = 8
intent = inout
top_at_one = .true.
[has_graupel]
standard_name = flag_indicating_cloud_microphysics_has_graupel
long_name = flag indicating that the cloud microphysics produces graupel
units = flag
dimensions = ()
type = logical
intent = in
[ errmsg ]
standard_name = ccpp_error_message
long_name = Error message for error handling in CCPP
Expand Down
6 changes: 5 additions & 1 deletion test/var_compatibility_test/run_test
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,21 @@ module_list="effr_calc"
#dependencies=""
suite_list="var_compatibility_suite"
required_vars_var_compatibility="ccpp_error_code,ccpp_error_message"
required_vars_var_compatibility="${required_vars_var_compatibility},effective_radius_of_stratiform_cloud_graupel"
required_vars_var_compatibility="${required_vars_var_compatibility},effective_radius_of_stratiform_cloud_ice_particle"
required_vars_var_compatibility="${required_vars_var_compatibility},effective_radius_of_stratiform_cloud_liquid_water_particle"
required_vars_var_compatibility="${required_vars_var_compatibility},effective_radius_of_stratiform_cloud_rain_particle"
required_vars_var_compatibility="${required_vars_var_compatibility},effective_radius_of_stratiform_cloud_snow_particle"
required_vars_var_compatibility="${required_vars_var_compatibility},flag_indicating_cloud_microphysics_has_graupel"
required_vars_var_compatibility="${required_vars_var_compatibility},horizontal_dimension"
required_vars_var_compatibility="${required_vars_var_compatibility},horizontal_loop_begin"
required_vars_var_compatibility="${required_vars_var_compatibility},horizontal_loop_end"
required_vars_var_compatibility="${required_vars_var_compatibility},vertical_layer_dimension"
input_vars_var_compatibility="effective_radius_of_stratiform_cloud_liquid_water_particle"
input_vars_var_compatibility="effective_radius_of_stratiform_cloud_graupel"
input_vars_var_compatibility="${input_vars_var_compatibility},effective_radius_of_stratiform_cloud_liquid_water_particle"
input_vars_var_compatibility="${input_vars_var_compatibility},effective_radius_of_stratiform_cloud_rain_particle"
input_vars_var_compatibility="${input_vars_var_compatibility},effective_radius_of_stratiform_cloud_snow_particle"
input_vars_var_compatibility="${input_vars_var_compatibility},flag_indicating_cloud_microphysics_has_graupel"
input_vars_var_compatibility="${input_vars_var_compatibility},horizontal_dimension"
input_vars_var_compatibility="${input_vars_var_compatibility},horizontal_loop_begin"
input_vars_var_compatibility="${input_vars_var_compatibility},horizontal_loop_end"
Expand Down
12 changes: 8 additions & 4 deletions test/var_compatibility_test/test_host.F90
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,12 @@ program test

character(len=cs), target :: test_parts1(1) = (/ 'radiation ' /)

character(len=cm), target :: test_invars1(3) = (/ &
character(len=cm), target :: test_invars1(5) = (/ &
'effective_radius_of_stratiform_cloud_rain_particle ', &
'effective_radius_of_stratiform_cloud_liquid_water_particle', &
'effective_radius_of_stratiform_cloud_snow_particle ' /)
'effective_radius_of_stratiform_cloud_snow_particle ', &
'effective_radius_of_stratiform_cloud_graupel ', &
'flag_indicating_cloud_microphysics_has_graupel '/)

character(len=cm), target :: test_outvars1(5) = (/ &
'ccpp_error_code ', &
Expand All @@ -363,13 +365,15 @@ program test
'effective_radius_of_stratiform_cloud_liquid_water_particle', &
'effective_radius_of_stratiform_cloud_snow_particle ' /)

character(len=cm), target :: test_reqvars1(6) = (/ &
character(len=cm), target :: test_reqvars1(8) = (/ &
'ccpp_error_code ', &
'ccpp_error_message ', &
'effective_radius_of_stratiform_cloud_rain_particle ', &
'effective_radius_of_stratiform_cloud_ice_particle ', &
'effective_radius_of_stratiform_cloud_liquid_water_particle', &
'effective_radius_of_stratiform_cloud_snow_particle ' /)
'effective_radius_of_stratiform_cloud_snow_particle ', &
'effective_radius_of_stratiform_cloud_graupel ', &
'flag_indicating_cloud_microphysics_has_graupel '/)

type(suite_info) :: test_suites(1)
logical :: run_okay
Expand Down
13 changes: 11 additions & 2 deletions test/var_compatibility_test/test_host_data.F90
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ module test_host_data
real(kind_phys), dimension(:,:), allocatable :: &
effrr, & ! effective radius of cloud rain
effrl, & ! effective radius of cloud liquid water
effri ! effective radius of cloud ice
effri, & ! effective radius of cloud ice
effrg ! effective radius of cloud graupel
end type physics_state

public allocate_physics_state

contains

subroutine allocate_physics_state(cols, levels, state)
subroutine allocate_physics_state(cols, levels, state, has_graupel)
integer, intent(in) :: cols
integer, intent(in) :: levels
type(physics_state), intent(out) :: state
logical, intent(in) :: has_graupel

if (allocated(state%effrr)) then
deallocate(state%effrr)
Expand All @@ -35,6 +37,13 @@ subroutine allocate_physics_state(cols, levels, state)
end if
allocate(state%effri(cols, levels))

if (has_graupel) then
if (allocated(state%effrg)) then
deallocate(state%effrg)
end if
allocate(state%effrg(cols, levels))
endif

end subroutine allocate_physics_state

end module test_host_data
8 changes: 8 additions & 0 deletions test/var_compatibility_test/test_host_data.meta
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@
dimensions = (horizontal_dimension,vertical_layer_dimension)
type = real
kind = kind_phys
[effrg]
standard_name = effective_radius_of_stratiform_cloud_graupel
long_name = effective radius of cloud graupel in meter
units = m
dimensions = (horizontal_dimension,vertical_layer_dimension)
type = real
kind = kind_phys
active = (flag_indicating_cloud_microphysics_has_graupel)
6 changes: 5 additions & 1 deletion test/var_compatibility_test/test_host_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module test_host_mod
integer, parameter :: pver = 4
type(physics_state) :: phys_state
real(kind_phys) :: effrs(ncols, pver)
logical, parameter :: mp_has_graupel = .true.

public :: init_data
public :: compare_data
Expand All @@ -22,11 +23,14 @@ module test_host_mod
subroutine init_data()

! Allocate and initialize state
call allocate_physics_state(ncols, pver, phys_state)
call allocate_physics_state(ncols, pver, phys_state, mp_has_graupel)
phys_state%effrr = 1.0E-3 ! 1000 microns, in meter
phys_state%effrl = 1.0E-4 ! 100 microns, in meter
phys_state%effri = 5.0E-5 ! 50 microns, in meter
effrs = 5.0E-4 ! 500 microns, in meter
if (mp_has_graupel) then
phys_state%effrg = 2.5E-4 ! 250 microns, in meter
endif

end subroutine init_data

Expand Down
6 changes: 6 additions & 0 deletions test/var_compatibility_test/test_host_mod.meta
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@
dimensions = (horizontal_dimension,vertical_layer_dimension)
type = real
kind = kind_phys
[mp_has_graupel]
standard_name = flag_indicating_cloud_microphysics_has_graupel
long_name = flag indicating that the cloud microphysics produces graupel
units = flag
dimensions = ()
type = logical
4 changes: 3 additions & 1 deletion test/var_compatibility_test/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def usage(errmsg=None):
_INPUT_VARS_VAR_ACTION = ["horizontal_loop_begin", "horizontal_loop_end", "horizontal_dimension", "vertical_layer_dimension",
"effective_radius_of_stratiform_cloud_liquid_water_particle",
"effective_radius_of_stratiform_cloud_rain_particle",
"effective_radius_of_stratiform_cloud_snow_particle"]
"effective_radius_of_stratiform_cloud_snow_particle",
"effective_radius_of_stratiform_cloud_graupel",
"flag_indicating_cloud_microphysics_has_graupel"]
_OUTPUT_VARS_VAR_ACTION = ["ccpp_error_code", "ccpp_error_message",
"effective_radius_of_stratiform_cloud_ice_particle",
"effective_radius_of_stratiform_cloud_liquid_water_particle",
Expand Down
Loading