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

Physics tendency updaters #163

Merged
merged 19 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion Externals_CAM.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ required = False
local_path = src/physics/ncar_ccpp
protocol = git
repo_url = https://github.com/NCAR/atmospheric_physics
tag = atmos_phys0_00_011
tag = atmos_phys0_00_013
required = True

[silhs]
Expand Down
24 changes: 18 additions & 6 deletions cime_config/cam_autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Import needed python libraries/modules
########################################

# Python library imports
import sys
import os
import logging
Expand Down Expand Up @@ -397,6 +398,7 @@ def generate_physics_suites(ccpp_scripts_path, build_cache, preproc_defs, host_n
# Import needed CCPP-framework scripts:
try:
from ccpp_capgen import capgen
from framework_env import CCPPFrameworkEnv
#pylint: disable=redefined-outer-name
# pylint change because of doctest import below
from metadata_table import find_scheme_names
Expand Down Expand Up @@ -453,6 +455,7 @@ def generate_physics_suites(ccpp_scripts_path, build_cache, preproc_defs, host_n
# Figure out if we need to generate new physics code
genccpp_dir = os.path.join(bldroot, "ccpp")
kind_phys = 'REAL64'
kind_types = ["kind_phys={}".format(kind_phys)]

# Set location of CCPP "capfiles.txt" file:
cap_output_file = os.path.join(genccpp_dir, "ccpp_datatable.xml")
Expand Down Expand Up @@ -482,8 +485,8 @@ def generate_physics_suites(ccpp_scripts_path, build_cache, preproc_defs, host_n
do_gen_ccpp = True
# End if
if do_gen_ccpp:
gen_hostcap = True
gen_docfiles = False
use_error_obj = False

# print extra info to bldlog if DEBUG is TRUE
_LOGGER.debug("Calling capgen: ")
Expand All @@ -492,13 +495,22 @@ def generate_physics_suites(ccpp_scripts_path, build_cache, preproc_defs, host_n
_LOGGER.debug(" suite definition files: %s", ', '.join(sdfs))
_LOGGER.debug(" preproc defs: %s", preproc_cache_str)
_LOGGER.debug(" output directory: '%s'", genccpp_dir)
_LOGGER.debug(" kind_phys: '%s'", kind_phys)
for kind_type in kind_types:
name, type = [x.strip() for x in kind_type.split('=')]
_LOGGER.debug(" %s: '%s'", name, type)
# end for

# generate CCPP caps
force_overwrite = False
capgen(host_files, scheme_files, sdfs, cap_output_file,
preproc_cache_str, gen_hostcap, gen_docfiles, genccpp_dir,
host_name, kind_phys, force_overwrite, _LOGGER)
run_env = CCPPFrameworkEnv(_LOGGER, host_files=host_files,
scheme_files=scheme_files, suites=sdfs,
preproc_directives=preproc_defs,
generate_docfiles=gen_docfiles,
host_name=host_name, kind_types=kind_types,
use_error_obj=use_error_obj,
force_overwrite=False,
output_root=genccpp_dir,
ccpp_datafile=cap_output_file)
capgen(run_env)

# save build details in the build cache
build_cache.update_ccpp(sdfs, scheme_files, preproc_cache_str, kind_phys)
Expand Down
28 changes: 17 additions & 11 deletions src/data/generate_registry_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

# CCPP framework imports
# pylint: disable=wrong-import-position
from framework_env import CCPPFrameworkEnv
from parse_tools import validate_xml_file, read_xml_file
from parse_tools import find_schema_file, find_schema_version
from parse_tools import init_log, CCPPError, ParseInternalError
Expand Down Expand Up @@ -433,7 +434,7 @@ def __init__(self, elem_node, parent_name, dimensions, known_types,
', '.join(dimensions)))
# end if
local_name = '{}({})'.format(parent_name, self.index_string)
super(ArrayElement, self).__init__(elem_node, local_name, my_dimensions,
VarBase.__init__(self, elem_node, local_name, my_dimensions,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super happy with explicitly calling out the class hierarchy. Not that I have a plan to change this one but changing it is more difficult if we put explicit names in derived classes. Is there a good reason to not use super (here and below)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting errors using super - I think related to a python version change.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I think this is probably due to taking out the (object) inheritance in the classes.
Since we do not use multiple inheritance, you should be able to just use super() in place of VarBase.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get that to work, I think I have to change class VarBase to class VarBase(object). Does that seem right?

known_types, parent_type,
units_default=parent_units,
kind_default=parent_kind,
Expand Down Expand Up @@ -555,7 +556,7 @@ def __init__(self, var_node, known_types, vdict, logger):
# end if
# end for
# Initialize the base class
super(Variable, self).__init__(var_node, local_name,
VarBase.__init__(self, var_node, local_name,
my_dimensions, known_types, ttype,
protected=protected)

Expand Down Expand Up @@ -591,7 +592,7 @@ def __init__(self, var_node, known_types, vdict, logger):
def write_metadata(self, outfile):
"""Write out this variable as CCPP metadata"""
if self.access != "private":
super(Variable, self).write_metadata(outfile)
VarBase.write_metadata(self, outfile)
if (self.allocatable == "parameter") or self.protected:
outfile.write(' protected = True\n')
# end if
Expand Down Expand Up @@ -1430,16 +1431,18 @@ def parse_command_line(args, description):
return pargs

###############################################################################
def metadata_file_to_files(file_path, known_types, dycore, config, logger):
def metadata_file_to_files(file_path, known_types, dycore, config, run_env):
###############################################################################
"""Read the metadata file at <relative_file_path> and convert it to a
registry File object.
"""
known_ddts = known_types.known_ddt_names()
mfiles = []
if os.path.exists(file_path):
meta_tables = parse_metadata_file(file_path, known_ddts, logger)
logger.info("Parsing metadata_file, '{}'".format(file_path))
meta_tables = parse_metadata_file(file_path, known_ddts, run_env)
if run_env.logger:
run_env.logger.info("Parsing metadata_file, '{}'".format(file_path))
# end if
nusbaume marked this conversation as resolved.
Show resolved Hide resolved
else:
emsg = "Metadata file, '{}', does not exist"
raise CCPPError(emsg.format(file_path))
Expand All @@ -1455,7 +1458,7 @@ def metadata_file_to_files(file_path, known_types, dycore, config, logger):
section = '<file name="{}" type="{}"></file>'.format(hname, htype)
sect_xml = ET.fromstring(section)
mfile = File(sect_xml, known_types, dycore, config,
logger, gen_code=False, file_path=file_path)
run_env.logger, gen_code=False, file_path=file_path)
# Add variables
# Note, we only support one section per table for host variables
sections = mtable.sections()
Expand Down Expand Up @@ -1497,7 +1500,7 @@ def metadata_file_to_files(file_path, known_types, dycore, config, logger):
# end if
vnode_str += '\n</variable>'
var_node = ET.fromstring(vnode_str)
mfile.add_variable(var_node, logger)
mfile.add_variable(var_node, run_env.logger)
# end for
if htype == 'ddt':
# We defined the variables, now create the DDT for them.
Expand All @@ -1509,8 +1512,8 @@ def metadata_file_to_files(file_path, known_types, dycore, config, logger):
vnode_str += '\n</ddt>'
var_node = ET.fromstring(vnode_str)
new_ddt = DDT(var_node, known_types, mfile.var_dict,
dycore, config, logger)
mfile.add_ddt(new_ddt, logger=logger)
dycore, config, run_env.logger)
mfile.add_ddt(new_ddt, logger=run_env.logger)
# end if
mfiles.append(mfile)
# end for
Expand All @@ -1532,6 +1535,9 @@ def write_registry_files(registry, dycore, config, outdir, src_mod, src_root,
"""
files = []
known_types = TypeRegistry()
# Create a fake CCPPFrameworkEnv object to contain the logger
run_env = CCPPFrameworkEnv(logger, host_files='',
scheme_files='', suites='')
for section in registry:
sec_name = section.get('name')
if sec_name:
Expand Down Expand Up @@ -1567,7 +1573,7 @@ def write_registry_files(registry, dycore, config, outdir, src_mod, src_root,
# end if
# end if
meta_files = metadata_file_to_files(file_path, known_types,
dycore, config, logger)
dycore, config, run_env)
files.extend(meta_files)
else:
emsg = "Unknown registry object type, '{}'"
Expand Down
21 changes: 13 additions & 8 deletions src/data/registry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,29 @@
<ic_file_input_names>tw_cur state_tw_cur</ic_file_input_names>
</variable>
<!-- State tendencies -->
<variable local_name="dTdt" standard_name="total_tendency_of_air_temperature"
<variable local_name="dTdt_total"
standard_name="tendency_of_air_temperature_due_to_model_physics"
units="K s-1" type="real" kind="kind_phys"
allocatable="pointer">
<long_name>Change in temperature from a parameterization</long_name>
<dimensions>horizontal_dimension vertical_layer_dimension</dimensions>
<ic_file_input_names>dTdt tend_dtdt</ic_file_input_names>
</variable>
<variable local_name="dudt" standard_name="total_tendency_of_x_wind"
<variable local_name="dudt_total"
standard_name="tendency_of_x_wind_due_to_model_physics"
units="m s-2" type="real" kind="kind_phys"
allocatable="pointer">
<long_name>Change in x wind from a parameterization</long_name>
<dimensions>horizontal_dimension vertical_layer_dimension</dimensions>
<ic_file_input_names>dudt tend_dudt</ic_file_input_names>
</variable>
<variable local_name="dvdt" standard_name="total_tendency_of_y_wind"
<variable local_name="dvdt_total"
standard_name="tendency_of_y_wind_due_to_model_physics"
units="m s-2" type="real" kind="kind_phys"
allocatable="pointer">
<long_name>Change in y wind from a parameterization</long_name>
<dimensions>horizontal_dimension vertical_layer_dimension</dimensions>
<ic_file_input_names>dvdt tend_dvdt</ic_file_input_names>
</variable>
<variable local_name="lagrangian_vertical"
standard_name="lagrangian_vertical_coordinate"
Expand Down Expand Up @@ -321,18 +326,18 @@
<data>frontogenesis_angle</data>
</ddt>
<ddt type="physics_tend">
<data>total_tendency_of_air_temperature</data>
<data>total_tendency_of_x_wind</data>
<data>total_tendency_of_y_wind</data>
<data>tendency_of_air_temperature_due_to_model_physics</data>
<data>tendency_of_x_wind_due_to_model_physics</data>
<data>tendency_of_y_wind_due_to_model_physics</data>
</ddt>
<variable local_name="phys_state"
standard_name="physics_state_due_to_dynamics"
units="None" type="physics_state">
<long_name>Physics state variables updated by dynamical core</long_name>
</variable>
<variable local_name="phys_tend"
standard_name="total_tendency_of_physics"
units="None" type="physics_tend" phys_timestep_init_zero="true">
nusbaume marked this conversation as resolved.
Show resolved Hide resolved
standard_name="tendency_due_to_model_physics"
units="None" type="physics_tend">
<long_name>Total tendency from physics suite</long_name>
</variable>
</file>
Expand Down
36 changes: 18 additions & 18 deletions src/physics/utils/phys_comp.F90
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,21 @@ subroutine phys_init(cam_runtime_opts, phys_state, phys_tend, cam_out)
! Local variables
real(kind_phys) :: dtime_phys = 0.0_kind_phys ! Not set yet
character(len=512) :: errmsg
integer :: errflg
integer :: errcode

errflg = 0
errcode = 0
call physconst_init(columns_on_task, pver, pverp)

call allocate_physics_types_fields(columns_on_task, pver, pverp, &
pcnst, set_init_val_in=.true., reallocate_in=.false.)
call cam_ccpp_physics_initialize(phys_suite_name, dtime_phys, &
errmsg, errflg)
if (errflg /= 0) then
errmsg, errcode)
if (errcode /= 0) then
call endrun('cam_ccpp_physics_initialize: '//trim(errmsg))
end if
call ccpp_physics_suite_part_list(phys_suite_name, suite_parts, &
errmsg, errflg)
if (errflg /= 0) then
errmsg, errcode)
if (errcode /= 0) then
call endrun('cam_ccpp_suite_part_list: '//trim(errmsg))
end if

Expand Down Expand Up @@ -202,14 +202,14 @@ subroutine phys_run2(dtime_phys, cam_runtime_opts, phys_state, phys_tend, &
! Local variables
type(file_desc_t), pointer :: ncdata
character(len=512) :: errmsg
integer :: errflg
integer :: errcode
integer :: part_ind
integer :: col_start
integer :: col_end
integer :: data_frame
logical :: use_init_variables

errflg = 0
errcode = 0
! Physics needs to read in all data not read in by the dycore
ncdata => initial_file_get_id()

Expand All @@ -231,8 +231,8 @@ subroutine phys_run2(dtime_phys, cam_runtime_opts, phys_state, phys_tend, &

! Initialize the physics time step
call cam_ccpp_physics_timestep_initial(phys_suite_name, dtime_phys, &
errmsg, errflg)
if (errflg /= 0) then
errmsg, errcode)
if (errcode /= 0) then
call endrun('cam_ccpp_physics_timestep_initial: '//trim(errmsg))
end if

Expand All @@ -243,16 +243,16 @@ subroutine phys_run2(dtime_phys, cam_runtime_opts, phys_state, phys_tend, &
! Run CCPP suite
do part_ind = 1, size(suite_parts, 1)
call cam_ccpp_physics_run(phys_suite_name, suite_parts(part_ind), &
col_start, col_end, dtime_phys, errmsg, errflg)
if (errflg /= 0) then
col_start, col_end, dtime_phys, errmsg, errcode)
if (errcode /= 0) then
call endrun('cam_ccpp_physics_run: '//trim(errmsg))
end if
end do

! Finalize the time step
call cam_ccpp_physics_timestep_final(phys_suite_name, dtime_phys, &
errmsg, errflg)
if (errflg /= 0) then
errmsg, errcode)
if (errcode /= 0) then
call endrun('cam_ccpp_physics_timestep_final: '//trim(errmsg))
end if

Expand All @@ -278,13 +278,13 @@ subroutine phys_final(cam_runtime_opts, phys_state, phys_tend)
! Local variables
real(kind_phys) :: dtime_phys = 0.0_kind_phys ! Not used
character(len=512) :: errmsg
integer :: errflg
integer :: errcode

errflg = 0
errcode = 0

call cam_ccpp_physics_finalize(phys_suite_name, dtime_phys, &
errmsg, errflg)
if (errflg /= 0) then
errmsg, errcode)
if (errcode /= 0) then
call endrun('cam_ccpp_physics_finalize: '//trim(errmsg))
end if
deallocate(suite_names)
Expand Down
4 changes: 2 additions & 2 deletions src/physics/utils/phys_comp.meta
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
dimensions = ()
type = character
kind = len=512
[ errflg ]
standard_name = ccpp_error_flag
[ errcode ]
standard_name = ccpp_error_code
long_name = Error flag for error handling in CCPP
units = flag
dimensions = ()
Expand Down
4 changes: 2 additions & 2 deletions test/unit/sample_files/write_init_files/missing_var_host.meta
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
type = character
kind = len=512
[ errflg ]
standard_name = ccpp_error_flag
standard_name = ccpp_error_code
long_name = Error flag for error handling in CCPP
units = flag
units = 1
dimensions = ()
type = integer
[ theta ]
Expand Down
4 changes: 2 additions & 2 deletions test/unit/sample_files/write_init_files/simple_host.meta
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
type = character
kind = len=512
[ errflg ]
standard_name = ccpp_error_flag
standard_name = ccpp_error_code
long_name = Error flag for error handling in CCPP
units = flag
units = 1
dimensions = ()
type = integer
12 changes: 6 additions & 6 deletions test/unit/sample_files/write_init_files/temp_adjust.meta
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
kind = len=512
intent = out
[ errflg ]
standard_name = ccpp_error_flag
standard_name = ccpp_error_code
long_name = Error flag for error handling in CCPP
units = flag
units = 1
dimensions = ()
type = integer
intent = out
Expand All @@ -65,9 +65,9 @@
kind = len=512
intent = out
[ errflg ]
standard_name = ccpp_error_flag
standard_name = ccpp_error_code
long_name = Error flag for error handling in CCPP
units = flag
units = 1
dimensions = ()
type = integer
intent = out
Expand All @@ -83,9 +83,9 @@
kind = len=512
intent = out
[ errflg ]
standard_name = ccpp_error_flag
standard_name = ccpp_error_code
long_name = Error flag for error handling in CCPP
units = flag
units = 1
dimensions = ()
type = integer
intent = out
Loading