123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- # Copyright (C) 2009-2017 Ovidio Pena <ovidio@bytesfall.com>
- # Copyright (C) 2013-2017 Konstantin Ladutenko <kostyfisik@gmail.com>
- #
- # This file is part of python-scattnlay
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # The only additional remark is that we expect that all publications
- # describing work using this software, or all commercial products
- # using it, cite the following reference:
- # O. Pena and U. Pal, "Scattering of electromagnetic radiation by
- # a multilayered sphere," Computer Physics Communications,
- # vol. 180, Nov. 2009, pp. 2348-2354.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- # distutils: language = c++
- # distutils: sources = nmie.cc
- from __future__ import division
- import numpy as np
- cimport numpy as np
- from libcpp.vector cimport vector
- from libcpp.vector cimport complex
- cdef inline double *npy2c(np.ndarray a):
- assert a.dtype == np.float64
- if not (<object>a).flags: # Array is not contiguous, need to make contiguous copy
- a = a.copy('C')
- # Return data pointer
- return <double *>(a.data)
- cdef extern from "py_nmie.h":
- cdef int ScattCoeffs(int L, int pl, vector x, vector m, int nmax, double anr, double ani, double bnr, double bni)
- # cdef int ExpansionCoeffs( int L, int pl, vector x, vector m,
- # int nmax,
- # vector& alnr,
- # vector& alni,
- # vector& blnr,
- # vector& blni,
- # vector& clnr,
- # vector& clni,
- # vector& dlnr,
- # vector& dlni)
- cdef int ExpansionCoeffs(int L, int pl, vector x, vector m, int nmax, double alnr, double alni, double blnr, double blni, double clnr, double clni, double dlnr, double dlni)
- cdef int nMie(int L, int pl, vector x, vector m, int nTheta, vector Theta, int nmax, double *Qext, double *Qsca, double *Qabs, double *Qbk, double *Qpr, double *g, double *Albedo, double S1r, double S1i, double S2r, double S2i)
- cdef int nField(int L, int pl, vector x, vector m, int nmax, int mode_n, int mode_type, int nCoords, vector Xp, vector Yp, vector Zp, double Erx, double Ery, double Erz, double Eix, double Eiy, double Eiz, double Hrx, double Hry, double Hrz, double Hix, double Hiy, double Hiz)
- def scattcoeffs(np.ndarray x, np.ndarray m, np.int_t nmax, np.int_t pl = -1):
- cdef Py_ssize_t i
- cdef np.ndarray terms = np.zeros(x.shape, dtype = np.int)
- cdef np.ndarray an = np.zeros(
- (x.shape, nmax), dtype = np.complex128)
- cdef np.ndarray bn = np.zeros(
- (x.shape, nmax), dtype = np.complex128)
- cdef np.ndarray anr
- cdef np.ndarray ani
- cdef np.ndarray bnr
- cdef np.ndarray bni
- for i in range(x.shape):
- anr = np.zeros(nmax, dtype = np.float64)
- ani = np.zeros(nmax, dtype = np.float64)
- bnr = np.zeros(nmax, dtype = np.float64)
- bni = np.zeros(nmax, dtype = np.float64)
- terms = ScattCoeffs(x.shape, pl, x.copy('C'), m.copy('C'), nmax, npy2c(anr), npy2c(ani), npy2c(bnr), npy2c(bni))
- an = anr.copy('C') + 1.0j*ani.copy('C')
- bn = bnr.copy('C') + 1.0j*bni.copy('C')
- return terms, an, bn
- def expansioncoeffs(np.ndarray x, np.ndarray m, np.int_t nmax, np.int_t pl = -1):
- cdef Py_ssize_t i
- cdef Py_ssize_t l
- cdef np.ndarray terms = np.zeros(x.shape, dtype = np.int)
- cdef np.ndarray aln = np.zeros((x.shape, x.shape+1, nmax), dtype = np.complex128)
- cdef np.ndarray bln = np.zeros((x.shape, x.shape+1, nmax), dtype = np.complex128)
- cdef np.ndarray cln = np.zeros((x.shape, x.shape+1, nmax), dtype = np.complex128)
- cdef np.ndarray dln = np.zeros((x.shape, x.shape+1, nmax), dtype = np.complex128)
- cdef np.ndarray alnr
- cdef np.ndarray alni
- cdef np.ndarray blnr
- cdef np.ndarray blni
- cdef np.ndarray clnr
- cdef np.ndarray clni
- cdef np.ndarray dlnr
- cdef np.ndarray dlni
- for i in range(x.shape):
- alnr = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- alni = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- blnr = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- blni = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- clnr = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- clni = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- dlnr = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- dlni = np.zeros((x.shape+1)*nmax, dtype = np.float64)
- #terms = ExpansionCoeffs(x.shape, pl, x.copy('C'), m.copy('C'), nmax, alnr, alni, blnr, blni, clnr, clni, dlnr, dlni)
- terms = ExpansionCoeffs(x.shape, pl, x.copy('C'), m.copy('C'), nmax, npy2c(alnr), npy2c(alni), npy2c(blnr), npy2c(blni), npy2c(clnr), npy2c(clni), npy2c(dlnr), npy2c(dlni))
- for l in range(x.shape+1):
- aln = alnr.copy('C') + 1.0j*alni.copy('C')
- bln = blnr.copy('C') + 1.0j*blni.copy('C')
- cln = clnr.copy('C') + 1.0j*clni.copy('C')
- dln = dlnr.copy('C') + 1.0j*dlni.copy('C')
- return terms, aln, bln, cln, dln
- def scattnlay(np.ndarray x, np.ndarray m, np.ndarray theta = np.zeros(0, dtype = np.float64), np.int_t nmax = -1, np.int_t pl = -1):
- cdef Py_ssize_t i
- cdef np.ndarray terms = np.zeros(x.shape, dtype = np.int)
- cdef np.ndarray Qext = np.zeros(x.shape, dtype = np.float64)
- cdef np.ndarray Qabs = np.zeros(x.shape, dtype = np.float64)
- cdef np.ndarray Qsca = np.zeros(x.shape, dtype = np.float64)
- cdef np.ndarray Qbk = np.zeros(x.shape, dtype = np.float64)
- cdef np.ndarray Qpr = np.zeros(x.shape, dtype = np.float64)
- cdef np.ndarray g = np.zeros(x.shape, dtype = np.float64)
- cdef np.ndarray Albedo = np.zeros(x.shape, dtype = np.float64)
- cdef np.ndarray S1 = np.zeros((x.shape, theta.shape), dtype = np.complex128)
- cdef np.ndarray S2 = np.zeros((x.shape, theta.shape), dtype = np.complex128)
- cdef np.ndarray S1r
- cdef np.ndarray S1i
- cdef np.ndarray S2r
- cdef np.ndarray S2i
- for i in range(x.shape):
- S1r = np.zeros(theta.shape, dtype = np.float64)
- S1i = np.zeros(theta.shape, dtype = np.float64)
- S2r = np.zeros(theta.shape, dtype = np.float64)
- S2i = np.zeros(theta.shape, dtype = np.float64)
- terms = nMie(x.shape, pl, x.copy('C'), m.copy('C'), theta.shape, theta.copy('C'), nmax, &Qext, &Qsca, &Qabs, &Qbk, &Qpr, &g, &Albedo, npy2c(S1r), npy2c(S1i), npy2c(S2r), npy2c(S2i))
- S1 = S1r.copy('C') + 1.0j*S1i.copy('C')
- S2 = S2r.copy('C') + 1.0j*S2i.copy('C')
- return terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2
- def fieldnlay(np.ndarray x, np.ndarray m, np.ndarray coords, np.int_t nmax = -1, np.int_t mode_n = -1, np.int_t mode_type = -1, np.int_t pl = -1):
- cdef Py_ssize_t i
- cdef np.ndarray terms = np.zeros(x.shape, dtype = np.int)
- cdef np.ndarray E = np.zeros((x.shape, coords.shape, 3), dtype = np.complex128)
- cdef np.ndarray H = np.zeros((x.shape, coords.shape, 3), dtype = np.complex128)
- cdef np.ndarray Erx
- cdef np.ndarray Ery
- cdef np.ndarray Erz
- cdef np.ndarray Eix
- cdef np.ndarray Eiy
- cdef np.ndarray Eiz
- cdef np.ndarray Hrx
- cdef np.ndarray Hry
- cdef np.ndarray Hrz
- cdef np.ndarray Hix
- cdef np.ndarray Hiy
- cdef np.ndarray Hiz
- for i in range(x.shape):
- Erx = np.zeros(coords.shape, dtype = np.float64)
- Ery = np.zeros(coords.shape, dtype = np.float64)
- Erz = np.zeros(coords.shape, dtype = np.float64)
- Eix = np.zeros(coords.shape, dtype = np.float64)
- Eiy = np.zeros(coords.shape, dtype = np.float64)
- Eiz = np.zeros(coords.shape, dtype = np.float64)
- Hrx = np.zeros(coords.shape, dtype = np.float64)
- Hry = np.zeros(coords.shape, dtype = np.float64)
- Hrz = np.zeros(coords.shape, dtype = np.float64)
- Hix = np.zeros(coords.shape, dtype = np.float64)
- Hiy = np.zeros(coords.shape, dtype = np.float64)
- Hiz = np.zeros(coords.shape, dtype = np.float64)
- terms = nField(x.shape, pl, x.copy('C'), m.copy('C'), nmax, mode_n, mode_type, coords.shape, coords.copy('C'), coords.copy('C'), coords.copy('C'), npy2c(Erx), npy2c(Ery), npy2c(Erz), npy2c(Eix), npy2c(Eiy), npy2c(Eiz), npy2c(Hrx), npy2c(Hry), npy2c(Hrz), npy2c(Hix), npy2c(Hiy), npy2c(Hiz))
- E = np.vstack((Erx.copy('C') + 1.0j*Eix.copy('C'), Ery.copy('C') + 1.0j*Eiy.copy('C'), Erz.copy('C') + 1.0j*Eiz.copy('C'))).transpose()
- H = np.vstack((Hrx.copy('C') + 1.0j*Hix.copy('C'), Hry.copy('C') + 1.0j*Hiy.copy('C'), Hrz.copy('C') + 1.0j*Hiz.copy('C'))).transpose()
- return terms, E, H
|