Skip to content
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

Add magnetic field example #3058

Merged
merged 23 commits into from
Mar 27, 2023
Merged

Add magnetic field example #3058

merged 23 commits into from
Mar 27, 2023

Conversation

akaszynski
Copy link
Member

@akaszynski akaszynski commented Jul 23, 2022

Adds two new example datasets generated from emg3d - Minimum working example. @prisae, your stuff is awesome, and I've been wanting to plot magnetic fields for a month now.

>>> import numpy as np
>>> from pyvista import examples
>>> grid = examples.download_dipole_hfield()
>>> data = (grid['hfield_fx']*5E10)
>>> grid['data'] = data
>>> grid.plot(volume=True, opacity='sigmoid_4')
ezgif.com-gif-maker.1.mp4

Bonus

  • Improve our opacity parameter docstring in add_volume.
  • Improve our invalid opacity error.

Notes

It was entirely not obvious that the scalars had to be between 0 and 255 for this to work. We should either provide more examples or perhaps a warning when the scalars are np.uint8, or ideally both. Volumetric plots are too cool to not have this spelled out.

@github-actions github-actions bot added the documentation Anything related to the documentation/website label Jul 23, 2022
@adeak
Copy link
Member

adeak commented Jul 23, 2022

Very nice!

When I run your example block locally, I get (beside the plot)

2022-07-24 00:13:18.347 (  11.844s) [        5B703740]vtkOpenGLVolumeLookupTa:84    WARN| vtkOpenGLVolumeOpacityTable (0x56ec100): This OpenGL implementation does not support the required texture size of 1048576, falling back to maximum allowed, 16384.This may cause an incorrect lookup table mapping.
>>> pv.Report()

--------------------------------------------------------------------------------
  Date: Sun Jul 24 00:14:03 2022 CEST

                OS : Linux
            CPU(s) : 8
           Machine : x86_64
      Architecture : 64bit
               RAM : 15.5 GB
       Environment : Python
        GPU Vendor : Intel
      GPU Renderer : Mesa Intel(R) UHD Graphics 620 (WHL GT2)
       GPU Version : 4.6 (Core Profile) Mesa 20.3.5

  Python 3.9.2 (default, Feb 28 2021, 17:03:44)  [GCC 10.2.1 20210110]

           pyvista : 0.36.dev0
               vtk : 9.1.0
             numpy : 1.23.0
           imageio : 2.19.3
           appdirs : 1.4.4
            scooby : 0.5.6
        matplotlib : 3.5.2
           IPython : 8.4.0
          colorcet : 3.0.0
           cmocean : 2.0
        ipyvtklink : 0.2.2
             scipy : 1.8.1
        itkwidgets : 0.32.1
              tqdm : 4.64.0
            meshio : 5.3.4
        jupyterlab : 3.4.3
         pythreejs : Version unknown
--------------------------------------------------------------------------------

@akaszynski
Copy link
Member Author

Very nice!

When I run your example block locally, I get (beside the plot)

I have that occur locally as well. Recommend a follow up to clean this up.

Same with the data being between 0-255. That was really frustrating.

@akaszynski akaszynski requested a review from adeak July 23, 2022 22:20
@codecov
Copy link

codecov bot commented Jul 23, 2022

Codecov Report

Merging #3058 (a7907a3) into main (984cddb) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #3058   +/-   ##
=======================================
  Coverage   95.74%   95.74%           
=======================================
  Files          96       96           
  Lines       20422    20431    +9     
=======================================
+ Hits        19553    19562    +9     
  Misses        869      869           

pyvista/plotting/tools.py Outdated Show resolved Hide resolved
akaszynski and others added 2 commits July 23, 2022 17:15
Co-authored-by: Andras Deak <adeak@users.noreply.github.com>
@prisae
Copy link
Member

prisae commented Jul 24, 2022

This is awesome @akaszynski ! I will have to try this with a more complex model, such as the SEG salt model: https://emsig.xyz/emg3d-gallery/gallery/models/SEG-EAGE_3D_salt_model.html

@prisae
Copy link
Member

prisae commented Jul 24, 2022

How did you store the *.vtk-files? (emg3d/dipole/efield.vtk and emg3d/dipole/hfield.vtk)

@akaszynski
Copy link
Member Author

How did you store the *.vtk-files? (emg3d/dipole/efield.vtk and emg3d/dipole/hfield.vtk)

These examples were generated from the following script:
https://github.com/pyvista/vtk-data/tree/master/Data/emg3d/dipole

If you have a submission, I'd love for them to be stored at pyvista/vtk-data.

@prisae
Copy link
Member

prisae commented Jul 24, 2022

How did you store the *.vtk-files? (emg3d/dipole/efield.vtk and emg3d/dipole/hfield.vtk)

These examples were generated from the following script: https://github.com/pyvista/vtk-data/tree/master/Data/emg3d/dipole

If you have a submission, I'd love for them to be stored at pyvista/vtk-data.

Interesting. emg3d uses under the hood discretize for the meshing (if installed). @banesullivan introduced once a to_vtk-mixin for it: https://discretize.simpeg.xyz/en/main/api/generated/discretize.mixins.InterfaceVTK.html . However, this is only implemented for cell-centred properties. As the fields live on their corresponding edges, one would first have to interpolate the fields to cell centres, before exporting:

grid.write_vtk(
    'efield',
    models={
        'efield_fx': abs(grid.average_edge_x_to_cell * efield.fx.ravel('F')),
        'efield_fy': abs(grid.average_edge_y_to_cell * efield.fz.ravel('F')),
        'efield_fz': abs(grid.average_edge_z_to_cell * efield.fy.ravel('F')),
    },
)

@akaszynski
Copy link
Member Author

I'll have to use that in the future, though a direct tie in with pyvista seems possible if you ever wanted to be able to directly plot the fields.

@prisae
Copy link
Member

prisae commented Jul 25, 2022

That could be cool. Currently I have, consciously, no plotting routines implemented in emg3d at all. The available plotting happens through discretize.

Besides volume plot would, of course, stream plots be very interesting for electromagnetic fields.

image
(from https://www.digitalearthlab.com/8-marine-csem-visualising-the-fields/)

Also cool: https://www.youtube.com/watch?v=EPxNdESwQP0

@akaszynski akaszynski marked this pull request as draft July 26, 2022 00:27
@akaszynski akaszynski marked this pull request as ready for review January 20, 2023 06:28
@akaszynski
Copy link
Member Author

Ended up going with magpylib for now.

image

There were issues with grid alignment with emg3d. I can return to it later in a follow-up PR.

@prisae
Copy link
Member

prisae commented Jan 20, 2023

Hm. Interesting. emg3d uses discretize for the meshing. So that would mean that everything of SimPEG has also this alignment problem...? But what do you mean with alignment issues? The fields in the different directions live on different edges, so they are interpolated if you want the full tensor. But that is all implemented.

@akaszynski
Copy link
Member Author

Hm. Interesting. emg3d uses discretize for the meshing. So that would mean that everything of SimPEG has also this alignment problem...? But what do you mean with alignment issues? The fields in the different directions live on different edges, so they are interpolated if you want the full tensor. But that is all implemented.

I'd like to have both. I'll open an issue in emg3d.

@akaszynski
Copy link
Member Author

akaszynski commented Jan 20, 2023

@prisae, I think my issue is I'm not implementing it correctly. I'll fix it to:

grid.write_vtk(
    'efield',
    models={
        'efield_fx': abs(grid.average_edge_x_to_cell * efield.fx.ravel('F')),
        'efield_fy': abs(grid.average_edge_y_to_cell * efield.fz.ravel('F')),
        'efield_fz': abs(grid.average_edge_z_to_cell * efield.fy.ravel('F')),
    },
)

Docs are down at the moment, see emsig/emg3d#308.

@prisae
Copy link
Member

prisae commented Jan 22, 2023

That looks good @akaszynski . The fields indeed live on the corresponding edges. So if you want to use them together it is the easiest to move them to cell centers first.

examples/99-advanced/magnetic-fields.py Outdated Show resolved Hide resolved
tests/examples/test_download_files.py Show resolved Hide resolved
@akaszynski
Copy link
Member Author

This will have to miss this release, and I'd like to make sure we get emg3d in as well here.

@banesullivan banesullivan marked this pull request as draft February 1, 2023 01:34
akaszynski and others added 4 commits March 26, 2023 16:15
Co-authored-by: Tetsuo Koyama <tkoyama010@gmail.com>
…yvista/pyvista into doc/volume_plotting_emag
@akaszynski akaszynski changed the title Add electric and magnetic field examples Add magnetic field example Mar 26, 2023
@akaszynski akaszynski marked this pull request as ready for review March 26, 2023 22:34
tkoyama010
tkoyama010 previously approved these changes Mar 26, 2023
@akaszynski
Copy link
Member Author

Recommending splitting this up into two PRs to get this merged. This PR will incorporate the magpylib example from Coil Field Lines.

@prisae, if you can get me a vtk dataset that I can use as a vtk file, I'll be able to generate that streamline example. Please post an issue and I can add it. I'm super busy at the moment and can't investigate this further, but I would love to include another example.

@akaszynski
Copy link
Member Author

@tkoyama010, would appreciate another review when you get a chance.

Copy link
Member

@tkoyama010 tkoyama010 left a comment

Choose a reason for hiding this comment

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

LGTM

@akaszynski akaszynski merged commit 8e752a8 into main Mar 27, 2023
@akaszynski akaszynski deleted the doc/volume_plotting_emag branch March 27, 2023 18:15
@akaszynski akaszynski mentioned this pull request Apr 30, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Anything related to the documentation/website
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants