{ "cells": [ { "cell_type": "markdown", "id": "384757f2", "metadata": {}, "source": [ "# Dipole antenna\n", "\n", "This example shows how to use PyAEDT to create a dipole antenna in HFSS\n", "and postprocess results.\n", "\n", "Keywords: **HFSS**, **modal**, **antenna**, **3D components**, **far field**." ] }, { "cell_type": "markdown", "id": "464ff4b9", "metadata": {}, "source": [ "## Perform imports and define constants\n", "\n", "Perform required imports." ] }, { "cell_type": "code", "execution_count": null, "id": "f51c6548", "metadata": {}, "outputs": [], "source": [ "import os\n", "import tempfile\n", "import time" ] }, { "cell_type": "code", "execution_count": null, "id": "fa728c16", "metadata": {}, "outputs": [], "source": [ "import ansys.aedt.core" ] }, { "cell_type": "markdown", "id": "e5c5d804", "metadata": {}, "source": [ "Define constants." ] }, { "cell_type": "code", "execution_count": null, "id": "8567942b", "metadata": {}, "outputs": [], "source": [ "AEDT_VERSION = \"2024.2\"\n", "NUM_CORES = 4\n", "NG_MODE = False # Open AEDT UI when it is launched." ] }, { "cell_type": "markdown", "id": "f1e48ab7", "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": "d5a936c0", "metadata": {}, "outputs": [], "source": [ "temp_folder = tempfile.TemporaryDirectory(suffix=\".ansys\")" ] }, { "cell_type": "markdown", "id": "259ca906", "metadata": {}, "source": [ "## Launch AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "81b8f10a", "metadata": {}, "outputs": [], "source": [ "d = ansys.aedt.core.launch_desktop(\n", " AEDT_VERSION, non_graphical=NG_MODE, new_desktop=True\n", ")" ] }, { "cell_type": "markdown", "id": "aa0ecb0d", "metadata": {}, "source": [ "## Launch HFSS\n", "\n", "Create an HFSS design." ] }, { "cell_type": "code", "execution_count": null, "id": "1917fc15", "metadata": {}, "outputs": [], "source": [ "project_name = os.path.join(temp_folder.name, \"dipole.aedt\")\n", "hfss = ansys.aedt.core.Hfss(\n", " version=AEDT_VERSION, project=project_name, solution_type=\"Modal\"\n", ")" ] }, { "cell_type": "markdown", "id": "cc5bfba0", "metadata": {}, "source": [ "## Define variable\n", "\n", "Define a variable for the dipole length." ] }, { "cell_type": "code", "execution_count": null, "id": "08d460a6", "metadata": {}, "outputs": [], "source": [ "hfss[\"l_dipole\"] = \"13.5cm\"" ] }, { "cell_type": "markdown", "id": "8e8bccd0", "metadata": {}, "source": [ "## Get 3D component from system library\n", "\n", "Get a 3D component from the ``syslib`` directory. For this example to run\n", "correctly, you must get all geometry parameters of the 3D component or, in\n", "case of an encrypted 3D component, create a dictionary of the parameters." ] }, { "cell_type": "code", "execution_count": null, "id": "57d21ac6", "metadata": {}, "outputs": [], "source": [ "compfile = hfss.components3d[\"Dipole_Antenna_DM\"]\n", "geometryparams = hfss.get_components3d_vars(\"Dipole_Antenna_DM\")\n", "geometryparams[\"dipole_length\"] = \"l_dipole\"\n", "hfss.modeler.insert_3d_component(compfile, geometryparams)" ] }, { "cell_type": "markdown", "id": "68fbfeff", "metadata": {}, "source": [ "## Create boundaries\n", "\n", "Create an open region." ] }, { "cell_type": "code", "execution_count": null, "id": "7d2c791a", "metadata": {}, "outputs": [], "source": [ "hfss.create_open_region(frequency=\"1GHz\")" ] }, { "cell_type": "markdown", "id": "23d39f4f", "metadata": {}, "source": [ "## Create setup\n", "\n", "Create a setup with a sweep to run the simulation." ] }, { "cell_type": "code", "execution_count": null, "id": "965233f2", "metadata": {}, "outputs": [], "source": [ "setup = hfss.create_setup(\"MySetup\")\n", "setup.props[\"Frequency\"] = \"1GHz\"\n", "setup.props[\"MaximumPasses\"] = 1\n", "hfss.create_linear_count_sweep(\n", " setup=setup.name,\n", " units=\"GHz\",\n", " start_frequency=0.5,\n", " stop_frequency=1.5,\n", " num_of_freq_points=101,\n", " name=\"sweep1\",\n", " sweep_type=\"Interpolating\",\n", " interpolation_tol=3,\n", " interpolation_max_solutions=255,\n", " save_fields=False,\n", ")" ] }, { "cell_type": "markdown", "id": "b11eadfe", "metadata": {}, "source": [ "## Run simulation" ] }, { "cell_type": "code", "execution_count": null, "id": "3df4cc82", "metadata": {}, "outputs": [], "source": [ "hfss.analyze_setup(name=\"MySetup\", cores=NUM_CORES)" ] }, { "cell_type": "markdown", "id": "40ecb185", "metadata": {}, "source": [ "### Postprocess\n", "\n", "Plot s-parameters and far field." ] }, { "cell_type": "code", "execution_count": null, "id": "da30b03e", "metadata": {}, "outputs": [], "source": [ "hfss.create_scattering(\"MyScattering\")\n", "variations = hfss.available_variations.nominal_w_values_dict\n", "variations[\"Freq\"] = [\"1GHz\"]\n", "variations[\"Theta\"] = [\"All\"]\n", "variations[\"Phi\"] = [\"All\"]\n", "hfss.post.create_report(\n", " \"db(GainTotal)\",\n", " hfss.nominal_adaptive,\n", " variations,\n", " primary_sweep_variable=\"Theta\",\n", " context=\"3D\",\n", " report_category=\"Far Fields\",\n", ")" ] }, { "cell_type": "markdown", "id": "454204d0", "metadata": {}, "source": [ "Create a far fields report using the ``report_by_category.far field()`` method." ] }, { "cell_type": "code", "execution_count": null, "id": "e83d08be", "metadata": {}, "outputs": [], "source": [ "new_report = hfss.post.reports_by_category.far_field(\n", " \"db(RealizedGainTotal)\", hfss.nominal_adaptive, \"3D\"\n", ")\n", "new_report.variations = variations\n", "new_report.primary_sweep = \"Theta\"\n", "new_report.create(\"Realized2D\")" ] }, { "cell_type": "markdown", "id": "16c1c76f", "metadata": {}, "source": [ "Generate multiple plots using the ``new_report`` object. This code generates\n", "2D and 3D polar plots." ] }, { "cell_type": "code", "execution_count": null, "id": "a23da123", "metadata": {}, "outputs": [], "source": [ "new_report.report_type = \"3D Polar Plot\"\n", "new_report.secondary_sweep = \"Phi\"\n", "new_report.create(\"Realized3D\")" ] }, { "cell_type": "markdown", "id": "76d2c9c7", "metadata": {}, "source": [ "Get solution data using the ``new_report`` object and postprocess or plot the\n", "data outside AEDT." ] }, { "cell_type": "code", "execution_count": null, "id": "833ef83e", "metadata": {}, "outputs": [], "source": [ "solution_data = new_report.get_solution_data()\n", "solution_data.plot()" ] }, { "cell_type": "markdown", "id": "85c3c9c3", "metadata": {}, "source": [ "Generate a far field plot by creating a postprocessing variable and assigning\n", "it to a new coordinate system. You can use the ``post`` prefix to create a\n", "postprocessing variable directly from a setter, or you can use the ``set_variable()``\n", "method with an arbitrary name." ] }, { "cell_type": "code", "execution_count": null, "id": "fa8b57a6", "metadata": {}, "outputs": [], "source": [ "hfss[\"post_x\"] = 2\n", "hfss.variable_manager.set_variable(name=\"y_post\", expression=1, is_post_processing=True)\n", "hfss.modeler.create_coordinate_system(origin=[\"post_x\", \"y_post\", 0], name=\"CS_Post\")\n", "hfss.insert_infinite_sphere(custom_coordinate_system=\"CS_Post\", name=\"Sphere_Custom\")" ] }, { "cell_type": "markdown", "id": "4718dc96", "metadata": {}, "source": [ "## Retrieve solution data\n", "\n", "You can also process solution data using Python libraries like Matplotlib." ] }, { "cell_type": "code", "execution_count": null, "id": "e9459d68", "metadata": {}, "outputs": [], "source": [ "new_report = hfss.post.reports_by_category.far_field(\n", " \"GainTotal\", hfss.nominal_adaptive, \"3D\"\n", ")\n", "new_report.primary_sweep = \"Theta\"\n", "new_report.far_field_sphere = \"3D\"\n", "solutions = new_report.get_solution_data()" ] }, { "cell_type": "markdown", "id": "2d8f9fb0", "metadata": {}, "source": [ "Generate a 3D plot using Matplotlib." ] }, { "cell_type": "code", "execution_count": null, "id": "15476bea", "metadata": {}, "outputs": [], "source": [ "solutions.plot_3d()" ] }, { "cell_type": "markdown", "id": "a2544e1d", "metadata": {}, "source": [ "Generate a far fields plot using Matplotlib." ] }, { "cell_type": "code", "execution_count": null, "id": "7977edd6", "metadata": {}, "outputs": [], "source": [ "new_report.far_field_sphere = \"Sphere_Custom\"\n", "solutions_custom = new_report.get_solution_data()\n", "solutions_custom.plot_3d()" ] }, { "cell_type": "markdown", "id": "90d7c906", "metadata": {}, "source": [ "Generate a 2D plot using Matplotlib where you specify whether it is a polar\n", "plot or a rectangular plot." ] }, { "cell_type": "code", "execution_count": null, "id": "0622b740", "metadata": {}, "outputs": [], "source": [ "solutions.plot(formula=\"db20\", is_polar=True)" ] }, { "cell_type": "markdown", "id": "fdd455b7", "metadata": {}, "source": [ "## Retrieve far-field data\n", "\n", "After the simulation completes, the far\n", "field data is generated port by port and stored in a data class. You can use this data\n", "once AEDT is released." ] }, { "cell_type": "code", "execution_count": null, "id": "5ebb02cb", "metadata": {}, "outputs": [], "source": [ "ffdata = hfss.get_antenna_data(\n", " sphere=\"Sphere_Custom\",\n", " setup=hfss.nominal_adaptive,\n", " frequencies=[\"1000MHz\"],\n", ")" ] }, { "cell_type": "markdown", "id": "8ccc1d83", "metadata": {}, "source": [ "## Generate 2D cutout plot\n", "\n", "Generate a 2D cutout plot. You can define the Theta scan\n", "and Phi scan." ] }, { "cell_type": "code", "execution_count": null, "id": "25fd062c", "metadata": {}, "outputs": [], "source": [ "ffdata.farfield_data.plot_cut(\n", " primary_sweep=\"theta\",\n", " secondary_sweep_value=0,\n", " quantity=\"RealizedGain\",\n", " title=\"FarField\",\n", " quantity_format=\"dB20\",\n", " is_polar=True,\n", ")" ] }, { "cell_type": "markdown", "id": "4e168d6f", "metadata": {}, "source": [ "## Release AEDT" ] }, { "cell_type": "code", "execution_count": null, "id": "a32bdd82", "metadata": {}, "outputs": [], "source": [ "hfss.save_project()\n", "d.release_desktop()\n", "# Wait 3 seconds to allow AEDT to shut down before cleaning the temporary directory.\n", "time.sleep(3)" ] }, { "cell_type": "markdown", "id": "d234180f", "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.\n", "The following cell removes all temporary files, including the project folder." ] }, { "cell_type": "code", "execution_count": null, "id": "6bfc9698", "metadata": {}, "outputs": [], "source": [ "temp_folder.cleanup()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }