nmie-precision.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef SRC_NMIE_PRECISION_H_
  2. #define SRC_NMIE_PRECISION_H_
  3. //**********************************************************************************//
  4. // Copyright (C) 2009-2018 Ovidio Pena <ovidio@bytesfall.com> //
  5. // Copyright (C) 2013-2018 Konstantin Ladutenko <kostyfisik@gmail.com> //
  6. // //
  7. // This file is part of scattnlay //
  8. // //
  9. // This program is free software: you can redistribute it and/or modify //
  10. // it under the terms of the GNU General Public License as published by //
  11. // the Free Software Foundation, either version 3 of the License, or //
  12. // (at your option) any later version. //
  13. // //
  14. // This program is distributed in the hope that it will be useful, //
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  17. // GNU General Public License for more details. //
  18. // //
  19. // The only additional remark is that we expect that all publications //
  20. // describing work using this software, or all commercial products //
  21. // using it, cite at least one of the following references: //
  22. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  23. // a multilayered sphere," Computer Physics Communications, //
  24. // vol. 180, Nov. 2009, pp. 2348-2354. //
  25. // [2] K. Ladutenko, U. Pal, A. Rivera, and O. Pena-Rodriguez, "Mie //
  26. // calculation of electromagnetic near-field for a multilayered //
  27. // sphere," Computer Physics Communications, vol. 214, May 2017, //
  28. // pp. 225-230. //
  29. // //
  30. // You should have received a copy of the GNU General Public License //
  31. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  32. //**********************************************************************************//
  33. //**********************************************************************************//
  34. // This class implements the algorithm for a multilayered sphere described by: //
  35. // [1] W. Yang, "Improved recursive algorithm for light scattering by a //
  36. // multilayered sphere,” Applied Optics, vol. 42, Mar. 2003, pp. 1710-1720. //
  37. // //
  38. // You can find the description of all the used equations in: //
  39. // [2] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  40. // a multilayered sphere," Computer Physics Communications, //
  41. // vol. 180, Nov. 2009, pp. 2348-2354. //
  42. // //
  43. // Hereinafter all equations numbers refer to [2] //
  44. //**********************************************************************************//
  45. #ifdef MULTI_PRECISION
  46. #include <boost/multiprecision/number.hpp>
  47. #include <boost/multiprecision/cpp_bin_float.hpp>
  48. #endif // MULTI_PRECISION
  49. namespace nmie {
  50. #ifdef MULTI_PRECISION
  51. namespace nmm = boost::multiprecision;
  52. typedef nmm::number<nmm::cpp_bin_float<MULTI_PRECISION> > FloatType;
  53. #else
  54. namespace nmm = std;
  55. typedef double FloatType;
  56. //typedef float FloatType;
  57. #endif // MULTI_PRECISION
  58. template<class T> T sin_t(T v) {
  59. if (std::is_same<T, double>::value) return static_cast<T>(std::sin(static_cast<double>(v)));
  60. return static_cast<T>(nmm::sin(static_cast<FloatType >(v)));
  61. }
  62. template<class T> T cos_t(T v) {
  63. if (std::is_same<T, double>::value) return static_cast<T>(std::cos(static_cast<double>(v)));
  64. return static_cast<T>(nmm::cos(static_cast<FloatType >(v)));
  65. }
  66. template<class T> T sqrt_t(T v) {
  67. if (std::is_same<T, double>::value) return static_cast<T>(std::sqrt(static_cast<double>(v)));
  68. return static_cast<T>(nmm::sqrt(static_cast<FloatType >(v)));
  69. }
  70. template <typename ToFloatType, typename FromFloatType>
  71. std::vector<ToFloatType> ConvertVector(const std::vector<FromFloatType> x) {
  72. std::vector<ToFloatType> new_x;
  73. for (auto element : x) {
  74. new_x.push_back(static_cast<ToFloatType>(element));
  75. }
  76. return new_x;
  77. }
  78. template <typename ToFloatType, typename FromFloatType>
  79. std::complex<ToFloatType> ConvertComplex(std::complex<FromFloatType> z) {
  80. return std::complex<ToFloatType>(static_cast<ToFloatType>(z.real()),
  81. static_cast<ToFloatType>(z.imag()));
  82. }
  83. template <typename ToFloatType, typename FromFloatType>
  84. std::vector<std::complex<ToFloatType> > ConvertComplexVector(std::vector<std::complex<FromFloatType> > x) {
  85. std::vector<std::complex<ToFloatType> > new_x;
  86. for (auto element : x) {
  87. new_x.push_back(std::complex<ToFloatType>(static_cast<ToFloatType>(element.real()),
  88. static_cast<ToFloatType>(element.imag()) ) );
  89. }
  90. return new_x;
  91. }
  92. template <typename ToFloatType, typename FromFloatType>
  93. std::vector<std::vector<std::complex<ToFloatType> > > ConvertComplexVectorVector(std::vector<std::vector<std::complex<FromFloatType> > > x) {
  94. std::vector<std::vector<std::complex<ToFloatType> > > new_x;
  95. std::vector<std::complex<ToFloatType> > new_y;
  96. for (auto y : x) {
  97. new_y.clear();
  98. for (auto element : y) {
  99. new_y.push_back(std::complex<ToFloatType>(static_cast<ToFloatType>(element.real()),
  100. static_cast<ToFloatType>(element.imag()) ) );
  101. }
  102. new_x.push_back(new_y);
  103. }
  104. return new_x;
  105. }
  106. } // end of namespace nmie
  107. #endif // SRC_NMIE_PRECISION_H_