Peano
Loading...
Searching...
No Matches
ParticleTreeAnalysis.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3from peano4.solversteps.ActionSet import ActionSet
4
5
6import jinja2
7
9
11
12
13
15 """
16
17 Create the cell marker that is used by all statistics
18
19 You can either create such a cell marker yourself, or you can make the
20 particle tree analysis create one for you.
21
22 @see ParticleTreeAnalysis.__init__()
23
24
25 """
26 cell_marker = peano4.datamodel.DaStGen2( name )
27
28 cell_marker.data.add_attribute( dastgen2.attributes.Integer("NewNumberOfParticles") )
29 cell_marker.data.add_attribute( dastgen2.attributes.Integer("NumberOfParticles") )
30 cell_marker.data.add_attribute( dastgen2.attributes.Boolean("NewParentOfRefinedCell") )
31 cell_marker.data.add_attribute( dastgen2.attributes.Boolean("ParentOfRefinedCell") )
32
33 return cell_marker
34
35
36
38 """
39
40 This action set implements an analysed tree grammar
41
42 The grammar goes down to the finest level and writes into each
43 leaf cell of the tree the following data:
44
45 - Number of particles within this cell
46 - The flag ParentOfRefinedCell is set to false
47
48 We then run through the tree bottom-up and set these two flags
49 on each spacetree cell along the following rules:
50
51 - Number of particles of a refined cell in the spacetree equals
52 the number of all the children cells plus the local particles,
53 i.e. those particles stored on the present level.
54 - ParentOfRefinedCell becomes true if one of the children is
55 refined. If none of the children is refined, the flag is false.
56
57 I use the analysed tree data to determine if a tree's has to be
58 refined further as it hosts too many particles. I also use it to
59 steer if a tree has to be coarsened if there are not enough
60 particles. Finally, I use the flag ParentOfRefinedCell to ensure
61 that we always remove at most one level from the tree in one
62 tree sweep, as I only erase refined tree nodes for which
63 ParentOfRefinedCell does not hold.
64
65
66
67 :: Usage ::
68
69 I need a particle set to work properly. You also have to
70 invoke add the tree cell properties to the tree and tell
71 every observer that invokes this particle set to handle
72 the tree marker:
73
74
75 my_Peano_project = ...
76
77 my_tree_analysis = ParticleTreeAnalysis(...)
78 my_Peano_project.add_cell(my_tree_analysis.cell_marker)
79
80
81
82 my_algorithmic_step = peano4.solversteps.Step( ... )
83 my_algorithmic_step.use_cell(my_tree_analysis.cell_marker)
84
85
86
87 ## Attributes
88
89 particle_set: ParticleSet
90 This is the particle set which is to be analysed.
91
92 cell_marker: peano4.datamodel.DaStGen2 or None
93 If you pass in None, the class will create an appropriate
94 cell marker itself. You can also add in a cell marker that
95 shall be used. In both cases, you have to ensure that your
96 code handles the cell marker accordingly, i.e. ensures that
97 the markers are streamed properly.
98
99 """
100 def __init__(self,
101 particle_set,
102 cell_marker = None):
103 super(ParticleTreeAnalysis,self).__init__(descend_invocation_order=1,parallel=False)
104 if cell_marker!=None:
105 self.cell_marker = cell_marker
106 self._cell_data_name = cell_marker.name
107 else:
108 self._cell_data_name = particle_set.name + "CellStatistics"
109 self.cell_marker = create_cell_marker(self._cell_data_name)
110
111 self.d = {
112 "CELL_DATA_NAME": self._cell_data_name,
113 "PARTICLE_SET_NAME": particle_set.name,
114 "PARTICLE_NAME": particle_set.particle_model.name
115 }
116 pass
117
118
119 __Template_TouchCellFirstTime = jinja2.Template( """
120 fineGridCell{{CELL_DATA_NAME}}.setNumberOfParticles( fineGridCell{{CELL_DATA_NAME}}.getNewNumberOfParticles() );
121 fineGridCell{{CELL_DATA_NAME}}.setParentOfRefinedCell( fineGridCell{{CELL_DATA_NAME}}.getNewParentOfRefinedCell() );
122 fineGridCell{{CELL_DATA_NAME}}.setNewNumberOfParticles(0);
123 fineGridCell{{CELL_DATA_NAME}}.setNewParentOfRefinedCell( false );
124""")
125
126
127 __Template_CreateCell = jinja2.Template("""
128 fineGridCell{{CELL_DATA_NAME}}.setNumberOfParticles( std::numeric_limits<int>::max() );
129 fineGridCell{{CELL_DATA_NAME}}.setNewNumberOfParticles( 0 );
130 fineGridCell{{CELL_DATA_NAME}}.setParentOfRefinedCell( true );
131 fineGridCell{{CELL_DATA_NAME}}.setNewParentOfRefinedCell( true );
132""")
133
134
135 __Template_TouchCellLastTime = jinja2.Template("""
136 int count = 0;
137 for (int i=0; i<TwoPowerD; i++) {
138 count += fineGridVertices{{PARTICLE_SET_NAME}}(i).size();
139 }
140
141 fineGridCell{{CELL_DATA_NAME}}.setNewNumberOfParticles(
142 fineGridCell{{CELL_DATA_NAME}}.getNewNumberOfParticles()
143 +
144 count
145 );
146
147 coarseGridCell{{CELL_DATA_NAME}}.setNewNumberOfParticles(
148 coarseGridCell{{CELL_DATA_NAME}}.getNewNumberOfParticles()
149 +
150 fineGridCell{{CELL_DATA_NAME}}.getNewNumberOfParticles()
151 );
152
153 if ( marker.willBeRefined() ) {
154 coarseGridCell{{CELL_DATA_NAME}}.setNewParentOfRefinedCell( true );
155 }
156""")
157
158
160 return ""
161
162
164 return ""
165
166
168 return " return std::vector< peano4::grid::GridControlEvent >();\n"
169
170
172 return __name__.replace(".py", "").replace(".", "_")
173
174
176 return False
177
178
179 def get_attributes(self):
180 return ""
181
182
183 __Template_Includes = jinja2.Template("""
184#include "../vertexdata/{{PARTICLE_SET_NAME}}.h"
185#include "../globaldata/{{PARTICLE_NAME}}.h"
186""")
187
188
189 def get_includes(self):
190 return self.__Template_Includes.render(**self.d)
191
192
193 def get_body_of_operation(self,operation_name):
194 result = "\n"
195 if operation_name==ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
196 result = self.__Template_TouchCellFirstTime.render(**self.d)
197 if operation_name==ActionSet.OPERATION_TOUCH_CELL_LAST_TIME:
198 result = self.__Template_TouchCellLastTime.render(**self.d)
199 if operation_name==ActionSet.OPERATION_CREATE_CELL:
200 result = self.__Template_CreateCell.render(**self.d)
201 return result
202
Default superclass for any data model in Peano which is stored within the grid.
Definition DaStGen2.py:47
Action set (reactions to events)
Definition ActionSet.py:6
This action set implements an analysed tree grammar.
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
get_attributes(self)
Return attributes as copied and pasted into the generated class.
user_should_modify_template(self)
Is the user allowed to modify the output.
create_cell_marker(name)
Create the cell marker that is used by all statistics.