Skip to content

Commit

Permalink
fix parse_coordinate_system (#340)
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Pastor Muela <[email protected]>
  • Loading branch information
akaszynski and RobPasMue authored Nov 16, 2023
1 parent 28119a0 commit 288d835
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
7 changes: 7 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ Please see the `PyMAPDL-Reader Documentation
interface using the same software used within Ansys Mechanical, but
via a Python client.

.. note::

Result file compatibility will be greatly improved by disabling result file
compression by setting ``/FCOMP,RST,0``.

DPF does not have this restriction.


Installation
------------
Expand Down
67 changes: 52 additions & 15 deletions ansys/mapdl/reader/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,15 @@ def _read_result_header(self):
return resultheader

def parse_coordinate_system(self):
"""Reads in coordinate system information from a binary result
file.
"""Reads in coordinate system information from a binary result file.
Returns
-------
c_systems : dict
Dictionary containing one entry for each defined
coordinate system. If no non-standard coordinate systems
have been defined, there will be only one None. First
coordinate system is assumed to be global cartesian.
Dictionary containing one entry for each defined coordinate system.
If no non-standard coordinate systems have been defined, an empty
dictionary will be returned. First coordinate system is assumed to
be global cartesian.
Notes
-----
Expand All @@ -325,8 +324,9 @@ def parse_coordinate_system(self):
- 1: Cylindrical (circular or elliptical)
- 2: Spherical (or spheroidal)
- 3: Toroidal
"""
c_systems = [None]
c_systems = {}

# load coordinate system index table
ptr_csy = self._geometry_header["ptrCSY"]
Expand All @@ -353,7 +353,7 @@ def parse_coordinate_system(self):
# * Item 22 is the coordinate system reference number.
for csys_record_pointer in csys_record_pointers:
if not csys_record_pointer:
c_system = None
continue
else:
data = self.read_record(ptr_csy + csys_record_pointer)
c_system = {
Expand All @@ -365,11 +365,9 @@ def parse_coordinate_system(self):
"theta singularity": data[18],
"phi singularity": data[19],
"type": int(data[20]),
"reference num": int(
data[21],
),
"reference num": int(data[21]),
}
c_systems.append(c_system)
c_systems[c_system["reference num"]] = c_system

return c_systems

Expand Down Expand Up @@ -2882,8 +2880,48 @@ def plot_principal_nodal_stress(
)

def cs_4x4(self, cs_cord, as_vtk_matrix=False):
"""return a 4x4 transformation array for a given coordinate system"""
# assemble 4 x 4 matrix
"""Return a 4x4 transformation matrix for a given coordinate system.
Parameters
----------
cs_cord : int
Coordinate system index.
as_vtk_matrix : bool, default: False
Return the transformation matrix as a ``vtkMatrix4x4``.
Returns
-------
np.ndarray | vtk.vtkMatrix4x4
Matrix or ``vtkMatrix4x4`` depending on the value of ``as_vtk_matrix``.
Notes
-----
Values 11 and greater correspond to local coordinate systems
Examples
--------
Return the transformation matrix for coordinate system 1.
>>> tmat = rst.cs_4x4(1)
>>> tmat
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
Return the transformation matrix for coordinate system 5. This
corresponds to ``CSYS, 5``, the cylindrical with global Cartesian Y as
the axis of rotation.
>>> tmat = rst.cs_4x4(5)
>>> tmat
array([[ 1., 0., 0., 0.],
[ 0., 0., -1., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 0., 1.]])
"""
csys = self._c_systems[cs_cord]
trans = np.hstack(
(csys["transformation matrix"], csys["origin"].reshape(-1, 1))
Expand All @@ -2892,7 +2930,6 @@ def cs_4x4(self, cs_cord, as_vtk_matrix=False):

if as_vtk_matrix:
return matrix

return pv.array_from_vtkmatrix(matrix)

def _plot_point_scalars(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_cyclic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
from pyvista.plotting import system_supports_plotting
from pyvista.plotting.renderer import CameraPosition
from vtkmodules.vtkCommonMath import vtkMatrix4x4

from ansys.mapdl import reader as pymapdl_reader
from ansys.mapdl.reader import examples
Expand Down Expand Up @@ -492,3 +493,11 @@ def test_nodal_thermal_strain_cyclic(result_x):
@skip_plotting
def test_plot_nodal_thermal_strain(result_x):
result_x.plot_nodal_thermal_strain(0, "X")


def test_cs_4x4(result_x):
assert isinstance(result_x._c_systems, dict)

# expect first CSYS to be cartesian
assert np.allclose(result_x.cs_4x4(1), np.eye(4))
assert isinstance(result_x.cs_4x4(1, as_vtk_matrix=True), vtkMatrix4x4)

0 comments on commit 288d835

Please sign in to comment.