tree.hpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. #ifndef _SCTL_TREE_
  2. #define _SCTL_TREE_
  3. #include SCTL_INCLUDE(common.hpp)
  4. #include SCTL_INCLUDE(morton.hpp)
  5. #include SCTL_INCLUDE(comm.hpp)
  6. #include <fstream>
  7. #include <algorithm>
  8. namespace SCTL_NAMESPACE {
  9. struct VTUData {
  10. typedef float VTKReal;
  11. // Point data
  12. Vector<VTKReal> coord; // always 3D
  13. Vector<VTKReal> value;
  14. // Cell data
  15. Vector<int32_t> connect;
  16. Vector<int32_t> offset;
  17. Vector<uint8_t> types;
  18. void WriteVTK(const std::string& fname, Comm comm = Comm::Self()) const {
  19. typedef typename VTUData::VTKReal VTKReal;
  20. Integer rank = comm.Rank();
  21. Integer np = comm.Size();
  22. Long value_dof = 0;
  23. { // Write vtu file.
  24. std::ofstream vtufile;
  25. { // Open file for writing.
  26. std::stringstream vtufname;
  27. vtufname << fname << std::setfill('0') << std::setw(6) << rank << ".vtu";
  28. vtufile.open(vtufname.str().c_str());
  29. if (vtufile.fail()) return;
  30. }
  31. { // Write to file.
  32. Long pt_cnt = coord.Dim() / 3;
  33. Long cell_cnt = types.Dim();
  34. value_dof = (pt_cnt ? value.Dim() / pt_cnt : 0);
  35. Vector<int32_t> mpi_rank;
  36. { // Set mpi_rank
  37. Integer new_myrank = rank;
  38. mpi_rank.ReInit(pt_cnt);
  39. for (Long i = 0; i < mpi_rank.Dim(); i++) mpi_rank[i] = new_myrank;
  40. }
  41. bool isLittleEndian;
  42. { // Set isLittleEndian
  43. uint16_t number = 0x1;
  44. uint8_t *numPtr = (uint8_t *)&number;
  45. isLittleEndian = (numPtr[0] == 1);
  46. }
  47. Long data_size = 0;
  48. vtufile << "<?xml version=\"1.0\"?>\n";
  49. vtufile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"" << (isLittleEndian ? "LittleEndian" : "BigEndian") << "\">\n";
  50. // ===========================================================================
  51. vtufile << " <UnstructuredGrid>\n";
  52. vtufile << " <Piece NumberOfPoints=\"" << pt_cnt << "\" NumberOfCells=\"" << cell_cnt << "\">\n";
  53. //---------------------------------------------------------------------------
  54. vtufile << " <Points>\n";
  55. vtufile << " <DataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"3\" Name=\"Position\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  56. data_size += sizeof(uint32_t) + coord.Dim() * sizeof(VTKReal);
  57. vtufile << " </Points>\n";
  58. //---------------------------------------------------------------------------
  59. vtufile << " <PointData>\n";
  60. if (value_dof) { // value
  61. vtufile << " <DataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"" << value_dof << "\" Name=\"value\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  62. data_size += sizeof(uint32_t) + value.Dim() * sizeof(VTKReal);
  63. }
  64. { // mpi_rank
  65. vtufile << " <DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"mpi_rank\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  66. data_size += sizeof(uint32_t) + pt_cnt * sizeof(int32_t);
  67. }
  68. vtufile << " </PointData>\n";
  69. //---------------------------------------------------------------------------
  70. //---------------------------------------------------------------------------
  71. vtufile << " <Cells>\n";
  72. vtufile << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  73. data_size += sizeof(uint32_t) + connect.Dim() * sizeof(int32_t);
  74. vtufile << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  75. data_size += sizeof(uint32_t) + offset.Dim() * sizeof(int32_t);
  76. vtufile << " <DataArray type=\"UInt8\" Name=\"types\" format=\"appended\" offset=\"" << data_size << "\" />\n";
  77. data_size += sizeof(uint32_t) + types.Dim() * sizeof(uint8_t);
  78. vtufile << " </Cells>\n";
  79. //---------------------------------------------------------------------------
  80. vtufile << " </Piece>\n";
  81. vtufile << " </UnstructuredGrid>\n";
  82. // ===========================================================================
  83. vtufile << " <AppendedData encoding=\"raw\">\n";
  84. vtufile << " _";
  85. int32_t block_size;
  86. { // coord
  87. block_size = coord.Dim() * sizeof(VTKReal);
  88. vtufile.write((char *)&block_size, sizeof(int32_t));
  89. if (coord.Dim()) vtufile.write((char *)&coord[0], coord.Dim() * sizeof(VTKReal));
  90. }
  91. if (value_dof) { // value
  92. block_size = value.Dim() * sizeof(VTKReal);
  93. vtufile.write((char *)&block_size, sizeof(int32_t));
  94. if (value.Dim()) vtufile.write((char *)&value[0], value.Dim() * sizeof(VTKReal));
  95. }
  96. { // mpi_rank
  97. block_size = mpi_rank.Dim() * sizeof(int32_t);
  98. vtufile.write((char *)&block_size, sizeof(int32_t));
  99. if (mpi_rank.Dim()) vtufile.write((char *)&mpi_rank[0], mpi_rank.Dim() * sizeof(int32_t));
  100. }
  101. { // block_size
  102. block_size = connect.Dim() * sizeof(int32_t);
  103. vtufile.write((char *)&block_size, sizeof(int32_t));
  104. if (connect.Dim()) vtufile.write((char *)&connect[0], connect.Dim() * sizeof(int32_t));
  105. }
  106. { // offset
  107. block_size = offset.Dim() * sizeof(int32_t);
  108. vtufile.write((char *)&block_size, sizeof(int32_t));
  109. if (offset.Dim()) vtufile.write((char *)&offset[0], offset.Dim() * sizeof(int32_t));
  110. }
  111. { // types
  112. block_size = types.Dim() * sizeof(uint8_t);
  113. vtufile.write((char *)&block_size, sizeof(int32_t));
  114. if (types.Dim()) vtufile.write((char *)&types[0], types.Dim() * sizeof(uint8_t));
  115. }
  116. vtufile << "\n";
  117. vtufile << " </AppendedData>\n";
  118. // ===========================================================================
  119. vtufile << "</VTKFile>\n";
  120. }
  121. vtufile.close(); // close file
  122. }
  123. if (!rank) { // Write pvtu file
  124. std::ofstream pvtufile;
  125. { // Open file for writing
  126. std::stringstream pvtufname;
  127. pvtufname << fname << ".pvtu";
  128. pvtufile.open(pvtufname.str().c_str());
  129. if (pvtufile.fail()) return;
  130. }
  131. { // Write to file.
  132. pvtufile << "<?xml version=\"1.0\"?>\n";
  133. pvtufile << "<VTKFile type=\"PUnstructuredGrid\">\n";
  134. pvtufile << " <PUnstructuredGrid GhostLevel=\"0\">\n";
  135. pvtufile << " <PPoints>\n";
  136. pvtufile << " <PDataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"3\" Name=\"Position\"/>\n";
  137. pvtufile << " </PPoints>\n";
  138. pvtufile << " <PPointData>\n";
  139. if (value_dof) { // value
  140. pvtufile << " <PDataArray type=\"Float" << sizeof(VTKReal) * 8 << "\" NumberOfComponents=\"" << value_dof << "\" Name=\"value\"/>\n";
  141. }
  142. { // mpi_rank
  143. pvtufile << " <PDataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"mpi_rank\"/>\n";
  144. }
  145. pvtufile << " </PPointData>\n";
  146. {
  147. // Extract filename from path.
  148. std::stringstream vtupath;
  149. vtupath << '/' << fname;
  150. std::string pathname = vtupath.str();
  151. std::string fname_ = pathname.substr(pathname.find_last_of("/\\") + 1);
  152. // char *fname_ = (char*)strrchr(vtupath.str().c_str(), '/') + 1;
  153. // std::string fname_ =
  154. // boost::filesystem::path(fname).filename().string().
  155. for (Integer i = 0; i < np; i++) pvtufile << " <Piece Source=\"" << fname_ << std::setfill('0') << std::setw(6) << i << ".vtu\"/>\n";
  156. }
  157. pvtufile << " </PUnstructuredGrid>\n";
  158. pvtufile << "</VTKFile>\n";
  159. }
  160. pvtufile.close(); // close file
  161. }
  162. };
  163. };
  164. template <class Real, Integer DIM> class Tree {
  165. public:
  166. struct NodeAttr {
  167. unsigned char Leaf : 1, Ghost : 1;
  168. };
  169. //struct PtrTree {
  170. // Long p2n;
  171. // Long parent;
  172. // Long child[1 << DIM];
  173. // Long nbr[pvfmm::pow<DIM>(3)];
  174. //};
  175. static constexpr Integer Dim() {
  176. return DIM;
  177. }
  178. Tree(const Comm& comm_ = Comm::Self()) {
  179. comm = comm_;
  180. Integer rank = comm.Rank();
  181. Integer np = comm.Size();
  182. Vector<Real> coord;
  183. { // Set coord
  184. Long N0 = 1;
  185. while (sctl::pow<DIM,Long>(N0) < np) N0++;
  186. Long N = sctl::pow<DIM,Long>(N0);
  187. Long start = N * (rank+0) / np;
  188. Long end = N * (rank+1) / np;
  189. coord.ReInit((end-start)*DIM);
  190. for (Long i = start; i < end; i++) {
  191. Long idx = i;
  192. for (Integer k = 0; k < DIM; k++) {
  193. coord[(i-start)*DIM+k] = (idx % N0) / (Real)N0;
  194. idx /= N0;
  195. }
  196. }
  197. }
  198. this->UpdateRefinement(coord);
  199. }
  200. ~Tree() {
  201. #ifdef SCTL_MEMDEBUG
  202. for (auto& pair : node_data) {
  203. SCTL_ASSERT(node_cnt.find(pair.first) != node_cnt.end());
  204. }
  205. #endif
  206. }
  207. const Vector<Morton<DIM>>& GetPartitionMID() const {
  208. return mins;
  209. }
  210. const Vector<Morton<DIM>>& GetNodeMID() const {
  211. return node_mid;
  212. }
  213. const Vector<NodeAttr>& GetNodeAttr() const {
  214. return node_attr;
  215. }
  216. const Comm& GetComm() const {
  217. return comm;
  218. }
  219. void UpdateRefinement(const Vector<Real>& coord, Long M = 1, bool balance21 = 0, bool periodic = 0) {
  220. Integer np = comm.Size();
  221. Integer rank = comm.Rank();
  222. Vector<Morton<DIM>> node_mid_orig;
  223. Long start_idx_orig, end_idx_orig;
  224. if (mins.Dim()) { // Set start_idx_orig, end_idx_orig
  225. start_idx_orig = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  226. end_idx_orig = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  227. node_mid_orig.ReInit(end_idx_orig - start_idx_orig, node_mid.begin() + start_idx_orig, true);
  228. } else {
  229. start_idx_orig = 0;
  230. end_idx_orig = 0;
  231. }
  232. auto coarsest_ancestor_mid = [](const Morton<DIM>& m0) {
  233. Morton<DIM> md;
  234. Integer d0 = m0.Depth();
  235. for (Integer d = 0; d <= d0; d++) {
  236. md = m0.Ancestor(d);
  237. if (md.Ancestor(d0) == m0) break;
  238. }
  239. return md;
  240. };
  241. Morton<DIM> pt_mid0;
  242. Vector<Morton<DIM>> pt_mid;
  243. { // Construct sorted pt_mid
  244. Long Npt = coord.Dim() / DIM;
  245. pt_mid.ReInit(Npt);
  246. for (Long i = 0; i < Npt; i++) {
  247. pt_mid[i] = Morton<DIM>(coord.begin() + i*DIM);
  248. }
  249. Vector<Morton<DIM>> sorted_mid;
  250. comm.HyperQuickSort(pt_mid, sorted_mid);
  251. pt_mid.Swap(sorted_mid);
  252. SCTL_ASSERT(pt_mid.Dim());
  253. pt_mid0 = pt_mid[0];
  254. }
  255. { // Update M = global_min(pt_mid.Dim(), M)
  256. Long M0, M1, Npt = pt_mid.Dim();
  257. comm.Allreduce(Ptr2ConstItr<Long>(&M,1), Ptr2Itr<Long>(&M0,1), 1, Comm::CommOp::MIN);
  258. comm.Allreduce(Ptr2ConstItr<Long>(&Npt,1), Ptr2Itr<Long>(&M1,1), 1, Comm::CommOp::MIN);
  259. M = std::min(M0,M1);
  260. SCTL_ASSERT(M > 0);
  261. }
  262. { // pt_mid <-- [M points from rank-1; pt_mid; M points from rank+1]
  263. Long send_size0 = (rank+1<np ? M : 0);
  264. Long send_size1 = (rank > 0 ? M : 0);
  265. Long recv_size0 = (rank > 0 ? M : 0);
  266. Long recv_size1 = (rank+1<np ? M : 0);
  267. Vector<Morton<DIM>> pt_mid_(recv_size0 + pt_mid.Dim() + recv_size1);
  268. memcopy(pt_mid_.begin()+recv_size0, pt_mid.begin(), pt_mid.Dim());
  269. void* recv_req0 = comm.Irecv(pt_mid_.begin(), recv_size0, (rank+np-1)%np, 0);
  270. void* recv_req1 = comm.Irecv(pt_mid_.begin() + recv_size0 + pt_mid.Dim(), recv_size1, (rank+1)%np, 1);
  271. void* send_req0 = comm.Isend(pt_mid .begin() + pt_mid.Dim() - send_size0, send_size0, (rank+1)%np, 0);
  272. void* send_req1 = comm.Isend(pt_mid .begin(), send_size1, (rank+np-1)%np, 1);
  273. comm.Wait(recv_req0);
  274. comm.Wait(recv_req1);
  275. comm.Wait(send_req0);
  276. comm.Wait(send_req1);
  277. pt_mid.Swap(pt_mid_);
  278. }
  279. { // Build linear MortonID tree from pt_mid
  280. node_mid.ReInit(0);
  281. Long idx = 0;
  282. Morton<DIM> m0;
  283. Morton<DIM> mend = Morton<DIM>().Next();
  284. while (m0 < mend) {
  285. Integer d = m0.Depth();
  286. Morton<DIM> m1 = (idx + M < pt_mid.Dim() ? pt_mid[idx+M] : Morton<DIM>().Next());
  287. while (d < Morton<DIM>::MAX_DEPTH && m0.Ancestor(d) == m1.Ancestor(d)) {
  288. node_mid.PushBack(m0.Ancestor(d));
  289. d++;
  290. }
  291. m0 = m0.Ancestor(d);
  292. node_mid.PushBack(m0);
  293. m0 = m0.Next();
  294. idx = std::lower_bound(pt_mid.begin(), pt_mid.end(), m0) - pt_mid.begin();
  295. }
  296. }
  297. { // Set mins
  298. mins.ReInit(np);
  299. Long min_idx = std::lower_bound(node_mid.begin(), node_mid.end(), pt_mid0) - node_mid.begin() - 1;
  300. if (!rank || min_idx < 0) min_idx = 0;
  301. Morton<DIM> m0 = coarsest_ancestor_mid(node_mid[min_idx]);
  302. comm.Allgather(Ptr2ConstItr<Morton<DIM>>(&m0,1), 1, mins.begin(), 1);
  303. }
  304. if (balance21) { // 2:1 balance refinement // TODO: optimize
  305. Vector<Morton<DIM>> parent_mid;
  306. { // add balancing Morton IDs
  307. Vector<std::set<Morton<DIM>>> parent_mid_set(Morton<DIM>::MAX_DEPTH+1);
  308. Vector<Morton<DIM>> nlst;
  309. for (const auto& m0 : node_mid) {
  310. Integer d0 = m0.Depth();
  311. parent_mid_set[m0.Depth()].insert(m0.Ancestor(d0-1));
  312. }
  313. for (Integer d = Morton<DIM>::MAX_DEPTH; d > 0; d--) {
  314. for (const auto& m : parent_mid_set[d]) {
  315. m.NbrList(nlst, d-1, periodic);
  316. parent_mid_set[d-1].insert(nlst.begin(), nlst.end());
  317. parent_mid.PushBack(m);
  318. }
  319. }
  320. }
  321. Vector<Morton<DIM>> parent_mid_sorted;
  322. { // sort and repartition
  323. comm.HyperQuickSort(parent_mid, parent_mid_sorted);
  324. comm.PartitionS(parent_mid_sorted, mins[comm.Rank()]);
  325. }
  326. Vector<Morton<DIM>> tmp_mid;
  327. { // add children
  328. Vector<Morton<DIM>> clst;
  329. tmp_mid.PushBack(Morton<DIM>()); // include root node
  330. for (Long i = 0; i < parent_mid_sorted.Dim(); i++) {
  331. if (i+1 == parent_mid_sorted.Dim() || parent_mid_sorted[i] != parent_mid_sorted[i+1]) {
  332. const auto& m = parent_mid_sorted[i];
  333. tmp_mid.PushBack(m);
  334. m.Children(clst);
  335. for (const auto& c : clst) tmp_mid.PushBack(c);
  336. }
  337. }
  338. auto insert_ancestor_children = [](Vector<Morton<DIM>>& mvec, const Morton<DIM>& m0) {
  339. Integer d0 = m0.Depth();
  340. Vector<Morton<DIM>> clst;
  341. for (Integer d = 0; d < d0; d++) {
  342. m0.Ancestor(d).Children(clst);
  343. for (const auto& m : clst) mvec.PushBack(m);
  344. }
  345. };
  346. insert_ancestor_children(tmp_mid, mins[rank]);
  347. omp_par::merge_sort(tmp_mid.begin(), tmp_mid.end());
  348. }
  349. node_mid.ReInit(0);
  350. for (Long i = 0; i < tmp_mid.Dim(); i++) { // remove duplicates
  351. if (i+1 == tmp_mid.Dim() || tmp_mid[i] != tmp_mid[i+1]) {
  352. node_mid.PushBack(tmp_mid[i]);
  353. }
  354. }
  355. }
  356. { // Add place-holder for ghost nodes
  357. Long start_idx, end_idx;
  358. { // Set start_idx, end_idx
  359. start_idx = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  360. end_idx = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  361. }
  362. { // Set user_node_lst
  363. Vector<SortPair<Long,Morton<DIM>>> user_node_lst;
  364. Vector<Morton<DIM>> nlst;
  365. std::set<Long> user_procs;
  366. for (Long i = start_idx; i < end_idx; i++) {
  367. Morton<DIM> m0 = node_mid[i];
  368. Integer d0 = m0.Depth();
  369. m0.NbrList(nlst, std::max<Integer>(d0-2,0), periodic);
  370. user_procs.clear();
  371. for (const auto& m : nlst) {
  372. Morton<DIM> m_start = m.DFD();
  373. Morton<DIM> m_end = m.Next();
  374. Integer p_start = std::lower_bound(mins.begin(), mins.end(), m_start) - mins.begin() - 1;
  375. Integer p_end = std::lower_bound(mins.begin(), mins.end(), m_end ) - mins.begin();
  376. SCTL_ASSERT(0 <= p_start);
  377. SCTL_ASSERT(p_start < p_end);
  378. SCTL_ASSERT(p_end <= np);
  379. for (Long p = p_start; p < p_end; p++) {
  380. if (p != rank) user_procs.insert(p);
  381. }
  382. }
  383. for (const auto p : user_procs) {
  384. SortPair<Long,Morton<DIM>> pair;
  385. pair.key = p;
  386. pair.data = m0;
  387. user_node_lst.PushBack(pair);
  388. }
  389. }
  390. omp_par::merge_sort(user_node_lst.begin(), user_node_lst.end());
  391. user_cnt.ReInit(np);
  392. user_mid.ReInit(user_node_lst.Dim());
  393. for (Integer i = 0; i < np; i++) {
  394. SortPair<Long,Morton<DIM>> pair_start, pair_end;
  395. pair_start.key = i;
  396. pair_end.key = i+1;
  397. Long cnt_start = std::lower_bound(user_node_lst.begin(), user_node_lst.end(), pair_start) - user_node_lst.begin();
  398. Long cnt_end = std::lower_bound(user_node_lst.begin(), user_node_lst.end(), pair_end ) - user_node_lst.begin();
  399. user_cnt[i] = cnt_end - cnt_start;
  400. for (Long j = cnt_start; j < cnt_end; j++) {
  401. user_mid[j] = user_node_lst[j].data;
  402. }
  403. std::sort(user_mid.begin() + cnt_start, user_mid.begin() + cnt_end);
  404. }
  405. }
  406. Vector<Morton<DIM>> ghost_mid;
  407. { // SendRecv user_node_lst
  408. const Vector<Long> &send_cnt = user_cnt;
  409. Vector<Long> send_dsp(np);
  410. scan(send_dsp, send_cnt);
  411. Vector<Long> recv_cnt(np), recv_dsp(np);
  412. comm.Alltoall(send_cnt.begin(), 1, recv_cnt.begin(), 1);
  413. scan(recv_dsp, recv_cnt);
  414. const Vector<Morton<DIM>>& send_mid = user_mid;
  415. Long Nsend = send_dsp[np-1] + send_cnt[np-1];
  416. Long Nrecv = recv_dsp[np-1] + recv_cnt[np-1];
  417. SCTL_ASSERT(send_mid.Dim() == Nsend);
  418. ghost_mid.ReInit(Nrecv);
  419. comm.Alltoallv(send_mid.begin(), send_cnt.begin(), send_dsp.begin(), ghost_mid.begin(), recv_cnt.begin(), recv_dsp.begin());
  420. }
  421. { // Update node_mid <-- ghost_mid + node_mid
  422. Vector<Morton<DIM>> new_mid(end_idx-start_idx + ghost_mid.Dim());
  423. Long Nsplit = std::lower_bound(ghost_mid.begin(), ghost_mid.end(), mins[rank]) - ghost_mid.begin();
  424. for (Long i = 0; i < Nsplit; i++) {
  425. new_mid[i] = ghost_mid[i];
  426. }
  427. for (Long i = 0; i < end_idx - start_idx; i++) {
  428. new_mid[Nsplit + i] = node_mid[start_idx + i];
  429. }
  430. for (Long i = Nsplit; i < ghost_mid.Dim(); i++) {
  431. new_mid[end_idx - start_idx + i] = ghost_mid[i];
  432. }
  433. node_mid.Swap(new_mid);
  434. }
  435. }
  436. { // Set node_mid, node_attr
  437. Morton<DIM> m0 = (rank ? mins[rank] : Morton<DIM>() );
  438. Morton<DIM> m1 = (rank+1<np ? mins[rank+1] : Morton<DIM>().Next());
  439. Long Nnodes = node_mid.Dim();
  440. node_attr.ReInit(Nnodes);
  441. for (Long i = 0; i < Nnodes; i++) {
  442. node_attr[i].Leaf = !(i+1<Nnodes && node_mid[i].isAncestor(node_mid[i+1]));
  443. node_attr[i].Ghost = (node_mid[i] < m0 || node_mid[i] >= m1);
  444. }
  445. }
  446. { // Check tree
  447. Morton<DIM> m0;
  448. SCTL_ASSERT(node_mid.Dim() && m0 == node_mid[0]);
  449. for (Long i = 1; i < node_mid.Dim(); i++) {
  450. const auto& m = node_mid[i];
  451. if (m0.isAncestor(m)) m0 = m0.Ancestor(m0.Depth()+1);
  452. else m0 = m0.Next();
  453. SCTL_ASSERT(m0 == m);
  454. }
  455. SCTL_ASSERT(m0.Next() == Morton<DIM>().Next());
  456. }
  457. { // Update node_data, node_cnt
  458. Long start_idx, end_idx;
  459. { // Set start_idx, end_idx
  460. start_idx = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  461. end_idx = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  462. }
  463. comm.PartitionS(node_mid_orig, mins[comm.Rank()]);
  464. Vector<Long> new_cnt_range0(node_mid.Dim()), new_cnt_range1(node_mid.Dim());
  465. { // Set new_cnt_range0, new_cnt_range1
  466. for (Long i = 0; i < start_idx; i++) {
  467. new_cnt_range0[i] = 0;
  468. new_cnt_range1[i] = 0;
  469. }
  470. for (Long i = start_idx; i < end_idx; i++) {
  471. auto m0 = (node_mid[i+0]);
  472. auto m1 = (i+1==end_idx ? Morton<DIM>().Next() : (node_mid[i+1]));
  473. new_cnt_range0[i] = std::lower_bound(node_mid_orig.begin(), node_mid_orig.begin() + node_mid_orig.Dim(), m0) - node_mid_orig.begin();
  474. new_cnt_range1[i] = std::lower_bound(node_mid_orig.begin(), node_mid_orig.begin() + node_mid_orig.Dim(), m1) - node_mid_orig.begin();
  475. }
  476. for (Long i = end_idx; i < node_mid.Dim(); i++) {
  477. new_cnt_range0[i] = 0;
  478. new_cnt_range1[i] = 0;
  479. }
  480. }
  481. Vector<Long> cnt_tmp;
  482. Vector<Real> data_tmp;
  483. for (const auto& pair : node_data) {
  484. const std::string& data_name = pair.first;
  485. Long dof = 0;
  486. Iterator<Vector<Real>> data_;
  487. Iterator<Vector<Long>> cnt_;
  488. GetData(data_, cnt_, data_name);
  489. { // Set dof
  490. StaticArray<Long,2> Nl, Ng;
  491. Nl[0] = data_->Dim();
  492. Nl[1] = omp_par::reduce(cnt_->begin(), cnt_->Dim());
  493. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  494. if (Ng[1]) dof = Ng[0] / Ng[1];
  495. SCTL_ASSERT(Nl[0] == Nl[1] * dof);
  496. SCTL_ASSERT(Ng[0] == Ng[1] * dof);
  497. }
  498. Long data_dsp = omp_par::reduce(cnt_->begin(), start_idx_orig);
  499. Long data_cnt = omp_par::reduce(cnt_->begin() + start_idx_orig, end_idx_orig - start_idx_orig);
  500. data_tmp.ReInit(data_cnt * dof, data_->begin() + data_dsp * dof, true);
  501. cnt_tmp.ReInit(end_idx_orig - start_idx_orig, cnt_->begin() + start_idx_orig, true);
  502. comm.PartitionN(cnt_tmp, node_mid_orig.Dim());
  503. cnt_->ReInit(node_mid.Dim());
  504. for (Long i = 0; i < node_mid.Dim(); i++) {
  505. Long sum = 0;
  506. Long j0 = new_cnt_range0[i];
  507. Long j1 = new_cnt_range1[i];
  508. for (Long j = j0; j < j1; j++) sum += cnt_tmp[j];
  509. cnt_[0][i] = sum;
  510. }
  511. SCTL_ASSERT(omp_par::reduce(cnt_->begin(), cnt_->Dim()) == omp_par::reduce(cnt_tmp.begin(), cnt_tmp.Dim()));
  512. Long Ndata = omp_par::reduce(cnt_->begin(), cnt_->Dim()) * dof;
  513. comm.PartitionN(data_tmp, Ndata);
  514. SCTL_ASSERT(data_tmp.Dim() == Ndata);
  515. data_->Swap(data_tmp);
  516. }
  517. }
  518. }
  519. void AddData(const std::string& name, const Vector<Real>& data, const Vector<Long>& cnt) {
  520. Long dof = 0;
  521. { // Check dof
  522. StaticArray<Long,2> Nl, Ng;
  523. Nl[0] = data.Dim();
  524. Nl[1] = omp_par::reduce(cnt.begin(), cnt.Dim());
  525. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  526. if (Ng[1]) dof = Ng[0] / Ng[1];
  527. SCTL_ASSERT(Nl[0] == Nl[1] * dof);
  528. SCTL_ASSERT(Ng[0] == Ng[1] * dof);
  529. }
  530. if (dof) SCTL_ASSERT(cnt.Dim() == node_mid.Dim());
  531. SCTL_ASSERT(node_data.find(name) == node_data.end());
  532. node_data[name] = data;
  533. node_cnt [name] = cnt;
  534. }
  535. void GetData(Iterator<Vector<Real>>& data, ConstIterator<Vector<Long>>& cnt, const std::string& name) {
  536. auto data_ = node_data.find(name);
  537. const auto cnt_ = node_cnt.find(name);
  538. SCTL_ASSERT(data_ != node_data.end());
  539. SCTL_ASSERT( cnt_ != node_cnt .end());
  540. data = Ptr2Itr<Vector<Real>>(&data_->second,1);
  541. cnt = Ptr2ConstItr<Vector<Long>>(& cnt_->second,1);
  542. }
  543. void GetData(ConstIterator<Vector<Real>>& data, ConstIterator<Vector<Long>>& cnt, const std::string& name) const {
  544. const auto data_ = node_data.find(name);
  545. const auto cnt_ = node_cnt.find(name);
  546. SCTL_ASSERT(data_ != node_data.end());
  547. SCTL_ASSERT( cnt_ != node_cnt .end());
  548. data = Ptr2ConstItr<Vector<Real>>(&data_->second,1);
  549. cnt = Ptr2ConstItr<Vector<Long>>(& cnt_->second,1);
  550. }
  551. void DeleteData(const std::string& name) {
  552. SCTL_ASSERT(node_data.find(name) != node_data.end());
  553. SCTL_ASSERT(node_cnt .find(name) != node_cnt .end());
  554. node_data.erase(name);
  555. node_cnt .erase(name);
  556. }
  557. void WriteTreeVTK(std::string fname, bool show_ghost = false) const {
  558. typedef typename VTUData::VTKReal VTKReal;
  559. VTUData vtu_data;
  560. if (DIM <= 3) { // Set vtu data
  561. static const Integer Ncorner = (1u << DIM);
  562. Vector<VTKReal> &coord = vtu_data.coord;
  563. //Vector<VTKReal> &value = vtu_data.value;
  564. Vector<int32_t> &connect = vtu_data.connect;
  565. Vector<int32_t> &offset = vtu_data.offset;
  566. Vector<uint8_t> &types = vtu_data.types;
  567. StaticArray<VTKReal, DIM> c;
  568. Long point_cnt = coord.Dim() / 3;
  569. Long connect_cnt = connect.Dim();
  570. for (Long nid = 0; nid < node_mid.Dim(); nid++) {
  571. const Morton<DIM> &mid = node_mid[nid];
  572. const NodeAttr &attr = node_attr[nid];
  573. if (!show_ghost && attr.Ghost) continue;
  574. if (!attr.Leaf) continue;
  575. mid.Coord((Iterator<VTKReal>)c);
  576. VTKReal s = sctl::pow<VTKReal>(0.5, mid.Depth());
  577. for (Integer j = 0; j < Ncorner; j++) {
  578. for (Integer i = 0; i < DIM; i++) coord.PushBack(c[i] + (j & (1u << i) ? 1 : 0) * s);
  579. for (Integer i = DIM; i < 3; i++) coord.PushBack(0);
  580. connect.PushBack(point_cnt);
  581. connect_cnt++;
  582. point_cnt++;
  583. }
  584. offset.PushBack(connect_cnt);
  585. if (DIM == 2)
  586. types.PushBack(8);
  587. else if (DIM == 3)
  588. types.PushBack(11);
  589. else
  590. types.PushBack(4);
  591. }
  592. }
  593. vtu_data.WriteVTK(fname, comm);
  594. }
  595. protected:
  596. void GetData(Iterator<Vector<Real>>& data, Iterator<Vector<Long>>& cnt, const std::string& name) {
  597. auto data_ = node_data.find(name);
  598. const auto cnt_ = node_cnt.find(name);
  599. SCTL_ASSERT(data_ != node_data.end());
  600. SCTL_ASSERT( cnt_ != node_cnt .end());
  601. data = Ptr2Itr<Vector<Real>>(&data_->second,1);
  602. cnt = Ptr2Itr<Vector<Long>>(& cnt_->second,1);
  603. }
  604. static void scan(Vector<Long>& dsp, const Vector<Long>& cnt) {
  605. dsp.ReInit(cnt.Dim());
  606. if (cnt.Dim()) dsp[0] = 0;
  607. omp_par::scan(cnt.begin(), dsp.begin(), cnt.Dim());
  608. }
  609. template <typename A, typename B> struct SortPair {
  610. int operator<(const SortPair<A, B> &p1) const { return key < p1.key; }
  611. A key;
  612. B data;
  613. };
  614. private:
  615. Vector<Morton<DIM>> mins;
  616. Vector<Morton<DIM>> node_mid;
  617. Vector<NodeAttr> node_attr;
  618. std::map<std::string, Vector<Real>> node_data;
  619. std::map<std::string, Vector<Long>> node_cnt;
  620. Vector<Morton<DIM>> user_mid;
  621. Vector<Long> user_cnt;
  622. Comm comm;
  623. };
  624. template <class Real, Integer DIM, class BaseTree = Tree<Real,DIM>> class PtTree : public BaseTree {
  625. public:
  626. PtTree(const Comm& comm = Comm::Self()) : BaseTree(comm) {}
  627. ~PtTree() {
  628. #ifdef SCTL_MEMDEBUG
  629. for (auto& pair : data_pt_name) {
  630. ConstIterator<Vector<Real>> data;
  631. ConstIterator<Vector<Long>> cnt;
  632. this->GetData(data, cnt, pair.second);
  633. SCTL_ASSERT(scatter_idx.find(pair.second) != scatter_idx.end());
  634. }
  635. #endif
  636. }
  637. void UpdateRefinement(const Vector<Real>& coord, Long M = 1, bool balance21 = 0, bool periodic = 0) {
  638. const auto& comm = this->GetComm();
  639. BaseTree::UpdateRefinement(coord, M, balance21, periodic);
  640. Long start_node_idx, end_node_idx;
  641. { // Set start_node_idx, end_node_idx
  642. const auto& mins = this->GetPartitionMID();
  643. const auto& node_mid = this->GetNodeMID();
  644. Integer np = comm.Size();
  645. Integer rank = comm.Rank();
  646. start_node_idx = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  647. end_node_idx = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  648. }
  649. const auto& mins = this->GetPartitionMID();
  650. const auto& node_mid = this->GetNodeMID();
  651. for (const auto& pair : pt_mid) {
  652. const auto& pt_name = pair.first;
  653. auto& pt_mid_ = pt_mid[pt_name];
  654. auto& scatter_idx_ = scatter_idx[pt_name];
  655. comm.PartitionS(pt_mid_, mins[comm.Rank()]);
  656. comm.PartitionN(scatter_idx_, pt_mid_.Dim());
  657. Vector<Long> pt_cnt(node_mid.Dim());
  658. for (Long i = 0; i < node_mid.Dim(); i++) { // Set pt_cnt
  659. Long start = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), node_mid[i]) - pt_mid_.begin();
  660. Long end = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), (i+1==node_mid.Dim() ? Morton<DIM>().Next() : node_mid[i+1])) - pt_mid_.begin();
  661. if (i == 0) SCTL_ASSERT(start == 0);
  662. if (i+1 == node_mid.Dim()) SCTL_ASSERT(end == pt_mid_.Dim());
  663. pt_cnt[i] = end - start;
  664. }
  665. for (const auto& pair : data_pt_name) {
  666. if (pair.second == pt_name) {
  667. const auto& data_name = pair.first;
  668. Iterator<Vector<Real>> data;
  669. Iterator<Vector<Long>> cnt;
  670. this->GetData(data, cnt, data_name);
  671. { // Update data
  672. Long dof = 0;
  673. { // Set dof
  674. StaticArray<Long,2> Nl = {0, 0}, Ng;
  675. Nl[0] = data->Dim();
  676. for (Long i = 0; i < cnt->Dim(); i++) Nl[1] += cnt[0][i];
  677. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  678. dof = Ng[0] / std::max<Long>(Ng[1],1);
  679. }
  680. Long offset = 0, count = 0;
  681. SCTL_ASSERT(0 <= start_node_idx);
  682. SCTL_ASSERT(start_node_idx <= end_node_idx);
  683. SCTL_ASSERT(end_node_idx <= cnt->Dim());
  684. for (Long i = 0; i < start_node_idx; i++) offset += cnt[0][i];
  685. for (Long i = start_node_idx; i < end_node_idx; i++) count += cnt[0][i];
  686. offset *= dof;
  687. count *= dof;
  688. Vector<Real> data_(count, data->begin() + offset);
  689. comm.PartitionN(data_, pt_mid_.Dim());
  690. data->Swap(data_);
  691. }
  692. cnt[0] = pt_cnt;
  693. }
  694. }
  695. }
  696. }
  697. void AddParticles(const std::string& name, const Vector<Real>& coord) {
  698. const auto& mins = this->GetPartitionMID();
  699. const auto& node_mid = this->GetNodeMID();
  700. const auto& comm = this->GetComm();
  701. SCTL_ASSERT(scatter_idx.find(name) == scatter_idx.end());
  702. Vector<Long>& scatter_idx_ = scatter_idx[name];
  703. Long N = coord.Dim() / DIM;
  704. SCTL_ASSERT(coord.Dim() == N * DIM);
  705. Nlocal[name] = N;
  706. Vector<Morton<DIM>>& pt_mid_ = pt_mid[name];
  707. if (pt_mid_.Dim() != N) pt_mid_.ReInit(N);
  708. for (Long i = 0; i < N; i++) {
  709. pt_mid_[i] = Morton<DIM>(coord.begin() + i*DIM);
  710. }
  711. comm.SortScatterIndex(pt_mid_, scatter_idx_, &mins[comm.Rank()]);
  712. comm.ScatterForward(pt_mid_, scatter_idx_);
  713. AddParticleData(name, name, coord);
  714. { // Set node_cnt
  715. Iterator<Vector<Real>> data_;
  716. Iterator<Vector<Long>> cnt_;
  717. this->GetData(data_,cnt_,name);
  718. cnt_[0].ReInit(node_mid.Dim());
  719. for (Long i = 0; i < node_mid.Dim(); i++) {
  720. Long start = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), node_mid[i]) - pt_mid_.begin();
  721. Long end = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), (i+1==node_mid.Dim() ? Morton<DIM>().Next() : node_mid[i+1])) - pt_mid_.begin();
  722. if (i == 0) SCTL_ASSERT(start == 0);
  723. if (i+1 == node_mid.Dim()) SCTL_ASSERT(end == pt_mid_.Dim());
  724. cnt_[0][i] = end - start;
  725. }
  726. }
  727. }
  728. void AddParticleData(const std::string& data_name, const std::string& particle_name, const Vector<Real>& data) {
  729. SCTL_ASSERT(scatter_idx.find(particle_name) != scatter_idx.end());
  730. SCTL_ASSERT(data_pt_name.find(data_name) == data_pt_name.end());
  731. data_pt_name[data_name] = particle_name;
  732. Iterator<Vector<Real>> data_;
  733. Iterator<Vector<Long>> cnt_;
  734. this->AddData(data_name, Vector<Real>(), Vector<Long>());
  735. this->GetData(data_,cnt_,data_name);
  736. { // Set data_[0]
  737. data_[0] = data;
  738. this->GetComm().ScatterForward(data_[0], scatter_idx[particle_name]);
  739. }
  740. if (data_name != particle_name) { // Set cnt_[0]
  741. Iterator<Vector<Real>> pt_coord;
  742. Iterator<Vector<Long>> pt_cnt;
  743. this->GetData(pt_coord, pt_cnt, particle_name);
  744. cnt_[0] = pt_cnt[0];
  745. }
  746. }
  747. void GetParticleData(Vector<Real>& data, const std::string& data_name) const {
  748. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  749. const std::string& particle_name = data_pt_name.find(data_name)->second;
  750. SCTL_ASSERT(scatter_idx.find(particle_name) != scatter_idx.end());
  751. const auto& scatter_idx_ = scatter_idx.find(particle_name)->second;
  752. const Long Nlocal_ = Nlocal.find(particle_name)->second;
  753. const auto& mins = this->GetPartitionMID();
  754. const auto& node_mid = this->GetNodeMID();
  755. const auto& comm = this->GetComm();
  756. Long dof = 0;
  757. Vector<Long> dsp;
  758. ConstIterator<Vector<Long>> cnt_;
  759. ConstIterator<Vector<Real>> data_;
  760. this->GetData(data_, cnt_, data_name);
  761. SCTL_ASSERT(cnt_->Dim() == node_mid.Dim());
  762. BaseTree::scan(dsp, cnt_[0]);
  763. { // Set dof
  764. Long Nn = node_mid.Dim();
  765. StaticArray<Long,2> Ng, Nl = {data_->Dim(), dsp[Nn-1]+cnt_[0][Nn-1]};
  766. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  767. if (Ng[1]) dof = Ng[0] / Ng[1];
  768. }
  769. { // Set data
  770. Integer np = comm.Size();
  771. Integer rank = comm.Rank();
  772. Long N0 = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  773. Long N1 = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  774. Long start = dsp[N0] * dof;
  775. Long end = (N1<dsp.Dim() ? dsp[N1] : dsp[N1-1]+cnt_[0][N1-1]) * dof;
  776. data.ReInit(end-start, (Iterator<Real>)data_->begin()+start, true);
  777. comm.ScatterReverse(data, scatter_idx_, Nlocal_ * dof);
  778. }
  779. }
  780. void DeleteParticleData(const std::string& data_name) {
  781. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  782. auto particle_name = data_pt_name[data_name];
  783. if (data_name == particle_name) {
  784. std::vector<std::string> data_name_lst;
  785. for (auto& pair : data_pt_name) {
  786. if (pair.second == particle_name) {
  787. data_name_lst.push_back(pair.first);
  788. }
  789. }
  790. for (auto x : data_name_lst) {
  791. if (x != particle_name) {
  792. DeleteParticleData(x);
  793. }
  794. }
  795. Nlocal.erase(particle_name);
  796. }
  797. this->DeleteData(data_name);
  798. data_pt_name.erase(data_name);
  799. }
  800. void WriteParticleVTK(std::string fname, std::string data_name, bool show_ghost = false) const {
  801. typedef typename VTUData::VTKReal VTKReal;
  802. const auto& node_mid = this->GetNodeMID();
  803. const auto& node_attr = this->GetNodeAttr();
  804. VTUData vtu_data;
  805. if (DIM <= 3) { // Set vtu data
  806. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  807. std::string particle_name = data_pt_name.find(data_name)->second;
  808. ConstIterator<Vector<Real>> pt_coord = NullIterator<Vector<Real>>();
  809. ConstIterator<Vector<Real>> pt_value = NullIterator<Vector<Real>>();
  810. ConstIterator<Vector<Long>> pt_cnt = NullIterator<Vector<Long>>();
  811. Vector<Long> pt_dsp;
  812. Long value_dof = 0;
  813. { // Set pt_coord, pt_cnt, pt_dsp
  814. this->GetData(pt_coord, pt_cnt, particle_name);
  815. Tree<Real,DIM>::scan(pt_dsp, pt_cnt[0]);
  816. }
  817. if (particle_name != data_name) { // Set pt_value, value_dof
  818. ConstIterator<Vector<Long>> pt_cnt = NullIterator<Vector<Long>>();
  819. this->GetData(pt_value, pt_cnt, data_name);
  820. Long Npt = omp_par::reduce(pt_cnt->begin(), pt_cnt->Dim());
  821. value_dof = pt_value->Dim() / std::max<Long>(Npt,1);
  822. }
  823. Vector<VTKReal> &coord = vtu_data.coord;
  824. Vector<VTKReal> &value = vtu_data.value;
  825. Vector<int32_t> &connect = vtu_data.connect;
  826. Vector<int32_t> &offset = vtu_data.offset;
  827. Vector<uint8_t> &types = vtu_data.types;
  828. Long point_cnt = coord.Dim() / DIM;
  829. Long connect_cnt = connect.Dim();
  830. value.ReInit(point_cnt * value_dof);
  831. value.SetZero();
  832. SCTL_ASSERT(node_mid.Dim() == node_attr.Dim());
  833. SCTL_ASSERT(node_mid.Dim() == pt_cnt->Dim());
  834. for (Long i = 0; i < node_mid.Dim(); i++) {
  835. if (!show_ghost && node_attr[i].Ghost) continue;
  836. if (!node_attr[i].Leaf) continue;
  837. for (Long j = 0; j < pt_cnt[0][i]; j++) {
  838. ConstIterator<Real> pt_coord_ = pt_coord->begin() + (pt_dsp[i] + j) * DIM;
  839. ConstIterator<Real> pt_value_ = (value_dof ? pt_value->begin() + (pt_dsp[i] + j) * value_dof : NullIterator<Real>());
  840. for (Integer k = 0; k < DIM; k++) coord.PushBack((VTKReal)pt_coord_[k]);
  841. for (Integer k = DIM; k < 3; k++) coord.PushBack(0);
  842. for (Integer k = 0; k < value_dof; k++) value.PushBack((VTKReal)pt_value_[k]);
  843. connect.PushBack(point_cnt);
  844. connect_cnt++;
  845. point_cnt++;
  846. offset.PushBack(connect_cnt);
  847. types.PushBack(1);
  848. }
  849. }
  850. }
  851. vtu_data.WriteVTK(fname, this->GetComm());
  852. }
  853. private:
  854. std::map<std::string, Long> Nlocal;
  855. std::map<std::string, Vector<Morton<DIM>>> pt_mid;
  856. std::map<std::string, Vector<Long>> scatter_idx;
  857. std::map<std::string, std::string> data_pt_name;
  858. };
  859. }
  860. #endif //_SCTL_TREE_