Skip to content

gradient calculation and visualization issue for ansys 2D #242

Description

@lordlzy

Describe the bug
issue by using "import_mesh_ansys.ipynb" for 2D ansys rst

To Reproduce
run import_mesh_ansys.ipynb with a 2D ansys rst (my example is with 4 & 3 nodes per elements)

change line: grad1=pylife_mesh.gradient.gradient_of('mises')

code that triggers the bug

import pyvista as pv
pv.close_all()
pv.set_jupyter_backend('trame')

mpl.style.use('bmh')

grid = pv.UnstructuredGrid(*pylife_mesh.mesh.vtk_data())
plotter = pv.Plotter(window_size=[1920, 1080])
plotter.add_mesh(grid, scalars=pylife_mesh.groupby('element_id')['S1'].mean().to_numpy(),
show_edges=True, cmap='jet')
plotter.add_scalar_bar()
plotter.show(jupyter_backend='ipyvtklink')

Expected result
calculation of stress gradient
and visualization

Observed result

gradient calculation not able to be finished
& visualization error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[12], line 8
      4 
      5 # import profile
      6 mpl.style.use('bmh')
      7 
----> 8 grid = pv.UnstructuredGrid(*pylife_mesh.mesh.vtk_data())
      9 plotter = pv.Plotter(window_size=[1920, 1080])
     10 plotter.add_mesh(grid, scalars=pylife_mesh.groupby('element_id')['S1'].mean().to_numpy(),
     11                 show_edges=True, cmap='jet')

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pylife\mesh\meshsignal.py:260, in Mesh.vtk_data(self)
    257     selection = points.index.isin(nodes)
    258     return points[selection]
--> 260 connectivity, cell_types = calc_cells()
    261 points = first_order_points(connectivity)
    262 cells = cells_with_lengths(points.index, connectivity)

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pylife\mesh\meshsignal.py:252, in Mesh.vtk_data.<locals>.calc_cells()
    249     choice = count == total_num
    250     connectivity[choice] = connectivity[choice].apply(lambda nds: nds[:first_order_num])
--> 252 return connectivity, count.apply(lambda c: element_types_dict[c][1]).to_numpy()

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pandas\core\series.py:4943, in Series.apply(self, func, convert_dtype, args, by_row, **kwargs)
   4808 def apply(
   4809     self,
   4810     func: AggFuncType,
   (...)   4815     **kwargs,
   4816 ) -> DataFrame | Series:
   4817     """
   4818     Invoke function on values of Series.
   4819 
   (...)   4934     dtype: float64
   4935     """
   4936     return SeriesApply(
   4937         self,
   4938         func,
   4939         convert_dtype=convert_dtype,
   4940         by_row=by_row,
   4941         args=args,
   4942         kwargs=kwargs,
-> 4943     ).apply()

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pandas\core\apply.py:1422, in SeriesApply.apply(self)
   1419     return self.apply_compat()
   1421 # self.func is Callable
-> 1422 return self.apply_standard()

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pandas\core\apply.py:1502, in SeriesApply.apply_standard(self)
   1496 # row-wise access
   1497 # apply doesn't have a `na_action` keyword and for backward compat reasons
   1498 # we need to give `na_action="ignore"` for categorical data.
   1499 # TODO: remove the `na_action="ignore"` when that default has been changed in
   1500 #  Categorical (GH51645).
   1501 action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None
-> 1502 mapped = obj._map_values(
   1503     mapper=curried, na_action=action, convert=self.convert_dtype
   1504 )
   1506 if len(mapped) and isinstance(mapped[0], ABCSeries):
   1507     # GH#43986 Need to do list(mapped) in order to get treated as nested
   1508     #  See also GH#25959 regarding EA support
   1509     return obj._constructor_expanddim(list(mapped), index=obj.index)

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pandas\core\base.py:925, in IndexOpsMixin._map_values(self, mapper, na_action, convert)
    922 if isinstance(arr, ExtensionArray):
    923     return arr.map(mapper, na_action=na_action)
--> 925 return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert)

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pandas\core\algorithms.py:1743, in map_array(arr, mapper, na_action, convert)
   1741 values = arr.astype(object, copy=False)
   1742 if na_action is None:
-> 1743     return lib.map_infer(values, mapper, convert=convert)
   1744 else:
   1745     return lib.map_infer_mask(
   1746         values, mapper, mask=isna(values).view(np.uint8), convert=convert
   1747     )

File pandas/_libs/lib.pyx:2999, in pandas._libs.lib.map_infer()
-> 2999 'Could not get source, probably due dynamically evaluated source code.'

File d:\PE0_Repo\pe0-repo\.venv\Lib\site-packages\pylife\mesh\meshsignal.py:252, in Mesh.vtk_data.<locals>.calc_cells.<locals>.<lambda>(c)
    249     choice = count == total_num
    250     connectivity[choice] = connectivity[choice].apply(lambda nds: nds[:first_order_num])
--> 252 return connectivity, count.apply(lambda c: element_types_dict[c][1]).to_numpy()

KeyError: 3

Environment (please complete the following information):

  • OS: windows
  • How installed uv
  • Version [2.2.1]

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions