Peano
Loading...
Searching...
No Matches
Solver.py
Go to the documentation of this file.
1# This file is part of the Peano multigrid project. For conditions of
2# distribution and use, please see the copyright notice at www.peano-framework.org
3import peano4
4import dastgen2
5
6import numpy as np
7from numpy.polynomial.legendre import leggauss
8from scipy.interpolate import lagrange
9
10from abc import abstractmethod
11
12class Solver(object):
13 """!
14
15Abstract base class for all PETSc solvers
16
17A PETSc solver in Peano is Python class which represents one solver aka one
18PDE problem. Various of these solvers might have to be combined in one
19project.
20
21It is the project's job to define which mesh traversals (grid
22construction, visualisation, ...) are required, how to run through these,
23how to load balance. Each solver in return will know what unknows we require
24per mesh entity (vertex, face, ...). Most solvers will create a Python
25representation of these unknowns in their constructor. Once the user has
26constructed a solver and added it to the underlying
27project, is it the project's job to ask each solver
28
29- can you add your data model to the underlying Peano project;
30- can you tell me which data entities all of the mesh traversals have to
31 use;
32- add your particular action sets to the grid construction, initialisation,
33 visualisation, ...
34
35For further information how the solvers add their solver-specific data to the
36project's generic mesh traversals, consult also generic documentation in peano4.actionsets.ActionSet.
37
38
39## Realisation
40
41Most solvers will split the realisation of the solver into different parts.
42
43First and foremost, it will have to manage data associated with the grid vertices,
44faces, cells. This is the static data model. As I don't want to write classes
45for these unknowns, the solvers typically use DaStGen 2, which simply is a
46code generator to create a class per data model. This part is all handled
47within Python, and you should not see them in the output. If you want to study
48these models however, you can peek into the generated vertexdata or facedata
49repositories.
50
51For the actual code semantics, most solvers introduce a type which represents
52the solver. This one defines, for example, what the initial value looks
53like. It is actually realised as two classes: An abstract base class and its
54implementation. The abstract base class contains all the variables that can
55be modified via Python. The realisation itself is then the actual C++ code
56which users might want to modify after the Python call has succeeded and set
57everything up. That is: the Python PETSc API will give you both an abstract
58solver class and its subclass in the working directory. The abstract base
59class will be overwritten every time you run the Python script. The subclass
60is only a template that you should befill with your problem-specific knowledge.
61It will not be overwritten by any subsequent Python calls.
62
63The actual solver class that you have to implement yourself is instantiated
64within a file called repositories/SolverRepository. This one is generated, too,
65but you can peek into it. It instantiates each solver exactly one and this is
66the object instance against which the actual solver algorithm operates from
67hereon.
68
69The glue between Peano and the solver is the action sets, i.e. the classes that encode how the grid
70traversal and observations made throughout the traversal are mapped onto
71either Peano's routines, routines from the toolbox, or functions from the
72solver objects. These action sets define what happens if the code reads a
73vertex for the first time of a mesh traversal, e.g.
74
75
76## Links to high level concepts
77
78- For the types of mesh entities, please consult @ref documentation_multigrid_boundary_conditions.
79
80
81
82 """
83
84
85 def __init__(self,
86 name,
87 min_h,
88 max_h):
89 """
90
91 name: String
92 Every solver has a unique name.
93
94 """
95 self._name = name
96 self.min_h = min_h
97 self.max_h = max_h
98
100 self._face_petsc_data = peano4.datamodel.DaStGen2( name + "PETScData" )
101 self._cell_petsc_data = peano4.datamodel.DaStGen2( name + "PETScData" )
102
103 self._vertex_petsc_data.data.add_attribute( dastgen2.attributes.Integer( "unknownBaseNumber" ) )
104 self._face_petsc_data.data.add_attribute( dastgen2.attributes.Integer( "unknownBaseNumber" ) )
105 self._cell_petsc_data.data.add_attribute( dastgen2.attributes.Integer( "unknownBaseNumber" ) )
106
107 self._vertex_petsc_data.data.add_attribute( dastgen2.attributes.Enumeration( name="type", variants=["Boundary", "Interior", "Coarse", "Outside", "Undefined"] ) )
108 self._face_petsc_data.data.add_attribute( dastgen2.attributes.Enumeration( name="type", variants=["Boundary", "Interior", "Coarse", "Outside", "Undefined"] ) )
109 self._cell_petsc_data.data.add_attribute( dastgen2.attributes.Enumeration( name="type", variants=["Interior", "Coarse", "Outside", "Undefined"] ) )
110 pass
111
112
113 @abstractmethod
114 def add_to_plot(self, observer):
115 """!
116
117 Add whatever action set you want to use to observer.
118
119 """
120 assert False, "should not be called"
121
122
123 @abstractmethod
124 def add_to_create_grid(self, observer):
125 """!
126
127 Add whatever action set you want to use to observer.
128
129 """
130 assert False, "should not be called"
131
132 @abstractmethod
134 """!
135
136 Add whatever action set you want to use to observer.
137
138 """
139 assert False, "should not be called"
140
141 @abstractmethod
142 def add_to_init_petsc(self, observer):
143 """!
144
145 Add whatever action set you want to use here. I am not sure if there is a
146 real grid traversal tied to this step. So maybe nothing is called. Has to
147 be checked. Anyway, most solvers leave this one blank.
148
149 """
150 assert False, "should not be called"
151
152
153 @abstractmethod
154 def add_to_assemble(self, observer):
155 """!
156
157 Add whatever action set you want to use to observer.
158
159 """
160 assert False, "should not be called"
161
162
163 @abstractmethod
164 def add_to_map_solution_onto_mesh(self, observer):
165 """
166
167 Add whatever action set you want to use to observer.
168
169 """
170 assert False, "should not be called"
171
172
173 @abstractmethod
174 def add_to_plot(self, observer):
175 """
176
177 Add whatever action set you want to use to observer.
178
179 """
180 assert False, "should not be called"
181
182 def add_to_Peano4_datamodel( self, datamodel, verbose ):
183 """!
184
185 Initialise index model
186
187 We build up a data model, which is really an index model in this case.
188 Every vertex, face and cell can hold a data model which is only one integer.
189 This integer encodes the first index of a matrix unknown held by the
190 grid entity. For the plain DG code, there are no vertex unknowns. However,
191 we have two types of face indices: Those for the projection and those for
192 the solution of the Riemann problem.
193
194 You might want to overwrite this routine, but please ensure that you still
195 call this superclass implementation, too.
196
197 """
198 datamodel.add_vertex(self._vertex_petsc_data)
199 datamodel.add_face(self._face_petsc_data)
200 datamodel.add_cell(self._cell_petsc_data)
201 pass
202
203 def add_use_statements(self, observer):
204 """!
205
206 Register the index numbers to be used in each and every mesh traversal. You
207 can overwrite the routine and add your own stuff. However, please ensure
208 this routine still is invoked, too.
209
210 """
211 observer.use_vertex(self._vertex_petsc_data)
212 observer.use_face(self._face_petsc_data)
213 observer.use_cell(self._cell_petsc_data)
214 pass
215
216
217 @abstractmethod
218 def add_implementation_files_to_project(self, namespace, output):
219 """!
220
221This routine is typically not invoked by a user.
222
223output: peano4.output.Output
224 Add artefacts this output in any implementation.
225
226 """
227 assert False, "should not be called"
228
229
230 def typename(self):
231 return self._name;
232
233
234 def instance_name(self):
235 """!
236
237Return the name of the object that will be created for this solver.
238
239 """
240 return "instanceOf" + self.typename()
241
242 @property
244 raise NotImplementedError
245
246 @property
248 raise NotImplementedError
249
250 @property
252 raise NotImplementedError
253
254 @abstractmethod
256 """!
257
258 Should really be abstract and properly be initialised
259
260 @todo Has to be implemented properly, i.e. should at least report on the
261 type and the name of the solver
262
263 """
264 return "Yet to be written"
265
266 @property
267 def name(self):
268 return self._name
Abstract base class for all PETSc solvers.
Definition Solver.py:12
add_use_statements(self, observer)
Register the index numbers to be used in each and every mesh traversal.
Definition Solver.py:203
number_of_matrix_entries_per_cell(self)
Definition Solver.py:251
add_to_plot(self, observer)
Add whatever action set you want to use to observer.
Definition Solver.py:114
instance_name(self)
Return the name of the object that will be created for this solver.
Definition Solver.py:234
__init__(self, name, min_h, max_h)
name: String Every solver has a unique name.
Definition Solver.py:88
add_to_init_petsc(self, observer)
Add whatever action set you want to use here.
Definition Solver.py:142
add_to_enumerate_and_init_solution(self, observer)
Add whatever action set you want to use to observer.
Definition Solver.py:133
number_of_matrix_entries_per_face(self)
Definition Solver.py:247
add_to_assemble(self, observer)
Add whatever action set you want to use to observer.
Definition Solver.py:154
create_readme_descriptor(self)
Should really be abstract and properly be initialised.
Definition Solver.py:255
add_to_Peano4_datamodel(self, datamodel, verbose)
Initialise index model.
Definition Solver.py:182
add_implementation_files_to_project(self, namespace, output)
This routine is typically not invoked by a user.
Definition Solver.py:218
add_to_create_grid(self, observer)
Add whatever action set you want to use to observer.
Definition Solver.py:124
add_to_map_solution_onto_mesh(self, observer)
Add whatever action set you want to use to observer.
Definition Solver.py:164
number_of_matrix_entries_per_vertex(self)
Definition Solver.py:243
Wrapper around C++ enumerations which is not a datatype supported natively by MPI.
Definition Enumeration.py:6
Default superclass for any data model in Peano which is stored within the grid.
Definition DaStGen2.py:47