Skip to content

Commit

Permalink
Make magdalena workflow work
Browse files Browse the repository at this point in the history
  • Loading branch information
priscavdsluis committed Nov 7, 2023
1 parent 93b64a1 commit b43aee5
Showing 1 changed file with 61 additions and 35 deletions.
96 changes: 61 additions & 35 deletions docs/tutorials/dsd_2023/magdalena_workflow/magdalena_answers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "d3a50766",
"metadata": {},
"source": [
"# Tutorial Delft3D FM 1D2D fluvial flood model of Magdalena river \n",
"# Creating a Delft3D FM 1D2D fluvial flood model of Magdalena river \n",
"\n",
"This notebook is an introduction on how to use [HYDROLIB-core](https://github.com/Deltares/HYDROLIB-core) functionalities to create and adjust a Delft3D FM Suite 1D2D fluvial flood model of the Magdalena river in Columbia. First a 1D river model of the Magdalena river and Canal del Dique will be created, shown in the left figure. This model will be extended with a 2D Flexible Mesh, with a uniform roughness field. Adding a DTM on that 2D mesh is currently not part of this tutorial, but normally this is of course indispensible to mimic the fluvial flood patterns. Running the model or importing the model in the Delft3D FM Suite 1D2D is not shown in this tutorial. \n",
"\n",
Expand Down Expand Up @@ -92,13 +92,17 @@
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import meshkernel as mk\n",
"\n",
"# I/O\n",
"from pathlib import Path\n",
"\n",
"# vector/polygon packages\n",
"import geopandas as gpd\n",
"\n",
"# mesh kerne\n",
"from meshkernel import MeshKernel, GeometryList, MakeGridParameters\n",
"\n",
"# HYDROLIB-core libraries\n",
"from hydrolib.core.dflowfm.mdu import FMModel, AutoStartOption\n",
"from hydrolib.core.dflowfm.net.models import *\n",
Expand Down Expand Up @@ -303,23 +307,23 @@
"outputs": [],
"source": [
"# Create a figure with the flowlinks of the model\n",
"figure, (ax1, ax2) = plt.subplots(ncols=2)\n",
"figure, ax1 = plt.subplots(ncols=1)\n",
"img = plt.imread(inputdir/\"osm_background.png\")\n",
"mesh1d_meshkernel = network._mesh1d._get_mesh1d()\n",
"\n",
"ax1.imshow(img, extent=[440000,531544.97964175534434617 ,1057000 ,1228644.01383191486820579 ])\n",
"mesh1d_meshkernel.plot_edges(ax=ax1, color='red')\n",
"\n",
"# Add some annotation of branches:\n",
"for index,branch in branches.iterrows():\n",
" middleIndex = int((len(branch['geometry'].coords) - 1)/2)\n",
" ax1.text(branch['geometry'].xy[0][middleIndex], branch['geometry'].xy[1][middleIndex], branch['Name'], ha='right')\n",
" \n",
"ax1.imshow(img, extent=[440000,\n",
" 531544.97964175534434617,\n",
" 1057000,\n",
" 1228644.01383191486820579])\n",
"mesh1d_meshkernel.plot_edges(ax=ax1, color='darkblue')\n",
"\n",
"\n",
"\n",
"# Make a second plot with a zoom-in view of the computational grid points near the junction\n",
"ax2.imshow(img, extent=[440000,531544.97964175534434617 ,1057000 ,1228644.01383191486820579 ])\n",
"ax2.set_xlim([500000, 525000])\n",
"ax2.set_ylim([1125000 ,1150000])\n",
"mesh1d_meshkernel.plot_edges(ax=ax2, color='red',marker='.',markeredgecolor='k',markersize=1)\n",
"\n"
]
},
Expand Down Expand Up @@ -890,15 +894,36 @@
"# Determine the boundary box\n",
"extent = gdf_grid.geometry[0].bounds\n",
"\n",
"# Create uniform rectilinear mesh\n",
"mesh2d = Mesh2d()\n",
"mesh_kernel = mesh2d.meshkernel\n",
"\n",
"xmin, ymin, xmax, ymax = extent\n",
"\n",
"dx=2500.0\n",
"dy=2500.0\n",
"\n",
"num_rows = int((ymax - ymin) / dy)\n",
"num_columns = int((xmax - xmin) / dx)\n",
"origin_x = xmin\n",
"origin_y = ymin\n",
"block_size_x = dx\n",
"block_size_y = dy\n",
"\n",
"make_grid_parameters = MakeGridParameters()\n",
"make_grid_parameters.num_columns = num_columns\n",
"make_grid_parameters.num_rows = num_rows\n",
"make_grid_parameters.origin_x = origin_x\n",
"make_grid_parameters.origin_y = origin_y\n",
"make_grid_parameters.block_size_x = dx\n",
"make_grid_parameters.block_size_y = dy\n",
"make_grid_parameters.upper_right_x = xmax\n",
"make_grid_parameters.upper_right_y = ymax\n",
"\n",
"# Answer EX7\n",
"mesh2d.create_rectilinear(\n",
" extent=extent, \n",
" dx=2500.0,\n",
" dy=2500.0\n",
")\n",
"# Create uniform rectilinear mesh\n",
"mesh_kernel.curvilinear_make_uniform(make_grid_parameters)\n",
"mesh_kernel.curvilinear_convert_to_mesh2d()\n",
"\n",
"mesh2d._process(mesh2d.get_mesh2d())\n",
"\n",
"# Clip resulting mesh within polygon area of interest\n",
"# Prepare a clipping polygon for MeshKernel input:\n",
Expand Down Expand Up @@ -957,29 +982,23 @@
"metadata": {},
"outputs": [],
"source": [
"# Create empty mesh2d object\n",
"mesh2d = Mesh2d()\n",
"\n",
"# Create rectangular grid\n",
"mesh2d.create_rectilinear(\n",
" extent=extent, \n",
" dx=2500.0,\n",
" dy=2500.0\n",
")\n",
"\n",
"# Clip resulting mesh within polygon area of interest\n",
"# Prepare a clipping polygon for MeshKernel input:\n",
"clip_polygon = np.array(gdf_grid.geometry[0].exterior.xy)\n",
"clip_geom = GeometryList(clip_polygon[0],clip_polygon[1])\n",
"mesh2d.clip(clip_geom)\n",
"\n",
"\n",
"# Refine the coarse uniform grid in a buffer region around the rivers\n",
"gdf_refine = gpd.read_file(inputdir / 'buffer_around_river_proj.shp')\n",
"refine_polygon = np.array(gdf_refine.geometry[0].exterior.xy)\n",
"refine_geom = GeometryList(refine_polygon[0], refine_polygon[1])\n",
"mesh2d.refine(refine_geom, level=1)\n",
"\n",
"parameters = mk.MeshRefinementParameters(\n",
" refine_intersected=True,\n",
" use_mass_center_when_refining=False,\n",
" min_edge_size=10.0,\n",
" refinement_type=1,\n",
" connect_hanging_nodes=True,\n",
" account_for_samples_outside_face=False,\n",
" max_refinement_iterations=1,\n",
")\n",
"\n",
"mesh_kernel.mesh2d_refine_based_on_polygon(refine_geom, parameters)\n",
"mesh2d._process(mesh2d.get_mesh2d())\n",
"fm.geometry.netfile.network._mesh2d = mesh2d\n",
"fm.geometry.netfile.filepath = \"FlowFM_1D2D_refined_net.nc\""
]
Expand Down Expand Up @@ -1030,6 +1049,13 @@
"fm.general.autostart = AutoStartOption.no\n",
"fm.save(recurse=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit b43aee5

Please sign in to comment.