HARPgroup / HSPsquared

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

modelObject: base class for object that has logging capabilities #37

Open rburghol opened 1 year ago

rburghol commented 1 year ago

Tasks:

hdf5 Data Model

Main data storage

class modelObject:
    name = "" # Ex: "Qin" 
    parent = False # will be a link to another object
    log_path = "" # Ex: "/RESULTS/RCHRES_001/SPECL" 
    attribute_path = "/OBJECTS/RCHRES_001" # 
    state_path = "" # Ex: "/STATE/RCHRES_001" # the pointer to this object state
    inputs = {} # associative array with key=local_variable_name, value=hdf5_path_of_source_variable_state
                      # like: [ 'Qin' : '/STATE/RCHRES_001/IVOL' ]

    def state_path(self):
        if not (self.parent == False):
            return self.parent.state_path() + "/" + self.name
        return "/STATE/" + self.name

    def find_var_path(self, var_name):
        if var_name in self.inputs.keys():
            return self.inputs[var_name]
        if not (self.parent == False):
            return self.parent.find_var_path(var_name)
        return False

    def add_input(self, var_name, var_path):
        # this will add to the inputs, but also insure that this 
        # requested path gets added to the state/exec stack via an input object if it does 
        # not already exist.

test = modelObject()
test.ncode_preStep() # generate the numba code for preStep() actions.

# ['ts["/RESULTS/RCHRES_002/SPECL/wd_mgd"][step] += ts["/RESULTS/RCHRES_001/SPECL/ps_refill_pump_mgd"][step]', 'ts["/RESULTS/RCHRES_002/SPECL/Qtrib"][step] += ts["/RESULTS/RCHRES_001/SPECL/Qout"][step]']
rburghol commented 1 year ago

Must load code from:

ts_ix = Dict.empty(key_type=types.int64, value_type=types.float32[:])
tsq = f['TIMESERIES/']
ts_ts = np.asarray(tsq['TS011']['table']['index']) # index of timestamps for each timestep. use known TS for this demo
# this code replicates a piece of the function of get_timeseries, and loads the full timeseries into a Dict
# the dict will be keyed by a simple integer, and have values at the time step only.  The actual time 
# at that step will be contained in ts_ts 
ts_tables = list(tsq.keys())
for i in ts_tables:
    if i == 'SUMMARY': continue # skip this non-numeric table
    var_path = '/TIMESERIES/' + i
    ix = set_state(state_ix, state_paths, var_path, 0.0)
    ts_ix[ix] = np.asarray(tsq[i]['table']['values'], dtype="float32")

op_tokens, state_paths, state_ix, dict_ix = init_sim_dicts()
river = ModelObject('JU3_8900_0001')
river.state_path = specl_state_path('RCHRES', 1)
river.register_path()

river.add_input("Qin", f'{river.state_path}/HYDR/IVOL')
# alternative, using TIMESERIES: 
# river.inputs["Qin"] = ["/TIMESERIES/TS011"]
# river.add_input("ps_mgd", "/TIMESERIES/TS3000")

facility = ModelObject('facility', river)
facility.make_state_path()
facility.register_path()

Qintake = Equation('Qintake', facility, "Qin * 1.21")
Qintake.make_state_path()
Qintake.register_path()
Qintake.tokenize()