Peano
Loading...
Searching...
No Matches
Project.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import peano4.api
5
6import dastgen2
7
8import os
9import sys
10import itertools
11
12from .PETScMain import PETScMain
13
14
15class Project(object):
16 r"""!
17
18 Multigrid solver project
19
20 A project hosts a set of multigrid solvers for different PDEs (which you might
21 eventually want to couple). Other than that, each project generates exactly
22 one main file and a solver repository. The main file is represented by
23 petsc.PETScMain, while the repository is generated directly by the project.
24 Consult generate_Peano4_project() for the latter.
25
26
27 ## Algorithmic steps
28
29 Every PETSc project runs through the following steps:
30
31 - algorithm_step_create_grid
32 - algorithm_step_enumerate_and_init_solution
33 - _algorithm_step_init_petsc
34 - algorithm_step_assemble
35 - _algorithm_step_solve
36 - algorithm_step_map_solution_onto_mesh
37 - algorithm_step_plot
38
39 Each of these steps is mapped onto one observer. It is clear that not all
40 steps actually lead to mesh traversals and, therefore, are not to be modified
41 by users. They are simply there to allow the main function to pick a step and
42 to tell all MPI ranks to run the same step on all local trees. I therefore
43 mark these steps as private via a leading underscore.
44
45
46 ## Attributes
47
48 \param _solvers: [Solver]
49 List of tuples of typename and names of solvers that this project should use simultaneously.
50 \param preconditioner_type: String
51 Preconditioner type to be used. This part of the string should match the final
52 part of the enum class in src/petsc/LinearEquationSystem.h. The namespaceing is
53 taken care of downstream.
54 \param solver_type: String
55 Solver type to be used. This part of the string should match the final
56 part of the enum class in src/petsc/LinearEquationSystem.h. The namespaceing is
57 taken care of downstream.
58
59 """
60
62 self,
63 namespace,
64 project_name,
65 directory=".",
66 executable="peano4petsc",
67 preconditioner_type="None",
68 solver_type="GMRES",
69 subdirectory="",
70 ):
72 namespace, project_name, directory, subdirectory
73 )
74 self._project.output.makefile.add_cmake_core_library("petsc")
75 self._subdirectory = subdirectory
76
77 self._domain_offset = [0.0, 0.0, 0.0]
78 self._domain_size = [1.0, 1.0, 1.0]
79 self._dimensions = 2
80 self._load_balancer_name = ""
82 self._Peano_src_directory = "."
83 self._build_mode = peano4.api.output.CompileMode.ASSERTS
84 self._executable_name = executable
85 self._output_path = "./"
86
87 self._solvers = []
88
90 "CreateGrid", False
91 )
93 "EnumerateAndInitSolution", False
94 )
96 "InitPETSc", False
97 )
101 "MapSolutionOntoMesh", False
102 )
104
105 self.preconditioner_type = preconditioner_type
106 self.solver_type = solver_type
107
108 def set_load_balancing(self, load_balancer_name, load_balancer_arguments):
109 """
110
111 load_balancer_name: string
112 Should be full-qualified name of the load balancer.
113 By default, I recommend to pass "toolbox::loadbalancing::RecursiveSubdivision"
114
115 load_balancer_arguments: string
116 If your load balancing requires parameters, add them
117 here. It is a string that will be copied into the C++ class instantiation.
118 Please add the brackets yourself, i.e. "(3,4,5)" is fine, but "3,4,5" is not.
119 The only exception is the empty parameter list. Here, you can/should simply
120 add the empty string.
121
122 """
123 self._load_balancer_name = load_balancer_name
124 self._load_balancer_arguments = load_balancer_arguments
125
126 """
127 The standard extensions that I use for both Peano and ExaHyPE.
128 """
129 LibraryDebug = "_debug"
130 LibraryRelease = ""
131 LibraryTrace = "_trace"
132 LibraryAsserts = "_asserts"
133 LibraryStats = "_stats"
134
136 self, src_path, mode=peano4.api.output.CompileMode.RELEASE
137 ):
138 """!
139
140 src_path: string
141 Path (relative or absolute) to the src directory of Peano. This path
142 should hold both the headers (in subdirectories) and all the static
143 libraries.
144
145 mode: peano4.api.output.CompileMode
146
147 """
148 self._Peano_src_directory = src_path
149 self._build_mode = mode
150
152 """!
153
154 Configure the makefile of the PETSc project
155
156 We parse the configure script's/CMAKE outcome first via parse_configure_script_outcome().
157 After that, we set the dimension manually as well as the executable name and
158 the build mode. The values for these parameters step from set_Peano4_installation().
159
160 """
161 self._project.output.makefile.parse_configure_script_outcome(
163 )
164 self._project.output.makefile.set_dimension(self._dimensions)
165 self._project.output.makefile.set_executable_name(self._executable_name)
166 self._project.output.makefile.set_mode(self._build_mode)
167
169
171 self,
172 dimensions,
173 offset,
174 domain_size,
175 plotter_precision=5,
176 ):
177 """
178
179 offset and size should be lists with dimensions double entries.
180
181 """
182 self._domain_offset = offset
183 self._domain_size = domain_size
184 self._dimensions = dimensions
185 self._plotter_precision = plotter_precision
186 if plotter_precision <= 0:
187 raise Exception("Plotter precision has to be bigger than 0")
188
189 def set_output_path(self, path):
190 self._output_path = path
191 if not self._output_path.endswith("/"):
192 self._output_path += "/"
193
194 def generate_Peano4_project(self, verbose=False):
195 """!
196
197 Build project
198
199 Construct and return the Peano4 project, i.e. all the action sets et al that
200 you require to run this Peano4 application with PETSc.
201
202 This routine generates a Peano project, i.e. the domain-specific
203 view is translated into a Peano model. Once you have called this routine,
204 any changes to the PETSc project configuration do not propagate into the Peano
205 setup anymore. If you alter the project setup, you have to call
206 generate_Peano4_project() again to get a new snapshot/version of the
207 Peano setup.
208
209 The function runs through various steps:
210
211 1. We create an instance of PETScMain that represents the C++ main file.
212
213 2. We add the essential algorithmic steps to the result project. At this
214 point, they should all be configured properly
215
216 3. We configure the makefile via __configure_makefile().
217
218 4. Add the repository holding the instances of the solver.
219
220 5. Ensure that the generated readme file holds all relevant literature and
221 links.
222
223 """
224 self._project.main = PETScMain(
225 self._project,
226 self._domain_offset,
227 self._domain_size,
228 )
229
230 self._project.solversteps.add_step(self.algorithm_step_create_grid)
231 self._project.solversteps.add_step(
233 )
234 self._project.solversteps.add_step(self._algorithm_step_init_petsc)
235 self._project.solversteps.add_step(self.algorithm_step_assemble)
236 self._project.solversteps.add_step(self._algorithm_step_solve)
237 self._project.solversteps.add_step(self.algorithm_step_map_solution_onto_mesh)
238 self._project.solversteps.add_step(self.algorithm_step_plot)
239
242
243 self._project.output.readme.add_package_description(
244 """
245
246### Peano4 PETSc backend
247
248This yet has to be written.
249
250"""
251 )
252
253 return self._project
254
256 self.dictionary = {}
257 self.dictionary["SOLVER_INSTANCES"] = [
258 (x.typename(), x.instance_name()) for x in self._solvers
259 ]
260 self.dictionary["PRECONDITIONER_TYPE"] = (
261 "::petsc::LinearEquationSystem::PreconditionerType::"
263 )
264 self.dictionary["SOLVER_TYPE"] = (
265 "::petsc::LinearEquationSystem::KSPSolverType::" + self.solver_type
266 )
267
269 """!
270
271 type_name_of_solvers: [String]
272 Name of the solvers that we should instantiate and use in the main.
273
274
275 """
277
278 templatefile_prefix = (
279 os.path.realpath(__file__).replace(".pyc", "").replace(".py", "")
280 )
281 template_name = templatefile_prefix + ".SolverRepository.template"
282 generated_repository_files = (
284 template_name + ".h",
285 template_name + ".cpp",
286 "SolverRepository",
287 self._project.namespace,
288 self._project.subdirectory + "repositories",
289 self.dictionary,
290 True,
291 True,
292 )
293 )
294
295 self._project.output.add(generated_repository_files)
296 self._project.output.makefile.add_h_file(
297 self._project.subdirectory + "repositories/SolverRepository.h",
298 generated=True,
299 )
300 self._project.output.makefile.add_cpp_file(
301 self._project.subdirectory + "repositories/SolverRepository.cpp",
302 generated=True,
303 )
304
305 def add_solver(self, solver):
306 """
307
308 Takes the solver and runs through the individual steps of the algorithm.
309 Per step, it tells the solver to inject its particular stuff to the
310 underlying observers.
311
312 """
313 print("---------------------------------------")
314 print("Create data for solver " + solver._name)
315 print("---------------------------------------")
316 print(str(solver))
317
318 solver.add_to_Peano4_datamodel(self._project.datamodel, verbose=False)
319
320 solver.add_use_statements(self.algorithm_step_create_grid)
321 solver.add_use_statements(self.algorithm_step_enumerate_and_init_solution)
322 solver.add_use_statements(self._algorithm_step_init_petsc)
323 solver.add_use_statements(self.algorithm_step_assemble)
324 solver.add_use_statements(self.algorithm_step_map_solution_onto_mesh)
325 solver.add_use_statements(self.algorithm_step_plot)
326
327 solver.add_to_create_grid(self.algorithm_step_create_grid)
328 solver.add_to_enumerate_and_init_solution(
330 )
331 solver.add_to_init_petsc(self._algorithm_step_init_petsc)
332 solver.add_to_assemble(self.algorithm_step_assemble)
333 solver.add_to_map_solution_onto_mesh(self.algorithm_step_map_solution_onto_mesh)
334 solver.add_to_plot(self.algorithm_step_plot)
335
336 solver.add_implementation_files_to_project(
337 self._project.namespace, self._project.output, self._subdirectory
338 )
339
340 self._solvers.append(solver)
341
342 pass
343
345 """
346 We export ExaHyPE's constants. Besides the constants from ExaHyPE,
347 I also export some parameters from Peano onto the ExaHyPE constants
348 file. Therefore, it is important that you parse the configure output
349 before we export the constants.
350 """
351 self._project.constants.clear()
352 offset_string = "{" + str(self._domain_offset[0])
353 size_string = "{" + str(self._domain_size[0])
354 for i in range(1, self._dimensions):
355 offset_string += ","
356 size_string += ","
357 offset_string += str(self._domain_offset[i])
358 size_string += str(self._domain_size[i])
359 offset_string += "}"
360 size_string += "}"
361 self._project.constants.add_include("""#include <bitset>""")
362 self._project.constants.add_include("""#include "tarch/la/Vector.h" """)
363 self._project.constants.add_include("""#include "peano4/utils/Globals.h" """)
364 self._project.constants.export_const_with_type(
365 "DomainOffset", offset_string, "tarch::la::Vector<Dimensions,double>"
366 )
367 self._project.constants.export_const_with_type(
368 "DomainSize", size_string, "tarch::la::Vector<Dimensions,double>"
369 )
370 self._project.constants.export("PlotterPrecision", str(self._plotter_precision))
371
372 build_string = "python3 "
373 for i in sys.argv:
374 build_string += " "
375 build_string += i
376 self._project.constants.export_string("BuildInformation", build_string)
377 self._project.constants.export_string(
378 "ConfigureInformation", self._project.output.makefile.configure_call
379 )
380
381 self._project.output.readme.add_package_description(
382 """
383
384### Peano's multigrid extension
385
386This is yet to be written, and we need to have a paper on our work.
387
388 @article{Reinarz:2020:ExaHyPE,
389 title = {ExaHyPE: An engine for parallel dynamically adaptive simulations of wave problems},
390 journal = {Computer Physics Communications},
391 volume = {254},
392 pages = {107251},
393 year = {2020},
394 issn = {0010-4655},
395 doi = {https://doi.org/10.1016/j.cpc.2020.107251},
396 url = {https://www.sciencedirect.com/science/article/pii/S001046552030076X},
397 author = {Anne Reinarz and Dominic E. Charrier and Michael Bader and Luke Bovard and Michael Dumbser and Kenneth Duru and Francesco Fambri and Alice-Agnes Gabriel and Jean-Matthieu Gallard and Sven K\"oppel and Lukas Krenz and Leonhard Rannabauer and Luciano Rezzolla and Philipp Samfass and Maurizio Tavelli and Tobias Weinzierl},
398 keywords = {Hyperbolic, PDE, ADER-DG, Finite volumes, AMR, MPI, TBB, MPI+X},
399 abstract = {ExaHyPE (An Exascale Hyperbolic PDE Engine) is a software engine for solving systems of first-order hyperbolic partial differential equations (PDEs). Hyperbolic PDEs are typically derived from the conservation laws of physics and are useful in a wide range of application areas. Applications powered by ExaHyPE can be run on a student's laptop, but are also able to exploit thousands of processor cores on state-of-the-art supercomputers. The engine is able to dynamically increase the accuracy of the simulation using adaptive mesh refinement where required. Due to the robustness and shock capturing abilities of ExaHyPE's numerical methods, users of the engine can simulate linear and non-linear hyperbolic PDEs with very high accuracy. Users can tailor the engine to their particular PDE by specifying evolved quantities, fluxes, and source terms. A complete simulation code for a new hyperbolic PDE can often be realised within a few hours - a task that, traditionally, can take weeks, months, often years for researchers starting from scratch. In this paper, we showcase ExaHyPE's workflow and capabilities through real-world scenarios from our two main application areas: seismology and astrophysics.
400 Program summary
401 Program title: ExaHyPE-Engine Program Files doi: http://dx.doi.org/10.17632/6sz8h6hnpz.1 Licensing provisions: BSD 3-clause Programming languages: C++, Python, Fortran Nature of Problem: The ExaHyPE PDE engine offers robust algorithms to solve linear and non-linear hyperbolic systems of PDEs written in first order form. The systems may contain both conservative and non-conservative terms. Solution method: ExaHyPE employs the discontinuous Galerkin (DG) method combined with explicit one-step ADER (arbitrary high-order derivative) time-stepping. An a-posteriori limiting approach is applied to the ADER-DG solution, whereby spurious solutions are discarded and recomputed with a robust, patch-based finite volume scheme. ExaHyPE uses dynamical adaptive mesh refinement to enhance the accuracy of the solution around shock waves, complex geometries, and interesting features.
402 }
403 }
404"""
405 )
406
407 for i in self._solvers:
408 self._project.output.readme.add_entry(i.create_readme_descriptor())
409
410 @property
411 def subdirectory(self):
412 return self._subdirectory
set_global_simulation_parameters(self, dimensions, offset, domain_size, plot_after_each_mesh_sweep, max_iterations=100, plotter_precision=5)
Configure global simulation settings.
Definition Project.py:159
__generate_solver_repository(self)
Definition Project.py:226
algorithm_step_map_solution_onto_mesh
Definition Project.py:100
__export_constants(self)
Export all the constrants of the project into a Configure.h file.
Definition Project.py:295
set_Peano4_installation(self, src_path, mode=peano4.api.output.CompileMode.RELEASE)
Tell the project where to find Peano.
Definition Project.py:114
__set_solver_repository_dict(self)
Definition Project.py:217
set_output_path(self, path)
Definition Project.py:179
generate_Peano4_project(self, verbose=False)
Build project.
Definition Project.py:185
__configure_makefile(self)
Parse makefile of Peano to extract the configuration settings.
Definition Project.py:135
set_load_balancing(self, load_balancer_name, load_balancer_arguments)
Set the load balancing scheme.
Definition Project.py:94
__init__(self, namespace, project_name, directory=".", executable="peano4matrixfree", subdirectory="", abstract_overwrite=True)
Create a matrix project.
Definition Project.py:49
add_solver(self, solver, auxiliary=False)
Add a new solver.
Definition Project.py:246
algorithm_step_enumerate_and_init_solution
Definition Project.py:92
Represents a Peano 4 project.
Definition Project.py:23