Peano 4
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
9tarch::logging::Log toolbox::blockstructured::GlobalDatabase::_log( "toolbox::blockstructured::GlobalDatabase" );
10
11
13 timestamp(timestamp_),
14 numberOfDataEntries(database._numberOfGlobalDataPoints) {
15 if (numberOfDataEntries>0) {
16 data = new double[numberOfDataEntries];
17 std::fill_n( data, numberOfDataEntries, 0.0 );
18 }
19 else {
20 data =nullptr;
21 }
22}
23
24
26 timestamp(copy.timestamp),
27 numberOfDataEntries(copy.numberOfDataEntries) {
28 if (numberOfDataEntries>0) {
29 data = new double[numberOfDataEntries];
30 std::copy_n( copy.data, numberOfDataEntries, data );
31 }
32 else {
33 data =nullptr;
34 }
35}
36
37
41
42
43toolbox::blockstructured::GlobalDatabase::GlobalDatabase( int growthBetweenTwoDatabaseFlushes, double dataDelta, double timeDelta, bool clearDatabaseAfterFlush, bool deltasAreRelative ):
44 _fileName(""),
45 _dataName(" data "),
46 _timeDelta(0.0),
47 _dataDelta(dataDelta),
48 _precision(0),
49 _maxDataDelta(0.0),
51 _deltaBetweenTwoDatabaseFlushes(growthBetweenTwoDatabaseFlushes),
52 _thresholdForNextDatabaseFlush(growthBetweenTwoDatabaseFlushes==0 ? std::numeric_limits<int>::max() : 0),
54 _deltasAreRelative(deltasAreRelative),
55 _rank(-1),
56 _maxTimestamp( -std::numeric_limits<double>::max() ) {
57}
58
59
61 if (_fileName!="") {
62 dumpCSVFile();
63 }
64 clear();
65}
66
67
69 tarch::multicore::Lock lock(_semaphore, false);
70
71 if (lockSemaphore) {
72 lock.lock();
73 }
74
75 _data.clear();
76}
77
78
80 std::ostringstream snapshotFileName;
81 snapshotFileName << _fileName;
82
83 if (tarch::mpi::Rank::getInstance().getNumberOfRanks()>0 ) {
84 if ( _rank<0 ) {
86 }
87 snapshotFileName << "-rank-" << _rank;
88 }
89
90 if (_clearDatabaseAfterFlush) {
91 static int snapshotCounter = -1;
92 snapshotCounter++;
93 snapshotFileName << "-snapshot-" << snapshotCounter;
94 }
95
96 snapshotFileName << ".csv";
97
98 tarch::multicore::Lock lock(_semaphore);
99 if (not _data.empty()) {
100 logInfo( "dumpCSVFile()", "dump particle trajectory database " << snapshotFileName.str() );
101 std::ofstream file( snapshotFileName.str() );
102 file << std::scientific;
103 file << "t," << _dataName << std::endl;
104
105 for (auto& snapshot: _data) {
106 file << snapshot.timestamp;
107
108 if (snapshot.data!=nullptr) {
109 if(_precision>0){ file.precision(_precision); }
110 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
111 file << ", "
112 << snapshot.data[i];
113 }
114 }
115 file << std::endl;
116 }
117
118 }
119 else {
120 #if PeanoDebug>=1
121 logInfo( "dumpCSVFile()", "particle trajectory database is empty. Do not dump " << snapshotFileName.str() );
122 #endif
123 }
124
125 if (_clearDatabaseAfterFlush) {
126 clear(false);
127 }
128}
129
130
132 _fileName = filename;
133}
134
135void toolbox::blockstructured::GlobalDatabase::setDataName( const std::string& dataname ) {
136 _dataName = dataname;
137}
138
140 _precision = precision;
141}
142
144 _clearDatabaseAfterFlush = value;
145}
146
148 assertion(value>=0.0);
149 _dataDelta = value;
150 _maxDataDelta = 1e-20;
151 _deltasAreRelative = deltasAreRelative;
152}
153
154
156 assertion(value>=0.0);
157 _timeDelta = value;
158}
159
161 double timestamp
162) {
163
164 if (_data.empty()) {
166 }
167 else if(tarch::la::equals(_data.front().timestamp,timestamp)){
168 logWarning( "getAction(...)", "There were two values for same time stamp " << timestamp << ". This is inconsistent");
170 }
171 else if(timestamp - _data.front().timestamp >=_timeDelta and _timeDelta>=0.0 ){
173 }
174 else{
176 }
177
178}
179
180
182 double timestamp,
183 int numberOfDataEntries,
184 double* data
185) {
186
188
190 double maxDataDeltaThisAction = 0;
191 for (int i=0; i<numberOfDataEntries; i++) {
192 maxDataDeltaThisAction = std::max( maxDataDeltaThisAction,
193 std::abs( _data.front().data[i] - data[i] ));
194 }
195 _maxDataDelta = std::max( _maxDataDelta, maxDataDeltaThisAction );
196
197 // if dataDelta this action is lower than the required dataDelta and things aren't relative, just keep
198 // if it's greater and it isn't relative, replace
199 // if it is relative, then use that to compare
200 if( maxDataDeltaThisAction > _dataDelta ){
202 }
203 else if( _deltasAreRelative
204 and maxDataDeltaThisAction>=_deltaCutOffThreshold
205 and maxDataDeltaThisAction/_maxDataDelta >= _dataDelta ){
207 }
208 else if ( not _deltasAreRelative and maxDataDeltaThisAction >= _dataDelta ) {
210 }
211 }
212
213 return result;
214
215}
216
217
219 tarch::multicore::Lock lock(_semaphore);
220 const int totalSize = _data.size();
221 return tarch::la::greater(_maxTimestamp,0.0) and totalSize >= _thresholdForNextDatabaseFlush;
222}
223
224
226 double timestamp
227) {
228
229 if ( _rank<0 ) {
231 }
232
233 tarch::multicore::Lock lock(_semaphore);
234 _maxTimestamp = std::max(_maxTimestamp,timestamp);
235 switch ( getAction(timestamp) ) {
236 case AddSnapshotAction::Ignore:
237 break;
238 case AddSnapshotAction::Append:
239 _data.push_front( Entry(*this,timestamp) );
240 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
241 _data.front().data[i] = 0.0;
242 }
243 break;
244 case AddSnapshotAction::Replace:
245 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
246 _data.front().data[i] = 0.0;
247 }
248 break;
249 }
250 lock.free();
251
252 if (dumpDatabaseSnapshot()) {
253 dumpCSVFile();
254 logInfo( "addSnapshot(...)", "flushed database file (temporary flush - simulation has not terminated yet)" );
255 _thresholdForNextDatabaseFlush += _deltaBetweenTwoDatabaseFlushes;
256 }
257}
258
259
261 double timestamp,
262 int numberOfDataEntries,
263 double* data
264) {
265 assertion( _numberOfGlobalDataPoints==numberOfDataEntries or _data.empty());
266 _numberOfGlobalDataPoints = std::max(_numberOfGlobalDataPoints,numberOfDataEntries);
267
268 if ( _rank<0 ) {
270 }
271
272 tarch::multicore::Lock lock(_semaphore);
273 _maxTimestamp = std::max(_maxTimestamp,timestamp);
274 switch ( getAction(timestamp,numberOfDataEntries,data) ) {
275 case AddSnapshotAction::Ignore:
276 break;
277 case AddSnapshotAction::Append:
278 _data.push_front( Entry(*this,timestamp) );
279 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
280 _data.front().data[i] = data[i];
281 }
282 break;
283 case AddSnapshotAction::Replace:
284 for (int i=0; i<_numberOfGlobalDataPoints; i++) {
285 _data.front().data[i] = data[i];
286 }
287 break;
288 }
289 lock.free();
290
291 if (dumpDatabaseSnapshot()) {
292 dumpCSVFile();
293 logInfo( "addSnapshot(...)", "flush database file " << _fileName << " (temporary flush - simulation has not terminated yet)" );
294 _thresholdForNextDatabaseFlush = _deltaBetweenTwoDatabaseFlushes;
295 }
296}
#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:538
int getRank() const
Return rank of this node.
Definition Rank.cpp:528
Create a lock around a boolean semaphore region.
Definition Lock.h:19
GlobalDatabase(int growthBetweenTwoDatabaseFlushes, double dataDelta=1e-8, double timeDelta=0.0, bool clearDatabaseAfterFlush=true, bool deltasAreRelative=false)
Global database.
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 dumpCSVFile()
Dump data into CSV file.
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.
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.