<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
<commit_msg>example for variable diffusivity added<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fickian diffusion with variable diffusivity\n",
    "\n",
    "In this example, we will implement a Fickian diffusion algorithm on a `Cubic` network assumming a variable diffusivity, such that diffusivity is significantly higher at high concentrations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import openpnm as op\n",
    "import matplotlib.pyplot as plt\n",
    "np.random.seed(10)\n",
    "ws = op.Workspace()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating network, phase and geometry\n",
    "We create a `Cubic` network and add the geometry and phase objects. Note that the same procedure can be applied on extracted networks and 3D networks. Here for simplicity and visualization we chose a 2D cubic network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "net = op.network.Cubic([1, 15, 15], spacing=1e-5)\n",
    "geom = op.geometry.StickAndBall(network=net)\n",
    "air = op.phases.Water(network=net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adding the diffusivity model\n",
    "We assume that the diffusivity of the phase air is not constant and is a function of the concentration of the species. Therefore, we define a model to calculate the `pore.diffusivity` at each concentration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def variable_diffusivity(target, c_ref, pore_concentration=\"pore.concentration\"):\n",
    "    X = target[pore_concentration]\n",
    "    val = 1e-9 * (1 + 5 * (X / c_ref) ** 2)\n",
    "    return val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "air.add_model(propname=\"pore.diffusivity\", model=variable_diffusivity, c_ref=10.0,\n",
    "              regen_mode=\"deferred\")\n",
    "phys = op.physics.Standard(network=net, geometry=geom, phase=air)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Performing Fickian diffusion\n",
    "We then add the the FickianDiffusion algorithm to our simulation and define \"Dirichlet\" boundary conditions assigned to the  pores that are on the left and the right face of the network. Note that values=10.0 for the first boundary condition means that the pore concentration of each pore (that is on the `left` boundary face) is equal to 10.0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "fd = op.algorithms.FickianDiffusion(network=net, phase=air)\n",
    "fd.set_value_BC(pores=net.pores(\"left\"), values=10.0)\n",
    "fd.set_value_BC(pores=net.pores(\"right\"), values=0.0)\n",
    "fd.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualizing\n",
    "\n",
    "We can visualize the average concentration distribution along the x direction (from the left boundary face towards the right boundary face)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "<KEY>//<KEY>",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "air.update(fd.results())\n",
    "c=air['pore.concentration']\n",
    "fig, ax = plt.subplots()\n",
    "c_xz_avg = c.reshape(net.shape).mean(axis=(0, 2))\n",
    "ax.plot(c_xz_avg, \"ko-\")\n",
    "ax.set_xlabel(r\"x\")\n",
    "ax.set_ylabel(r\"c\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The concentration of species throughout the network is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.colorbar.Colorbar at 0x1d83ebf2520>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "<KEY>CSV<KEY>",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "c2d = c.reshape(net._shape).squeeze()\n",
    "plt.imshow(np.rot90(c2d))\n",
    "plt.colorbar()"
   ]
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

{'fraction_non_alphanumeric': 0.1885356780520798, 'fraction_numerical': 0.014203584714237404, 'mean_word_length': 2.8534201954397393, 'pattern_counts': {'":': 117, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7155970', 'n_tokens_mistral': 2250, 'n_tokens_neox': 2144, 'n_words': 477}