Skip to content

Commit d05d1ae

Browse files
docs: Update to use objects. (#4295)
Updated the user guide replacing strings and low-level data with object-based alternatives --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 2e3ffa3 commit d05d1ae

File tree

8 files changed

+103
-52
lines changed

8 files changed

+103
-52
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update to use objects.

doc/source/user_guide/fields/field_data.rst

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ To obtain surface vertex coordinates for a given surface, create a
4444
.. code-block:: python
4545
4646
>>> from ansys.fluent.core import SurfaceDataType, SurfaceFieldDataRequest
47+
>>> from ansys.fluent.core.solver import VelocityInlet
4748
4849
>>> vertices_request = SurfaceFieldDataRequest(
49-
>>> surfaces=["inlet"],
50+
>>> surfaces=[VelocityInlet(settings_source=solver_session, name="inlet")],
5051
>>> data_types=[SurfaceDataType.Vertices],
5152
>>> )
5253
>>> vertices_data = field_data.get_field_data(vertices_request)
@@ -64,8 +65,6 @@ in the ``data_types`` list.
6465

6566
.. code-block:: python
6667
67-
>>> from ansys.fluent.core.solver import VelocityInlet
68-
6968
>>> faces_normal_and_centroid_request = SurfaceFieldDataRequest(
7069
>>> surfaces=[VelocityInlet(settings_source=solver_session, name="inlet")],
7170
>>> data_types=[SurfaceDataType.FacesNormal, SurfaceDataType.FacesCentroid],
@@ -127,7 +126,12 @@ To retrieve scalar field data, such as absolute pressure, use ``ScalarFieldDataR
127126
.. code-block:: python
128127
129128
>>> from ansys.fluent.core import ScalarFieldDataRequest
130-
>>> absolute_pressure_request = ScalarFieldDataRequest(field_name="absolute-pressure", surfaces=["inlet"])
129+
>>> from ansys.units import VariableCatalog
130+
131+
>>> absolute_pressure_request = ScalarFieldDataRequest(
132+
>>> field_name=VariableCatalog.ABSOLUTE_PRESSURE,
133+
>>> surfaces=[VelocityInlet(settings_source=solver_session, name="inlet")],
134+
>>> )
131135
>>> absolute_pressure_data = field_data.get_field_data(absolute_pressure_request)
132136
133137
# Shape: (389,) - A single scalar value (e.g., pressure) for each of the 389 vertices.
@@ -144,7 +148,12 @@ To obtain vector field data, such as velocity vectors, use ``VectorFieldDataRequ
144148
.. code-block:: python
145149
146150
>>> from ansys.fluent.core import VectorFieldDataRequest
147-
>>> velocity_request = VectorFieldDataRequest(field_name="velocity", surfaces=["inlet", "inlet1"])
151+
>>> from ansys.fluent.core.solver import VelocityInlets
152+
153+
>>> velocity_request = VectorFieldDataRequest(
154+
>>> field_name=VariableCatalog.VELOCITY,
155+
>>> surfaces=VelocityInlets(settings_source=solver_session),
156+
>>> )
148157
>>> velocity_vector_data = field_data.get_field_data(velocity_request)
149158
# Shape: (262, 3) - Velocity vectors for 262 faces, each with components (vx, vy, vz) for 'inlet'.
150159
>>> velocity_vector_data["inlet"].shape
@@ -161,7 +170,7 @@ To obtain pathlines field data, use ``PathlinesFieldDataRequest``:
161170
162171
>>> from ansys.fluent.core import PathlinesFieldDataRequest
163172
>>> velocity_pathlines_request = PathlinesFieldDataRequest(
164-
>>> field_name="x-velocity",
173+
>>> field_name=VariableCatalog.VELOCITY_X,
165174
>>> surfaces=[VelocityInlet(settings_source=solver_session, name="inlet")]
166175
>>> flatten_connectivity=True,
167176
>>> )
@@ -193,9 +202,17 @@ Add multiple requests using ``add_requests`` and access the data with ``get_resp
193202

194203
.. code-block:: python
195204
196-
>>> vertices_and_centroid_request = SurfaceFieldDataRequest(surfaces=[1], data_types=[SurfaceDataType.Vertices, SurfaceDataType.FacesCentroid])
197-
>>> pressure_request = ScalarFieldDataRequest(surfaces=[1, 2], field_name="pressure", node_value=True, boundary_value=True)
198-
>>> velocity_request = VectorFieldDataRequest(surfaces=[1, 2], field_name="velocity")
205+
>>> vertices_and_centroid_request = SurfaceFieldDataRequest(
206+
>>> surfaces=[1],
207+
>>> data_types=[SurfaceDataType.Vertices, SurfaceDataType.FacesCentroid],
208+
>>> )
209+
>>> pressure_request = ScalarFieldDataRequest(
210+
>>> surfaces=[1, 2],
211+
>>> field_name=VariableCatalog.PRESSURE,
212+
>>> node_value=True,
213+
>>> boundary_value=True,
214+
>>> )
215+
>>> velocity_request = VectorFieldDataRequest(surfaces=[1, 2], field_name=VariableCatalog.VELOCITY)
199216
200217
>>> payload_data = batch.add_requests(vertices_and_centroid_request, pressure_request, velocity_request).get_response()
201218

doc/source/user_guide/fields/reduction.rst

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ Here's how to set up a simple example:
5050
.. code-block:: python
5151
5252
>>> from ansys.fluent.core.solver import VelocityInlets
53+
>>> from ansys.units import VariableCatalog
5354
>>> # Compute the minimum of absolute pressure across multiple solvers
5455
>>> reduction.minimum(
55-
... expression="AbsolutePressure",
56+
... expression=VariableCatalog.ABSOLUTE_PRESSURE,
5657
... locations=VelocityInlets(settings_source=solver1) + VelocityInlets(settings_source=solver2),
5758
... )
5859
101343.2241809384
@@ -69,7 +70,7 @@ To use reduction functions within a specific solver instance, initialize the sol
6970
.. code-block:: python
7071
7172
>>> solver_session.fields.reduction.area_average(
72-
... expression="AbsolutePressure",
73+
... expression=VariableCatalog.ABSOLUTE_PRESSURE,
7374
... locations=solver_session.settings.setup.boundary_conditions.velocity_inlet,
7475
... )
7576
101957.2452989816
@@ -78,7 +79,8 @@ For convenience, context-aware reductions are also supported:
7879

7980
.. code-block:: python
8081
81-
>>> solver_session.fields.reduction.area(locations=["inlet1"])
82+
>>> from ansys.fluent.core.solver import VelocityInlet
83+
>>> solver_session.fields.reduction.area(locations=[VelocityInlet(settings_source=solver_session, name="inlet1")])
8284
0.002555675491754098
8385
8486
>>> reduction.area(locations=["inlet1"], ctxt=solver_session)
@@ -248,7 +250,7 @@ Functional:
248250
.. code-block:: python
249251
250252
>>> reduction.area_average(
251-
... expression="AbsolutePressure",
253+
... expression=VariableCatalog.ABSOLUTE_PRESSURE,
252254
... locations=solver_session.setup.boundary_conditions.velocity_inlet,
253255
... )
254256
101957.2452989816
@@ -258,7 +260,7 @@ Object-Oriented:
258260
.. code-block:: python
259261
260262
>>> solver_session.fields.reduction.area_average(
261-
... expression="AbsolutePressure",
263+
... expression=VariableCatalog.ABSOLUTE_PRESSURE,
262264
... locations=solver_session.settings.setup.boundary_conditions.velocity_inlet,
263265
... )
264266
101957.2452989816
@@ -268,7 +270,7 @@ Object-Oriented:
268270
.. code-block:: python
269271
270272
>>> reduction.minimum(
271-
... expression="AbsolutePressure",
273+
... expression=VariableCatalog.ABSOLUTE_PRESSURE,
272274
... locations=solver1.setup.boundary_conditions.pressure_outlet
273275
... + solver2.setup.boundary_conditions.pressure_outlet,
274276
... )
@@ -279,7 +281,7 @@ Object-Oriented:
279281
.. code-block:: python
280282
281283
>>> reduction.minimum(
282-
... expression="AbsolutePressure",
284+
... expression=VariableCatalog.ABSOLUTE_PRESSURE,
283285
... locations=VelocityInlets(solver1) + VelocityInlets(solver2),
284286
... )
285287
101343.2241809384
@@ -310,9 +312,9 @@ Object-Oriented:
310312
.. code-block:: python
311313
312314
>>> reduction.sum(
313-
>>> expression="AbsolutePressure",
315+
>>> expression=VariableCatalog.ABSOLUTE_PRESSURE,
314316
>>> locations=solver_session.settings.setup.boundary_conditions.velocity_inlet,
315-
>>> weight="Area"
317+
>>> weight=reduction.weight.AREA
316318
>>> )
317319
80349034.56621933
318320
@@ -321,10 +323,10 @@ Object-Oriented:
321323
.. code-block:: python
322324
323325
>>> reduction.sum_if(
324-
>>> expression="AbsolutePressure",
326+
>>> expression=VariableCatalog.ABSOLUTE_PRESSURE,
325327
>>> condition="AbsolutePressure > 0[Pa]",
326328
>>> locations=solver_session.settings.setup.boundary_conditions.velocity_inlet,
327-
>>> weight="Area"
329+
>>> weight=reduction.weight.AREA
328330
>>> )
329331
80349034.56621933
330332

doc/source/user_guide/offline/file_session.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ Single-phase
3131
>>> SurfaceFieldDataRequest,
3232
>>> VectorFieldDataRequest,
3333
>>> )
34+
>>> from ansys.units import VariableCatalog
3435
3536
>>> case_file_name = examples.download_file("elbow1.cas.h5", "pyfluent/file_session")
3637
>>> data_file_name = examples.download_file("elbow1.dat.h5", "pyfluent/file_session")
3738
>>> file_session = FileSession(case_file_name, data_file_name)
3839
39-
>>> file_session.fields.field_data.scalar_fields.range("SV_T")
40+
>>> file_session.fields.field_data.scalar_fields.range(VariableCatalog.TEMPERATURE)
4041
[0.0, 313.1515948109515]
4142
>>> file_session.fields.field_data.surfaces()
4243
{'wall': {'surface_id': [3],
@@ -73,8 +74,8 @@ Single-phase
7374
>>> surfaces=[3, 4],
7475
>>> flatten_connectivity=True,
7576
>>> )
76-
>>> solution_variable_temperature_request = ScalarFieldDataRequest(field_name="SV_T", surfaces=[3, 4], node_value=False, boundary_value=False)
77-
>>> velocity_request = VectorFieldDataRequest(field_name="velocity", surfaces=[3, 4])
77+
>>> solution_variable_temperature_request = ScalarFieldDataRequest(field_name=VariableCatalog.TEMPERATURE, surfaces=[3, 4], node_value=False, boundary_value=False)
78+
>>> velocity_request = VectorFieldDataRequest(field_name=VariableCatalog.VELOCITY, surfaces=[3, 4])
7879
>>> batch.add_requests(vertices_and_faces_connectivity_request, solution_variable_temperature_request, velocity_request)
7980
>>> data = batch.get_response()
8081
>>> data.get_field_data(vertices_and_faces_connectivity_request)[3].vertices
@@ -101,11 +102,11 @@ Single-phase
101102
(3810, 3)
102103
>>> file_session.fields.field_data.get_field_data(vertices_request)[3][1500][0]
103104
0.12405861914157867
104-
>>> file_session.fields.field_data.get_field_data(ScalarFieldDataRequest(field_name="SV_T", surfaces=["wall"]))["wall"].shape
105+
>>> file_session.fields.field_data.get_field_data(ScalarFieldDataRequest(field_name=VariableCatalog.TEMPERATURE, surfaces=["wall"]))["wall"].shape
105106
(3630,)
106-
>>> file_session.fields.field_data.get_field_data(ScalarFieldDataRequest(field_name="SV_T", surfaces=["wall"]))["wall"][1500]
107+
>>> file_session.fields.field_data.get_field_data(ScalarFieldDataRequest(field_name=VariableCatalog.TEMPERATURE, surfaces=["wall"]))["wall"][1500]
107108
293.18071329432047
108-
>>> velocity_request = VectorFieldDataRequest(field_name="velocity", surfaces=["symmetry"])
109+
>>> velocity_request = VectorFieldDataRequest(field_name=VariableCatalog.VELOCITY, surfaces=["symmetry"])
109110
>>> file_session.fields.field_data.get_field_data(velocity_request)["symmetry"].shape
110111
(2018, 3)
111112
>>> file_session.fields.field_data.get_field_data(velocity_request)["symmetry"][1000][0]

src/ansys/fluent/core/file_session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,14 @@ def add_requests(
508508
)
509509
elif isinstance(req, ScalarFieldDataRequest):
510510
self._add_scalar_fields_request(
511-
field_name=req.field_name,
511+
field_name=_to_scalar_field_name(req.field_name),
512512
surfaces=req.surfaces,
513513
node_value=req.node_value,
514514
boundary_value=req.boundary_value,
515515
)
516516
elif isinstance(req, VectorFieldDataRequest):
517517
self._add_vector_fields_request(
518-
field_name=req.field_name,
518+
field_name=_to_vector_field_name(req.field_name),
519519
surfaces=req.surfaces,
520520
)
521521
elif isinstance(req, PathlinesFieldDataRequest):
@@ -1221,7 +1221,7 @@ def _get_surface_ids(
12211221
surface_ids = []
12221222
for surf in surfaces:
12231223
if isinstance(surf, str):
1224-
surface_ids.extend(field_info.get_surfaces_info()[surf]["surface_id"])
1224+
surface_ids.extend(field_info._get_surfaces_info()[surf]["surface_id"])
12251225
else:
12261226
surface_ids.append(surf)
12271227
return surface_ids
@@ -1232,5 +1232,5 @@ class Fields:
12321232

12331233
def __init__(self, _session: FileSession):
12341234
"""Initialize Fields."""
1235-
self.field_info = FileFieldInfo(_session)
1235+
self.field_info = _FileFieldInfo(_session)
12361236
self.field_data = FileFieldData(_session, self.field_info)

src/ansys/fluent/core/services/reduction.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
GrpcErrorInterceptor,
3838
TracingInterceptor,
3939
)
40+
from ansys.fluent.core.solver.function.reduction import Weight
4041
from ansys.fluent.core.variable_strategies import (
4142
FluentExprNamingStrategy as naming_strategy,
4243
)
@@ -315,12 +316,17 @@ def _make_request(
315316
if expression is not None:
316317
request.expression = self._to_str(expression)
317318
if weight is not None:
318-
request.weight = weight
319+
request.weight = Weight(weight).value
319320
if condition is not None:
320321
request.condition = condition
321322
request.locations.extend(self._get_location_string(locations, ctxt))
322323
return request
323324

325+
@property
326+
def weight(self):
327+
"""Weight for calculating sum."""
328+
return Weight
329+
324330
def area(self, locations, ctxt=None) -> Any:
325331
"""Get area."""
326332
request = self._make_request("AreaRequest", locations, ctxt)
@@ -445,13 +451,15 @@ def moment(self, expression, locations, ctxt=None) -> Any:
445451
response = self.service.moment(request)
446452
return (response.value.x, response.value.y, response.value.z)
447453

448-
def sum(self, expression, locations, weight, ctxt=None) -> Any:
454+
def sum(self, expression, locations, weight: str | Weight, ctxt=None) -> Any:
449455
"""Get sum."""
450456
request = self._make_request("SumRequest", locations, ctxt, expression, weight)
451457
response = self.service.sum(request)
452458
return _convert_variant_to_value(response.value)
453459

454-
def sum_if(self, expression, condition, locations, weight, ctxt=None) -> Any:
460+
def sum_if(
461+
self, expression, condition, locations, weight: str | Weight, ctxt=None
462+
) -> Any:
455463
"""Compute the weighted sum of the expression at locations where the given condition is satisfied."""
456464
request = self._make_request(
457465
"SumIfRequest", locations, ctxt, expression, weight, condition

src/ansys/fluent/core/solver/function/reduction.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,28 @@
8080
19.28151
8181
"""
8282
from collections.abc import Iterable
83+
from enum import Enum
8384

8485
import numpy as np
8586
from numpy import array
8687

8788
from ansys.fluent.core.exceptions import DisallowedValuesError
89+
from ansys.fluent.core.variable_strategies import (
90+
FluentExprNamingStrategy as naming_strategy,
91+
)
92+
93+
94+
class Weight(Enum):
95+
"""Weight for sum."""
96+
97+
AREA = "Area"
98+
VOLUME = "Volume"
99+
MASS = "Mass"
100+
MASS_FLOW_RATE = "MassFlowRate"
101+
ABS_MASS_FLOW_RATE = "AbsMassFlowRate"
102+
103+
def __str__(self):
104+
return self.value
88105

89106

90107
class BadReductionRequest(Exception):
@@ -194,7 +211,7 @@ def _eval_reduction(
194211
weight = "Weight=" + str(weight)
195212
locations = str(locations) + ", " + weight
196213

197-
expr_str = _expr_to_expr_str(expr)
214+
expr_str = _expr_to_expr_str(naming_strategy().to_string(expr))
198215
if condition:
199216
expr_str = expr_str + ", " + condition
200217
return _eval_expr(
@@ -298,6 +315,10 @@ def _limit(limit, expr, locations, ctxt):
298315
return limit_val
299316

300317

318+
# Weight for sum
319+
weight = Weight
320+
321+
301322
def area_average(expression, locations, ctxt=None):
302323
"""Compute the area averaged value of the specified expression over the specified
303324
locations.
@@ -586,14 +607,14 @@ def mass_flow(locations, ctxt=None):
586607
return _extent("MassFlow", locations, ctxt)
587608

588609

589-
def sum(expression, locations, weight, ctxt=None):
610+
def sum(expression, locations, weight: str | Weight, ctxt=None):
590611
"""Compute the sum of the specified expression over the specified locations.
591612
592613
Parameters
593614
----------
594615
expression : Any
595616
locations : Any
596-
weight: str
617+
weight: str | Weight
597618
ctxt : Any, optional
598619
Returns
599620
-------
@@ -602,7 +623,7 @@ def sum(expression, locations, weight, ctxt=None):
602623
return _extent_expression("Sum", "Sum", expression, locations, ctxt, weight=weight)
603624

604625

605-
def sum_if(expression, condition, locations, weight, ctxt=None):
626+
def sum_if(expression, condition, locations, weight: str | Weight, ctxt=None):
606627
"""Compute the sum of the specified expression over the specified locations if a
607628
condition is satisfied.
608629
@@ -611,7 +632,7 @@ def sum_if(expression, condition, locations, weight, ctxt=None):
611632
expression : Any
612633
condition: str
613634
locations : Any
614-
weight: str
635+
weight: str | Weight
615636
ctxt : Any, optional
616637
Returns
617638
-------

0 commit comments

Comments
 (0)