Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PatchTreeNode.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
20#include <nlohmann/json.hpp>
21
22namespace shamrock::scheduler {
23
24 template<class vec>
26 vec box_min;
27 vec box_max;
28 std::array<u64, 8> childs_id;
29 };
30
35 public:
38
47 std::array<u64, 8> childs_nid{u64_max};
48
49 bool is_leaf = true;
50
56 bool child_are_all_leafs = false;
57 };
58
60 inline bool operator==(const LinkedTreeNode &lhs, const LinkedTreeNode &rhs) {
61 return (lhs.level == rhs.level) && (lhs.parent_nid == rhs.parent_nid)
62 && (lhs.childs_nid == rhs.childs_nid) && (lhs.is_leaf == rhs.is_leaf)
64 }
65
71 public:
72 static constexpr u32 split_count = patch::PatchCoord<3>::splts_count;
74
75 PatchCoord patch_coord;
76
77 LinkedTreeNode tree_node;
78 u64 linked_patchid;
79
80 // patch fields
81 u64 load_value = u64_max;
82
83 std::array<PatchTreeNode, split_count> get_split_nodes(u32 cur_id);
84
85 bool is_leaf() { return tree_node.is_leaf; }
86
87 u64 get_child_nid(u32 id) { return tree_node.childs_nid[id]; }
88
96 template<class vec>
100
101 // Convert patch range to object coordinates using given coordinate transform
102 auto [bmin, bmax] = box_transform.to_obj_coord(patch_coord.get_patch_range());
103
104 n.box_min = bmin;
105 n.box_max = bmax;
106 n.childs_id[0] = tree_node.childs_nid[0];
107 n.childs_id[1] = tree_node.childs_nid[1];
108 n.childs_id[2] = tree_node.childs_nid[2];
109 n.childs_id[3] = tree_node.childs_nid[3];
110 n.childs_id[4] = tree_node.childs_nid[4];
111 n.childs_id[5] = tree_node.childs_nid[5];
112 n.childs_id[6] = tree_node.childs_nid[6];
113 n.childs_id[7] = tree_node.childs_nid[7];
114 return n;
115 }
116 };
117
119 inline bool operator==(const PatchTreeNode &lhs, const PatchTreeNode &rhs) {
120 return (lhs.patch_coord == rhs.patch_coord) && (lhs.tree_node == rhs.tree_node)
121 && (lhs.linked_patchid == rhs.linked_patchid) && (lhs.load_value == rhs.load_value);
122 }
123
124 inline auto PatchTreeNode::get_split_nodes(u32 cur_id)
125 -> std::array<PatchTreeNode, split_count> {
126 std::array<PatchCoord, split_count> splt_coord = patch_coord.split();
127
128 std::array<PatchTreeNode, split_count> ret;
129
130#pragma unroll
131 for (u32 i = 0; i < split_count; i++) {
132 ret[i].patch_coord = splt_coord[i];
133 ret[i].tree_node.level = tree_node.level + 1;
134 ret[i].tree_node.parent_nid = cur_id;
135 }
136
137 return ret;
138 }
139
146 inline void to_json(nlohmann::json &j, const LinkedTreeNode &p) {
147
148 j = nlohmann::json{
149 {"level", p.level},
150 {"parent_nid", p.parent_nid},
151 {"childs_nid", p.childs_nid},
152 {"is_leaf", p.is_leaf},
153 {"child_are_all_leafs", p.child_are_all_leafs}};
154 }
155
162 inline void from_json(const nlohmann::json &j, LinkedTreeNode &p) {
163 j.at("level").get_to(p.level);
164 j.at("parent_nid").get_to(p.parent_nid);
165 j.at("childs_nid").get_to(p.childs_nid);
166 j.at("is_leaf").get_to(p.is_leaf);
167 j.at("child_are_all_leafs").get_to(p.child_are_all_leafs);
168 }
169
176 inline void to_json(nlohmann::json &j, const PatchTreeNode &p) {
177
178 j = nlohmann::json{
179 {"linked_patchid", p.linked_patchid},
180 {"load_value", p.load_value},
181 {"tree_node", p.tree_node},
182 {"patch_coord",
183 {
184 {"min", p.patch_coord.coord_min},
185 {"max", p.patch_coord.coord_max},
186 }},
187 };
188 }
189
196 inline void from_json(const nlohmann::json &j, PatchTreeNode &p) {
197 j.at("linked_patchid").get_to(p.linked_patchid);
198 j.at("load_value").get_to(p.load_value);
199 j.at("tree_node").get_to(p.tree_node);
200 j.at("patch_coord").at("min").get_to(p.patch_coord.coord_min);
201 j.at("patch_coord").at("max").get_to(p.patch_coord.coord_max);
202 }
203
204} // namespace shamrock::scheduler
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
Node information in the PatchTree link list.
bool child_are_all_leafs
Store true if all childrens of this node are leafs.
std::array< u64, 8 > childs_nid
Array of childs node ids.
u32 level
Level of the tree node.
bool is_leaf
Is this node a leaf.
Node information in the patchtree + held patch info.
SerialPatchNode< vec > convert(const shamrock::patch::PatchCoordTransform< vec > box_transform)
Convert PatchTreeNode to SerialPatchNode using given coordinate transform.
constexpr u64 u64_max
u64 max value