Peano
Loading...
Searching...
No Matches
LinearCombination.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 os
4
5import exahype2.api.dslhype as dslhype
6
7
8
39 solution: "double**",
40 estimates: "double**",
41 output: "double**",
42 weights: "const double*",
43 timeStepSize: "double",
44) -> "void":
45 dt = Float("timeStepSize")
46 Q = DataBlock(
47 [[0, NumberOfUnknowns + NumberOfAuxiliaryVariables], [0, NumberOfDoFs], [0, 1]],
48 "solution",
49 )
50 K = DataBlock(
51 [[0, NumberOfUnknowns], [0, NumberOfDoFs], [0, NumberOfEstimates]], "estimates"
52 )
53 out = DataBlock(
54 [[0, NumberOfUnknowns + NumberOfAuxiliaryVariables], [0, NumberOfDoFs], [0, 1]],
55 "output",
56 )
57 W = DiagonalMatrix(NumberOfEstimates, "weights")
58 out = Q[:, :, :]
59 dot(W, K[0:NumberOfUnknowns, :, :], contribution)
60 out[0:NumberOfUnknowns, :, 0] = out[0:NumberOfUnknowns, :, 0] + dt * contribution
61
62
63LINEAR_COMBINATION_KERNEL_NAME = "linearCombinationOfRungeKuttaEstimates"
64LINEAR_COMBINATION_KERNEL_FILE = "rungekutta_linear_combination"
65
66
67def generate_linear_combination_kernel(namespace, output_directory):
68 """!
69
70 Translate linearCombinationOfRungeKuttaEstimates() into C++
71
72 The kernel ends up in kernels/rungekutta_linear_combination.h in the
73 output directory. It is a function template over NumberOfDoFs,
74 NumberOfUnknowns, NumberOfAuxiliaryVariables and NumberOfEstimates within
75 namespace. It does not depend on any solver, nor on the Runge-Kutta
76 scheme (the weights and the number of estimates enter at the call site),
77 so several solvers within one project can share it. Each of them calls
78 this routine, but only the first call generates the file: The DSLHyPE
79 translation appends to existing files, i.e. a second generation would
80 define the function template twice.
81
82 @param namespace: [str]
83 Namespace of the generated function template, usually the Peano
84 project's namespace.
85
86 @param output_directory: str
87 Directory the kernels/ subdirectory is created in, usually the Peano
88 project's directory.
89
90 """
91 header = os.path.join(
92 output_directory, "kernels", LINEAR_COMBINATION_KERNEL_FILE + ".h"
93 )
94 if os.path.exists(header):
95 return
96
97 dslhype.translate_dslhype_function(
98 function=linearCombinationOfRungeKuttaEstimates,
99 function_namespace=namespace,
100 template_parameters=[
103 "NumberOfUnknowns", dslhype.SyntaxTree.TInteger()
104 ),
106 "NumberOfAuxiliaryVariables", dslhype.SyntaxTree.TInteger()
107 ),
109 "NumberOfEstimates", dslhype.SyntaxTree.TInteger()
110 ),
111 ],
112 functor_parameters=[],
113 pde_terms=[],
114 output_file_name=LINEAR_COMBINATION_KERNEL_FILE,
115 target=dslhype.Target.Serial,
116 stateless=False,
117 output_directory=output_directory,
118 )
120 os.path.join(output_directory, "kernels"), LINEAR_COMBINATION_KERNEL_FILE
121 )
122
123
124def include_implementation_into_header(directory: str, file_name: str):
125 """!
126
127 Make a generated kernel header pull in the template definitions
128
129 The translation of a DSLHyPE function yields the declaration in
130 file_name.h and the definition of the function template in
131 file_name.cpph, which itself includes the header. We therefore guard the
132 header and let it include the definitions at its end.
133
134 @param directory: str
135 Directory of the kernels.
136
137 @param file_name: str
138 Name of the kernel without suffix.
139
140 """
141 header = os.path.join(directory, file_name + ".h")
142 with open(header, "r") as file:
143 content = file.read()
144 with open(header, "w") as file:
145 file.write("#pragma once\n" + content + f'\n#include "{file_name}.cpph"\n')
146
147
149 namespace,
150 number_of_dofs,
151 unknowns,
152 auxiliary_variables,
153 weights,
154 solution,
155 estimates,
156 output,
157 time_step_size,
158):
159 """!
160
161 C++ code which invokes the kernel from generate_linear_combination_kernel()
162
163 The code is a block of its own. It sets up the arrays of pointers the
164 kernel expects and stores the weights as compile-time constants.
165
166 Only the stages with a non-zero weight enter the kernel call. This is
167 not an optimisation only: The estimates of the stages which a
168 combination does not need may be uninitialised, e.g. the estimates of
169 the later stages before the first time step has completed. Multiplying
170 them with zero would still yield NaN if they hold NaN. If all weights
171 are zero, the code copies the solution into the output.
172
173 @param namespace: [str]
174 Namespace of the kernel, see generate_linear_combination_kernel().
175
176 @param number_of_dofs: int
177 Degrees of freedom per cell.
178
179 @param unknowns: int
180 Number of unknowns per degree of freedom.
181
182 @param auxiliary_variables: int
183 Number of auxiliary variables per degree of freedom.
184
185 @param weights: [float]
186 One weight per Runge-Kutta stage, e.g. a row of
187 exahype2.api.rungekutta.ButcherTableau.weight_matrix().
188
189 @param solution: str
190 C++ expression for the pointer to the input data.
191
192 @param estimates: str
193 C++ expression for the pointer to the estimates of all stages. They
194 are stored one after the other, each with number_of_dofs*unknowns
195 entries.
196
197 @param output: str
198 C++ expression for the pointer to the output data. May equal solution.
199
200 @param time_step_size: str
201 C++ expression for the time step size.
202
203 """
204 stages = [stage for stage, weight in enumerate(weights) if weight != 0.0]
205 if not stages:
206 if solution == output:
207 return "{}"
208 return f"""{{
209 std::copy_n( {solution}, {number_of_dofs * (unknowns + auxiliary_variables)}, {output} );
210 }}"""
211
212 stage_pointers = ", ".join(
213 f"{estimates} + {stage * number_of_dofs * unknowns}" for stage in stages
214 )
215 weight_values = ", ".join(repr(float(weights[stage])) for stage in stages)
216 return f"""{{
217 static constexpr double weights[{len(stages)}] = {{ {weight_values} }};
218 double* solutionPointers[1] = {{ {solution} }};
219 double* estimatePointers[{len(stages)}] = {{ {stage_pointers} }};
220 double* outputPointers[1] = {{ {output} }};
221 ::{"::".join(namespace)}::{LINEAR_COMBINATION_KERNEL_NAME}<
222 {number_of_dofs},
223 {unknowns},
224 {auxiliary_variables},
225 {len(stages)}
226 >(solutionPointers, estimatePointers, outputPointers, weights, {time_step_size});
227 }}"""
create_linear_combination_call(namespace, number_of_dofs, unknowns, auxiliary_variables, weights, solution, estimates, output, time_step_size)
C++ code which invokes the kernel from generate_linear_combination_kernel().
generate_linear_combination_kernel(namespace, output_directory)
Translate linearCombinationOfRungeKuttaEstimates() into C++.
include_implementation_into_header(str directory, str file_name)
Make a generated kernel header pull in the template definitions.
"void" linearCombinationOfRungeKuttaEstimates("double**" solution, "double**" estimates, "double**" output, "const double*" weights, "double" timeStepSize)
DSLHyPE kernel for the linear combinations of Runge-Kutta.