{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Porous structure on the microscopic scale: a sintered grain packing\n",
    "\n",
    "A macroscopic model of a porous material - Darcy's law or the Brinkman equations - needs a\n",
    "handful of numbers that summarize its microstructure: the porosity $\\varepsilon$, the specific\n",
    "surface $S$ (pore wall area per volume), the tortuosity $\\tau$ of the flow paths and the\n",
    "permeability $K$. All of them follow from a resolved model of one representative volume of the\n",
    "pores. Here the volume is the unit cell, of side $L=1$ mm, of a body-centred cubic packing of\n",
    "spherical grains of radius $a$. Above $a=\\sqrt3L/4=0.433$ mm the centre grain merges with the\n",
    "corner grains through necks, as in a sintered metal or ceramic filter, and the pore space becomes\n",
    "a network of channels without a simple shape.\n",
    "\n",
    "A uniform pressure gradient $G$ along $x$, applied as a body force, drives the creeping flow\n",
    "through the pores; then $K=\\mu\\langle u\\rangle/G$ with the superficial velocity $\\langle u\\rangle$,\n",
    "and the tortuosity is $\\tau=\\langle|\\mathbf u|\\rangle/\\langle u\\rangle$:\n",
    "\n",
    "$$-\\nabla p+\\mu\\nabla^2\\mathbf u+G\\,\\mathbf e_x=0,\\qquad\\nabla\\cdot\\mathbf u=0 .$$\n",
    "\n",
    "The planes through the grain centres and between them are mirror planes of the packing, so a\n",
    "quarter of the cell, 1 mm long in the flow direction and 0.5 mm by 0.5 mm across, is enough. It\n",
    "comes from the CAD files `porous_microstructure_hexagonal_array_<a/L in %>.step` for $a/L=0.38$,\n",
    "0.40, 0.45 and 0.47, written by `porous_microstructure_hexagonal_array_geometry.py` with the CAD\n",
    "objects of OneSimulation.\n",
    "\n",
    "| face | condition |\n",
    "|---|---|\n",
    "| `x_min` / `x_max` | periodic pair |\n",
    "| `y_min`, `y_max`, `z_min`, `z_max` (mirror planes) | impermeable, shear free |\n",
    "| `grains` | no slip |\n",
    "\n",
    "**Reference.** The Kozeny-Carman relation of packed beds (P. C. Carman, *Trans. Inst. Chem. Eng.*\n",
    "15, 150-166 (1937); J. Bear, *Dynamics of Fluids in Porous Media*, Elsevier 1972, Sec. 5.10)\n",
    "\n",
    "$$K=\\frac{\\varepsilon^3}{k\\,S^2},\\qquad k\\approx5,$$\n",
    "\n",
    "with $S$ the grain surface per bulk volume. Carman's constant $k=5$ is an empirical average over\n",
    "beds of irregular grains; for a regular packing the computed Kozeny constant $k=\\varepsilon^3/(KS^2)$\n",
    "shows how far the structure departs from it."
   ],
   "id": "markdown-01"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "from onesim.cad.exchange import load\n",
    "from onesim.materials import NewtonianFluid\n",
    "from onesim.meshing import LocalRefinement, PeriodicPair, TetrahedralMesher\n",
    "from onesim.numerics import Numerics\n",
    "from onesim.numerics.fem import FiniteElement, TaylorHood\n",
    "from onesim.numerics.linear import SparseDirect\n",
    "from onesim.numerics.nonlinear import Newton\n",
    "from onesim.physics.hydrodynamics import (FreeSlipLaw,\n",
    "                                          IncompressibleNavierStokes,\n",
    "                                          NoSlipWall, WallShear)\n",
    "from onesim.results.comparisons import convergence\n",
    "from onesim.results.derived import derived_field\n",
    "from onesim.simulation import PeriodicBoundary, Problem, Simulation\n",
    "from onesim.simulation.field_constraints import MeanValue\n",
    "from onesim.visualization import plots, theme\n",
    "\n",
    "theme.set()"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "code-02"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Geometry and mesh\n",
    "\n",
    "The fine mesh uses 0.045 mm in the pores and 0.016 mm on the grains. The two periodic faces carry the\n",
    "same triangles. The mesh study at $a/L=0.45$ adds 0.032 mm and 0.022 mm on the grains. The\n",
    "specific surface is the area of the grain faces of the CAD part over the volume of the quarter."
   ],
   "id": "markdown-03"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "ratios = (0.38, 0.40, 0.45, 0.47)\n",
    "cells = {ratio: load(f\"porous_microstructure_hexagonal_array_{round(100 * ratio)}.step\") for ratio in ratios}\n",
    "side = cells[0.45].size[0]\n",
    "volume = side**3 / 4\n",
    "grain_sizes = (0.032e-3, 0.022e-3, 0.016e-3)\n",
    "cases = [(ratio, grain_sizes[-1]) for ratio in ratios] + [(0.45, size) for size in grain_sizes[:-1]]\n",
    "meshes = {(ratio, size): TetrahedralMesher(target_size=0.045e-3, refinements=(LocalRefinement(region=cells[ratio].boundaries.grains, target_size=size),),\n",
    "                                           periodic_pairs=(PeriodicPair(first=cells[ratio].boundaries.x_min, second=cells[ratio].boundaries.x_max),))\n",
    "          .generate(cells[ratio]) for ratio, size in cases}\n",
    "for (ratio, size), mesh in meshes.items():\n",
    "    print(f\"a/L={ratio:.2f}, grain spacing {size*1e3:.3f} mm: {len(mesh.coordinates):,} nodes, {mesh.cell_count:,} tetrahedra\")\n",
    "surface = {ratio: sum(face.area for face in cell.faces if face.id in cell.boundaries.grains.members) / volume for ratio, cell in cells.items()}\n",
    "\n",
    "cell = load(\"porous_microstructure_hexagonal_array.step\")\n",
    "view = plots.Camera(elevation=28, azimuth=-60)\n",
    "fig = plots.figure(1, 2, size=(14, 6))\n",
    "plots.geometry(cell, selection={\"grains\": cell.boundaries.grains, \"periodic pair\": cell.boundaries.x_min | cell.boundaries.x_max},\n",
    "               labels=False, camera=view, title=\"Pore space of a quarter cell, sintered grains (a/L = 0.45)\", ax=fig[0])\n",
    "plots.mesh(meshes[0.45, grain_sizes[-1]], groups=(\"all\",), legend=False, camera=view,\n",
    "           title=f\"{meshes[0.45, grain_sizes[-1]].cell_count} tetrahedra\", ax=fig[1])\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "code-04"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model\n",
    "\n",
    "Water and a pressure gradient of 1 Pa/m keep the Reynolds number far below one. The pressure,\n",
    "periodic and fixed only up to a constant, gets the mean value zero."
   ],
   "id": "markdown-05"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "water, gradient = NewtonianFluid(density=1000.0, dynamic_viscosity=1.0e-3), 1.0\n",
    "models = {ratio: IncompressibleNavierStokes(domain=cells[ratio].volume, material=water, body_force=(gradient, 0.0, 0.0)) for ratio in ratios}\n",
    "problems = {ratio: Problem(name=\"Grain packing unit cell\", geometry=cells[ratio], physics=(model,), boundary_conditions=(\n",
    "    NoSlipWall(boundary=cells[ratio].boundaries.grains, physics=model),\n",
    "    WallShear(boundary=cells[ratio].boundaries.y_min | cells[ratio].boundaries.y_max | cells[ratio].boundaries.z_min | cells[ratio].boundaries.z_max,\n",
    "              physics=model, law=FreeSlipLaw()),\n",
    "    PeriodicBoundary(pair=PeriodicPair(first=cells[ratio].boundaries.x_min, second=cells[ratio].boundaries.x_max), physics=model)),\n",
    "    constraints=(MeanValue(model.pressure, 0.0),)) for ratio, model in models.items()}\n",
    "flows = {(ratio, size): Simulation(problem=problems[ratio], mesh=meshes[ratio, size], numerics=Numerics(\n",
    "    spatial=FiniteElement(element=TaylorHood()), initial_guess={models[ratio].velocity: (0.0, 0.0, 0.0)},\n",
    "    nonlinear_solver=Newton(linear_solver=SparseDirect(), absolute_tolerance=1e-20, relative_tolerance=1e-10))).solve_stationary()\n",
    "    .field(models[ratio].velocity) for ratio, size in cases}"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "code-06"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Results\n",
    "\n",
    "The porosity of the mesh against the fluid volume of the CAD part, the specific surface, the\n",
    "permeability, the tortuosity and the Kozeny constant for the four grain sizes, the Kozeny-Carman\n",
    "permeability with Carman's $k=5$, and the mesh study of the permeability."
   ],
   "id": "markdown-07"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "main = grain_sizes[-1]\n",
    "porosity = {ratio: derived_field(flows[ratio, main], lambda u, points: np.ones(len(points)), name=\"one\", unit=\"1\").integrate().value / volume\n",
    "            for ratio in ratios}\n",
    "permeability = {key: water.dynamic_viscosity * velocity.integrate().value[0] / volume / gradient for key, velocity in flows.items()}\n",
    "tortuosity = {ratio: derived_field(flows[ratio, main], lambda u, points: np.linalg.norm(u, axis=1), name=\"speed\", unit=\"m/s\").integrate().value\n",
    "              / flows[ratio, main].integrate().value[0] for ratio in ratios}\n",
    "kozeny = {ratio: porosity[ratio] ** 3 / (permeability[ratio, main] * surface[ratio] ** 2) for ratio in ratios}\n",
    "carman = {ratio: porosity[ratio] ** 3 / (5 * surface[ratio] ** 2) for ratio in ratios}\n",
    "study = convergence([permeability[0.45, size] for size in grain_sizes], list(grain_sizes))\n",
    "\n",
    "print(\" a/L   grains      porosity   CAD      S [1/mm]   K [m²]       Kozeny-Carman   K / K_KC   tortuosity   Kozeny k\")\n",
    "for ratio in ratios:\n",
    "    print(f\"{ratio:4.2f}  {'sintered' if ratio > np.sqrt(3) / 4 else 'separate':9s} {porosity[ratio]:9.4f} {cells[ratio].measure / volume:8.4f}\"\n",
    "          f\" {surface[ratio] / 1e3:9.3f} {permeability[ratio, main]:12.4e} {carman[ratio]:14.4e} {permeability[ratio, main] / carman[ratio]:9.3f}\"\n",
    "          f\" {tortuosity[ratio]:11.3f} {kozeny[ratio]:10.2f}\")\n",
    "print(\"\\nmesh study at a/L = 0.45: K = \" + \", \".join(f\"{permeability[0.45, size]:.5e} m² ({meshes[0.45, size].cell_count} tetrahedra)\"\n",
    "                                                   for size in grain_sizes))\n",
    "print(f\"change between the two finest meshes {100 * study.change:.2f} %, extrapolated {study.extrapolated:.5e} m²\")"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "code-08"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Figure\n",
    "\n",
    "The flow speed on the faces of the quarter cell with sintered grains, the permeability against\n",
    "the Kozeny-Carman relation over the porosity, and tortuosity and Kozeny constant over the grain\n",
    "size."
   ],
   "id": "markdown-09"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": [
    "fig = plots.figure(1, 3, size=(19, 5.5))\n",
    "plots.surface(flows[0.45, main], component=lambda u, position: 1e9 * np.linalg.norm(u, axis=1), limits=\"robust\", contours=6, subdivisions=2,\n",
    "              camera=view, label=\"speed [nm/s]\", title=\"Speed on the cell faces, a/L = 0.45\", ax=fig[0])\n",
    "plots.line(points={\"simulation\": ([porosity[r] for r in ratios], [permeability[r, main] for r in ratios]),\n",
    "                   \"Kozeny-Carman, k = 5\": ([porosity[r] for r in ratios], [carman[r] for r in ratios])},\n",
    "           markers=True, scale=\"logy\", xlabel=\"porosity ε\", ylabel=\"permeability K [m²]\", title=\"Permeability of the grain packing\",\n",
    "           legend=\"upper left\", ax=fig[1])\n",
    "plots.line(list(ratios), {\"tortuosity ⟨|u|⟩ / ⟨u⟩\": [tortuosity[r] for r in ratios]}, right={\"Kozeny constant k\": [kozeny[r] for r in ratios]},\n",
    "           vlines={\"grains touch\": np.sqrt(3) / 4}, right_label=\"Kozeny constant k = ε³ / (K S²)\", markers=True, xlabel=\"grain radius a / L\",\n",
    "           ylabel=\"tortuosity\", title=\"Tortuosity and Kozeny constant\", legend=\"upper left\", ax=fig[2])\n",
    "plots.show()"
   ],
   "execution_count": null,
   "outputs": [],
   "id": "code-10"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Going further\n",
    "\n",
    "Drive the flow along $y$ in a second run to confirm that the cubic packing is isotropic, or move\n",
    "the centre grain off the body centre in `porous_microstructure_hexagonal_array_geometry.py` to\n",
    "make the medium anisotropic. A packing read from a scan of a real material replaces the regular\n",
    "cell without changing the model."
   ],
   "id": "markdown-11"
  }
 ]
}
