{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf36f861-a268-4e3e-b759-85d1d2e5d0ce",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pathlib\n",
    "import tempfile\n",
    "\n",
    "import gmsh\n",
    "from roxieapi.output.plots import RoxiePlotOutputs\n",
    "from roxieapi.tool_adapter.RoxieToolAdapter import RestRoxieToolAdapter\n",
    "\n",
    "from roxieinterfaces.mesh import HmoGenerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "890e8e26-6c46-4e4e-b012-88c845ca9f40",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "gmsh.initialize()\n",
    "gmsh.clear()\n",
    "\n",
    "r_inner = 70  # Inner radius of shell\n",
    "r_outer = 80  # outer radius of shell\n",
    "mesh_size = 5.0\n",
    "\n",
    "p1 = gmsh.model.geo.addPoint(r_inner, 0, 0, mesh_size)\n",
    "p2 = gmsh.model.geo.addPoint(0, r_inner, 0, mesh_size)\n",
    "p3 = gmsh.model.geo.addPoint(0, 0, 0, mesh_size)\n",
    "p4 = gmsh.model.geo.addPoint(r_outer, 0, 0, mesh_size)\n",
    "p5 = gmsh.model.geo.addPoint(0, r_outer, 0, mesh_size)\n",
    "\n",
    "\n",
    "c1 = gmsh.model.geo.add_circle_arc(p1, p3, p2)\n",
    "l1 = gmsh.model.geo.add_line(p2, p5)\n",
    "c2 = gmsh.model.geo.add_circle_arc(p5, p3, p4)\n",
    "l2 = gmsh.model.geo.add_line(p4, p1)\n",
    "\n",
    "face1 = gmsh.model.geo.add_curve_loop([c1, l1, c2, l2])\n",
    "surf = gmsh.model.geo.add_plane_surface([face1])\n",
    "\n",
    "gmsh.model.geo.synchronize()\n",
    "\n",
    "gmsh.option.setNumber(\"Mesh.RecombineAll\", 1)\n",
    "gmsh.option.setNumber(\"Mesh.RecombinationAlgorithm\", 1)\n",
    "gmsh.option.setNumber(\"Mesh.Recombine3DLevel\", 2)\n",
    "gmsh.option.setNumber(\"Mesh.ElementOrder\", 2)\n",
    "\n",
    "\n",
    "gmsh.model.mesh.generate()\n",
    "\n",
    "gmsh.write(\"outputs/mesh.msh\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd119e8a-c8c4-4a9f-bb99-3fc5f374a35f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import meshio\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.collections import PolyCollection\n",
    "\n",
    "mesh = meshio.read(\"outputs/mesh.msh\")\n",
    "\n",
    "points = mesh.points[:, :2]\n",
    "\n",
    "fig, ax = plt.subplots(figsize=(6, 6))\n",
    "\n",
    "for cell_block in mesh.cells:\n",
    "\n",
    "    if cell_block.type != \"quad9\":\n",
    "        continue\n",
    "\n",
    "    cells = cell_block.data\n",
    "\n",
    "    polygons = []\n",
    "\n",
    "    for cell in cells:\n",
    "\n",
    "        # only corner nodes\n",
    "        corners = [cell[0], cell[1], cell[2], cell[3]]\n",
    "\n",
    "        polygons.append(points[corners])\n",
    "\n",
    "    collection = PolyCollection(\n",
    "        polygons,\n",
    "        facecolors=\"none\",\n",
    "        edgecolors=\"black\",\n",
    "        linewidths=0.5\n",
    "    )\n",
    "\n",
    "    ax.add_collection(collection)\n",
    "\n",
    "ax.autoscale()\n",
    "ax.set_aspect(\"equal\")\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6283a732-edd0-4f0a-b179-4b747f4c3d0a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "gen = HmoGenerator()\n",
    "gen.load_gmsh_file(\"outputs/mesh.msh\")\n",
    "print(f\"Generated Mesh with {len(gen.nodes)} nodes, {len(gen.elements)} elements.\")\n",
    "print(\"Materials:\")\n",
    "for id, name in gen.map_components.items():\n",
    "    print(f\"  {id}: {name}\")\n",
    "\n",
    "hmo_file = gen.to_hmo()\n",
    "hmo_filepath = pathlib.Path(\"examples/ext_mesh.hmo\").absolute()\n",
    "with open(hmo_filepath, \"w\") as f:\n",
    "    f.write(hmo_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5ba9a7e-13f4-48e0-be14-b46eb8ff0766",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pathlib\n",
    "import tempfile\n",
    "\n",
    "from roxieapi.input.builder import RoxieInputBuilder\n",
    "\n",
    "folder = pathlib.Path(\"./examples\").absolute()\n",
    "datafile = \"dipole_iron.data\"\n",
    "\n",
    "rib = RoxieInputBuilder.from_datafile(folder / datafile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e92b66e1-1f39-4ffc-ab44-feb12305facd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "rib.iron_path=None\n",
    "rib.hmo_path=\"ext_mesh.hmo\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2acc9bf-0cec-4156-92bf-08a957358aa2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "rib.ironyokeoptions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eedae7a3-0130-4552-83cd-cf007d15cc19",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "rib.flags['LIRON']=False\n",
    "rib.flags['LHMO']=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e548314a-e1fe-4a0c-9773-2a9a0f981274",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "tmp_file = folder / \"custom_iron.data\"\n",
    "rib.build(str(tmp_file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0dfcba5f-7e4c-4e5e-b191-6dcf98f001f9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "input_files = [\n",
    "    tmp_file,\n",
    "    hmo_filepath,\n",
    "]\n",
    "\n",
    "runner = RestRoxieToolAdapter(\n",
    "    service_url=f\"http://roxie:8080\",\n",
    "    input_files=input_files,\n",
    ")\n",
    "\n",
    "result = runner.run()\n",
    "\n",
    "print(f\"Run executed with return code {result}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13fef769-2b84-469a-893d-53ea32c7060b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "output_folder = pathlib.Path(\"./outputs\").absolute()\n",
    "\n",
    "runner.download_artefacts(output_folder, \"*.xml\")\n",
    "runner.download_artefacts(output_folder, \"*.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec469596-8762-4145-8f68-6404b45dde6b",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from roxieapi.output.plots import RoxiePlotOutputs\n",
    "\n",
    "plots = RoxiePlotOutputs(\n",
    "    output_folder/\"custom_iron.post.xml\", tmp_file\n",
    ")\n",
    "del plots.output.opt[1].step[1].eddyTimeSteps[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26797fdd-21f3-4bf4-b482-65f1afe707c2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from roxieapi.commons.roxie_constants import PlotLabels\n",
    "from roxieapi.commons.types import Plot2D, PlotLegend\n",
    "from roxieapi.output.plots import RoxiePlotOutputs\n",
    "\n",
    "plot_created = Plot2D.create(\"My Plot2D\")\n",
    "plot_created.add_meshPlot(\"35\", legend=PlotLegend(\"e\", False, 0, 1))\n",
    "\n",
    "figure = plots.plots2d.plot_xs(plot_created)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb70a354-6d41-4cf0-b780-c5532036508f",
   "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.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
