Skip to content

Commit

Permalink
small clean-ups to SPECL functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDudaRESPEC committed Jan 3, 2024
1 parent 9635626 commit 35c85e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
41 changes: 21 additions & 20 deletions HSP2/SPECL.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@
import h5py

def specl_load_actions(state, io_manager, siminfo):
dc = state['specactions']['ACTIONS']
#print(dc.index)
#print("speca entry 0:0", dc[0:0])
#print("speca entry 0:1", dc[0:1])
#print("speca entry 1:2", dc[1:2])
#print("speca entry 0:", dc[0:])
#print("speca entry 1:", dc[1:])
#print("speca entry 1:1", dc[1:1])
for ix in dc.index:
# add the items to the state['model_data'] dict
speca = dc[ix:(ix+1)]
# need to add a name attribute
opname = 'SPEC' + 'ACTION' + str(ix)
state['model_data'][opname] = {}
state['model_data'][opname]['name'] = opname
for ik in speca.keys():
#print("looking for speca key ", ik)
state['model_data'][opname][ik] = speca.to_dict()[ik][ix]
state['model_data'][opname]['object_class'] = 'SpecialAction'
#print("model_data", ix, " = ", state['model_data'][opname])
if 'ACTIONS' in state['specactions']:
dc = state['specactions']['ACTIONS']
#print(dc.index)
#print("speca entry 0:0", dc[0:0])
#print("speca entry 0:1", dc[0:1])
#print("speca entry 1:2", dc[1:2])
#print("speca entry 0:", dc[0:])
#print("speca entry 1:", dc[1:])
#print("speca entry 1:1", dc[1:1])
for ix in dc.index:
# add the items to the state['model_data'] dict
speca = dc[ix:(ix+1)]
# need to add a name attribute
opname = 'SPEC' + 'ACTION' + str(ix)
state['model_data'][opname] = {}
state['model_data'][opname]['name'] = opname
for ik in speca.keys():
#print("looking for speca key ", ik)
state['model_data'][opname][ik] = speca.to_dict()[ik][ix]
state['model_data'][opname]['object_class'] = 'SpecialAction'
#print("model_data", ix, " = ", state['model_data'][opname])
return

def state_load_dynamics_specl(state, io_manager, siminfo):
Expand Down
18 changes: 10 additions & 8 deletions HSP2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,31 @@ def main(io_manager:IOManager, saveall:bool=False, jupyterlab:bool=True) -> None
gener_instances = {}

#######################################################################################
# initilize STATE dicts
# initialize STATE dicts
#######################################################################################
# Set up Things in state that will be used in all modular activitis like SPECL
# Set up Things in state that will be used in all modular activities like SPECL
state = init_state_dicts()
state_siminfo_hsp2(uci_obj, siminfo)
# Add support for dynamic functins to operate on STATE
# Add support for dynamic functions to operate on STATE
# - Load any dynamic components if present, and store variables on objects
state_load_dynamics_hsp2(state, io_manager, siminfo)
# Iterate through all segments and add crucial paths to state
# before loading dynamic components that may reference them
for _, operation, segment, delt in opseq.itertuples():
for activity, function in activities[operation].items():
if activity == 'HYDR':
state_context_hsp2(state, operation, segment, activity)
print("Init HYDR state context for domain", state['domain'])
hydr_init_ix(state['state_ix'], state['state_paths'], state['domain'])
if operation != 'GENER' and operation != 'COPY':
for activity, function in activities[operation].items():
if activity == 'HYDR':
state_context_hsp2(state, operation, segment, activity)
print("Init HYDR state context for domain", state['domain'])
hydr_init_ix(state['state_ix'], state['state_paths'], state['domain'])
# - finally stash specactions in state, not domain (segment) dependent so do it once
state['specactions'] = specactions # stash the specaction dict in state
state_load_dynamics_specl(state, io_manager, siminfo)
state_load_dynamics_om(state, io_manager, siminfo)
# finalize all dynamically loaded components and prepare to run the model
state_om_model_run_prep(state, io_manager, siminfo)
#######################################################################################

# main processing loop
msg(1, f'Simulation Start: {start}, Stop: {stop}')
tscat = {}
Expand Down
15 changes: 7 additions & 8 deletions HSP2/om_special_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,13 @@ def handle_ac(self, ac):
# 11 LN T= Ln(A)
# 12 LOG T= Log10(A)
# 13 MOD T= Mod(T,A)
if !is_float_digit(self.ac):
if !(self in cop_codes.values())
raise Exception("Error: in "+ self.name + " AC (" + self.ac + ") not supported. Object creation halted. Path to object with error is " + self.state_path)
ac = self.ac
else:
# this will fail catastrophically if the requested function is not supported
# which is a good thing
ac = cop_codes[self.ac]
if not is_float_digit(ac):
if not ac in cop_codes:
raise Exception("Error: in "+ self.name + " AC (" + ac + ") not supported. Object creation halted. Path to object with error is " + self.state_path)
else:
# this will fail catastrophically if the requested function is not supported
# which is a good thing
ac = cop_codes[ac]
self.opid = ac

def tokenize(self):
Expand Down

1 comment on commit 35c85e6

@rburghol
Copy link
Contributor

Choose a reason for hiding this comment

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

These are great fixes @PaulDudaRESPEC -- thanks for catching this!
FWIW - I was wondering if in the functon spec_load_actions(), setting there would be utility in setting 'ACTIONS' if it did not exist? I.e. something like:

if 'ACTIONS' in state['specactions']:

else:
   state['specactions']['ACTIONS'] = {}

Or perhaps that is better done in the parse UCI functions? Or is it better the way you did it, where if it is not in the UCI, then it doesn't get included and all code should check for the existence of a given set of data in a UCI explicitly rather than trust it being there?

Please sign in to comment.