100 Generate an artificial regular grid with uniform refinement for testing purposes.
102 This function creates a complete regular grid structure where all regions are
103 refined uniformly to the specified maximum level. All cells are initially
104 assigned to subtree 0, providing a clean starting point for load balancing
105 experiments and algorithm testing.
109 The generated grid follows Peano's hierarchical structure:
110 - Level 0: 3^1 cells per dimension (3x3 for 2D, 3x3x3 for 3D)
111 - Level k: 3^(k+1) cells per dimension
112 - All cells have weight=1 (exist) and subtree_id=0 (single resource)
116 - **Algorithm Development**: Test splitting strategies on predictable structures
117 - **Performance Benchmarking**: Measure algorithm performance with known complexity
118 - **Regression Testing**: Ensure consistent behavior across code changes
119 - **Educational Examples**: Demonstrate API functionality with simple cases
122 name (str, optional): Descriptive name for the generated tree (e.g., "TestGrid_L4").
123 If not provided, a deterministic default name is generated from dimensions and level.
124 max_level (int): Maximum refinement level (must be > 0)
125 dimensions (int): Spatial dimensions (2 for 2D, 3 for 3D)
128 Tree: Tree object with complete regular grid structure
132 # Create a 2D regular grid for testing
133 test_tree = RegularGridGenerator(
134 name="Uniform_Test_2D",
139 print(f"Generated tree: {test_tree._name}")
140 print(f"Total cells: {test_tree._leaf_count}")
141 print(f"Levels: {test_tree._max_level}")
143 # Apply splitting for load balancing test
144 from strategies.SplitTrees import split_tree
145 split_tree(test_tree, number_of_subtrees=3)
149 - Domain offset and size are set to placeholder values (origin and unit size)
150 - For large max_level values, memory usage grows as 3^(dimensions * max_level)
151 - Generated trees are deterministic - same parameters always produce identical results
153 if max_level
is None:
154 raise ValueError(
"max_level must be provided")
156 if dimensions
is None:
157 raise ValueError(
"dimensions must be provided")
160 raise ValueError(f
"max_level must be > 0, got {max_level}")
162 if dimensions
not in [2, 3]:
163 raise ValueError(f
"dimensions must be 2 or 3, got {dimensions}")
166 name = f
"regular-grid-{dimensions}d-l{max_level}"
168 print(f
"Generating {dimensions}D regular grid '{name}' with {max_level} levels")
171 artificial_arrays = []
174 for level
in range(max_level):
176 level_cells = size ** dimensions
177 total_cells += level_cells
179 print(f
" Level {level+1}: {size}^{dimensions} = {level_cells:,} cells")
182 array = np.empty((size, size, size), dtype=object)
183 for a
in range(size):
184 for b
in range(size):
185 for c
in range(size):
186 array[a][b][c] = (1, 0)
187 elif dimensions == 2:
188 array = np.empty((size, size), dtype=object)
189 for a
in range(size):
190 for b
in range(size):
193 artificial_arrays.append(array)
195 print(f
"Total cells generated: {total_cells:,}")
202 elif dimensions == 2:
207 peano_tree = Tree(name, dimensions, offset, size)
208 peano_tree.read_in_tree_from_artificial(
209 input_artificial_arrays=artificial_arrays,
214 print(f
"Regular grid generation completed successfully")
215 print(f
" Tree: {peano_tree._name}")
216 print(f
" Leaf count: {peano_tree._leaf_count}")
217 print(f
" Subtree count: {peano_tree._subtree_count}")