S4 icon indicating copy to clipboard operation
S4 copied to clipboard

Problem with Python shared object

Open kilianmuller opened this issue 7 years ago • 26 comments

Hi! I am running Arch linux, and am following the instructions to compile and install the python extension of S4. I clone the git repository, and run make, make S4_pyext, and python setup.py install. This gives me a .so file. When I try to include it in a python script and then running it I get the following message:

ImportError: /usr/lib/python3.6/site-packages/S4.cpython-36m-x86_64-linux-gnu.so: undefined symbol: Layer_Init

The python script just tries to import S4:

import S4

That is the entirety of the script. Any suggestions are welcome!

kilianmuller avatar Oct 26 '17 14:10 kilianmuller

It doesn't work under python 3. You need to use Python 2. So instead of

python setup.py install

Try

pip2 install ./

Then use a python2 interpreter and try to import.

Someone needs to port to the Python3 C API (which would be sweet).

There are also some bugs in the Python 2 API. I think some of the shape functions are broken, and using the Lanczos smoothing option just doesn't work. I have a fix for the Lanczos smoothing bug (I'll post it if you or anyone needs it), and there should be a pull request or something like that on the S4v2 branch with a fix for the circle shapes at least.

kwrobert avatar Oct 26 '17 14:10 kwrobert

I tried that, but when I want to run the python script (which again just contains the import of S4) I get a similar error: ImportError: /usr/lib/python2.7/site-packages/S4.so: undefined symbol: Layer_Init Thanks so much for your reply - but I've spent too much time on this already. The Lua frontend works for me, I'll just use that instead.

kilianmuller avatar Oct 26 '17 15:10 kilianmuller

Have you tried switching to the S4v2 branch? Try

git branch S4v2

I've been using the Python interface under python 2 quite extensively and it works for me. The problem with the Lua API is you miss out on all the great libraries that are available in Python for manipulating the data that S4 generates.

kwrobert avatar Oct 26 '17 15:10 kwrobert

I just cloned the S4v2 branch, but it won't compile. Running make results in the errors below. I am way out of my comfort zone here; but it seems like the errors are not something to be fixed by editing the Makefile(s) (which I had left untouched).

You are right about the Python libraries, that's the reason why I was trying to make it work in the first place. As of now I have a cumbersome melange of S4 + Lua -> .csv file -> Python script to create and analyse the data.

mkdir -p build mkdir -p build/S4k g++ -c -O3 -Wall -march=native -fcx-limited-range -fno-exceptions -fPIC -IS4 -IS4/RNP -IS4/kiss_fft S4/rcwa.cpp -o build/S4k/rcwa.o S4/rcwa.cpp: In function ‘void SolveLayerEigensystem(double, size_t, const double*, const double*, const std::complex<double>*, const std::complex<double>*, int, std::complex<double>*, std::complex<double>*, std::complex<double>*, double*, size_t)’: S4/rcwa.cpp:562:67: error: cannot convert ‘double*’ to ‘std::complex<double>*’ for argument ‘9’ to ‘int RNP::Eigensystem(size_t, std::complex<double>*, size_t, std::complex<double>*, std::complex<double>*, size_t, std::complex<double>*, size_t, std::complex<double>*, double*, size_t)’ gensystem(n2, NULL, n2, q, NULL, 1, phi, n2, work_, lwork); ^ S4/rcwa.cpp:573:81: error: cannot convert ‘double*’ to ‘std::complex<double>*’ for argument ‘2’ to ‘int RNP::Eigensystem(size_t, std::complex<double>*, size_t, std::complex<double>*, std::complex<double>*, size_t, std::complex<double>*, size_t, std::complex<double>*, double*, size_t)’ (double*)NULL, n2, q, NULL, 1, phi, n2, (double*)q, lwork); ^ S4/rcwa.cpp:668:80: error: cannot convert ‘double*’ to ‘std::complex<double>*’ for argument ‘2’ to ‘int RNP::Eigensystem(size_t, std::complex<double>*, size_t, std::complex<double>*, std::complex<double>*, size_t, std::complex<double>*, size_t, std::complex<double>*, double*, size_t)’ em(n2, op, n2, q, NULL, 1, phi, n2, eigenwork, eigenlwork); ^ S4/rcwa.cpp:599:24: warning: unused variable ‘kp_use’ [-Wunused-variable] std::complex<double> *kp_use = (NULL != kp ? kp : phi); ^~~~~~ S4/rcwa.cpp: In function ‘void GetFieldOnGrid(size_t, int*, const double*, const double*, std::complex<double>, const std::complex<double>*, const std::complex<double>*, const std::complex<double>*, const std::complex<double>*, int, const std::complex<double>*, const size_t*, const double*, std::complex<double>*, std::complex<double>*)’: S4/rcwa.cpp:1575:6: warning: unused variable ‘inxy’ [-Wunused-variable] int inxy[2] = { (int)nxy[0], (int)nxy[1] }; ^~~~ make: *** [Makefile:58: build/S4k/rcwa.o] Error 1

kilianmuller avatar Oct 27 '17 10:10 kilianmuller

I also had a lot of problems running S4 in Python (in general), but I now have a branch which has worked for me (on Ubuntu and MacOS) and a couple of other people in Python3 (note that the changes I made have made it incompatible with Python2): https://github.com/phoebe-p/S4

After cloning you should only have to make S4_pyext (not make or python setup.py install, although I guess they don't hurt).

phoebe-p avatar Oct 27 '17 10:10 phoebe-p

That did the trick! For the people having the same problem as me, this is what finally worked:

git clone https://github.com/phoebe-p/S4.git cd S4 make sudo make S4_pyext sudo python setup.py install

I tested in an ipython (3.6.2) shell. Whereas before the import of the S4 library resulted in an error, this now works:

import S4

Thanks a lot kwrobert and phoebe-p!

kilianmuller avatar Oct 27 '17 10:10 kilianmuller

Hi kilianmuller and phoebe-p,

Which version of python are you using? I am having difficulty to compile S4.

I did the following: git clone https://github.com/phoebe-p/S4.git cd S4 make S4_pyext

Unfortunately, the build/lib folder was not created. Could you help me?

Here is the full error:

Processing /home/sajidmc/Apple/metaelement/S4/S4
Installing collected packages: S4
  Running setup.py install for S4 ... error
    Complete output from command /usr/bin/python3.4 -u -c "import setuptools, tokenize;__file__=/tmp/pip-vf3a89ot-build/setup.py;exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-6cfmeott-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_ext
    building 'S4' extension
    creating build/temp.linux-x86_64-3.4
    creating build/temp.linux-x86_64-3.4/S4
    gcc -Wno-unused-result -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python3.4m -c S4/main_python.c -o build/temp.linux-x86_64-3.4/S4/main_python.o -std=gnu99
    S4/main_python.c: In function ‘S4_SolveInParallel’:
    S4/main_python.c:1993:14: warning: unused variable ‘layerName’ [-Wunused-variable]
      const char *layerName;
                  ^~~~~~~~~
    S4/main_python.c:1992:15: warning: unused variable ‘kwlist’ [-Wunused-variable]
      static char *kwlist[] = {"Layer", "Simulations", NULL};
                   ^~~~~~
    At top level:
    S4/main_python.c:1992:15: warning: ‘kwlist’ defined but not used [-Wunused-variable]
    S4/main_python.c:659:18: warning: ‘S4Sim_ConvertUnits’ defined but not used [-Wunused-function]
     static PyObject *S4Sim_ConvertUnits(S4Sim *self, PyObject *args)
                      ^~~~~~~~~~~~~~~~~~
    creating build/lib.linux-x86_64-3.4
    gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-3.4/S4/main_python.o -L./build -L/usr/lib64 -lS4 -lstdc++ -lblas -llapack -lpthread -lpython3.4m -o build/lib.linux-x86_64-3.4/S4.cpython-34m.so ./build/libS4.a
    /usr/bin/uuidgen: /apps/cent7/anaconda/5.1.0-py36/lib/libuuid.so.1: no version information available (required by /usr/bin/uuidgen)
    /usr/bin/ld: cannot find -lblas
    /usr/bin/ld: cannot find -llapack
    /usr/bin/sha1sum: build/lib.linux-x86_64-3.4/S4.cpython-34m.so: No such file or directory
    collect2: error: ld returned 1 exit status
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python3.4 -u -c "import setuptools, tokenize;__file__='/tmp/pip-vf3a89ot-build/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-6cfmeott-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-vf3a89ot-build/
You are using pip version 8.1.2, however version 19.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

sajidmc avatar Jan 28 '19 19:01 sajidmc

After some digging around and checking all the repositories, the https://github.com/bro-acid/S4 fork worked for me, in python 2.7. I could run the test script with it. Hope someone would give a better working version for python 3 that does not need SU privilege.

sajidmc avatar Jan 28 '19 20:01 sajidmc

Hi @sajidmc, as the errors indicate, you don't have LAPACK or BLAS libraries installed, which is why gcc fails. depending on whether you are using Linux or MacOS, you can install these in various ways - e.g. on Ubuntu you can use OpenBLAS (https://www.openblas.net/) and install lapack libraries with sudo apt-get install liblapack-dev. I've used various versions of Python3, they should all work.

phoebe-p avatar Jan 29 '19 03:01 phoebe-p

Also, the reason you need sudo/root privileges to install the python extension is because the Makefile calls “pip install —upgrade ./“ in the S4_pyext target. This will install S4 alongside your global python packages, which is generally in a directory that requires root privileges to write to. I would recommend against installing S4 globally, and my suggestion would be to either install S4 in a virtual environment (google “python virtual environment” if you don’t know what that is) or modify the Makefile so that it calls “pip install —upgrade —user ./“ to install S4 in your home directory instead.

Best of luck!

Sent from my iPhone

On Jan 28, 2019, at 10:36 PM, Phoebe Pearce [email protected] wrote:

Hi @sajidmc, as the errors indicate, you don't have LAPACK or BLAS libraries installed, which is why gcc fails. depending on whether you are using Linux or MacOS, you can install these in various ways - e.g. on Ubuntu you can use OpenBLAS (https://www.openblas.net/) and install lapack libraries with sudo apt-get install liblapack-dev. I've used various versions of Python3, they should all work.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

kwrobert avatar Jan 29 '19 11:01 kwrobert

Thank you very much for the replies. I was trying to use a University cluster for the compilation, that did not have the LAPACK and OpenBLAS. I will try a virtual box to compile it.

sajidmc avatar Jan 29 '19 16:01 sajidmc

I would be surprised if an HPC cluster didn’t have a set of matrix libraries to link against. It might be that the libraries are installed in some unconventional place, and so the linker doesn’t know where to look for them. You’ll need to find where the matrix libraries are located, and add those locations to the INCLUDE paths in the Makefile. If your HPC cluster has a support contact, I would recommend reaching out to them to help out with this.

FYI, if you compile S4 on a VM, you can’t just copy it up to your cluster and run it there because the compiled binaries will be looking for dynamically linked libraries at their original locations on your VM. Those locations might not exist on the cluster, so you’ll get mysterious runtime errors. You might be able to statically compile S4, so all the libraries are included in the binaries, but I’m not really sure how to do that and won’t be of much help.

On Jan 29, 2019, at 11:51 AM, sajidmc [email protected] wrote:

Thank you very much for the replies. I was trying to use a University cluster for the compilation, that did not have the LAPACK and OpenBLAS. I will try a virtual box to compile it.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/victorliu/S4/issues/61#issuecomment-458616884, or mute the thread https://github.com/notifications/unsubscribe-auth/AHr4o2RfvUvWw0v7O5i3mEeNEXX7X1qvks5vIHwVgaJpZM4QHsfI.

kwrobert avatar Jan 29 '19 17:01 kwrobert

Thank you very much @kwrobert and @phoebe-p . Indeed I found that the library locations are in unconventional places:

/apps/snyder7/openblas/0.2.19_gcc-4.8.5/lib /apps/snyder7/fftw/3.3.7/openmpi-2.1.0_intel-17.0.1.132/lib /apps/snyder7/fftw/3.3.7/openmpi-2.1.0_intel-17.0.1.132/

I tried to directly add them in the make file as:

BLAS_LIB = -L/apps/snyder7/openblas/0.2.19_gcc-4.8.5/lib/libblas.so
LAPACK_LIB = -L/apps/snyder7/openblas/0.2.19_gcc-4.8.5/lib/liblapack.so

It gives warning:

warning: no library file corresponding to '/apps/snyder7/openblas/0.2.19_gcc-4.8.5/lib/libblas.so' found (skipping)
warning: no library file corresponding to '/apps/snyder7/openblas/0.2.19_gcc-4.8.5/lib/liblapack.so' found (skipping)
warning: no library file corresponding to './apps/snyder7/fftw/3.3.7/openmpi-2.1.0_intel-17.0.1.132/lib' found (skipping)

That means it still could not find the library files. I also tried the /apps/snyder7/openblas/0.2.19_gcc-4.8.5/lib -llapack there. Still has same error. Although the folder clearly has the so file:

sajidmc@snyder-fe01:/apps/snyder7/openblas/0.2.19_gcc-4.8.5/lib $ ls
cmake      libblas.so  libcblas.so   libf77blas.so  liblapack.so   libopenblas_haswellp-r0.2.19.a   libopenblas.so
libblas.a  libcblas.a  libf77blas.a  liblapack.a    libopenblas.a  libopenblas_haswellp-r0.2.19.so  libopenblas.so.0

Am I missing something here? Or do you guys know how to directly link a library without using symbolic link?

sajidmc avatar Feb 01 '19 15:02 sajidmc

After quite a bit of help from Michael Povolosky of Purdue, I managed to compile the S4 code using python 3.6.4 in our HPC cluster.

I am adding the fork as https://github.com/sajidmc/S4 , if anyone needs to use it. Thank you so much for the help @phoebe-p and @kwrobert

I added this example script in my fork as well, to quickly check if the extension is working.

sajidmc avatar Feb 11 '19 23:02 sajidmc

Hi Folks,

I cloned from Phoebe's site using the Windows Subsystem for Linux (WSL) and it choked when I tried to make the Python extension. The error seems to concern "boost" -- I'm not sure what that is used for. Here is the error. Any thoughts? Thank you in advance!

$ sudo make S4_pyext mkdir -p ./build mkdir -p ./build/S4k mkdir -p ./build/S4r mkdir -p ./build/modules g++ -c -Wall -O3 -m64 -march=native -mtune=native -msse3 -msse2 -msse -fPIC -Wall -I. -IS4 -IS4/RNP -IS4/kiss_fft -I/home/superman/PPS4/S4/S4/include -L/home/superman/PPS4/S4/S4/lib/ -lboost_serialization -DHAVE_BLAS -DHAVE_LAPACK -DHAVE_FFTW3 -DHAVE_LIBCHOLMOD -I/usr/include/suitesparse S4/S4.cpp -o build/S4k/S4.o S4/S4.cpp:51:10: fatal error: boost/serialization/nvp.hpp: No such file or directory 51 | #include <boost/serialization/nvp.hpp> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. make: *** [Makefile:243: build/S4k/S4.o] Error 1

jlawall avatar May 18 '20 18:05 jlawall

Hi there,

I think I might have introduced this dependency a long time ago while I was working on my Master’s, Phoebe must have pulled it into her fork.

Boost is used to serialize the state of a simulation so you can save the RCWA solutions and recompute fields at different discretizations without rerunning the whole simulation.

Try running “make boost” before trying to build the python extension.

Best,

Kyle

On May 18, 2020, at 2:01 PM, jlawall [email protected] wrote:

Hi Folks,

I cloned from Phoebe's site using the Windows Subsystem for Linux (WSL) and it choked when I tried to make the Python extension. The error seems to concern "boost" -- I'm not sure what that is used for. Here is the error. Any thoughts? Thank you in advance!

$ sudo make S4_pyext mkdir -p ./build mkdir -p ./build/S4k mkdir -p ./build/S4r mkdir -p ./build/modules g++ -c -Wall -O3 -m64 -march=native -mtune=native -msse3 -msse2 -msse -fPIC -Wall -I. -IS4 -IS4/RNP -IS4/kiss_fft -I/home/superman/PPS4/S4/S4/include -L/home/superman/PPS4/S4/S4/lib/ -lboost_serialization -DHAVE_BLAS -DHAVE_LAPACK -DHAVE_FFTW3 -DHAVE_LIBCHOLMOD -I/usr/include/suitesparse S4/S4.cpp -o build/S4k/S4.o S4/S4.cpp:51:10: fatal error: boost/serialization/nvp.hpp: No such file or directory 51 | #include <boost/serialization/nvp.hpp> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. make: *** [Makefile:243: build/S4k/S4.o] Error 1

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/victorliu/S4/issues/61#issuecomment-630345379, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB5PRIZOU2OY2CI3KVJUSYLRSFZYBANCNFSM4EA6Y7EA.

kwrobert avatar May 19 '20 20:05 kwrobert

Hi Kyle,

Thank you for the suggestion. When I tried it, the window filled up with paths and file names, such as "boost_1_61_0/libs/math/doc/html/math_toolkit/expint/" and this just went on and on. There must have been tens of thousands of such files, and after about ten minutes of this, with no indication of it stopping, I hit Ctrl-C. Is this the expected behavior? If so, how long should I expect it to take? If not, do you have any suggestions? Thank you!

jlawall avatar May 19 '20 22:05 jlawall

Boost is en enormous library, so that behavior doesn’t really surprise me too much. All those path names are probably being printed when the gzipped tar archive file containing all of boost gets unpacked.

If you are concerned about the behavior of the Makefile (entirely reasonable), you can always read it yourself to assuage your concerns.

Sent from my iPhone

On May 19, 2020, at 6:18 PM, jlawall [email protected] wrote:

 Hi Kyle,

Thank you for the suggestion. When I tried it, the window filled up with paths and file names, such as "boost_1_61_0/libs/math/doc/html/math_toolkit/expint/" and this just went on and on. There must have been tens of thousands of such files, and after about ten minutes of this, with no indication of it stopping, I hit Ctrl-C. Is this the expected behavior? If so, how long should I expect it to take? If not, do you have any suggestions? Thank you!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

kwrobert avatar May 20 '20 09:05 kwrobert

Just want to echo what Kyle wrote; this was something he introduced which I pulled into my fork. If you don't want to use boost you could go back a couple of commits and look at an old Makefile. But it sounds like Boost was installing as expected, it does take a pretty long time and that is what it looks like by installing.

If you're using MacOS there was a small issue which could be fixed by updating which version of boost is downloaded, I'll try to merge it into my fork when i get a chance.

phoebe-p avatar May 20 '20 14:05 phoebe-p

Hi Kyle,

Thank you again. This time when I tried "make boost," it simply said: "make:Nothing to be done for 'boost'" (again, last time I did it, I interrupted it after ten minutes or so with Ctrl-C). So I went on to try the Python extension, and got exactly the same fatal error as before ("fatal error:boost/serialization/nvp.hpp:No such file or directory") Is there some way I can force it to let me do the complete "make boost" again, if indeed that might help? As you can tell, I am pretty well out of my league here. I have used S4 a lot with lua and thought it would be a simple thing to try it with Python, but the whole "make" process is overwhelming me. I did it a bit with simple C code that I wrote myself 30 years ago, but that's all!

jlawall avatar May 20 '20 14:05 jlawall

Hi Phoebe,

Thank you for weighing in as well. I'll look into trying to "go back a couple of commits and look at an old Makefile." Actually I had been planning to try to look through the Makefile and just strip out all references to "boost," but I didn't know how successful that would have been. Once again, I didn't appreciate how much would be involved here (for example, this is the first time I have heard of a "commit").

jlawall avatar May 20 '20 14:05 jlawall

Try deleting all the boost files in your directory and making boost again. The boost target probably checks for the existence of some files to determine if it needs to be run again. I didn’t do a very good job writing the boost target when I set this all up, I got it working as fast as I could and moved on, so my apologies for the difficulties.

I highly encourage you to google around and read up on the syntax of Makefiles (GNU Make, not Cmake or other alternatives), and then read the actual Makefile that is used to build all this stuff. Once you get the basic principle of Makefiles, just read the Makefile and you’ll understand what’s going on, why things aren’t working, and what you can do to fix them.

Good luck! This stuff all seems archaic and overwhelming at first but with patience, discipline, and lots of googling you’ll get the hang of it.

On May 20, 2020, at 10:43 AM, jlawall [email protected] wrote:

Hi Kyle,

Thank you again. This time when I tried "make boost," it simply said: "make:Nothing to be done for 'boost'" (again, last time I did it, I interrupted it after ten minutes or so with Ctrl-C). So I went on to try the Python extension, and got exactly the same fatal error as before ("fatal error:boost/serialization/nvp.hpp:No such file or directory") Is there some way I can force it to let me do the complete "make boost" again, if indeed that might help? As you can tell, I am pretty well out of my league here. I have used S4 a lot with lua and thought it would be a simple thing to try it with Python, but the whole "make" process is overwhelming me. I did it a bit with simple C code that I wrote myself 30 years ago, but that's all!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/victorliu/S4/issues/61#issuecomment-631518697, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB5PRI7THYP5BXZIPDCT4ATRSPUA7ANCNFSM4EA6Y7EA.

kwrobert avatar May 20 '20 14:05 kwrobert

Yes, it can be quite overwhelming, it's not just you! You can see the commit history of a repository when you go to the main page, top left it will say 'x commits' (for my fork it says '219 commits' right now). That's all the different changes that have been made to the code over time, you can browse files as they were after that commit by clicking the '<>' button. I think this commit is before the boost stuff was added: https://github.com/phoebe-p/S4/tree/363411d0b2d007151700c7a249f4804320dc290c

but do try what Kyle said - it sounds like it thinks boost is already installed because the files were downloaded and the folder was made although it didn't finish installing.

phoebe-p avatar May 20 '20 15:05 phoebe-p

Hi Phoebe -- I'm getting a lot closer! I used the "commit" that you pointed to (#149). It does still use "boost," but I got it to make the Python extension, and it concluded with "Successfully installed S4-1.1" I was thrilled to see that!

However, I still got an error when I tried to run the example file "PythonTest.py": Traceback (most recent call last): File "PythonTest.py", line 21, in S = S4.New(Lattice=((period[0],0),(0,period[1])),NumBasis=20) AttributeError: module 'S4' has no attribute 'New'

Any thoughts? Should I perhaps use a different "commit"? Many thanks!

jlawall avatar May 21 '20 01:05 jlawall

I tried your most recent "commit" and this time it worked for me -- including PythonTest! Hopefully I'm all set now...many thanks!

jlawall avatar May 21 '20 03:05 jlawall

Hi all,

First of all, thanks all for all your work that allows me to have a running S4 on my machine. I am working on the example PythonTest.py contained in the S4 folder. I am experiencing inconsistencies between my results and the one published in the "Highly sensitive biosensors based on all-dielectric nanoresonators" by Bontempi and coauthors.

By running the PythonTest.py without any modifications, I got the same results posted by sajidmc. The problem is that those results are not the ones shown in the publications mentioned above. Trying to obtain a more fair comparison, I slightly modified the PythonTest.py script. In particular, I have changed the material of the bottom layer from Air to Silicon, increased the number of harmonics from 20 up to 120, and the frequencies resolutions (to guarantee convergencies). I am attaching the modified script and the results as well. As you can see, S4 cannot reproduce the published results in a lower wavelength range. I am wondering why. I am wondering if you can share your thoughts. Any suggestions are more than welcome!

Comparison

CODE: import S4 import numpy as np import matplotlib.pyplot as plt import time

t = time.time()

p = 0.96 d = 0.73 h = 0.22 S = S4.New(Lattice=((p, 0), (0, p)), NumBasis=100)

Define material

S.SetMaterial(Name='SiO2', Epsilon=(1.45 + 0.0j)**2) S.SetMaterial(Name='Si', Epsilon=(3.487+0.0j)**2) S.SetMaterial(Name='Vacuum', Epsilon=(1 + 0j)**2)

Define structure

Top semi-infinite latyer

S.AddLayer(Name='AirAbove', Thickness=0, Material='Vacuum') #S.AddLayer(Name = 'AirDisp',Thickness = 1, Material = 'Vacuum')

Si-Nnaodisck: Air block plus Si circle

S.AddLayer(Name='Si_disks', Thickness=h, Material='Vacuum') S.SetRegionCircle(Layer='Si_disks', Material='Si', Center=(0, 0), Radius=d/2)

SiO2 substrate

S.AddLayer(Name='Glass_Below', Thickness=1.99, Material='SiO2')

Semiinfite layer: Semi infite Silicon

S.AddLayer(Name='SiBelow', Thickness=0, Material='Si')

Excitation: plane wave

S.SetExcitationPlanewave( IncidenceAngles=( 0,# polar angle in [0,180) 0 # azimuthal angle in [0,360) ), sAmplitude=0, pAmplitude=1, Order=0 )

Set options

S.SetOptions( PolarizationDecomposition=True, PolarizationBasis='Normal' )

#wavelength_um = 1; #freq = 1 / float(wavelength_um); #S.SetFrequency(freq) #unit 2pic_const / a #forward,backward = S.GetPowerFlux(Layer = 'AirAbove', zOffset = 0) # reflected power #forward = S.GetPowerFlux(Layer = 'SiBelow',zOffset = 0) #print('f='+str(1/freq)+', Power_forward= '+str(forward[0].real)+', Power_backward='+str(-backward.real))

frequency sweep

wavelength_space = np.linspace(1.3, 1.7, 1500)

memory allocation

R = np.zeros((len(wavelength_space))) T = np.zeros((len(wavelength_space))) for ii in range(len(wavelength_space)): lam = wavelength_space[ii] f = 1 / float(lam) S.SetFrequency(f) (forward1, backward1) = S.GetPowerFlux(Layer='AirAbove', zOffset=0) (forward2, backward2) = S.GetPowerFlux(Layer='SiBelow', zOffset=0) reflection = -backward1 transmission = forward2 R[ii] = np.real(reflection) T[ii] = np.real(transmission)

plt.plot(wavelength_space, R, label="R") plt.plot(wavelength_space, T, label="T") plt.plot(wavelength_space, T+R, label="T+R") plt.xlabel('Wavelength (um)') plt.ylabel('Transmission') plt.legend(loc="upper left") plt.ylim(0, 1.1) plt.show()

sgrigno2 avatar Dec 27 '21 18:12 sgrigno2