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

Statey #116

Closed
wants to merge 132 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
132 commits
Select commit Hold shift + click to select a range
166628a
Fix pip install bug when jupyter lab version restricted to 3.0.*
rburghol Jun 21, 2023
027f775
Fix pip install bug when jupyter lab version restricted to 3.0.*
rburghol Jun 21, 2023
f65be05
Fix pip install bug when jupyter lab version restricted to 3.0.*
rburghol Jun 21, 2023
cbe2359
set up first dicts and includes
Jun 22, 2023
90c1641
basic dicts set, new functions used
Jun 22, 2023
3dea04a
annotations
Jun 22, 2023
679abd6
send dicts
Jun 22, 2023
578c2d6
annotations
Jun 22, 2023
470e9a5
call state_step_hydr if present
Jun 22, 2023
bb725e3
load dynamic code
Jun 22, 2023
cb35bd0
pass state to hydr
Jun 22, 2023
ec3ba0a
add sim meta data to state
Jun 22, 2023
bdd32ee
remove bad code from debugging
Jun 22, 2023
8610038
pass state
Jun 22, 2023
9bae607
extract function as string
Jun 22, 2023
8363363
include state_paths retrieval
Jun 22, 2023
cc40af0
pass state_step_hydr in state_info as string
Jun 22, 2023
822fe45
debug
Jun 22, 2023
85abfa1
debug
Jun 22, 2023
864c039
debug
Jun 22, 2023
012feae
can I import a function inside a statement?
Jun 22, 2023
d93d30e
get the lib
Jun 22, 2023
32394f0
debug
Jun 22, 2023
ec22ac3
import all
Jun 22, 2023
0dfe90b
import all
Jun 22, 2023
19aad16
debug
Jun 22, 2023
685856b
debug
Jun 22, 2023
88efa28
debug
Jun 22, 2023
14590a1
debug less
Jun 22, 2023
f9909f5
debug less
Jun 22, 2023
06deb4e
pass the module in and resore hydr_ix
Jun 22, 2023
655a839
try loading dynamics inside HYDR
Jun 22, 2023
889b624
use module name prefix
Jun 22, 2023
dfafba6
import in function
Jun 22, 2023
bf468dc
force module name in import
Jun 22, 2023
9abcee0
no module prefix
Jun 22, 2023
f2cf82c
debug
Jun 22, 2023
2afa62c
debug
Jun 22, 2023
8f07182
try to load the default then override
Jun 22, 2023
729f74c
try to load the default then override
Jun 22, 2023
f8a055f
njit helpr functions
Jun 22, 2023
d825f2a
split helpers
Jun 22, 2023
2c2ca53
fix
Jun 22, 2023
78246e7
fix
Jun 22, 2023
30d01ec
fix
Jun 22, 2023
d8d0160
fix
Jun 22, 2023
92d5e7c
debug less
Jun 22, 2023
d0949d2
fn as arg
Jun 22, 2023
28a203b
debug
Jun 22, 2023
e2c5e46
debug
Jun 22, 2023
c254035
debug
Jun 22, 2023
c10e34f
import default in code too
Jun 22, 2023
5f75cd7
fix typo
Jun 22, 2023
fc6214b
add write-backs
Jun 22, 2023
b00bbc0
add write-backs
Jun 22, 2023
1aaaa15
can send hsp2_local_py in via state?
Jun 22, 2023
345c637
use state loaded hsp2_local instead of reloading
Jun 22, 2023
ab4e8a7
disable speed test
Jun 22, 2023
8ca5d45
disable speed test
Jun 22, 2023
0af3bde
also remove passing hsp2 local speed test
Jun 22, 2023
1cb2c04
merge state_ix lines
Jun 22, 2023
242d076
remove initial import of state defaults in HYDR, and restore use of f…
Jun 23, 2023
6ae7973
disabled custom code
Jun 23, 2023
8ee9f1b
enable initial loader only
Jun 23, 2023
9fce9e5
disable all
Jun 23, 2023
8d964ff
disable all
Jun 23, 2023
1b7a58b
blank state_paths
Jun 23, 2023
1b6a30d
do not set state each ts
Jun 23, 2023
6547e75
do not call hydr_get_ix
Jun 23, 2023
1141c40
do not pass any state vars to _hydr_
Jun 23, 2023
9106093
do not pass any state vars to _hydr_
Jun 23, 2023
97b8bd7
disable state_paths init
Jun 23, 2023
6a03dfc
disable state_info init
Jun 23, 2023
abe274d
re enable all but dynamic module load
Jun 23, 2023
b6f7055
re enable all but dynamic module load including loading dynamic file …
Jun 23, 2023
9d0a465
load dyn module in HYDR
Jun 23, 2023
9fd0873
load dyn module in HYDR
Jun 23, 2023
b3ab75d
pass state_paths to _hydr_
Jun 23, 2023
852c65a
pass all to _hydr_
Jun 23, 2023
e5e1b09
pass all to _hydr_
Jun 23, 2023
48101f6
pass all except dyn fn _hydr_
Jun 23, 2023
cd8fe69
load state in _hydr_
Jun 23, 2023
280eba5
load and set state in _hydr_
Jun 23, 2023
04f8972
run state_step_hydr
Jun 23, 2023
6c2b610
run state_step_hydr
Jun 23, 2023
d46b852
try to make a slim dyn function
Jun 23, 2023
bd2ebed
streamline and improve code ordering into sections
Jun 26, 2023
2b527c2
messages
Jun 26, 2023
8506807
message less
Jun 26, 2023
28a1649
must pass state_info to allow domain specific logic and potentially e…
Jun 26, 2023
2e50c16
debug
Jun 26, 2023
8913176
remove extra specl test
Jun 26, 2023
c7ae4a0
remove extra specl test
Jun 26, 2023
b3e47db
debug
Jun 26, 2023
415b9d5
debug
Jun 26, 2023
4aaeedf
try to stash varying outdgt lengths
Jun 26, 2023
6fe2ba1
try to stash varying outdgt lengths
Jun 26, 2023
a9a05da
use arange to set size
Jun 26, 2023
0e8fa9b
use arange to set size
Jun 26, 2023
b53fa19
use numout duh
Jun 26, 2023
db96a20
array use
Jun 26, 2023
cdff611
array use arange
Jun 26, 2023
f7d5397
array use arange
Jun 26, 2023
85fffe9
array use arange
Jun 26, 2023
68ee5ab
loop
Jun 26, 2023
51a7e73
denubug
Jun 26, 2023
beaa34c
denubug
Jun 26, 2023
5f3e715
debug
Jun 26, 2023
6eff8e7
debug
Jun 26, 2023
2267093
omit testing code from specl
Jun 26, 2023
0b01399
copy to and from
Jun 26, 2023
657ebdd
no counter
Jun 26, 2023
757f3b2
fix
Jun 26, 2023
1fba473
forgot to remove the bad line of cod
Jun 27, 2023
8dfa122
Pass activity not function as string
Jun 27, 2023
c073c87
Pass activity not function as string
Jun 27, 2023
a4e5611
debug off
Jun 27, 2023
8002899
pass state to specl
Jun 27, 2023
c862ac3
Merge pull request #68 from HARPgroup/statex
rburghol Jun 27, 2023
99c0e62
added test10 support
rburghol Jun 27, 2023
31be941
insure we only operate on rchres1 and also send state_paths to variou…
Jun 27, 2023
9c961bf
fix specl
Jun 27, 2023
0ac5ef7
fix specl
Jun 27, 2023
ac3ceb8
fix specl
Jun 27, 2023
61e83c5
fix specl state_paths
Jun 27, 2023
a01c44e
check for single rchres and activity in dynamic python test
rburghol Jun 28, 2023
dd4df97
tidy up state into functions
Jun 28, 2023
596dd04
add pandas
Jun 28, 2023
c7f0f5b
fix start retrieval
Jun 28, 2023
2b5e510
date import
Jun 28, 2023
03ab358
added point source return flow to demo project test 10
rburghol Jun 28, 2023
e66c6c6
clean up
Jun 28, 2023
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
126 changes: 71 additions & 55 deletions HSP2/HYDR.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
'''


from numpy import zeros, any, full, nan, array, int64
from numpy import zeros, any, full, nan, array, int64, arange
from pandas import DataFrame
from math import sqrt, log10
from numba import njit
from numba.typed import List
from HSP2.utilities import initm, make_numba_dict
from HSP2.SPECL import specl, _specl_
from HSP2.state import *
from HSP2.SPECL import specl


ERRMSGS =('HYDR: SOLVE equations are indeterminate', #ERRMSG0
Expand All @@ -31,7 +32,7 @@
MAXLOOPS = 100 # newton method exit tolerance


def hydr(io_manager, siminfo, uci, ts, ftables, specactions):
def hydr(io_manager, siminfo, uci, ts, ftables, state):
''' find the state of the reach/reservoir at the end of the time interval
and the outflows during the interval

Expand All @@ -40,7 +41,9 @@ def hydr(io_manager, siminfo, uci, ts, ftables, specactions):
general is a dictionary with simulation level infor (OP_SEQUENCE for example)
ui is a dictionary with RID specific HSPF UCI like data
ts is a dictionary with RID specific timeseries
specactions is a dictionary with all special actions'''
state is a dictionary that contains all dynamic code dictionaries such as:
- specactions is a dictionary with all special actions
'''

steps = siminfo['steps'] # number of simulation points
uunits = siminfo['units']
Expand Down Expand Up @@ -72,33 +75,11 @@ def hydr(io_manager, siminfo, uci, ts, ftables, specactions):

# OUTDGT timeseries might come in as OUTDGT, OUTDGT0, etc. otherwise UCI default
names = list(sorted([n for n in ts if n.startswith('OUTDG')], reverse=True))
print(names)
df = DataFrame()
for i,c in enumerate(ODGTF):
df[i] = ts[names.pop()][0:steps] if c > 0 else zeros(steps)
OUTDGT = df.to_numpy()

print(OUTDGT)

# OUTDGT replacement testing
# OUTDGT = ts[names][0:steps]
xkeys = ['OUTDGT1', 'OUTDGT2']
# xvalues = list(map(ts.get, xkeys))
xvalues = array(ts['OUTDGT1'],ts['OUTDGT2'])
# xvalues = array(map(ts.get, xkeys))
# xvalues = list(map(ts.get, names))
# print(names)
print(xvalues)
# print(xvalues.shape)
# print(xvalues[0][0])
# xvalues[0][0] = xvalues[0][0] * 2
# print("ts['OUTDGT1']", ts['OUTDGT1'][0])
# print("ts['OUTDGT2']", ts['OUTDGT2'][0])

# List all names in ts, for jk testing purposes only
# ts_names = list(sorted([n for n in ts], reverse=True))
# print(ts_names)

# generic SAVE table doesn't know nexits for output flows and rates
if nexits > 1:
u = uci['SAVE']
Expand Down Expand Up @@ -145,11 +126,33 @@ def hydr(io_manager, siminfo, uci, ts, ftables, specactions):
for i in range(nexits):
Olabels.append(f'O{i+1}')
OVOLlabels.append(f'OVOL{i+1}')

specactions = make_numba_dict(specactions) # Note: all values coverted to float automatically

# must split dicts out of state Dict since numba cannot handle mixed-type nested Dicts
# state_info is some generic things about the simulation
state_info = Dict.empty(key_type=types.unicode_type, value_type=types.unicode_type)
state_info['operation'], state_info['segment'], state_info['activity'] = state['operation'], state['segment'], state['activity']
state_info['domain'], state_info['state_step_hydr'] = state['domain'], state['state_step_hydr']
hsp2_local_py = state['hsp2_local_py']
# It appears necessary to load this here, instead of from main.py, otherwise,
# _hydr_() does not recognize the function state_step_hydr()?
if (hsp2_local_py != False):
from hsp2_local_py import state_step_hydr
else:
from HSP2.state_fn_defaults import state_step_hydr
state_ix, dict_ix, ts_ix = state['state_ix'], state['dict_ix'], state['ts_ix']
state_paths = state['state_paths']
# initialize the hydr paths in case they don't already reside here
hydr_init_ix(state_ix, state_paths, state['domain'])

###########################################################################
# specactions - special actions code TBD
###########################################################################
specactions = make_numba_dict(state['specactions']) # Note: all values coverted to float automatically

###########################################################################
# Do the simulation with _hydr_()
###########################################################################
errors = _hydr_(ui, ts, COLIND, OUTDGT, rchtab, funct, Olabels, OVOLlabels, specactions) # run reaches simulation code
# errors = _hydr_(ui, ts, COLIND, OUTDGT, rchtab, funct, Olabels, OVOLlabels) # run reaches simulation code
errors = _hydr_(ui, ts, COLIND, OUTDGT, rchtab, funct, Olabels, OVOLlabels, state_info, state_paths, state_ix, dict_ix, ts_ix, specactions, state_step_hydr) # run reaches simulation code
###########################################################################

if 'O' in ts: del ts['O']
Expand All @@ -164,7 +167,7 @@ def hydr(io_manager, siminfo, uci, ts, ftables, specactions):


@njit(cache=True)
def _hydr_(ui, ts, COLIND, OUTDGT, rowsFT, funct, Olabels, OVOLlabels, specactions):
def _hydr_(ui, ts, COLIND, OUTDGT, rowsFT, funct, Olabels, OVOLlabels, state_info, state_paths, state_ix, dict_ix, ts_ix, specactions, state_step_hydr):
errors = zeros(int(ui['errlen'])).astype(int64)

steps = int(ui['steps']) # number of simulation steps
Expand Down Expand Up @@ -290,38 +293,51 @@ def _hydr_(ui, ts, COLIND, OUTDGT, rowsFT, funct, Olabels, OVOLlabels, specactio
for index in range(nexits):
ui['OS' + str(index + 1)] = o[index]

# other initial vars
rovol = 0.0
volev = 0.0
IVOL0 = ts['IVOL'] # the actual inflow in simulation native units
# prepare for dynamic state
hydr_ix = hydr_get_ix(state_ix, state_paths, state_info['domain'])
# these are integer placeholders faster than calling the array look each timestep
o1_ix, o2_ix, o3_ix, ivol_ix = hydr_ix['O1'], hydr_ix['O2'], hydr_ix['O3'], hydr_ix['IVOL']
ro_ix, rovol_ix, volev_ix, vol_ix = hydr_ix['RO'], hydr_ix['ROVOL'], hydr_ix['VOLEV'], hydr_ix['VOL']
# handle varying length outdgt
out_ix = arange(nexits)
if nexits > 0:
out_ix[0] = o1_ix
if nexits > 1:
out_ix[1] = o2_ix
if nexits > 2:
out_ix[2] = o3_ix
# HYDR (except where noted)
for step in range(steps):
# print('\n', 'step: ', step, ' of: ', steps, ' steps')

# ------------------------------------------------------------------------
# print('Trying specl')
# OUTDGT2_save = ts['OUTDGT2'][step - 1] # save before calling specl()
# OUTDGT1_save = ts['OUTDGT1'][step - 1]
# print("OUTDGT[step, :]", OUTDGT[step, :])
# print("ro", ro)

# call specl
specl(ui, ts, step, specactions)
# print("ts['OUTDGT2'][step]", ts['OUTDGT2'][step])
# print("ts['OUTDGT1'][step]", ts['OUTDGT1'][step])
# print("OUTDGT[step, :]", OUTDGT[step, :])

# set OUTDGT using the values in the ts object which were set inside specl()
OUTDGT[step, :] = [ts['OUTDGT1'][step], ts['OUTDGT2'][step], 0.0]
# print("OUTDGT[step, :]", OUTDGT[step, :])
# ------------------------------------------------------------------------

specl(ui, ts, step, state_info, state_paths, state_ix, specactions)
convf = CONVF[step]
outdgt[:] = OUTDGT[step, :]
colind[:] = COLIND[step, :]
roseff = ro
oseff[:] = o[:]

# jk test prints
# print("roseff", roseff)
# print("outdgt[:]", outdgt[:])

# set state_ix with value of local state variables and/or needed vars
# Note: we pass IVOL0, not IVOL here since IVOL has been converted to different units
state_ix[ro_ix], state_ix[rovol_ix] = ro, rovol
di = 0
for oi in range(nexits):
state_ix[out_ix[oi]] = outdgt[oi]
state_ix[vol_ix], state_ix[ivol_ix] = vol, IVOL0[step]
state_ix[volev_ix] = volev
# Execute dynamic code if enabled
if (state_info['state_step_hydr'] == 'enabled'):
state_step_hydr(state_info, state_paths, state_ix, dict_ix, ts_ix, hydr_ix, step)
# Do write-backs for editable STATE variables
# OUTDGT is writeable
for oi in range(nexits):
outdgt[oi] = state_ix[out_ix[oi]]
# IVOL is writeable.
# Note: we must convert IVOL to the units expected in _hydr_
# maybe routines should do this, and this is not needed (but pass VFACT in state)
IVOL[step] = state_ix[ivol_ix] * VFACT
# vols, sas variables and their initializations not needed.
if irexit >= 0: # irrigation exit is set, zero based number
if rirwdl > 0.0: # equivalent to OVOL for the irrigation exit
Expand Down
33 changes: 3 additions & 30 deletions HSP2/SPECL.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,7 @@
from numba import njit

@njit
# def specl(io_manager, siminfo, uci, ts, step, specl_actions):
def specl(ui, ts, step, specactions):

# print('Made it to specl()')
ts = _specl_(ui, ts, step, specactions)

# return errors, ERRMSGS
# return ts



# def _specl_(ui, ts, COLIND, OUTDGT, rowsFT, funct, Olabels, OVOLlabels):
@njit
def _specl_(ui, ts, step, specactions):

# print('Made it to _specl_()')
# ts['VOL'][step - 1] = ts['VOL'][step - 1] * 5.0
# ts['VOL'][step - 1] = ts['VOL'][step - 1] - specactions['test_wd']

# ts['OUTDGT'][step - 1] = ts['OUTDGT'][step - 1] - specactions['test_wd']
# ts['OUTDGT2'][step - 1] = 99
# ts['OUTDGT2'][step - 1] = ts['OUTDGT2'][step - 1]
# ts['OUTDGT2'][step] = ts['OUTDGT2'][step - 1] + 99

ts['OUTDGT2'][step] = 99
# ts['OUTDGT2'][step, :] = 99 # this resulted in errors

# print(specactions['outdgt'])
# return errors
# return ts
def specl(ui, ts, step, state_info, state_paths, state_ix, specactions):
# ther eis no need for _specl_ because this code must already be njit
return

20 changes: 18 additions & 2 deletions HSP2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
from HSP2.utilities import versions, get_timeseries, expand_timeseries_names, save_timeseries, get_gener_timeseries
from HSP2.configuration import activities, noop, expand_masslinks
from HSP2.state import *

from HSP2IO.io import IOManager, SupportsReadTS, Category

Expand Down Expand Up @@ -56,6 +57,19 @@ def main(io_manager:IOManager, saveall:bool=False, jupyterlab:bool=True) -> None
copy_instances = {}
gener_instances = {}

#######################################################################################
# initilize STATE dicts
#######################################################################################
# Set up Things in state that will be used in all modular activitis like SPECL
state = init_state_dicts()
state_siminfo_hsp2(uci_obj, siminfo)
# Add support for dynamic functins to operate on STATE
# - Load any dynamic components if present, and store variables on objects
state_load_dynamics_hsp2(state, io_manager, siminfo)
# - finally stash specactions in state, not domain (segment) dependent so do it once
state['specactions'] = specactions # stash the specaction dict in state
#######################################################################################

# main processing loop
msg(1, f'Simulation Start: {start}, Stop: {stop}')
tscat = {}
Expand Down Expand Up @@ -99,7 +113,9 @@ def main(io_manager:IOManager, saveall:bool=False, jupyterlab:bool=True) -> None
continue

msg(3, f'{activity}')

# Set context for dynamic executables.
state_context_hsp2(state, operation, segment, activity)

ui = uci[(operation, activity, segment)] # ui is a dictionary
if operation == 'PERLND' and activity == 'SEDMNT':
# special exception here to make CSNOFG available
Expand Down Expand Up @@ -209,7 +225,7 @@ def main(io_manager:IOManager, saveall:bool=False, jupyterlab:bool=True) -> None
############ calls activity function like snow() ##############
if operation not in ['COPY','GENER']:
if (activity == 'HYDR'):
errors, errmessages = function(io_manager, siminfo, ui, ts, ftables, specactions)
errors, errmessages = function(io_manager, siminfo, ui, ts, ftables, state)
elif (activity != 'RQUAL'):
errors, errmessages = function(io_manager, siminfo, ui, ts)
else:
Expand Down
Loading