Commit 020c5a37 authored by Leon Merten Lohse's avatar Leon Merten Lohse
Browse files

use scikit-build

parent a295e825
cmake_minimum_required (VERSION 3.15)
cmake_minimum_required (VERSION 3.15..3.17)
project (fresnel VERSION 0.1)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(default_build_type "Release")
include (GNUInstallDirs)
#set(PYBIND11_PYTHON_VERSION 3.8)
find_package (Python COMPONENTS Interpreter Development)
#add_subdirectory (extern/pybind11)
find_package (pybind11 CONFIG)
#find_package (Python COMPONENTS Interpreter Development)
add_subdirectory (extern/pybind11)
#find_package (pybind11 CONFIG REQUIRED)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
find_package (OpenMP)
pybind11_add_module (_solver source/python.cpp)
target_link_libraries (_solver PRIVATE Eigen3::Eigen)
target_compile_options(_solver PRIVATE -Wpedantic -Wall -O3 -ffast-math)
set(python_module_name _solver)
pybind11_add_module (${python_module_name} MODULE
source/solver_py.cpp
)
target_link_libraries (${python_module_name} PRIVATE Eigen3::Eigen)
target_compile_options(${python_module_name} PRIVATE -Wpedantic -Wall -O3 -ffast-math)
if(OpenMP_CXX_FOUND)
target_link_libraries(_solver PRIVATE OpenMP::OpenMP_CXX)
target_link_libraries(${python_module_name} PRIVATE OpenMP::OpenMP_CXX)
endif()
install(TARGETS _solver DESTINATION fresnel)
install(DIRECTORY fresnel/ DESTINATION fresnel
FILES_MATCHING PATTERN "*.py")
install(TARGETS ${python_module_name} DESTINATION .)
#install(DIRECTORY fresnel/ DESTINATION fresnel
# FILES_MATCHING PATTERN "*.py")
# Quiet a warning
set(ignoreMe "${SKBUILD}")
%% Cell type:code id: tags:
``` python
import sys
import numpy as np
import xraylib as xrl
import matplotlib.pyplot as plt
from pathlib import Path
#from pathlib import Path
# path to fresnel (adjust if necessary)
sys.path.append(f'{Path.home()}/fresnel/')
#sys.path.append(f'{Path.home()}/fresnel/')
import fresnel.solver as solver
import fresnel.propagation as propagation
```
%% Output
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-566ac7b624d1> in <module>
8 # path to fresnel (adjust if necessary)
9 #sys.path.append(f'{Path.home()}/fresnel/')
---> 10 import fresnel.solver as solver
11 import fresnel.propagation as propagation
~/.local/lib/python3.8/site-packages/fresnel/solver.py in <module>
1 import numpy as np
2
----> 3 from . import _solver
4
5 _k = 2 * np.pi # all lengths are in units of the wavelength
ImportError: cannot import name '_solver' from 'fresnel' (/home/leon/.local/lib/python3.8/site-packages/fresnel/__init__.py)
%% Cell type:code id: tags:
``` python
from platform import python_version
print(python_version())
```
%% Output
3.8.7
%% Cell type:code id: tags:
``` python
%matplotlib inline
```
%% Cell type:code id: tags:
``` python
#xrl.GetCompoundDataNISTByName("Kapton Polyimide Film")
```
%% Cell type:code id: tags:
``` python
# simulation box
energy = 12. # keV
wavelength = 1.2398 / (energy * 1e3) * 1e-3 # mm
nx = 1010
nz = 530
wg_radius = 25e-6
xtot = 10 * wg_radius # mm
ztot = 0.4 # mm
# units
dx = xtot / nx / wavelength
dz = ztot / nz / wavelength
print(f"dx: {dx}, dz: {dz} wavelengths")
```
%% Output
dx: 2.395787247703638, dz: 7304.89092884732 wavelengths
%% Cell type:code id: tags:
``` python
# materials and geometry
density_Ge = xrl.ElementDensity(xrl.SymbolToAtomicNumber('Ge'))
density_Polyimide = 1.42
density_Si = xrl.ElementDensity(xrl.SymbolToAtomicNumber('Si'))
n_Ge = xrl.Refractive_Index('Ge', energy, density_Ge)
n_Si = xrl.Refractive_Index('Si', energy, density_Si)
n_PI = xrl.Refractive_Index('Kapton Polyimide Film', energy, density_Polyimide)
xx = np.linspace(-xtot/2, xtot/2, nx)
core = (np.abs(xx) < wg_radius)
cladding = ~core
refractive_index = n_Ge * cladding + 1 * core
```
%% Cell type:code id: tags:
``` python
boundary = (0, 0,)
u0 = np.exp(-xx**2 / (2 * (2 * wg_radius)**2)).astype(np.complex128)
```
%% Cell type:code id: tags:
``` python
propagator = solver.Propagator2d(refractive_index, u0, dz, dx)
```
%% Cell type:code id: tags:
``` python
field = np.zeros((nz, nx), dtype=np.complex128)
field[0,...] = u0
for iz in range(1, nz):
boundary = (0,0)
field[iz, ...] = propagator.step(refractive_index, boundary)
```
%% Cell type:code id: tags:
``` python
result = field.transpose()
fig, ax = plt.subplots()
im = ax.imshow(np.abs(result[:,:])**2, aspect='auto', clim=(0,3), cmap='jet', extent=[0, ztot, -xtot/2 *1e6, xtot/2 * 1e6])
ax.set_ylim(-2e6 * wg_radius, 2e6 * wg_radius )
fig.colorbar(im)
ax.set_xlabel(r'$z$ (mm)')
ax.set_ylabel(r'$x$ (nm)')
```
%% Output
Text(0, 0.5, '$x$ (nm)')
%% Cell type:code id: tags:
``` python
fresnelpropagator = propagation.FresnelPropagator2d(u0, dz, dx)
```
%% Cell type:code id: tags:
``` python
field = np.zeros((nz, nx), dtype=np.complex128)
field[0,...] = u0
for iz in range(1, nz):
boundary = (0,0)
field[iz, ...] = fresnelpropagator.step(refractive_index)
```
%% Cell type:code id: tags:
``` python
result = field.transpose()
fig, ax = plt.subplots()
im = ax.imshow(np.abs(result[:,:])**2, aspect='auto', clim=(0,3), cmap='jet', extent=[0, ztot, -xtot/2 *1e6, xtot/2 * 1e6])
ax.set_ylim(-2e6 * wg_radius, 2e6 * wg_radius )
fig.colorbar(im)
ax.set_xlabel(r'$z$ (mm)')
ax.set_ylabel(r'$x$ (nm)')
```
%% Output
Text(0, 0.5, '$x$ (nm)')
%% Cell type:code id: tags:
``` python
```
......
%% Cell type:code id: tags:
``` python
import sys
import numpy as np
import xraylib as xrl
import matplotlib.pyplot as plt
from pathlib import Path
# path to fresnel (adjust if necessary)
sys.path.append(f'{Path.home()}/fresnel/')
import fresnel.solver as solver
```
%% Cell type:code id: tags:
``` python
%matplotlib inline
```
%% Cell type:code id: tags:
``` python
energy = 12. # keV
# geometry
wavelength = 1.2398 / (energy * 1e3) * 1e-3 # mm
nx = 1024
ny = 512
nz = 1000
ztot = 0.4 # mm
wg_radius = 25e-6
xtot = 10 * wg_radius # mm
ytot = 10 * wg_radius
# units
dx = xtot / nx / wavelength
dy = ytot / ny / wavelength
dz = ztot / nz / wavelength
print(f"dx: {dx}, dy: {dy}, dz: {dz} wavelengths")
```
%% Output
dx: 2.36303234392644, dy: 4.72606468785288, dz: 3871.592192289079 wavelengths
%% Cell type:code id: tags:
``` python
density_Ge = xrl.ElementDensity(xrl.SymbolToAtomicNumber('Ge'))
density_Polyimide = 1.42
density_Si = xrl.ElementDensity(xrl.SymbolToAtomicNumber('Si'))
n_Ge = xrl.Refractive_Index('Ge', energy, density_Ge)
n_Si = xrl.Refractive_Index('Si', energy, density_Si)
n_PI = xrl.Refractive_Index('Kapton Polyimide Film', energy, density_Polyimide)
print('Ge', n_Ge -1)
print('Si', n_Si -1)
print('PI', n_PI -1)
xx = np.linspace(-xtot/2, xtot/2, nx).reshape([1,-1])
yy = np.linspace(-ytot/2, ytot/2, ny).reshape([-1,1])
core = (np.abs(xx) < wg_radius) * (np.abs(yy) < wg_radius)
cladding = ~core
refractive_index = n_Ge * cladding + 1 * core
```
%% Output
Ge (-6.426292114225518e-06+7.120128111534574e-07j)
Si (-3.3845180545943876e-06+3.810012288290064e-08j)
PI (-2.1023289580313076e-06+2.2059592416020462e-09j)
%% Cell type:code id: tags:
``` python
u0 = np.exp(-(xx**2 + yy**2)/ (2 * (2 * wg_radius)**2)).astype(np.complex128)
boundary = (0, 0, 0, 0)
propagator = solver.Propagator3d(refractive_index, u0, dz, dy, dx)
```
%% Cell type:code id: tags:
``` python
field = np.zeros((nz, ny, nx), dtype=np.complex128)
field[0,...] = u0
for iz in range(1, nz):
boundary = (0, 0, 0, 0)
field[iz, ...] = propagator.step(refractive_index, boundary)
if iz % 100 == 0:
print(iz)
```
%% Output
100
200
300
400
500
600
700
800
900
%% Cell type:code id: tags:
``` python
result = field[:,ny//2, :].transpose()
fig, ax = plt.subplots()
im = ax.imshow(np.abs(result)**2, aspect='auto', clim=(0,4), cmap='cubehelix', extent=[0, ztot, -xtot/2 *1e6, xtot/2 * 1e6])
ax.set_ylim(-2e6 * wg_radius, 2e6 * wg_radius )
fig.colorbar(im)
ax.set_xlabel(r'$z$ (mm)')
ax.set_ylabel(r'$x$ (nm)')
result = field[:,:, nx//2].transpose()
fig, ax = plt.subplots()
im = ax.imshow(np.abs(result)**2, aspect='auto', clim=(0,4), cmap='cubehelix', extent=[0, ztot, -xtot/2 *1e6, xtot/2 * 1e6])
ax.set_ylim(-2e6 * wg_radius, 2e6 * wg_radius )
fig.colorbar(im)
ax.set_xlabel(r'$z$ (um)')
ax.set_ylabel(r'$y$ (nm)')
```
%% Output
Text(0, 0.5, '$y$ (nm)')
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
```
......
%% Cell type:code id: tags:
``` python
import sys
import numpy as np
import xraylib as xrl
import matplotlib.pyplot as plt
from pathlib import Path
# path to fresnel (adjust if necessary)
sys.path.append(f'{Path.home()}/fresnel/')
import fresnel.solver as solver
```
%% Cell type:code id: tags:
``` python
%matplotlib inline
```
%% Cell type:code id: tags:
``` python
energy = 12. # keV
# geometry
wavelength = 1.2398 / (energy * 1e3) * 1e-3 # mm
nx = 1024
ny = 512
nz = 1000
ztot = 0.4 # mm
wg_radius = 25e-6
xtot = 10 * wg_radius # mm
# units
dx = xtot / nx / wavelength
dz = ztot / nz / wavelength
print(f"dx: {dx}, dz: {dz} wavelengths")
```
%% Output
dx: 2.36303234392644, dz: 3871.592192289079 wavelengths
%% Cell type:code id: tags:
``` python
density_Ge = xrl.ElementDensity(xrl.SymbolToAtomicNumber('Ge'))
density_Polyimide = 1.42
density_Si = xrl.ElementDensity(xrl.SymbolToAtomicNumber('Si'))
n_Ge = xrl.Refractive_Index('Ge', energy, density_Ge)
n_Si = xrl.Refractive_Index('Si', energy, density_Si)
n_PI = xrl.Refractive_Index('Kapton Polyimide Film', energy, density_Polyimide)
xx = np.linspace(-xtot/2, xtot/2, nx)
core = (np.abs(xx) < wg_radius)
cladding = ~core
refractive_index = n_Ge * cladding + 1 * core
```
%% Cell type:code id: tags:
``` python
boundary = (0, 0,)
u0 = np.exp(-xx**2 / (2 * (2 * wg_radius)**2)).astype(np.complex128)
```
%% Cell type:code id: tags:
``` python
propagator = solver.PropagatorCS(refractive_index, u0, dz, dx)
```
%% Cell type:code id: tags:
``` python
field = np.zeros((nz, nx), dtype=np.complex128)
field[0,...] = u0
for iz in range(1, nz):
boundary = (0,0)
field[iz, ...] = propagator.step(refractive_index, boundary)
```
%% Cell type:code id: tags:
``` python
result = field.transpose()
fig, ax = plt.subplots()
im = ax.imshow(np.abs(result[:,:])**2, aspect='auto', clim=(0,3), cmap='jet', extent=[0, ztot, -xtot/2 *1e6, xtot/2 * 1e6])
ax.set_ylim(-2e6 * wg_radius, 2e6 * wg_radius )
fig.colorbar(im)
ax.set_xlabel(r'$z$ (mm)')
ax.set_ylabel(r'$x$ (nm)')
```
%% Output
Text(0, 0.5, '$x$ (nm)')
%% Cell type:code id: tags:
``` python
```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment