Home > Software design >  Python VTK on M2 macbook air
Python VTK on M2 macbook air

Time:11-12

I am writing this code for simulation of earths magnetic field:

import numpy as np
import matplotlib.pyplot as plt
import magpylib as magpy
import pyvista as pv

ts = np.linspace(-8,8, 150)
t = np.linspace(-6,6, 150)
axis = np.c_[2*np.cos(ts*2*np.pi), 2*np.sin(ts*2*np.pi), ts]
aux = np.c_[2*np.cos(ts*2*np.pi), 2*np.sin(ts*2*np.pi), t]
def make_coil(pos, vertices):
    coil = magpy.current.Line(
    current = 100,
    vertices = vertices,
    position= pos,
    style_color="green",
    )
    return coil

theta = np.sqrt(2)/2
r = 4
coil1 = make_coil((0,0,0), axis)
coil2 = make_coil((r*1,0,0), aux)
coil3 = make_coil((r*theta,r*theta,0), aux)
coil4 = make_coil((0,1*r,0), aux)
coil5 = make_coil((-r*theta,r*theta,0), aux)
coil6 = make_coil((-r*1,0,0), aux)
coil7 = make_coil((-r*theta,-r*theta,0), aux)
coil8 = make_coil((0,-r*1,0), aux)
coil9 = make_coil((r*theta,-r*theta,0), aux)

coil = coil1   coil2   coil3   coil4   coil5   coil6   coil7   coil8   coil9 
coil.show()



grid = pv.UniformGrid(
    dimensions=(41, 41, 41),
    spacing=(2, 2, 2),
    origin=(-40, -40, -40),
)

# compute B-field and add as data to grid
grid["B"] = coil.getB(grid.points)
# compute field lines
seed = pv.Disc(inner=1, outer=5.2, r_res=3, c_res=12)
strl = grid.streamlines_from_source(
    seed,
    vectors='B',
    max_time=180,
    initial_step_length=0.01,
    integration_direction='both',
)

# create plotting scene
pl = pv.Plotter()

# add field lines and legend to scene
legend_args = {
    'title': 'B [mT]',
    'title_font_size': 20,
    'color': 'black',
    'position_y': 0.25,
    'vertical': True,
}

# draw coils
magpy.show(coil, color="orange", canvas=pl, backend='pyvista')

# add streamlines
pl.add_mesh(
    strl.tube(radius=.2),
    cmap="bwr",
    scalar_bar_args=legend_args,
)
# display scene
pl.camera.position=(160, 10, -10)
pl.set_background("white")
pl.show()

and I get this error message

danieltran@eduroam-193-157-168-102 OneDrive-UniversitetetiOslo % /usr/local/bin/python3 "/Users/danieltran/Library/CloudStorage/OneDrive-UniversitetetiOslo/H22/FYS1120/Comp Essay/d
ouble_solenoids.py"
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/_vtk.py", line 547, in <module>
    from vtk.vtkCommonKitPython import buffer_shared, vtkAbstractArray, vtkWeakReference
ModuleNotFoundError: No module named 'vtk'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/danieltran/Library/CloudStorage/OneDrive-UniversitetetiOslo/H22/FYS1120/Comp Essay/double_solenoids.py", line 4, in <module>
    import pyvista as pv
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/__init__.py", line 12, in <module>
    from pyvista.plotting import *
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/plotting/__init__.py", line 4, in <module>
    from .charts import Chart2D, ChartMPL, ChartBox, ChartPie
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/plotting/charts.py", line 13, in <module>
    from pyvista import _vtk
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/_vtk.py", line 549, in <module>
    from vtk.vtkCommonCore import buffer_shared, vtkAbstractArray, vtkWeakReference
ModuleNotFoundError: No module named 'vtk'. 

CodePudding user response:

First, you need to post a question, not just code error message.

Based off of your error message, this is what I would try:

Ensure VTK is installed. https://pypi.org/project/vtk/

Try a different Python version. 3.11 is fresh off the shelf and it looks like the VTK library was last updated prior to 3.11's release.

There are other things that you could try but this is likely the best starting point based on your post...

CodePudding user response:

VTK currently does not have a supported python 3.11 compatible version published. See files available for latest version 9.2.2 https://pypi.org/project/vtk/9.2.2/#files which has no 3.11 compatible files [as of today]. Some options are to build VTK yourself (may or may not be possible without fixes to support python 3.11) or use a different python version or wait until a new vtk release that is compatible with python 3.11.

  • Related