fmm_cheb.txx 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. size_t progress=0, class_count=0;
  355. { // Determine precomputation progress.
  356. size_t mat_cnt=this->interac_list.ListCount((Mat_Type)type);
  357. for(size_t i=0; i<mat_cnt; i++){
  358. size_t indx=this->interac_list.InteracClass((Mat_Type)type,i);
  359. if(indx==i){
  360. class_count++;
  361. if(i<mat_indx) progress++;
  362. }
  363. }
  364. }
  365. //Compute the matrix.
  366. Matrix<Real_t> M;
  367. int n_src=((cheb_deg+1)*(cheb_deg+2)*(cheb_deg+3))/6;
  368. switch (type){
  369. case S2U_Type:
  370. {
  371. if(this->MultipoleOrder()==0) break;
  372. Real_t r=pow(0.5,level);
  373. Real_t c[3]={0,0,0};
  374. // Coord of upward check surface
  375. std::vector<Real_t> uc_coord=u_check_surf(this->MultipoleOrder(),c,level);
  376. size_t n_uc=uc_coord.size()/3;
  377. // Evaluate potential at check surface.
  378. Matrix<Real_t> M_s2c(n_src*this->aux_kernel.ker_dim[0],n_uc*this->aux_kernel.ker_dim[1]); //source 2 check
  379. Matrix<Real_t> M_s2c_local(n_src*this->aux_kernel.ker_dim[0],n_uc*this->aux_kernel.ker_dim[1]);
  380. {
  381. M_s2c.SetZero();
  382. M_s2c_local.SetZero();
  383. size_t cnt_done=0;
  384. #pragma omp parallel for schedule(dynamic)
  385. for(size_t i=myrank;i<n_uc;i+=np){
  386. std::vector<Real_t> M_=cheb_integ(cheb_deg, &uc_coord[i*3], r, this->aux_kernel);
  387. #ifdef __VERBOSE__
  388. #pragma omp critical
  389. if(!myrank){
  390. cnt_done++;
  391. std::cout<<"\r Progress: "<<(100*progress*n_uc+100*cnt_done*np)/(class_count*n_uc)<<"% "<<std::flush;
  392. }
  393. #endif
  394. for(int k=0; k<this->aux_kernel.ker_dim[1]; k++)
  395. for(size_t j=0; j<(size_t)M_s2c.Dim(0); j++)
  396. M_s2c_local[j][i*this->aux_kernel.ker_dim[1]+k] = M_[j+k*M_s2c.Dim(0)];
  397. }
  398. if(!myrank) std::cout<<"\r \r"<<std::flush;
  399. 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);
  400. }
  401. Matrix<Real_t>& M_c2e = this->Precomp(level, UC2UE_Type, 0);
  402. M=M_s2c*M_c2e;
  403. break;
  404. }
  405. case D2T_Type:
  406. {
  407. if(this->MultipoleOrder()==0) break;
  408. Matrix<Real_t>& M_s2t=FMM_Pts<FMMNode>::Precomp(level, type, mat_indx);
  409. int n_trg=M_s2t.Dim(1)/this->kernel.ker_dim[1];
  410. // Compute Chebyshev approx from target potential.
  411. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  412. #pragma omp parallel for schedule(dynamic)
  413. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  414. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  415. M_trg=M_trg.Transpose();
  416. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  417. }
  418. #pragma omp critical (PRECOMP_MATRIX_PTS)
  419. {
  420. M_s2t.Resize(0,0);
  421. }
  422. break;
  423. }
  424. case U0_Type:
  425. {
  426. // Coord of target points
  427. Real_t s=pow(0.5,level);
  428. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  429. Real_t coord_diff[3]={(coord[0]-1)*s*0.5,(coord[1]-1)*s*0.5,(coord[2]-1)*s*0.5};
  430. std::vector<Real_t>& rel_trg_coord = this->mat->RelativeTrgCoord();
  431. size_t n_trg = rel_trg_coord.size()/3;
  432. std::vector<Real_t> trg_coord(n_trg*3);
  433. for(size_t j=0;j<n_trg;j++){
  434. trg_coord[j*3+0]=rel_trg_coord[j*3+0]*s-coord_diff[0];
  435. trg_coord[j*3+1]=rel_trg_coord[j*3+1]*s-coord_diff[1];
  436. trg_coord[j*3+2]=rel_trg_coord[j*3+2]*s-coord_diff[2];
  437. }
  438. // Evaluate potential at target points.
  439. Matrix<Real_t> M_s2t(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  440. Matrix<Real_t> M_s2t_local(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  441. {
  442. M_s2t.SetZero();
  443. M_s2t_local.SetZero();
  444. size_t cnt_done=0;
  445. #pragma omp parallel for schedule(dynamic)
  446. for(size_t i=myrank;i<n_trg;i+=np){
  447. std::vector<Real_t> s2t=cheb_integ(cheb_deg, &trg_coord[i*3], (Real_t)(s*2.0), this->kernel);
  448. #ifdef __VERBOSE__
  449. #pragma omp critical
  450. if(!myrank){
  451. cnt_done++;
  452. std::cout<<"\r Progress: "<<(100*progress*n_trg+100*cnt_done*np)/(class_count*n_trg)<<"% "<<std::flush;
  453. }
  454. #endif
  455. for(int k=0; k<this->kernel.ker_dim[1]; k++)
  456. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++)
  457. M_s2t_local[j][i*this->kernel.ker_dim[1]+k] = s2t[j+k*M_s2t.Dim(0)];
  458. }
  459. if(!myrank) std::cout<<"\r \r"<<std::flush;
  460. 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);
  461. }
  462. // Compute Chebyshev approx from target potential.
  463. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  464. #pragma omp parallel for schedule(dynamic)
  465. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  466. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  467. M_trg=M_trg.Transpose();
  468. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  469. }
  470. break;
  471. }
  472. case U1_Type:
  473. {
  474. // Coord of target points
  475. Real_t s=pow(0.5,level);
  476. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  477. Real_t coord_diff[3]={coord[0]*s,coord[1]*s,coord[2]*s};
  478. std::vector<Real_t>& rel_trg_coord = this->mat->RelativeTrgCoord();
  479. size_t n_trg = rel_trg_coord.size()/3;
  480. std::vector<Real_t> trg_coord(n_trg*3);
  481. for(size_t j=0;j<n_trg;j++){
  482. trg_coord[j*3+0]=rel_trg_coord[j*3+0]*s-coord_diff[0];
  483. trg_coord[j*3+1]=rel_trg_coord[j*3+1]*s-coord_diff[1];
  484. trg_coord[j*3+2]=rel_trg_coord[j*3+2]*s-coord_diff[2];
  485. }
  486. // Evaluate potential at target points.
  487. Matrix<Real_t> M_s2t(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  488. Matrix<Real_t> M_s2t_local(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  489. {
  490. M_s2t.SetZero();
  491. M_s2t_local.SetZero();
  492. size_t cnt_done=0;
  493. #pragma omp parallel for schedule(dynamic)
  494. for(size_t i=myrank;i<n_trg;i+=np){
  495. std::vector<Real_t> s2t=cheb_integ(cheb_deg, &trg_coord[i*3], s, this->kernel);
  496. #ifdef __VERBOSE__
  497. #pragma omp critical
  498. if(!myrank){
  499. cnt_done++;
  500. std::cout<<"\r Progress: "<<(100*progress*n_trg+100*cnt_done*np)/(class_count*n_trg)<<"% "<<std::flush;
  501. }
  502. #endif
  503. for(int k=0; k<this->kernel.ker_dim[1]; k++)
  504. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++)
  505. M_s2t_local[j][i*this->kernel.ker_dim[1]+k] = s2t[j+k*M_s2t.Dim(0)];
  506. }
  507. if(!myrank) std::cout<<"\r \r"<<std::flush;
  508. 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);
  509. }
  510. // Compute Chebyshev approx from target potential.
  511. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  512. #pragma omp parallel for schedule(dynamic)
  513. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  514. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  515. M_trg=M_trg.Transpose();
  516. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  517. }
  518. break;
  519. }
  520. case U2_Type:
  521. {
  522. // Coord of target points
  523. Real_t s=pow(0.5,level);
  524. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  525. Real_t coord_diff[3]={(coord[0]+1)*s*0.25,(coord[1]+1)*s*0.25,(coord[2]+1)*s*0.25};
  526. std::vector<Real_t>& rel_trg_coord = this->mat->RelativeTrgCoord();
  527. size_t n_trg = rel_trg_coord.size()/3;
  528. std::vector<Real_t> trg_coord(n_trg*3);
  529. for(size_t j=0;j<n_trg;j++){
  530. trg_coord[j*3+0]=rel_trg_coord[j*3+0]*s-coord_diff[0];
  531. trg_coord[j*3+1]=rel_trg_coord[j*3+1]*s-coord_diff[1];
  532. trg_coord[j*3+2]=rel_trg_coord[j*3+2]*s-coord_diff[2];
  533. }
  534. // Evaluate potential at target points.
  535. Matrix<Real_t> M_s2t(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  536. Matrix<Real_t> M_s2t_local(n_src*this->kernel.ker_dim [0], n_trg*this->kernel.ker_dim [1]);
  537. {
  538. M_s2t.SetZero();
  539. M_s2t_local.SetZero();
  540. size_t cnt_done=0;
  541. #pragma omp parallel for schedule(dynamic)
  542. for(size_t i=myrank;i<n_trg;i+=np){
  543. std::vector<Real_t> s2t=cheb_integ(cheb_deg, &trg_coord[i*3], (Real_t)(s*0.5), this->kernel);
  544. #ifdef __VERBOSE__
  545. #pragma omp critical
  546. if(!myrank){
  547. cnt_done++;
  548. std::cout<<"\r Progress: "<<(100*progress*n_trg+100*cnt_done*np)/(class_count*n_trg)<<"% "<<std::flush;
  549. }
  550. #endif
  551. for(int k=0; k<this->kernel.ker_dim[1]; k++)
  552. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++)
  553. M_s2t_local[j][i*this->kernel.ker_dim[1]+k] = s2t[j+k*M_s2t.Dim(0)];
  554. }
  555. if(!myrank) std::cout<<"\r \r"<<std::flush;
  556. 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);
  557. }
  558. // Compute Chebyshev approx from target potential.
  559. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  560. #pragma omp parallel for schedule(dynamic)
  561. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  562. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  563. M_trg=M_trg.Transpose();
  564. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  565. }
  566. break;
  567. }
  568. case W_Type:
  569. {
  570. if(this->MultipoleOrder()==0) break;
  571. Matrix<Real_t>& M_s2t=FMM_Pts<FMMNode>::Precomp(level, type, mat_indx);
  572. int n_trg=M_s2t.Dim(1)/this->kernel.ker_dim[1];
  573. // Compute Chebyshev approx from target potential.
  574. M.Resize(M_s2t.Dim(0), n_src*this->kernel.ker_dim [1]);
  575. #pragma omp parallel for schedule(dynamic)
  576. for(size_t j=0; j<(size_t)M_s2t.Dim(0); j++){
  577. Matrix<Real_t> M_trg(n_trg,this->kernel.ker_dim[1],M_s2t[j],false);
  578. M_trg=M_trg.Transpose();
  579. cheb_approx<Real_t,double>(M_s2t[j],cheb_deg,this->kernel.ker_dim[1],M[j]);
  580. }
  581. #pragma omp critical (PRECOMP_MATRIX_PTS)
  582. {
  583. M_s2t.Resize(0,0);
  584. }
  585. break;
  586. }
  587. case X_Type:
  588. {
  589. if(this->MultipoleOrder()==0) break;
  590. // Coord of target points
  591. Real_t s=pow(0.5,level-1);
  592. int* coord=this->interac_list.RelativeCoord(type,mat_indx);
  593. Real_t c[3]={-(coord[0]-1)*s*0.25,-(coord[1]-1)*s*0.25,-(coord[2]-1)*s*0.25};
  594. std::vector<Real_t> trg_coord=d_check_surf(this->MultipoleOrder(),c,level);
  595. size_t n_trg=trg_coord.size()/3;
  596. // Evaluate potential at target points.
  597. Matrix<Real_t> M_xs2c(n_src*this->aux_kernel.ker_dim[0], n_trg*this->aux_kernel.ker_dim[1]);
  598. Matrix<Real_t> M_xs2c_local(n_src*this->aux_kernel.ker_dim[0], n_trg*this->aux_kernel.ker_dim[1]);
  599. {
  600. M_xs2c.SetZero();
  601. M_xs2c_local.SetZero();
  602. size_t cnt_done=0;
  603. #pragma omp parallel for schedule(dynamic)
  604. for(size_t i=myrank;i<n_trg;i+=np){
  605. std::vector<Real_t> M_=cheb_integ(cheb_deg, &trg_coord[i*3], s, this->aux_kernel);
  606. #ifdef __VERBOSE__
  607. #pragma omp critical
  608. if(!myrank){
  609. cnt_done++;
  610. std::cout<<"\r Progress: "<<(100*progress*n_trg+100*cnt_done*np)/(class_count*n_trg)<<"% "<<std::flush;
  611. }
  612. #endif
  613. for(int k=0; k<this->aux_kernel.ker_dim[1]; k++)
  614. for(size_t j=0; j<(size_t)M_xs2c.Dim(0); j++)
  615. M_xs2c_local[j][i*this->aux_kernel.ker_dim[1]+k] = M_[j+k*M_xs2c.Dim(0)];
  616. }
  617. if(!myrank) std::cout<<"\r \r"<<std::flush;
  618. 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);
  619. }
  620. Matrix<Real_t>& M_c2e = this->Precomp(level, DC2DE_Type, 0);
  621. M=M_xs2c*M_c2e;
  622. break;
  623. }
  624. default:
  625. {
  626. return FMM_Pts<FMMNode>::Precomp(level, type, mat_indx);
  627. break;
  628. }
  629. }
  630. //Save the matrix for future use.
  631. #pragma omp critical (PRECOMP_MATRIX_PTS)
  632. if(M_.Dim(0)==0 && M_.Dim(1)==0){ M_=M;}
  633. return M_;
  634. }
  635. template <class FMMNode>
  636. 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){
  637. if( buff.size()<6) buff.resize(6);
  638. if( n_list.size()<6) n_list.resize(6);
  639. if(extra_size.size()<6) extra_size.resize(6,0);
  640. size_t n_coeff=(cheb_deg+1)*(cheb_deg+2)*(cheb_deg+3)/6;
  641. if(node.size()==0) return;
  642. {// 4. cheb_in
  643. int indx=4;
  644. int dof=this->kernel.ker_dim[0];
  645. size_t vec_sz=dof*n_coeff;
  646. std::vector< FMMNode* > node_lst;
  647. for(size_t i=0;i<node.size();i++)
  648. if(node[i]->IsLeaf())
  649. node_lst.push_back(node[i]);
  650. n_list[indx]=node_lst;
  651. extra_size[indx]+=node_lst.size()*vec_sz;
  652. #pragma omp parallel for
  653. for(size_t i=0;i<node.size();i++){ // Move data before resizing buff[indx]
  654. Vector<Real_t>& cheb_in =node[i]->ChebData();
  655. Vector<Real_t> new_buff=cheb_in;
  656. cheb_in.Swap(new_buff);
  657. }
  658. }
  659. {// 5. cheb_out
  660. int indx=5;
  661. int dof=this->kernel.ker_dim[1];
  662. size_t vec_sz=dof*n_coeff;
  663. std::vector< FMMNode* > node_lst;
  664. for(size_t i=0;i<node.size();i++)
  665. if(node[i]->IsLeaf() && !node[i]->IsGhost())
  666. node_lst.push_back(node[i]);
  667. n_list[indx]=node_lst;
  668. extra_size[indx]+=node_lst.size()*vec_sz;
  669. #pragma omp parallel for
  670. for(size_t i=0;i<node.size();i++){ // Move data before resizing buff[indx]
  671. Vector<Real_t>& cheb_out=((FMMData*)node[i]->FMMData())->cheb_out;
  672. cheb_out.ReInit(0);
  673. }
  674. }
  675. FMM_Pts<FMMNode>::CollectNodeData(node, buff, n_list, extra_size);
  676. {// 4. cheb_in
  677. int indx=4;
  678. int dof=this->kernel.ker_dim[0];
  679. size_t vec_sz=dof*n_coeff;
  680. Vector< FMMNode* >& node_lst=n_list[indx];
  681. Real_t* buff_ptr=buff[indx][0]+buff[indx].Dim(0)*buff[indx].Dim(1)-extra_size[indx];
  682. #pragma omp parallel for
  683. for(size_t i=0;i<node_lst.Dim();i++){
  684. Vector<Real_t>& cheb_in =node_lst[i]->ChebData();
  685. mem::memcopy(buff_ptr+i*vec_sz, &cheb_in [0], cheb_in .Dim()*sizeof(Real_t));
  686. cheb_in .ReInit(vec_sz, buff_ptr+i*vec_sz, false);
  687. //if(node_lst[i]->IsGhost()) cheb_in .SetZero();
  688. }
  689. buff[indx].AllocDevice(true);
  690. }
  691. {// 5. cheb_out
  692. int indx=5;
  693. int dof=this->kernel.ker_dim[1];
  694. size_t vec_sz=dof*n_coeff;
  695. Vector< FMMNode* >& node_lst=n_list[indx];
  696. Real_t* buff_ptr=buff[indx][0]+buff[indx].Dim(0)*buff[indx].Dim(1)-extra_size[indx];
  697. #pragma omp parallel for
  698. for(size_t i=0;i<node_lst.Dim();i++){
  699. Vector<Real_t>& cheb_out=((FMMData*)node_lst[i]->FMMData())->cheb_out;
  700. cheb_out.ReInit(vec_sz, buff_ptr+i*vec_sz, false);
  701. cheb_out.SetZero();
  702. }
  703. buff[indx].AllocDevice(true);
  704. }
  705. }
  706. template <class FMMNode>
  707. void FMM_Cheb<FMMNode>::Source2UpSetup(SetupData<Real_t>& setup_data, std::vector<Matrix<Real_t> >& buff, std::vector<Vector<FMMNode_t*> >& n_list, int level, bool device){
  708. if(this->MultipoleOrder()==0) return;
  709. FMM_Pts<FMMNode>::Source2UpSetup(setup_data, buff, n_list, level, device);
  710. { // Set setup_data
  711. setup_data.level=level;
  712. setup_data.kernel=&this->aux_kernel;
  713. setup_data.interac_type.resize(1);
  714. setup_data.interac_type[0]=S2U_Type;
  715. setup_data. input_data=&buff[4];
  716. setup_data.output_data=&buff[0];
  717. Vector<FMMNode_t*>& nodes_in =n_list[4];
  718. Vector<FMMNode_t*>& nodes_out=n_list[0];
  719. setup_data.nodes_in .clear();
  720. setup_data.nodes_out.clear();
  721. 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]);
  722. 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]);
  723. }
  724. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  725. std::vector<void*>& nodes_out=setup_data.nodes_out;
  726. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  727. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  728. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(&( ((FMMNode*)nodes_in [i]) )->ChebData() );
  729. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->upward_equiv);
  730. this->SetupInterac(setup_data,device);
  731. }
  732. template <class FMMNode>
  733. void FMM_Cheb<FMMNode>::Source2Up (SetupData<Real_t>& setup_data, bool device){
  734. //Add Source2Up contribution.
  735. this->EvalList(setup_data, device);
  736. }
  737. template <class FMMNode>
  738. 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){
  739. if(this->MultipoleOrder()==0) return;
  740. FMM_Pts<FMMNode>::X_ListSetup(setup_data, buff, n_list, level, device);
  741. { // Set setup_data
  742. setup_data.level=level;
  743. setup_data.kernel=&this->aux_kernel;
  744. setup_data.interac_type.resize(1);
  745. setup_data.interac_type[0]=X_Type;
  746. setup_data. input_data=&buff[4];
  747. setup_data.output_data=&buff[1];
  748. Vector<FMMNode_t*>& nodes_in =n_list[4];
  749. Vector<FMMNode_t*>& nodes_out=n_list[1];
  750. setup_data.nodes_in .clear();
  751. setup_data.nodes_out.clear();
  752. 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]);
  753. 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]);
  754. }
  755. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  756. std::vector<void*>& nodes_out=setup_data.nodes_out;
  757. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  758. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  759. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(&( ((FMMNode*)nodes_in [i]) )->ChebData() );
  760. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->dnward_equiv);
  761. this->SetupInterac(setup_data,device);
  762. { // Resize device buffer
  763. size_t n=setup_data.output_data->Dim(0)*setup_data.output_data->Dim(1)*sizeof(Real_t);
  764. if(this->dev_buffer.Dim()<n) this->dev_buffer.Resize(n);
  765. }
  766. }
  767. template <class FMMNode>
  768. void FMM_Cheb<FMMNode>::X_List (SetupData<Real_t>& setup_data, bool device){
  769. //Add X_List contribution.
  770. FMM_Pts<FMMNode>::X_List(setup_data, device);
  771. this->EvalList(setup_data, device);
  772. }
  773. template <class FMMNode>
  774. 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){
  775. if(this->MultipoleOrder()==0) return;
  776. { // Set setup_data
  777. setup_data.level=level;
  778. setup_data.kernel=&this->kernel;
  779. setup_data.interac_type.resize(1);
  780. setup_data.interac_type[0]=W_Type;
  781. setup_data. input_data=&buff[0];
  782. setup_data.output_data=&buff[5];
  783. Vector<FMMNode_t*>& nodes_in =n_list[0];
  784. Vector<FMMNode_t*>& nodes_out=n_list[5];
  785. setup_data.nodes_in .clear();
  786. setup_data.nodes_out.clear();
  787. 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]);
  788. 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]);
  789. }
  790. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  791. std::vector<void*>& nodes_out=setup_data.nodes_out;
  792. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  793. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  794. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(&((FMMData*)((FMMNode*)nodes_in [i])->FMMData())->upward_equiv);
  795. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->cheb_out );
  796. this->SetupInterac(setup_data,device);
  797. { // Resize device buffer
  798. size_t n=setup_data.output_data->Dim(0)*setup_data.output_data->Dim(1)*sizeof(Real_t);
  799. if(this->dev_buffer.Dim()<n) this->dev_buffer.Resize(n);
  800. }
  801. }
  802. template <class FMMNode>
  803. void FMM_Cheb<FMMNode>::W_List (SetupData<Real_t>& setup_data, bool device){
  804. //Add W_List contribution.
  805. this->EvalList(setup_data, device);
  806. }
  807. template <class FMMNode>
  808. 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){
  809. FMM_Pts<FMMNode>::U_ListSetup(setup_data, buff, n_list, level, device);
  810. { // Set setup_data
  811. setup_data.level=level;
  812. setup_data.kernel=&this->kernel;
  813. setup_data.interac_type.resize(3);
  814. setup_data.interac_type[0]=U0_Type;
  815. setup_data.interac_type[1]=U1_Type;
  816. setup_data.interac_type[2]=U2_Type;
  817. setup_data. input_data=&buff[4];
  818. setup_data.output_data=&buff[5];
  819. Vector<FMMNode_t*>& nodes_in =n_list[4];
  820. Vector<FMMNode_t*>& nodes_out=n_list[5];
  821. setup_data.nodes_in .clear();
  822. setup_data.nodes_out.clear();
  823. 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]);
  824. 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]);
  825. }
  826. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  827. std::vector<void*>& nodes_out=setup_data.nodes_out;
  828. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  829. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  830. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(&( ((FMMNode*)nodes_in [i]) )->ChebData());
  831. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->cheb_out );
  832. this->SetupInterac(setup_data,device);
  833. { // Resize device buffer
  834. size_t n=setup_data.output_data->Dim(0)*setup_data.output_data->Dim(1)*sizeof(Real_t);
  835. if(this->dev_buffer.Dim()<n) this->dev_buffer.Resize(n);
  836. }
  837. }
  838. template <class FMMNode>
  839. void FMM_Cheb<FMMNode>::U_List (SetupData<Real_t>& setup_data, bool device){
  840. //Add U_List contribution.
  841. FMM_Pts<FMMNode>::U_List(setup_data, device);
  842. this->EvalList(setup_data, device);
  843. }
  844. template <class FMMNode>
  845. 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){
  846. if(this->MultipoleOrder()==0) return;
  847. { // Set setup_data
  848. setup_data.level=level;
  849. setup_data.kernel=&this->kernel;
  850. setup_data.interac_type.resize(1);
  851. setup_data.interac_type[0]=D2T_Type;
  852. setup_data. input_data=&buff[1];
  853. setup_data.output_data=&buff[5];
  854. Vector<FMMNode_t*>& nodes_in =n_list[1];
  855. Vector<FMMNode_t*>& nodes_out=n_list[5];
  856. setup_data.nodes_in .clear();
  857. setup_data.nodes_out.clear();
  858. 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]);
  859. 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]);
  860. }
  861. std::vector<void*>& nodes_in =setup_data.nodes_in ;
  862. std::vector<void*>& nodes_out=setup_data.nodes_out;
  863. std::vector<Vector<Real_t>*>& input_vector=setup_data. input_vector; input_vector.clear();
  864. std::vector<Vector<Real_t>*>& output_vector=setup_data.output_vector; output_vector.clear();
  865. for(size_t i=0;i<nodes_in .size();i++) input_vector.push_back(&((FMMData*)((FMMNode*)nodes_in [i])->FMMData())->dnward_equiv);
  866. for(size_t i=0;i<nodes_out.size();i++) output_vector.push_back(&((FMMData*)((FMMNode*)nodes_out[i])->FMMData())->cheb_out );
  867. this->SetupInterac(setup_data,device);
  868. }
  869. template <class FMMNode>
  870. void FMM_Cheb<FMMNode>::Down2Target (SetupData<Real_t>& setup_data, bool device){
  871. //Add Down2Target contribution.
  872. this->EvalList(setup_data, device);
  873. }
  874. template <class FMMNode>
  875. void FMM_Cheb<FMMNode>::PostProcessing(std::vector<FMMNode_t*>& nodes){
  876. size_t n=nodes.size();
  877. #pragma omp parallel
  878. {
  879. int omp_p=omp_get_num_threads();
  880. int pid = omp_get_thread_num();
  881. size_t a=(pid*n)/omp_p;
  882. size_t b=((pid+1)*n)/omp_p;
  883. //For each node, compute interaction from point sources.
  884. for(size_t i=a;i<b;i++){
  885. Vector<Real_t>& trg_coord=nodes[i]->trg_coord;
  886. Vector<Real_t>& trg_value=nodes[i]->trg_value;
  887. Vector<Real_t>& cheb_out =((FMMData*)nodes[i]->FMMData())->cheb_out;
  888. //Initialize target potential.
  889. size_t trg_cnt=trg_coord.Dim()/COORD_DIM;
  890. //trg_value.assign(trg_cnt*dof*this->kernel.ker_dim[1],0);
  891. //Sample local expansion at target points.
  892. if(trg_cnt>0 && cheb_out.Dim()>0){
  893. Real_t* c=nodes[i]->Coord();
  894. Real_t scale=pow(2.0,nodes[i]->Depth()+1);
  895. std::vector<Real_t> rel_coord(COORD_DIM*trg_cnt);
  896. for(size_t j=0;j<trg_cnt;j++) for(int k=0;k<COORD_DIM;k++)
  897. rel_coord[j*COORD_DIM+k]=(trg_coord[j*COORD_DIM+k]-c[k])*scale-1.0;
  898. cheb_eval(cheb_out, cheb_deg, rel_coord, trg_value);
  899. }
  900. }
  901. }
  902. }
  903. }//end namespace