Peano
Loading...
Searching...
No Matches
SolverToCPPFiles.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
10
11from .Solver import Solver
12
13
15 TEMPLATE_ABSTRACT_HEADER_FILE = """
16// This file is part of the ExaHyPE2 project. For conditions of distribution and
17// use, please see the copyright notice at www.peano-framework.org
18#pragma once
19
20#include "exahype2/RefinementControl.h"
21#include "exahype2/Solver.h"
22#include "tarch/la/Vector.h"
23#include "tarch/accelerator/accelerator.h"
24#include "tarch/NonCriticalAssertions.h"
25
26#include "peano4/utils/Globals.h"
27
28#include "Constants.h"
29
30
31{% for item in NAMESPACE -%}
32 namespace {{ item }} {
33 {# Empty line here #}
34{%- endfor %}
35 class {{CLASSNAME}};
36
37{% for item in NAMESPACE -%}
38 }
39{%- endfor %}
40
41
42class {{NAMESPACE | join("::")}}::{{CLASSNAME}}: public ::exahype2::Solver {
43 public:
44 {{CLASSNAME}}();
45
46 static constexpr int NumberOfUnknowns = {{NUMBER_OF_UNKNOWNS}};
47 static constexpr int NumberOfAuxiliaryVariables = {{NUMBER_OF_AUXILIARY_VARIABLES}};
48 static constexpr int PatchSize = {{PATCH_SIZE}};
49 static constexpr int HaloSize = {{HALO_SIZE}};
50
51 {{VARIABLES}}
52
53 {{PDE_TERM_DECLARATIONS}}
54
55 {{ADDITIONAL_METHOD_DECLARATIONS}}
56
57 protected:
58 static tarch::logging::Log _log;
59
60 {{ADDITIONAL_ATTRIBUTES}}
61};
62
63{# Empty line here #}
64"""
65
66 TEMPLATE_ABSTRACT_CPP_FILE = """
67#include "{{CLASSNAME}}.h"
68#include "exahype2/RefinementControl.h"
69{{USER_SOLVER_INCLUDES}}
70
71tarch::logging::Log {{NAMESPACE | join("::")}}::{{CLASSNAME}}::_log( "{{NAMESPACE | join("::")}}::{{CLASSNAME}}" );
72
73
74{{NAMESPACE | join("::")}}::{{CLASSNAME}}::{{CLASSNAME}}() {
75}
76
77
78{{PDE_TERM_DEFINITIONS}}
79
80{{ADDITIONAL_METHOD_DEFINITIONS}}
81"""
82
83 TEMPLATE_SOLVER_HEADER_FILE = """
84// This file has been generated by the ExaHyPE2 api which is part
85// of the ExaHyPE2 project. For conditions of distribution and
86// use of ExaHyPE2, please see the copyright notice at
87// www.peano-framework.org. However, this file is only a suggestion
88// and can be freely changed by the ExaHyPE 2 user who might decide
89// to apply their own copyright.
90#pragma once
91
92#include "Abstract{{CLASSNAME}}.h"
93
94
95{% for item in NAMESPACE -%}
96 namespace {{ item }} {
97 {# Empty line here #}
98{%- endfor %}
99 class {{CLASSNAME}};
100
101{% for item in NAMESPACE -%}
102 }
103{%- endfor %}
104
105
106class {{NAMESPACE | join("::")}}::{{CLASSNAME}}: public {{NAMESPACE | join("::")}}::Abstract{{CLASSNAME}} {
107 public:
108 {{CLASSNAME}}() = default;
109
110 {{PDE_TERM_STUB_DECLARATIONS}}
111};
112
113{# Empty line here #}
114"""
115
116 TEMPLATE_SOLVER_CPP_FILE = """
117#include "{{CLASSNAME}}.h"
118
119
120{{PDE_TERM_STUB_DEFINITIONS}}
121"""
122
123 """!
124
125 Default implementation that takes a solver and maps it onto two C++ classes:
126 An abstract base class and a class for the user to implement it.
127
128 The user class is only generated if at least one PDE term lacks an
129 implementation (@see Solver.requires_user_class). It is never
130 overwritten once it exists, and it is added to the Makefile as a
131 non-generated file. Without it, the solver repository instantiates
132 the abstract base class directly.
133
134 """
135
136 def __init__(self, solver: Solver):
137 super().__init__(solver)
138
139 def _variables_declaration(self) -> str:
140 """!
141
142 Nested struct Variables with the position of each named variable
143 within Q, or the empty string if the solver's variables carry no
144 names. User code (and code injected from Python) then can write
145 Q[Variables::rho] instead of Q[0].
146
147 """
148 declarations = []
149 if self._solver._variable_positions:
150 constants = "\n".join(
151 f" static constexpr int {variable_name} = {position};"
152 for variable_name, position in self._solver._variable_positions.items()
153 )
154 declarations.append(
155 "/**\n"
156 " * Positions of the named unknowns within Q\n"
157 " */\n"
158 " struct Variables {\n" + constants + "\n };"
159 )
160 for auxiliary in self._solver.auxiliary_variables:
161 declarations.append(auxiliary.variables_declaration())
162 return "\n".join(declarations)
163
165 self,
166 exahype2_project,
167 peano_project,
168 ):
169 """!
170
171 Dump the solver into actual C++ classes.
172
173 """
174 class_name = "Abstract" + self._solver.name
175 templatefile_prefix = (
176 os.path.realpath(__file__).replace(".pyc", "").replace(".py", "")
177 )
178
179 peano4.api.Logger().log(
180 "Map class {} onto C++ class {} using {}".format(
181 self._solver.name, class_name, templatefile_prefix
182 )
183 )
184
185 qualified_class_name = "::".join(peano_project.namespace) + "::" + class_name
186
187 local_d = {
188 "NUMBER_OF_UNKNOWNS": self._solver._unknowns,
189 "NUMBER_OF_AUXILIARY_VARIABLES": self._solver.number_of_auxiliary_variables,
190 "PATCH_SIZE": self._solver.numerical_scheme.patch_size,
191 "HALO_SIZE": self._solver.numerical_scheme.halo_size,
192 "USER_SOLVER_INCLUDES": self._solver.user_solver_includes,
193 "VARIABLES": self._variables_declaration(),
194 "PDE_TERM_DECLARATIONS": "\n".join(
195 term.generator_for(self._solver).generate_declaration() for term in self._solver.pde_terms
196 ),
197 "PDE_TERM_DEFINITIONS": "\n".join(
198 term.generator_for(self._solver).generate_out_of_class_definitions(qualified_class_name)
199 for term in self._solver.pde_terms
200 ),
201 "ADDITIONAL_ATTRIBUTES": "\n".join(
202 self._solver._abstract_solver_attributes
203 ),
204 "ADDITIONAL_METHOD_DECLARATIONS": "\n".join(
205 declaration
206 for declaration, definition_generator in self._solver._abstract_solver_methods
207 ),
208 "ADDITIONAL_METHOD_DEFINITIONS": "\n".join(
209 definition_generator(qualified_class_name)
210 for declaration, definition_generator in self._solver._abstract_solver_methods
211 ),
212 }
213
214 generated_files = (
216 headerfile_template=self.TEMPLATE_ABSTRACT_HEADER_FILE,
217 cppfile_template=self.TEMPLATE_ABSTRACT_CPP_FILE,
218 classname=class_name,
219 namespace=peano_project.namespace,
220 subdirectory=peano_project.temporary_subdirectory,
221 dictionary=local_d,
222 default_overwrite=True,
223 apply_iteratively=False,
224 )
225 )
226 peano_project.output.add(generated_files)
227 peano_project.output.makefile.add_cpp_file(
228 peano_project.subdirectory + class_name + ".cpp", generated=True
229 )
230
231 if not self._solver.requires_user_class:
232 return
233
234 solver_class_name = self._solver.name
235 qualified_solver_class_name = (
236 "::".join(peano_project.namespace) + "::" + solver_class_name
237 )
238
239 solver_local_d = {
240 "PDE_TERM_STUB_DECLARATIONS": "\n".join(
241 term.generator_for(self._solver).generate_stub_declaration()
242 for term in self._solver.pde_terms
243 ),
244 "PDE_TERM_STUB_DEFINITIONS": "\n".join(
245 term.generator_for(self._solver).generate_stub_definition(qualified_solver_class_name)
246 for term in self._solver.pde_terms
247 ),
248 }
249
250 solver_generated_files = (
252 headerfile_template=self.TEMPLATE_SOLVER_HEADER_FILE,
253 cppfile_template=self.TEMPLATE_SOLVER_CPP_FILE,
254 classname=solver_class_name,
255 namespace=peano_project.namespace,
256 subdirectory=peano_project.subdirectory,
257 dictionary=solver_local_d,
258 default_overwrite=False,
259 apply_iteratively=False,
260 )
261 )
262 peano_project.output.add(solver_generated_files)
263 # Not flagged as generated: the user owns these files, so the
264 # Makefile's clean targets must not delete them.
265 peano_project.output.makefile.add_h_file(
266 peano_project.subdirectory + solver_class_name + ".h", generated=False
267 )
268 peano_project.output.makefile.add_cpp_file(
269 peano_project.subdirectory + solver_class_name + ".cpp", generated=False
270 )
str _variables_declaration(self)
Nested struct Variables with the position of each named variable within Q, or the empty string if the...
construct_output(self, exahype2_project, peano_project)
Dump the solver into actual C++ classes.
Turns a Solver into actual classes.
Definition Solver.py:44
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37