NanoComp / mpb

MIT Photonic-Bands: computation of photonic band structures in periodic media
GNU General Public License v2.0
167 stars 89 forks source link

Segmentation fault on numpy/matplotlib import #64

Closed alkamid closed 6 years ago

alkamid commented 6 years ago

Minimal example:

import math
import numpy as np
import meep as mp
from meep import mpb

geometry_lattice = mp.Lattice(size=mp.Vector3(1, 1),
                              basis1=mp.Vector3(math.sqrt(3) / 2, 0.5),
                              basis2=mp.Vector3(math.sqrt(3) / 2, -0.5))
geometry = [mp.Cylinder(0.2, material=mp.Medium(epsilon=12))]
k_points = [
    mp.Vector3(),               # Gamma
    mp.Vector3(y=0.5),          # M
]

ms = mpb.ModeSolver(
    geometry=geometry,
    geometry_lattice=geometry_lattice,
    k_points=k_points)

ms.run_te()

When running this code (I have a fresh meep conda environment from conda create -n mp -c chogan -c conda-forge pymeep), I'm getting a segmentation fault:

Initializing eigensolver data
Computing 1 bands with 1e-07 tolerance
Working in 2 dimensions.
Grid size is 10 x 10 x 1.
Solving for 1 bands at a time.
Creating Maxwell data...
Mesh size is 3.
Lattice vectors:
     (0.866025, 0.5, 0)
     (0.866025, -0.5, 0)
     (0, 0, 1)
Cell volume = 0.866025
Reciprocal lattice vectors (/ 2 pi):
     (0.57735, 1, -0)
     (0.57735, -1, 0)
     (-0, 0, 1)
Geometric objects:
     cylinder, center = (0,0,0)
          radius 0.2, height 1e+20, axis (0, 0, 1)
Geometric object tree has depth 1 and 1 object nodes (vs. 1 actual objects)
Initializing epsilon function...
Allocating fields...
Solving for band polarization: tm.
Initializing fields to random numbers...
2 k-points
  Vector3<0.0, 0.0, 0.0>
  Vector3<0.0, 0.5, 0.0>
elapsed time for initialization: 0.0009262561798095703
epsilon: 1-12, mean 2.59614, harm. mean 1.21562, 23% > 1, 14.5104% "fill"
Outputting seg-fault-epsilon.h5...
solve_kpoint (0,0,0):
tmfreqs:, k index, k1, k2, k3, kmag/2pi, tm band 1
tmfreqs:, 1, 0, 0, 0, 0, 0
elapsed time for k point: 0.00011920928955078125
solve_kpoint (0,0.5,0):
Solving for bands 1 to 1...
Segmentation fault (core dumped)

With numpy import line commented out, the code runs fine. It's also fine if I move the import after import meet as mp. I saw exactly the same behaviour with matplotlib instead of numpy, so I suppose it might be anything internally importing numpy.

Also, surprisingly (?), if I reduce the list of k-vectors to just the Gamma point:

k_points = [
    mp.Vector3(),               # Gamma
]

The script exits without an error. But then if I specify num_bands>1:

ms = mpb.ModeSolver(
    geometry=geometry,
    geometry_lattice=geometry_lattice,
    k_points=k_points,
    num_bands=2)

Segfault is back again... FWIW, below is the output of conda list:

backcall                  0.1.0                    py36_0  
blas                      1.0                         mkl  
bleach                    2.1.3                    py36_0  
ca-certificates           2018.4.16                     0    conda-forge
certifi                   2018.4.16                py36_0    conda-forge
click                     6.7                        py_1    conda-forge
cycler                    0.10.0           py36h93f1223_0  
dbus                      1.13.2               h714fa37_1  
decorator                 4.3.0                    py36_0  
entrypoints               0.2.3            py36h1aec115_2  
expat                     2.2.5                he0dffb1_0  
fftw                      3.3.7                         0    conda-forge
fontconfig                2.12.6               h49f89f6_0  
freetype                  2.8                  hab7d2ae_1  
glib                      2.56.1               h000015b_0  
gmp                       6.1.2                h6c8ec71_1  
gsl                       2.2.1                h0c605f7_3  
gst-plugins-base          1.14.0               hbbd80ab_1  
gstreamer                 1.14.0               hb453b48_1  
h5py                      2.8.0            py36hb794570_1    conda-forge
h5utils                   1.13                          1    chogan
harminv                   1.4.1                         4    chogan
hdf5                      1.10.2                        0    conda-forge
hdf5-parallel             1.10.2                        0    chogan
html5lib                  1.0.1            py36h2f9c1c0_0  
icu                       58.2                 h9c2bf20_1  
intel-openmp              2018.0.3                      0  
ipykernel                 4.8.2                    py36_0  
ipython                   6.4.0                    py36_0  
ipython_genutils          0.2.0            py36hb52b0d5_0  
jedi                      0.12.0                   py36_1  
jinja2                    2.10             py36ha16c418_0  
jpeg                      9b                   h024ee3a_2  
jsonschema                2.6.0            py36h006f8b5_0  
jupyter_client            5.2.3                    py36_0  
jupyter_core              4.4.0            py36h7c827e3_0  
jupyterlab                0.32.1                   py36_0  
jupyterlab_launcher       0.10.5                   py36_0  
kiwisolver                1.0.1            py36h764f252_0  
libctl                    4.1.1                         0    chogan
libedit                   3.1.20170329         h6b74fdf_2  
libffi                    3.2.1                hd88cf55_4  
libgcc                    7.2.0                h69d50b8_2  
libgcc-ng                 7.2.0                hdf63c60_3  
libgfortran               3.0.0                         1  
libgfortran-ng            7.2.0                hdf63c60_3  
libopenblas               0.2.20               h9ac9557_7  
libpng                    1.6.34                        0    conda-forge
libsodium                 1.0.16               h1bed415_0  
libstdcxx-ng              7.2.0                hdf63c60_3  
libxcb                    1.13                 h1bed415_1  
libxml2                   2.9.8                hf84eae3_0  
livereload                2.5.2                      py_0    conda-forge
markdown                  2.6.11                     py_0    conda-forge
markupsafe                1.0              py36hd9260cd_1  
matplotlib                2.2.2            py36h0e671d2_1  
mistune                   0.8.3            py36h14c3975_1  
mkdocs                    0.17.5                     py_0    conda-forge
mkl                       2018.0.3                      1  
mkl_fft                   1.0.2                    py36_0    conda-forge
mkl_random                1.0.1                    py36_0    conda-forge
mpb                       1.6.2                         3    chogan
nbconvert                 5.3.1            py36hb41ffb7_0  
nbformat                  4.4.0            py36h31c9010_0  
ncurses                   6.1                  hf484d3e_0  
notebook                  5.5.0                    py36_0  
numpy                     1.14.5           py36hcd700cb_3  
numpy-base                1.14.5           py36hdbf6ddf_3  
openblas                  0.2.20                        8    conda-forge
openmpi                   2.0.2                         0    conda-forge
openssl                   1.0.2o                        0    conda-forge
pandoc                    2.2.1                h629c226_0  
pandocfilters             1.4.2            py36ha6701b7_1  
parso                     0.2.1                    py36_0  
pcre                      8.42                 h439df22_0  
pexpect                   4.6.0                    py36_0  
pickleshare               0.7.4            py36h63277f8_0  
pip                       9.0.3                    py36_0    conda-forge
prompt_toolkit            1.0.15           py36h17d85b1_0  
ptyprocess                0.6.0                    py36_0  
pygments                  2.2.0            py36h0d3125c_0  
pymeep                    1.5              py36h7f3c865_0    conda-forge
pyparsing                 2.2.0            py36hee85983_1  
pyqt                      5.9.2            py36h751905a_0  
python                    3.6.5                hc3d631a_2  
python-dateutil           2.7.3                    py36_0  
python-markdown-math      0.6                        py_0    conda-forge
pytz                      2018.5                   py36_0  
pyyaml                    3.12                     py36_1    conda-forge
pyzmq                     17.0.0           py36h14c3975_0  
qt                        5.9.5                h7e424d6_0  
readline                  7.0                  ha6073c6_4  
scipy                     1.1.0            py36hfc37229_0  
send2trash                1.5.0                    py36_0  
setuptools                39.2.0                   py36_0    conda-forge
simplegeneric             0.8.1                    py36_2  
sip                       4.19.8           py36hf484d3e_0  
six                       1.11.0                   py36_1    conda-forge
sqlite                    3.24.0               h84994c4_0  
terminado                 0.8.1                    py36_1  
testpath                  0.3.1            py36h8cadb63_0  
tk                        8.6.7                         0    conda-forge
tornado                   4.5.3                    py36_0    conda-forge
traitlets                 4.3.2            py36h674d592_0  
wcwidth                   0.1.7            py36hdf4376a_0  
webencodings              0.5.1            py36h800622e_1  
wheel                     0.31.0                   py36_0    conda-forge
xz                        5.2.3                         0    conda-forge
yaml                      0.1.7                         0    conda-forge
zeromq                    4.2.5                h439df22_0  
zlib                      1.2.11               h470a237_3    conda-forge
ChristopherHogan commented 6 years ago

This is likely caused by MKL. When I run conda create -n mp -c chogan -c conda-forge pymeep, I get an environment without any MKL packages (i.e., numpy built with openblas, etc.). My guess is that after this step, you ran something that installed MKL into the environment. You can try installing the no-mkl package, which should remove all the MKL packages from your environment and replace them with OpenBLAS versions. Or you can get all your required packages from conda-forge instead of the default channel.

alkamid commented 6 years ago

Maybe it's worth adding a note about it in the Installation docs? Shuffling imports is not really a problem, but this might be confusing for new users.