{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Pickling of NGSolve objects\n",
    "\n",
    "Python objects an be converted to byte-streams, which can be stored to files, and later the \n",
    "Python object can be reconstructed by the inverse process. In general this is known as serialization,\n",
    "in Python it is called pickling, see\n",
    "https://docs.python.org/3/library/pickle.html.\n",
    "Another usecase is parallel computing, where it is used to send whole Python objects across a network.\n",
    "\n",
    "Many of the NGSolve-objects support pickling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ngsolve import *\n",
    "from ngsolve.webgui import Draw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "mesh = Mesh(unit_square.GenerateMesh(maxh=0.3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "outfile = open(\"mesh.pkl\", \"wb\")\n",
    "pickle.dump(mesh, outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "infile = open(\"mesh.pkl\", \"rb\")\n",
    "mesh2 = pickle.load(infile)\n",
    "mesh2.nv, mesh2.ne"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw (mesh2);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "## Shared objects remain shared\n",
    "\n",
    "When we create several spaces on the same mesh, all spaces link to the mesh via a shared pointer. Similarly, if we have several `GridFunction`s defined on the same space, they link to it using a shared pointer. These shared objects remain shared after pickling and unpickling:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "fes = H1(mesh, order=2)\n",
    "gfu1 = GridFunction(fes)\n",
    "gfu2 = GridFunction(fes)\n",
    "gfu1.Set(x)\n",
    "gfu2.Set(y)\n",
    "\n",
    "outfile = open(\"gridfunction.pkl\", \"wb\")\n",
    "pickle.dump([gfu1,gfu2], outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "infile = open(\"gridfunction.pkl\", \"rb\")\n",
    "gfv1,gfv2 = pickle.load(infile)\n",
    "print (\"the same spaces:\", id(gfv1.space), \"=?=\", id(gfv2.space))\n",
    "\n",
    "Draw (gfv1);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "## Pickling expression trees\n",
    "\n",
    "`CoefficientFunction` expression trees support pickling as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "func = x*gfu1 + y\n",
    "print (func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "outfile = open(\"func.pkl\", \"wb\")\n",
    "pickle.dump([mesh,func], outfile)\n",
    "\n",
    "infile = open(\"func.pkl\", \"rb\")\n",
    "mesh2,func2 = pickle.load(infile)\n",
    "\n",
    "print (func2)\n",
    "\n",
    "Draw (func2, mesh2);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "We were pickling the mesh explicitly to have it available for drawing, it would be contained in the function `func` anyway.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.remove(\"mesh.pkl\")\n",
    "os.remove(\"gridfunction.pkl\")\n",
    "os.remove(\"func.pkl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "## Implementation of pickling\n",
    "\n",
    "[pybind11-pickling](https://pybind11.readthedocs.io/en/stable/advanced/classes.html?highlight=pickling#pickling-support) supports wrapping of serialization of user-classes. \n",
    "\n",
    "And then there is ngcore - archive ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
