{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "05de63af", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import geopandas as gpd\n", "from numba import jit \n", "from numba.experimental import jitclass\n", "from numba.typed import List\n", "import xarray as xr\n", "\n", "import source.riversection as sectorg\n", "import source.s1driverflow as model" ] }, { "cell_type": "code", "execution_count": 3, "id": "b81c9ed1", "metadata": {}, "outputs": [], "source": [ "def makeSect(classsect, gdfsorted):\n", " \n", " def from3Dto2D(pointz, porg=None):\n", " point = pointz[:,:2]\n", " if porg is None : porg = pointz[0]\n", " v = point[-1] - point[0]\n", " e = v/np.linalg.norm(v)\n", " d = point - porg[:2]\n", " L = np.dot(d, e)\n", " Z = pointz[:,2]\n", " return L, Z\n", " \n", " channel = List()\n", " for calc, dist in zip( gdfsorted['calc-input'].values, gdfsorted['distancefromDB'].values ):\n", " \n", " typed_ps = List()\n", " typed_ns = List()\n", " for i, c in enumerate(calc['point-data']):\n", " p3d = np.array( c['coordinates'] )\n", " n = np.array( c['manning'] )\n", " if len(n) == 1 : n = np.repeat(n, (len(p3d) - 1))\n", " \n", " if i == 0 : porg = p3d[0]\n", " \n", " L, Z = from3Dto2D(p3d, porg)\n", " p = np.c_[L, Z]\n", " \n", " typed_ps.append(p)\n", " typed_ns.append(n)\n", " \n", " channel.append( classsect.section(typed_ps, typed_ns, dist) )\n", " \n", " return channel" ] }, { "cell_type": "markdown", "id": "df524cf9", "metadata": {}, "source": [ "# read section data" ] }, { "cell_type": "code", "execution_count": 4, "id": "f1aac9e0", "metadata": {}, "outputs": [], "source": [ "fin = '8909090001CalcData.geojson'\n", "gdfsect = gpd.read_file(fin)\n", "\n", "# Since the downstream end is at point 3.000, the data downstream of that point will be deleted.\n", "val = gdfsect[gdfsect['name'] == '3.000']['distancefromDB'].values[0]\n", "gdfsect = gdfsect[gdfsect['distancefromDB'] >= val]\n", "gdfsect = gdfsect.reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": 5, "id": "d07b5de6", "metadata": {}, "outputs": [], "source": [ "gdfsectsorted = gdfsect.sort_values('distancefromDB', ascending=True)\n", "chD2U = makeSect(sectorg, gdfsectsorted)\n", "\n", "gdfsectsorted = gdfsect.sort_values('distancefromDB', ascending=False)\n", "chU2D = makeSect(sectorg, gdfsectsorted)" ] }, { "cell_type": "markdown", "id": "e8afe5bc", "metadata": {}, "source": [ "# read upstream boundary Q and downstream boundary H data" ] }, { "cell_type": "code", "execution_count": 6, "id": "5e3c1d88", "metadata": {}, "outputs": [], "source": [ "dfhydro = pd.read_csv('hydrologicData.csv',index_col='date',parse_dates=True)\n", "\n", "dt = float(10)\n", "dfhydroip = dfhydro.resample(str(dt) + 'S').interpolate()\n", "Qup = dfhydroip['代継橋_時間流量'].values\n", "Hdown = dfhydroip['小島_時間水位'].values" ] }, { "cell_type": "markdown", "id": "66d15bb7", "metadata": {}, "source": [ "# set initial condition " ] }, { "cell_type": "code", "execution_count": 7, "id": "ef69047a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wall time: 9.41 s\n" ] } ], "source": [ "%%time\n", "# nonuniform flow\n", "Qt = np.full(len(chD2U), Qup[0], dtype=np.float64)\n", "Huni = model.NonUniformflow(chD2U, Qt, Hdown[0])\n", "Auni = np.array( [chD2U[i].H2ABS(hh)[0] for i, hh in enumerate(Huni)] )\n", "A0 = Auni[0]\n", "\n", "# unsteady flow : 20hr\n", "Q = np.full_like(Auni, Qup[0])\n", "A, H = Auni[::-1], Huni[::-1]\n", "for n in range(1, int(3600*20/dt)):\n", " A, Q, H = model.UnSteadyflow(chU2D, A, Q, H, A0, Qup[0], dt)" ] }, { "cell_type": "markdown", "id": "fdfe2152", "metadata": {}, "source": [ "# main simulation" ] }, { "cell_type": "code", "execution_count": 8, "id": "8193b24a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Wall time: 14.4 s\n" ] } ], "source": [ "%%time\n", "nmax = len(chU2D)\n", "ntmax = len(Qup)\n", "Amat, Qmat, Hmat = np.zeros((ntmax, nmax)), np.zeros((ntmax, nmax)), np.zeros((ntmax, nmax))\n", "\n", "Amat[0], Qmat[0], Hmat[0] = A, Q, H\n", "\n", "for n in range(1, ntmax):\n", " A0, _, _ = chU2D[-1].H2ABS(Hdown[n])\n", " A, Q, H = model.UnSteadyflow(chU2D, A, Q, H, A0, Qup[n], dt)\n", " Amat[n], Qmat[n], Hmat[n] = A, Q, H" ] }, { "cell_type": "markdown", "id": "8a4639b5", "metadata": {}, "source": [ "# export" ] }, { "cell_type": "code", "execution_count": 13, "id": "7d55bfae", "metadata": {}, "outputs": [], "source": [ "gdfsectsorted = gdfsect.sort_values('distancefromDB', ascending=False)\n", "\n", "ds = xr.Dataset( {'A': (['t','x'], Amat)\n", " ,'Q': (['t','x'], Qmat)\n", " ,'H': (['t','x'], Hmat)\n", " ,'zbmin': (['x'], [c.zbmin() for c in chU2D] )\n", " }\n", " , coords={'x':gdfsectsorted['distancefromDB'].values\n", " , 't':dfhydroip.index.values} \n", " ,attrs = {'name':gdfsectsorted['name'].values.tolist()}\n", " )\n", "\n", "dout = ds.to_netcdf(r'calout.nc')\n", "del dout" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }