fmm_cheb.txx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /**
  2. * \file fmm_cheb.txx
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 3-07-2011
  5. * \brief This file contains the implementation of the FMM_Cheb class.
  6. */
  7. #include <mpi.h>
  8. #include <fftw3.h>
  9. #include <parUtils.h>
  10. #ifdef PVFMM_HAVE_SYS_STAT_H
  11. #include <sys/stat.h>
  12. #endif
  13. namespace pvfmm{
  14. template <class FMMNode>
  15. FMM_Cheb<FMMNode>::~FMM_Cheb() {
  16. if(this->mat!=NULL){
  17. int rank;
  18. MPI_Comm_rank(this->comm,&rank);
  19. if(!rank){
  20. FILE* f=fopen(this->mat_fname.c_str(),"r");
  21. if(f==NULL) { //File does not exists.
  22. { // Delete easy to compute matrices.
  23. Mat_Type type=W_Type;
  24. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  25. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  26. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  27. M.Resize(0,0);
  28. }
  29. type=V_Type;
  30. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  31. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  32. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  33. M.Resize(0,0);
  34. }
  35. type=V1_Type;
  36. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  37. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  38. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  39. M.Resize(0,0);
  40. }
  41. type=U2U_Type;
  42. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  43. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  44. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  45. M.Resize(0,0);
  46. }
  47. type=D2D_Type;
  48. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  49. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  50. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  51. M.Resize(0,0);
  52. }
  53. type=D2T_Type;
  54. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  55. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  56. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  57. M.Resize(0,0);
  58. }
  59. }
  60. this->mat->Save2File(this->mat_fname.c_str());
  61. }else fclose(f);
  62. }
  63. }
  64. }
  65. template <class FMMNode>
  66. void FMM_Cheb<FMMNode>::Initialize(int mult_order, int cheb_deg_, const MPI_Comm& comm_, const Kernel<Real_t>* kernel_, const Kernel<Real_t>* aux_kernel_){
  67. Profile::Tic("InitFMM_Cheb",&comm_,true);{
  68. int rank;
  69. MPI_Comm_rank(comm_,&rank);
  70. int dim=3; //Only supporting 3D
  71. cheb_deg=cheb_deg_;
  72. if(this->mat_fname.size()==0){
  73. std::stringstream st;
  74. st<<PVFMM_PRECOMP_DATA_PATH;
  75. if(!st.str().size()){ // look in PVFMM_DIR
  76. char* pvfmm_dir = getenv ("PVFMM_DIR");
  77. if(pvfmm_dir) st<<pvfmm_dir;
  78. }
  79. #ifndef STAT_MACROS_BROKEN
  80. if(st.str().size()){ // check if the path is a directory
  81. struct stat stat_buff;
  82. if(stat(st.str().c_str(), &stat_buff) || !S_ISDIR(stat_buff.st_mode)){
  83. std::cout<<"error: path not found: "<<st.str()<<'\n';
  84. exit(0);
  85. }
  86. }
  87. #endif
  88. if(st.str().size()) st<<'/';
  89. st<<"Precomp_"<<kernel_->ker_name.c_str()<<"_c"<<cheb_deg<<"_m"<<mult_order<<(typeid(Real_t)==typeid(float)?"_f":"")<<".data";
  90. this->mat_fname=st.str();
  91. }
  92. if(!rank){
  93. FILE* f=fopen(this->mat_fname.c_str(),"r");
  94. if(f==NULL) { //File does not exists.
  95. std::cout<<"Could not find precomputed data file for "<<kernel_->ker_name.c_str()<<" kernel with q="<<cheb_deg<<" and m="<<mult_order<<".\n";
  96. std::cout<<"This data will be computed and stored for future use at:\n"<<this->mat_fname<<'\n';
  97. std::cout<<"This may take a while...\n";
  98. }else fclose(f);
  99. }
  100. //this->mat->LoadFile(this->mat_fname.c_str(), this->comm);
  101. FMM_Pts<FMMNode>::Initialize(mult_order, comm_, kernel_, aux_kernel_);
  102. this->mat->RelativeTrgCoord()=cheb_nodes<Real_t>(ChebDeg(),dim);
  103. Profile::Tic("PrecompD2T",&this->comm,false,4);
  104. this->PrecompAll(D2T_Type);
  105. Profile::Toc();
  106. //Volume solver.
  107. Profile::Tic("PrecompS2M",&this->comm,false,4);
  108. this->PrecompAll(S2U_Type);
  109. Profile::Toc();
  110. Profile::Tic("PrecompX",&this->comm,false,4);
  111. this->PrecompAll(X_Type);
  112. Profile::Toc();
  113. Profile::Tic("PrecompW",&this->comm,false,4);
  114. this->PrecompAll(W_Type);
  115. Profile::Toc();
  116. Profile::Tic("PrecompU0",&this->comm,false,4);
  117. this->PrecompAll(U0_Type);
  118. Profile::Toc();
  119. Profile::Tic("PrecompU1",&this->comm,false,4);
  120. this->PrecompAll(U1_Type);
  121. Profile::Toc();
  122. Profile::Tic("PrecompU2",&this->comm,false,4);
  123. this->PrecompAll(U2_Type);
  124. Profile::Toc();
  125. Profile::Tic("Save2File",&this->comm,false,4);
  126. if(!rank){
  127. FILE* f=fopen(this->mat_fname.c_str(),"r");
  128. if(f==NULL) { //File does not exists.
  129. { // Delete easy to compute matrices.
  130. Mat_Type type=W_Type;
  131. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  132. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  133. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  134. M.Resize(0,0);
  135. }
  136. type=V_Type;
  137. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  138. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  139. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  140. M.Resize(0,0);
  141. }
  142. type=V1_Type;
  143. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  144. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  145. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  146. M.Resize(0,0);
  147. }
  148. type=U2U_Type;
  149. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  150. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  151. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  152. M.Resize(0,0);
  153. }
  154. type=D2D_Type;
  155. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  156. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  157. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  158. M.Resize(0,0);
  159. }
  160. type=D2T_Type;
  161. for(int l=-BC_LEVELS;l<MAX_DEPTH;l++)
  162. for(size_t indx=0;indx<this->interac_list.ListCount(type);indx++){
  163. Matrix<Real_t>& M=this->mat->Mat(l, type, indx);
  164. M.Resize(0,0);
  165. }
  166. }
  167. this->mat->Save2File(this->mat_fname.c_str());
  168. }else fclose(f);
  169. }
  170. Profile::Toc();
  171. Profile::Tic("Recompute",&this->comm,false,4);
  172. { // Recompute matrices.
  173. this->PrecompAll(W_Type);
  174. this->PrecompAll(V_Type);
  175. this->PrecompAll(V1_Type);
  176. this->PrecompAll(U2U_Type);
  177. this->PrecompAll(D2D_Type);
  178. this->PrecompAll(D2T_Type);
  179. }
  180. Profile::Toc();
  181. }Profile::Toc();
  182. }
  183. template <class FMMNode>
  184. Permutation<typename FMMNode::Real_t>& FMM_Cheb<FMMNode>::PrecompPerm(Mat_Type type, Perm_Type perm_indx){
  185. int dim=3; //Only supporting 3D
  186. //Check if the matrix already exists.
  187. Permutation<Real_t>& P_ = FMM_Pts<FMMNode>::PrecompPerm(type, perm_indx);
  188. if(P_.Dim()!=0) return P_;
  189. Matrix<size_t> swap_xy(10,9);
  190. Matrix<size_t> swap_xz(10,9);
  191. { // This is repeated from FMM_Pts::PrecompPerm, but I dont see any other way.
  192. for(int i=0;i<9;i++)
  193. for(int j=0;j<9;j++){
  194. swap_xy[i][j]=j;
  195. swap_xz[i][j]=j;
  196. }
  197. swap_xy[3][0]=1; swap_xy[3][1]=0; swap_xy[3][2]=2;
  198. swap_xz[3][0]=2; swap_xz[3][1]=1; swap_xz[3][2]=0;
  199. swap_xy[6][0]=1; swap_xy[6][1]=0; swap_xy[6][2]=2;
  200. swap_xy[6][3]=4; swap_xy[6][4]=3; swap_xy[6][5]=5;
  201. swap_xz[6][0]=2; swap_xz[6][1]=1; swap_xz[6][2]=0;
  202. swap_xz[6][3]=5; swap_xz[6][4]=4; swap_xz[6][5]=3;
  203. swap_xy[9][0]=4; swap_xy[9][1]=3; swap_xy[9][2]=5;
  204. swap_xy[9][3]=1; swap_xy[9][4]=0; swap_xy[9][5]=2;
  205. swap_xy[9][6]=7; swap_xy[9][7]=6; swap_xy[9][8]=8;
  206. swap_xz[9][0]=8; swap_xz[9][1]=7; swap_xz[9][2]=6;
  207. swap_xz[9][3]=5; swap_xz[9][4]=4; swap_xz[9][5]=3;
  208. swap_xz[9][6]=2; swap_xz[9][7]=1; swap_xz[9][8]=0;
  209. }
  210. //Compute the matrix.
  211. Permutation<Real_t> P;
  212. switch (type){
  213. case UC2UE_Type:
  214. {
  215. break;
  216. }
  217. case DC2DE_Type:
  218. {
  219. break;
  220. }
  221. case S2U_Type:
  222. {
  223. break;
  224. }
  225. case U2U_Type:
  226. {
  227. break;
  228. }
  229. case D2D_Type:
  230. {
  231. break;
  232. }
  233. case D2T_Type:
  234. {
  235. break;
  236. }
  237. case U0_Type:
  238. {
  239. int coeff_cnt=((cheb_deg+1)*(cheb_deg+2)*(cheb_deg+3))/6;
  240. int n3=(int)pow((Real_t)(cheb_deg+1),dim);
  241. int dof=(perm_indx<C_Perm?this->kernel.ker_dim[0]:this->kernel.ker_dim[1]);
  242. size_t p_indx=perm_indx % C_Perm;
  243. Permutation<Real_t> P0(n3*dof);
  244. if(dof%3==0 && this->kernel.ker_name.compare("biot_savart")==0) //biot_savart
  245. for(int j=0;j<dof;j++)
  246. for(int i=0;i<n3;i++)
  247. P0.scal[i+n3*j]*=(perm_indx<C_Perm?1:-1);
  248. if(p_indx==ReflecX || p_indx==ReflecY || p_indx==ReflecZ){
  249. for(int j=0;j<dof;j++)
  250. for(int i=0;i<n3;i++){
  251. int x[3]={i%(cheb_deg+1), (i/(cheb_deg+1))%(cheb_deg+1), i/(cheb_deg+1)/(cheb_deg+1)};
  252. P0.scal[i+n3*j]*=(x[p_indx]%2?-1.0:1.0);
  253. if(dof%3==0) //stokes_vel (and like kernels)
  254. P0.scal[i+n3*j]*=( j %3==p_indx?-1.0:1.0);
  255. if(dof%3==0 && (dof/3)%3==0)
  256. P0.scal[i+n3*j]*=((j/3)%3==p_indx?-1.0:1.0);
  257. }
  258. }else if(p_indx==SwapXY || p_indx==SwapXZ){
  259. int indx[3];
  260. if(p_indx==SwapXY) {indx[0]=1; indx[1]=0; indx[2]=2;}
  261. if(p_indx==SwapXZ) {indx[0]=2; indx[1]=1; indx[2]=0;}
  262. for(int j=0;j<dof;j++)
  263. for(int i=0;i<n3;i++){
  264. int x[3]={i%(cheb_deg+1), (i/(cheb_deg+1))%(cheb_deg+1), i/(cheb_deg+1)/(cheb_deg+1)};
  265. P0.perm[i+n3*j]=x[indx[0]]+(x[indx[1]]+x[indx[2]]*(cheb_deg+1))*(cheb_deg+1)
  266. +n3*(p_indx==SwapXY?swap_xy[dof][j]:swap_xz[dof][j]);
  267. }
  268. }
  269. std::vector<size_t> coeff_map(n3*dof,0);
  270. {
  271. int indx=0;
  272. for(int j=0;j<dof;j++)
  273. for(int i=0;i<n3;i++){
  274. int x[3]={i%(cheb_deg+1), (i/(cheb_deg+1))%(cheb_deg+1), i/(cheb_deg+1)/(cheb_deg+1)};
  275. if(x[0]+x[1]+x[2]<=cheb_deg){
  276. coeff_map[i+n3*j]=indx;
  277. indx++;
  278. }
  279. }
  280. }
  281. P=Permutation<Real_t>(coeff_cnt*dof);
  282. {
  283. int indx=0;
  284. for(int j=0;j<dof;j++)
  285. for(int i=0;i<n3;i++){
  286. int x[3]={i%(cheb_deg+1), (i/(cheb_deg+1))%(cheb_deg+1), i/(cheb_deg+1)/(cheb_deg+1)};
  287. if(x[0]+x[1]+x[2]<=cheb_deg){
  288. P.perm[indx]=coeff_map[P0.perm[i+n3*j]];
  289. P.scal[indx]= P0.scal[i+n3*j] ;
  290. indx++;
  291. }
  292. }
  293. }
  294. break;
  295. }
  296. case U1_Type:
  297. {
  298. P=PrecompPerm(U0_Type, perm_indx);
  299. break;
  300. }
  301. case U2_Type:
  302. {
  303. P=PrecompPerm(U0_Type, perm_indx);
  304. break;
  305. }
  306. case V_Type:
  307. {
  308. break;
  309. }
  310. case V1_Type:
  311. {
  312. break;
  313. }
  314. case W_Type:
  315. {
  316. if(perm_indx>=C_Perm) P=PrecompPerm(U0_Type, perm_indx);
  317. else P=PrecompPerm(D2D_Type, perm_indx);
  318. break;
  319. }
  320. case X_Type:
  321. {
  322. if(perm_indx<C_Perm) P=PrecompPerm(U0_Type, perm_indx);
  323. else P=PrecompPerm(D2D_Type, perm_indx);
  324. break;
  325. }
  326. default:
  327. return FMM_Pts<FMMNode>::PrecompPerm(type, perm_indx);
  328. break;
  329. }
  330. //Save the matrix for future use.
  331. #pragma omp critical (PRECOMP_MATRIX_PTS)
  332. if(P_.Dim()==0){ P_=P;}
  333. return P_;
  334. }
  335. template <class FMMNode>
  336. Matrix<typename FMMNode::Real_t>& FMM_Cheb<FMMNode>::Precomp(int level, Mat_Type type, size_t mat_indx){
  337. if(this->Homogen()) level=0;
  338. //Check if the matrix already exists.
  339. //Compute matrix from symmetry class (if possible).
  340. Matrix<Real_t>& M_ = this->mat->Mat(level, type, mat_indx);
  341. if(M_.Dim(0)!=0 && M_.Dim(1)!=0) return M_;
  342. else{ //Compute matrix from symmetry class (if possible).
  343. size_t class_indx = this->interac_list.InteracClass(type, mat_indx);
  344. if(class_indx!=mat_indx){
  345. Matrix<Real_t>& M0 = this->Precomp(level, type, class_indx);
  346. Permutation<Real_t>& Pr = this->interac_list.Perm_R(level, type, mat_indx);
  347. Permutation<Real_t>& Pc = this->interac_list.Perm_C(level, type, mat_indx);
  348. if(Pr.Dim()>0 && Pc.Dim()>0 && M0.Dim(0)>0 && M0.Dim(1)>0) return M_;
  349. }
  350. }
  351. int myrank, np;
  352. MPI_Comm_rank(this->comm, &myrank);
  353. MPI_Comm_size(this->comm,&np);
  354. //Compute the matrix.
  355. Matrix<Real_t> M;
  356. int n_src=((cheb_deg+1)*(cheb_deg+2)*(cheb_deg+3))/6;
  357. switch (type){
  358. case S2U_Type:
  359. {
  360. Real_t r=pow(0.5,level);
  361. Real_t c[3]={0,0,0};
  362. // Coord of upward check surface
  363. std::vector<Real_t> uc_coord=u_check_surf(this->MultipoleOrder(),c,level);
  364. size_t n_uc=uc_coord.size()/3;
  365. // Evaluate potential at check surface.
  366. Matrix<Real_t> M_s2c(n_src*this->aux_kernel.ker_dim[0],n_uc*this->aux_kernel.ker_dim[1]); //source 2 check
  367. Matrix<Real_t> M_s2c_local(n_src*this->aux_kernel.ker_dim[0],n_uc*this->aux_kernel.ker_dim[1]);
  368. {
  369. M_s2c.SetZero();
  370. M_s2c_local.SetZero();
  371. #pragma omp parallel for
  372. for(size_t i=((myrank+0)*n_uc)/np;i<((myrank+1)*n_uc)/np;i++){
  373. std::vector<Real_t> M_=cheb_integ(cheb_deg, &uc_coord[i*3], r,
  374. this->aux_kernel.ker_poten, this->aux_kernel.ker_dim);
  375. for(int k=0; k<this->aux_kernel.ker_dim[1]; k++)
  376. for(size_t j=0; j<(size_t)M_s2c.Dim(0); j++)
  377. M_s2c_local[j][i*this->aux_kernel.ker_dim[1]+k] = M_[j+k*M_s2c.Dim(0)];
  378. }
  379. MPI_Allreduce(M_s2c_local[0], M_s2c[0], M_s2c.Dim(0)*M_s2c.Dim(1), par::Mpi_datatype<Real_t>::value(), MPI_SUM, this->comm);
  380. }
  381. Matrix<Real_t>& M_c2e = this->Precomp(level, UC2UE_Type, 0);
  382. M=M_s2c*M_c2e;
  383. break;
  384. }
  385. case D2T_Type:
  386. {
  387. Matrix<Real_t>& M_s2t=FMM_Pts<FMMNode>::Precomp(level, type, mat_indx);
  388. int n_trg=M_s2t.Dim(1)/this->kernel.ker_dim[1];
  389. // Compute Chebyshev approx from target potential.
  390. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  391. #pragma omp parallel for
  392. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  393. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  394. M_trg=M_trg.Transpose();
  395. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  396. }
  397. #pragma omp critical (PRECOMP_MATRIX_PTS)
  398. {
  399. M_s2t.Resize(0,0);
  400. }
  401. break;
  402. }
  403. case U0_Type:
  404. {
  405. // Coord of target points
  406. Real_t s=pow(0.5,level);
  407. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  408. Real_t coord_diff[3]={(coord[0]-1)*s*0.5,(coord[1]-1)*s*0.5,(coord[2]-1)*s*0.5};
  409. std::vector<Real_t>& rel_trg_coord = this->mat->RelativeTrgCoord();
  410. size_t n_trg = rel_trg_coord.size()/3;
  411. std::vector<Real_t> trg_coord(n_trg*3);
  412. for(size_t j=0;j<n_trg;j++){
  413. trg_coord[j*3+0]=rel_trg_coord[j*3+0]*s-coord_diff[0];
  414. trg_coord[j*3+1]=rel_trg_coord[j*3+1]*s-coord_diff[1];
  415. trg_coord[j*3+2]=rel_trg_coord[j*3+2]*s-coord_diff[2];
  416. }
  417. // Evaluate potential at target points.
  418. Matrix<Real_t> M_s2t(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  419. Matrix<Real_t> M_s2t_local(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  420. {
  421. M_s2t.SetZero();
  422. M_s2t_local.SetZero();
  423. #pragma omp parallel for
  424. for(size_t i=((myrank+0)*n_trg)/np;i<((myrank+1)*n_trg)/np;i++){
  425. std::vector<Real_t> s2t=cheb_integ(cheb_deg, &trg_coord[i*3], (Real_t)(s*2.0),
  426. this->kernel.ker_poten, this->kernel.ker_dim);
  427. for(int k=0; k<this->kernel.ker_dim[1]; k++)
  428. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++)
  429. M_s2t_local[j][i*this->kernel.ker_dim[1]+k] = s2t[j+k*M_s2t.Dim(0)];
  430. }
  431. MPI_Allreduce(M_s2t_local[0], M_s2t[0], M_s2t.Dim(0)*M_s2t.Dim(1), par::Mpi_datatype<Real_t>::value(), MPI_SUM, this->comm);
  432. }
  433. // Compute Chebyshev approx from target potential.
  434. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  435. #pragma omp parallel for
  436. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  437. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  438. M_trg=M_trg.Transpose();
  439. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  440. }
  441. break;
  442. }
  443. case U1_Type:
  444. {
  445. // Coord of target points
  446. Real_t s=pow(0.5,level);
  447. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  448. Real_t coord_diff[3]={coord[0]*s,coord[1]*s,coord[2]*s};
  449. std::vector<Real_t>& rel_trg_coord = this->mat->RelativeTrgCoord();
  450. size_t n_trg = rel_trg_coord.size()/3;
  451. std::vector<Real_t> trg_coord(n_trg*3);
  452. for(size_t j=0;j<n_trg;j++){
  453. trg_coord[j*3+0]=rel_trg_coord[j*3+0]*s-coord_diff[0];
  454. trg_coord[j*3+1]=rel_trg_coord[j*3+1]*s-coord_diff[1];
  455. trg_coord[j*3+2]=rel_trg_coord[j*3+2]*s-coord_diff[2];
  456. }
  457. // Evaluate potential at target points.
  458. Matrix<Real_t> M_s2t(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  459. Matrix<Real_t> M_s2t_local(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  460. {
  461. M_s2t.SetZero();
  462. M_s2t_local.SetZero();
  463. #pragma omp parallel for
  464. for(size_t i=((myrank+0)*n_trg)/np;i<((myrank+1)*n_trg)/np;i++){
  465. std::vector<Real_t> s2t=cheb_integ(cheb_deg, &trg_coord[i*3], s,
  466. this->kernel.ker_poten, this->kernel.ker_dim);
  467. for(int k=0; k<this->kernel.ker_dim[1]; k++)
  468. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++)
  469. M_s2t_local[j][i*this->kernel.ker_dim[1]+k] = s2t[j+k*M_s2t.Dim(0)];
  470. }
  471. MPI_Allreduce(M_s2t_local[0], M_s2t[0], M_s2t.Dim(0)*M_s2t.Dim(1), par::Mpi_datatype<Real_t>::value(), MPI_SUM, this->comm);
  472. }
  473. // Compute Chebyshev approx from target potential.
  474. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  475. #pragma omp parallel for
  476. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  477. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  478. M_trg=M_trg.Transpose();
  479. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  480. }
  481. break;
  482. }
  483. case U2_Type:
  484. {
  485. // Coord of target points
  486. Real_t s=pow(0.5,level);
  487. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  488. Real_t coord_diff[3]={(coord[0]+1)*s*0.25,(coord[1]+1)*s*0.25,(coord[2]+1)*s*0.25};
  489. std::vector<Real_t>& rel_trg_coord = this->mat->RelativeTrgCoord();
  490. size_t n_trg = rel_trg_coord.size()/3;
  491. std::vector<Real_t> trg_coord(n_trg*3);
  492. for(size_t j=0;j<n_trg;j++){
  493. trg_coord[j*3+0]=rel_trg_coord[j*3+0]*s-coord_diff[0];
  494. trg_coord[j*3+1]=rel_trg_coord[j*3+1]*s-coord_diff[1];
  495. trg_coord[j*3+2]=rel_trg_coord[j*3+2]*s-coord_diff[2];
  496. }
  497. // Evaluate potential at target points.
  498. Matrix<Real_t> M_s2t(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  499. Matrix<Real_t> M_s2t_local(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  500. {
  501. M_s2t.SetZero();
  502. M_s2t_local.SetZero();
  503. #pragma omp parallel for
  504. for(size_t i=((myrank+0)*n_trg)/np;i<((myrank+1)*n_trg)/np;i++){
  505. std::vector<Real_t> s2t=cheb_integ(cheb_deg, &trg_coord[i*3], (Real_t)(s*0.5),
  506. this->kernel.ker_poten, this->kernel.ker_dim);
  507. for(int k=0; k<this->kernel.ker_dim[1]; k++)
  508. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++)
  509. M_s2t_local[j][i*this->kernel.ker_dim[1]+k] = s2t[j+k*M_s2t.Dim(0)];
  510. }
  511. MPI_Allreduce(M_s2t_local[0], M_s2t[0], M_s2t.Dim(0)*M_s2t.Dim(1), par::Mpi_datatype<Real_t>::value(), MPI_SUM, this->comm);
  512. }
  513. // Compute Chebyshev approx from target potential.
  514. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  515. #pragma omp parallel for
  516. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  517. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  518. M_trg=M_trg.Transpose();
  519. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  520. }
  521. break;
  522. }
  523. case W_Type:
  524. {
  525. Matrix<Real_t>& M_s2t=FMM_Pts<FMMNode>::Precomp(level, type, mat_indx);
  526. int n_trg=M_s2t.Dim(1)/this->kernel.ker_dim[1];
  527. // Compute Chebyshev approx from target potential.
  528. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  529. #pragma omp parallel for
  530. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  531. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  532. M_trg=M_trg.Transpose();
  533. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  534. }
  535. #pragma omp critical (PRECOMP_MATRIX_PTS)
  536. {
  537. M_s2t.Resize(0,0);
  538. }
  539. break;
  540. }
  541. case X_Type:
  542. {
  543. // Coord of target points
  544. Real_t s=pow(0.5,level-1);
  545. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  546. Real_t c[3]={-(coord[0]-1)*s*0.25,-(coord[1]-1)*s*0.25,-(coord[2]-1)*s*0.25};
  547. std::vector<Real_t> trg_coord=d_check_surf(this->MultipoleOrder(),c,level);
  548. size_t n_trg=trg_coord.size()/3;
  549. // Evaluate potential at target points.
  550. Matrix<Real_t> M_xs2c(n_src*this->aux_kernel.ker_dim[0], n_trg*this->aux_kernel.ker_dim[1]);
  551. Matrix<Real_t> M_xs2c_local(n_src*this->aux_kernel.ker_dim[0], n_trg*this->aux_kernel.ker_dim[1]);
  552. {
  553. M_xs2c.SetZero();
  554. M_xs2c_local.SetZero();
  555. #pragma omp parallel for
  556. for(size_t i=((myrank+0)*n_trg)/np;i<((myrank+1)*n_trg)/np;i++){
  557. std::vector<Real_t> M_=cheb_integ(cheb_deg, &trg_coord[i*3], s,
  558. this->aux_kernel.ker_poten, this->aux_kernel.ker_dim);
  559. for(int k=0; k<this->aux_kernel.ker_dim[1]; k++)
  560. for(size_t j=0; j<(size_t)M_xs2c.Dim(0); j++)
  561. M_xs2c_local[j][i*this->aux_kernel.ker_dim[1]+k] = M_[j+k*M_xs2c.Dim(0)];
  562. }
  563. MPI_Allreduce(M_xs2c_local[0], M_xs2c[0], M_xs2c.Dim(0)*M_xs2c.Dim(1), par::Mpi_datatype<Real_t>::value(), MPI_SUM, this->comm);
  564. }
  565. Matrix<Real_t>& M_c2e = this->Precomp(level, DC2DE_Type, 0);
  566. M=M_xs2c*M_c2e;
  567. break;
  568. }
  569. default:
  570. {
  571. return FMM_Pts<FMMNode>::Precomp(level, type, mat_indx);
  572. break;
  573. }
  574. }
  575. //Save the matrix for future use.
  576. #pragma omp critical (PRECOMP_MATRIX_PTS)
  577. if(M_.Dim(0)==0 && M_.Dim(1)==0){ M_=M;}
  578. return M_;
  579. }
  580. template <class FMMNode>
  581. void FMM_Cheb<FMMNode>::CollectNodeData(std::vector<FMMNode*>& node, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, std::vector<size_t> extra_size){
  582. if( buff.size()<6) buff.resize(6);
  583. if( n_list.size()<6) n_list.resize(6);
  584. if(extra_size.size()<6) extra_size.resize(6,0);
  585. size_t n_coeff=(cheb_deg+1)*(cheb_deg+2)*(cheb_deg+3)/6;
  586. if(node.size()==0) return;
  587. {// 4. cheb_in
  588. int indx=4;
  589. int dof=this->kernel.ker_dim[0];
  590. size_t vec_sz=dof*n_coeff;
  591. std::vector< FMMNode* > node_lst;
  592. for(size_t i=0;i<node.size();i++)
  593. if(node[i]->IsLeaf())
  594. node_lst.push_back(node[i]);
  595. n_list[indx]=node_lst;
  596. extra_size[indx]+=node_lst.size()*vec_sz;
  597. #pragma omp parallel for
  598. for(size_t i=0;i<node_lst.size();i++){ // Move data before resizing buff[indx]
  599. //FMMData* fmm_data=((FMMData*)node_lst[i]->FMMData());
  600. //Vector<Real_t>& cheb_in =fmm_data->cheb_in ;
  601. Vector<Real_t>& cheb_in =node_lst[i]->ChebData();
  602. Vector<Real_t> new_buff=cheb_in;
  603. cheb_in.Swap(new_buff);
  604. }
  605. }
  606. {// 5. cheb_out
  607. int indx=5;
  608. int dof=this->kernel.ker_dim[1];
  609. size_t vec_sz=dof*n_coeff;
  610. std::vector< FMMNode* > node_lst;
  611. for(size_t i=0;i<node.size();i++)
  612. if(node[i]->IsLeaf() && !node[i]->IsGhost())
  613. node_lst.push_back(node[i]);
  614. n_list[indx]=node_lst;
  615. extra_size[indx]+=node_lst.size()*vec_sz;
  616. }
  617. FMM_Pts<FMMNode>::CollectNodeData(node, buff, n_list, extra_size);
  618. {// 4. cheb_in
  619. int indx=4;
  620. int dof=this->kernel.ker_dim[0];
  621. size_t vec_sz=dof*n_coeff;
  622. Vector< FMMNode* >& node_lst=n_list[indx];
  623. Real_t* buff_ptr=buff[indx][0]+buff[indx].Dim(0)*buff[indx].Dim(1)-extra_size[indx];
  624. #pragma omp parallel for
  625. for(size_t i=0;i<node_lst.Dim();i++){
  626. //FMMData* fmm_data=((FMMData*)node_lst[i]->FMMData());
  627. //Vector<Real_t>& cheb_in =fmm_data->cheb_in ;
  628. Vector<Real_t>& cheb_in =node_lst[i]->ChebData();
  629. mem::memcopy(buff_ptr+i*vec_sz, &cheb_in [0], cheb_in .Dim()*sizeof(Real_t));
  630. cheb_in .ReInit(vec_sz, buff_ptr+i*vec_sz, false);
  631. //if(node_lst[i]->IsGhost()) cheb_in .SetZero();
  632. }
  633. buff[indx].AllocDevice(true);
  634. }
  635. {// 5. cheb_out
  636. int indx=5;
  637. int dof=this->kernel.ker_dim[1];
  638. size_t vec_sz=dof*n_coeff;
  639. Vector< FMMNode* >& node_lst=n_list[indx];
  640. Real_t* buff_ptr=buff[indx][0]+buff[indx].Dim(0)*buff[indx].Dim(1)-extra_size[indx];
  641. #pragma omp parallel for
  642. for(size_t i=0;i<node_lst.Dim();i++){
  643. Vector<Real_t>& cheb_out=((FMMData*)node_lst[i]->FMMData())->cheb_out;
  644. mem::memcopy(buff_ptr+i*vec_sz, &cheb_out[0], cheb_out.Dim()*sizeof(Real_t));
  645. cheb_out.ReInit(vec_sz, buff_ptr+i*vec_sz, false);
  646. cheb_out.SetZero();
  647. }
  648. buff[indx].AllocDevice(true);
  649. }
  650. }
  651. template <class FMMNode>
  652. void FMM_Cheb<FMMNode>::InitMultipole(FMMNode** node, size_t n, int level){
  653. if(n==0) return;
  654. FMM_Pts<FMMNode>::InitMultipole(node,n,level);
  655. int dim=3; //Only supporting 3D
  656. int dof=1;
  657. #pragma omp parallel
  658. {
  659. int omp_p=omp_get_num_threads();
  660. int pid = omp_get_thread_num();
  661. size_t a=(pid*n)/omp_p;
  662. size_t b=((pid+1)*n)/omp_p;
  663. size_t cnt;
  664. Matrix<Real_t>& M = this->mat->Mat(level, S2U_Type, 0);
  665. if(M.Dim(0)!=0 && M.Dim(1)!=0){
  666. cnt=0;
  667. for(size_t i=a;i<b;i++)
  668. if(node[i]->ChebData().Dim()>0) cnt++;
  669. Matrix<Real_t> src_vec(cnt*dof,M.Dim(0));
  670. Matrix<Real_t> trg_vec(cnt*dof,M.Dim(1));
  671. cnt=0;
  672. for(size_t i=a;i<b;i++)
  673. if(node[i]->ChebData().Dim()>0){
  674. Vector<Real_t>& cheb_in=node[i]->ChebData();
  675. assert(cheb_in.Dim()==dof*M.Dim(0));
  676. mem::memcopy(&(src_vec[cnt*dof][0]), &(cheb_in[0]), dof*M.Dim(0)*sizeof(Real_t));
  677. cnt++;
  678. }
  679. Matrix<Real_t>::DGEMM(trg_vec,src_vec,M);
  680. cnt=0;
  681. size_t vec_len=M.Dim(1)*dof;
  682. for(size_t i=a;i<b;i++)
  683. if(node[i]->ChebData().Dim()>0){
  684. Real_t scale_factor=(this->Homogen()?pow(0.5,dim*node[i]->Depth()):1.0);
  685. Real_t* trg_vec_=trg_vec[cnt*dof];
  686. Vector<Real_t>& upward_equiv=((FMMData*)node[i]->FMMData())->upward_equiv;
  687. assert(upward_equiv.Dim()==vec_len);
  688. for(size_t j=0;j<vec_len;j++)
  689. upward_equiv[j]+=trg_vec_[j]*scale_factor;
  690. cnt++;
  691. }
  692. Profile::Add_FLOP(2*cnt*vec_len);
  693. }
  694. }
  695. }
  696. template <class FMMNode>
  697. void FMM_Cheb<FMMNode>::X_ListSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device){
  698. FMM_Pts<FMMNode>::X_ListSetup(setup_data, buff, n_list, level, device);
  699. { // Set setup_data
  700. setup_data.level=level;
  701. setup_data.kernel=&this->aux_kernel;
  702. setup_data.interac_type.resize(1);
  703. setup_data.interac_type[0]=X_Type;
  704. setup_data. input_data=&buff[4];
  705. setup_data.output_data=&buff[1];
  706. Vector<FMMNode_t*>& nodes_in =n_list[4];
  707. Vector<FMMNode_t*>& nodes_out=n_list[1];
  708. setup_data.nodes_in .clear();
  709. setup_data.nodes_out.clear();
  710. for(size_t i=0;i<nodes_in .Dim();i++) if(nodes_in [i]->Depth()==level-1 || level==-1) setup_data.nodes_in .push_back(nodes_in [i]);
  711. for(size_t i=0;i<nodes_out.Dim();i++) if(nodes_out[i]->Depth()==level || level==-1) setup_data.nodes_out.push_back(nodes_out[i]);
  712. }
  713. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  714. std::vector<void*>& nodes_out=setup_data.nodes_out;
  715. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  716. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  717. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(& ((FMMNode*)nodes_in [i])->ChebData());
  718. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->dnward_equiv);
  719. this->SetupInterac(setup_data,device);
  720. { // Resize device buffer
  721. size_t n=setup_data.output_data->Dim(0)*setup_data.output_data->Dim(1)*sizeof(Real_t);
  722. if(this->dev_buffer.Dim()<n) this->dev_buffer.Resize(n);
  723. }
  724. }
  725. template <class FMMNode>
  726. void FMM_Cheb<FMMNode>::X_List (SetupData<Real_t>& setup_data, bool device){
  727. //Add X_List contribution.
  728. FMM_Pts<FMMNode>::X_List(setup_data, device);
  729. this->EvalList(setup_data, device);
  730. }
  731. template <class FMMNode>
  732. void FMM_Cheb<FMMNode>::W_ListSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device){
  733. { // Set setup_data
  734. setup_data.level=level;
  735. setup_data.kernel=&this->kernel;
  736. setup_data.interac_type.resize(1);
  737. setup_data.interac_type[0]=W_Type;
  738. setup_data. input_data=&buff[0];
  739. setup_data.output_data=&buff[5];
  740. Vector<FMMNode_t*>& nodes_in =n_list[0];
  741. Vector<FMMNode_t*>& nodes_out=n_list[5];
  742. setup_data.nodes_in .clear();
  743. setup_data.nodes_out.clear();
  744. for(size_t i=0;i<nodes_in .Dim();i++) if(nodes_in [i]->Depth()==level+1 || level==-1) setup_data.nodes_in .push_back(nodes_in [i]);
  745. for(size_t i=0;i<nodes_out.Dim();i++) if(nodes_out[i]->Depth()==level || level==-1) setup_data.nodes_out.push_back(nodes_out[i]);
  746. }
  747. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  748. std::vector<void*>& nodes_out=setup_data.nodes_out;
  749. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  750. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  751. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(&((FMMData*)((FMMNode*)nodes_in [i])->FMMData())->upward_equiv);
  752. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->cheb_out);
  753. this->SetupInterac(setup_data,device);
  754. { // Resize device buffer
  755. size_t n=setup_data.output_data->Dim(0)*setup_data.output_data->Dim(1)*sizeof(Real_t);
  756. if(this->dev_buffer.Dim()<n) this->dev_buffer.Resize(n);
  757. }
  758. }
  759. template <class FMMNode>
  760. void FMM_Cheb<FMMNode>::W_List (SetupData<Real_t>& setup_data, bool device){
  761. //Add W_List contribution.
  762. this->EvalList(setup_data, device);
  763. }
  764. template <class FMMNode>
  765. void FMM_Cheb<FMMNode>::U_ListSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device){
  766. FMM_Pts<FMMNode>::U_ListSetup(setup_data, buff, n_list, level, device);
  767. { // Set setup_data
  768. setup_data.level=level;
  769. setup_data.kernel=&this->kernel;
  770. setup_data.interac_type.resize(3);
  771. setup_data.interac_type[0]=U0_Type;
  772. setup_data.interac_type[1]=U1_Type;
  773. setup_data.interac_type[2]=U2_Type;
  774. setup_data. input_data=&buff[4];
  775. setup_data.output_data=&buff[5];
  776. Vector<FMMNode_t*>& nodes_in =n_list[4];
  777. Vector<FMMNode_t*>& nodes_out=n_list[5];
  778. setup_data.nodes_in .clear();
  779. setup_data.nodes_out.clear();
  780. for(size_t i=0;i<nodes_in .Dim();i++) if((level-1<=nodes_in [i]->Depth() && nodes_in [i]->Depth()<=level+1) || level==-1) setup_data.nodes_in .push_back(nodes_in [i]);
  781. for(size_t i=0;i<nodes_out.Dim();i++) if(( nodes_out[i]->Depth()==level ) || level==-1) setup_data.nodes_out.push_back(nodes_out[i]);
  782. }
  783. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  784. std::vector<void*>& nodes_out=setup_data.nodes_out;
  785. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  786. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  787. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(& ((FMMNode*)nodes_in [i])->ChebData());
  788. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->cheb_out);
  789. this->SetupInterac(setup_data,device);
  790. { // Resize device buffer
  791. size_t n=setup_data.output_data->Dim(0)*setup_data.output_data->Dim(1)*sizeof(Real_t);
  792. if(this->dev_buffer.Dim()<n) this->dev_buffer.Resize(n);
  793. }
  794. }
  795. template <class FMMNode>
  796. void FMM_Cheb<FMMNode>::U_List (SetupData<Real_t>& setup_data, bool device){
  797. //Add U_List contribution.
  798. FMM_Pts<FMMNode>::U_List(setup_data, device);
  799. this->EvalList(setup_data, device);
  800. }
  801. template <class FMMNode>
  802. void FMM_Cheb<FMMNode>::Down2TargetSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device){
  803. { // Set setup_data
  804. setup_data.level=level;
  805. setup_data.kernel=&this->kernel;
  806. setup_data.interac_type.resize(1);
  807. setup_data.interac_type[0]=D2T_Type;
  808. setup_data. input_data=&buff[1];
  809. setup_data.output_data=&buff[5];
  810. Vector<FMMNode_t*>& nodes_in =n_list[1];
  811. Vector<FMMNode_t*>& nodes_out=n_list[5];
  812. setup_data.nodes_in .clear();
  813. setup_data.nodes_out.clear();
  814. for(size_t i=0;i<nodes_in .Dim();i++) if(nodes_in [i]->Depth()==level || level==-1) setup_data.nodes_in .push_back(nodes_in [i]);
  815. for(size_t i=0;i<nodes_out.Dim();i++) if(nodes_out[i]->Depth()==level || level==-1) setup_data.nodes_out.push_back(nodes_out[i]);
  816. }
  817. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  818. std::vector<void*>& nodes_out=setup_data.nodes_out;
  819. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  820. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  821. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(&((FMMData*)((FMMNode*)nodes_in [i])->FMMData())->dnward_equiv);
  822. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->cheb_out);
  823. this->SetupInterac(setup_data,device);
  824. }
  825. template <class FMMNode>
  826. void FMM_Cheb<FMMNode>::Down2Target (SetupData<Real_t>& setup_data, bool device){
  827. //Add X_List contribution.
  828. this->EvalList(setup_data, device);
  829. }
  830. template <class FMMNode>
  831. void FMM_Cheb<FMMNode>::PostProcessing(std::vector<FMMNode_t*>& nodes){
  832. size_t n=nodes.size();
  833. #pragma omp parallel
  834. {
  835. int omp_p=omp_get_num_threads();
  836. int pid = omp_get_thread_num();
  837. size_t a=(pid*n)/omp_p;
  838. size_t b=((pid+1)*n)/omp_p;
  839. //For each node, compute interaction from point sources.
  840. for(size_t i=a;i<b;i++){
  841. Vector<Real_t>& trg_coord=nodes[i]->trg_coord;
  842. Vector<Real_t>& trg_value=nodes[i]->trg_value;
  843. Vector<Real_t>& cheb_out =((FMMData*)nodes[i]->FMMData())->cheb_out;
  844. //Initialize target potential.
  845. size_t trg_cnt=trg_coord.Dim()/COORD_DIM;
  846. //trg_value.assign(trg_cnt*dof*this->kernel.ker_dim[1],0);
  847. //Sample local expansion at target points.
  848. if(trg_cnt>0 && cheb_out.Dim()>0){
  849. Real_t* c=nodes[i]->Coord();
  850. Real_t scale=pow(2.0,nodes[i]->Depth()+1);
  851. std::vector<Real_t> rel_coord(COORD_DIM*trg_cnt);
  852. for(size_t j=0;j<trg_cnt;j++) for(int k=0;k<COORD_DIM;k++)
  853. rel_coord[j*COORD_DIM+k]=(trg_coord[j*COORD_DIM+k]-c[k])*scale-1.0;
  854. cheb_eval(cheb_out, cheb_deg, rel_coord, trg_value);
  855. }
  856. }
  857. }
  858. }
  859. }//end namespace