interac_list.txx 11 KB

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