5.3. Creating and Using Expressions

There are two ways that you can create expressions in Ansys Fluent. The first way is by creating an expression directly in the field where it will be applied. The second is by creating a named expression, which can be reused at multiple locations. These two formats are discussed in the following sections:


Important:
  • Be careful to avoid situations where you are creating a circular dependency for a field or property. For example, setting the Velocity Magnitude field of a velocity inlet to Velocity.mag, without specifying a value for the velocity magnitude elsewhere, results in a 0-value for the velocity magnitude.

  • 2D axisymmetric cases—expressions report quantities such as Area, on the actual cells/faces or on a per radian basis. Therefore, these quantities must be multiplied by to get the value for the whole domain.

  • 2D cases—expressions report quantities such as Area, on the actual cells/faces or on a per unit basis. Therefore, these quantities must be multiplied by the reference depth to get the value for the whole domain (otherwise the depth is assumed to be 1).



Note:  For postprocessing, expressions are evaluated using an algorithm that is suitable for a wide range of computations. First cell values are evaluated, then node values are interpolated from the cell values. Custom field functions (Custom Field Functions) interpolate cell values to nodes prior to performing the computations of the provided definition. The observable difference in these two approaches appears in cases where there are steep gradients in cell values.

Depending on the specific expression, you may notice small differences in postprocessing for node-based variables (such as mesh or structures-based variables) if comparing expression results with directly evaluated postprocessing of the same variables. For example, if you compare a contour plot of Total Mesh Displacement with a contour plot of an expression evaluating total mesh displacement, you may see minute differences due to small approximation errors.


5.3.1. Directly Applied Expressions

You can use expressions at boundary and cell zone conditions, at certain material properties, and at fields and properties where profiles and parameters can also be defined. Expressions can also be used at most other non-cell zone or boundary condition locations where parameters can be defined.

5.3.1.1. Expressions for Cell Zones and Boundary Conditions

To define an expression for a boundary or cell zone condition:

  1. Open the dialog box for the boundary/cell zone where you want to create an expression. For example, the Velocity Inlet dialog box.

  2. Click the drop-down arrow to the right of the field that you would like to define using an expression and select expression.


    Note:  Alternatively, you can:

    • Create a new named expression (Named Expressions).

    • Select a valid expression from the list.


  3. You can enter your expression directly in the text field. Ansys Fluent provides messaging regarding the validity of the expression as soon as you begin entering it.

    Alternatively, you can click   to open a bigger editor (Figure 5.2: The Expression Editor Dialog Box).

    Figure 5.2: The Expression Editor Dialog Box

    The Expression Editor Dialog Box


    Important:  Expressions allow for implicit locations. For example, you could specify the mass flux at a mass-flow-inlet boundary as 1 [kg/s]/Area(). However, if you explicitly provide a location, that will be used (for example, 1 [kg/s]/Area(['outlet']).


5.3.1.2. Expressions for Material Properties

You can use expressions to define the following material properties:

  • Density

  • Viscosity

  • Thermal conductivity

  • Absorption coefficient

  • Scattering coefficient

  • Lithium diffusivity

  • Partition coefficient

  • UDS diffusivities

To define an expression for a material property:

  1. Open the Create/Edit Materials dialog box.

     Setup Materials Fluid | Solid <material name>  Edit...

  2. Click the drop-down arrow to the right of the property field that you would like to define using an expression and select expression.

  3. Enter the expression in the Expression editor dialog box that opens.

    Figure 5.3: Example Expression for Water Density

    Example Expression for Water Density

    You can use the Functions, Variables, Constants, and Expressions drop-down lists when creating your expression. These lists contain the valid expression inputs.


    Note:  Density can be a function of static temperature or absolute pressure, but not both. Density specification as a function of absolute pressure is primarily for modeling mild compressibility, such as compressible liquids. If density is specified as a function of absolute pressure, Fluent will automatically compute the speed of sound.


  4. Click OK to save the expression.

5.3.1.3. Expressions in the Console

You can specify expressions for settings through the text user interface (Console). To do so, enter a string instead of a real value to specify a setting as an expression. For example:

5.3.2. Named Expressions

You can create, edit, and delete named expressions through the graphical user interface (GUI) via the right-click context menu in the Outline View tree. Right-click options are also available for importing/exporting expressions from/to a file, computing expressions, marking them as input/output parameters, and printing expression details to the console.

You can also managed named expression using the Expression Manager (Expression Manager).

 Setup Named Expressions  New...

Figure 5.4: The Expression Dialog Box

The Expression Dialog Box

  1. Provide a Name for the expression. The name must start with a letter and it may contain numbers and underscores.

    Naming Guidance:

    • Expression names must be unique. That is, they cannot use the same name as other expressions, report definitions, or expression constants or variables such as "PI", "avogadro", "T", "vol", "mass", and so on. Note that expression names cannot contain dashes "-", so if, for example, you have a report definition named surface-temperature-1, you cannot name your expression surface_temperature_1, because the expression language automatically converts dashes to underscores, resulting in a name conflict.

    • It may cause confusion if you create named expressions with names that match expression functions such as "exp" and "abs".

  2. Enter the Definition of the expression. You can use the drop-downs to the right of the Definition text box to add functions, variables, cell registers, constants, existing expressions, report definitions, locations, and profiles to this expression definition.

    All of the variables supported by the Fluent Expression Language are listed in the drop-down list.


    Note:
    • When you use curly brackets "{}" to specify the name of a defined object, such as a report definition or cell register, automatic suggestions are disabled and you must provide the display name of that object.

    • The instantaneous value of a report definition is always used when a report definition is included in an expression.


  3. (Optional) Add a Description of the expression.

  4. (Optional) Enable Use as Input Parameter, if you want to use the named expression as an input parameter. Refer to Defining and Viewing Parameters for additional information on parameters.


    Note:  Only constant-value expressions can be marked as input parameters.


  5. (Optional) Enable Use as Output Parameter, if you want to use the named expression as an output parameter. Refer to Creating Output Parameters and Working With Input and Output Parameters in Workbench in the Fluent in Workbench User's Guide for additional information on parameters.

  6. (Optional) Click   to refresh the Current Value field to display the current value of the expression.


    Note:  This feature is only available for expressions that evaluate to a single value, for example Average(StaticPressure, ["inlet_1"]), and not for expressions that evaluate as a field, such as 2*AbsolutePressure.


  7. Click OK to create the expression.

The Used In field lists the locations where the expression is used, including other expressions.


Important:  When you select a named expression for a field, either as just an expression or as an input parameter, the expression only shows as being selected under Named Expressions or parameters until you save the changes and close the dialog box. When the dialog box is reopened, you will see that the expression name is now displayed in the field and expression is selected from the drop-down. This is only a visual change in how this selection is shown; the field is still defined using that expression as it was when it was selected earlier.

Figure 5.5: Velocity Magnitude Defined as "expr1", An Expression Input Parameter

Velocity Magnitude Defined as "expr1", An Expression Input Parameter

In Figure 5.5: Velocity Magnitude Defined as "expr1", An Expression Input Parameter, the Velocity Magnitude field was previously set as an input parameter called expr1, but this is how it appears after the change is applied and the dialog box closed, and then reopened.


You can use a named expression to define other expressions—both inline expressions and named expressions. For example, a named expression with the name Vel_magnitude can be used to setup the velocity magnitude setting of an inlet as follows:


Note:
  • Expressions that are in use cannot be deleted.

  • You can create and modify named expressions through the define/named-expressions/ text command in the console.



Important:
  • Expressions must be dimensionally consistent. For example, you cannot add two quantities that have different unit dimensions.

  • Report definitions that are included in expressions and have the Per Surface | Per Zone option enabled, must use an Average Over value of 1.


Saving and Importing Named Expressions

You have the option of saving some or all of the named expressions you create to a file for future use.

Click Export To File... to choose a directory and save your expression(s) to a file.

Click Import From File... to select and load an expression file.

5.3.3. Context Specification

Some variables require additional context before they can be evaluated. For example, MassFraction requires the species context. Velocity may require phase context when it is evaluated in a multiphase simulation. The context is specified as a keyword argument to a function call on the variable. For example, MassFraction(species="co2", phase="smoke").

5.3.4. Plotting Expressions

You can plot expressions to evaluate and visualize their behavior, as shown in Figure 5.6: Plotting an Expression.

Figure 5.6: Plotting an Expression

Plotting an Expression

To plot an expression:

  1. Open the Plot tab.

  2. Specify the Primary Independent Variable from the drop-down list.

  3. Specify the number of plot points in the Count field.

  4. Set the Min and Max range for the plot.

  5. If you have more than one variable in your expression, you can define an additional variable as the Secondary Independent Variable.

    For the secondary variable, you can specify:

    1. The number of Curves.

    2. The Min and Max values for this variable.

  6. (Optional) Click Copy to Clipboard to copy the plot image onto your clipboard.

  7. You can use the left and right-mouse buttons to zoom in and out on the plot. Click Reset Zoom to return to the original view for the plot.

5.3.5. Postprocessing Expressions

You can visualize expression results using the Expressions... field (Figure 5.7: Expressions... Postprocessing Field) in postprocessing objects (contours, vectors, pathlines, particle tracks, XY plots). You also have the option of computing expression derivatives, which may also be visualized.

In addition to postprocessing expressions as field variables, you can also create expression volumes, which allow you to define a volume surface where you can display other field variables. Refer to Expression Volumes for additional information.

Figure 5.7: Expressions... Postprocessing Field

Expressions... Postprocessing Field

Expressions available for postprocessing must:

  • be of type Double or Double Vector. If it is of type Double Vector, the components and magnitude are available for postprocessing.

  • evaluate to a field (and not a single-value)

  • be fully specified. For example, if the expression computes mass fraction, the specie must also be included in the definition (MassFraction(species='co2'). Similarly, for a multiphase case, the phase must be specified, where required (VolumeFraction(phase='water').


    Important:
    • For multiphase cases, the Phase drop-down is not relevant, because the available expressions are already fully specified. Phase specification is not required for "mixture only" variables, such as static pressure.

    • Expressions do not work reliably when exported to other postprocessors. They are not exported to CFD-Post.



    Note:  While the plot displayed in the graphics window will show the units in the colormap, the Min and Max fields in the dialog box for the same plot, will not.


    Computing Statistics on Expressions

    You can select expressions that evaluate to a field in the Zone-Specific Sampling Options dialog box. Refer to Inputs for Time-Dependent Problems and Data Sampling for Steady Statistics for additional information on statistics.


    Important:  RMSE values computed on expressions may differ from RMSE values computed on equivalent custom field functions. The difference is due to the number of significant digits used in expression computations (9 decimal places).


5.3.6. Expression Manager

As expressions are created, they are added to the Outline View tree under the Named Expressions branch. You can perform various operations on defined expressions (such as copying and deleting) using context menus available via right-mouse-button click of the expression(s) in the Outline View tree. The Expression Manager (Figure 5.8: Expression Manager Dialog Box) allows you to perform all of these operations within a single dialog box.

To use the Expression Manager:

  1. Open the Expression Manager dialog box.

     Setup Named Expressions  Manage...

    Figure 5.8: Expression Manager Dialog Box

    Expression Manager Dialog Box

  2. Select the desired expression(s) and click the relevant button to perform the desired operation. Not all operations are available when multiple expressions are selected, for example, Edit... is grayed-out.

  3. Click Close when you are done modifying the expressions.

Refer to Expression Manager Dialog Box for detailed explanations of each button function.