In [47]:
import python_subgrid.wrapper
import logging
In [48]:
subgrid = python_subgrid.wrapper.SubgridWrapper('/home/fedor/Checkouts/models/brouwersdam/brouwersdam.mdu')
python_subgrid.wrapper.logger.setLevel(logging.WARN)
In [49]:
subgrid.start()
In [50]:
import python_subgrid.plotting
In [51]:
rows = []
for i in range(500):
s1 = subgrid.get_nd('s1')[320].copy()
t1 = subgrid.get_nd('t1').copy()
rows.append(dict(s1=s1, t1=t1, i=i))
subgrid.update(-1)
In [56]:
import pandas
df = pandas.DataFrame.from_records(rows)
plt.plot(df['t1']/3600.0, df['s1'])
Out[56]:
In [5]:
names = {'FlowElem_xcc', 'FlowElem_ycc',
'FlowLink_xu', 'FlowLink_yu', 's1',
'nod_type', 'link_type',
'FlowElemContour_x', 'FlowElemContour_y', 'x0p', 'x1p', 'imax',
'jmax', 'y0p', 'y1p', 'dxp', 'dps', 'dx'}
vars = {var: subgrid.get_nd(var) for var in names}
In [6]:
print vars['x1p'], vars['x0p'] + vars['imax']*vars['dxp']
In [7]:
# compute an index for all 2d cells
idx2d = vars['nod_type'] == 1
idx1d = vars['nod_type'] == 2
python_subgrid.wrapper.NODE_TYPES, idx2d.sum(), idx1d.sum()
Out[7]:
In [8]:
# compute the coordinates for the bathymetry
slice = np.s_[::4] # don't plot everything
x = np.arange(vars['x0p'], vars['x1p'], vars['dxp'])
y = np.arange(vars['y0p'], vars['y1p'], vars['dxp'])
In [34]:
orifices = subgrid.get_nd('orifices')
orifice = orifices.set_index('id').ix['3']
In [35]:
# plot everything
fig, ax = plt.subplots(figsize=(15,8))
X,Y=np.meshgrid(x[slice], y[slice])
print(X.shape, Y.shape)
#ax.pcolormesh(X,Y, -vars['dps'][:,:-1][slice, slice], vmin=-20, vmax=20, cmap='gist_earth', alpha=0.5)
ax.pcolorfast(x[slice],y[slice], -vars['dps'][:,:-1][slice, slice], vmin=-20, vmax=20, cmap='gist_earth', alpha=0.5)
ax.plot(vars['FlowElem_xcc'][idx2d], vars['FlowElem_ycc'][idx2d], 'k.', alpha=0.3)
#ax.plot(vars['FlowElem_xcc'][idx1d], vars['FlowElem_ycc'][idx1d], 'ro', alpha=0.3)
ax.plot(vars['FlowElemContour_x'].ravel(), vars['FlowElemContour_y'].ravel(), 'k+', alpha=0.3)
ax.axis('equal')
ax.set_xlim((45000,51000))
ax.set_ylim(417000, 424000)
for i, orifice in orifices.iterrows():
x0 = vars['FlowElem_xcc'][orifice['left_calc_point']]
y0 = vars['FlowElem_ycc'][orifice['left_calc_point']]
x1 = vars['FlowElem_xcc'][orifice['right_calc_point']]
y1 = vars['FlowElem_ycc'][orifice['right_calc_point']]
lx = vars['FlowLink_xu'][orifice['link_number']]
ly = vars['FlowLink_yu'][orifice['link_number']]
ax.plot([x0, x1], [y0, y1], 'r-', linewidth=3 )
#ax.annotate(orifice['id'],(lx, ly))
ax.text(lx, ly, orifice['id'].strip())
In [36]:
orifices[['left_calc_point', 'right_calc_point', 'link_number']]
Out[36]:
In [37]:
#this should work
subgrid.set_structure_field('orifices', orifice['id'], 'crest_level', -1)
subgrid.get_nd('orifices').set_index('id').ix[orifice['id']]['crest_level']
Out[37]:
In [38]:
orifices['crest_level']
Out[38]:
In [91]:
V = {}
for i in range(0, 300):
subgrid.update(-1)
V[i] = {var: subgrid.get_nd(var).copy() for var in {'q', 's1'}}
In [40]:
print(orifice)
# Lookup waterlevel and discharge for this structure
V[4]['s1'][orifice['left_calc_point']], V[4]['s1'][orifice['right_calc_point']], V[4]['q'][orifice['link_number']]
Out[40]:
In [41]:
quad_grid = python_subgrid.plotting.make_quad_grid(subgrid)
In [42]:
s1 = V[4]['s1'].copy()
s1[0] = -3
plt.imshow(s1[quad_grid[::4,::4].filled(0)], origin='lower', vmin=-2, vmax=2, cmap='gist_earth')
plt.colorbar()
Out[42]:
In [77]:
fig, axes = plt.subplots(1,3, figsize=(13,4))
nod_type = subgrid.get_nd('nod_type')
link_type = subgrid.get_nd('link_type')
axes[0].plot([v['s1'][nod_type == 1][orifice['left_calc_point']-1] for v in V.values()])
axes[1].plot([v['s1'][nod_type == 1][orifice['right_calc_point']-1] for v in V.values()])
axes[2].plot([v['q'][link_type >= 1][orifice['link_number']-1] for v in V.values()])
Out[77]:
In [45]:
import netCDF4
ds = netCDF4.Dataset('/home/fedor/Checkouts/models/brouwersdam/subgrid_map.nc')
x = ds.variables['FlowElem_xcc'][:]
y = ds.variables['FlowElem_ycc'][:]
plt.plot(x,y, 'k.')
plt.xlim(45000, 50000)
plt.ylim(400000, 430000)
for i, (x,y) in enumerate(zip(x,y)):
if 45000 < x < 50000 and 400000 < y < 430000:
plt.text(x,y, str(i))
In [46]:
t = netCDF4.num2date(ds.variables['time'][:], ds.variables['time'].units)
plt.plot(t, ds.variables['s1'][:,319])
Out[46]:
In [ ]: