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 .AbstractFVActionSet import AbstractFVActionSet
5
6import peano4
7import jinja2
8
10 def __init__(self,solver,guard,grid_is_constructed, restart_from_checkpoint=False, loading_scheme="local-loading"):
11 AbstractFVActionSet.__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
50 return __name__.replace(".py", "").replace(".", "_")
51
52 def get_includes(self):
53 return """
54#include <functional>
55#include <iostream>
56#include <fstream>
57#include <sstream>
58
59#include "tarch/reader/PeanoTextPatchFileReader.h"
60""" + self._solver._get_default_includes() + self._solver.user_action_set_includes
61
62 def get_static_initialisations(self,full_qualified_classname):
63 if self._restart_from_checkpoint and self._loading_scheme=="global-loading":
64 return """
65std::vector<double> """ + full_qualified_classname + """::domainDataFromFiles;
66std::vector<double> """ + full_qualified_classname + """::offsetIndex;
67int """ + full_qualified_classname + """::domainPatchCount=0;
68"""
69 elif self._restart_from_checkpoint and self._loading_scheme=="local-loading":
70 return """
71//std::unordered_map<std::tuple<double, double, double>, std::string, tarch::reader::offsetHash, tarch::reader::offsetEqual> """ + full_qualified_classname + """::OffsetIndexMap;
72"""
73 else:
74 return """\n"""
75
76 def get_attributes(self):
77 if self._restart_from_checkpoint and self._loading_scheme=="global-loading":
78 return """
79 static std::vector<double> domainDataFromFiles;
80 static std::vector<double> offsetIndex;
81 static int domainPatchCount;
82"""
83 elif self._restart_from_checkpoint and self._loading_scheme=="local-loading":
84 return """
85 static inline std::map< tarch::la::Vector<Dimensions,double> , std::string, tarch::la::VectorCompare<Dimensions> > OffsetIndexMap{ tarch::la::VectorCompare<Dimensions>(1e-5) };
86 static constexpr size_t memoryLimit = 100ULL * 1024 * 1024 * 1024;
87 static tarch::reader::CheckpointFilesManager subFilesManager{memoryLimit};
88"""
89 else:
90 return """\n"""
91
92
93 TemplateInitialCondition = """
94 if ({{PREDICATE}}) {
95 logTraceIn( "touchCellFirstTime(...)---InitialCondition" );
96 parallelDfor( volume, {{NUMBER_OF_VOLUMES_PER_AXIS}} ) {
97 int index = peano4::utils::dLinearised(volume,{{NUMBER_OF_VOLUMES_PER_AXIS}}) * ({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}});
98 repositories::{{SOLVER_INSTANCE}}.initialCondition(
99 fineGridCell{{UNKNOWN_IDENTIFIER}}.value + index,
100 ::exahype2::fv::getVolumeCentre( marker.x(), marker.h(), {{NUMBER_OF_VOLUMES_PER_AXIS}}, volume),
101 ::exahype2::fv::getVolumeSize( marker.h(), {{NUMBER_OF_VOLUMES_PER_AXIS}} ),
102 {{GRID_IS_CONSTRUCTED}}
103 );
104 } endParallelDfor
105
106 // relevant for tracing et al which kicks in if and only if cell has been
107 // updated
108 fineGridCell{{SOLVER_NAME}}CellLabel.setHasUpdated(true);
109
110 logTraceOut( "touchCellFirstTime(...)---InitialCondition" );
111 }
112"""
113
115 if self._loading_scheme=="global-loading":
116 return """
117//loading-scheme: global-loading
118if ({{PREDICATE}}) {
119 logTraceIn( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
120
121 #if Dimensions==2
122 tarch::la::Vector<2,double> offset;
123 const int totalVolumeCount={{NUMBER_OF_VOLUMES_PER_AXIS}}*{{NUMBER_OF_VOLUMES_PER_AXIS}};
124 #elif Dimensions==3
125 tarch::la::Vector<3,double> offset;
126 const int totalVolumeCount={{NUMBER_OF_VOLUMES_PER_AXIS}}*{{NUMBER_OF_VOLUMES_PER_AXIS}}*{{NUMBER_OF_VOLUMES_PER_AXIS}};
127 #endif
128
129 for (int i=0;i<domainPatchCount;i++) {
130 bool FoundCell = false;
131 #if Dimensions==2
132 offset[0]=offsetIndex[i*2+0];
133 offset[1]=offsetIndex[i*2+1];
134 #elif Dimensions==3
135 offset[0]=offsetIndex[i*3+0];
136 offset[1]=offsetIndex[i*3+1];
137 offset[2]=offsetIndex[i*3+2];
138 #endif
139
140 if ( tarch::la::equals( marker.x() - 0.5*marker.h(), offset, 1e-5) ) { FoundCell=true; }
141
142 if (FoundCell){
143 parallelDfor( volume, {{NUMBER_OF_VOLUMES_PER_AXIS}} ) {
144 int index = peano4::utils::dLinearised(volume,{{NUMBER_OF_VOLUMES_PER_AXIS}}) * ({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}});
145 for (int k=0; k<=({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}}); k++){
146 fineGridCell{{UNKNOWN_IDENTIFIER}}.value[index+k]=domainDataFromFiles[i*({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}})*totalVolumeCount+index+k];
147 }
148 } endParallelDfor
149 break;
150 }
151 }
152
153 fineGridCell{{SOLVER_NAME}}CellLabel.setTimeStamp(CheckpointTimeStamp);
154 fineGridCell{{SOLVER_NAME}}CellLabel.setHasUpdated(true);
155 logTraceOut( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
156}
157"""
158 elif self._loading_scheme=="local-loading":
159 return """
160//loading-scheme: local-loading
161//warning: it only support 3D checkpointing file currently!
162if ({{PREDICATE}}) {
163 logTraceIn( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
164
165 #if Dimensions==2
166 tarch::la::Vector<2,double> offset;
167 const int totalCellCount={{NUMBER_OF_VOLUMES_PER_AXIS}}*{{NUMBER_OF_VOLUMES_PER_AXIS}};
168 #elif Dimensions==3
169 tarch::la::Vector<3,double> offset;
170 const int totalCellCount={{NUMBER_OF_VOLUMES_PER_AXIS}}*{{NUMBER_OF_VOLUMES_PER_AXIS}}*{{NUMBER_OF_VOLUMES_PER_AXIS}};
171 #endif
172 double patchDataFromFile[totalCellCount*({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}})];
173
174 std::string targetCheckpointFileName=OffsetIndexMap[{(marker.x() - 0.5*marker.h())[0], (marker.x() - 0.5*marker.h())[1], (marker.x() - 0.5*marker.h())[2]}];
175
176 const auto& lines = subFilesManager.getCheckpointFileData(targetCheckpointFileName);
177 bool PatchFound=false;
178
179 for (const auto& line : lines) {
180 if (line.find(" offset") == 0) {
181 std::istringstream iss(line);
182 std::string token; int index=0;
183
184 while (iss >> token) { try {offset[index]=std::stod(token); index++; } catch (...){ } }
185 if ( tarch::la::equals( marker.x() - 0.5*marker.h(), offset, 1e-6) ){ PatchFound=true; }
186 }
187 if ( PatchFound and (line.find(" ") == 0) ){
188 std::istringstream iss(line);
189 std::string token; int index=0;
190
191 while (iss >> token) { patchDataFromFile[index]=std::stod(token); index++; }
192 break;
193 }
194 }
195
196 parallelDfor( volume, {{NUMBER_OF_VOLUMES_PER_AXIS}} ) {
197 int index = peano4::utils::dLinearised(volume,{{NUMBER_OF_VOLUMES_PER_AXIS}}) * ({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}}) ;
198 for (int i=0; i<=({{NUMBER_OF_UNKNOWNS}} + {{NUMBER_OF_AUXILIARY_VARIABLES}}); i++){
199 fineGridCell{{UNKNOWN_IDENTIFIER}}.value[index+i]=patchDataFromFile[index+i];
200 }
201 } endParallelDfor
202
203 fineGridCell{{SOLVER_NAME}}CellLabel.setTimeStamp(CheckpointTimeStamp);
204 fineGridCell{{SOLVER_NAME}}CellLabel.setHasUpdated(true);
205 logTraceOut( "touchCellFirstTime(...)---RestartingFromCheckpoint" );
206}
207"""
208
210 if self._loading_scheme=="global-loading":
211 return """
212 tarch::reader::readInCheckpointFiles(CheckpointFilesOf{{SOLVER_NAME}}, domainDataFromFiles, offsetIndex,
213 {{NUMBER_OF_UNKNOWNS}}, {{NUMBER_OF_AUXILIARY_VARIABLES}}, Dimensions, {{NUMBER_OF_VOLUMES_PER_AXIS}});
214 #if Dimensions==2
215 domainPatchCount=offsetIndex.size()/2;
216 #elif Dimensions==3
217 domainPatchCount=offsetIndex.size()/3;
218 #endif
219
220"""
221 elif self._loading_scheme=="local-loading":
222 return """
223 tarch::reader::readInCheckpointOffsetIndex(OffsetIndexMap, CheckpointFilesOf{{SOLVER_NAME}});
224
225"""
get_attributes(self)
Return attributes as copied and pasted into the generated class.
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="local-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.