HARPgroup / HSPsquared

Hydrologic Simulation Program Python (HSPsquared)
GNU Affero General Public License v3.0
1 stars 0 forks source link

Dynamic python code #65

Closed rburghol closed 9 months ago

rburghol commented 1 year ago

Goal: have a set of functions defined by local code that are executable in @njit. see https://github.com/respec/HSPsquared/issues/113

Loading and Executing @njit code,

Since all functions called by njit code must be njit also, the dynamic functions must be defined before the first call of the larger routine, such as _hydr_().

Example Withdrawal

@njit def state_step_hydr(state_ix, dict_ix, ts_ix, hydr_ix, step): fn_defined = False state_ix[hydr_ix['O1']] = 0.0 1.547 if (step <= 1): print("Custom state_step_hydr() called") print("state at start", state_ix) if (step > 2440*364): print("state at end", state_ix) return

- Then run hsp2 and export hydr:

hsp2 hsp2 run PL3_5250_0001.h5 Rscript /opt/model/meta_model/scripts/h5/export_hsp_h5.R PL3_5250_0001.h5 hydr_10mgd.csv "//RESULTS/RCHRES_R001/HYDR/table"

- Then edit `PL3_5250_0001.py` and set the withdrawa (O1)l to 0.0 
- Then run and export again:

hsp2 hsp2 run PL3_5250_0001.h5 Rscript /opt/model/meta_model/scripts/h5/export_hsp_h5.R PL3_5250_0001.h5 hydr.csv "//RESULTS/RCHRES_R001/HYDR/table"

jdkleiner commented 1 year ago

Notes 6/21/23:

These notes have been incorporated into the body of this issue, and in a separate issue #67

rburghol commented 1 year ago

This allows you to test manually at the command line. More illuminating is the code in the body of the issue.


from numba.typed import Dict
from numba import types
import numpy as np
os.chdir("/opt/model/HSPsquared")
from HSP2.utilities_specl import *
from HSP2.SPECL import specl, _specl_
from HSP2.om_model_object import *
from HSP2.om_equation import *
from HSP2.om_data_matrix import *
from HSP2.om_sim_timer import *
from HSP2.om_model_linkage import ModelLinkage, step_model_link
from HSP2.om_model_broadcast import *

hydr_ix = Dict.empty(key_type=types.unicode_type, value_type=types.int64)
op_tokens = Dict.empty(key_type=types.int64, value_type=types.i8[:])
state_paths = Dict.empty(key_type=types.unicode_type, value_type=types.int64)
state_ix = Dict.empty(key_type=types.int64, value_type=types.float64)
dict_ix = Dict.empty(key_type=types.int64, value_type=types.float64[:,:])
ts_ix = Dict.empty(key_type=types.int64, value_type=types.float64[:])

local_path = os.getcwd()
fbase="PL3_5250_0001"
numdict = numdict=np.asarray([],dtype="float32")
io_manager, siminfo, op_tokens, state_paths, state_ix, dict_ix, ts_ix, model_object_cache = numdict,numdict,numdict,numdict,numdict,numdict,numdict,numdict

hsp2_local_py = dynamic_module_import(fbase, local_path + "/" + fbase + ".py", "hsp2_local_py")

# just set some test values
hydr_ix['O1'] = 1
from hsp2_local_py import state_step_hydr
state_step_hydr(numdict, op_tokens, state_ix, dict_ix, ts_ix, hydr_ix, 1)
# now show the values
state_ix[hydr_ix['O1']]

Sample code to put into a model [riverseg].py file.

import sys

print("Loaded a dynamic HSP2 code file!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

def om_init_model(io_manager, siminfo, op_tokens, state_paths, state_ix, dict_ix, ts_ix, model_object_cache):
    print("Initialize om_init_model from code")

@njit
def state_step_hydr(model_exec_list, op_tokens, state_ix, dict_ix, ts_ix, hydr_ix, step):
    print("Do something")

print("Loaded state_step_hydr()")
jdkleiner commented 1 year ago

@rburghol Couple notes when running the test above:

rburghol commented 1 year ago

Hey @jdkleiner -- sorry, the test code does suffer from the lack you describe :) -- dynamic_module_import() is in the HSP2/STATE.py file. However, you may find the actual embedded code in hsp2 more interesting. That is the code that is in the body of this issue at the top. Also, in the state branch now there is a sample file in tests/test10/test10.py that will function fully, and should run out of the box in that branch on your install, provided of course that there is no incompatibility that I did not foresee in a windows versus linux install?

rburghol commented 1 year ago
rburghol commented 9 months ago

Tested on: