Peano
Loading...
Searching...
No Matches
ActionSet.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
3import os
4import re
5
6from .Helper import write_file
7
8from .Overwrite import Overwrite
9
11
12class ActionSet(object):
13 def __init__(self,classname,namespace,subdirectory,implementation = None):
14 """!
15
16 Implementation of an Action Set
17
18 Please consult peano4.solversteps.ActionSet for a description.
19
20 implementation Should be of type peano4.solversteps.Mapping or None. If
21 it is None, then the generated stuff will be a sole
22 interface.
23
24 """
25 self.classname = classname
26 self.namespace = namespace
27 self.subdirectory = subdirectory
28 self.operations = [
29 (peano4.solversteps.ActionSet.OPERATION_BEGIN_TRAVERSAL,"void"),
30 (peano4.solversteps.ActionSet.OPERATION_END_TRAVERSAL,"void")
31 ]
32 self.include_files = []
33 self.typedefs = []
34 self.implementation = implementation
35
36 def add_operation(self,name,signature):
37 """
38 signature is a long tuple. The first entry is the name of the routine,
39 the second entry is the result type. From thereon, one entry gives the
40 name of an attribute, the second one the type.
41 """
42 self.operations.append(signature)
43
44 def __generate_includes(self, outputfile):
45 for i in self.include_files: outputfile.write( '#include "{}"\n'.format(i) )
46
47 def __get_operation_arguments(self,operation):
48 result = "(\n "
49 i = 2
50 while i<len(operation):
51 result += operation[i+1]
52 result += " "
53 result += operation[i]
54 i+=2
55 if i<len(operation):
56 result += ",\n "
57
58 result += ")"
59 return result
60
61 def __generate_operation(self,outputfile,operation):
62 """
63 outputfile points to file, operation to an operation object as added by the solver step
64 """
65 if self.implementation == None:
66 outputfile.write( " virtual " )
67 else:
68 outputfile.write( " " )
69 outputfile.write( operation[1] )
70 outputfile.write( " ")
71 outputfile.write( operation[0] )
72 outputfile.write( self.__get_operation_arguments(operation) )
73 if self.implementation == None:
74 outputfile.write( " = 0")
75 outputfile.write( ";\n\n")
76
78 full_qualified_classname = ""
79 for i in self.namespace:
80 full_qualified_classname += i
81 full_qualified_classname += "::"
82 full_qualified_classname += self.classname
83 return full_qualified_classname
84
85 def __generate_header(self,overwrite,directory):
86 filename = directory + "/" + self.subdirectory + "/" + self.classname + ".h";
87 default_overwrite = True
88 if self.implementation.user_should_modify_template():
89 default_overwrite = False
90
91 if write_file(overwrite,default_overwrite,filename):
92 import inspect, os
93 print( "{} written by {}".format(filename, os.path.basename(inspect.getfile(self.__class__))))
94
95 with open( filename, "w" ) as outputfile:
96 full_qualified_classname = self.__get_full_qualified_class_name()
97 include_guard = "_{}_H_".format(full_qualified_classname.replace( "::", "_" ).upper())
98
99 outputfile.write( "#ifndef {}\n#define {}\n\n\n".format(include_guard, include_guard))
100
101 outputfile.write( """
102#include "peano4/utils/Globals.h"
103#include "peano4/datamanagement/VertexEnumerator.h"
104#include "peano4/datamanagement/VertexMarker.h"
105#include "peano4/datamanagement/FaceEnumerator.h"
106#include "peano4/datamanagement/FaceMarker.h"
107#include "peano4/datamanagement/CellMarker.h"
108#include "peano4/grid/GridControlEvent.h"
109#include "tarch/la/Vector.h"
110
111#include <vector>
112
113
114""" )
115
116 outputfile.write( self.implementation.get_includes() )
117 outputfile.write( "\n\n" )
118
119 self.__generate_includes(outputfile)
120
121 for i in self.namespace: outputfile.write( "namespace " + i + " {\n" )
122 outputfile.write(" class {};\n".format(self.classname))
123 for i in self.namespace: outputfile.write( "}\n" )
124
125 outputfile.write( "class " + full_qualified_classname + "{\n" )
126 outputfile.write( " private:\n" )
127 outputfile.write( " static tarch::logging::Log _log;\n" )
128 outputfile.write( self.implementation.get_attributes() )
129
130 outputfile.write( " public:\n" )
131 outputfile.write( """
132 /**
133 * Create action instance for one tree for one grid sweep
134 *
135 * <h2> Thread safety </h2>
136 *
137 * The creation of individual trees usually happens through peano4::parallel::SpacetreeSet::createObserverCloneIfRequired().
138 * This routine is called lazily when we start to traverse a subtree.
139 * Therefore, the creation of actions is not thread-safe.
140 *
141 *
142 * @param treeNumber Number of the spacetree for which we create the tree instance. Is
143 * smaller 0 if this is the prototype action used on a rank from which
144 * the real actions are constructed from.
145 */
146""" )
147 outputfile.write( " " + self.classname + "(int treeNumber);\n\n" )
148 outputfile.write( " ~" + self.classname + "();\n\n" )
149 outputfile.write( """
150 std::vector< peano4::grid::GridControlEvent > getGridControlEvents() const;
151 static void prepareTraversal();
152 static void unprepareTraversal();
153""" )
154
155 for i in self.operations: self.__generate_operation(outputfile,i)
156 outputfile.write( "};\n\n\n" )
157 outputfile.write( "#endif\n\n" )
158
160 return self.subdirectory + "/" + self.classname + ".cpp"
161
162 def __generate_implementation(self,overwrite,directory):
163 filename = directory + "/" + self.get_cpp_file_name()
164 default_overwrite = True
165 if self.implementation.user_should_modify_template():
166 default_overwrite = False
167 if write_file(overwrite,default_overwrite,filename):
168 import inspect, os
169 print( "{} written by {}".format(filename, os.path.basename(inspect.getfile(self.__class__))))
170 outputfile = open( filename, "w" )
171
172 #full_qualified_classname = self.__get_full_qualified_class_name()
173
174 outputfile.write( "#include \"" + self.classname + ".h\"\n\n\n" )
175
176 outputfile.write( "tarch::logging::Log " + self.__get_full_qualified_class_name() + "::_log( \""+ self.__get_full_qualified_class_name() + "\");\n\n\n" )
177
178 outputfile.write( self.implementation.get_static_initialisations(self.__get_full_qualified_class_name()) )
179 outputfile.write( "\n\n\n" )
180
181 outputfile.write( self.__get_full_qualified_class_name() + "::" + self.classname + "(int treeNumber) {\n" )
182 outputfile.write( self.implementation.get_constructor_body() )
183 outputfile.write( "}\n\n\n" )
184
185 outputfile.write( self.__get_full_qualified_class_name() + "::~" + self.classname + "() {\n" )
186 outputfile.write( self.implementation.get_destructor_body() )
187 outputfile.write( "}\n\n\n" )
188
189 outputfile.write( "std::vector< peano4::grid::GridControlEvent > " + self.__get_full_qualified_class_name() + "::getGridControlEvents() const {\n" )
190 outputfile.write( self.implementation.get_body_of_getGridControlEvents() )
191 outputfile.write( "}\n\n\n" )
192
193 outputfile.write( "void " + self.__get_full_qualified_class_name() + "::prepareTraversal() {\n" )
194 outputfile.write( self.implementation.get_body_of_prepareTraversal() )
195 outputfile.write( "}\n\n\n" )
196
197 outputfile.write( "void " + self.__get_full_qualified_class_name() + "::unprepareTraversal() {\n" )
198 outputfile.write( self.implementation.get_body_of_unprepareTraversal() )
199 outputfile.write( "}\n\n\n" )
200
201 for operation in self.operations:
202 outputfile.write( operation[1] )
203 outputfile.write( " " )
204 outputfile.write( self.__get_full_qualified_class_name() + "::" + operation[0] )
205 outputfile.write( self.__get_operation_arguments(operation) )
206 outputfile.write( " {\n" )
207 outputfile.write( self.implementation.get_body_of_operation( operation[0] ) )
208 outputfile.write( "\n" )
209 outputfile.write( "}\n\n\n" )
210
211 def generate(self,overwrite,directory):
212 if not os.path.exists( directory + "/" + self.subdirectory ):
213 os.mkdir(directory + "/" + self.subdirectory)
214 self.__generate_header(overwrite,directory)
215 self.__generate_implementation(overwrite,directory)
__generate_implementation(self, overwrite, directory)
Definition ActionSet.py:162
__generate_includes(self, outputfile)
Definition ActionSet.py:44
__generate_operation(self, outputfile, operation)
outputfile points to file, operation to an operation object as added by the solver step
Definition ActionSet.py:61
generate(self, overwrite, directory)
Definition ActionSet.py:211
add_operation(self, name, signature)
signature is a long tuple.
Definition ActionSet.py:36
__generate_header(self, overwrite, directory)
Definition ActionSet.py:85
__get_operation_arguments(self, operation)
Definition ActionSet.py:47
__init__(self, classname, namespace, subdirectory, implementation=None)
Implementation of an Action Set.
Definition ActionSet.py:13
Action set (reactions to events)
Definition ActionSet.py:6