Peano
Loading...
Searching...
No Matches
GlobalDatabase.cpp
Go to the documentation of this file.
1#include "GlobalDatabase.h"
2
4#include "tarch/mpi/Rank.h"
5
6#include <fstream>
7
8#include <algorithm>
9
10tarch::logging::Log toolbox::blockstructured::GlobalDatabase::_log( "toolbox::blockstructured::GlobalDatabase" );
11
12
14 timestamp(timestamp_),
15 numberOfDataEntries(database._numberOfGlobalDataPoints) {
16 if (numberOfDataEntries>0) {
17 data = new double[numberOfDataEntries];
18 std::fill_n( data, numberOfDataEntries, 0.0 );
19 }
20 else {
21 data =nullptr;
22 }
23}
24
25
27 timestamp(copy.timestamp),
28 numberOfDataEntries(copy.numberOfDataEntries) {
29 if (numberOfDataEntries>0) {
30 data = new double[numberOfDataEntries];
31 std::copy_n( copy.data, numberOfDataEntries, data );
32 }
33 else {
34 data =nullptr;
35 }
36}
37
38
42
43
44toolbox::blockstructured::GlobalDatabase::GlobalDatabase( int growthBetweenTwoDatabaseFlushes, double dataDelta, double timeDelta, bool clearDatabaseAfterFlush, bool deltasAreRelative ):
45 _fileName(""),
46 _dataName(" data "),
47 _timeDelta(0.0),
48 _dataDelta(dataDelta),
49 _precision(0),
50 _maxDataDelta(0.0),
52 _deltaBetweenTwoDatabaseFlushes(growthBetweenTwoDatabaseFlushes),
53 _thresholdForNextDatabaseFlush(growthBetweenTwoDatabaseFlushes==0 ? std::numeric_limits<int>::max() : 0),
55 _deltasAreRelative(deltasAreRelative),
56 _rank(-1),
57 _maxTimestamp( -std::numeric_limits<double>::max() ) {
58}
59
60
62 if (_fileName!="") {
63 dumpCSVFile();
64 }
65 clear();
66}
67
68
70 tarch::multicore::Lock lock(_semaphore, false);
71
72 if (lockSemaphore) {
73 lock.lock();
74 }
75
76 _data.clear();
77}
78
79
81 std::ostringstream snapshotFileName;
82 snapshotFileName << _fileName;
83
84 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>0 ) {
85 if ( _rank<0 ) {
87 }
88 snapshotFileName << "-rank-" << _rank;
89 }
90
91 if (_clearDatabaseAfterFlush) {
92 static int snapshotCounter = -1;
93 snapshotCounter++;
94 snapshotFileName << "-snapshot-" << snapshotCounter;
95 }
96
97 snapshotFileName << ".csv";
98
99 tarch::multicore::Lock lock(_semaphore);
100 if (not _data.empty()) {
101 logInfo( "dumpCSVFile()", "dump particle trajectory database " << snapshotFileName.str() );
102 std::ofstream file( snapshotFileName.str() );
103 file << std::scientific;
104 file << "t," << _dataName << std::endl;
105
106 for (auto& snapshot: _data) {
107 file << snapshot.timestamp;
108
109 if (snapshot.data!=nullptr) {
110 if(_precision>0){ file.precision(_precision); }
111 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
112 file << ", "
113 << snapshot.data[i];
114 }
115 }
116 file << std::endl;
117 }
118
119 }
120 else {
121 #if PeanoDebug>=1
122 logInfo( "dumpCSVFile()", "particle trajectory database is empty. Do not dump " << snapshotFileName.str() );
123 #endif
124 }
125
126 if (_clearDatabaseAfterFlush) {
127 clear(false);
128 }
129}
130
132 _deltaBetweenTwoDatabaseFlushes = deltaBetweenTwoDatabaseFlushes;
133}
134
136 _fileName = filename;
137}
138
139void toolbox::blockstructured::GlobalDatabase::setDataName( const std::string& dataname ) {
140 _dataName = dataname;
141}
142
144 _precision = precision;
145}
146
148 _clearDatabaseAfterFlush = value;
149}
150
152 assertion(value>=0.0);
153 _dataDelta = value;
154 _maxDataDelta = 1e-20;
155 _deltasAreRelative = deltasAreRelative;
156}
157
159 assertion(value>=0.0);
160 _timeDelta = value;
161}
162
164 double timestamp
165) {
166
167 if (_data.empty()) {
169 }
170 else if(tarch::la::equals(_data.front().timestamp,timestamp)){
171 logWarning( "getAction(...)", "There were two values for same time stamp " << timestamp << ". This is inconsistent");
173 }
174 else if(timestamp - _data.front().timestamp >=_timeDelta and _timeDelta>=0.0 ){
176 }
177 else{
179 }
180
181}
182
183
185 double timestamp,
186 int numberOfDataEntries,
187 double* data
188) {
189
191
193 double maxDataDeltaThisAction = 0;
194 for (int i=0; i<numberOfDataEntries; i++) {
195 maxDataDeltaThisAction = std::max( maxDataDeltaThisAction,
196 std::abs( _data.front().data[i] - data[i] ));
197 }
198 _maxDataDelta = std::max( _maxDataDelta, maxDataDeltaThisAction );
199
200 // if dataDelta this action is lower than the required dataDelta and things aren't relative, just keep
201 // if it's greater and it isn't relative, replace
202 // if it is relative, then use that to compare
203 if( maxDataDeltaThisAction > _dataDelta ){
205 }
206 else if( _deltasAreRelative
207 and maxDataDeltaThisAction>=_deltaCutOffThreshold
208 and maxDataDeltaThisAction/_maxDataDelta >= _dataDelta ){
210 }
211 else if ( not _deltasAreRelative and maxDataDeltaThisAction >= _dataDelta ) {
213 }
214 }
215
216 return result;
217
218}
219
220
222 tarch::multicore::Lock lock(_semaphore);
223 const int totalSize = _data.size();
224 return tarch::la::greater(_maxTimestamp,0.0) and totalSize >= _thresholdForNextDatabaseFlush;
225}
226
227
229 double timestamp
230) {
231
232 if ( _rank<0 ) {
234 }
235
236 tarch::multicore::Lock lock(_semaphore);
237 _maxTimestamp = std::max(_maxTimestamp,timestamp);
238 switch ( getAction(timestamp) ) {
239 case AddSnapshotAction::Ignore:
240 break;
241 case AddSnapshotAction::Append:
242 _data.push_front( Entry(*this,timestamp) );
243 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
244 _data.front().data[i] = 0.0;
245 }
246 break;
247 case AddSnapshotAction::Replace:
248 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
249 _data.front().data[i] = 0.0;
250 }
251 break;
252 }
253 lock.free();
254
255 if (dumpDatabaseSnapshot()) {
256 dumpCSVFile();
257 logInfo( "addSnapshot(...)", "flushed database file (temporary flush - simulation has not terminated yet)" );
258 _thresholdForNextDatabaseFlush += _deltaBetweenTwoDatabaseFlushes;
259 }
260}
261
262
264 double timestamp,
265 int numberOfDataEntries,
266 double* data
267) {
268 assertion( _numberOfGlobalDataPoints==numberOfDataEntries or _data.empty());
269 _numberOfGlobalDataPoints = std::max(_numberOfGlobalDataPoints,numberOfDataEntries);
270
271 if ( _rank<0 ) {
273 }
274
275 tarch::multicore::Lock lock(_semaphore);
276 _maxTimestamp = std::max(_maxTimestamp,timestamp);
277 switch ( getAction(timestamp,numberOfDataEntries,data) ) {
278 case AddSnapshotAction::Ignore:
279 break;
280 case AddSnapshotAction::Append:
281 _data.push_front( Entry(*this,timestamp) );
282 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
283 _data.front().data[i] = data[i];
284 }
285 break;
286 case AddSnapshotAction::Replace:
287 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
288 _data.front().data[i] = data[i];
289 }
290 break;
291 }
292 lock.free();
293
294 if (dumpDatabaseSnapshot()) {
295 dumpCSVFile();
296 logInfo( "addSnapshot(...)", "flush database file " << _fileName << " (temporary flush - simulation has not terminated yet)" );
297 _thresholdForNextDatabaseFlush = _deltaBetweenTwoDatabaseFlushes;
298 }
299}
#define assertion(expr)
#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
static Rank & getInstance()
This operation returns the singleton instance.
Definition Rank.cpp:539
int getRank() const
Return rank of this node.
Definition Rank.cpp:529
Create a lock around a boolean semaphore region.
Definition Lock.h:19
void free()
Free the lock.
Definition Lock.cpp:37
bool _clearDatabaseAfterFlush
Flag that indicates if we erase the database after a flush.
void clear(bool lockSemaphore=true)
void setOutputFileName(const std::string &filename)
void setDeltaBetweenTwoDatabaseFlushes(const int deltaBetweenTwoDatabaseFlushes)
void dumpCSVFile()
Dump data into CSV file.
GlobalDatabase(int growthBetweenTwoDatabaseFlushes=0, double dataDelta=1e-8, double timeDelta=0.0, bool clearDatabaseAfterFlush=true, bool deltasAreRelative=false)
Global database.
void setDataDeltaBetweenTwoSnapshots(double value, bool deltasAreRelative=false)
int _rank
This is a hack: Usually, I'd just ask the rank what its number is.
void setDataName(const std::string &dataname)
void addGlobalSnapshot(double timestamp)
Add snapshot.
AddSnapshotAction getAction(double timestamp)
Determine what to do with new entry.
STL namespace.
CF abs(const CF &cf)
bool greater(double lhs, double rhs, double tolerance=NUMERICAL_ZERO_DIFFERENCE)
bool equals(const Matrix< Rows, Cols, Scalar > &lhs, const Matrix< Rows, Cols, Scalar > &rhs, const Scalar &tolerance=NUMERICAL_ZERO_DIFFERENCE)
Compares to matrices on equality by means of a numerical accuracy.
const int numberOfDataEntries
Have to memorise this guy to be able to write a proper copy constructor.