Peano
Loading...
Searching...
No Matches
ProjectIntoCellAndUpdateCellSolution.py
Go to the documentation of this file.
1from peano4.solversteps.ActionSet import ActionSet
2
3import peano4
4import jinja2
5
6
8 templateTouchCellFirstTime="""
9 if (
10 fineGridCell{{SOLVER_NAME}}.getType() != celldata::{{SOLVER_NAME}}::Type::Coarse
11 and
12 repositories::{{SOLVER_INSTANCE}}.projectOntoCells()
13 ) {
14 logTraceInWith2Arguments( "ProjectOntoCells", fineGridCell{{SOLVER_NAME}}.getResidual(), fineGridCell{{SOLVER_NAME}}.getSolution() );
15
16 // we intend that the internal residual vector should be reset and updated in the
17 // cell update procedure
18
19 // This vector faceSol will contain: soln on face 0 | soln on face 1 | soln on face 2 | .....
20 tarch::la::Vector< repositories::{{SOLVER_INSTANCE}}.FaceUnknownsSolution*TwoTimesD, double > faceSol;
21 for (int f=0; f<TwoTimesD; f++)
22 for (int s=0; s<repositories::{{SOLVER_INSTANCE}}.FaceUnknownsSolution; s++)
23 faceSol( f*repositories::{{SOLVER_INSTANCE}}.FaceUnknownsSolution + s ) = fineGridFaces{{SOLVER_NAME}}(f).getSolution(s);
24
25 // next, we need to project solution on face onto the cell...
26 auto projection = repositories::{{SOLVER_INSTANCE}}.applyCellFromFaceMatrix(marker.x(), marker.h(), faceSol);
27
28 // loop over each face, finish updating residual as we go
29 for (int f=0; f<repositories::{{SOLVER_INSTANCE}}.CellUnknowns; f++)
30 {
31 // project the face solution onto the cell; add to r
32 fineGridCell{{SOLVER_NAME}}.setResidual(f,
33 fineGridCell{{SOLVER_NAME}}.getResidual(f)- projection(f));
34 }
35
36 // update global residual before multiplying by the preconditioner
37 for (int i=0; i<fineGridCell{{SOLVER_NAME}}.getSolution().size(); i++) {
38 double res = fineGridCell{{SOLVER_NAME}}.getResidual( i );
39 _solverStatistics.updateGlobalResidual(res, marker.h());
40 }
41
42 // multiply by "smoother" / preconditioner
43 fineGridCell{{SOLVER_NAME}}.setResidual( repositories::{{SOLVER_INSTANCE}}.applyInvertedApproxSystemMatrix(
44 marker.x(),
45 marker.h(),
46 fineGridCell{{SOLVER_NAME}}.getResidual()
47 ));
48
49 for (int i=0; i<fineGridCell{{SOLVER_NAME}}.getSolution().size(); i++) {
50 double res = fineGridCell{{SOLVER_NAME}}.getResidual( i );
51 double du = repositories::{{SOLVER_INSTANCE}}.OmegaCell * res;
52 fineGridCell{{SOLVER_NAME}}.setSolution( i,
53 fineGridCell{{SOLVER_NAME}}.getSolution( i ) + du
54 );
55 _solverStatistics.updateGlobalSolutionUpdates(du, marker.h()(0));
56 _solverStatistics.updateMinMaxMeshSize( marker.h() );
57 }
58
59 logTraceOutWith2Arguments( "ProjectOntoCells", fineGridCell{{SOLVER_NAME}}.getResidual(), fineGridCell{{SOLVER_NAME}}.getSolution() );
60 }
61
62 """
63 def __init__(self,
64 solver,
65 descend_invocation_order=0,
66 parallel=True):
67 super( ProjectIntoCellAndUpdateCellSolution, self ).__init__(
68 descend_invocation_order,
69 parallel
70 )
71 self.d = {}
72 self.d["SOLVER_INSTANCE"] = solver.instance_name()
73 self.d["SOLVER_NAME"] = solver.typename()
74
75 def get_body_of_operation(self,operation_name):
76 result = ""
77 if operation_name==peano4.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
78 result = jinja2.Template(self.templateTouchCellFirstTime).render(**self.d)
79 pass
80 if operation_name==peano4.solversteps.ActionSet.OPERATION_BEGIN_TRAVERSAL:
81 result = jinja2.Template("""
82 _solverStatistics.clearGlobalPrecondResidualUpdate();
83 _solverStatistics.clearGlobalResidualAndSolutionUpdate();
84""").render(**self.d)
85 if operation_name==peano4.solversteps.ActionSet.OPERATION_END_TRAVERSAL:
86 result = jinja2.Template("""
87 repositories::{{SOLVER_INSTANCE}}.merge( _solverStatistics );
88""").render(**self.d)
89 return result
90
92 """!
93
94 Configure name of generated C++ action set
95
96 This action set will end up in the directory observers with a name that
97 reflects how the observer (initialisation) is mapped onto this action
98 set. The name pattern is ObserverName2ActionSetIdentifier where this
99 routine co-determines the ActionSetIdentifier. We make is reflect the
100 Python class name.
101
102 """
103 return __name__.replace(".py", "").replace(".", "_") + "_ProjectIntoCellAndUpdateCellSolution"
104
106 """!
107
108 The action set that Peano will generate that corresponds to this class
109 should not be modified by users and can safely be overwritten every time
110 we run the Python toolkit.
111
112 """
113 return False
114
115 def get_includes(self):
116 """!
117
118 We need the solver repository in this action set, as we directly access
119 the solver object. We also need access to Peano's d-dimensional loops.
120
121 """
122 return """
123#include "../repositories/SolverRepository.h"
124#include "peano4/utils/Loop.h"
125"""
126
127 def get_attributes(self):
128 """!
129
130 Return attributes as copied and pasted into the generated class.
131
132 Please note that action sets are not persistent, i.e. there is one
133 object creation per grid sweep per tree.
134
135 """
136 return """
137 ::mghype::matrixfree::solvers::SolverStatistics _solverStatistics;
138"""
139
Action set (reactions to events)
Definition ActionSet.py:6
get_includes(self)
We need the solver repository in this action set, as we directly access the solver object.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
user_should_modify_template(self)
The action set that Peano will generate that corresponds to this class should not be modified by user...