Skip to content

Commit

Permalink
cython._binary_reader: cells_with_*_nodes: Generalize and simplify by…
Browse files Browse the repository at this point in the history
… directly using `offset` for next cell offset; close: `_file_out` does not exist; correct pyvista version check for version 0.32.dev0 (#74)

* Result._plot_point_scalars: Use argument return_cpos in any case

... before, it was used only, when `if animate`. Now, it used in any case of the call `plotter.show`.

* Result._plot_point_scalars: fix screenshot function on Windows OS

pyvista\plotting\plotting.py:4882: On Windows OS "in the event that the user hits
the exit-button on the GUI then it must be finalized and deleted as accessing it
will kill the kernel... proper screenshots cannot be saved if this happens"
Therefore, when you used ´_plot_point_scalars(..., screenshot=...)´, you got at
ansys\mapdl\reader\rst.py:2830:
´RuntimeError: This plotter is closed and unable to save a screenshot.´

The only solution is, to pass the ´screenshot´ argument to ´plotter.show´
as done in this patch.

* Result._plot_point_scalars: future-safe pyvista version check

... the old version would break with version 1.0.0.
I used the same compare operation as in
<https://github.com/rbarrois/python-semanticversion/blob/master/semantic_version/base.py>
tested manually.

* cython._binary_reader.cells_with_*_nodes: Generalize and simplify by directly using `offset` for next cell offset

- Therefore the function `cell_lookup`, which was limited to 3D cells, is no longer necessary.
- Now, all cell types are supported, while the functions has even been simplified.
- Now, the functions do not need the parameter `celltypes` any more. Since they are only used in rst.py in the private method `Result._extract_node_components`, I took the liberty to remove the parameter A in the respective definition und the respective call.

Remark: Only tested with a pure python version of the functions, please test the cython function of the commit!

* cython._binary_reader.cells_with_*_nodes: correct expression for `ncells`

* _binary.reader: _file_out does not exist

When you called `AnsysFile.close()` you got at line 255

   'ansys.mapdl.reader._binary_reader.AnsysFile' object has no attribute '_file_out'

as `_file_out` is never created (this line is the only occurrence).

* Result._plot_point_scalars: correct pyvista version check for version 0.32.dev0

The actual development version is 0.32.dev0: `pv._version.version_info = (0, 32, "dev0")`. There, the string `"dev0"` can not be compared with `0`, so that comparison failed for this version. To solve this, I removed the last element on the right-hand side expression of the comparison. The new comparison `(0, 32, "dev0") > (0, 32)` works now as expected. Tested manually.
  • Loading branch information
beppo-dd authored Nov 8, 2021
1 parent 827437a commit 5658ddf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
39 changes: 11 additions & 28 deletions ansys/mapdl/reader/cython/_binary_reader.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ cdef class AnsysFile:
def close(self):
"""Close the file"""
del self._file
del self._file_out

def read_element_data(self, int64_t [::1] ele_ind_table, int table_index,
int64_t ptr_off):
Expand Down Expand Up @@ -1638,61 +1637,45 @@ def affline_transform(float_or_double [:, ::1] points, float_or_double [:, ::1]
points[i, 2] = t20*x + t21*y + t22*z + t23


cdef inline int cell_lookup(uint8 celltype) nogil:
if celltype == VTK_HEXAHEDRON or celltype == VTK_QUADRATIC_HEXAHEDRON:
return 8
elif celltype == VTK_TETRA or celltype == VTK_QUADRATIC_TETRA:
return 4
elif celltype == VTK_PYRAMID or celltype == VTK_QUADRATIC_PYRAMID:
return 5
elif celltype == VTK_WEDGE or celltype == VTK_QUADRATIC_WEDGE:
return 6


def cells_with_all_nodes(index_type [::1] offset, index_type [::1] cells,
uint8 [::1] celltypes, uint8 [::1] point_mask):
uint8 [::1] point_mask):
"""
Updates mask of cells containing all points in the point indices
or mask.
"""
cdef int ncells = celltypes.size
cdef uint8 celltype
cdef int ncell_points, i, j
cdef index_type cell_offset
cdef int ncells = offset.size - 1
cdef int i, j
cdef index_type cell_offset, next_cell_offset
cdef uint8 [::1] cell_mask = np.ones(ncells, np.uint8)

with nogil:
for i in range(ncells):
celltype = celltypes[i]
ncell_points = cell_lookup(celltype)
cell_offset = offset[i] + 1
for j in range(cell_offset, cell_offset + ncell_points):
next_cell_offset = offset[i+1] + 1
for j in range(cell_offset, next_cell_offset):
if point_mask[cells[j]] != 1:
cell_mask[i] = 0

return np.asarray(cell_mask, dtype=np.bool)


def cells_with_any_nodes(index_type [::1] offset, index_type [::1] cells,
uint8 [::1] celltypes, uint8 [::1] point_mask):
uint8 [::1] point_mask):
"""
Updates mask of cells containing at least one point in the point
indices or mask.
"""
cdef int ncells = celltypes.size
cdef uint8 celltype
cdef int ncell_points
cdef index_type cell_offset
cdef int ncells = offset.size - 1
cdef index_type cell_offset, next_cell_offset
cdef int i, j

cdef uint8 [::1] cell_mask = np.zeros(ncells, np.uint8)

with nogil:
for i in range(ncells):
celltype = celltypes[i]
ncell_points = cell_lookup(celltype)
cell_offset = offset[i] + 1
for j in range(cell_offset, cell_offset + ncell_points):
next_cell_offset = offset[i+1] + 1
for j in range(cell_offset, next_cell_offset):
if point_mask[cells[j]] == 1:
cell_mask[i] = 1
break
Expand Down
6 changes: 3 additions & 3 deletions ansys/mapdl/reader/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,10 @@ def _extract_node_components(self, node_components,
# need to extract the mesh
cells, offset = vtk_cell_info(grid)
if sel_type_all:
cell_mask = cells_with_all_nodes(offset, cells, grid.celltypes,
cell_mask = cells_with_all_nodes(offset, cells,
mask.view(np.uint8))
else:
cell_mask = cells_with_any_nodes(offset, cells, grid.celltypes,
cell_mask = cells_with_any_nodes(offset, cells,
mask.view(np.uint8))

if not cell_mask.any():
Expand Down Expand Up @@ -2754,7 +2754,7 @@ def _plot_point_scalars(self, scalars, rnum=None, grid=None,

# camera position added in 0.32.0
show_kwargs = {}
if pv._version.version_info >= (0, 32, 0):
if pv._version.version_info >= (0, 32):
show_kwargs['return_cpos'] = return_cpos

if animate:
Expand Down

0 comments on commit 5658ddf

Please sign in to comment.