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

New way to register dynamic constituents #16

Closed
Closed
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
54 changes: 18 additions & 36 deletions scripts/constituents.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def write_constituent_use_statements(cap, suite_list, indent):
@staticmethod
def write_host_routines(cap, host, reg_funcname, init_funcname, num_const_funcname,
query_const_funcname, copy_in_funcname, copy_out_funcname, cleanup_funcname,
const_obj_name, dyn_const_name, const_names_name, const_indices_name,
const_obj_name, const_names_name, const_indices_name,
const_array_func, advect_array_func, prop_array_func,
const_index_func, suite_list, dyn_const_dict, err_vars):
"""Write out the host model <reg_funcname> routine which will
Expand Down Expand Up @@ -497,19 +497,12 @@ def write_host_routines(cap, host, reg_funcname, init_funcname, num_const_funcna
spc = ' '*37
cap.write(f"integer{spc} :: num_suite_consts", 2)
cap.write(f"integer{spc} :: num_consts", 2)
cap.write(f"integer{spc} :: num_dyn_consts", 2)
cap.write(f"integer{spc} :: index, index_start", 2)
cap.write(f"integer{spc} :: index", 2)
cap.write(f"integer{spc} :: field_ind", 2)
cap.write(f"type({CONST_PROP_TYPE}), pointer :: const_prop => NULL()", 2)
# Declare dynamic constituent properties variables
for idx, scheme in enumerate(sorted(dyn_const_dict)):
cap.comment(f"dynamic constituent props variable for {scheme}", 2)
cap.write(f"type({CONST_PROP_TYPE}), allocatable :: dyn_const_prop_{idx}(:)", 2)
# end for
cap.blank_line()
cap.write(f"{herrcode} = 0", 2)
cap.write("num_consts = size(host_constituents, 1)", 2)
cap.write("num_dyn_consts = 0", 2)
for suite in suite_list:
const_dict = suite.constituent_dictionary()
funcname = const_dict.num_consts_funcname()
Expand All @@ -530,22 +523,7 @@ def write_host_routines(cap, host, reg_funcname, init_funcname, num_const_funcna
cap.write(f"if ({herrcode} /= 0) then", 2)
cap.write("return", 3)
cap.write("end if", 2)
cap.write(f"num_dyn_consts = num_dyn_consts + size(dyn_const_prop_{idx})", 2)
# end for
cap.write("num_consts = num_consts + num_dyn_consts", 2)
cap.comment(f"Pack {dyn_const_name} array", 2)
cap.write(f"allocate({dyn_const_name}(num_dyn_consts), stat={herrcode})", 2)
cap.write(f"if ({herrcode} /= 0) then", 2)
cap.write(f"{herrmsg} = 'failed to allocate {dyn_const_name}'", 3)
cap.write("return", 3)
cap.write("end if", 2)
cap.write("index_start = 0", 2)
for idx, scheme in enumerate(sorted(dyn_const_dict)):
cap.write(f"do index = 1, size(dyn_const_prop_{idx}, 1)", 2)
cap.write(f"{dyn_const_name}(index + index_start) = dyn_const_prop_{idx}(index)", 3)
cap.write("end do", 2)
cap.write(f"index_start = size(dyn_const_prop_{idx}, 1)", 2)
cap.write(f"deallocate(dyn_const_prop_{idx})", 2)
cap.write(f"num_consts = num_consts + size(dyn_const_prop_{idx})", 2)
# end for
# end if
cap.comment("Initialize constituent data and field object", 2)
Expand All @@ -566,17 +544,19 @@ def write_host_routines(cap, host, reg_funcname, init_funcname, num_const_funcna
# Register dynamic constituents
if len(dyn_const_dict) > 0:
cap.comment("Add dynamic constituent properties", 2)
cap.write(f"do index = 1, size({dyn_const_name}, 1)", 2)
cap.write(f"const_prop => {dyn_const_name}(index)", 3)
stmt = f"call {const_obj_name}%new_field(const_prop, {obj_err_callstr})"
cap.write(stmt, 3)
cap.write("nullify(const_prop)", 3)
cap.write(f"if ({herrcode} /= 0) then", 3)
cap.write("return", 4)
cap.write("end if", 3)
cap.write("end do", 2)
for idx, scheme in enumerate(sorted(dyn_const_dict)):
cap.write(f"do index = 1, size(dyn_const_prop_{idx}, 1)", 2)
cap.write(f"const_prop => dyn_const_prop_{idx}(index)", 3)
stmt = f"call {const_obj_name}%new_field(const_prop, {obj_err_callstr})"
cap.write(stmt, 3)
cap.write("nullify(const_prop)", 3)
cap.write(f"if ({herrcode} /= 0) then", 3)
cap.write("return", 4)
cap.write("end if", 3)
cap.write("end do", 2)
# end for
# end if

# Register suite constituents
for suite in suite_list:
errvar_str = ConstituentVarDict.__errcode_callstr(herrcode,
Expand Down Expand Up @@ -718,7 +698,9 @@ def write_host_routines(cap, host, reg_funcname, init_funcname, num_const_funcna
cap.write(f"{substmt}()", 1)
cap.comment("Deallocate dynamic constituent array", 2)
cap.blank_line()
cap.write(f"deallocate({dyn_const_name})", 2)
for idx, scheme in enumerate(sorted(dyn_const_dict)):
cap.write(f"deallocate(dyn_const_prop_{idx})", 2)
# end for
cap.write(f"end {substmt}", 1)
# Write constituents routine
cap.blank_line()
Expand Down
23 changes: 9 additions & 14 deletions scripts/host_cap.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,6 @@ def constituent_model_object_name(host_model):
# end if
return hvar.get_prop_value('local_name')

###############################################################################
def dynamic_constituent_array_name(host_model):
###############################################################################
"""Return the name of the allocatable dynamic constituent properites array"""
hstr = f"{host_model.name}_dynamic_constituents"
return unique_local_name(hstr, host_model)

###############################################################################
def constituent_model_const_stdnames(host_model):
###############################################################################
Expand Down Expand Up @@ -267,7 +260,7 @@ def constituent_model_const_index(host_model):
return unique_local_name(hstr, host_model)

###############################################################################
def add_constituent_vars(cap, host_model, suite_list, run_env):
def add_constituent_vars(cap, host_model, suite_list, dyn_const_dict, run_env):
###############################################################################
"""Create a DDT library containing array reference variables
for each constituent field for all suites in <suite_list>.
Expand Down Expand Up @@ -378,9 +371,11 @@ def add_constituent_vars(cap, host_model, suite_list, run_env):
# end if
ddt_lib.collect_ddt_fields(const_dict, const_var, run_env,
skip_duplicates=True)
# Declare the allocatable dynamic constituents array
dyn_const_name = dynamic_constituent_array_name(host_model)
cap.write(f"type({CONST_PROP_TYPE}), allocatable, target :: {dyn_const_name}(:)", 1)
# Declare the allocatable dynamic constituents arrays
for idx, scheme in enumerate(sorted(dyn_const_dict)):
cap.comment(f"dynamic constituent props variable for {scheme}", 2)
cap.write(f"type({CONST_PROP_TYPE}), allocatable, target :: dyn_const_prop_{idx}(:)", 2)
# end for
# Declare variable for the constituent standard names array
max_csname = max([len(x) for x in const_stdnames]) if const_stdnames else 0
num_const_fields = len(const_stdnames)
Expand Down Expand Up @@ -541,7 +536,8 @@ def write_host_cap(host_model, api, module_name, output_dir, run_env):
cap.write(f"public :: {const_index_func}", 1)
cap.write("", 0)
cap.write("! Private module variables", 1)
const_dict = add_constituent_vars(cap, host_model, api.suites, run_env)
const_dict = add_constituent_vars(cap, host_model, api.suites,
api.dyn_const_dict, run_env)
cap.end_module_header()
for stage in CCPP_STATE_MACH.transitions():
# Create a dict of local variables for stage
Expand Down Expand Up @@ -661,6 +657,7 @@ def write_host_cap(host_model, api, module_name, output_dir, run_env):
emsg = "write({errmsg}, '(3a)')".format(errmsg=errmsg_name)
emsg += '"No suite named ", '
emsg += 'trim(suite_name), "found"'

cap.write(emsg, 3)
cap.write("{errflg} = 1".format(errflg=errflg_name), 3)
cap.write("end if", 2)
Expand All @@ -675,13 +672,11 @@ def write_host_cap(host_model, api, module_name, output_dir, run_env):
cap.write("", 0)
const_names_name = constituent_model_const_stdnames(host_model)
const_indices_name = constituent_model_const_indices(host_model)
dyn_const_name = dynamic_constituent_array_name(host_model)
ConstituentVarDict.write_host_routines(cap, host_model, reg_name, init_name,
numconsts_name, queryconsts_name,
copyin_name, copyout_name,
cleanup_name,
const_obj_name,
dyn_const_name,
const_names_name,
const_indices_name,
const_array_func,
Expand Down
Loading