From d7f74b41d8f7a7e9a68c4ceb9a653324223a5df5 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 16 Jan 2024 15:09:11 +0100
Subject: [PATCH] [py] ruff fixes in
 Tests/Data/ThermoHydroMechanics/Linear/Point_injection/SaturatedPointheatsource.ipynb.

---
 .../SaturatedPointheatsource.ipynb            | 98 ++++++++++---------
 1 file changed, 50 insertions(+), 48 deletions(-)

diff --git a/Tests/Data/ThermoHydroMechanics/Linear/Point_injection/SaturatedPointheatsource.ipynb b/Tests/Data/ThermoHydroMechanics/Linear/Point_injection/SaturatedPointheatsource.ipynb
index 10cdf79ce0b..1844d163e14 100644
--- a/Tests/Data/ThermoHydroMechanics/Linear/Point_injection/SaturatedPointheatsource.ipynb
+++ b/Tests/Data/ThermoHydroMechanics/Linear/Point_injection/SaturatedPointheatsource.ipynb
@@ -486,46 +486,16 @@
     "from timeit import default_timer as timer\n",
     "\n",
     "# Run models in parallel via concurrent.futures\n",
-    "ogs_models = []\n",
-    "ogs_models.append(\n",
-    "    {\n",
-    "        \"model\": ogs_model_lin.prjfile,\n",
-    "        \"logfile\": f\"{out_dir}/lin-out.txt\",\n",
-    "        \"args\": f\"-o {out_dir} -m . -s .\",\n",
-    "    }\n",
-    ")\n",
-    "ogs_models.append(\n",
-    "    {\n",
-    "        \"model\": ogs_model_quad.prjfile,\n",
-    "        \"logfile\": f\"{out_dir}/quad-out.txt\",\n",
-    "        \"args\": f\"-o {out_dir} -m . -s .\",\n",
-    "    }\n",
-    ")\n",
-    "ogs_models.append(\n",
-    "    {\n",
-    "        \"model\": ogs_model_th2m.prjfile,\n",
-    "        \"logfile\": f\"{out_dir}/th2m-out.txt\",\n",
-    "        \"args\": f\"-o {out_dir} -m {path_th2m} -s {path_th2m}\",\n",
-    "    }\n",
-    ")\n",
-    "ogs_models.append(\n",
-    "    {\n",
-    "        \"model\": ogs_model_trm.prjfile,\n",
-    "        \"logfile\": f\"{out_dir}/trm-out.txt\",\n",
-    "        \"args\": f\"-o {out_dir} -m {path_trm} -s {path_trm}\",\n",
-    "    }\n",
-    ")\n",
+    "ogs_models = [\n",
+    "    (ogs_model_lin, f\"-o {out_dir} -m . -s .\"),\n",
+    "    (ogs_model_quad, f\"-o {out_dir} -m . -s .\"),\n",
+    "    (ogs_model_th2m, f\"-o {out_dir} -m {path_th2m} -s {path_th2m}\"),\n",
+    "    (ogs_model_trm, f\"-o {out_dir} -m {path_trm} -s {path_trm}\"),\n",
+    "]\n",
     "\n",
     "\n",
     "def run_ogs(model):\n",
-    "    prj = model[\"model\"]\n",
-    "    print(f\"Starting {prj} ...\\n\")\n",
-    "    start_sim = timer()\n",
-    "    # Starting via ogs6py does not work (\"cannot pickle lxml\"), at least on mac.\n",
-    "    ! ogs {prj} {model[\"args\"]} > {model[\"logfile\"]}\n",
-    "    assert _exit_code == 0\n",
-    "    runtime = timer() - start_sim\n",
-    "    return [f\"Finished {prj} in {runtime} s\", runtime]\n",
+    "    model[0].run_model(args=model[1])\n",
     "\n",
     "\n",
     "import platform\n",
@@ -535,13 +505,46 @@
     "\n",
     "    mp.set_start_method(\"fork\")\n",
     "\n",
+    "    # From https://stackoverflow.com/questions/25991860\n",
+    "    import copyreg\n",
+    "\n",
+    "    from lxml import etree\n",
+    "\n",
+    "    def compute(tree):\n",
+    "        print(\"Start Process\")\n",
+    "        print(type(tree))  # Returns <class 'lxml.etree._ElementTree'>\n",
+    "        print(tree.getroot())  # Returns <Element SymCLI_ML at 0x29f5dc8>. Success!\n",
+    "\n",
+    "    def pool_init(queue):\n",
+    "        # see http://stackoverflow.com/a/3843313/852994\n",
+    "        compute.queue = queue\n",
+    "\n",
+    "    def element_unpickler(data):\n",
+    "        return etree.fromstring(data)\n",
+    "\n",
+    "    def element_pickler(element):\n",
+    "        data = etree.tostring(element)\n",
+    "        return element_unpickler, (data,)\n",
+    "\n",
+    "    copyreg.pickle(etree._Element, element_pickler, element_unpickler)\n",
+    "\n",
+    "    def elementtree_unpickler(data):\n",
+    "        return etree.fromstring(data)\n",
+    "\n",
+    "    def elementtree_pickler(tree):\n",
+    "        return elementtree_unpickler, (etree.tostring(tree),)\n",
+    "\n",
+    "    copyreg.pickle(etree._ElementTree, elementtree_pickler, elementtree_unpickler)\n",
+    "\n",
+    "\n",
     "runtimes = []\n",
     "start = timer()\n",
     "with concurrent.futures.ProcessPoolExecutor() as executor:\n",
     "    results = executor.map(run_ogs, ogs_models)\n",
     "    for result in results:\n",
-    "        print(result[0])\n",
-    "        runtimes.append(result[1])\n",
+    "        if platform.system() != \"Darwin\":\n",
+    "            print(result[0])\n",
+    "            runtimes.append(result[1])\n",
     "print(f\"Elapsed time for all simulations: {timer() - start} s\")"
    ]
   },
@@ -578,7 +581,7 @@
     "]\n",
     "\n",
     "pvds = []\n",
-    "for i, prj in enumerate(projects):\n",
+    "for prj in projects:\n",
     "    pvds.append(vtuIO.PVDIO(f\"{out_dir}/{prj}.pvd\", dim=2))"
    ]
   },
@@ -599,8 +602,6 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "import matplotlib.pyplot as plt\n",
-    "\n",
     "plt.rcParams[\"lines.linewidth\"] = 2.0\n",
     "plt.rcParams[\"lines.color\"] = \"black\"\n",
     "plt.rcParams[\"legend.frameon\"] = True\n",
@@ -957,12 +958,13 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "fig = plt.figure()\n",
-    "ax = fig.add_axes([0, 0, 1, 1])\n",
-    "mesh = [\"thm linear\", \"thm quadratic\", \"th2m\", \"trm\"]\n",
-    "ax.bar(mesh, runtimes)\n",
-    "plt.ylabel(\"exec. time / s\")\n",
-    "plt.show()"
+    "if platform.system() != \"Darwin\":\n",
+    "    fig = plt.figure()\n",
+    "    ax = fig.add_axes([0, 0, 1, 1])\n",
+    "    mesh = [\"thm linear\", \"thm quadratic\", \"th2m\", \"trm\"]\n",
+    "    ax.bar(mesh, runtimes)\n",
+    "    plt.ylabel(\"exec. time / s\")\n",
+    "    plt.show()"
    ]
   },
   {
-- 
GitLab