{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial-1\n",
    "\n",
    "The first thing to do to use the python wrappers is to import the package. **PyDealII** is only a shell and importing it will only allow you to call\n",
    "```python \n",
    "help(PyDealII)\n",
    "``` \n",
    "**PyDealII** is composed of two libraries: \n",
    " - **PyDealII.Debug** which uses the debug version of **deal.II**\n",
    " - **PyDealII.Release** which uses the release version of **deal.II**\n",
    " \n",
    "In this tutorial, we import the debug version of the library as **dealii**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import PyDealII.Debug as dealii"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start by creating a 2D **Triangulation** of an hyper cube and we globally refine it twice. You can read the documentation of **Triangulation** by typing:\n",
    "```python\n",
    "help(dealii.Triangulation)\n",
    "```\n",
    "This will show you all the functions associated with **Triangulation** and for each one, you will get a short explanation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "triangulation = dealii.Triangulation('2D')\n",
    "triangulation.generate_hyper_cube()\n",
    "triangulation.refine_global(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we would like to visualize the mesh that has been created. We can output a _vtu_ file using\n",
    "```python\n",
    "triangulation.write('triangulation.vtu', 'vtu')\n",
    "```\n",
    "and then use VisIt or Paraview. This is probably what you want to do for a 3D **Triangulation**. However, in this tutorial, we will create a function to plot the result using matplotlib. This will allow us to look at the mesh inside the notebook. To do that we will use _matplotlib_ and _numpy_."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.patches import Polygon\n",
    "from matplotlib.collections import PatchCollection\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The function below takes as input a **Triangulation** and a function that is used to define the color scheme. In this function, we loop over all the active cells, get the coordinates of the vertices, and use these coordinates to create polygons that we plot. We can loop over the active cells using\n",
    "```python\n",
    "for cell in triangulation.active_cells()\n",
    "```\n",
    "Once we have a cell, we can get any vertex using\n",
    "```python\n",
    "vertex = cell.get_vertex(i)\n",
    "```\n",
    "Since a _vertex_ is a **Point**, we can the coordinates of the **Point** using\n",
    "```python\n",
    "x = vertex.x\n",
    "y = vertex.y\n",
    "z = vertex.z\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_triangulation(triangulation, color_scheme):\n",
    "    fig, ax = plt.subplots()\n",
    "    patches = []\n",
    "    colors = []\n",
    "    cell_id = 0 \n",
    "    for cell in triangulation.active_cells():\n",
    "        quad_vertices = np.zeros((4,2))\n",
    "        # The shift variable is used to reorder the vertices because\n",
    "        # deal.II and matplotlib require different ordering\n",
    "        shift = [0,1,3,2]\n",
    "        for i in range(4):\n",
    "            vertex = cell.get_vertex(i)\n",
    "            quad_vertices[shift[i]][0] = vertex.x\n",
    "            quad_vertices[shift[i]][1] = vertex.y\n",
    "        quad = Polygon(quad_vertices, closed=True)\n",
    "        patches.append(quad)\n",
    "        colors.append(color_scheme(cell_id, cell))\n",
    "        cell_id += 1\n",
    "    p = PatchCollection(patches)  \n",
    "    p.set_array(np.array(colors))\n",
    "    ax.add_collection(p, autolim=True)\n",
    "    ax.autoscale_view()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We know define a color scheme function and plot the **Triangulation**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADltJREFUeJzt3V2MnOV5h/HrDw6JSPgo9UZB2M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cKuky55vAvZX1asAVfXyGs84al32XMC5/dPncfw3vk2UqnqEJb6RbsBu4CvVcwg4P8mFo7r/\n0znu6/GLubvsedCN9P6Xf5IN3XOSy4HNVfXttRxsFXV5nC8FLk3yaJJDSXau2XSro8uePwvckGSe\n3vdHfHptRhub5f57X5ZOX9YxJiP7Yu4J0nk/SW4ApoEPr+pEq++ke05yBnAX8Im1GmgNdHmcN9B7\nauYqev/v7N+SXFZV/73Ks62WLnveC9xTVX+V5DfofbvbZVX189UfbyxWtV+n85H7yL6Ye4J02TNJ\nrgFuA3ZV1c/WaLbVMmzP5wCXAQ8neY7ec5MzE/6iatef7W9V1VtV9SPgKL3YT6oue74RuA+gqr4H\nvIve72BpVad/7yt1Osd9PX4x99A995+i+DK9sE/687AwZM9V9VpVbayqrVW1ld7rDLuqanY8445E\nl5/tB+i9eE6SjfSepjm2plOOVpc9/xi4GiDJB+jFfWFNp1xbM8DH+++auRJ4rapeGtmtj/sV5SGv\nNl8H/JDeq+y39S+7g94/bug9+PcDc8C/A+8f98xrsOd/Af4LeKr/Z2bcM6/2nhetfZgJf7dMx8c5\nwF8DR4AfAHvGPfMa7Hk78Ci9d9I8BfzOuGc+xf1+HXgJeIveUfqNwCeBTw48xvv7/z1+MOqfaz+h\nKkkNOp2flpEkrZBxl6QGGXdJapBxl6QGGXdJapBxl6QGGXdJapBxl6QG/R/mmfWnndOcZwAAAABJ\nRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f9ef4ca20b8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def color_sc(cell_id, cell):\n",
    "    return cell_id\n",
    "\n",
    "plot_triangulation(triangulation, color_sc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's assume that the left half of the domain is composed of a different material than the right half of the domain. We will loop over all the cells and if the abscissa of the cell barycenter is less than 0.5, we will assign zero to the *material_id* of the cell. The others cells will be assigned a *material_id* of one.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADdlJREFUeJzt3V2InOd5h/Hrb6luaOvEpdrQoA/LoTJEmILN4roEGgc7\nRdaBdOIECUyaIiyS1umBQ8HFxQ3KUR1aQ0BtIlrjJhA7cg6SJSiobWrjYiJXa/wRS0ZhqzjRIlNv\nEtcnxrFF7h7MNAyjlebd1eyO9tH1g4V5Z56dvR/t6vLrmR1NqgpJUluumvQAkqTxM+6S1CDjLkkN\nMu6S1CDjLkkNMu6S1CDjLkkNMu6S1CDjLkkNWj+pL7xhw4baunXrpL68JK1Jzz333E+ramrUuonF\nfevWrczOzk7qy0vSmpTkx13W+bCMJDXIuEtSg4y7JDXIuEtSg4y7JDVoZNyTPJLk9SQvX+D2JPlS\nkrkkLyW5efxjSpKWosuZ+6PAjovcfiewrf+xH/jHSx9LknQpRsa9qp4Gfn6RJbuBr1bPMeDaJB8Y\n14CSpKUbx4uYNgJnBo7n+9e9NrwwyX56Z/ds2bJl2V/wY1d9fNmfK41y9OyLkx5Bjbvqd3+48l9j\nDPeRRa5b9F23q+pQVU1X1fTU1MhXz0qSlmkccZ8HNg8cbwLOjuF+JUnLNI64zwCf7P/WzK3Am1V1\n3kMykqTVM/Ix9ySPAbcBG5LMA38D/BpAVX0ZOALsBOaAt4A/XalhJUndjIx7Ve0dcXsBfz62iSRJ\nl8xXqEpSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXI\nuEtSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXIuEtSg4y7JDXIuEtS\ng4y7JDXIuEtSg4y7JDXIuEtSg4y7JDWoU9yT7EhyKslckvsXuX1LkieTPJ/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Aqnp9\nlWccty57LuC9/cvv4/x3fFtTquppFnlHugG7ga9WzzHg2iQfGNfXv5zjfiW+MXeXPQ/aR++//GvZ\nyD0nuQnYXFXfWc3BVlCX7/MNwA1JnklyLMmOVZtuZXTZ8+eBu5PM03v/iM+uzmgTs9S/70vS6c06\nJmRsb8y9hnTeT5K7gWngIys60cq76J6TXAU8DHxqtQZaBV2+z+vpPTRzG73/O/vPJDdW1f+u8Gwr\npcue9wKPVtXfJflDeu/udmNV/XLlx5uIFe3X5XzmPrY35l5DuuyZJHcADwC7quoXqzTbShm152uA\nG4GnkrxK77HJmTX+pGrXn+1vV9W7VfUj4BS92K9VXfa8DzgMUFXfB95D799gaVWnv+/LdTnH/Up8\nY+6Re+4/RPEVemFf64/Dwog9V9WbVbWhqrZW1VZ6zzPsqqrZyYw7Fl1+tr9F78lzkmyg9zDN6VWd\ncry67PknwO0AST5EL+4Lqzrl6poBPtn/rZlbgTer6rWx3fukn1Ee8WzzTuCH9J5lf6B/3QF6f7mh\n981/ApgD/gv44KRnXoU9/zvwP8AL/Y+ZSc+80nseWvsUa/y3ZTp+nwP8PXAS+AGwZ9Izr8KetwPP\n0PtNmheAP570zJe438eA14B36Z2l7wM+DXx64Ht8sP/n8YNx/1z7ClVJatDl/LCMJGmZjLskNci4\nS1KDjLskNci4S1KDjLskNci4S1KDjLskNej/APau5b7+ZeEuAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f9ee871d278>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for cell in triangulation.active_cells():\n",
    "    if cell.barycenter().x < 0.5:\n",
    "        cell.material_id = 0\n",
    "    else:\n",
    "        cell.material_id = 1\n",
    "\n",
    "plot_triangulation(triangulation, lambda cell_id,cell :  cell.material_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will refine isotropically the cells that have a *material_id* equal to zero and plot the **Triangulation**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADwtJREFUeJzt3V+MXGd9xvHvYzsbpxASFC8Sim2cqk6FFVUK3aapkEoQ\naeXkwr6hyJYiShVhQRsqFVQ1VaoUhauCWiQkF7BaREGCELgACxkFQYNSIUyzKBCwU7eLgWQb1JgQ\nUkjIHzu/XswUrTZjz9n17J7d19+PtNKcc96ded6d3cfHZ+bMSVUhSWrLhr4DSJImz3KXpAZZ7pLU\nIMtdkhpkuUtSgyx3SWqQ5S5JDbLcJalBlrskNWhTXw+8ZcuW2rFjR18PL0nr0re+9a2fVNX0uHG9\nlfuOHTuYnZ3t6+ElaV1K8qMu4zwsI0kNstwlqUGWuyQ1yHKXpAZZ7pLUoLHlnuRjSR5P8r2zbE+S\nDyWZS/JQktdNPqYkaSm67Ll/HNh9ju03ATuHXweAD59/LEnS+Rhb7lV1P/DTcwzZC3yiBo4Clyd5\n9aQCSpKWbhInMV0JPLpgeX647seLByY5wGDvnu3bty/7AT/7/d9e9veupMs3PNN3hJF+5+Jf9B1h\npFdsuKTvCCPt3nNL3xHWnRc3b+w7wrrylfvvWPHHmMQLqhmxbuRVt6vqUFXNVNXM9PTYs2clScs0\niXKfB7YtWN4KPDaB+5UkLdMkyv0w8Nbhu2auB56qqpcckpEkrZ6xx9yTfBq4AdiSZB74W+AigKr6\nCHAEuBmYA54B/mSlwkqSuhlb7lW1f8z2Av5sYokkSefNM1QlqUGWuyQ1yHKXpAZZ7pLUIMtdkhpk\nuUtSgyx3SWqQ5S5JDbLcJalBlrskNchyl6QGWe6S1CDLXZIaZLlLUoMsd0lqkOUuSQ2y3CWpQZa7\nJDXIcpekBo29hupa9IPnXtV3hJEu2/hM3xFGumLj031HGOnyDT/vO4LULPfcJalBlrskNchyl6QG\nWe6S1CDLXZ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O\n+vfaM1QlqUFr+bCMJGmZLHdJapDlLkkNstwlqUGWuyQ1yHKXpAZZ7pLUIMtdkhr0fyV8FuOMVRBw\nAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f9ee85f5dd8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for cell in triangulation.active_cells():\n",
    "    if cell.material_id == 0:\n",
    "        cell.refine_flag ='isotropic'\n",
    "triangulation.execute_coarsening_and_refinement()\n",
    "\n",
    "plot_triangulation(triangulation, color_sc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will now show how to merge two **Triangulations**. In order to merge the two **Triangulations**, we will need to move (shift) the second **Triangulation** such that it doesn't overlap with the first one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADyRJREFUeJzt3X+s3Xddx/Hni3YDxsYG9CJLf9BFi1AIOryWRSQON2O3\nP1ZNpmlRQLLQP3CIgRgmmqrjLyARQxzMBgk/onQDES6kMIkMMULn7mQUujpyU37sOpKVMYdKcHa8\n/eMcyMnpbc/33p57T8+nz0fS5HzP+dx73p+0e+57v/eee1JVSJLa8oRJDyBJGj/jLkkNMu6S1CDj\nLkkNMu6S1CDjLkkNMu6S1CDjLkkNMu6S1KD1k3riDRs21NatWyf19JI0le65557vVNXMqHUTi/vW\nrVuZn5+f1NNL0lRK8s0u67wsI0kNMu6S1CDjLkkNMu6S1CDjLkkNGhn3JO9N8lCSr57i8SR5Z5KF\nJIeTvGj8Y0qSlqPLmfv7gJ2nefwaYFv/z17g3Wc+liTpTIyMe1V9HvjuaZbsAj5QPYeAS5JcOq4B\nJUnLN44XMW0EHhg4Xuzf9+3hhUn20ju7Z8uWLSt+wh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m+Qe89et+V5NfWcMSx6/hv\n+9NV9T9V9R3g88C0f/N81J5fTe9SVFXVAvB14LlrOeNqqar/BD4H7Bx66McNS7IeuJje5agzdtbG\nvX/d6a+Bo1X156dYNge8sv9TM1cAj1bVt9dsyDHrsuckW4CPAq+Y9rM46LbnqrqsqrZW1VZ61yVf\nW1UfW8Mxx6rjv+2PAy9Nsj7JBcCL6V2nnkod9/wtel+pkOQngJ8Gjq3NhOOXZKZ/xk6SJwNXA/8+\ntGwOeFX/9vXAZ6v/3dUzdTZflnkJ8ArgK/1rVtD7bvoWgKq6ld5PTlwLLADfp/d//mnWZc/76F2T\ne1f/EsWJmu5futRlz60ZueeqOprk08Bh4IfAe4a+nJ82Xf6e3wK8L8lX6F2ueFP/q5ZpdSnw/iTr\n6J1I315Vn0xyMzBfVXP0/of3wSQL9M7Yd4/ryX2FqiQ16Ky9LCNJWjnjLkkNMu6S1CDjLkkNMu6S\n1CDjLkkNMu6S1CDjLkkN+n8frNamzekaCQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f9ee8591780>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "triangulation_2 = dealii.Triangulation('2D')\n",
    "triangulation_2.generate_hyper_cube()\n",
    "triangulation_2.refine_global(2)\n",
    "triangulation_2.shift([2.,0.])\n",
    "\n",
    "plot_triangulation(triangulation_2, color_sc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are now almost ready to merge the **Triangulations**. However, deal.II does not allow us to merge **Triangulations** that have been refined. We can use the *flatten_triangulation* function to create new **Triangulations** that are not refined but this function does not work if the mesh contains hanging nodes. Thus, we need to modify the first triangulation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAL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3FcOs5lWo7L9iT/kuRzvbn87oCa8WVYVW3KL1Y+XvhLwA8A24DPAfv6\nan4ZeHdv+TDwoUn3fRFzuRl416R77TCXnwCuBT5/nv03AR9j5elc1wEPTrrni5jL9cC9k+6zwzyu\nBK7tLV8OfHHA/19TcVw6zmVajkuAy3rLW4EHgev6asaWYZv5zL2lB3N3mctUqKpPMuApW6scAj5Q\nKx4Arkhy5cZ0tzYd5jIVquqrVfXp3vI3gVOc+5zjqTguHecyFXr/rf+rt7q199X/R86xZdhmDveW\nHszdZS4Ar+n9yvyRJLsG7J8GXec6LX6892v1x5L88KSbGab3a/01rJwlrjZ1x+UCc4EpOS5JZpJ8\nFngc+HhVnfe4jDrDNnO4j+zB3JtAlz7/BthTVS8H/o7/+2k+bablmHTxaVZu9f4R4A+Bj064nwtK\nchnwl8CvVdU3+ncP+JZNe1yGzGVqjktVfaeqfpSVZ0/vT/KyvpKxHZfNHO4jezD3JjB0LlX1RFU9\n3Vv9U+AVG9TbqHU5blOhqr7x7K/VtfI0sq1Jdky4rYGSbGUlDP+8qv5qQMnUHJdhc5mm4/KsqvpP\n4BPAgb5dY8uwzRzuLT2Ye+hc+q5/HmTlWuM0Og68rvfujOuAs1X11Uk3tR5JXvzs9c8k+1n59/LE\nZLs6V6/H9wCnquoPzlM2Fcely1ym6LjMJrmit/xc4AbgC31lY8uwTs9QnYRq6MHcHefy5iQHgWVW\n5nLzxBq+gCR3sfJuhR1JFoG3svKHIqrq3aw8a/cmYAF4Cnj9ZDodrsNcXgv8UpJl4FvA4U168vBK\n4OeBf+1d3wX4TWA3TN1x6TKXaTkuVwLvTzLDyg+ge6rq3o3KMO9QlaQGbebLMpKkdTLcJalBhrsk\nNchwl6QGGe6S1CDDXZIaZLhLUoMMd0lq0P8CHcfaEjU+8S4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f9ee84b3c18>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "flatten_triangulation_1 = dealii.Triangulation('2D')\n",
    "triangulation.generate_hyper_cube()\n",
    "triangulation.refine_global(2)\n",
    "triangulation.flatten_triangulation(flatten_triangulation_1)\n",
    "flatten_triangulation_2 = dealii.Triangulation('2D')\n",
    "triangulation_2.flatten_triangulation(flatten_triangulation_2)\n",
    "triangulation_3 = dealii.Triangulation('2D')\n",
    "triangulation_3.merge_triangulations(flatten_triangulation_1, flatten_triangulation_2)\n",
    "\n",
    "plot_triangulation(triangulation_3, color_sc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Like expected the second **Triangulation** is moved too far on the right. This mistake can easily fixed by moving *flatten_triangulation_2* to the left and merging the **Triangulations** once again. We can see the advantage of using python over C++. In C++, we would have to recompile and rerun the code while in python we can very easily fix our mistake."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAELxJREFUeJzt3XuMXGd9xvHvgx3H5MIleClREuOgmrYGUQVWKYWqhEuF\nE0TcqrRyVFSgKRYtoRcQUlCqFKV/tIU/qFDT0pQiCmoTAm2pi4wChSBUqEOWW0KSGoy5xDIiJqQB\nCiQYfv1jjmGYzHrP7s7sbF6+H2mVc3ln5snr42eP5+zsSVUhSWrLQ2YdQJI0eZa7JDXIcpekBlnu\nktQgy12SGmS5S1KDLHdJapDlLkkNstwlqUEbZ/XCW7ZsqW3bts3q5SXpQenjH//416pqbqlxMyv3\nbdu2sbCwMKuXl6QHpSRf6jPOt2UkqUGWuyQ1yHKXpAZZ7pLUIMtdkhq0ZLkneUuSu5J8ZpH9SfLG\nJAeT3JLkyZOPKUlajj5n7m8Fdp5g/4XA9u5rD/C3q48lSVqNJcu9qj4MfP0EQ3YBb6uB/cAjkpw5\nqYCSpOWbxIeYzgLuHFo/3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Aix3SWqQ5S5J\nDbLcJalBlrskNchyl6QGWe6S1CDLXZIa9P/ZadqaWVal0QAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f9ee84b3240>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "flatten_triangulation_2.shift([-1.,0])\n",
    "triangulation_3.merge_triangulations(flatten_triangulation_1, flatten_triangulation_2)\n",
    "\n",
    "plot_triangulation(triangulation_3, color_sc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we are done generating the grid, we need to save it in a format that will make it easy to load in our C++ code. This can  be done using the _save_ function python and then using the _Triangulation::load()_ function in C++. The only caveat is that **parallel::distributed::Triangulation** cannot load a grid which has refined cells. Once again this can be fixed by flattening the **Triangulation** (this is not necessary here)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "triangulation_3.save('merged_triangulation')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The C++ code to load the mesh is\n",
    "```C++\n",
    "triangulation.load('merged_triangulation');\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**NOTE**\n",
    "If the C++ code throws an exception, the error message will be shown. However, if the code segfaults then the kernel will simply be killed. In this case, the easiest way to debug your code is to use gdb to find the problem. This can be done by exporting your notebook as a python code and then typing:\n",
    "``` bash\n",
    "gdb python\n",
    "run my_program.py\n",
    "```\n",
    "Below, we show an example of an error message coming from the C++ code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "\n--------------------------------------------------------\nAn error occurred in line <443> of file </home/jovyan/work/dealii/contrib/python-bindings/source/cell_accessor_wrapper.cc> in function\n    dealii::python::PointWrapper dealii::python::CellAccessorWrapper::get_vertex(int) const\nThe violated condition was: \n    i<std::pow(2,dim)\nAdditional information: \n    Requested vertex number 5 does not exist. The largest vertex number acceptable is 3\n--------------------------------------------------------\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-12-93dcdab2ba83>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mcell\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtriangulation\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mactive_cells\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m     \u001b[0mvertex\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcell\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_vertex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m: \n--------------------------------------------------------\nAn error occurred in line <443> of file </home/jovyan/work/dealii/contrib/python-bindings/source/cell_accessor_wrapper.cc> in function\n    dealii::python::PointWrapper dealii::python::CellAccessorWrapper::get_vertex(int) const\nThe violated condition was: \n    i<std::pow(2,dim)\nAdditional information: \n    Requested vertex number 5 does not exist. The largest vertex number acceptable is 3\n--------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "for cell in triangulation.active_cells():\n",
    "    vertex = cell.get_vertex(5)"
   ]
  }
 ],
 "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.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
