Peano 4
Loading...
Searching...
No Matches
PeanoTextPatchFileWriter.cpp
Go to the documentation of this file.
2
3#include <filesystem>
4#include <fstream>
5#include <iomanip>
6
7#include "tarch/Assertions.h"
8#include "tarch/mpi/Lock.h"
9#include "tarch/mpi/Rank.h"
10
12 "griddata::"
13 "blockstructured::"
14 "PeanoTextPatchFileWriter"
15);
16
18 = "# \n"
19 "# Peano patch file \n"
20 "# Version 0.2 \n"
21 "# \n";
22
23namespace {
24 const std::string Token_BeginDataSet = "begin dataset";
25 const std::string Token_EndDataSet = "end dataset";
26 const std::string Token_TimeStamp = "timestamp";
27} // namespace
28
30 std::ifstream inFile(_indexFile);
31 std::ofstream outFile(_indexFile + ".bak");
32 for (std::string line; std::getline(inFile, line); ) {
33 outFile << line << std::endl;
34 }
35}
36
38 double result = -1.0;
39
40 std::ifstream ifs(_indexFile);
41 std::vector<std::string> lines;
42 for (std::string line; std::getline(ifs, line); ) {
43 lines.push_back(line);
44 }
45 ifs.close();
46
47 for (auto line : lines) {
48 if (line.find(Token_TimeStamp) != std::string::npos) {
49 std::string timeStampToken = line.substr(line.find(Token_TimeStamp) + Token_TimeStamp.length());
50 try {
51 double timeStamp = std::stod(timeStampToken);
52 result = std::max(result, timeStamp);
53 } catch (std::exception&) {
55 "getLatestTimeStepInIndexFile()", "file " << _indexFile << " contained invalid time stamp " << timeStampToken
56 );
57 result = std::numeric_limits<double>::max();
58 }
59 }
60 }
61
62 return result;
63}
64
66 std::ofstream indexFileOut;
67 indexFileOut.open(_indexFile, std::ios::app);
68 indexFileOut << std::endl << Token_BeginDataSet << std::endl;
69 indexFileOut << std::setprecision(24) << std::scientific;
70 indexFileOut << " " << Token_TimeStamp << " " << timestamp << std::endl;
71 indexFileOut << std::endl << Token_EndDataSet << std::endl;
72 indexFileOut.close();
73}
74
75
77 const std::string& filename
78) {
79 std::ifstream ifs(_indexFile);
80 std::vector<std::string> lines;
81 for (std::string line; std::getline(ifs, line); ) {
82 lines.push_back(line);
83 }
84 ifs.close();
85
86 assertion2(lines.size() >= 2, filename, lines.size());
87
88 if (lines[lines.size() - 1].find(Token_EndDataSet) == std::string::npos) {
90 "addNewFileToCurrentDataSetInIndexFile(...)",
91 "last line in index file "
92 << _indexFile << " has not been end of dataset as expected. Expected \"" << Token_EndDataSet << "\" but got \""
93 << lines[lines.size() - 1] << "\""
94 );
95 }
96
97
98 const std::string newEntry = " include \"" + filename + "\"";
99 lines[lines.size() - 1] = newEntry;
100 lines.push_back(Token_EndDataSet);
101 std::ofstream indexFileOut;
102 indexFileOut.open(_indexFile.c_str(), std::ios::out);
103 for (auto& p : lines) {
104 indexFileOut << p << std::endl;
105 }
106 indexFileOut.close();
107
108 logDebug( "addNewFileToCurrentDataSetInIndexFile(...)", "added new entry " << newEntry << " to latest time stamp section within " << _indexFile << " (#lines:" << lines.size() << ")" );
109}
110
111
113 logInfo("createEmptyIndexFile()", "create empty index file " << _indexFile);
114
115 std::ofstream indexFileOut;
116 indexFileOut.open(_indexFile.c_str(), std::ios::out);
117
118 if ((!indexFileOut.fail()) && indexFileOut.is_open()) {
119 indexFileOut << HEADER << "format ASCII" << std::endl;
120
121 addNewDatasetToIndexFile(0.0);
122 }
123
124 if (!indexFileOut.is_open()) {
125 logError("PeanoTextPatchFileWriter()", "have not been able to open file " << _indexFile);
126 } else {
127 indexFileOut.close();
128 }
129}
130
132 int dimension,
133 const std::string& fileName,
134 const std::string& indexFileName,
135 IndexFileMode appendToIndexFile,
136 double timeStamp
137):
138 _fileName(fileName),
139 _indexFile(indexFileName),
140 _writtenToFile(false),
141 _dimensions(dimension) {
142
143 assertion(dimension >= 2);
144 assertion(dimension <= 3);
145
146 if (fileName.rfind(".peano-patch-file") != std::string::npos) {
148 "writeToFile()",
149 "filename should not end with .peano-patch-file as routine adds extension automatically. Chosen filename prefix="
150 << fileName
151 );
152 } else {
153 _fileName += ".peano-patch-file";
154 }
155 if (indexFileName.rfind(".peano-patch-file") != std::string::npos) {
157 "writeToFile()",
158 "index filename should not end with .peano-patch-file as routine adds extension automatically. Chosen filename "
159 "prefix="
160 << indexFileName
161 );
162 } else {
163 _indexFile += ".peano-patch-file";
164 }
165
166 clear();
167
168 const double DefaultTimeStampPrecision = 1e-5;
169
170 switch (appendToIndexFile) {
174 break;
176 break;
178 if (not std::filesystem::exists(_indexFile)) {
179 logInfo("PeanoTextPatchFileWriter(...)", "no index file " << _indexFile << " found. Create new one");
181 } else if (tarch::la::smaller(
182 timeStamp,
185 timeStamp, getLatestTimeStepInIndexFile(), DefaultTimeStampPrecision
186 )
187 )) {
189 "PeanoTextPatchFileWriter(...)",
190 "there is an index file "
191 << _indexFile << " with data for time stamp " << getLatestTimeStepInIndexFile()
192 << ". Will be overwritten as we dump data for time " << timeStamp
193 );
195 }
196
198 if (tarch::la::smaller(getLatestTimeStepInIndexFile(), timeStamp, DefaultTimeStampPrecision)) {
199 logDebug(
200 "PeanoTextPatchFileWriter(...)",
201 "create new dataset for time stamp "
202 << timeStamp << " linking to " << _fileName << " as last time stamp had been " << getLatestTimeStepInIndexFile()
203 );
204 addNewDatasetToIndexFile(timeStamp);
205 }
207 break;
208 }
209 logDebug("PeanoTextPatchFileWriter(...)", "index file is ready");
210
211 _snapshotFileOut << std::scientific;
212}
213
215
217 int totalEntries, double* values
218) {
219 if (values != nullptr) {
220 _snapshotFileOut << " mapping ";
221 for (int i = 0; i < totalEntries * _dimensions; i++) {
222 _snapshotFileOut << " " << values[i];
223 }
224 _snapshotFileOut << std::endl;
225 }
226}
227
229 if (!metaData.empty()) {
230 _snapshotFileOut
231 << " begin meta-data" << std::endl
232 << " " << metaData << std::endl
233 << " end meta-data" << std::endl;
234 }
235}
236
239 const std::string& identifier, int unknownsPerAxis, int recordsPerCell, const std::string& description
240 ) {
241 return new CellDataWriter(identifier, unknownsPerAxis, recordsPerCell, description, "", nullptr, *this);
242}
243
246 const std::string& identifier,
247 int unknownsPerAxis,
248 int recordsPerCell,
249 const std::string& description,
250 const std::string& metaData
251 ) {
252 return new CellDataWriter(identifier, unknownsPerAxis, recordsPerCell, description, metaData, nullptr, *this);
253}
254
257 const std::string& identifier,
258 int unknownsPerAxis,
259 int recordsPerCell,
260 const std::string& description,
261 const std::string& metaData,
262 double* mapping
263 ) {
264 return new CellDataWriter(identifier, unknownsPerAxis, recordsPerCell, description, metaData, mapping, *this);
265}
266
269 const std::string& identifier, int unknownsPerAxis, int recordsPerVertex, const std::string& description
270 ) {
271 return new VertexDataWriter(identifier, unknownsPerAxis, recordsPerVertex, description, "", nullptr, *this);
272}
273
276 const std::string& identifier,
277 int unknownsPerAxis,
278 int recordsPerVertex,
279 const std::string& description,
280 const std::string& metaData
281 ) {
282 return new VertexDataWriter(identifier, unknownsPerAxis, recordsPerVertex, description, metaData, nullptr, *this);
283}
284
287 const std::string& identifier,
288 int unknownsPerAxis,
289 int recordsPerVertex,
290 const std::string& description,
291 const std::string& metaData,
292 double* mapping
293 ) {
294 return new VertexDataWriter(identifier, unknownsPerAxis, recordsPerVertex, description, metaData, mapping, *this);
295}
296
299) {
300 if (_haveWrittenAtLeastOnePatch) {
301 _snapshotFileOut << "end patch" << std::endl << std::endl;
302 }
303
304 _snapshotFileOut << "begin patch" << std::endl << " offset";
305
306 for (int d = 0; d < 2; d++) {
307 _snapshotFileOut << " " << offset(d);
308 }
309 if (_dimensions == 3) {
310 _snapshotFileOut << " 0";
311 }
312 _snapshotFileOut << std::endl;
313
314 _snapshotFileOut << " size";
315
316 for (int d = 0; d < 2; d++) {
317 _snapshotFileOut << " " << size(d);
318 }
319 if (_dimensions == 3) {
320 _snapshotFileOut << " 0";
321 }
322 _snapshotFileOut << std::endl;
323
324 _haveWrittenAtLeastOnePatch = true;
325
326 _patchCounter++;
327 return _patchCounter - 1;
328}
329
332) {
333 assertion(_dimensions == 3);
334
335 if (_haveWrittenAtLeastOnePatch) {
336 _snapshotFileOut << "end patch" << std::endl << std::endl;
337 }
338
339 _snapshotFileOut << "begin patch" << std::endl << " offset";
340
341 for (int d = 0; d < 3; d++) {
342 _snapshotFileOut << " " << offset(d);
343 }
344 _snapshotFileOut << std::endl;
345
346 _snapshotFileOut << " size";
347
348 for (int d = 0; d < 3; d++) {
349 _snapshotFileOut << " " << size(d);
350 }
351 _snapshotFileOut << std::endl;
352
353 _haveWrittenAtLeastOnePatch = true;
354
355 _patchCounter++;
356 return _patchCounter - 1;
357}
358
360 assertion(!_writtenToFile);
361
362 std::ofstream out;
363 out.open(_fileName, std::ios::binary);
364 if ((!out.fail()) && out.is_open()) {
365 logDebug("writeToFile()", "opened data file " + _fileName);
366
367 if (_haveWrittenAtLeastOnePatch) {
368 _snapshotFileOut << "end patch" << std::endl << std::endl;
369 }
370
371 out << _snapshotFileOut.rdbuf();
372
373 _writtenToFile = true;
374 return true;
375 } else {
376 logError("close()", "unable to write output file " + _fileName);
377 return false;
378 }
379}
380
382
384 _writtenToFile = false;
385
386 _snapshotFileOut.clear();
387
388 _patchCounter = 0;
389
390 _haveWrittenAtLeastOnePatch = false;
391
392 _snapshotFileOut << HEADER << "format ASCII" << std::endl << "dimensions " << _dimensions << std::endl;
393
394 _snapshotFileOut << std::endl << std::endl;
395}
396
397
398
#define assertion2(expr, param0, param1)
#define assertion(expr)
And from this we can write down f$ nabla phi_i nabla phi_i dx but since we are constructing matrix let s investigate the f$ our matrix elements will nabla phi_i dx f By this will be a sparse as these basis functions are chosen to not overlap with each other almost everywhere In other they have only local support We can read off the right hand side values
#define logError(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:464
#define logDebug(methodName, logMacroMessageStream)
Definition Log.h:50
#define logWarning(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:440
#define logInfo(methodName, logMacroMessageStream)
Wrapper macro around tarch::tarch::logging::Log to improve logging.
Definition Log.h:411
Log Device.
Definition Log.h:516
virtual int plotPatch(const tarch::la::Vector< 2, double > &offset, const tarch::la::Vector< 2, double > &size) override
PeanoTextPatchFileWriter(int dimension, const std::string &fileName, const std::string &indexFileName, IndexFileMode appendToIndexFile, double timeStamp)
Create a new Peano text file output.
virtual CellDataWriter * createCellDataWriter(const std::string &identifier, int unknownsPerAxis, int recordsPerCell, const std::string &description) override
Caller has to destroy this instance manually.
virtual VertexDataWriter * createVertexDataWriter(const std::string &identifier, int unknownsPerAxis, int recordsPerVertex, const std::string &description) override
Caller has to destroy this instance manually.
double getLatestTimeStepInIndexFile() const
Find latest time step in index file.
static bool smaller(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE) InlineMethod
Smaller operator for floating point values.
double relativeEpsNormaledAgainstValueGreaterOne(double valueA, double valueB=std::numeric_limits< double >::min(), double eps=NUMERICAL_ZERO_DIFFERENCE)
Determine a relative tolerance from one or two values.
Definition Scalar.cpp:10
Simple vector class.
Definition Vector.h:134