|
| 1 | +.. _user_guide_variables: |
| 2 | + |
| 3 | +=============================== |
| 4 | +Working with physical variables |
| 5 | +=============================== |
| 6 | + |
| 7 | +PyFluent integrates with the PyAnsys-units library, which provides a shared catalog of variable objects based on physical quantities like temperature, pressure, and velocity. These variable objects, or VariableDescriptors, can be used throughout PyFluent to reference fields and quantities in a clear, consistent, and reliable way. |
| 8 | + |
| 9 | +Instead of relying on raw strings like ``"temperature"`` or ``"SV_T"``, which may vary between Fluent interfaces or be hard to interpret, you can use named descriptors from the catalog such as ``VariableCatalog``.``TEMPERATURE``). This improves code readability, reduces the chance of errors, and makes it easier to work across different APIs. |
| 10 | + |
| 11 | +The same catalog is designed to work not just with PyFluent, but also with other PyAnsys libraries, offering a unified and expressive way to interact with physical quantities across products. |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +The key benefits of using the ``VariableCatalog`` include: |
| 17 | + |
| 18 | +- Code that is portable across Ansys products and more resistant to typos |
| 19 | +- Simplified refactoring when field names change in Fluent or across versions |
| 20 | +- Integrated dimensional consistency (via ``ansys-units``) |
| 21 | +- Autocompletion and discoverability of supported quantities |
| 22 | + |
| 23 | +Accessing field data |
| 24 | +--------------------- |
| 25 | + |
| 26 | +Here’s how to use ``VariableCatalog`` to read and reduce field data using unit-aware quantity references: |
| 27 | + |
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + from ansys.fluent.core import launch_fluent, examples |
| 31 | + from ansys.units.variable_descriptor import VariableCatalog |
| 32 | +
|
| 33 | + solver = launch_fluent() |
| 34 | + case_path = examples.download_file("mixing_elbow.cas.h5", "pyfluent/mixing_elbow") |
| 35 | + solver.file.read(file_type="case", file_name=case_path) |
| 36 | +
|
| 37 | + solver.settings.solution.initialization.hybrid_initialize() |
| 38 | +
|
| 39 | + temperature = VariableCatalog.TEMPERATURE |
| 40 | + locations = ["hot-inlet"] |
| 41 | +
|
| 42 | + # Access scalar field data |
| 43 | + temp_data = solver.fields.field_data.get_scalar_field_data( |
| 44 | + field_name=temperature, surfaces=locations |
| 45 | + ) |
| 46 | + print(temp_data[locations[0]][0]) # value at hot-inlet |
| 47 | +
|
| 48 | + # Compute minimum of a physical quantity |
| 49 | + temp_min = solver.fields.reduction.minimum(expression=temperature, locations=locations) |
| 50 | + print(temp_min) |
| 51 | +
|
| 52 | + # Access solution variable data |
| 53 | + sol_data = solver.fields.solution_variable_data.get_data( |
| 54 | + solution_variable_name=temperature, zone_names=locations |
| 55 | + ) |
| 56 | + print(sol_data[locations[0]][0]) |
| 57 | +
|
| 58 | +Using variables with report definitions |
| 59 | +--------------------------------------- |
| 60 | + |
| 61 | +You can also use physical quantities in report definitions to improve clarity and maintainability: |
| 62 | + |
| 63 | +.. code-block:: python |
| 64 | +
|
| 65 | + surface_report = solver.settings.solution.report_definitions.surface["avg_temp"] |
| 66 | + surface_report.report_type = "surface-areaavg" |
| 67 | + surface_report.field = temperature # Note: using VariableCatalog, not a string |
| 68 | + surface_report.surface_names = locations |
| 69 | +
|
| 70 | + result = solver.solution.report_definitions.compute(report_defs=["avg_temp"]) |
| 71 | + print(result[0]["avg_temp"][0]) |
| 72 | +
|
| 73 | +Notes |
| 74 | +----- |
| 75 | + |
| 76 | +- All quantities used via ``VariableCatalog`` map to their correct Fluent field identifiers internally. |
| 77 | +- These objects can be validated against their expected dimensional types. |
| 78 | +- ``VariableCatalog`` is part of ``ansys-units``, which is automatically installed with PyFluent. |
0 commit comments