Peano
Loading...
Searching...
No Matches
PlotExactSolution.py
Go to the documentation of this file.
1# This file is part of the Peano's PETSc extension. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3from peano4.solversteps.ActionSet import ActionSet
4
5import peano4
6import jinja2
7
8
10 """!
11
12 For development purposes, we add a way to print the exact solution
13 from the cells to a file.
14
15 We do this lazily, without concerning ourselves with what indices
16 are available to us over mpi ranks.
17
18 We store everything in a std::vector, and print it out to file when
19 we are done
20 """
21
22 templateTouchCellFirstTime = """
23 if ( fineGridCell{{SOLVER_NAME}}PETScData.getType() == celldata::{{SOLVER_NAME}}PETScData::Type::Interior )
24 {
25 std::pair<int,int> localCellIndex = std::make_pair(_spacetreeId, fineGridCell{{SOLVER_NAME}}PETScData.getUnknownBaseNumber());
26 int globalCellIndex = repositories::{{SOLVER_INSTANCE}}.getLocalToGlobalMap().getGlobalIndex(localCellIndex, ::petsc::LocalToGlobalMap::Type::Cell);
27
28 // fill the vector
29 for (int i=0; i<repositories::{{SOLVER_INSTANCE}}.DoFsPerCell; i++)
30 {
31 // we will want this to be get exactSol
32 exactSolutionAtEachCellDof[globalCellIndex+i] = fineGridCell{{SOLVER_NAME}}.getExactSol(i);
33 }
34 }
35
36 """
37 templateEndTraversal = """
38 std::ofstream outfile("./exactSol.txt");
39 for (const auto& s:exactSolutionAtEachCellDof) outfile << s << "\\n";
40 outfile.close();
41
42 """
43
44 def __init__(self,
45 solver):
46 """!
47
48Initialise vertex-associated degrees of freedom
49
50The initialisation requires a solver object, as we have to know what C++
51object this solver will produce.
52
53solver: petsc.solvers.CollocatedLowOrderDiscretisation or similar solver where
54 degrees of freedom are assigned exclusively to the vertices.
55
56 """
57 super( PlotExactSolution, self ).__init__()
58 self.d = {}
59 self.d["SOLVER_INSTANCE"] = solver.instance_name()
60 self.d["SOLVER_NAME"] = solver.typename()
61
62 def get_body_of_operation(self,operation_name):
63 """!
64
65Provide C++ code snippet for peano4.solversteps.ActionSet.OPERATION_TOUCH_VERTEX_FIRST_TIME
66
67Only touchVertexFirstTime is an event where this action set actually
68does something: It inserts the template TemplateInitVertex and
69replaces it with entries from the dictionary. The latter is befilled
70in init().
71
72 """
73 result = ""
74 if operation_name==peano4.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
75 result = jinja2.Template(self.templateTouchCellFirstTime).render(**self.d)
76 pass
77
78 if operation_name==peano4.solversteps.ActionSet.OPERATION_END_TRAVERSAL:
79 result = jinja2.Template(self.templateEndTraversal).render(**self.d)
80 pass
81 return result
82
83
85 """!
86
87 Configure name of generated C++ action set
88
89 This action set will end up in the directory observers with a name that
90 reflects how the observer (initialisation) is mapped onto this action
91 set. The name pattern is ObserverName2ActionSetIdentifier where this
92 routine co-determines the ActionSetIdentifier. We make is reflect the
93 Python class name.
94
95 """
96 return __name__.replace(".py", "").replace(".", "_")
97
98
100 """!
101
102 The action set that Peano will generate that corresponds to this class
103 should not be modified by users and can safely be overwritten every time
104 we run the Python toolkit.
105
106 """
107 return False
108
109
110 def get_includes(self):
111 """!
112
113 Add some additional includes
114
115 We need the solver repository here as we access hte solver object, and we
116 also need access to all of Peano's d-dimensional for loops.
117
118 """
119 return """
120#include "../repositories/SolverRepository.h"
121#include "peano4/utils/Loop.h"
122#include <fstream>
123"""
124
125 def get_attributes(self):
126 """!
127
128 Return attributes as copied and pasted into the generated class.
129
130 Please note that action sets are not persistent, i.e. there is one
131 object creation per grid sweep per tree.
132
133 """
134 return """
135 int _spacetreeId;
136 std::vector<double> exactSolutionAtEachCellDof;
137"""
138
140 body = """
141 _spacetreeId = treeNumber;
142 exactSolutionAtEachCellDof.resize(
143 repositories::{{SOLVER_INSTANCE}}.getLocalToGlobalMap().getTotalNumberOfCells()
144 );
145
146"""
147
148 return jinja2.Template(body).render(**self.d)
For development purposes, we add a way to print the exact solution from the cells to a file.
get_attributes(self)
Return attributes as copied and pasted into the generated class.
get_action_set_name(self)
Configure name of generated C++ action set.
__init__(self, solver)
Initialise vertex-associated degrees of freedom.
user_should_modify_template(self)
The action set that Peano will generate that corresponds to this class should not be modified by user...
get_constructor_body(self)
Define a tailored constructor body.
get_body_of_operation(self, operation_name)
Provide C++ code snippet for peano4.solversteps.ActionSet.OPERATION_TOUCH_VERTEX_FIRST_TIME
get_includes(self)
Add some additional includes.
Action set (reactions to events)
Definition ActionSet.py:6