There are many component models in Source, such as rainfall runoff models and routing models. Many of these can be run independently, outside of the context of a Source scenario.
There are various ways to do this from Python, some which use Veneer and some that don't.
This example notebook demonstrates how it can be done through Veneer.
In [1]:
import veneer
from veneer.manage import start, kill_all_now
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
Recent versions of Veneer include the option to start the command line without specifying a Source project.
In this case, the Veneer command line starts, and creates a new Source Project and Source Scenario 'in memory'. This option can be used to create a scenario (node, links, etc) entirely from Python. Alternatively (and in this example) we can simply ignore the newly created project and scenario and run individual models through Veneer
In [2]:
PATH_TO_VENEER='<path to FlowMatters.Source.VeneerCmd.exe, likely set up by veneer.manage.create_command_line>'
In [4]:
processes,ports = start(veneer_exe=PATH_TO_VENEER,debug=True,project_fn=None)
We will run the Simhyd rainfall runoff model. Simhyd requires rainfall and PET and the Source version of Simhyd has 9 parameters.
We need to know:
These are discoverable through the Veneer interface
In [5]:
v = veneer.Veneer(ports[0])
In [6]:
model_name = v.model.find_model_type('SimHydCs')[0]
model_name
Out[6]:
In [7]:
v.model.find_inputs(model_name)
Out[7]:
In [8]:
v.model.find_outputs(model_name)
Out[8]:
In [9]:
v.model.find_states(model_name)
Out[9]:
In [10]:
DEFAULT_PARAMETERS = v.model.find_default_parameters(model_name)
DEFAULT_PARAMETERS
Out[10]:
In [11]:
tbl = pd.read_csv('eg_climate.csv',index_col='date',parse_dates=True)
tbl[:10]
Out[11]:
In [12]:
tbl.index
Out[12]:
In [13]:
inputs = {
'rainfall':tbl['rain'],
'pet':tbl['evap']
}
In [14]:
things_to_record = [
'runoff',
'baseflow',
'SoilMoistureStore',
'Groundwater'
]
In [15]:
res = v.model.component.run_model(model_name,
inputs=inputs,
parameters=DEFAULT_PARAMETERS,
outputs=things_to_record)
In [16]:
res[:10]
Out[16]:
In [17]:
res['runoff'].plot()
Out[17]:
In [18]:
kill_all_now(processes)
In [ ]: