{ "cells": [ { "cell_type": "markdown", "id": "fdefd10e", "metadata": {}, "source": [ "# Optimetrics setup\n", "\n", "This example shows how to use PyAEDT to create a project in HFSS and create all optimetrics\n", "setups.\n", "\n", "Keywords: **AEDT**, **General**, **optimetrics**." ] }, { "cell_type": "markdown", "id": "242c5a99", "metadata": {}, "source": [ "## Perform imports and define constants\n", "Import the required packages." ] }, { "cell_type": "code", "execution_count": null, "id": "b2d6f27c", "metadata": {}, "outputs": [], "source": [ "import os\n", "import tempfile\n", "import time\n", "\n", "import ansys.aedt.core" ] }, { "cell_type": "markdown", "id": "8b6190d2", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "aae7e9c8", "metadata": {}, "outputs": [], "source": [ "AEDT_VERSION = \"2025.2\"\n", "NG_MODE = False # Open AEDT UI when it is launched." ] }, { "cell_type": "markdown", "id": "5581e294", "metadata": {}, "source": [ "## Create temporary directory\n", "\n", "Create a temporary directory where downloaded data or\n", "dumped data can be stored.\n", "If you'd like to retrieve the project data for subsequent use,\n", "the temporary folder name is given by ``temp_folder.name``." ] }, { "cell_type": "code", "execution_count": null, "id": "7a19cc3c", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "9dc61c51", "metadata": {}, "source": [ "## Initialize HFSS and create variables\n", "\n", "Initialize the ``Hfss`` object and create two needed design variables,\n", "``w1`` and ``w2``." ] }, { "cell_type": "code", "execution_count": null, "id": "3fb24351", "metadata": {}, "outputs": [], "source": [ "project_name = os.path.join(temp_folder.name, \"optimetrics.aedt\")\n", "\n", "hfss = ansys.aedt.core.Hfss(\n", " project=project_name,\n", " version=AEDT_VERSION,\n", " new_desktop=True,\n", " non_graphical=NG_MODE,\n", " solution_type=\"Modal\",\n", ")\n", "\n", "hfss[\"w1\"] = \"1mm\"\n", "hfss[\"w2\"] = \"100mm\"" ] }, { "cell_type": "markdown", "id": "fbf00f04", "metadata": {}, "source": [ "## Create waveguide with sheets on it\n", "\n", "Create one of the standard waveguide structures and parametrize it.\n", "You can also create rectangles of waveguide openings and assign ports later." ] }, { "cell_type": "code", "execution_count": null, "id": "569f9f8f", "metadata": {}, "outputs": [], "source": [ "wg1, p1, p2 = hfss.modeler.create_waveguide(\n", " [0, 0, 0],\n", " hfss.AXIS.Y,\n", " \"WG17\",\n", " wg_thickness=\"w1\",\n", " wg_length=\"w2\",\n", " create_sheets_on_openings=True,\n", ")\n", "\n", "model = hfss.plot(show=False)\n", "\n", "model.show_grid = False\n", "model.plot(os.path.join(hfss.working_directory, \"Image.jpg\"))" ] }, { "cell_type": "markdown", "id": "05d5d2a0", "metadata": {}, "source": [ "## Create wave ports on sheets\n", "\n", "Create two wave ports on the sheets." ] }, { "cell_type": "code", "execution_count": null, "id": "f21d6be0", "metadata": {}, "outputs": [], "source": [ "hfss.wave_port(p1, integration_line=hfss.AxisDir.ZPos, name=\"1\")\n", "hfss.wave_port(p2, integration_line=hfss.AxisDir.ZPos, name=\"2\")" ] }, { "cell_type": "markdown", "id": "b7a616bb", "metadata": {}, "source": [ "## Create setup and frequency sweep\n", "\n", "Create a setup and a frequency sweep to use as the base for optimetrics\n", "setups." ] }, { "cell_type": "code", "execution_count": null, "id": "d5cda797", "metadata": {}, "outputs": [], "source": [ "setup = hfss.create_setup()\n", "hfss.create_linear_step_sweep(\n", " setup=setup.name,\n", " unit=\"GHz\",\n", " start_frequency=1,\n", " stop_frequency=5,\n", " step_size=0.1,\n", " name=\"Sweep1\",\n", " save_fields=True,\n", ")" ] }, { "cell_type": "markdown", "id": "a63fa388", "metadata": {}, "source": [ "## Create optimetrics analyses\n", "\n", "### Create parametric analysis\n", "\n", "Create a simple optimetrics parametrics analysis with output calculations." ] }, { "cell_type": "code", "execution_count": null, "id": "e195affb", "metadata": {}, "outputs": [], "source": [ "sweep = hfss.parametrics.add(\"w2\", 90, 200, 5)\n", "sweep.add_variation(\"w1\", 0.1, 2, 10)\n", "sweep.add_calculation(calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.5GHz\"})\n", "sweep.add_calculation(calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.6GHz\"})" ] }, { "cell_type": "markdown", "id": "6f3f966e", "metadata": {}, "source": [ "### Create sensitivity analysis\n", "\n", "Create an optimetrics sensitivity analysis with output calculations." ] }, { "cell_type": "code", "execution_count": null, "id": "b0428087", "metadata": {}, "outputs": [], "source": [ "sweep2 = hfss.optimizations.add(\n", " calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.5GHz\"}, optimization_type=\"Sensitivity\"\n", ")\n", "sweep2.add_variation(\"w1\", 0.1, 3, 0.5)\n", "sweep2.add_calculation(calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.6GHz\"})" ] }, { "cell_type": "markdown", "id": "289e088c", "metadata": {}, "source": [ "### Create an optimization analysis\n", "\n", "Create an optimization analysis based on goals and calculations." ] }, { "cell_type": "code", "execution_count": null, "id": "76161574", "metadata": {}, "outputs": [], "source": [ "sweep3 = hfss.optimizations.add(calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.5GHz\"})\n", "sweep3.add_variation(\"w1\", 0.1, 3, 0.5)\n", "sweep3.add_goal(calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.6GHz\"})\n", "sweep3.add_goal(calculation=\"dB(S(1,1))\", ranges={\"Freq\": (\"2.6GHz\", \"5GHz\")})\n", "sweep3.add_goal(\n", " calculation=\"dB(S(1,1))\",\n", " ranges={\"Freq\": (\"2.6GHz\", \"5GHz\")},\n", " condition=\"Maximize\",\n", ")" ] }, { "cell_type": "markdown", "id": "c7dc5bdc", "metadata": {}, "source": [ "### Create a DesignXplorer optimization\n", "\n", "Create a DesignXplorer optimization based on a goal and a calculation." ] }, { "cell_type": "code", "execution_count": null, "id": "2ae10f38", "metadata": {}, "outputs": [], "source": [ "sweep4 = hfss.optimizations.add(\n", " calculation=\"dB(S(1,1))\",\n", " ranges={\"Freq\": \"2.5GHz\"},\n", " optimization_type=\"DesignExplorer\",\n", ")\n", "sweep4.add_goal(calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.6GHz\"})" ] }, { "cell_type": "markdown", "id": "25a7db23", "metadata": {}, "source": [ "### Create a Design of Experiments (DOE)\n", "\n", "Create a DOE based on a goal and a calculation." ] }, { "cell_type": "code", "execution_count": null, "id": "97907dcc", "metadata": {}, "outputs": [], "source": [ "sweep5 = hfss.optimizations.add(\n", " calculation=\"dB(S(1,1))\", ranges={\"Freq\": \"2.5GHz\"}, optimization_type=\"DXDOE\"\n", ")" ] }, { "cell_type": "markdown", "id": "6fb50a1d", "metadata": {}, "source": [ "### Create another DOE\n", "\n", "Create another DOE based on a goal and a calculation." ] }, { "cell_type": "code", "execution_count": null, "id": "ee6434dd", "metadata": {}, "outputs": [], "source": [ "region = hfss.modeler.create_region()\n", "hfss.assign_radiation_boundary_to_objects(region)\n", "hfss.insert_infinite_sphere(name=\"Infinite_1\")\n", "sweep6 = hfss.optimizations.add(\n", " calculation=\"RealizedGainTotal\",\n", " solution=hfss.nominal_adaptive,\n", " ranges={\"Freq\": \"5GHz\", \"Theta\": [\"0deg\", \"10deg\", \"20deg\"], \"Phi\": \"0deg\"},\n", " context=\"Infinite_1\",\n", ")" ] }, { "cell_type": "markdown", "id": "a2a017d7", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "cae6e957", "metadata": {}, "outputs": [], "source": [ "hfss.save_project()\n", "hfss.release_desktop()\n", "time.sleep(3) # Allow AEDT to shut down before cleaning the temporary project folder." ] }, { "cell_type": "markdown", "id": "54db0ba4", "metadata": {}, "source": [ "## Clean up\n", "\n", "All project files are saved in the folder ``temp_folder.name``.\n", "If you've run this example as a Jupyter notebook, you\n", "can retrieve those project files. The following cell removes\n", "all temporary files, including the project folder." ] }, { "cell_type": "code", "execution_count": null, "id": "d70045b2", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }