sph_harm.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef _SCTL_SPH_HARM_HPP_
  2. #define _SCTL_SPH_HARM_HPP_
  3. #define SCTL_SHMAXDEG 1024
  4. #include SCTL_INCLUDE(matrix.hpp)
  5. #include SCTL_INCLUDE(fft_wrapper.hpp)
  6. #include SCTL_INCLUDE(common.hpp)
  7. namespace SCTL_NAMESPACE {
  8. enum class SHCArrange {
  9. // (p+1) x (p+1) complex elements in row-major order.
  10. // A : { A(0,0), A(0,1), ... A(0,p), A(1,0), ... A(p,p) }
  11. // where, A(n,m) = { Ar(n,m), Ai(n,m) } (real and imaginary parts)
  12. ALL,
  13. // (p+1)(p+2)/2 complex elements in row-major order (lower triangular part)
  14. // A : { A(0,0), A(1,0), A(1,1), A(2,0), A(2,1), A(2,2), ... A(p,p) }
  15. // where, A(n,m) = { Ar(n,m), Ai(n,m) } (real and imaginary parts)
  16. ROW_MAJOR,
  17. // (p+1)(p+1) real elements in col-major order (non-zero lower triangular part)
  18. // A : { Ar(0,0), Ar(1,0), ... Ar(p,0), Ar(1,1), ... Ar(p,1), Ai(1,1), ... Ai(p,1), ..., Ar(p,p), Ai(p,p)
  19. // where, A(n,m) = { Ar(n,m), Ai(n,m) } (real and imaginary parts)
  20. COL_MAJOR_NONZERO
  21. };
  22. template <class Real> class SphericalHarmonics{
  23. static constexpr Integer COORD_DIM = 3;
  24. public:
  25. static void Grid2SHC(const Vector<Real>& X_in, Long Nt_in, Long Np_in, Long p_out, Vector<Real>& S_out, SHCArrange arrange_out);
  26. static void SHC2Grid(const Vector<Real>& S_in, SHCArrange arrange_in, Long p_in, Long Nt_out, Long Np_out, Vector<Real>* X_out, Vector<Real>* X_theta_out=nullptr, Vector<Real>* X_phi_out=nullptr);
  27. static void SHC2Pole(const Vector<Real>& S_in, SHCArrange arrange_in, Long p_in, Vector<Real>& P_out);
  28. static void WriteVTK(const char* fname, const Vector<Real>* S, const Vector<Real>* f_val, SHCArrange arrange, Long p_in, Long p_out, Real period=0, const Comm& comm = Comm::World());
  29. static void Grid2VecSHC(const Vector<Real>& X_in, Long Nt_in, Long Np_in, Long p_out, Vector<Real>& S_out, SHCArrange arrange_out);
  30. static void test() {
  31. int p = 3;
  32. int dof = 2;
  33. int Ncoeff = (p + 1) * (p + 1);
  34. Vector<Real> Xcoeff(dof * Ncoeff);
  35. for (int i=0;i<Xcoeff.Dim();i++) Xcoeff[i]=i;
  36. Vector<Real> Xgrid;
  37. int Nt = p+1, Np = 2*p+1;
  38. SHC2Grid(Xcoeff, sctl::SHCArrange::COL_MAJOR_NONZERO, p, Nt, Np, &Xgrid);
  39. Grid2SHC(Xgrid, Nt, Np, p, Xcoeff, sctl::SHCArrange::ROW_MAJOR);
  40. int indx=0;
  41. for (int i=0;i<dof;i++) {
  42. for (int n=0;n<=p;n++){
  43. std::cout<<Vector<Real>(2*n+2,Xcoeff.begin()+indx);
  44. indx+=2*n+2;
  45. }
  46. }
  47. SphericalHarmonics<Real>::WriteVTK("test", nullptr, &Xcoeff, sctl::SHCArrange::ROW_MAJOR, p, 32);
  48. }
  49. static void Clear() { MatrixStore().Resize(0); }
  50. private:
  51. // Probably don't work anymore, need to be updated :(
  52. static void SHC2GridTranspose(const Vector<Real>& X, Long p0, Long p1, Vector<Real>& S);
  53. static void RotateAll(const Vector<Real>& S, Long p0, Long dof, Vector<Real>& S_);
  54. static void RotateTranspose(const Vector<Real>& S_, Long p0, Long dof, Vector<Real>& S);
  55. static void StokesSingularInteg(const Vector<Real>& S, Long p0, Long p1, Vector<Real>* SLMatrix=nullptr, Vector<Real>* DLMatrix=nullptr);
  56. static void Grid2SHC_(const Vector<Real>& X, Long Nt, Long Np, Long p, Vector<Real>& B1);
  57. static void SHCTranspose(const Vector<Real>& B1, Long p, Vector<Real>& S, SHCArrange arrange);
  58. /**
  59. * \brief Computes all the Associated Legendre Polynomials (normalized) up to the specified degree.
  60. * \param[in] degree The degree up to which the Legendre polynomials have to be computed.
  61. * \param[in] X The input values for which the polynomials have to be computed.
  62. * \param[in] N The number of input points.
  63. * \param[out] poly_val The output array of size (degree+1)*(degree+2)*N/2 containing the computed polynomial values.
  64. * The output values are in the order:
  65. * P(n,m)[i] => {P(0,0)[0], P(0,0)[1], ..., P(0,0)[N-1], P(1,0)[0], ..., P(1,0)[N-1],
  66. * P(2,0)[0], ..., P(degree,0)[N-1], P(1,1)[0], ...,P(2,1)[0], ..., P(degree,degree)[N-1]}
  67. */
  68. static void LegPoly(Vector<Real>& poly_val, const Vector<Real>& X, Long degree);
  69. static void LegPolyDeriv(Vector<Real>& poly_val, const Vector<Real>& X, Long degree);
  70. static const Vector<Real>& LegendreNodes(Long p1);
  71. static const Vector<Real>& LegendreWeights(Long p1);
  72. static const Vector<Real>& SingularWeights(Long p1);
  73. static const Matrix<Real>& MatFourier(Long p0, Long p1);
  74. static const Matrix<Real>& MatFourierInv(Long p0, Long p1);
  75. static const Matrix<Real>& MatFourierGrad(Long p0, Long p1);
  76. static const FFT<Real>& OpFourier(Long Np);
  77. static const FFT<Real>& OpFourierInv(Long Np);
  78. static const std::vector<Matrix<Real>>& MatLegendre(Long p0, Long p1);
  79. static const std::vector<Matrix<Real>>& MatLegendreInv(Long p0, Long p1);
  80. static const std::vector<Matrix<Real>>& MatLegendreGrad(Long p0, Long p1);
  81. static const std::vector<Matrix<Real>>& MatRotate(Long p0);
  82. template <bool SLayer, bool DLayer> static void StokesSingularInteg_(const Vector<Real>& X0, Long p0, Long p1, Vector<Real>& SL, Vector<Real>& DL);
  83. struct MatrixStorage{
  84. MatrixStorage(){
  85. const Long size = SCTL_SHMAXDEG;
  86. Resize(size);
  87. }
  88. void Resize(Long size){
  89. Qx_ .resize(size);
  90. Qw_ .resize(size);
  91. Sw_ .resize(size);
  92. Mf_ .resize(size*size);
  93. Mdf_.resize(size*size);
  94. Ml_ .resize(size*size);
  95. Mdl_.resize(size*size);
  96. Mr_ .resize(size);
  97. Mfinv_ .resize(size*size);
  98. Mlinv_ .resize(size*size);
  99. Mfft_.resize(size);
  100. Mfftinv_.resize(size);
  101. }
  102. std::vector<Vector<Real>> Qx_;
  103. std::vector<Vector<Real>> Qw_;
  104. std::vector<Vector<Real>> Sw_;
  105. std::vector<Matrix<Real>> Mf_ ;
  106. std::vector<Matrix<Real>> Mdf_;
  107. std::vector<std::vector<Matrix<Real>>> Ml_ ;
  108. std::vector<std::vector<Matrix<Real>>> Mdl_;
  109. std::vector<std::vector<Matrix<Real>>> Mr_;
  110. std::vector<Matrix<Real>> Mfinv_ ;
  111. std::vector<std::vector<Matrix<Real>>> Mlinv_ ;
  112. std::vector<FFT<Real>> Mfft_;
  113. std::vector<FFT<Real>> Mfftinv_;
  114. };
  115. static MatrixStorage& MatrixStore(){
  116. static MatrixStorage storage;
  117. return storage;
  118. }
  119. };
  120. template class SphericalHarmonics<double>;
  121. } // end namespace
  122. #include SCTL_INCLUDE(sph_harm.txx)
  123. #endif // _SCTL_SPH_HARM_HPP_