Peano
Loading...
Searching...
No Matches
Hardcoded_demo.py
Go to the documentation of this file.
1import os
2import sys
3sys.path.insert(0, os.path.abspath('../../../python'))
4
5import peano4.api
6import exahype2
7
8# @todo Dmitry: This file does not belong in here! This is either part of the
9# tutorials or the documentation, but not directly in the api
10# dir.
11
12
13# Workspace API modules
14from input.GridPatchFileReader import GridPatchFileReader # [src/toolbox/loadbalancing/api/input/GridPatchFileReader.py](src/toolbox/loadbalancing/api/input/GridPatchFileReader.py)
15import strategies.SplitTrees as split_strategy # [src/toolbox/loadbalancing/api/strategies/SplitTrees.py](src/toolbox/loadbalancing/api/strategies/SplitTrees.py)
16
17def demo(file_path: str, dimensions: int):
18 """
19 Demo: Read grid patch files, extract hardcoded split parameters, configure Hardcoded LB.
20
21 Args:
22 file_path: Directory containing Peano grid patch files (prefix 'grid-')
23 dimensions: 2 or 3 (must match input files)
24 """
25 if not os.path.isdir(file_path):
26 raise FileNotFoundError(f"Input directory not found: {file_path}")
27 print(f"Loading grid patch files from '{file_path}' (dimensions={dimensions})")
28 tree = GridPatchFileReader(file_path=file_path, dimensions=dimensions)
29 print(tree)
30
31 # Reset to single subtree and apply splitting to create multiple subtrees for the demo
32 tree = split_strategy.reset_tree_to_single_subtree(tree)
33
34 destination_ranks = [1, 2]
35 hardcoded_config, hardcoded_mode = split_strategy.split_tree(
36 tree,
37 number_of_subtrees=3,
38 enable_propagate_bottom_up=True,
39 enable_extract_split_instructions=True,
40 destination_ranks=destination_ranks,
41 verbose=True,
42 ) # creates subtrees {0,1,2}
43 print("After split_tree():")
44 print(tree)
45
46 print("Hardcoded constructor arguments:")
47 print(" destination_ranks =", destination_ranks)
48 print(" mode =", hardcoded_mode)
49 print(" config =", hardcoded_config)
50
51 # Create minimal ExaHyPE2 project
52 project = exahype2.Project(
53 namespace=["toolbox","loadbalancing","demo"],
54 project_name="HardcodedLB",
55 directory=".",
56 executable="HardcodedLB.demo"
57 )
58
59 solver = exahype2.solvers.fv.rusanov.GlobalFixedTimeStep(
60 "DemoSolver",
61 patch_size=9,
62 unknowns=1,
63 auxiliary_variables=0,
64 min_volume_h=0.05,
65 max_volume_h=0.1,
66 normalised_time_step_size=0.01,
67 flux=exahype2.solvers.PDETerms.User_Defined_Implementation,
68 eigenvalues=exahype2.solvers.PDETerms.User_Defined_Implementation,
69 source_term=exahype2.solvers.PDETerms.None_Implementation
70 )
71 project.add_solver(solver)
72
73 project.set_global_simulation_parameters(
74 dimensions=tree._dimension,
75 size=[1.0,1.0,1.0][0:tree._dimension],
76 offset=[0.0,0.0,0.0][0:tree._dimension],
77 min_end_time=0.0,
78 max_end_time=0.0,
79 first_plot_time_stamp=0.0,
80 time_in_between_plots=0.0,
81 periodic_BC=[False,False,False]
82 )
83
84 # Set the Hardcoded strategy using the extracted parameters
85 project.set_load_balancing(
86 "toolbox::loadbalancing::strategies::Hardcoded",
87 hardcoded_config
88 )
89
90 # Configure build (adjust path/mode to your setup)
91 project.set_Peano4_installation("../../../../", peano4.api.output.CompileMode.RELEASE)
92
93 # Generate only (no compile) for demonstration
94 peano4_project = project.generate_Peano4_project(verbose=True)
95 peano4_project.generate()
96 print("Demo project generated with Hardcoded load balancing.")
97 peano4_project.build(make_clean_first=True, number_of_parallel_builds=-1)
98
99 # ---- Demo summary output -------------------------------------------------
100 print("\n=== Hardcoded Load Balancing Summary ===")
101 print(f"Subtrees: {tree._subtree_count}")
102 print(f"Leaf count: {int(tree._leaf_count)}")
103 print("destination_ranks =", destination_ranks)
104 print("Hardcoded mode =", hardcoded_mode)
105 print("hardcoded_config =", hardcoded_config)
106 print("\nProject object:")
107 print(project)
108
109if __name__ == "__main__":
110 # Adjust file_path and dimensions as needed
111 # Provide a directory containing 'grid-*' files.
112 demo(file_path="Input/example", dimensions=3)
demo(str file_path, int dimensions)
Demo: Read grid patch files, extract hardcoded split parameters, configure Hardcoded LB.