Download this example
Download this example as a Jupyter Notebook or as a Python script.
Dipole antenna#
This example shows how to use PyAEDT to create a dipole antenna in HFSS and postprocess results.
Keywords: HFSS, modal, antenna, 3D components, far field.
Perform imports and define constants#
Perform required imports.
[1]:
import os
import tempfile
import time
[2]:
import ansys.aedt.core
Define constants.
[3]:
AEDT_VERSION = "2024.2"
NUM_CORES = 4
NG_MODE = False # Open AEDT UI when it is launched.
Create temporary directory#
Create a temporary directory where downloaded data or dumped data can be stored. If you’d like to retrieve the project data for subsequent use, the temporary folder name is given by temp_folder.name
.
[4]:
temp_folder = tempfile.TemporaryDirectory(suffix=".ansys")
Launch AEDT#
[5]:
d = ansys.aedt.core.launch_desktop(
AEDT_VERSION, non_graphical=NG_MODE, new_desktop=True
)
PyAEDT INFO: Python version 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
PyAEDT INFO: PyAEDT version 0.14.dev0.
PyAEDT INFO: Initializing new Desktop session.
PyAEDT INFO: Log on console is enabled.
PyAEDT INFO: Log on file C:\Users\ansys\AppData\Local\Temp\pyaedt_ansys_180abbbb-e3d7-45d4-b7e2-2a909f530287.log is enabled.
PyAEDT INFO: Log on AEDT is enabled.
PyAEDT INFO: Debug logger is disabled. PyAEDT methods will not be logged.
PyAEDT INFO: Launching PyAEDT with gRPC plugin.
PyAEDT INFO: New AEDT session is starting on gRPC port 54890
PyAEDT INFO: AEDT installation Path C:\Program Files\AnsysEM\v242\Win64
PyAEDT INFO: Ansoft.ElectronicsDesktop.2024.2 version started with process ID 2688.
Launch HFSS#
Create an HFSS design.
[6]:
project_name = os.path.join(temp_folder.name, "dipole.aedt")
hfss = ansys.aedt.core.Hfss(
version=AEDT_VERSION, project=project_name, solution_type="Modal"
)
PyAEDT INFO: Python version 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
PyAEDT INFO: PyAEDT version 0.14.dev0.
PyAEDT INFO: Returning found Desktop session with PID 2688!
PyAEDT INFO: Project dipole has been created.
PyAEDT INFO: No design is present. Inserting a new design.
PyAEDT INFO: Added design 'HFSS_6VB' of type HFSS.
PyAEDT INFO: Aedt Objects correctly read
Define variable#
Define a variable for the dipole length.
[7]:
hfss["l_dipole"] = "13.5cm"
Get 3D component from system library#
Get a 3D component from the syslib
directory. For this example to run correctly, you must get all geometry parameters of the 3D component or, in case of an encrypted 3D component, create a dictionary of the parameters.
[8]:
compfile = hfss.components3d["Dipole_Antenna_DM"]
geometryparams = hfss.get_components3d_vars("Dipole_Antenna_DM")
geometryparams["dipole_length"] = "l_dipole"
hfss.modeler.insert_3d_component(compfile, geometryparams)
PyAEDT INFO: Modeler class has been initialized! Elapsed time: 0m 1sec
PyAEDT INFO: Parsing C:/Users/ansys/AppData/Local/Temp/tmpmtlrf9in.ansys/dipole.aedt.
PyAEDT INFO: File C:/Users/ansys/AppData/Local/Temp/tmpmtlrf9in.ansys/dipole.aedt correctly loaded. Elapsed time: 0m 0sec
PyAEDT INFO: aedt file load time 0.015618324279785156
[8]:
<ansys.aedt.core.modeler.cad.components_3d.UserDefinedComponent at 0x1cf66542800>
Create boundaries#
Create an open region.
[9]:
hfss.create_open_region(frequency="1GHz")
PyAEDT INFO: Open Region correctly created.
PyAEDT INFO: Project dipole Saved correctly
[9]:
True
Create setup#
[10]:
setup = hfss.create_setup("MySetup")
setup.props["Frequency"] = "1GHz"
setup.props["MaximumPasses"] = 1
Run simulation#
[11]:
hfss.analyze_setup(name="MySetup", cores=NUM_CORES)
PyAEDT INFO: Key Desktop/ActiveDSOConfigurations/HFSS correctly changed.
PyAEDT INFO: Solving design setup MySetup
PyAEDT INFO: Key Desktop/ActiveDSOConfigurations/HFSS correctly changed.
PyAEDT INFO: Design setup MySetup solved correctly in 0.0h 0.0m 19.0s
[11]:
True
Postprocess#
Plot s-parameters and far field.
[12]:
hfss.create_scattering("MyScattering")
variations = hfss.available_variations.nominal_w_values_dict
variations["Freq"] = ["1GHz"]
variations["Theta"] = ["All"]
variations["Phi"] = ["All"]
hfss.post.create_report(
"db(GainTotal)",
hfss.nominal_adaptive,
variations,
primary_sweep_variable="Theta",
context="3D",
report_category="Far Fields",
)
PyAEDT INFO: Parsing C:/Users/ansys/AppData/Local/Temp/tmpmtlrf9in.ansys/dipole.aedt.
PyAEDT INFO: File C:/Users/ansys/AppData/Local/Temp/tmpmtlrf9in.ansys/dipole.aedt correctly loaded. Elapsed time: 0m 0sec
PyAEDT INFO: aedt file load time 0.03248739242553711
PyAEDT INFO: PostProcessor class has been initialized! Elapsed time: 0m 0sec
PyAEDT INFO: Post class has been initialized! Elapsed time: 0m 0sec
[12]:
<ansys.aedt.core.visualization.report.field.FarField at 0x1cf674bfdc0>
Create a far field report.
[13]:
new_report = hfss.post.reports_by_category.far_field(
"db(RealizedGainTotal)", hfss.nominal_adaptive, "3D"
)
new_report.report_type = "3D Polar Plot"
new_report.secondary_sweep = "Phi"
new_report.create("Realized3D")
[13]:
True
This code generates a 2D plot.
[14]:
hfss.field_setups[2].phi_step = 90
new_report2 = hfss.post.reports_by_category.far_field(
"db(RealizedGainTotal)", hfss.nominal_adaptive, hfss.field_setups[2].name
)
new_report2.variations = variations
new_report2.primary_sweep = "Theta"
new_report2.create("Realized2D")
[14]:
True
Get solution data using the new_report
object and postprocess or plot the data outside AEDT.
[15]:
solution_data = new_report.get_solution_data()
solution_data.plot()
PyAEDT INFO: Solution Data Correctly Loaded.
[15]:
Generate a far field plot by creating a postprocessing variable and assigning it to a new coordinate system. You can use the post
prefix to create a postprocessing variable directly from a setter, or you can use the set_variable()
method with an arbitrary name.
[16]:
hfss["post_x"] = 2
hfss.variable_manager.set_variable(name="y_post", expression=1, is_post_processing=True)
hfss.modeler.create_coordinate_system(origin=["post_x", "y_post", 0], name="CS_Post")
hfss.insert_infinite_sphere(custom_coordinate_system="CS_Post", name="Sphere_Custom")
[16]:
<ansys.aedt.core.modules.boundary.hfss_boundary.FarFieldSetup at 0x1cf6a6d2f50>
Retrieve solution data#
You can also process solution data using Python libraries like Matplotlib.
[17]:
new_report = hfss.post.reports_by_category.far_field(
"GainTotal", hfss.nominal_adaptive, "3D"
)
new_report.primary_sweep = "Theta"
new_report.far_field_sphere = "3D"
solutions = new_report.get_solution_data()
PyAEDT INFO: Solution Data Correctly Loaded.
Generate a 3D plot using Matplotlib.
[18]:
solutions.plot_3d()
[18]:
<ansys.aedt.core.visualization.plot.matplotlib.ReportPlotter at 0x1cf54b4de70>
Generate a far fields plot using Matplotlib.
[19]:
new_report.far_field_sphere = "Sphere_Custom"
solutions_custom = new_report.get_solution_data()
solutions_custom.plot_3d()
PyAEDT INFO: Solution Data Correctly Loaded.
[19]:
<ansys.aedt.core.visualization.plot.matplotlib.ReportPlotter at 0x1cf68189300>
Generate a 2D plot using Matplotlib where you specify whether it is a polar plot or a rectangular plot.
[20]:
solutions.plot(formula="db20", is_polar=True)
[20]:
Retrieve far-field data#
After the simulation completes, the far field data is generated port by port and stored in a data class. You can use this data once AEDT is released.
[21]:
ffdata = hfss.get_antenna_data(
sphere="Sphere_Custom",
setup=hfss.nominal_adaptive,
frequencies=1
)
PyAEDT INFO: Far field sphere Sphere_Custom is assigned
PyAEDT INFO: Exporting antenna metadata...
PyAEDT INFO: Antenna metadata exported.
PyAEDT INFO: Exporting geometry...
PyAEDT INFO: Exporting embedded element patterns.... Done: 0.537086009979248 seconds
Generate 2D cutout plot#
Generate a 2D cutout plot. You can define the Theta scan and Phi scan.
[22]:
ffdata.farfield_data.plot_cut(
primary_sweep="theta",
secondary_sweep_value=0,
quantity="RealizedGain",
title="FarField",
quantity_format="dB20",
is_polar=True,
)
[22]:
<ansys.aedt.core.visualization.plot.matplotlib.ReportPlotter at 0x1cf6a6d3130>
Release AEDT#
[23]:
hfss.save_project()
d.release_desktop()
# Wait 3 seconds to allow AEDT to shut down before cleaning the temporary directory.
time.sleep(3)
PyAEDT INFO: Project dipole Saved correctly
PyAEDT INFO: Desktop has been released and closed.
Clean up#
All project files are saved in the folder temp_folder.name
. If you’ve run this example as a Jupyter notebook, you can retrieve those project files. The following cell removes all temporary files, including the project folder.
[24]:
temp_folder.cleanup()
Download this example
Download this example as a Jupyter Notebook or as a Python script.