{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7ee1f0b7-2695-465c-b585-d8c44a8bcb70",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Endspacer generation\n",
    "\n",
    "As a first step, we load and execute the roxie file as usual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2296c40-f610-4646-b501-4ad9f5a97d45",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pathlib\n",
    "import tempfile\n",
    "import pyvista\n",
    "\n",
    "folder = pathlib.Path(\"./examples/endspacers\").absolute()\n",
    "\n",
    "cadata_file = pathlib.Path('./examples/roxie.cadata')\n",
    "datafile_rend = folder / \"dipole_3d.data\"\n",
    "#datafile_cend = folder / \"CEND.data\"\n",
    "\n",
    "pyvista.start_xvfb()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "710fe19e-da8f-4fbc-a410-4c978e514f60",
   "metadata": {},
   "source": [
    "## Load input file, prepare for 3D generation\n",
    "\n",
    "In this example we only care about endspacer generation, so we deactivate any computationally heavy actions in the roxie file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fbf2680-91b5-44c8-9503-245083cdc675",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from roxieapi.input.builder import RoxieInputBuilder\n",
    "\n",
    "import pandas as pd\n",
    "\n",
    "roxie_input = RoxieInputBuilder.from_datafile(datafile_rend)\n",
    "\n",
    "roxie_input.bhdata_path = ''\n",
    "roxie_input.iron_path = ''\n",
    "\n",
    "roxie_input.flags['LWEDG']=False\n",
    "roxie_input.flags['LHARM']=False\n",
    "roxie_input.flags['LMATRF']=False\n",
    "roxie_input.flags['LBEMFEM']=False\n",
    "roxie_input.flags['LIRON']=False\n",
    "roxie_input.flags['LHARM']=False\n",
    "roxie_input.flags['LFIELD3']=False\n",
    "\n",
    "roxie_input.harmonictable = pd.DataFrame()\n",
    "roxie_input.matrf = pd.DataFrame()\n",
    "roxie_input.linefield = pd.DataFrame()\n",
    "roxie_input.peak= pd.DataFrame()\n",
    "roxie_input.plot2d = pd.DataFrame()\n",
    "roxie_input.plot3d = pd.DataFrame()\n",
    "roxie_input.graph = pd.DataFrame()\n",
    "roxie_input.objective= pd.DataFrame()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aafff19f-c300-4a7f-983c-92e27cdaedb6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "datafile_rend.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eef3f62-a370-4760-94f5-7a9a8dbd48df",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "output_folder = pathlib.Path(\"./outputs/endspacers\").absolute()\n",
    "datafile_out = output_folder /datafile_rend.name\n",
    "roxie_input.build(datafile_out)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccb2b2eb-6422-4ae3-8dc0-b9d2a83c49e2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pathlib\n",
    "import tempfile\n",
    "\n",
    "from roxieapi.tool_adapter.RoxieToolAdapter import RestRoxieToolAdapter\n",
    "\n",
    "input_files = [\n",
    "    datafile_out,\n",
    "    cadata_file,\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",
    "runner.download_artefacts(output_folder, \"*.xml\")\n",
    "\n",
    "print(f\"Run executed with return code {result}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aac63bf3-1f0b-488b-8d45-c5dc6e9ad6e9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41e528e2-aac3-4f0c-862a-194c56fb4ee9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from roxieapi.commons.roxie_constants import PlotLabels\n",
    "from roxieapi.commons.types import Plot3D\n",
    "from roxieapi.output.plots import RoxiePlotOutputs\n",
    "\n",
    "plots = RoxiePlotOutputs(\n",
    "    datafile_out.with_suffix(\".post.xml\"), datafile_out\n",
    ")\n",
    "parser = plots.output\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a8f9ece-88a9-47fa-9164-53e35c1e88cd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "\n",
    "p3d = Plot3D.create(\"Custom 3D Plot for FR\")\n",
    "p3d.add_coilPlot(\"6\")\n",
    "\n",
    "figure = plots.plots3d.plot_3d(p3d)\n",
    "figure.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cad28e1d-be8c-47ef-9947-b5752fccf94a",
   "metadata": {},
   "source": [
    "## Step File Generation\n",
    "\n",
    "With an executed roxie case, we can produce step files, based on the input file (data), and the generated output (post.xml)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca577009-30f9-4418-a032-149186b486fd",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from roxieinterfaces.geom import StepGenerator\n",
    "\n",
    "stepGen = StepGenerator(roxie_input=plots.input, roxie_output=plots.output, opt_nr=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dff8f06-a7ad-4da0-b9ef-47740fc00df5",
   "metadata": {},
   "source": [
    "### Builder pattern\n",
    "\n",
    "Endspacer generation can have convoluted options and settings, so there is a builder for setting up the generation.\n",
    "\n",
    "The builder object collects all settings, options and variations and only generates the geometry once all is selected.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a32ab7f-5192-46c2-adef-0b5b92f3316c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Create a new builder object\n",
    "stepGen.clear_assembly()\n",
    "builder = stepGen.builder()\n",
    "\n",
    "# We create endspacer for one coil\n",
    "# radial_nr=1 is the innermost radius, \n",
    "# angular_nr=1 is the first coil from the x axis in CCW\n",
    "# z_dir=1 is only z positive direction\n",
    "builder.add_coil_section(radial_nr=1, angular_nr=1,z_dir=1) \n",
    "\n",
    "# Alternative: Add blocks\n",
    "# builder.add_block(1)\n",
    "\n",
    "# add 20mm to the maximum z extend of the headspacer\n",
    "builder.with_endspacers(add_z=20,min_width=2.0)\n",
    "builder.with_blocks()\n",
    "\n",
    "assembly = builder.generate()\n",
    "\n",
    "assembly.export(str(output_folder / \"blocks_endspacers_rend.step\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f43180d3-2621-4cb5-a6fa-6965783883ad",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Create a new builder object\n",
    "stepGen.clear_assembly()\n",
    "builder = stepGen.builder()\n",
    "\n",
    "# We create endspacer for one coil\n",
    "# radial_nr=1 is the innermost radius, \n",
    "# angular_nr=1 is the first coil from the x axis in CCW\n",
    "# z_dir=1 is only z positive direction\n",
    "builder.add_coil_section(radial_nr=1, angular_nr=1,z_dir=1) \n",
    "\n",
    "# Alternative: Add blocks\n",
    "# builder.add_block(1)\n",
    "\n",
    "# add 20mm to the maximum z extend of the headspacer\n",
    "builder.with_coils()\n",
    "builder.with_insulations()\n",
    "\n",
    "assembly = builder.generate()\n",
    "\n",
    "assembly.export(str(output_folder / \"blocks_endspacers_conductors.step\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b0a4ee6-8a9f-4833-95fd-7ec50d812040",
   "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
}
