# Optimetrics Example Analysis¶

This example shows how you can use PyAEDT to create a project in HFSS and create all optimetrics setups.

```# sphinx_gallery_thumbnail_path = 'Resources/optimetrics.png'

from pyaedt import Hfss
from pyaedt import Desktop
import os
```

## Launch AEDT in Non-Graphical Mode¶

You can change the Boolean parameter `NonGraphical` to `False` to launch AEDT in graphical mode.

```NG = False
d = Desktop("2021.1", non_graphical=NG)
```

Out:

```Launching PyAEDT outside Electronics Desktop with CPython and Pythonnet
Launching AEDT installation C:\Program Files\AnsysEM\AnsysEM21.1\Win64
===================================================================================
pyaedt info: Launching AEDT with module Pythonnet.
pyaedt info:: Ansoft.ElectronicsDesktop.2021.1 Started with process ID 1780.
pyaedt info: Logger Started on C:\Users\ansys\Documents\Ansoft\pyaedt20211026_142552.log
pyaedt info: pyaedt v0.4.0
pyaedt info: Python version 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
```

## Initialize the Hfss Object and Create the Needed Design Variables¶

In this example, HFSS is to have two design variables, `w1` and `w2`.

```hfss = Hfss()
hfss["w1"] = "1mm"
hfss["w2"] = "100mm"
```

Out:

```pyaedt info: Project Project6077 has been created.
pyaedt info: No design is present. Inserting a new design.
pyaedt info: Successfully loaded project materials !
```

## Create a Waveguide with Sheets on It¶

This method creates one of the standard waveguide structures and parametrizes it. You can also create rectangles of waveguide openings and assign ports later.

```wg1, p1, p2 = hfss.modeler.create_waveguide(
[0, 0, 0],
hfss.CoordinateSystemAxis.YAxis,
"WG17",
wg_thickness="w1",
wg_length="w2",
create_sheets_on_openings=True,
)
```

## Create Wave Ports on the Sheets¶

This example creates two wave ports on the sheets.

```hfss.create_wave_port_from_sheet(p1, axisdir=hfss.AxisDir.ZPos, portname="1")
hfss.create_wave_port_from_sheet(p2, axisdir=hfss.AxisDir.ZPos, portname="2")
```

Out:

```pyaedt info: Boundary WavePort 1 has been correctly created.
pyaedt info: Boundary WavePort 2 has been correctly created.

[<pyaedt.modules.Boundary.BoundaryObject object at 0x000001CD71F0B8B0>]
```

## Create a Setup and a Frequency Sweep¶

This example creates a setup and a frequency sweep to use as the base for Optimetrics setups.

```setup = hfss.create_setup()
hfss.create_linear_step_sweep(
setupname=setup.name, unit="GHz", freqstart=1, freqstop=5, step_size=0.1, sweepname="Sweep1", save_fields=True
)
```

Out:

```pyaedt info: Linear step sweep Sweep1 has been correctly created

<pyaedt.modules.SetupTemplates.SweepHFSS object at 0x000001CD1C3B6C70>
```

# Optimetrics Parametrics Setup¶

## Simple Parametrics Analysis with Output Calculations¶

This example creates a simple parametrics analysis with output calculations.

```sweep = hfss.opti_parametric.add_parametric_setup("w2", "LIN 90mm 200mm 5mm")
```

Out:

```True
```

# Optimetrics Sensitivity Setup¶

## Sensitivity Analysis with Output Calculations¶

This example creates a sensitivity analysis with output calculations.

```sweep2 = hfss.opti_sensitivity.add_sensitivity(calculation="dB(S(1,1))", calculation_value="2.5GHz")
```

Out:

```True
```

# Optimetrics Optimization Setup¶

## Optimization Based on Goals and Calculations¶

This example creates an optimization based on goals and calculations.

```sweep3 = hfss.opti_optimization.add_optimization(calculation="dB(S(1,1))", calculation_value="2.5GHz")
calculation="dB(S(1,1))",
calculation_value="2.6GHz",
calculation_type="rd",
calculation_stop="5GHz",
condition="Maximize",
)
```

Out:

```True
```

# Optimetrics DesignXplorer (DX) Setup¶

## DX Optimization Based on a Goal and a Calculation¶

This example creates a DX optimization based on a goal and a calculation.

```sweep4 = hfss.opti_designxplorer.add_dx_setup(variables_to_include=["w1"])
```

Out:

```True
```

# Optimetrics DOE (Design of Experiments) Setup¶

## DOE Based on a Goal and a Calculation¶

This example creates a DOE based on a goal and a calculation.

```sweep5 = hfss.opti_doe.add_doe(calculation="dB(S(1,1))", calculation_value="2.6GHz")
```

Out:

```True
```

# Close AEDT¶

After the simulaton is completed, you can close AEDT or release it using the `pyaedt.Desktop.release_desktop()` method. All methods provide for saving the project before exiting.

```if os.name != "posix":
d.force_close_desktop()
```

Total running time of the script: ( 0 minutes 22.143 seconds)

Gallery generated by Sphinx-Gallery