77
88import numpy as np
99import pyvista as pv
10- from pyvista import _vtk as vtk
11- from pyvista ._vtk import (
12- VTK_HEXAHEDRON ,
13- VTK_PYRAMID ,
14- VTK_QUAD ,
15- VTK_QUADRATIC_HEXAHEDRON ,
16- VTK_QUADRATIC_PYRAMID ,
17- VTK_QUADRATIC_TETRA ,
18- VTK_QUADRATIC_WEDGE ,
19- VTK_TETRA ,
20- VTK_TRIANGLE ,
21- VTK_WEDGE ,
22- )
10+ from pyvista import CellType
2311
2412VTK_VOXEL = 11
2513
@@ -217,7 +205,7 @@ def __repr__(self):
217205
218206 @property
219207 def grid (self ):
220- """``vtk .UnstructuredGrid`` of the archive file.
208+ """Return a ``pyvista .UnstructuredGrid`` of the archive file.
221209
222210 Examples
223211 --------
@@ -258,7 +246,7 @@ def quality(self):
258246 """
259247 if self ._grid is None : # pragma: no cover
260248 raise AttributeError (
261- "Archive must be parsed as a vtk grid.\n " "Set `parse_vtk=True`"
249+ "Archive must be parsed as a vtk grid.\n Set `parse_vtk=True`"
262250 )
263251 return quality (self ._grid )
264252
@@ -388,27 +376,29 @@ def save_as_archive(
388376 if hasattr (grid , "cast_to_unstructured_grid" ):
389377 grid = grid .cast_to_unstructured_grid ()
390378
391- if not isinstance (grid , vtk .vtkUnstructuredGrid ):
392- raise TypeError ("``grid`` argument must be an UnstructuredGrid" )
379+ if not isinstance (grid , pv .UnstructuredGrid ):
380+ raise TypeError (
381+ f"``grid`` argument must be an UnstructuredGrid, not { type (grid )} "
382+ )
393383
394384 allowable = []
395385 if include_solid_elements :
396386 allowable .extend (
397387 [
398- VTK_VOXEL ,
399- VTK_TETRA ,
400- VTK_QUADRATIC_TETRA ,
401- VTK_PYRAMID ,
402- VTK_QUADRATIC_PYRAMID ,
403- VTK_WEDGE ,
404- VTK_QUADRATIC_WEDGE ,
405- VTK_HEXAHEDRON ,
406- VTK_QUADRATIC_HEXAHEDRON ,
388+ CellType . VOXEL ,
389+ CellType . TETRA ,
390+ CellType . QUADRATIC_TETRA ,
391+ CellType . PYRAMID ,
392+ CellType . QUADRATIC_PYRAMID ,
393+ CellType . WEDGE ,
394+ CellType . QUADRATIC_WEDGE ,
395+ CellType . HEXAHEDRON ,
396+ CellType . QUADRATIC_HEXAHEDRON ,
407397 ]
408398 )
409399
410400 if include_surface_elements :
411- allowable .extend ([VTK_TRIANGLE , VTK_QUAD ])
401+ allowable .extend ([CellType . TRIANGLE , CellType . QUAD ])
412402 # VTK_QUADRATIC_TRIANGLE,
413403 # VTK_QUADRATIC_QUAD
414404
@@ -432,7 +422,7 @@ def save_as_archive(
432422 nodenum = grid .point_data ["ansys_node_num" ]
433423 else :
434424 log .info ("No ANSYS node numbers set in input. Adding default range" )
435- nodenum = np .arange (1 , grid .number_of_points + 1 , dtype = np .int32 )
425+ nodenum = np .arange (1 , grid .n_points + 1 , dtype = np .int32 )
436426
437427 missing_mask = nodenum == - 1
438428 if np .any (missing_mask ):
@@ -457,12 +447,12 @@ def save_as_archive(
457447 nodenum [missing_mask ] = np .arange (start_num , end_num , dtype = np .int32 )
458448
459449 # element block
460- ncells = grid .number_of_cells
450+ ncells = grid .n_cells
461451 if "ansys_elem_num" in grid .cell_data :
462452 enum = grid .cell_data ["ansys_elem_num" ]
463453 else :
464454 if not allow_missing :
465- raise Exception ('Missing node numbers. Exiting due "allow_missing=False"' )
455+ raise Exception ('Missing node numbers. Exiting due "allow_missing=False"' )
466456 log .info (
467457 "No ANSYS element numbers set in input. "
468458 "Adding default range starting from %d" ,
@@ -483,7 +473,7 @@ def save_as_archive(
483473 nadd = np .sum (enum == - 1 )
484474 end_num = start_num + nadd
485475 log .info (
486- "FEM missing some cell numbers. Adding numbering " " from %d to %d" ,
476+ "FEM missing some cell numbers. Adding numbering from %d to %d" ,
487477 start_num ,
488478 end_num ,
489479 )
@@ -555,37 +545,50 @@ def save_as_archive(
555545 + "Adding default range starting from %d" % etype_start
556546 )
557547
558- etype = np .empty (grid .number_of_cells , np .int32 )
559- etype_185 = etype_start + 2
560- etype [grid .celltypes == VTK_VOXEL ] = etype_185
561- etype [grid .celltypes == VTK_TETRA ] = etype_185
562- etype [grid .celltypes == VTK_HEXAHEDRON ] = etype_185
563- etype [grid .celltypes == VTK_WEDGE ] = etype_185
564- etype [grid .celltypes == VTK_PYRAMID ] = etype_185
548+ etype = np .empty (grid .n_cells , np .int32 )
565549
550+ # VTK to SOLID186 mapping
551+ # TETRA delegated to SOLID187
566552 etype_186 = etype_start
567- etype [grid .celltypes == VTK_QUADRATIC_HEXAHEDRON ] = etype_186
568- etype [grid .celltypes == VTK_QUADRATIC_WEDGE ] = etype_186
569- etype [grid .celltypes == VTK_QUADRATIC_PYRAMID ] = etype_186
553+ etype_186_types = [
554+ CellType .QUADRATIC_HEXAHEDRON ,
555+ CellType .QUADRATIC_WEDGE ,
556+ CellType .QUADRATIC_PYRAMID ,
557+ ]
558+ etype [np .isin (grid .celltypes , etype_186_types )] = etype_186
570559
571560 etype_187 = etype_start + 1
572- etype [grid .celltypes == VTK_QUADRATIC_TETRA ] = etype_187
561+ etype [grid .celltypes == CellType .QUADRATIC_TETRA ] = etype_187
562+
563+ # VTK to SOLID185 mapping
564+ etype_185 = etype_start + 2
565+ etype_185_types = [
566+ CellType .VOXEL ,
567+ CellType .TETRA ,
568+ CellType .HEXAHEDRON ,
569+ CellType .WEDGE ,
570+ CellType .PYRAMID ,
571+ ]
572+ etype [np .isin (grid .celltypes , etype_185_types )] = etype_185
573573
574574 # Surface elements
575575 etype_181 = etype_start + 3
576- etype [grid .celltypes == VTK_TRIANGLE ] = etype_181
577- etype [grid .celltypes == VTK_QUAD ] = etype_181
576+ etype_181_types = [
577+ CellType .TRIANGLE ,
578+ CellType .QUAD ,
579+ ]
580+ etype [np .isin (grid .celltypes , etype_181_types )] = etype_181
578581
579582 typenum = np .empty_like (etype )
580583 typenum [etype == etype_185 ] = 185
581584 typenum [etype == etype_186 ] = 186
582585 typenum [etype == etype_187 ] = 187
583586 typenum [etype == etype_181 ] = 181
584587
585- header += "ET, %d, 185\n " % etype_185
586- header += "ET, %d, 186\n " % etype_186
587- header += "ET, %d, 187\n " % etype_187
588- header += "ET, %d, 181\n " % etype_181
588+ header += f "ET,{ etype_185 } , 185\n "
589+ header += f "ET,{ etype_186 } , 186\n "
590+ header += f "ET,{ etype_187 } , 187\n "
591+ header += f "ET,{ etype_181 } , 181\n "
589592
590593 # number of nodes written per element
591594 elem_nnodes = np .empty (etype .size , np .int32 )
@@ -623,7 +626,7 @@ def save_as_archive(
623626 write_nblock (filename , nodenum , grid .points , mode = "a" )
624627
625628 # write remainder of eblock
626- cells , offset = vtk_cell_info (grid , shift_offset = False )
629+ cells , offset = vtk_cell_info (grid , force_int64 = False , shift_offset = False )
627630 _write_eblock (
628631 filename ,
629632 enum ,
@@ -798,26 +801,16 @@ def _write_eblock(
798801):
799802 """Write EBLOCK to disk"""
800803 # perform type checking here
801- if elem_id .dtype != np .int32 :
802- elem_id = elem_id .astype (np .int32 )
803- if etype .dtype != np .int32 :
804- etype = etype .astype (np .int32 )
805- if mtype .dtype != np .int32 :
806- mtype = mtype .astype (np .int32 )
807- if rcon .dtype != np .int32 :
808- rcon = rcon .astype (np .int32 )
809- if elem_nnodes .dtype != np .int32 :
810- elem_nnodes = elem_nnodes .astype (np .int32 )
811- if cells .dtype != np .int64 :
812- cells = cells .astype (np .int64 )
813- if offset .dtype != np .int64 :
814- offset = offset .astype (np .int64 )
815- if celltypes .dtype != np .uint8 :
816- celltypes = celltypes .astype (np .uint8 )
817- if typenum .dtype != np .int32 :
818- typenum = typenum .astype (np .int32 )
819- if nodenum .dtype != np .int32 :
820- nodenum = nodenum .astype (np .int32 )
804+ elem_id = elem_id .astype (np .int32 , copy = False )
805+ etype = etype .astype (np .int32 , copy = False )
806+ mtype = mtype .astype (np .int32 , copy = False )
807+ rcon = rcon .astype (np .int32 , copy = False )
808+ elem_nnodes = elem_nnodes .astype (np .int32 , copy = False )
809+ cells = cells .astype (np .int32 , copy = False )
810+ offset = offset .astype (np .int32 , copy = False )
811+ celltypes = celltypes .astype (np .uint8 , copy = False )
812+ typenum = typenum .astype (np .int32 , copy = False )
813+ nodenum = nodenum .astype (np .int32 , copy = False )
821814
822815 _archive .py_write_eblock (
823816 filename ,
0 commit comments