interac_list.txx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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,dim);
  84. int n_child=(int)pow(2,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(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. row_perm=Permutation<Real_t>(M0.Dim(0));
  275. for(int i=p_list.size()-1; i>=0; i--){
  276. Permutation<Real_t>& pr=mat->Perm(type, R_Perm + p_list[i]);
  277. if(pr.Dim()!=M0.Dim(0)){
  278. row_perm=Permutation<Real_t>();
  279. break;
  280. }
  281. row_perm=pr.Transpose()*row_perm;
  282. }
  283. }
  284. return row_perm;
  285. }
  286. template <class Node_t>
  287. Permutation<typename Node_t::Real_t>& InteracList<Node_t>::Perm_C(int l, Mat_Type type, size_t indx){
  288. size_t indx0=InteracClass(type, indx);
  289. Matrix<Real_t>& M0=mat->Mat(l, type, indx0);
  290. Permutation<Real_t>& col_perm=mat->Perm_C(type, indx);
  291. if(M0.Dim(0)==0 || M0.Dim(1)==0) return col_perm;
  292. //Get the necessary permutations.
  293. if(col_perm.Dim()==0){
  294. std::vector<Perm_Type>& p_list=PermutList(type, indx);
  295. col_perm=Permutation<Real_t>(M0.Dim(1));
  296. for(int i=p_list.size()-1; i>=0; i--){
  297. Permutation<Real_t>& pc=mat->Perm(type, C_Perm + p_list[i]);
  298. if(pc.Dim()!=M0.Dim(1)){
  299. col_perm=Permutation<Real_t>();
  300. break;
  301. }
  302. col_perm=col_perm*pc;
  303. }
  304. }
  305. return col_perm;
  306. }
  307. /**
  308. * \brief A hash function defined on the relative coordinates of octants.
  309. */
  310. #define PVFMM_MAX_COORD_HASH 2000
  311. template <class Node_t>
  312. int InteracList<Node_t>::coord_hash(int* c){
  313. const int n=5;
  314. return ( (c[2]+n) * (2*n) + (c[1]+n) ) *(2*n) + (c[0]+n);
  315. }
  316. template <class Node_t>
  317. int InteracList<Node_t>::class_hash(int* c_){
  318. if(!use_symmetries) return coord_hash(c_);
  319. int c[3]={abs(c_[0]), abs(c_[1]), abs(c_[2])};
  320. if(c[1]>c[0] && c[1]>c[2])
  321. {int tmp=c[0]; c[0]=c[1]; c[1]=tmp;}
  322. if(c[0]>c[2])
  323. {int tmp=c[0]; c[0]=c[2]; c[2]=tmp;}
  324. if(c[0]>c[1])
  325. {int tmp=c[0]; c[0]=c[1]; c[1]=tmp;}
  326. assert(c[0]<=c[1] && c[1]<=c[2]);
  327. return coord_hash(&c[0]);
  328. }
  329. /**
  330. * \brief Set relative coordinates of the interacting node in
  331. * rel_coord[Type][idx][1:3].
  332. */
  333. template <class Node_t>
  334. void InteracList<Node_t>::InitList(int max_r, int min_r, int step, Mat_Type t){
  335. size_t count=(size_t)(pow((max_r*2)/step+1,dim)-(min_r>0?pow((min_r*2)/step-1,dim):0));
  336. Matrix<int>& M=rel_coord[t];
  337. M.Resize(count,dim);
  338. hash_lut[t].assign(PVFMM_MAX_COORD_HASH, -1);
  339. std::vector<int> class_size_hash(PVFMM_MAX_COORD_HASH, 0);
  340. std::vector<int> class_disp_hash(PVFMM_MAX_COORD_HASH, 0);
  341. for(int k=-max_r;k<=max_r;k+=step)
  342. for(int j=-max_r;j<=max_r;j+=step)
  343. for(int i=-max_r;i<=max_r;i+=step)
  344. if(abs(i)>=min_r || abs(j)>=min_r || abs(k) >= min_r){
  345. int c[3]={i,j,k};
  346. class_size_hash[class_hash(c)]++;
  347. }
  348. omp_par::scan(&class_size_hash[0], &class_disp_hash[0], PVFMM_MAX_COORD_HASH);
  349. size_t count_=0;
  350. for(int k=-max_r;k<=max_r;k+=step)
  351. for(int j=-max_r;j<=max_r;j+=step)
  352. for(int i=-max_r;i<=max_r;i+=step)
  353. if(abs(i)>=min_r || abs(j)>=min_r || abs(k) >= min_r){
  354. int c[3]={i,j,k};
  355. int& idx=class_disp_hash[class_hash(c)];
  356. for(size_t l=0;l<dim;l++) M[idx][l]=c[l];
  357. hash_lut[t][coord_hash(c)]=idx;
  358. count_++;
  359. idx++;
  360. }
  361. assert(count_==count);
  362. interac_class[t].resize(count);
  363. perm_list[t].resize(count);
  364. if(!use_symmetries){ // Set interac_class=self
  365. for(size_t j=0;j<count;j++){
  366. int c_hash = coord_hash(&M[j][0]);
  367. interac_class[t][j]=hash_lut[t][c_hash];
  368. }
  369. }
  370. else for(size_t j=0;j<count;j++){
  371. if(M[j][0]<0) perm_list[t][j].push_back(ReflecX);
  372. if(M[j][1]<0) perm_list[t][j].push_back(ReflecY);
  373. if(M[j][2]<0) perm_list[t][j].push_back(ReflecZ);
  374. int coord[3];
  375. coord[0]=abs(M[j][0]);
  376. coord[1]=abs(M[j][1]);
  377. coord[2]=abs(M[j][2]);
  378. if(coord[1]>coord[0] && coord[1]>coord[2]){
  379. perm_list[t][j].push_back(SwapXY);
  380. int tmp=coord[0]; coord[0]=coord[1]; coord[1]=tmp;
  381. }
  382. if(coord[0]>coord[2]){
  383. perm_list[t][j].push_back(SwapXZ);
  384. int tmp=coord[0]; coord[0]=coord[2]; coord[2]=tmp;
  385. }
  386. if(coord[0]>coord[1]){
  387. perm_list[t][j].push_back(SwapXY);
  388. int tmp=coord[0]; coord[0]=coord[1]; coord[1]=tmp;
  389. }
  390. assert(coord[0]<=coord[1] && coord[1]<=coord[2]);
  391. int c_hash = coord_hash(&coord[0]);
  392. interac_class[t][j]=hash_lut[t][c_hash];
  393. }
  394. }
  395. #undef PVFMM_MAX_COORD_HASH
  396. }//end namespace