tree.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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) {
  220. Integer np = comm.Size();
  221. Integer rank = comm.Rank();
  222. Morton<DIM> pt_mid0;
  223. Vector<Morton<DIM>> pt_mid;
  224. { // Construct sorted pt_mid
  225. Long Npt = coord.Dim() / DIM;
  226. pt_mid.ReInit(Npt);
  227. for (Long i = 0; i < Npt; i++) {
  228. pt_mid[i] = Morton<DIM>(coord.begin() + i*DIM);
  229. }
  230. Vector<Morton<DIM>> sorted_mid;
  231. comm.HyperQuickSort(pt_mid, sorted_mid);
  232. pt_mid.Swap(sorted_mid);
  233. SCTL_ASSERT(pt_mid.Dim());
  234. pt_mid0 = pt_mid[0];
  235. }
  236. { // Update M = global_min(pt_mid.Dim(), M)
  237. Long M0, M1, Npt = pt_mid.Dim();
  238. comm.Allreduce(Ptr2ConstItr<Long>(&M,1), Ptr2Itr<Long>(&M0,1), 1, Comm::CommOp::MIN);
  239. comm.Allreduce(Ptr2ConstItr<Long>(&Npt,1), Ptr2Itr<Long>(&M1,1), 1, Comm::CommOp::MIN);
  240. M = std::min(M0,M1);
  241. SCTL_ASSERT(M > 0);
  242. }
  243. { // pt_mid <-- [M points from rank-1; pt_mid; M points from rank+1]
  244. Long send_size0 = (rank+1<np ? M : 0);
  245. Long send_size1 = (rank > 0 ? M : 0);
  246. Long recv_size0 = (rank > 0 ? M : 0);
  247. Long recv_size1 = (rank+1<np ? M : 0);
  248. Vector<Morton<DIM>> pt_mid_(recv_size0 + pt_mid.Dim() + recv_size1);
  249. memcopy(pt_mid_.begin()+recv_size0, pt_mid.begin(), pt_mid.Dim());
  250. void* recv_req0 = comm.Irecv(pt_mid_.begin(), recv_size0, (rank+np-1)%np, 0);
  251. void* recv_req1 = comm.Irecv(pt_mid_.begin() + recv_size0 + pt_mid.Dim(), recv_size1, (rank+1)%np, 1);
  252. void* send_req0 = comm.Isend(pt_mid .begin() + pt_mid.Dim() - send_size0, send_size0, (rank+1)%np, 0);
  253. void* send_req1 = comm.Isend(pt_mid .begin(), send_size1, (rank+np-1)%np, 1);
  254. comm.Wait(recv_req0);
  255. comm.Wait(recv_req1);
  256. comm.Wait(send_req0);
  257. comm.Wait(send_req1);
  258. pt_mid.Swap(pt_mid_);
  259. }
  260. { // Build linear MortonID tree from pt_mid
  261. node_mid.ReInit(0);
  262. Long idx = 0;
  263. Morton<DIM> m0;
  264. Morton<DIM> mend = Morton<DIM>().Next();
  265. while (m0 < mend) {
  266. Integer d = m0.Depth();
  267. Morton<DIM> m1 = (idx + M < pt_mid.Dim() ? pt_mid[idx+M] : Morton<DIM>().Next());
  268. while (d < Morton<DIM>::MAX_DEPTH && m0.Ancestor(d) == m1.Ancestor(d)) d++;
  269. m0 = m0.Ancestor(d);
  270. node_mid.PushBack(m0);
  271. m0 = m0.Next();
  272. idx = std::lower_bound(pt_mid.begin(), pt_mid.end(), m0) - pt_mid.begin();
  273. }
  274. }
  275. { // Set mins
  276. mins.ReInit(np);
  277. Long min_idx = std::lower_bound(node_mid.begin(), node_mid.end(), pt_mid0) - node_mid.begin() - 1;
  278. if (min_idx < 0) min_idx = 0;
  279. comm.Allgather(node_mid.begin() + min_idx, 1, mins.begin(), 1);
  280. }
  281. { // Set node_mid, node_attr
  282. Morton<DIM> m0 = (rank ? mins[rank] : Morton<DIM>() );
  283. Morton<DIM> m1 = (rank+1<np ? mins[rank+1] : Morton<DIM>().Next());
  284. Long Nnodes = node_mid.Dim();
  285. node_attr.ReInit(Nnodes);
  286. for (Long i = 0; i < Nnodes; i++) {
  287. node_attr[i].Leaf = 1;
  288. node_attr[i].Ghost = (node_mid[i] < m0 || node_mid[i] >= m1);
  289. }
  290. }
  291. { // Add non-leaf nodes and place-holder for ghost nodes
  292. // TODO
  293. }
  294. { // Update node_data, node_cnt
  295. // TODO
  296. }
  297. }
  298. void AddData(const std::string& name, const Vector<Real>& data, const Vector<Long>& cnt) {
  299. Long dof = 0;
  300. { // Check dof
  301. StaticArray<Long,2> Nl, Ng;
  302. Nl[0] = data.Dim();
  303. Nl[1] = omp_par::reduce(cnt.begin(), cnt.Dim());
  304. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  305. if (Ng[1]) dof = Ng[0] / Ng[1];
  306. SCTL_ASSERT(Nl[0] == Nl[1] * dof);
  307. SCTL_ASSERT(Ng[0] == Ng[1] * dof);
  308. }
  309. if (dof) SCTL_ASSERT(cnt.Dim() == node_mid.Dim());
  310. SCTL_ASSERT(node_data.find(name) == node_data.end());
  311. node_data[name] = data;
  312. node_cnt [name] = cnt;
  313. }
  314. void GetData(Iterator<Vector<Real>>& data, ConstIterator<Vector<Long>>& cnt, const std::string& name) {
  315. auto data_ = node_data.find(name);
  316. const auto cnt_ = node_cnt.find(name);
  317. SCTL_ASSERT(data_ != node_data.end());
  318. SCTL_ASSERT( cnt_ != node_cnt .end());
  319. data = Ptr2Itr<Vector<Real>>(&data_->second,1);
  320. cnt = Ptr2ConstItr<Vector<Long>>(& cnt_->second,1);
  321. }
  322. void GetData(ConstIterator<Vector<Real>>& data, ConstIterator<Vector<Long>>& cnt, const std::string& name) const {
  323. const auto data_ = node_data.find(name);
  324. const auto cnt_ = node_cnt.find(name);
  325. SCTL_ASSERT(data_ != node_data.end());
  326. SCTL_ASSERT( cnt_ != node_cnt .end());
  327. data = Ptr2ConstItr<Vector<Real>>(&data_->second,1);
  328. cnt = Ptr2ConstItr<Vector<Long>>(& cnt_->second,1);
  329. }
  330. void DeleteData(const std::string& name) {
  331. SCTL_ASSERT(node_data.find(name) != node_data.end());
  332. SCTL_ASSERT(node_cnt .find(name) != node_cnt .end());
  333. node_data.erase(name);
  334. node_cnt .erase(name);
  335. }
  336. void WriteTreeVTK(std::string fname, bool show_ghost = false) const {
  337. typedef typename VTUData::VTKReal VTKReal;
  338. VTUData vtu_data;
  339. if (DIM <= 3) { // Set vtu data
  340. static const Integer Ncorner = (1u << DIM);
  341. Vector<VTKReal> &coord = vtu_data.coord;
  342. //Vector<VTKReal> &value = vtu_data.value;
  343. Vector<int32_t> &connect = vtu_data.connect;
  344. Vector<int32_t> &offset = vtu_data.offset;
  345. Vector<uint8_t> &types = vtu_data.types;
  346. StaticArray<VTKReal, DIM> c;
  347. Long point_cnt = coord.Dim() / 3;
  348. Long connect_cnt = connect.Dim();
  349. for (Long nid = 0; nid < node_mid.Dim(); nid++) {
  350. const Morton<DIM> &mid = node_mid[nid];
  351. const NodeAttr &attr = node_attr[nid];
  352. if (!show_ghost && attr.Ghost) continue;
  353. if (!attr.Leaf) continue;
  354. mid.Coord((Iterator<VTKReal>)c);
  355. VTKReal s = sctl::pow<VTKReal>(0.5, mid.Depth());
  356. for (Integer j = 0; j < Ncorner; j++) {
  357. for (Integer i = 0; i < DIM; i++) coord.PushBack(c[i] + (j & (1u << i) ? 1 : 0) * s);
  358. for (Integer i = DIM; i < 3; i++) coord.PushBack(0);
  359. connect.PushBack(point_cnt);
  360. connect_cnt++;
  361. point_cnt++;
  362. }
  363. offset.PushBack(connect_cnt);
  364. if (DIM == 2)
  365. types.PushBack(8);
  366. else if (DIM == 3)
  367. types.PushBack(11);
  368. else
  369. types.PushBack(4);
  370. }
  371. }
  372. vtu_data.WriteVTK(fname, comm);
  373. }
  374. protected:
  375. void GetData(Iterator<Vector<Real>>& data, Iterator<Vector<Long>>& cnt, const std::string& name) {
  376. auto data_ = node_data.find(name);
  377. const auto cnt_ = node_cnt.find(name);
  378. SCTL_ASSERT(data_ != node_data.end());
  379. SCTL_ASSERT( cnt_ != node_cnt .end());
  380. data = Ptr2Itr<Vector<Real>>(&data_->second,1);
  381. cnt = Ptr2Itr<Vector<Long>>(& cnt_->second,1);
  382. }
  383. static void scan(Vector<Long>& dsp, const Vector<Long>& cnt) {
  384. dsp.ReInit(cnt.Dim());
  385. dsp[0] = 0;
  386. omp_par::scan(cnt.begin(), dsp.begin(), cnt.Dim());
  387. }
  388. //template <typename A, typename B> struct SortPair {
  389. // int operator<(const SortPair<A, B> &p1) const { return key < p1.key; }
  390. // A key;
  391. // B data;
  392. //};
  393. private:
  394. Vector<Morton<DIM>> mins;
  395. Vector<Morton<DIM>> node_mid;
  396. Vector<NodeAttr> node_attr;
  397. std::map<std::string, Vector<Real>> node_data;
  398. std::map<std::string, Vector<Long>> node_cnt;
  399. Comm comm;
  400. };
  401. template <class Real, Integer DIM, class BaseTree = Tree<Real,DIM>> class PtTree : public BaseTree {
  402. public:
  403. PtTree(const Comm& comm = Comm::Self()) : BaseTree(comm) {}
  404. ~PtTree() {
  405. #ifdef SCTL_MEMDEBUG
  406. for (auto& pair : data_pt_name) {
  407. ConstIterator<Vector<Real>> data;
  408. ConstIterator<Vector<Long>> cnt;
  409. this->GetData(data, cnt, pair.second);
  410. SCTL_ASSERT(scatter_idx.find(pair.second) != scatter_idx.end());
  411. }
  412. #endif
  413. }
  414. void UpdateRefinement(const Vector<Real>& coord, Long M = 1) {
  415. const auto& comm = this->GetComm();
  416. Long start_node_idx, end_node_idx;
  417. { // Set start_node_idx, end_node_idx
  418. const auto& mins = this->GetPartitionMID();
  419. const auto& node_mid = this->GetNodeMID();
  420. Integer np = comm.Size();
  421. Integer rank = comm.Rank();
  422. start_node_idx = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  423. end_node_idx = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  424. }
  425. BaseTree::UpdateRefinement(coord, M);
  426. const auto& mins = this->GetPartitionMID();
  427. const auto& node_mid = this->GetNodeMID();
  428. for (const auto& pair : pt_mid) {
  429. const auto& pt_name = pair.first;
  430. auto& pt_mid_ = pt_mid[pt_name];
  431. auto& scatter_idx_ = scatter_idx[pt_name];
  432. comm.PartitionS(pt_mid_, mins[comm.Rank()]);
  433. comm.PartitionN(scatter_idx_, pt_mid_.Dim());
  434. Vector<Long> pt_cnt(node_mid.Dim());
  435. for (Long i = 0; i < node_mid.Dim(); i++) { // Set pt_cnt
  436. Long start = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), node_mid[i]) - pt_mid_.begin();
  437. 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();
  438. if (i == 0) SCTL_ASSERT(start == 0);
  439. if (i+1 == node_mid.Dim()) SCTL_ASSERT(end == pt_mid_.Dim());
  440. pt_cnt[i] = end - start;
  441. }
  442. for (const auto& pair : data_pt_name) {
  443. if (pair.second == pt_name) {
  444. const auto& data_name = pair.first;
  445. Iterator<Vector<Real>> data;
  446. Iterator<Vector<Long>> cnt;
  447. this->GetData(data, cnt, data_name);
  448. { // Update data
  449. Long dof = 0;
  450. { // Set dof
  451. StaticArray<Long,2> Nl = {0, 0}, Ng;
  452. Nl[0] = data->Dim();
  453. for (Long i = 0; i < cnt->Dim(); i++) Nl[1] += cnt[0][i];
  454. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  455. dof = Ng[0] / std::max<Long>(Ng[1],1);
  456. }
  457. Long offset = 0, count = 0;
  458. SCTL_ASSERT(0 <= start_node_idx);
  459. SCTL_ASSERT(start_node_idx <= end_node_idx);
  460. SCTL_ASSERT(end_node_idx <= cnt->Dim());
  461. for (Long i = 0; i < start_node_idx; i++) offset += cnt[0][i];
  462. for (Long i = start_node_idx; i < end_node_idx; i++) count += cnt[0][i];
  463. offset *= dof;
  464. count *= dof;
  465. Vector<Real> data_(count, data->begin() + offset);
  466. comm.PartitionN(data_, pt_mid_.Dim());
  467. data->Swap(data_);
  468. }
  469. cnt[0] = pt_cnt;
  470. }
  471. }
  472. }
  473. }
  474. void AddParticles(const std::string& name, const Vector<Real>& coord) {
  475. const auto& mins = this->GetPartitionMID();
  476. const auto& node_mid = this->GetNodeMID();
  477. const auto& comm = this->GetComm();
  478. SCTL_ASSERT(scatter_idx.find(name) == scatter_idx.end());
  479. Vector<Long>& scatter_idx_ = scatter_idx[name];
  480. Long N = coord.Dim() / DIM;
  481. SCTL_ASSERT(coord.Dim() == N * DIM);
  482. Nlocal[name] = N;
  483. Vector<Morton<DIM>>& pt_mid_ = pt_mid[name];
  484. if (pt_mid_.Dim() != N) pt_mid_.ReInit(N);
  485. for (Long i = 0; i < N; i++) {
  486. pt_mid_[i] = Morton<DIM>(coord.begin() + i*DIM);
  487. }
  488. comm.SortScatterIndex(pt_mid_, scatter_idx_, &mins[comm.Rank()]);
  489. comm.ScatterForward(pt_mid_, scatter_idx_);
  490. AddParticleData(name, name, coord);
  491. { // Set node_cnt
  492. Iterator<Vector<Real>> data_;
  493. Iterator<Vector<Long>> cnt_;
  494. this->GetData(data_,cnt_,name);
  495. cnt_[0].ReInit(node_mid.Dim());
  496. for (Long i = 0; i < node_mid.Dim(); i++) {
  497. Long start = std::lower_bound(pt_mid_.begin(), pt_mid_.end(), node_mid[i]) - pt_mid_.begin();
  498. 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();
  499. if (i == 0) SCTL_ASSERT(start == 0);
  500. if (i+1 == node_mid.Dim()) SCTL_ASSERT(end == pt_mid_.Dim());
  501. cnt_[0][i] = end - start;
  502. }
  503. }
  504. }
  505. void AddParticleData(const std::string& data_name, const std::string& particle_name, const Vector<Real>& data) {
  506. SCTL_ASSERT(scatter_idx.find(particle_name) != scatter_idx.end());
  507. SCTL_ASSERT(data_pt_name.find(data_name) == data_pt_name.end());
  508. data_pt_name[data_name] = particle_name;
  509. Iterator<Vector<Real>> data_;
  510. Iterator<Vector<Long>> cnt_;
  511. this->AddData(data_name, Vector<Real>(), Vector<Long>());
  512. this->GetData(data_,cnt_,data_name);
  513. { // Set data_[0]
  514. data_[0] = data;
  515. this->GetComm().ScatterForward(data_[0], scatter_idx[particle_name]);
  516. }
  517. if (data_name != particle_name) { // Set cnt_[0]
  518. Iterator<Vector<Real>> pt_coord;
  519. Iterator<Vector<Long>> pt_cnt;
  520. this->GetData(pt_coord, pt_cnt, particle_name);
  521. cnt_[0] = pt_cnt[0];
  522. }
  523. }
  524. void GetParticleData(Vector<Real>& data, const std::string& data_name) const {
  525. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  526. const std::string& particle_name = data_pt_name.find(data_name)->second;
  527. SCTL_ASSERT(scatter_idx.find(particle_name) != scatter_idx.end());
  528. const auto& scatter_idx_ = scatter_idx.find(particle_name)->second;
  529. const Long Nlocal_ = Nlocal.find(particle_name)->second;
  530. const auto& mins = this->GetPartitionMID();
  531. const auto& node_mid = this->GetNodeMID();
  532. const auto& comm = this->GetComm();
  533. Long dof = 0;
  534. Vector<Long> dsp;
  535. ConstIterator<Vector<Long>> cnt_;
  536. ConstIterator<Vector<Real>> data_;
  537. this->GetData(data_, cnt_, data_name);
  538. SCTL_ASSERT(cnt_->Dim() == node_mid.Dim());
  539. BaseTree::scan(dsp, cnt_[0]);
  540. { // Set dof
  541. Long Nn = node_mid.Dim();
  542. StaticArray<Long,2> Ng, Nl = {data_->Dim(), dsp[Nn-1]+cnt_[0][Nn-1]};
  543. comm.Allreduce((ConstIterator<Long>)Nl, (Iterator<Long>)Ng, 2, Comm::CommOp::SUM);
  544. if (Ng[1]) dof = Ng[0] / Ng[1];
  545. }
  546. { // Set data
  547. Integer np = comm.Size();
  548. Integer rank = comm.Rank();
  549. Long N0 = std::lower_bound(node_mid.begin(), node_mid.end(), mins[rank]) - node_mid.begin();
  550. Long N1 = std::lower_bound(node_mid.begin(), node_mid.end(), (rank+1==np ? Morton<DIM>().Next() : mins[rank+1])) - node_mid.begin();
  551. Long start = dsp[N0] * dof;
  552. Long end = (N1<dsp.Dim() ? dsp[N1] : dsp[N1-1]+cnt_[0][N1-1]) * dof;
  553. data.ReInit(end-start, (Iterator<Real>)data_->begin()+start);
  554. comm.ScatterReverse(data, scatter_idx_, Nlocal_ * dof);
  555. }
  556. }
  557. void DeleteParticleData(const std::string& data_name) {
  558. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  559. auto particle_name = data_pt_name[data_name];
  560. if (data_name == particle_name) {
  561. std::vector<std::string> data_name_lst;
  562. for (auto& pair : data_pt_name) {
  563. if (pair.second == particle_name) {
  564. data_name_lst.push_back(pair.first);
  565. }
  566. }
  567. for (auto x : data_name_lst) {
  568. if (x != particle_name) {
  569. DeleteParticleData(x);
  570. }
  571. }
  572. Nlocal.erase(particle_name);
  573. }
  574. this->DeleteData(data_name);
  575. data_pt_name.erase(data_name);
  576. }
  577. void WriteParticleVTK(std::string fname, std::string data_name, bool show_ghost = false) const {
  578. typedef typename VTUData::VTKReal VTKReal;
  579. const auto& node_mid = this->GetNodeMID();
  580. const auto& node_attr = this->GetNodeAttr();
  581. VTUData vtu_data;
  582. if (DIM <= 3) { // Set vtu data
  583. SCTL_ASSERT(data_pt_name.find(data_name) != data_pt_name.end());
  584. std::string particle_name = data_pt_name.find(data_name)->second;
  585. ConstIterator<Vector<Real>> pt_coord = NullIterator<Vector<Real>>();
  586. ConstIterator<Vector<Real>> pt_value = NullIterator<Vector<Real>>();
  587. ConstIterator<Vector<Long>> pt_cnt = NullIterator<Vector<Long>>();
  588. Vector<Long> pt_dsp;
  589. Long value_dof = 0;
  590. { // Set pt_coord, pt_cnt, pt_dsp
  591. this->GetData(pt_coord, pt_cnt, particle_name);
  592. Tree<Real,DIM>::scan(pt_dsp, pt_cnt[0]);
  593. }
  594. if (particle_name != data_name) { // Set pt_value, value_dof
  595. ConstIterator<Vector<Long>> pt_cnt = NullIterator<Vector<Long>>();
  596. this->GetData(pt_value, pt_cnt, data_name);
  597. Long Npt = omp_par::reduce(pt_cnt->begin(), pt_cnt->Dim());
  598. value_dof = pt_value->Dim() / std::max<Long>(Npt,1);
  599. }
  600. Vector<VTKReal> &coord = vtu_data.coord;
  601. Vector<VTKReal> &value = vtu_data.value;
  602. Vector<int32_t> &connect = vtu_data.connect;
  603. Vector<int32_t> &offset = vtu_data.offset;
  604. Vector<uint8_t> &types = vtu_data.types;
  605. Long point_cnt = coord.Dim() / DIM;
  606. Long connect_cnt = connect.Dim();
  607. value.ReInit(point_cnt * value_dof);
  608. value.SetZero();
  609. SCTL_ASSERT(node_mid.Dim() == node_attr.Dim());
  610. SCTL_ASSERT(node_mid.Dim() == pt_cnt->Dim());
  611. for (Long i = 0; i < node_mid.Dim(); i++) {
  612. if (!show_ghost && node_attr[i].Ghost) continue;
  613. if (!node_attr[i].Leaf) continue;
  614. for (Long j = 0; j < pt_cnt[0][i]; j++) {
  615. ConstIterator<Real> pt_coord_ = pt_coord->begin() + (pt_dsp[i] + j) * DIM;
  616. ConstIterator<Real> pt_value_ = (value_dof ? pt_value->begin() + (pt_dsp[i] + j) * value_dof : NullIterator<Real>());
  617. for (Integer k = 0; k < DIM; k++) coord.PushBack((VTKReal)pt_coord_[k]);
  618. for (Integer k = DIM; k < 3; k++) coord.PushBack(0);
  619. for (Integer k = 0; k < value_dof; k++) value.PushBack((VTKReal)pt_value_[k]);
  620. connect.PushBack(point_cnt);
  621. connect_cnt++;
  622. point_cnt++;
  623. offset.PushBack(connect_cnt);
  624. types.PushBack(1);
  625. }
  626. }
  627. }
  628. vtu_data.WriteVTK(fname, this->GetComm());
  629. }
  630. private:
  631. std::map<std::string, Long> Nlocal;
  632. std::map<std::string, Vector<Morton<DIM>>> pt_mid;
  633. std::map<std::string, Vector<Long>> scatter_idx;
  634. std::map<std::string, std::string> data_pt_name;
  635. };
  636. }
  637. #endif //_SCTL_TREE_