interac_list.txx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /**
  2. * \file interac_list.txx
  3. * \author Dhairya Malhotra, dhairya.malhotra@gmail.com
  4. * \date 6-11-2012
  5. * \brief This file contains the implementation of the InteracList class.
  6. * Handles the logic for different interaction lists, and determines the
  7. * symmetry class for each interaction.
  8. */
  9. #include <cmath>
  10. #include <cassert>
  11. #include <parUtils.h>
  12. #include <ompUtils.h>
  13. namespace pvfmm{
  14. /**
  15. * \brief Initialize.
  16. */
  17. template <class Node_t>
  18. void InteracList<Node_t>::Initialize(unsigned int dim_, PrecompMat<Real_t>* mat_){
  19. #ifdef PVFMM_NO_SYMMETRIES
  20. use_symmetries=false;
  21. #else
  22. use_symmetries=true;
  23. #endif
  24. dim=dim_;
  25. assert(dim==3); //Only supporting 3D for now.
  26. mat=mat_;
  27. interac_class.resize(Type_Count);
  28. perm_list.resize(Type_Count);
  29. rel_coord.resize(Type_Count);
  30. hash_lut.resize(Type_Count);
  31. InitList(0,0,1,UC2UE_Type);
  32. InitList(0,0,1,DC2DE_Type);
  33. InitList(0,0,1,S2U_Type);
  34. InitList(1,1,2,U2U_Type);
  35. InitList(1,1,2,D2D_Type);
  36. InitList(0,0,1,D2T_Type);
  37. InitList(3,3,2,U0_Type);
  38. InitList(1,0,1,U1_Type);
  39. InitList(3,3,2,U2_Type);
  40. InitList(3,2,1,V_Type);
  41. InitList(1,1,1,V1_Type);
  42. InitList(5,5,2,W_Type);
  43. InitList(5,5,2,X_Type);
  44. InitList(0,0,1,BC_Type);
  45. }
  46. /**
  47. * \brief Number of possible interactions in each list.
  48. */
  49. template <class Node_t>
  50. size_t InteracList<Node_t>::ListCount(Mat_Type t){
  51. return rel_coord[t].Dim(0);
  52. }
  53. /**
  54. * \brief Returns the relative octant coordinates for an interaction i of
  55. * type t.
  56. */
  57. template <class Node_t>
  58. int* InteracList<Node_t>::RelativeCoord(Mat_Type t, size_t i){
  59. return rel_coord[t][i];
  60. }
  61. /**
  62. * \brief For an interaction of type t and index i, returns the symmetry
  63. * class for the same.
  64. */
  65. template <class Node_t>
  66. size_t InteracList<Node_t>::InteracClass(Mat_Type t, size_t i){
  67. return interac_class[t][i];
  68. }
  69. /**
  70. * \brief Returns the list of permutations to be applied to the matrix to
  71. * convert it to its interac_class.
  72. */
  73. template <class Node_t>
  74. std::vector<Perm_Type>& InteracList<Node_t>::PermutList(Mat_Type t, size_t i){
  75. return perm_list[t][i];
  76. }
  77. /**
  78. * \brief Build interaction list for this node.
  79. */
  80. template <class Node_t>
  81. std::vector<Node_t*> InteracList<Node_t>::BuildList(Node_t* n, Mat_Type t){
  82. std::vector<Node_t*> interac_list(ListCount(t),NULL);
  83. int n_collg=(int)pow(3.0,(int)dim);
  84. int n_child=(int)pow(2.0,(int)dim);
  85. int rel_coord[3];
  86. switch (t){
  87. case S2U_Type:
  88. {
  89. if(!n->IsGhost() && n->IsLeaf()) interac_list[0]=n;
  90. break;
  91. }
  92. case U2U_Type:
  93. {
  94. if(n->IsGhost() || n->IsLeaf()) return interac_list;
  95. for(int j=0;j<n_child;j++){
  96. rel_coord[0]=-1+(j & 1?2:0);
  97. rel_coord[1]=-1+(j & 2?2:0);
  98. rel_coord[2]=-1+(j & 4?2:0);
  99. int c_hash = coord_hash(rel_coord);
  100. int idx=hash_lut[t][c_hash];
  101. Node_t* chld=(Node_t*)n->Child(j);
  102. if(idx>=0 && !chld->IsGhost()) interac_list[idx]=chld;
  103. }
  104. break;
  105. }
  106. case D2D_Type:
  107. {
  108. if(n->IsGhost() || n->Parent()==NULL) return interac_list;
  109. Node_t* p=(Node_t*)n->Parent();
  110. int p2n=n->Path2Node();
  111. {
  112. rel_coord[0]=-1+(p2n & 1?2:0);
  113. rel_coord[1]=-1+(p2n & 2?2:0);
  114. rel_coord[2]=-1+(p2n & 4?2:0);
  115. int c_hash = coord_hash(rel_coord);
  116. int idx=hash_lut[t][c_hash];
  117. if(idx>=0) interac_list[idx]=p;
  118. }
  119. break;
  120. }
  121. case D2T_Type:
  122. {
  123. if(!n->IsGhost() && n->IsLeaf()) interac_list[0]=n;
  124. break;
  125. }
  126. case U0_Type:
  127. {
  128. if(n->IsGhost() || n->Parent()==NULL || !n->IsLeaf()) return interac_list;
  129. Node_t* p=(Node_t*)n->Parent();
  130. int p2n=n->Path2Node();
  131. for(int i=0;i<n_collg;i++){
  132. Node_t* pc=(Node_t*)p->Colleague(i);
  133. if(pc!=NULL && pc->IsLeaf()){
  134. rel_coord[0]=( i %3)*4-4-(p2n & 1?2:0)+1;
  135. rel_coord[1]=((i/3)%3)*4-4-(p2n & 2?2:0)+1;
  136. rel_coord[2]=((i/9)%3)*4-4-(p2n & 4?2:0)+1;
  137. int c_hash = coord_hash(rel_coord);
  138. int idx=hash_lut[t][c_hash];
  139. if(idx>=0) interac_list[idx]=pc;
  140. }
  141. }
  142. break;
  143. }
  144. case U1_Type:
  145. {
  146. if(n->IsGhost() || !n->IsLeaf()) return interac_list;
  147. for(int i=0;i<n_collg;i++){
  148. Node_t* col=(Node_t*)n->Colleague(i);
  149. if(col!=NULL && col->IsLeaf()){
  150. rel_coord[0]=( i %3)-1;
  151. rel_coord[1]=((i/3)%3)-1;
  152. rel_coord[2]=((i/9)%3)-1;
  153. int c_hash = coord_hash(rel_coord);
  154. int idx=hash_lut[t][c_hash];
  155. if(idx>=0) interac_list[idx]=col;
  156. }
  157. }
  158. break;
  159. }
  160. case U2_Type:
  161. {
  162. if(n->IsGhost() || !n->IsLeaf()) return interac_list;
  163. for(int i=0;i<n_collg;i++){
  164. Node_t* col=(Node_t*)n->Colleague(i);
  165. if(col!=NULL && !col->IsLeaf()){
  166. for(int j=0;j<n_child;j++){
  167. rel_coord[0]=( i %3)*4-4+(j & 1?2:0)-1;
  168. rel_coord[1]=((i/3)%3)*4-4+(j & 2?2:0)-1;
  169. rel_coord[2]=((i/9)%3)*4-4+(j & 4?2:0)-1;
  170. int c_hash = coord_hash(rel_coord);
  171. int idx=hash_lut[t][c_hash];
  172. if(idx>=0){
  173. assert(col->Child(j)->IsLeaf()); //2:1 balanced
  174. interac_list[idx]=(Node_t*)col->Child(j);
  175. }
  176. }
  177. }
  178. }
  179. break;
  180. }
  181. case V_Type:
  182. {
  183. if(n->IsGhost() || n->Parent()==NULL) return interac_list;
  184. Node_t* p=(Node_t*)n->Parent();
  185. int p2n=n->Path2Node();
  186. for(int i=0;i<n_collg;i++){
  187. Node_t* pc=(Node_t*)p->Colleague(i);
  188. if(pc!=NULL?!pc->IsLeaf():0){
  189. for(int j=0;j<n_child;j++){
  190. rel_coord[0]=( i %3)*2-2+(j & 1?1:0)-(p2n & 1?1:0);
  191. rel_coord[1]=((i/3)%3)*2-2+(j & 2?1:0)-(p2n & 2?1:0);
  192. rel_coord[2]=((i/9)%3)*2-2+(j & 4?1:0)-(p2n & 4?1:0);
  193. int c_hash = coord_hash(rel_coord);
  194. int idx=hash_lut[t][c_hash];
  195. if(idx>=0) interac_list[idx]=(Node_t*)pc->Child(j);
  196. }
  197. }
  198. }
  199. break;
  200. }
  201. case V1_Type:
  202. {
  203. if(n->IsGhost() || n->IsLeaf()) return interac_list;
  204. for(int i=0;i<n_collg;i++){
  205. Node_t* col=(Node_t*)n->Colleague(i);
  206. if(col!=NULL && !col->IsLeaf()){
  207. rel_coord[0]=( i %3)-1;
  208. rel_coord[1]=((i/3)%3)-1;
  209. rel_coord[2]=((i/9)%3)-1;
  210. int c_hash = coord_hash(rel_coord);
  211. int idx=hash_lut[t][c_hash];
  212. if(idx>=0) interac_list[idx]=col;
  213. }
  214. }
  215. break;
  216. }
  217. case W_Type:
  218. {
  219. if(n->IsGhost() || !n->IsLeaf()) return interac_list;
  220. for(int i=0;i<n_collg;i++){
  221. Node_t* col=(Node_t*)n->Colleague(i);
  222. if(col!=NULL && !col->IsLeaf()){
  223. for(int j=0;j<n_child;j++){
  224. rel_coord[0]=( i %3)*4-4+(j & 1?2:0)-1;
  225. rel_coord[1]=((i/3)%3)*4-4+(j & 2?2:0)-1;
  226. rel_coord[2]=((i/9)%3)*4-4+(j & 4?2:0)-1;
  227. int c_hash = coord_hash(rel_coord);
  228. int idx=hash_lut[t][c_hash];
  229. if(idx>=0) interac_list[idx]=(Node_t*)col->Child(j);
  230. }
  231. }
  232. }
  233. break;
  234. }
  235. case X_Type:
  236. {
  237. if(n->IsGhost() || n->Parent()==NULL) return interac_list;
  238. Node_t* p=(Node_t*)n->Parent();
  239. int p2n=n->Path2Node();
  240. for(int i=0;i<n_collg;i++){
  241. Node_t* pc=(Node_t*)p->Colleague(i);
  242. if(pc!=NULL && pc->IsLeaf()){
  243. rel_coord[0]=( i %3)*4-4-(p2n & 1?2:0)+1;
  244. rel_coord[1]=((i/3)%3)*4-4-(p2n & 2?2:0)+1;
  245. rel_coord[2]=((i/9)%3)*4-4-(p2n & 4?2:0)+1;
  246. int c_hash = coord_hash(rel_coord);
  247. int idx=hash_lut[t][c_hash];
  248. if(idx>=0) interac_list[idx]=pc;
  249. }
  250. }
  251. break;
  252. }
  253. default:
  254. std::vector<Node_t*> empty_list;
  255. return empty_list;
  256. break;
  257. }
  258. return interac_list;
  259. }
  260. template <class Node_t>
  261. Matrix<typename Node_t::Real_t>& InteracList<Node_t>::ClassMat(int l, Mat_Type type, size_t indx){
  262. size_t indx0=InteracClass(type, indx);
  263. return mat->Mat(l, type, indx0);
  264. }
  265. template <class Node_t>
  266. Permutation<typename Node_t::Real_t>& InteracList<Node_t>::Perm_R(int l, Mat_Type type, size_t indx){
  267. size_t indx0=InteracClass(type, indx);
  268. Matrix <Real_t>& M0 =mat->Mat (l, type, indx0);
  269. Permutation<Real_t>& row_perm=mat->Perm_R(l, type, indx );
  270. if(M0.Dim(0)==0 || M0.Dim(1)==0) return row_perm;
  271. //Get the necessary permutations.
  272. if(row_perm.Dim()==0){
  273. std::vector<Perm_Type> p_list=PermutList(type, indx);
  274. for(int i=0;i<l;i++) p_list.push_back(Scaling);
  275. Permutation<Real_t> row_perm_=Permutation<Real_t>(M0.Dim(0));
  276. for(int i=0;i<C_Perm;i++){
  277. Permutation<Real_t>& pr=mat->Perm(type, R_Perm + i);
  278. if(!pr.Dim()) row_perm_=Permutation<Real_t>(0);
  279. }
  280. if(row_perm_.Dim()>0)
  281. for(int i=p_list.size()-1; i>=0; i--){
  282. assert(type!=V_Type);
  283. Permutation<Real_t>& pr=mat->Perm(type, R_Perm + p_list[i]);
  284. row_perm_=pr.Transpose()*row_perm_;
  285. }
  286. row_perm=row_perm_;
  287. }
  288. return row_perm;
  289. }
  290. template <class Node_t>
  291. Permutation<typename Node_t::Real_t>& InteracList<Node_t>::Perm_C(int l, Mat_Type type, size_t indx){
  292. size_t indx0=InteracClass(type, indx);
  293. Matrix <Real_t>& M0 =mat->Mat (l, type, indx0);
  294. Permutation<Real_t>& col_perm=mat->Perm_C(l, type, indx );
  295. if(M0.Dim(0)==0 || M0.Dim(1)==0) return col_perm;
  296. //Get the necessary permutations.
  297. if(col_perm.Dim()==0){
  298. std::vector<Perm_Type> p_list=PermutList(type, indx);
  299. for(int i=0;i<l;i++) p_list.push_back(Scaling);
  300. Permutation<Real_t> col_perm_=Permutation<Real_t>(M0.Dim(1));
  301. for(int i=0;i<C_Perm;i++){
  302. Permutation<Real_t>& pc=mat->Perm(type, C_Perm + i);
  303. if(!pc.Dim()) col_perm_=Permutation<Real_t>(0);
  304. }
  305. if(col_perm_.Dim()>0)
  306. for(int i=p_list.size()-1; i>=0; i--){
  307. assert(type!=V_Type);
  308. Permutation<Real_t>& pc=mat->Perm(type, C_Perm + p_list[i]);
  309. col_perm_=col_perm_*pc;
  310. }
  311. col_perm=col_perm_;
  312. }
  313. return col_perm;
  314. }
  315. /**
  316. * \brief A hash function defined on the relative coordinates of octants.
  317. */
  318. #define PVFMM_MAX_COORD_HASH 2000
  319. template <class Node_t>
  320. int InteracList<Node_t>::coord_hash(int* c){
  321. const int n=5;
  322. return ( (c[2]+n) * (2*n) + (c[1]+n) ) *(2*n) + (c[0]+n);
  323. }
  324. template <class Node_t>
  325. int InteracList<Node_t>::class_hash(int* c_){
  326. if(!use_symmetries) return coord_hash(c_);
  327. int c[3]={abs(c_[0]), abs(c_[1]), abs(c_[2])};
  328. if(c[1]>c[0] && c[1]>c[2])
  329. {int tmp=c[0]; c[0]=c[1]; c[1]=tmp;}
  330. if(c[0]>c[2])
  331. {int tmp=c[0]; c[0]=c[2]; c[2]=tmp;}
  332. if(c[0]>c[1])
  333. {int tmp=c[0]; c[0]=c[1]; c[1]=tmp;}
  334. assert(c[0]<=c[1] && c[1]<=c[2]);
  335. return coord_hash(&c[0]);
  336. }
  337. /**
  338. * \brief Set relative coordinates of the interacting node in
  339. * rel_coord[Type][idx][1:3].
  340. */
  341. template <class Node_t>
  342. void InteracList<Node_t>::InitList(int max_r, int min_r, int step, Mat_Type t){
  343. size_t count=(size_t)(pow((max_r*2)/step+1.0,(int)dim)-(min_r>0?pow((min_r*2)/step-1.0,(int)dim):0));
  344. Matrix<int>& M=rel_coord[t];
  345. M.Resize(count,dim);
  346. hash_lut[t].assign(PVFMM_MAX_COORD_HASH, -1);
  347. std::vector<int> class_size_hash(PVFMM_MAX_COORD_HASH, 0);
  348. std::vector<int> class_disp_hash(PVFMM_MAX_COORD_HASH, 0);
  349. for(int k=-max_r;k<=max_r;k+=step)
  350. for(int j=-max_r;j<=max_r;j+=step)
  351. for(int i=-max_r;i<=max_r;i+=step)
  352. if(abs(i)>=min_r || abs(j)>=min_r || abs(k) >= min_r){
  353. int c[3]={i,j,k};
  354. class_size_hash[class_hash(c)]++;
  355. }
  356. omp_par::scan(&class_size_hash[0], &class_disp_hash[0], PVFMM_MAX_COORD_HASH);
  357. size_t count_=0;
  358. for(int k=-max_r;k<=max_r;k+=step)
  359. for(int j=-max_r;j<=max_r;j+=step)
  360. for(int i=-max_r;i<=max_r;i+=step)
  361. if(abs(i)>=min_r || abs(j)>=min_r || abs(k) >= min_r){
  362. int c[3]={i,j,k};
  363. int& idx=class_disp_hash[class_hash(c)];
  364. for(size_t l=0;l<dim;l++) M[idx][l]=c[l];
  365. hash_lut[t][coord_hash(c)]=idx;
  366. count_++;
  367. idx++;
  368. }
  369. assert(count_==count);
  370. interac_class[t].resize(count);
  371. perm_list[t].resize(count);
  372. if(!use_symmetries){ // Set interac_class=self
  373. for(size_t j=0;j<count;j++){
  374. int c_hash = coord_hash(&M[j][0]);
  375. interac_class[t][j]=hash_lut[t][c_hash];
  376. }
  377. }
  378. else for(size_t j=0;j<count;j++){
  379. if(M[j][0]<0) perm_list[t][j].push_back(ReflecX);
  380. if(M[j][1]<0) perm_list[t][j].push_back(ReflecY);
  381. if(M[j][2]<0) perm_list[t][j].push_back(ReflecZ);
  382. int coord[3];
  383. coord[0]=abs(M[j][0]);
  384. coord[1]=abs(M[j][1]);
  385. coord[2]=abs(M[j][2]);
  386. if(coord[1]>coord[0] && coord[1]>coord[2]){
  387. perm_list[t][j].push_back(SwapXY);
  388. int tmp=coord[0]; coord[0]=coord[1]; coord[1]=tmp;
  389. }
  390. if(coord[0]>coord[2]){
  391. perm_list[t][j].push_back(SwapXZ);
  392. int tmp=coord[0]; coord[0]=coord[2]; coord[2]=tmp;
  393. }
  394. if(coord[0]>coord[1]){
  395. perm_list[t][j].push_back(SwapXY);
  396. int tmp=coord[0]; coord[0]=coord[1]; coord[1]=tmp;
  397. }
  398. assert(coord[0]<=coord[1] && coord[1]<=coord[2]);
  399. int c_hash = coord_hash(&coord[0]);
  400. interac_class[t][j]=hash_lut[t][c_hash];
  401. }
  402. }
  403. #undef PVFMM_MAX_COORD_HASH
  404. }//end namespace