{ "cells": [ { "cell_type": "markdown", "id": "6243b5f7", "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": "788dad8a", "metadata": {}, "source": [ "## Perform imports and define constants\n", "Import the required packages." ] }, { "cell_type": "code", "execution_count": null, "id": "4b4e5c7c", "metadata": {}, "outputs": [], "source": [ "import os\n", "import tempfile\n", "import time\n", "\n", "import ansys.aedt.core" ] }, { "cell_type": "markdown", "id": "25a3761d", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "8510ef29", "metadata": {}, "outputs": [], "source": [ "AEDT_VERSION = \"2025.1\"\n", "NG_MODE = False # Open AEDT UI when it is launched." ] }, { "cell_type": "markdown", "id": "b731557f", "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": "1f0f6897", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "8c093ca3", "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": "e9f6387b", "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": "d20549f9", "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": "f85d551d", "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": "28b017b0", "metadata": {}, "source": [ "## Create wave ports on sheets\n", "\n", "Create two wave ports on the sheets." ] }, { "cell_type": "code", "execution_count": null, "id": "b6717e95", "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": "5d07b0c4", "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": "72388d87", "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": "596e3fd5", "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": "24c89e43", "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": "e6916289", "metadata": {}, "source": [ "### Create sensitivity analysis\n", "\n", "Create an optimetrics sensitivity analysis with output calculations." ] }, { "cell_type": "code", "execution_count": null, "id": "53d38a91", "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": "cd4c029e", "metadata": {}, "source": [ "### Create an optimization analysis\n", "\n", "Create an optimization analysis based on goals and calculations." ] }, { "cell_type": "code", "execution_count": null, "id": "abb331b3", "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": "881ee9c2", "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": "e270a0c9", "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": "d1d3d4f0", "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": "85fdb4ef", "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": "e3c3aad6", "metadata": {}, "source": [ "### Create another DOE\n", "\n", "Create another DOE based on a goal and a calculation." ] }, { "cell_type": "code", "execution_count": null, "id": "ebf58e80", "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": "f620d749", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "612ea19f", "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": "d7fe4d0f", "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": "163642cb", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }