{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "56124a84-9241-4223-8b6a-6b597edb02df",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.sparse as ssp\n",
    "import scipy.sparse.linalg as sspla\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c8851736-5c0d-43bc-8eaf-7738f7896fd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def xc(i):\n",
    "    return (i%NxNy)%Nx\n",
    "def yc(i):\n",
    "    return (i%NxNy)//Nx\n",
    "def zc(i):\n",
    "    return i//NxNy\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "edafae36-06e9-4e31-8a60-abeb06264b8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "#el = np.loadtxt('./comp_det', dtype=int)\n",
    "#el = np.loadtxt('./comp_shu', dtype=int)\n",
    "#el = np.loadtxt('./comp_nhi', dtype=int)\n",
    "el = np.loadtxt('../composite/edgelist_0_seed1_0', dtype=int)\n",
    "steps = 3\n",
    "Nx = 2**steps\n",
    "Ny = Nx\n",
    "Nz = 2*(steps+2)-2\n",
    "NxNy = Nx*Ny\n",
    "N = NxNy*Nz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "51c78c6d-139c-4d9a-b58d-cdca843511e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "nhs = el//NxNy\n",
    "maxh = np.max(nhs)\n",
    "levs = (nhs[:,0]+nhs[:,1])/2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0e0489da-fd8a-485e-90ed-88717c4d4411",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5, 0.5, 0.5, ..., 6. , 6.5, 6.5], shape=(1024,))"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "levs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "04df0fec-a353-49dd-aef6-622369fad2d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "subnets = []\n",
    "for l in range(maxh+1):\n",
    "    subnets.append(el[levs==l])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "cbf72919-b5b1-4c4f-a5f5-20539e2fe334",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_layer(l):\n",
    "    f, ax = plt.subplots()\n",
    "    ax.set_aspect('equal')\n",
    "    el0 = subnets[l]\n",
    "    for e in el0:\n",
    "        x0,x1 = xc(e)\n",
    "        y0,y1 = yc(e)\n",
    "        if (x1==x0+1) and (y1==y0):\n",
    "            ax.plot([x0,x1],[y0,y1],'k-')\n",
    "        if (y1==y0+1) and (x1==x0):\n",
    "            ax.plot([x0,x1],[y0,y1],'k-')\n",
    "        #now pbc\n",
    "        if (x1==x0+Nx-1) and (y1==y0):\n",
    "            ax.plot([x1+1,x1],[y0,y1],'k-')\n",
    "        if (y1==y0+Nx-1) and (x1==x0):\n",
    "            ax.plot([x0,x1],[y1+1,y1],'k-')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "602aa246-8780-47f8-bedb-3d885e9921bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9420 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_layer(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "93028a58-1774-4bb1-9d63-f20f89d30a5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10356 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_layer(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "57b9ef9e-59fb-4c6e-a75e-3108a9b96ca3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10316 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_layer(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4c6cbdbb-7ecc-42dc-91d2-5a936d755508",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 10336 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_layer(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d283a28d-b65e-4e13-a588-cd7d81b47b0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9328 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_layer(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8ab0d0be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 9732 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\n",
    "plot_layer(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "96223e90-bc98-4c7d-b17e-d9e508c073aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "verticals = []\n",
    "for l in range(maxh):\n",
    "    verticals.append(el[levs==l+0.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "66ceef79-9186-4c6f-8bd7-9e1e1a097117",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_verticals(l):\n",
    "    f, ax = plt.subplots()\n",
    "    ax.set_aspect('equal')\n",
    "    el0 = verticals[l]\n",
    "    for e in el0:\n",
    "        x0,x1 = xc(e)\n",
    "        y0,y1 = yc(e)\n",
    "        if x0!=x1 or y0!=y1:\n",
    "            print('Error')\n",
    "        else:\n",
    "            ax.plot([x0],[y0], 'k.', ms=0.2)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "558bced5-9d0c-4738-95d6-276b3ecbb53f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8800 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_verticals(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "58fa4708-c12a-483f-92d8-f13d37931632",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4096, 4096, 4096, 4096, 4096, 4096, 3840, 4096, 4096, 4096, 4096, 4096, 4096]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[len(v) for v in verticals]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "18e37510-a164-43d4-8653-bc92080b0a3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "xx = xc(el)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8ea5d1d5-9c6e-46eb-9b0d-7bb728645dec",
   "metadata": {},
   "outputs": [],
   "source": [
    "yy = yc(el)\n",
    "zz = zc(el)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3a2780ea-cd9d-4bba-b05a-b842f0ff5110",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((zz[:,0]-zz[:,1])**2  >1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "139fced5-3684-4a94-97ea-3c0eeb4654c0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71fbaa1c-c8b4-45c6-92cb-ed7a459450d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mechnet_env",
   "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.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
