123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- #ifndef SRC_NMIE_NEARFIELD_HPP_
- #define SRC_NMIE_NEARFIELD_HPP_
- #include <iostream>
- #include <iomanip>
- #include <stdexcept>
- #include <vector>
- namespace nmie {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename FloatType>
- void MultiLayerMie<FloatType>::calcExpanCoeffs() {
- if (!isScaCoeffsCalc_)
- throw std::invalid_argument("(calcExpanCoeffs) You should calculate external coefficients first!");
- isExpCoeffsCalc_ = false;
- std::complex<FloatType> c_one(1.0, 0.0), c_zero(0.0, 0.0);
- const int L = refractive_index_.size();
- aln_.resize(L + 1);
- bln_.resize(L + 1);
- cln_.resize(L + 1);
- dln_.resize(L + 1);
- for (int l = 0; l <= L; l++) {
- aln_[l].resize(nmax_);
- bln_[l].resize(nmax_);
- cln_[l].resize(nmax_);
- dln_[l].resize(nmax_);
- }
-
-
- for (int n = 0; n < nmax_; n++) {
- aln_[L][n] = an_[n];
- bln_[L][n] = bn_[n];
- cln_[L][n] = c_one;
- dln_[L][n] = c_one;
- }
- std::vector<std::complex<FloatType> > D1z(nmax_ + 1), D1z1(nmax_ + 1), D3z(nmax_ + 1), D3z1(nmax_ + 1);
- std::vector<std::complex<FloatType> > Psiz(nmax_ + 1), Psiz1(nmax_ + 1), Zetaz(nmax_ + 1), Zetaz1(nmax_ + 1);
- std::complex<FloatType> denomZeta, denomPsi, T1, T2, T3, T4;
- auto &m = refractive_index_;
- std::vector< std::complex<FloatType> > m1(L);
- for (int l = 0; l < L - 1; l++) m1[l] = m[l + 1];
- m1[L - 1] = std::complex<FloatType> (1.0, 0.0);
- std::complex<FloatType> z, z1;
- for (int l = L - 1; l >= 0; l--) {
- if (l <= PEC_layer_position_) {
- for (int n = 0; n < nmax_; n++) {
-
- aln_[l][n] = c_zero;
-
- bln_[l][n] = c_zero;
-
- cln_[l][n] = c_zero;
-
- dln_[l][n] = c_zero;
- }
- } else {
- z = size_param_[l]*m[l];
- z1 = size_param_[l]*m1[l];
- calcD1D3(z, D1z, D3z);
- calcD1D3(z1, D1z1, D3z1);
- calcPsiZeta(z, Psiz, Zetaz);
- calcPsiZeta(z1, Psiz1, Zetaz1);
- for (int n = 0; n < nmax_; n++) {
- int n1 = n + 1;
- denomZeta = Zetaz[n1]*(D1z[n1] - D3z[n1]);
- denomPsi = Psiz[n1]*(D1z[n1] - D3z[n1]);
- T1 = aln_[l + 1][n]*Zetaz1[n1] - dln_[l + 1][n]*Psiz1[n1];
- T2 = (bln_[l + 1][n]*Zetaz1[n1] - cln_[l + 1][n]*Psiz1[n1])*m[l]/m1[l];
- T3 = (dln_[l + 1][n]*D1z1[n1]*Psiz1[n1] - aln_[l + 1][n]*D3z1[n1]*Zetaz1[n1])*m[l]/m1[l];
- T4 = cln_[l + 1][n]*D1z1[n1]*Psiz1[n1] - bln_[l + 1][n]*D3z1[n1]*Zetaz1[n1];
-
- aln_[l][n] = (D1z[n1]*T1 + T3)/denomZeta;
-
- bln_[l][n] = (D1z[n1]*T2 + T4)/denomZeta;
-
- cln_[l][n] = (D3z[n1]*T2 + T4)/denomPsi;
-
- dln_[l][n] = (D3z[n1]*T1 + T3)/denomPsi;
- }
- }
- }
-
- for (int n = 0; n < nmax_; ++n) {
- if (cabs(aln_[0][n]) < 1e-10) aln_[0][n] = 0.0;
- else {
-
- std::cout<< std::setprecision(100)
- << "Warning: Potentially unstable calculation of aln[0]["
- << n << "] = "<< aln_[0][n] <<std::endl;
- aln_[0][n] = 0.0;
- }
- if (cabs(bln_[0][n]) < 1e-10) bln_[0][n] = 0.0;
- else {
-
- std::cout<< std::setprecision(100)
- << "Warning: Potentially unstable calculation of bln[0]["
- << n << "] = "<< bln_[0][n] <<std::endl;
- bln_[0][n] = 0.0;
- }
- }
- isExpCoeffsCalc_ = true;
- }
- template <typename FloatType>
- void MultiLayerMie<FloatType>::convertFieldsFromSphericalToCartesian() {
- long total_points = coords_polar_.size();
- E_.clear(); H_.clear();
- for (int point=0; point < total_points; point++) {
- auto Theta = coords_polar_[point][1];
- auto Phi = coords_polar_[point][2];
- auto Es = Es_[point];
- auto Hs = Hs_[point];
- using nmm::sin;
- using nmm::cos;
- E_.push_back({ sin(Theta)*cos(Phi)*Es[0] + cos(Theta)*cos(Phi)*Es[1] - sin(Phi)*Es[2],
- sin(Theta)*sin(Phi)*Es[0] + cos(Theta)*sin(Phi)*Es[1] + cos(Phi)*Es[2],
- cos(Theta)*Es[0] - sin(Theta)*Es[1]});
- H_.push_back({ sin(Theta)*cos(Phi)*Hs[0] + cos(Theta)*cos(Phi)*Hs[1] - sin(Phi)*Hs[2],
- sin(Theta)*sin(Phi)*Hs[0] + cos(Theta)*sin(Phi)*Hs[1] + cos(Phi)*Hs[2],
- cos(Theta)*Hs[0] - sin(Theta)*Hs[1]});
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename FloatType> template <typename evalType>
- void MultiLayerMie<FloatType>::calcFieldByComponents(const evalType Rho,
- const evalType Theta, const evalType Phi,
- const std::vector<std::complex<evalType> > &Psi,
- const std::vector<std::complex<evalType> > &D1n,
- const std::vector<std::complex<evalType> > &Zeta,
- const std::vector<std::complex<evalType> > &D3n,
- const std::vector<evalType> &Pi,
- const std::vector<evalType> &Tau,
- std::vector<std::complex<evalType> > &E,
- std::vector<std::complex<evalType> > &H) {
- auto nmax = Psi.size() - 1;
- std::complex<evalType> c_zero(0.0, 0.0), c_i(0.0, 1.0), c_one(1.0, 0.0);
-
- std::vector<std::complex<evalType> > ipow = {c_one, c_i, -c_one, -c_i};
- std::vector<std::complex<evalType> > M3o1n(3), M3e1n(3), N3o1n(3), N3e1n(3);
- std::vector<std::complex<evalType> > M1o1n(3), M1e1n(3), N1o1n(3), N1e1n(3);
- std::complex<evalType> ml;
-
- for (int i = 0; i < 3; i++) {
- E[i] = c_zero;
- H[i] = c_zero;
- }
- unsigned int l;
- GetIndexAtRadius(Rho, ml, l);
- for (unsigned int n = 0; n < nmax; n++) {
- int n1 = n + 1;
- auto rn = static_cast<evalType>(n1);
-
- calcSpherHarm(Rho*ml, Theta, Phi, Psi[n1], D1n[n1], Pi[n], Tau[n], rn, M1o1n, M1e1n, N1o1n, N1e1n);
- calcSpherHarm(Rho*ml, Theta, Phi, Zeta[n1], D3n[n1], Pi[n], Tau[n], rn, M3o1n, M3e1n, N3o1n, N3e1n);
-
- std::complex<evalType> En = ipow[n1 % 4]
- *static_cast<evalType>((rn + rn + 1.0)/(rn*rn + rn));
- std::complex<evalType> Ediff, Hdiff;
- for (int i = 0; i < 3; i++) {
- Ediff = En*( cln_[l][n]*M1o1n[i] - c_i*dln_[l][n]*N1e1n[i]
- + c_i*aln_[l][n]*N3e1n[i] - bln_[l][n]*M3o1n[i]);
- Hdiff = En*( -dln_[l][n]*M1e1n[i] - c_i*cln_[l][n]*N1o1n[i]
- + c_i*bln_[l][n]*N3o1n[i] + aln_[l][n]*M3e1n[i]);
- if (nmm::isnan(Ediff.real()) || nmm::isnan(Ediff.imag()) ||
- nmm::isnan(Hdiff.real()) || nmm::isnan(Hdiff.imag())
- ) {
- std::cout << "Unexpected truncation during near-field evaluation at n = "<< n
- << " (of total nmax = "<<nmax<<")!!!"<<std::endl;
- break;
- }
- if (mode_n_ == Modes::kAll) {
-
- E[i] += Ediff;
- H[i] += Hdiff;
- continue;
- }
- if (n1 == mode_n_) {
- if (mode_type_ == Modes::kElectric || mode_type_ == Modes::kAll) {
- E[i] += En*( -c_i*dln_[l][n]*N1e1n[i]
- + c_i*aln_[l][n]*N3e1n[i]);
- H[i] += En*(-dln_[l][n]*M1e1n[i]
- +aln_[l][n]*M3e1n[i]);
-
- }
- if (mode_type_ == Modes::kMagnetic || mode_type_ == Modes::kAll) {
- E[i] += En*( cln_[l][n]*M1o1n[i]
- - bln_[l][n]*M3o1n[i]);
- H[i] += En*( -c_i*cln_[l][n]*N1o1n[i]
- + c_i*bln_[l][n]*N3o1n[i]);
-
- }
-
- }
-
- }
- if (nmm::isnan(Ediff.real()) || nmm::isnan(Ediff.imag()) ||
- nmm::isnan(Hdiff.real()) || nmm::isnan(Hdiff.imag())
- ) break;
- }
-
- std::complex<evalType> hffact = ml/static_cast<evalType>(cc_*mu_);
- for (int i = 0; i < 3; i++) {
- H[i] = hffact*H[i];
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <typename FloatType>
- void MultiLayerMie<FloatType>::RunFieldCalculation() {
-
- calcScattCoeffs();
-
- calcExpanCoeffs();
- Es_.clear(); Hs_.clear(); coords_polar_.clear();
- long total_points = coords_[0].size();
- for (int point = 0; point < total_points; point++) {
- const FloatType &Xp = coords_[0][point];
- const FloatType &Yp = coords_[1][point];
- const FloatType &Zp = coords_[2][point];
-
- auto Rho = nmm::sqrt(pow2(Xp) + pow2(Yp) + pow2(Zp));
-
- auto Theta = (Rho > 0.0) ? nmm::acos(Zp/Rho) : 0.0;
-
- auto Phi = nmm::atan2(Yp,Xp);
- coords_polar_.push_back({Rho, Theta, Phi});
-
- if (Rho < 1e-5) Rho = 1e-5;
-
-
-
-
-
-
- std::vector<std::complex<FloatType> > Es(3), Hs(3);
-
- std::vector<std::complex<FloatType> > Psi(nmax_ + 1), D1n(nmax_ + 1), Zeta(nmax_ + 1), D3n(nmax_ + 1);
- std::vector<FloatType> Pi(nmax_), Tau(nmax_);
- std::complex<FloatType> ml;
- GetIndexAtRadius(Rho, ml);
-
- calcD1D3(Rho*ml, D1n, D3n);
-
- calcPsiZeta(Rho*ml, Psi, Zeta);
-
- calcPiTau(nmm::cos(Theta), Pi, Tau);
- calcFieldByComponents(Rho, Theta, Phi, Psi, D1n, Zeta, D3n, Pi, Tau, Es, Hs);
- Es_.push_back(Es);
- Hs_.push_back(Hs);
- }
- convertFieldsFromSphericalToCartesian();
- }
- template <typename FloatType>
- double eval_delta(const unsigned int steps, const double from_value, const double to_value) {
- auto delta = std::abs(from_value - to_value);
- if (steps < 2) return delta;
- delta /= static_cast<double>(steps-1);
-
- if ( (2.*delta)/std::abs(from_value+to_value) < 1e-9)
- throw std::invalid_argument("Error! The step is too fine, not supported!");
- return delta;
- }
- template <typename FloatType> template <typename evalType>
- void MultiLayerMie<FloatType>::GetIndexAtRadius(const evalType Rho,
- std::complex<evalType> &ml,
- unsigned int &l) {
- l = 0;
- if (Rho > size_param_.back()) {
- l = size_param_.size();
- ml = std::complex<FloatType>(1.0, 0.0);
- } else {
- for (int i = size_param_.size() - 1; i >= 0 ; i--) {
- if (Rho <= size_param_[i]) {
- l = i;
- }
- }
- ml = refractive_index_[l];
- }
- }
- template <typename FloatType> template <typename evalType>
- void MultiLayerMie<FloatType>::GetIndexAtRadius(const evalType Rho,
- std::complex<evalType> &ml) {
- unsigned int l;
- GetIndexAtRadius(Rho, ml, l);
- }
- template <typename FloatType>
- void MultiLayerMie<FloatType>::calcMieSeriesNeededToConverge(const FloatType Rho) {
- auto required_near_field_nmax = calcNmax(Rho);
- SetMaxTerms(required_near_field_nmax);
-
- calcScattCoeffs();
-
- available_maximal_nmax_ = nmax_;
-
- calcExpanCoeffs();
- }
- template <typename FloatType>
- void MultiLayerMie<FloatType>::calcRadialOnlyDependantFunctions(const double from_Rho, const double to_Rho,
- const bool isIgnoreAvailableNmax,
- std::vector<std::vector<std::complex<FloatType> > > &Psi,
- std::vector<std::vector<std::complex<FloatType> > > &D1n,
- std::vector<std::vector<std::complex<FloatType> > > &Zeta,
- std::vector<std::vector<std::complex<FloatType> > > &D3n) {
- auto radius_points = Psi.size();
- std::vector<std::vector<std::complex<FloatType> > > PsiZeta(radius_points);
- double delta_Rho = eval_delta<double>(radius_points, from_Rho, to_Rho);
- for (unsigned int j=0; j < radius_points; j++) {
- auto Rho = static_cast<FloatType>(from_Rho + j*delta_Rho);
- int near_field_nmax = calcNmax(Rho);
-
- if (near_field_nmax > available_maximal_nmax_ && !isIgnoreAvailableNmax) continue;
- if (near_field_nmax > available_maximal_nmax_) near_field_nmax = available_maximal_nmax_;
- Psi[j].resize(near_field_nmax + 1); D1n[j].resize(near_field_nmax + 1);
- Zeta[j].resize(near_field_nmax + 1); D3n[j].resize(near_field_nmax + 1);
- PsiZeta[j].resize(near_field_nmax + 1);
- std::complex<FloatType> ml;
- GetIndexAtRadius(Rho, ml);
- auto z = Rho*ml;
- evalDownwardD1(z, D1n[j]);
- evalUpwardPsi(z, D1n[j], Psi[j]);
- evalUpwardD3 (z, D1n[j], D3n[j], PsiZeta[j]);
- for (unsigned int k = 0; k < Zeta[j].size(); k++) {
- Zeta[j][k] = PsiZeta[j][k]/Psi[j][k];
- }
- }
- }
- template <typename FloatType>
- void MultiLayerMie<FloatType>::RunFieldCalculationPolar(const int outer_arc_points,
- const int radius_points,
- const double from_Rho, const double to_Rho,
- const double from_Theta, const double to_Theta,
- const double from_Phi, const double to_Phi,
- const bool isIgnoreAvailableNmax) {
- if (from_Rho > to_Rho || from_Theta > to_Theta || from_Phi > to_Phi
- || outer_arc_points < 1 || radius_points < 1
- || from_Rho < 0.)
- throw std::invalid_argument("Error! Invalid argument for RunFieldCalculationPolar() !");
- int theta_points = 0, phi_points = 0;
- if (to_Theta-from_Theta > to_Phi-from_Phi) {
- theta_points = outer_arc_points;
- phi_points = static_cast<int>((to_Phi-from_Phi)/(to_Theta-from_Theta) * outer_arc_points);
- } else {
- phi_points = outer_arc_points;
- theta_points = static_cast<int>((to_Theta-from_Theta)/(to_Phi-from_Phi) * outer_arc_points);
- }
- if (theta_points == 0) theta_points = 1;
- if (phi_points == 0) phi_points = 1;
- calcMieSeriesNeededToConverge(to_Rho);
- std::vector<std::vector<FloatType> > Pi(theta_points), Tau(theta_points);
- calcPiTauAllTheta(from_Theta, to_Theta, Pi, Tau);
- std::vector<std::vector<std::complex<FloatType> > > Psi(radius_points), D1n(radius_points),
- Zeta(radius_points), D3n(radius_points), PsiZeta(radius_points);
- calcRadialOnlyDependantFunctions(from_Rho, to_Rho, isIgnoreAvailableNmax,
- Psi, D1n, Zeta, D3n);
- double delta_Rho = eval_delta<double>(radius_points, from_Rho, to_Rho);
- double delta_Theta = eval_delta<double>(theta_points, from_Theta, to_Theta);
- double delta_Phi = eval_delta<double>(phi_points, from_Phi, to_Phi);
- Es_.clear(); Hs_.clear(); coords_polar_.clear();
- for (int j=0; j < radius_points; j++) {
- auto Rho = static_cast<FloatType>(from_Rho + j * delta_Rho);
- for (int i = 0; i < outer_arc_points; i++) {
- auto Theta = static_cast<FloatType>(from_Theta + i * delta_Theta);
- for (int k = 0; k < outer_arc_points; k++) {
- auto Phi = static_cast<FloatType>(from_Phi + k * delta_Phi);
- coords_polar_.push_back({Rho, Theta, Phi});
-
- std::vector<std::complex<FloatType> > Es(3), Hs(3);
- calcFieldByComponents(Rho, Theta, Phi,
- Psi[j], D1n[j], Zeta[j], D3n[j],
- Pi[i], Tau[i], Es, Hs);
- Es_.push_back(Es);
- Hs_.push_back(Hs);
- }
- }
- }
- convertFieldsFromSphericalToCartesian();
- }
- }
- #endif
|