Skip to content

Commit

Permalink
Merge pull request #16 from DanielKotik/develop
Browse files Browse the repository at this point in the history
Merge `develop` into `master`
  • Loading branch information
DanielKotik committed Aug 16, 2021
2 parents f593972 + eaa95d5 commit eca939b
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Expand Up @@ -3,7 +3,7 @@
cff-version: 1.0.3
message: If you use this software, please cite it using these metadata.
title: 'Optical-beams-MEEP'
doi: 10.5281/zenodo.3691799
doi: 10.5281/zenodo.2631828 # This DOI represents all versions, and will always resolve to the latest one.
authors:
- given-names: Daniel
family-names: Kotik
Expand Down
46 changes: 46 additions & 0 deletions optbeam/_2d/beams.pxd
@@ -0,0 +1,46 @@
from .helpers cimport Beam2d, _complex_quad

# -----------------------------------------------------------------------------
# declare C functions as "cpdef" to export them to the module
# -----------------------------------------------------------------------------
cdef extern from "math.h":
cpdef double _exp "exp" (double x) nogil
cpdef double _sqrt "sqrt" (double x) nogil

cdef extern from "complex.h":
cpdef double complex _cexp "cexp" (double complex z) nogil

# -----------------------------------------------------------------------------
# function declarations
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# class declarations
# -----------------------------------------------------------------------------
cdef class Beam2dCartesian(Beam2d):
cdef:
double _ry, _rz

cdef double complex spectrum(self, double k_y) nogil
cdef double _phase(self, double k_y, double x, double y) nogil
cdef double complex _integrand(self, double k_y) nogil

cdef class Gauss2d(Beam2dCartesian):
cdef:
double _W_y
double _norm

cdef double _f_Gauss(self, double k_y, double W_y) nogil
cdef double complex spectrum(self, double k_y) nogil

cdef class IncAiry2d(Beam2dCartesian):
cdef:
double _W_y
double _M
double _W
double xi

cdef double _heaviside(self, double x) nogil
cdef double complex _f_Airy(self, double k_y, double W_y, double M, double W) nogil
cdef double complex spectrum(self, double k_y) nogil
14 changes: 7 additions & 7 deletions optbeam/_2d/beams.py
Expand Up @@ -4,20 +4,20 @@

try:
import cython
cython_imported = True
except ModuleNotFoundError:
cython = None
cython_imported = False

if not cython or not cython.compiled:
if not cython_imported or not cython.compiled:
from math import (exp as _exp,
sqrt as _sqrt)
from cmath import exp as _cexp
from .helpers import Beam2d, _complex_quad

if cython and not cython.compiled:
if cython_imported and not cython.compiled:
print("\nPlease consider compiling `%s.py` via Cython: "
"`$ cythonize -3 -i %s.py`\n" % (__name__, __name__))

from .helpers import _complex_quad, Beam2d


class Beam2dCartesian(Beam2d):
"""..."""
Expand All @@ -39,8 +39,8 @@ def profile(self, r):
try:
(result,
real_tol,
imag_tol) = _complex_quad(self if cython
and cython.compiled else self._integrand,
imag_tol) = _complex_quad(self if cython_imported and cython.compiled
else self._integrand,
-self._k, self._k)
except Exception as e:
print(type(e).__name__ + ":", e)
Expand Down
25 changes: 25 additions & 0 deletions optbeam/_2d/helpers.pxd
@@ -0,0 +1,25 @@
cimport cython
from cpython.pycapsule cimport PyCapsule_New
from cpython cimport bool


# -----------------------------------------------------------------------------
# function declarations
# -----------------------------------------------------------------------------
cdef double _imag_1d_func_c(int n, double *arr, void *func_ptr)
cdef double _real_1d_func_c(int n, double *arr, void *func_ptr)

@cython.locals(real=cython.double, imag=cython.double, real_tol=cython.double,
imag_tol=cython.double)
cpdef (double complex, double, double) _complex_quad(func, double a, double b, dict kwargs=*)

# -----------------------------------------------------------------------------
# class declarations
# -----------------------------------------------------------------------------
cdef class Beam2d:
cdef:
dict __dict__
double x, _k
public bool called

cdef double complex _integrand(self, double x) nogil
70 changes: 70 additions & 0 deletions optbeam/_3d/beams.pxd
@@ -0,0 +1,70 @@
from .helpers cimport Beam3d, _complex_dblquad

# -----------------------------------------------------------------------------
# declare C functions as "cpdef" to export them to the module
# -----------------------------------------------------------------------------
# TODO: check why we use cpdef and not cdef
cdef extern from "stdlib.h":
cpdef int _abs "abs" (int n) nogil

cdef extern from "math.h":
cpdef double _sin "sin" (double x) nogil
cpdef double _cos "cos" (double x) nogil
cpdef double _exp "exp" (double x) nogil
cpdef double _acos "acos" (double x) nogil
cpdef double _atan2 "atan2" (double y, double x) nogil

cdef extern from "complex.h":
cpdef double complex _cexp "cexp" (double complex z) nogil
cpdef double complex _csqrt "csqrt" (double complex z) nogil

# -----------------------------------------------------------------------------
# function declarations
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# class declarations
# -----------------------------------------------------------------------------
cdef class Beam3dSpherical(Beam3d):
cdef:
double _ry, _rz

cdef double _phase(self, double sin_theta, double cos_theta, double phi,
double x, double y, double z) nogil
# TODO: check if next line is actually needed since we can declared in Beam3d
cdef double complex spectrum(self, double sin_theta, double theta, double phi) nogil
# TODO: check if next line is actually needed since it is declared in Beam3d
cdef double complex _integrand(self, double theta, double phi) nogil

cdef class Beam3dCartesian(Beam3d):
cdef:
double _ry, _rz

cdef double _phase(self, double k_y, double k_z, double x, double y, double z) nogil
cdef double complex spectrum(self, double k_y, double k_z) nogil
cdef double complex _integrand(self, double k_y, double k_z) nogil

cdef class LaguerreGauss3d(Beam3dSpherical):
cdef:
int _m
double _W_y
double _norm

cdef double complex _f_Gauss_spherical(self, double sin_theta, double _W_y, double k) nogil
cdef double complex _f_Laguerre_Gauss_spherical(self, double sin_theta, double theta, double phi,
double _W_y, double k, int _m) nogil
cdef double complex spectrum(self, double sin_theta, double theta, double phi) nogil

cdef class LaguerreGauss3dCartesian(Beam3dCartesian):
cdef:
int _m
double _W_y
double _norm

cdef double _phi(self, double k_y, double k_z) nogil
cdef double _theta(self, double k_y, double k_z, double k) nogil
cdef double complex _f_Gauss_cartesian(self, double k_y, double k_z, double _W_y) nogil
cdef double complex _f_Laguerre_Gauss_cartesian(self, double k_y, double k_z,
double _W_y, double k, int _m) nogil
cdef double complex spectrum(self, double k_y, double k_z) nogil
12 changes: 7 additions & 5 deletions optbeam/_3d/beams.py
Expand Up @@ -4,10 +4,12 @@

try:
import cython
cython_imported = True
except ModuleNotFoundError:
cython = None
cython_imported = False

if not cython or not cython.compiled:
# TODO: check if conditional statement is needed at all
if not cython_imported or not cython.compiled:
from math import (sin as _sin,
cos as _cos,
exp as _exp,
Expand All @@ -16,13 +18,13 @@
from cmath import (exp as _cexp,
sqrt as _csqrt)
from builtins import abs as _abs
from .helpers import Beam3d, _complex_dblquad

if cython and not cython.compiled:

if cython_imported and not cython.compiled:
print("\nPlease consider compiling `%s.py` via Cython: "
"`$ cythonize -3 -i %s.py`\n" % (__name__, __name__))

from .helpers import _complex_dblquad, Beam3d


class Beam3dSpherical(Beam3d):
"""Reference implementaion of a 3d beam in spherical coordinates."""
Expand Down
27 changes: 27 additions & 0 deletions optbeam/_3d/helpers.pxd
@@ -0,0 +1,27 @@
cimport cython
from cpython.pycapsule cimport PyCapsule_New
from cpython cimport bool

# -----------------------------------------------------------------------------
# function declarations
# -----------------------------------------------------------------------------
cdef double _imag_2d_func_c(int n, double *arr, void *func_ptr)
cdef double _real_2d_func_c(int n, double *arr, void *func_ptr)

@cython.locals(real=cython.double, imag=cython.double, real_tol=cython.double,
imag_tol=cython.double)
cpdef (double complex, double, double) _complex_dblquad(Beam3d func,
double a, double b,
double gfun, double hfun,
dict kwargs=*)

# -----------------------------------------------------------------------------
# class declarations
# -----------------------------------------------------------------------------
cdef class Beam3d:
cdef:
dict __dict__
double x, _k
public bool called

cdef double complex _integrand(self, double x, double y) nogil
7 changes: 4 additions & 3 deletions optbeam/_3d/helpers.py
Expand Up @@ -3,10 +3,11 @@

try:
import cython
cython_imported = True
except ModuleNotFoundError:
cython = None
cython_imported = False

if cython:
if cython_imported:
if cython.compiled:
from scipy import LowLevelCallable
else:
Expand Down Expand Up @@ -49,7 +50,7 @@ def _real_2d_func_c(n, arr, func_ptr):

def _complex_dblquad(func, a, b, gfun, hfun, kwargs={}):
"""Integrate real and imaginary part of the given function."""
if cython and cython.compiled:
if cython_imported and cython.compiled:
# pure python formulation of: cdef void *f_ptr = <void*>func
f_ptr = cython.declare(cython.p_void, cython.cast(cython.p_void, func))

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
@@ -0,0 +1,2 @@
[build-system]
requires = ["setuptools", "wheel", "Cython"]
8 changes: 7 additions & 1 deletion setup.py
@@ -1,4 +1,5 @@
from setuptools import setup, find_packages
from Cython.Build import cythonize


with open("README.md") as f:
Expand All @@ -23,7 +24,12 @@
author_email="kotik@physics.org",
license=license,
packages=find_packages(exclude=("scripts")),
ext_modules=cythonize("optbeam/_2d/*.py",
"optbeam/_3d/*.py",
force=True,
compiler_directives={'language_level': 3}),
zip_safe=False,
include_package_data=True,
install_requires=["scipy"],
install_requires=["scipy", "cython"],
extras_require=extras,
)

0 comments on commit eca939b

Please sign in to comment.