{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setting Boundary Conditions on a Voronoi.\n", "\n", "
\n", "For more Landlab tutorials, click here: https://landlab.readthedocs.io/en/latest/user_guide/tutorials.html\n", "
\n", "\n", "### This tutorial illustrates how to modify the boundary conditions on a voronoi grid." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from landlab import VoronoiDelaunayGrid\n", "from landlab.plot.imshow import imshow_grid\n", "import numpy as np\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Instantiate a grid." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.seed(1234)\n", "x, y = np.random.rand(25), np.random.rand(25)\n", "vg = VoronoiDelaunayGrid(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The node boundary condition options are:\n", "\n", "- vg.BC_NODE_IS_CORE (status value = 0; all operations are performed on a mg.BC_NODE_IS_CORE)\n", "- vg.BC_NODE_IS_FIXED_VALUE (status value = 1; a boundary node with a fixed value)\n", "- vg.BC_NODE_IS_FIXED_GRADIENT (status value = 2; a boundary node with a fixed gradient)\n", "- vg.BC_NODE_IS_LOOPED (status value = 3; a boundary node that is wrap-around)\n", "- vg.BC_NODE_IS_CLOSED (status value = 4; closed boundary, or no flux can cross this node, or more accurately, can cross the faces around the node)\n", "\n", "(Note that these options are designed for the convenience in writing Landlab applications, and are not \"automatically enforced\" in internal Landlab functions. For example, if you add two node fields together, as in `my_field1 + my_field2`, *all* values will be added, not just core nodes; to take advantage of boundary coding, you would use a syntax like `my_field1[grid.core_nodes] + my_field2[grid.core_nodes]`.)\n", "\n", "Check the status of boundaries immediately after instantiating the grid:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vg.status_at_node" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default conditions are for the perimeter to be fixed value (status of 1) and the interior nodes to be core (status of 0).\n", "\n", "This is not actually much easier to see graphically, because the perimeter nodes are not colored. Note that they have a value of 1, so it seems they should be shown as purple, but the plotter does not color them because they don't have a defined cell size." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(vg, vg.status_at_node, cmap='cool', show_elements=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now suppose we want closed nodes on the boundary, instead of fixed value nodes.\n", "\n", "We can change this by accessing the nodes that are currently fixed value and changing their status." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vg.status_at_node[vg.status_at_node ==\n", " vg.BC_NODE_IS_FIXED_GRADIENT] = vg.BC_NODE_IS_CLOSED\n", "imshow_grid(vg,\n", " vg.status_at_node,\n", " cmap='cool',\n", " limits=(-0.01, 4),\n", " color_for_closed='red',\n", " show_elements=True)\n", "vg.status_at_node" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now all of the nodes that had a status value of 1 have a status value of 4, but again viewing this graphically is not very helpful. The array values have appropriately changed.\n", "\n", "Now let's instantiate a new grid and give it some elevation values. In this case any node that has an x value < 0.5 will have an elevation of -1. Othewise, the nodes have a value of 1." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.random.seed(4321)\n", "x, y = np.random.rand(25), np.random.rand(25)\n", "vg2 = VoronoiDelaunayGrid(x, y)\n", "vg2.add_ones('topographic__elevation', at='node')\n", "vg2.at_node['topographic__elevation'][vg2.x_of_node < 0.5] = -1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Illustrate the topography and show the values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "imshow_grid(vg2,\n", " vg2.at_node['topographic__elevation'],\n", " cmap='cool', \n", " show_elements=True)\n", "vg2.at_node['topographic__elevation']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can illustrate the grid and see at least some of the closed elements by using the imshow_grid option color_for_closed. Again remember that some of the nodes that are plotting in white areas are closed as well - that is, the ones that have an x value less than 0.5 are closed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vg2.set_nodata_nodes_to_closed(vg2.at_node['topographic__elevation'], -1.)\n", "imshow_grid(vg2,\n", " vg2.status_at_node,\n", " color_for_closed='red',\n", " cmap='cool',\n", " limits=(-0.01, 1), \n", " show_elements=True)\n", "vg2.status_at_node" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Click here for more Landlab tutorials" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.0" } }, "nbformat": 4, "nbformat_minor": 1 }