Peano
Loading...
Searching...
No Matches
InitialCondition.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3
4from .AbstractRKFDActionSet import AbstractRKFDActionSet
5
6import peano4
7import jinja2
8
10 def __init__(self,solver,guard,grid_is_constructed, restart_from_checkpoint=False, loading_scheme="global-loading"):
11 AbstractRKFDActionSet.__init__(self,solver)
12 self.guard = guard
13 self.grid_is_constructed = grid_is_constructed
14 self._restart_from_checkpoint = restart_from_checkpoint
15 self._loading_scheme = loading_scheme
16
17
18 def get_body_of_operation(self,operation_name):
19 result = ""
20
21 if operation_name==peano4.solversteps.ActionSet.OPERATION_TOUCH_CELL_FIRST_TIME:
22 d = {}
23 self._solver._init_dictionary_with_default_parameters(d)
24 self._solver.add_entries_to_text_replacement_dictionary(d)
25 d[ "PREDICATE" ] = self.guard
26 d[ "GRID_IS_CONSTRUCTED" ] = self.grid_is_constructed
27 d[ "CHECKPOINTFILELIST"] = "CheckpointFilesOf" + self._solver._name
29 result = jinja2.Template(self.get_template_for_restarting()).render(**d)
30 else:
31 result = jinja2.Template( self.TemplateInitialCondition ).render(**d)
32
33 return result
34
36 result = "\n"
37
39 d = {}
40 self._solver._init_dictionary_with_default_parameters(d)
41 self._solver.add_entries_to_text_replacement_dictionary(d)
42 d[ "PREDICATE" ] = self.guard
43 d[ "GRID_IS_CONSTRUCTED" ] = self.grid_is_constructed
44 d[ "CHECKPOINTFILELIST"] = "CheckpointFilesOf" + self._solver._name
45 result = jinja2.Template( self.get_template_for_restart_preprocessing() ).render(**d)
46
47 return result
48
49
51 return __name__.replace(".py", "").replace(".", "_")
52
53
54 def get_includes(self):
55 return """
56#include <functional>
57#include <iostream>
58#include <fstream>
59#include <sstream>
60
61#include "exahype2/fd/PatchUtils.h"
62#include "tarch/reader/PeanoTextPatchFileReader.h"
63""" + self._solver._get_default_includes() + self._solver.user_action_set_includes
64
65
66 def get_static_initialisations(self,full_qualified_classname):
67 if self._restart_from_checkpoint and self._loading_scheme=="global-loading":
68 return """
69std::vector<double> """ + full_qualified_classname + """::domainDataFromFiles;
70std::vector<double> """ + full_qualified_classname + """::offsetIndex;
71int """ + full_qualified_classname + """::domainPatchCount=0;
72"""
73 elif self._restart_from_checkpoint and self._loading_scheme=="local-loading":
74 return """
75//std::unordered_map<std::tuple<double, double, double>, std::string, tarch::reader::offsetHash, tarch::reader::offsetEqual> """ + full_qualified_classname + """::OffsetIndexMap;
76//tarch::reader::CheckpointFilesManager """ + full_qualified_classname + """::subFilesManager(100*1024*1024*1024);
77"""
78 else:
79 return """\n"""
80
81
82 def get_attributes(self):
83 if self._restart_from_checkpoint and self._loading_scheme=="global-loading":
84 return """
85 static std::vector<double> domainDataFromFiles;
86 static std::vector<double> offsetIndex;
87 static int domainPatchCount;
88"""
89 elif self._restart_from_checkpoint and self._loading_scheme=="local-loading":
90 return """
91 static inline std::map< tarch::la::Vector<Dimensions,double> , std::string, tarch::la::VectorCompare<Dimensions> > OffsetIndexMap{ tarch::la::VectorCompare<Dimensions>(1e-5) };
92 static constexpr size_t memoryLimit = 100ULL * 1024 * 1024 * 1024;
93 static inline tarch::reader::CheckpointFilesManager subFilesManager{memoryLimit};
94"""
95 else:
96 return """\n"""
97
98 TemplateInitialCondition = """
99 if ({{PREDICATE}}) {
100 logTraceIn( "touchCellFirstTime(...)---InitialCondition" );
101 parallelDfor( volume, {{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}} ) {
102 int index = peano4::utils::dLinearised(volume,{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}) * ({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}});
103 repositories::{{SOLVER_INSTANCE}}.initialCondition(
104 fineGridCell{{UNKNOWN_IDENTIFIER}}.value + index,
105 ::exahype2::fd::getGridCellCentre( marker.x(), marker.h(), {{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}, volume),
106 ::exahype2::fd::getGridCellSize( marker.h(), {{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}} ),
107 {{GRID_IS_CONSTRUCTED}}
108 );
109 //index += {{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}};
110 } endParallelDfor
111
112 // relevant for tracing et al which kicks in if and only if cell has been
113 // updated
114 fineGridCell{{SOLVER_NAME}}CellLabel.setHasUpdated(true);
115
116 logTraceOut( "touchCellFirstTime(...)---InitialCondition" );
117 }
118"""
119
121 if self._loading_scheme=="global-loading":
122 return """
123//loading-scheme: global-loading
124if ({{PREDICATE}}) {
125 logTraceIn( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
126
127 #if Dimensions==2
128 tarch::la::Vector<2,double> offset;
129 const int totalCellCount={{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}*{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}};
130 #elif Dimensions==3
131 tarch::la::Vector<3,double> offset;
132 const int totalCellCount={{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}*{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}*{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}};
133 #endif
134
135 for (int i=0;i<domainPatchCount;i++) {
136 bool FoundCell = false;
137 #if Dimensions==2
138 offset[0]=offsetIndex[i*2+0];
139 offset[1]=offsetIndex[i*2+1];
140 #elif Dimensions==3
141 offset[0]=offsetIndex[i*3+0];
142 offset[1]=offsetIndex[i*3+1];
143 offset[2]=offsetIndex[i*3+2];
144 #endif
145
146 if ( tarch::la::equals( marker.x() - 0.5*marker.h(), offset, 1e-5) ) { FoundCell=true; }
147
148 if (FoundCell){
149 parallelDfor( volume, {{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}} ) {
150 int index = peano4::utils::dLinearised(volume,{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}) * ({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}});
151 for (int k=0; k<=({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}}); k++){
152 fineGridCell{{UNKNOWN_IDENTIFIER}}.value[index+k]=domainDataFromFiles[i*({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}})*totalCellCount+index+k];
153 }
154 } endParallelDfor
155 break;
156 }
157 }
158
159 fineGridCell{{SOLVER_NAME}}CellLabel.setTimeStamp(CheckpointTimeStamp);
160 fineGridCell{{SOLVER_NAME}}CellLabel.setHasUpdated(true);
161 logTraceOut( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
162}
163"""
164 elif self._loading_scheme=="local-loading":
165 return """
166//loading-scheme: local-loading
167//warning: it only support 3D checkpointing file currently!
168if ({{PREDICATE}}) {
169 logTraceIn( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
170
171 #if Dimensions==2
172 tarch::la::Vector<2,double> offset;
173 const int totalCellCount={{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}*{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}};
174 #elif Dimensions==3
175 tarch::la::Vector<3,double> offset;
176 const int totalCellCount={{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}*{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}*{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}};
177 #endif
178 double patchDataFromFile[totalCellCount*({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}})];
179
180 std::string targetCheckpointFileName=OffsetIndexMap[( marker.x() - 0.5*marker.h() )];
181 if (targetCheckpointFileName.empty()) {std::cout<<"Patch "<< marker.x() - 0.5*marker.h() << " do not see a correct file " << targetCheckpointFileName <<std::endl;}
182 //else {std::cout<<"Patch "<< marker.x() - 0.5*marker.h() << " should be in " << targetCheckpointFileName <<std::endl;}
183
184 const auto& lines = subFilesManager.getCheckpointFileData(targetCheckpointFileName);
185 bool PatchFound=false;
186
187 for (const auto& line : lines) {
188 if (line.find(" offset") == 0) {
189 std::istringstream iss(line);
190 std::string token; int index=0;
191
192 while (iss >> token) { try {offset[index]=std::stod(token); index++; } catch (...){ } }
193 if ( tarch::la::equals( marker.x() - 0.5*marker.h(), offset, 1e-6) ){ PatchFound=true; }
194 }
195 if ( PatchFound and (line.find(" ") == 0) ){
196 std::istringstream iss(line);
197 std::string token; int index=0;
198
199 while (iss >> token) { patchDataFromFile[index]=std::stod(token); index++; }
200 //std::cout<<"Found patch with "<< marker.x() - 0.5*marker.h() << " in " << targetCheckpointFileName <<std::endl;
201 break;
202 }
203 }
204
205 parallelDfor( volume, {{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}} ) {
206 int index = peano4::utils::dLinearised(volume,{{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}}) * ({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}}) ;
207 for (int i=0; i<=({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}}); i++){
208 fineGridCell{{UNKNOWN_IDENTIFIER}}.value[index+i]=patchDataFromFile[index+i];
209 }
210 } endParallelDfor
211
212 fineGridCell{{SOLVER_NAME}}CellLabel.setTimeStamp(CheckpointTimeStamp);
213 fineGridCell{{SOLVER_NAME}}CellLabel.setHasUpdated(true);
214 logTraceOut( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
215}
216"""
217
219 if self._loading_scheme=="global-loading":
220 return """
221 tarch::reader::readInCheckpointFiles(CheckpointFilesOf{{SOLVER_NAME}}, domainDataFromFiles, offsetIndex,
222 {{NUMBER_OF_UNKNOWNS}}, {{NUMBER_OF_AUXILIARY_VARIABLES}}, Dimensions, {{NUMBER_OF_GRID_CELLS_PER_PATCH_PER_AXIS}});
223 #if Dimensions==2
224 domainPatchCount=offsetIndex.size()/2;
225 #elif Dimensions==3
226 domainPatchCount=offsetIndex.size()/3;
227 #endif
228
229"""
230 elif self._loading_scheme=="local-loading":
231 return """
232 tarch::reader::readInCheckpointOffsetIndex(OffsetIndexMap, CheckpointFilesOf{{SOLVER_NAME}});
233
234"""
get_action_set_name(self)
You should replicate this function in each subclass, so you get meaningful action set names (otherwis...
__init__(self, solver, guard, grid_is_constructed, restart_from_checkpoint=False, loading_scheme="global-loading")
solver: ADERDG Reference to creating class
get_body_of_operation(self, operation_name)
Return actual C++ code snippets to be inserted into C++ code.
get_includes(self)
Return include statements that you need.
get_attributes(self)
Return attributes as copied and pasted into the generated class.