Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code snippets #1270

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/source/getting_started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ Use PyDPF-Core

To use PyDPF-Core, in the same Python environment, run this command:

.. code:: python
.. code-block:: python

>>> from ansys.dpf import core as dpf
>>> from ansys.dpf.core import examples
>>> model = dpf.Model(examples.download_crankshaft())
>>> print(model)
from ansys.dpf import core as dpf
from ansys.dpf.core import examples
model = dpf.Model(examples.download_crankshaft())
print(model)


.. rst-class:: sphx-glr-script-out
Expand Down Expand Up @@ -122,10 +122,10 @@ To use PyDPF-Core, in the same Python environment, run this command:



.. code:: python
.. code-block:: python

>>> over_time_disp = model.results.displacement().eval()
>>> over_time_disp[0].plot()
over_time_disp = model.results.displacement().eval()
over_time_disp[0].plot()


.. figure:: ../images/plotting/crankshaft_disp.png
Expand Down
16 changes: 8 additions & 8 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ Brief demo
Here is how you open a result file generated by MAPDL (or another ANSYS solver)
and extract results:

.. code:: python
.. code-block:: python

>>> from ansys.dpf.core import Model
>>> from ansys.dpf.core import examples
>>> model = Model(examples.find_simple_bar())
>>> print(model)
from ansys.dpf.core import Model
from ansys.dpf.core import examples
model = Model(examples.find_simple_bar())
print(model)


Here is how you plot displacement results:

.. code:: python
.. code-block:: python

>>> disp = model.results.displacement().X()
>>> model.metadata.meshed_region.plot(disp.outputs.fields_container())
disp = model.results.displacement().X()
model.metadata.meshed_region.plot(disp.outputs.fields_container())

For comprehensive examples of how you use PyDPF-Core, see :ref:`gallery`.

Expand Down
147 changes: 97 additions & 50 deletions docs/source/user_guide/fields_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ The field's scoping defines the order of the data, for example: the first ID in

To access the scoping of the field, use the ``scoping`` attribute:

.. code::
.. code-block:: python

>>> print(field.scoping)
>>> print('field.scoping.ids:', field.scoping.ids)
>>> print('field.location:', field.location)
print(field.scoping)
print('field.scoping.ids:', field.scoping.ids)
print('field.location:', field.location)


.. rst-class:: sphx-glr-script-out
Expand Down Expand Up @@ -333,26 +333,21 @@ the data stored, location of the field, number of components, and
units of the data:


.. code::
.. code-block:: python

>>> stress = model.results.stress
>>> field = stress.eval()[0]
stress = model.results.stress
field = stress.eval()[0]

Units of the field describing volume
# Units of the field describing volume
field.unit

>>> field.unit


Location of the field (Elemental, ElementalNodal, or Nodal)

>>> field.location

Number of components associated with the field. It's expected to
be a single dimension because there can only be one volume per
element.

>>> field.component_count
#Location of the field (Elemental, ElementalNodal, or Nodal)
field.location

# Number of components associated with the field. It's expected to
# be a single dimension because there can only be one volume per
# element.
field.component_count


.. rst-class:: sphx-glr-script-out
Expand All @@ -378,10 +373,15 @@ that is needed.
The field's ``data`` is ordered with respect to its ``scoping ids`` (see above).
To access the entire array of data as a ``numpy`` array:

.. code::
.. code-block:: python

array = field.data
print(array)

.. rst-class:: sphx-glr-script-out

.. code-block:: none

>>> array = field.data
>>> array
DPFArray([[ 4.01372930e+04, 3.85071930e+02, -1.40019130e+07,
7.48472351e+02, -2.60259531e+04, -2.62856938e+05],
[-1.19228638e+03, -6.18210815e+02, -1.39912700e+07,
Expand All @@ -396,20 +396,32 @@ To access the entire array of data as a ``numpy`` array:
[ 5.56899536e+02, 3.88515320e+02, 1.17119880e+07,
-1.68983887e+03, -1.21768023e+05, -2.41346125e+05]])

This array has 6 components by elementary data (symmetrical tensor XX,YY,ZZ,XY,YZ,XZ).
Note that this array is a genuine, local, numpy array (overloaded by the DPFArray).
This array has 6 components by elementary data (symmetrical tensor XX,YY,ZZ,XY,YZ,XZ).
Note that this array is a genuine, local, numpy array (overloaded by the DPFArray).

.. code-block:: python

print(type(array))

.. rst-class:: sphx-glr-script-out

.. code-block:: none

>>> type(array)
<class 'ansys.dpf.gate.dpf_array.DPFArray'>

If you need to access an individual node or element, request it
using either the ``get_entity_data()`` or ``get_entity_data_by_id()`` method:

.. code::
Get the data from the first element in the field.

.. code-block:: python

field.get_entity_data(0)

Get the data from the first element in the field.
.. rst-class:: sphx-glr-script-out

.. code-block:: none

>>> field.get_entity_data(0)
DPFArray([[ 4.01372930e+04, 3.85071930e+02, -1.40019130e+07,
7.48472351e+02, -2.60259531e+04, -2.62856938e+05],
[-1.19228638e+03, -6.18210815e+02, -1.39912700e+07,
Expand All @@ -427,9 +439,16 @@ using either the ``get_entity_data()`` or ``get_entity_data_by_id()`` method:
[-3.97351016e+04, 2.43927902e+02, 1.17250040e+07,
6.08326172e+02, -2.46618770e+04, 2.43019891e+05]])

Get the data for the element with ID 10.
Get the data for the element with ID 10.

.. code-block:: python

field.get_entity_data_by_id(10)

.. rst-class:: sphx-glr-script-out

.. code-block:: none

>>> field.get_entity_data_by_id(10)
DPFArray([[ 4.99232031e+04, 1.93570602e+02, -3.08514075e+06,
-5.48255615e+02, -1.37476562e+04, 1.34827719e+05],
[ 5.23090469e+04, -1.87847885e+02, -1.98004588e+06,
Expand All @@ -447,28 +466,43 @@ using either the ``get_entity_data()`` or ``get_entity_data_by_id()`` method:
[-2.63994102e+01, -1.50429443e+02, 3.06906050e+06,
-1.17046619e+03, -6.76924219e+04, -1.34773391e+05]])

Note that this would correspond to an index of 29 within the
field. Be aware that scoping IDs are not sequential. You would
get the index of element 29 in the field with:
Note that this would correspond to an index of 29 within the
field. Be aware that scoping IDs are not sequential. You would
get the index of element 29 in the field with:

.. code-block:: python

field.scoping.ids.index(10)

.. rst-class:: sphx-glr-script-out

.. code-block:: none

>>> field.scoping.ids.index(10)
29

Here the data for the element with ID 10 is made of 8 symmetrical tensors.
The elastic strain has one tensor value by node by element (ElementalNodal location)
Here the data for the element with ID 10 is made of 8 symmetrical tensors.
The elastic strain has one tensor value by node by element (ElementalNodal location)

To get the displacement on node 3, you would use:
>>> disp = model.results.displacement.eval()[0]
>>> disp.get_entity_data_by_id(3)
To get the displacement on node 3, you would use:

.. code-block:: python

disp = model.results.displacement.eval()[0]
disp.get_entity_data_by_id(3)

.. rst-class:: sphx-glr-script-out

.. code-block:: none

DPFArray([[8.06571808e-14, 4.03580652e-04, 2.61804706e-05]])

One 3D vector (X,Y,Z) displacement
One 3D vector (X,Y,Z) displacement

While these methods are acceptable when requesting data for a few elements
or nodes, they should not be used when looping over the entire array. For efficiency,
a field's data can be recovered locally before sending a large number of requests:

.. code-block::
.. code-block:: python

with field.as_local_field() as f:
for i in range(1,100):
Expand All @@ -488,26 +522,39 @@ from the field itself.
This example uses the ``min_max`` operator to compute the maximum of
the field while returning the field:

.. code::
Compute the maximum of the field within DPF and return the result
in a numpy array

Compute the maximum of the field within DPF and return the result
in a numpy array
.. code-block:: python

max_field = field.max()
max_field.data

.. rst-class:: sphx-glr-script-out

.. code-block:: none

>>> max_field = field.max()
>>> max_field.data
DPFArray([22083762. , 22406040. , 52603044. , 1623704.25, 2443320.75,
5014283.5 ])

Get the element or node ID of the maximum value.
Get the element or node ID of the maximum value.


.. code-block:: python

max_field.scoping.ids

.. rst-class:: sphx-glr-script-out

.. code-block:: none

>>> max_field.scoping.ids
DPFArray([39, 39, 39, 40, 39, 39])


This example uses the ``elemental_mean`` operator to compute the
average of a field:

.. code-block::
.. code-block:: python

from ansys.dpf.core import operators as ops
avg_op = ops.averaging.elemental_mean(field)
Expand Down
22 changes: 10 additions & 12 deletions docs/source/user_guide/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To create an instance of the ``Model`` object, import the ``pydpf-core`` package
load a result file. The path that you provide must be an absolute path
or a path relative to the DPF server.

.. code-block:: default
.. code-block:: python

from ansys.dpf import core as dpf
from ansys.dpf.core import examples
Expand All @@ -23,12 +23,10 @@ or a path relative to the DPF server.
To understand what is available in the result file, you can print the model
(or any other instance):

.. code-block:: default
.. code-block:: python

print(model)



.. rst-class:: sphx-glr-script-out

.. code-block:: none
Expand Down Expand Up @@ -80,7 +78,7 @@ To access all information about an analysis, you can use model metadata:
This example shows how you get the analysis type:


.. code-block:: default
.. code-block:: python

model.metadata.result_info.analysis_type

Expand All @@ -93,11 +91,11 @@ This example shows how you get the analysis type:
This example shows how you get mesh information:


.. code:: default
.. code:: python

>>> model.metadata.meshed_region.nodes.n_nodes
>>> model.metadata.meshed_region.elements.n_elements
>>> print(model.metadata.meshed_region.elements.element_by_id(1))
model.metadata.meshed_region.nodes.n_nodes
model.metadata.meshed_region.elements.n_elements
print(model.metadata.meshed_region.elements.element_by_id(1))

.. rst-class:: sphx-glr-script-out

Expand All @@ -115,7 +113,7 @@ This example shows how you get mesh information:
This example shows how you get time sets:


.. code-block:: default
.. code-block:: python

time_freq_support = model.metadata.time_freq_support
print(time_freq_support.time_frequencies.data)
Expand All @@ -137,7 +135,7 @@ create operators to access certain results.
This example shows how you view available results:


.. code-block:: default
.. code-block:: python

print(model.results)

Expand Down Expand Up @@ -170,7 +168,7 @@ on which to get a given result is straightforward.
This example shows how you get displacement results on all time frequencies on
the mesh scoping:

.. code-block:: default
.. code-block:: python

disp_result = model.results.displacement
disp_at_all_times_on_node_1 = disp_result.on_all_time_freqs.on_mesh_scoping([1])
Expand Down
Loading
Loading