In [1]:
import pandas
import faulthandler
# Now let's generate the same thing from memory
import python_subgrid.wrapper
import numpy as np
import matplotlib.pyplot as plt

In [2]:
subgrid = python_subgrid.wrapper.SubgridWrapper(mdu='../../models/1d-democase/1D-democase.mdu')
subgrid.start()
#subgrid.library.initmodel()
subgrid.update(10.0)                           
# We are now at t
print(subgrid.get_nd('t1'))


INFO:python_subgrid.wrapper:Using subgrid fortran library /home/fedor/.local/lib/libsubgrid.so
INFO:python_subgrid.wrapper:Loading library from path /home/fedor/.local/lib/libsubgrid.so
INFO:python_subgrid.wrapper:Current version: Deltares, 3Di - subgrid Version 0.1.1.957:960M, Dec 17 2013, 18:02:12
INFO:python_subgrid.wrapper:Startup complete.
INFO:python_subgrid.wrapper:Loading model ../../models/1d-democase/1D-democase.mdu in directory /home/fedor/Checkouts/models/1d-democase
INFO:python_subgrid.wrapper:Start loadModel for '../../models/1d-democase/1D-democase.mdu'.
WARNING:python_subgrid.wrapper:Ignored 'Teta' in MDU file, please use 'IntegrationMethod' instead.
INFO:python_subgrid.wrapper:Wrong NumLayers. Ground water calculations switched off.
INFO:python_subgrid.wrapper:Reading bathmetry file ' subgrid/zak_drieharing_100x100.tif '...
DEBUG:python_subgrid.wrapper:           0   4.08039987E-02  dps(0:imax+1, 0:jmax+1)
INFO:python_subgrid.wrapper:Completed reading bathymetry file.
INFO:python_subgrid.wrapper:Start initialization of grid administration.
INFO:python_subgrid.wrapper: Coarse grid parameters: kmax=           1 , dxmin=   10.0000000
DEBUG:python_subgrid.wrapper:           0   3.99999991E-02  lg(1:imax,1:jmax)
INFO:python_subgrid.wrapper:lg init on coarsest level took:   0.1000D-02 seconds.
INFO:python_subgrid.wrapper:guess dams took:   0.1000D-02 seconds.
INFO:python_subgrid.wrapper:Searching refinement areas for quadtrees generation took:  0.3000D-02 seconds.
INFO:python_subgrid.wrapper:Making quadtrees took:   0.6000D-02 seconds.
INFO:python_subgrid.wrapper:Opened file : boundaries/bound.ext
INFO:python_subgrid.wrapper: Internal 2d nodes =       100
INFO:python_subgrid.wrapper: Boundary 2d nodes =   0
DEBUG:python_subgrid.wrapper: make_channel_network: k1dmax=          11 , k1dtot=           7
INFO:python_subgrid.wrapper:Opened file : boundaries/bound.ext
INFO:python_subgrid.wrapper:Opened file : boundaries/bound_wl_up.pli
INFO:python_subgrid.wrapper:waterlevelbnd boundaries/bound_wl_up.pli       1 nr of open bndcells
INFO:python_subgrid.wrapper:Opened file : boundaries/bound_wl_down.pli
INFO:python_subgrid.wrapper:waterlevelbnd boundaries/bound_wl_down.pli       1 nr of open bndcells
INFO:python_subgrid.wrapper: Internal 1d nodes =        67
INFO:python_subgrid.wrapper: Boundary 1d nodes =   2
DEBUG:python_subgrid.wrapper:           0   1.35999999E-03  nru,nrv
INFO:python_subgrid.wrapper:Opened file : boundaries/bound.ext
INFO:python_subgrid.wrapper:Initializing external forcing 'waterlevelbnd' from file 'boundaries/bound_wl_up.pli'.
INFO:python_subgrid.wrapper:Opened file : boundaries/bound_wl_up.pli
INFO:python_subgrid.wrapper:Opened file : boundaries/bound_wl_up_0001.tim
INFO:python_subgrid.wrapper:Initializing external forcing 'waterlevelbnd' from file 'boundaries/bound_wl_down.pli'.
INFO:python_subgrid.wrapper:Opened file : boundaries/bound_wl_down.pli
INFO:python_subgrid.wrapper:Opened file : boundaries/bound_wl_down_0001.tim
INFO:python_subgrid.wrapper:Initializing boundaries took:   0.4000D-02 seconds.
INFO:python_subgrid.wrapper:Assignment of flooding thresholds took:   0.1120D+00 seconds.
INFO:python_subgrid.wrapper:Grid Admin initialisation took:  0.1130D+00 seconds.
INFO:python_subgrid.wrapper:Completed initialization of grid administration.
INFO:python_subgrid.wrapper:Calculation of bathymetry statistics took:   0.9000D-02 seconds.
INFO:python_subgrid.wrapper:Setting initial water levels  from WaterLevelFile took:   0.0000D+00 seconds.
INFO:python_subgrid.wrapper:Setting initial water levels on 1d links from WaterLevelFile took:   0.1000D-02 seconds.
INFO:python_subgrid.wrapper:Initial floodfill from FloodFill file took:   0.0000D+00 seconds.
INFO:python_subgrid.wrapper:loadModel complete.
INFO:python_subgrid.wrapper:Start timestep-update.
DEBUG:python_subgrid.wrapper: Trying to use timestep:    10.000000000000000
DEBUG:python_subgrid.wrapper:Start initFlowAdmin, flow_init=              3
INFO:python_subgrid.wrapper:reduce time:       0.001000
INFO:python_subgrid.wrapper: noactive=       167 nogauss=       135 nocg        32
DEBUG:python_subgrid.wrapper:           0   2.70799990E-03  vqt,denom(0:4*nodall),fbq(0:4*nodall),umq(0:4*nodall)
DEBUG:python_subgrid.wrapper:           0   1.01699994E-03  qs,qs1,qs2(0:2*nodall)
DEBUG:python_subgrid.wrapper:           0   1.24999997E-03  cu,ru(linall)
INFO:python_subgrid.wrapper:Initialization of Flow Values took:   0.5000D-02 seconds.
DEBUG:python_subgrid.wrapper:Completed initFlowAdmin.
INFO:python_subgrid.wrapper:finding rain data for input
INFO:python_subgrid.wrapper:Timestep 1 number of iterations 1 epsmax   0.0000E+00 idthalf 1  np = 1
INFO:python_subgrid.wrapper:Timestep complete.
60.0

In [3]:
# Let's get the waterlevel array
s = subgrid.get_nd('s1') # on the cells
# Let's get the velocity array
u = subgrid.get_nd('u1') # on the links

In [4]:
# And the corresponding link types
link_type = subgrid.get_nd('link_type')
# And the node types
nod_type = subgrid.get_nd('nod_type')


# Notice that this array is 1 longer than the netCDF file
u.shape, link_type.shape, s.shape, nod_type.shape


Out[4]:
((251,), (251,), (170,), (170,))

In [5]:
nod_branch = subgrid.get_nd('nod_branchid')
nod_chainage = subgrid.get_nd('nod_chainage')
nod_idx = subgrid.get_nd('nod_idx')
link_branch = subgrid.get_nd('link_branchid')
link_chainage = subgrid.get_nd('link_chainage')
link_idx = subgrid.get_nd('link_idx')


nod_df = pandas.DataFrame(data=dict(branch=nod_branch,
                           chainage=nod_chainage,
                           idx=nod_idx))
link_df = pandas.DataFrame(data=dict(branch=link_branch,
                           chainage=link_chainage,
                           idx=link_idx))


DEBUG:python_subgrid.wrapper:copying nod_chainage, memory will be reallocated
DEBUG:python_subgrid.wrapper:copying nod_idx, memory will be reallocated
DEBUG:python_subgrid.wrapper:copying link_branchid, memory will be reallocated
DEBUG:python_subgrid.wrapper:copying link_chainage, memory will be reallocated
DEBUG:python_subgrid.wrapper:copying link_idx, memory will be reallocated

In [6]:
subgrid.get_nd('FlowLink_xu')


Out[6]:
array([  6.92213716e-310,   1.36370000e+005,   1.36370000e+005,
         1.36370000e+005,   1.36370000e+005,   1.36370000e+005,
         1.36370000e+005,   1.36370000e+005,   1.36370000e+005,
         1.36370000e+005,   1.36370000e+005,   1.36380000e+005,
         1.36380000e+005,   1.36380000e+005,   1.36380000e+005,
         1.36380000e+005,   1.36380000e+005,   1.36380000e+005,
         1.36380000e+005,   1.36380000e+005,   1.36380000e+005,
         1.36390000e+005,   1.36390000e+005,   1.36390000e+005,
         1.36390000e+005,   1.36390000e+005,   1.36390000e+005,
         1.36390000e+005,   1.36390000e+005,   1.36390000e+005,
         1.36390000e+005,   1.36400000e+005,   1.36400000e+005,
         1.36400000e+005,   1.36400000e+005,   1.36400000e+005,
         1.36400000e+005,   1.36400000e+005,   1.36400000e+005,
         1.36400000e+005,   1.36400000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36420000e+005,   1.36420000e+005,   1.36420000e+005,
         1.36420000e+005,   1.36420000e+005,   1.36420000e+005,
         1.36420000e+005,   1.36420000e+005,   1.36420000e+005,
         1.36420000e+005,   1.36430000e+005,   1.36430000e+005,
         1.36430000e+005,   1.36430000e+005,   1.36430000e+005,
         1.36430000e+005,   1.36430000e+005,   1.36430000e+005,
         1.36430000e+005,   1.36430000e+005,   1.36440000e+005,
         1.36440000e+005,   1.36440000e+005,   1.36440000e+005,
         1.36440000e+005,   1.36440000e+005,   1.36440000e+005,
         1.36440000e+005,   1.36440000e+005,   1.36440000e+005,
         1.36450000e+005,   1.36450000e+005,   1.36450000e+005,
         1.36450000e+005,   1.36450000e+005,   1.36450000e+005,
         1.36450000e+005,   1.36450000e+005,   1.36450000e+005,
         1.36450000e+005,   1.36365000e+005,   1.36375000e+005,
         1.36385000e+005,   1.36395000e+005,   1.36405000e+005,
         1.36415000e+005,   1.36425000e+005,   1.36435000e+005,
         1.36445000e+005,   1.36455000e+005,   1.36365000e+005,
         1.36375000e+005,   1.36385000e+005,   1.36395000e+005,
         1.36405000e+005,   1.36415000e+005,   1.36425000e+005,
         1.36435000e+005,   1.36445000e+005,   1.36455000e+005,
         1.36365000e+005,   1.36375000e+005,   1.36385000e+005,
         1.36395000e+005,   1.36405000e+005,   1.36415000e+005,
         1.36425000e+005,   1.36435000e+005,   1.36445000e+005,
         1.36455000e+005,   1.36365000e+005,   1.36375000e+005,
         1.36385000e+005,   1.36395000e+005,   1.36405000e+005,
         1.36415000e+005,   1.36425000e+005,   1.36435000e+005,
         1.36445000e+005,   1.36455000e+005,   1.36365000e+005,
         1.36375000e+005,   1.36385000e+005,   1.36395000e+005,
         1.36405000e+005,   1.36415000e+005,   1.36425000e+005,
         1.36435000e+005,   1.36445000e+005,   1.36455000e+005,
         1.36365000e+005,   1.36375000e+005,   1.36385000e+005,
         1.36395000e+005,   1.36405000e+005,   1.36415000e+005,
         1.36425000e+005,   1.36435000e+005,   1.36445000e+005,
         1.36455000e+005,   1.36365000e+005,   1.36375000e+005,
         1.36385000e+005,   1.36395000e+005,   1.36405000e+005,
         1.36415000e+005,   1.36425000e+005,   1.36435000e+005,
         1.36445000e+005,   1.36455000e+005,   1.36365000e+005,
         1.36375000e+005,   1.36385000e+005,   1.36395000e+005,
         1.36405000e+005,   1.36415000e+005,   1.36425000e+005,
         1.36435000e+005,   1.36445000e+005,   1.36455000e+005,
         1.36365000e+005,   1.36375000e+005,   1.36385000e+005,
         1.36395000e+005,   1.36405000e+005,   1.36415000e+005,
         1.36425000e+005,   1.36435000e+005,   1.36445000e+005,
         1.36455000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36411250e+005,   1.36413750e+005,
         1.36416250e+005,   1.36418750e+005,   1.36421250e+005,
         1.36423750e+005,   1.36426250e+005,   1.36428750e+005,
         1.36431250e+005,   1.36433750e+005,   1.36433750e+005,
         1.36431250e+005,   1.36428750e+005,   1.36426250e+005,
         1.36423750e+005,   1.36421250e+005,   1.36418750e+005,
         1.36416250e+005,   1.36413750e+005,   1.36411250e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36410000e+005,   1.36410000e+005,   1.36410000e+005,
         1.36408750e+005,   1.36406250e+005,   1.36403750e+005,
         1.36401250e+005,   1.36398750e+005,   1.36396250e+005,
         1.36393750e+005,   1.36391250e+005,   1.36388750e+005,
         1.36386250e+005,   1.36432500e+005,   1.36427500e+005,
         1.36422500e+005,   1.36417500e+005,   1.36412500e+005,
         1.36407500e+005,   1.36402500e+005,   1.36397500e+005,
         1.36392500e+005,   1.36387500e+005,   1.36386250e+005,
         1.36388750e+005,   1.36391250e+005,   1.36393750e+005,
         1.36396250e+005,   1.36398750e+005,   1.36401250e+005,
         1.36403750e+005,   1.36406250e+005,   1.36408750e+005,
         1.36410000e+005,   1.36410000e+005])

In [7]:
link_df['type'] = np.take(link_type, link_df.idx)
link_df['u'] = np.take(u, link_df.idx)

link_x = subgrid.get_nd('FlowLink_xu')
link_y = subgrid.get_nd('FlowLink_yu')

link_df['x'] = np.take(link_x, link_df.idx)
link_df['y'] = np.take(link_y, link_df.idx)
# the first element is a 
python_subgrid.wrapper.LINK_TYPES[link_type[0]]


Out[7]:
'internal boundary'

In [8]:
plt.scatter(link_df.x, link_df.y, c=link_df.u[:], vmin=0, vmax=0.1)


Out[8]:
<matplotlib.collections.PathCollection at 0x3b60ad0>

In [9]:
plt.scatter(link_df.x, link_df.y, c=link_df.u.abs(), cmap='Set1', vmin=-link_df.u.abs().max(), vmax=link_df.u.abs().max(), s=100)
plt.colorbar()
plt.title('u1 velocity')


Out[9]:
<matplotlib.text.Text at 0x383ca10>

In [10]:
nod_x = subgrid.get_nd('FlowElem_xcc')
nod_y = subgrid.get_nd('FlowElem_ycc')

nod_df['type'] = np.take(nod_type, nod_df.idx)
nod_df['u'] = np.take(u, nod_df.idx)

nod_df['x'] = np.take(nod_x, nod_df.idx)
nod_df['y'] = np.take(nod_y, nod_df.idx)
# the first element is a 
python_subgrid.wrapper.LINK_TYPES[nod_type[0]]


Out[10]:
'internal boundary'