evaluate_expression#

Hfss.evaluate_expression(expression: str) float | str | None#

Evaluate a valid string expression and return the numerical value in SI units.

This method evaluates mathematical expressions containing design variables, project variables, units, and mathematical operations. It handles various expression types including: - Simple numeric values, for example "42". - Values with units, for example "10mm". - Mathematical expressions, for example "34mm*sqrt(2)" or "pi/2". - Variable references, for example "$var1". - PWL (Piecewise Linear) dataset references. - Combined expressions, for example "$G1*p2/34".

Parameters:
expressionstr

A valid string expression for a design property or project variable.

Returns:
float or str or None

Evaluated value for the string expression in SI units. - Returns float for successfully evaluated numeric expressions - Returns str for PWL dataset references or invalid expressions - Returns None if evaluation fails completely

Notes

The method attempts multiple strategies to evaluate the expression:

  • Direct variable lookup if the expression is a variable name.

  • Check for PWL dataset references.

  • Try direct numeric conversion.

  • Create temporary internal variable to leverage AEDT’s expression evaluator.

For expressions containing project variables (prefixed with $), AEDT restrictions apply. Project variables cannot reference design variables.

The method uses an internal variable named “pyaedt_evaluator” for complex evaluations. All results are returned in SI units regardless of the input unit system.

Examples

>>> from ansys.aedt.core import Hfss
>>> hfss = Hfss()
>>> hfss["width"] = "10mm"
>>> hfss["height"] = "20mm"
>>> # Evaluate simple numeric value
>>> result = hfss.evaluate_expression("42")  # Returns 42.0
>>> # Evaluate value with units
>>> result = hfss.evaluate_expression("10mm")  # Returns 0.01 (in meters)
>>> # Evaluate expression with variables
>>> result = hfss.evaluate_expression("width*height")  # Returns 0.0002 (in m²)
>>> # Evaluate mathematical expression
>>> result = hfss.evaluate_expression("sqrt(width^2 + height^2)")