1#ifndef LOSSYSTORAGEDUNE_HH
2#define LOSSYSTORAGEDUNE_HH
7#include "dune/grid/common/grid.hh"
8#include "dune/grid/common/entity.hh"
9#include "dune/grid/io/file/dgfparser/dgfparser.hh"
10#include "dune/istl/matrix.hh"
11#include "dune/istl/bcrsmatrix.hh"
12#include "dune/common/fmatrix.hh"
13#include "dune/common/iteratorfacades.hh"
14#include "dune/istl/matrixindexset.hh"
24template <
class Gr
id,
class CoeffVector>
28 static const int dim = Grid::dimension ;
30 static const int order = 1 ;
36 typedef typename Grid::LevelGridView::IndexSet::IndexType
IndexType;
39 typedef typename Grid::Traits::GlobalIdSet::IdType
IdType;
41 typedef unsigned long int ULong;
43 LossyStorage(
int coarseLevel_,
double aTol_) : ps(NULL), coarseLevel(coarseLevel_), aTol(aTol_),
44 accumulatedEntropy(0), accumulatedOverhead(0),
45 accumulatedUncompressed(0), accumulatedCompressed(0)
64 return accumulatedEntropy;
70 accumulatedEntropy = 0;
78 return accumulatedOverhead;
83 accumulatedOverhead = 0;
91 return accumulatedCompressed;
96 accumulatedCompressed = 0 ;
103 return accumulatedCompressed + accumulatedOverhead;
109 accumulatedEntropy = 0;
110 accumulatedOverhead = 0;
111 accumulatedCompressed = 0;
112 accumulatedUncompressed = 0;
119 return accumulatedUncompressed;
124 accumulatedUncompressed = 0 ;
131 if( accumulatedCompressed + accumulatedOverhead > 0 )
132 return accumulatedUncompressed / (accumulatedCompressed + accumulatedOverhead );
141 if( ps != NULL )
delete ps;
146 typename Grid::GlobalIdSet
const& idSet = grid.globalIdSet();
149 for(
int level = grid.maxLevel(); level >= 0; --level )
152 for(
VertexLevelIterator it = grid.levelGridView( level ).template begin<dim>(); it != lEnd; ++it )
154 levelInfo[idSet.id( *it )] = level ;
165 void encode( Grid
const& grid, CoeffVector
const& sol, std::string fn,
double aTol_ = 0,
int maxLevel_ = -1 )
167 std::ofstream out( fn.c_str(), std::ios::binary ) ;
168 encode( grid, sol, out, aTol_, maxLevel_);
173 void encode( Grid
const& grid, CoeffVector
const& sol, std::ostream& out,
double aTol_ = 0,
int maxLevel_ = -1 )
175 double aTolSave = aTol ;
176 if( aTol_ > 0 ) aTol = aTol_ ;
179 int maxLevel = maxLevel_;
180 if( maxLevel < 0 ) maxLevel = grid.maxLevel() ;
182 double overhead = 0, entropy = 0, compressed = 0 ;
183 size_t nNodes = grid.size(
dim);
185 typename Grid::GlobalIdSet
const& idSet = grid.globalIdSet();
186 typename Grid::LeafGridView::IndexSet
const& leafIndexSet = grid.leafIndexSet();
188 std::vector<long int> indices ;
189 std::vector<double> predictionError ;
191 std::vector<std::vector<long int> > levelIndices;
194 for(
VertexLevelIterator it = grid.levelGridView( coarseLevel ).template begin<dim>() ; it != itEnd; ++it )
196 predictionError.push_back( -sol[leafIndexSet.index(*it)] ) ;
199 quantize( predictionError, indices) ;
200 reconstruct( predictionError, indices ) ;
202 levelIndices.push_back(indices);
204 CoeffVector reconstruction(grid.size(coarseLevel,
dim) ) ;
207 size_t nEntries = sol.size();
208 std::vector<long int> intervalIndicesTmp( nEntries );
209 long int minIndex = 0;
211 size_t vertexCount = 0 ;
212 for(
VertexLevelIterator it = grid.levelGridView(coarseLevel).template begin<dim>(); it != itEnd; ++it)
214 reconstruction[vertexCount] = -predictionError[vertexCount] ;
216 long int tmpIndex = indices[vertexCount];
217 intervalIndicesTmp[leafIndexSet.index(*it)] = tmpIndex;
218 if( tmpIndex < minIndex ) minIndex = tmpIndex;
223 CoeffVector prediction;
225 for(
int l = coarseLevel ; l < maxLevel ; l++ )
227 ps->mv(l, reconstruction, prediction) ;
229 std::vector<std::vector<size_t> > vertexInfo( prediction.size(), std::vector<size_t>(3) );
231 predictionError.clear();
232 predictionError.reserve( prediction.size() );
235 typename Grid::LevelGridView::IndexSet
const& levelIndexSet = grid.levelGridView(l+1).indexSet();
237 itEnd = grid.levelGridView(l+1).template end<dim>();
238 for(
VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
240 unsigned char vertexLevel = levelInfo[idSet.id( *it )] ;
241 if( vertexLevel == l+1 )
243 predictionError.push_back(prediction[levelIndexSet.index(*it)] - sol[leafIndexSet.index(*it)] );
245 vertexInfo[vertexCount][0] = levelIndexSet.index(*it) ;
246 vertexInfo[vertexCount][1] = leafIndexSet.index(*it) ;
247 vertexInfo[vertexCount][2] = vertexLevel ;
252 quantize( predictionError, indices) ;
253 reconstruct( predictionError, indices) ;
255 levelIndices.push_back(indices);
257 if( (l+1) < maxLevel )
260 reconstruction.resize( prediction.size() );
263 for(
size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
266 unsigned char vertexLevel = vertexInfo[ii][2];
267 IndexType levelIndex = vertexInfo[ii][0];
268 if( vertexLevel < l+1 )
270 reconstruction[levelIndex] = prediction[levelIndex];
274 long int tmpIndex = indices[vertexCount];
275 intervalIndicesTmp[vertexInfo[ii][1] ] = tmpIndex;
276 if( tmpIndex < minIndex ) minIndex = tmpIndex;
277 reconstruction[levelIndex] = prediction[levelIndex]-predictionError[vertexCount] ;
286 for(
size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
288 unsigned char vertexLevel = vertexInfo[ii][2];
289 if( vertexLevel < l+1 )
continue;
291 long int tmpIndex = indices[vertexCount];
292 intervalIndicesTmp[vertexInfo[ii][1] ] = tmpIndex;
293 if( tmpIndex < minIndex ) minIndex = tmpIndex;
299 std::vector<ULong> intervalIndices( nEntries ) ;
300 for(
size_t i = 0; i < nEntries; i++ ) intervalIndices[i] = intervalIndicesTmp[i] - minIndex ;
302 out.write(
reinterpret_cast<char*
>( &minIndex ),
sizeof(
long int) ) ;
303 overhead +=
sizeof(
long int);
305 std::map<unsigned long, unsigned long> freqMap;
306 unsigned int nnz = 0;
307 for(
size_t i = 0 ; i < nEntries ; i++ )
309 if( freqMap.find(intervalIndices[i] ) != freqMap.end() )
311 freqMap[intervalIndices[i]]++ ;
316 freqMap[intervalIndices[i]] = 1;
320 out.write(
reinterpret_cast<char*
>( &nnz ),
sizeof(
unsigned int) ) ;
321 overhead +=
sizeof(
unsigned int);
324 std::vector<unsigned long> frequenciesForRangeCoder(nnz);
326 std::map<unsigned long, unsigned long> dictMap;
327 unsigned long curNo = 0 ;
329 std::map<unsigned long,unsigned long>::iterator mapIt;
330 std::map<unsigned long,unsigned long>::iterator mapEnd = freqMap.end();
331 for( mapIt = freqMap.begin(); mapIt != mapEnd; ++mapIt )
333 unsigned long lv = mapIt->first;
334 frequenciesForRangeCoder[curNo] = mapIt->second;
335 dictMap.insert( dictMap.begin(), std::pair<unsigned long, unsigned long>(lv, curNo) ) ;
336 out.write(
reinterpret_cast<char*
>( &lv ),
sizeof(
unsigned long) ) ;
337 overhead +=
sizeof(
unsigned long);
342 for(
unsigned int i = 0 ; i < nnz ; i++ )
345 out.write(
reinterpret_cast<char*
>( &frequenciesForRangeCoder[i] ),
sizeof(
unsigned long) ) ;
346 overhead +=
sizeof(
unsigned long);
349 double tmp = -ld(frequenciesForRangeCoder[i]/((
double)nNodes)) *
350 frequenciesForRangeCoder[i]/((double)nNodes);
354 accumulatedCompressed += compressed/8*nNodes;
358 frequenciesForRangeCoder.clear();
367 for(
size_t i = 0 ; i < nEntries; i++ )
369 encodeSymbol( encoder, alphabet, dictMap.find(intervalIndices[i])->second );
375 size_t symbolCounter = 0 ;
376 std::vector<unsigned long> count(nnz, 1) ;
381 for(
size_t i = 0 ; i < nEntries; i++ )
383 encodeSymbol( encoder, alphabet, dictMap.find(intervalIndices[i])->second);
384 ++
count[dictMap[intervalIndices[i]]];
386 if (symbolCounter>0.1*nEntries)
394 accumulatedEntropy += entropy/8 ;
395 accumulatedOverhead += overhead;
396 accumulatedUncompressed += 8*nNodes;
405 void decode( Grid
const& gridState, CoeffVector& sol, std::string fn,
double aTol_ = 0,
int maxLevel_ = -1 )
407 std::ifstream in( fn.c_str(), std::ios::binary ) ;
408 decode(gridState, sol, in, aTol_, maxLevel_);
413 void decode( Grid
const& gridState, CoeffVector& sol, std::istream& in,
double aTol_ = 0,
int maxLevel_ = -1 )
415 double aTolSave = aTol ;
416 if( aTol_ > 0 ) aTol = aTol_ ;
418 int maxLevel = maxLevel_;
419 if( maxLevel < 0 ) maxLevel = gridState.maxLevel();
422 typename Grid::GlobalIdSet
const& idSet = gridState.globalIdSet();
423 IndexSet const& indexSet = gridState.leafIndexSet();
425 std::vector<double> values ;
426 std::vector<long int> intervalIndices( gridState.size(
dim) );
427 size_t nEntries = intervalIndices.size();
433 in.read(
reinterpret_cast<char*
>( &minIndex ),
sizeof(
long int) ) ;
436 in.read(
reinterpret_cast<char*
>( &nnz ),
sizeof(
unsigned int) ) ;
437 std::vector<long int> dictionary( nnz ) ;
438 for(
int i = 0 ; i < nnz ; i++ )
440 in.read(
reinterpret_cast<char*
>( &dictionary[i] ),
sizeof(
unsigned long) ) ;
443 std::vector<unsigned long> frequencies( nnz, 0 ) ;
444 for(
int i = 0 ; i < nnz ; i++ )
446 in.read(
reinterpret_cast<char*
>( &frequencies[i] ),
sizeof(
unsigned long) ) ;
453 for(
int i = 0 ; i < intervalIndices.size() ; i++ )
455 unsigned long s = decodeSymbol(decoder,alphabet) ;
456 intervalIndices[i] = dictionary[s] + minIndex ;
459 catch( std::ios_base::failure& e )
461 if (in.rdstate() & std::ifstream::eofbit)
463 std::cout <<
"EOF reached.\n";
467 std::cout <<
" Decoding error\n" << e.what() <<
"\n";
471 size_t symbolCounter = 0 ;
473 std::vector<unsigned long> symcount(nnz, 1) ;
479 for(
size_t i = 0 ; i < nEntries ; i++ )
481 unsigned long s = decodeSymbol(decoder,alphabet) ;
482 intervalIndices[i] = dictionary[s] + minIndex ;
487 if (symbolCounter>0.1*nEntries)
489 alphabet.
update(symcount.begin(),symcount.end());
494 catch( std::ios_base::failure& e )
496 if (in.rdstate() & std::ifstream::eofbit)
498 std::cout <<
"EOF reached.\n";
502 std::cout <<
" Decoding error\n" << e.what() <<
"\n";
512 CoeffVector reconstruction( gridState.size(coarseLevel,
dim) );
513 sol.resize( gridState.size(
dim) );
516 for(
VertexLevelIterator it = gridState.levelGridView( coarseLevel ).template begin<dim>(); it != itEnd; ++it)
519 double recVal = reconstruct( intervalIndices[indexSet.index(*it)]);
520 reconstruction[gridState.levelGridView(coarseLevel).indexSet().index(*it)] = -recVal ;
523 sol[ indexSet.index(*it) ] = -recVal ;
528 CoeffVector prediction;
530 for(
int l = coarseLevel ; l < maxLevel ; l++ )
532 ps->mv( l, reconstruction, prediction ) ;
534 reconstruction.resize( prediction.size() );
536 typename Grid::LevelGridView::IndexSet
const& levelIndexSet = gridState.levelGridView(l+1).indexSet();
540 itEnd = gridState.levelGridView(l+1).template end<dim>();
541 for(
VertexLevelIterator it = gridState.levelGridView(l+1).template begin<dim>(); it != itEnd ; ++it)
543 IndexType levelIndex = levelIndexSet.index(*it);
544 if(levelInfo[gridState.globalIdSet().id(*it)] == l+1)
546 reconstruction[levelIndex] = prediction[levelIndex] - reconstruct( intervalIndices[indexSet.index(*it)]);
550 reconstruction[levelIndex] = prediction[levelIndex];
552 sol[indexSet.index(*it)] = reconstruction[ levelIndex] ;
567 void quantize( std::vector<double>
const& values, std::vector<long int>& indices)
570 indices.resize( values.size(), 0 ) ;
572 for(
size_t i = 0 ; i < values.size() ; i++ )
574 indices[i] =
static_cast<long int>( floor( values[i] / (2*aTol) + 0.5 ) );
580 void reconstruct( std::vector<double>& values, std::vector<long int>
const& indices)
583 values.resize( indices.size() ) ;
584 for(
size_t i = 0 ; i < indices.size() ; i++ )
586 values[i] = indices[i] * 2* aTol ;
593 double reconstruct(
long int const& index )
599 double ld(
double val ) {
return log(val)/log(2.0) ; }
602 std::map<IdType, unsigned char> levelInfo ;
605 double accumulatedEntropy, accumulatedOverhead, accumulatedUncompressed, accumulatedCompressed;
A simple alphabet of symbols with frequencies to be used with the range coder.
void update(InIter first, InIter last)
Modifies the symbols' half-open ranges to represent the symbols' frequencies given in the range [firs...
void setup(Grid const &grid)
Grid::template Codim< dim >::LevelIterator VertexLevelIterator
Grid::template Codim< dim >::LeafIterator VertexLeafIterator
double reportOverallSize()
void decode(Grid const &gridState, CoeffVector &sol, std::istream &in, double aTol_=0, int maxLevel_=-1)
void decode(Grid const &gridState, CoeffVector &sol, std::string fn, double aTol_=0, int maxLevel_=-1)
Grid::Traits::GlobalIdSet::IdType IdType
double reportUncompressedSize()
Grid::LeafGridView::IndexSet::IndexType LeafIndexType
void encode(Grid const &grid, CoeffVector const &sol, std::string fn, double aTol_=0, int maxLevel_=-1)
Grid::LevelGridView::IndexSet::IndexType IndexType
void decode(GridManager< Grid > &gridManagerState, typename VariableSet::VariableSet &sol, std::string fn)
LossyStorage(int coarseLevel_, double aTol_)
void resetUncompressedSize()
VariableSet::VariableSet encode(typename VariableSet::VariableSet const &sol, std::string fn)
LossyStorage(GridManager< Grid > &gridManager_, VariableSet const &varSet_, int coarseLevel_, double aTol_, bool uniform_=false, QuantizationPolicy &quantizationPolicy_=QuantizationPolicy())
Grid::LeafIndexSet IndexSet
double reportCompressedSize()
void resetCompressedSize()
void encode(Grid const &grid, CoeffVector const &sol, std::ostream &out, double aTol_=0, int maxLevel_=-1)
Grid::template Codim< dim >::LeafIndexSet IndexSet
Entropy coding with range decoder.
Entropy coding with range encoder.
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Range coder for fast entropy coding.