Skip to content

PoC / Paraview integration for mesh_doctor #2790

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

Closed
wants to merge 4 commits into from
Closed
Changes from 2 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
57 changes: 57 additions & 0 deletions src/docs/sphinx/pythonTools/mesh_doctor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,60 @@ It will also verify that the ``VTK_POLYHEDRON`` cells can effectively get conver

.. command-output:: python mesh_doctor.py supported_elements --help
:cwd: ../../../coreComponents/python/modules/geosx_mesh_doctor

``Using mesh_doctor in paraview``
""""""""""""""""""""""""""""""""""
To use ``mesh_doctor`` in Paraview as a python programmable filter, a python package install is required first in Paraview python resolved
path. Paraview is storing its python ressources under its *lib/pythonX.X* depending on the paraview version, *e.g* Paraview 5.11 is working
with python 3.9. As a results the following command will install ``mesh_doctor`` package into Paraview resolved path.

.. command-output:: python3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps --upgrade --target /path/to/Paraview/lib/python3.9/ mesh_doctor

.. note::
``pip`` is installing the ``mesh_doctor`` package from the test.pypi repo, which is intended to test package deployment.
Once stabilized and ``mesh_doctor`` uploaded onto the main package repo, this should be dropped out.
Comment on lines +138 to +140
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image 😱 🤣

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hacky I said 😝

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is installing it like this working for you? When I tried it seemed to me that there were some issue in the setup.py that did not allow for this to work. Basically I could not find the modules when trying to import them.

Copy link
Contributor Author

@jafranc jafranc Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this output

Looking in indexes: https://test.pypi.org/simple/
Collecting mesh_doctor
  Downloading https://test-files.pythonhosted.org/packages/97/cc/1ece6916f6e87f95cff4b887f2e6ce50488b146a2e025a959c3665a12d6e/mesh_doctor-0.0.1-py3-none-any.whl (42 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 42.4/42.4 KB 2.1 MB/s eta 0:00:00
Installing collected packages: mesh_doctor
Successfully installed mesh_doctor-0.0.1

and I can checked afterwards that checks is in `/path/to/Paraview/lib/PythonX.X' but this need refactor/cleanup obv.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a workaround you can try while being in mesh_doctor directory conatining the .toml

python3 -m pip install --upgrade build
python3 -m build

which should produce de dist/ directory with .whl and .tar.gz.

The install command on the .tar.gz might also work, as

python3 -m pip install dist/mesh_doctor-0.0.1.tar.gz --target /path/to/paraview/lib/pythonX.X'


Once the installation done, the */path/to/Paraview/lib/pythonX.X* should holds ``mesh_doctor`` package content, *i.e.* ``checks`` and ``parsing``.
Then launching ``Paraview`` and loading our *mesh.vtu*, as an example, we will design a *Programmable python filter* relying on *element_volumes* from
``mesh_doctor``. Add such a filter pipelined after the mesh reader, in the script section paste the following,

.. code-block:: python
:linenos:

mesh = inputs[0].VTKObject
tol = 1.2e-6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know how to define tol from the GUI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That I think would remain to be set from the script. This would maybe possible via Plugin to have a QLineEdit (just guessing though)


from checks import element_volumes
import vtk

res = element_volumes.__check(mesh, element_volumes.Options(tol))
#print(res)
ids = vtk.vtkIdTypeArray()
ids.SetNumberOfComponents(1)
for cell_index, volume in res.element_volumes:
ids.InsertNextValue(cell_index)

selectionNode = vtk.vtkSelectionNode()
selectionNode.SetFieldType(vtk.vtkSelectionNode.CELL)
selectionNode.SetContentType(vtk.vtkSelectionNode.INDICES)
selectionNode.SetSelectionList(ids)
selection = vtk.vtkSelection()
selection.AddNode(selectionNode)
extracted = vtk.vtkExtractSelection()
extracted.SetInputDataObject(0, mesh)
extracted.SetInputData(1, selection)
extracted.Update()
print("There are {} cells under {} m3 vol".format(extracted.GetOutput().GetNumberOfCells(), tol))
output.ShallowCopy(extracted.GetOutput())

Here we rely on ``pyvtk`` interface more than on Paraview adaptation, for legacy and reusability reasons. This is the reason
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that we cannot access vtkSelectionNode from within paraview?
We may want to find a way to detect that we're in the "paraview plugin env" to rely on the paraview vtk not to risk any conflict.
But the requirements.txt part remains. However, we'll move forward!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is exactly that. As I tried to launched it having from paraview import vtk instead of import vtk, I bumped into an error saying that there was no vtkSelectionNode. As Paraview is able to do the operation, I guess this is just that this is not exposed but wrapped. But there must be a way through Paraview, it is just a question of 1. I started from a pyvtk script, which I found more versatile but not Paraview compativle and 2. we might want to rewrite the script in a more Paraview way then.

for the full ``import vtk`` instead of ``from paraview import vtk``, the `vtkSelectionNode` being fully wrapped in paraview
and not accessible otherwise.

On line 7, we leverage ``mesh_doctor`` package to provide us with pairs of `(index,volumes)` of cells with volumes lower
than tolerance `tol`. As input of *Programmable Python Filter* is wrapped in a `dataset_adapter.UnstructuredGrid`, we rely on
the copy of the inital VTKObject `inputs[0].VTKObject` to ensure consistency with our ``pyvtk`` workflow.

What follows is ``pyvtk`` steps in oder to convert into input struct and extract from the original mesh this list of cells.
Eventually, the `extracted` selection is shallow-copied to the output and then accessible in ``Paraview``. An helper print
is left and should be reported in *Output Message* of ``Paraview`` (and in launching terminal if exist).