1#ifndef LOSSYSTORAGEDUNE_HH
2#define LOSSYSTORAGEDUNE_HH
6#include "dune/grid/common/grid.hh"
7#include "dune/grid/common/entitypointer.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"
20template <
class Gr
id,
class CoeffVector>
24 static const int dim = Grid::dimension ;
26 static const int order = 1 ;
32 typedef typename Grid::LevelGridView::IndexSet::IndexType
IndexType;
36 typedef typename Grid::Traits::GlobalIdSet::IdType
IdType;
38 typedef unsigned long int ULong;
40 LossyStorage(
int coarseLevel_,
double aTol_ ) : ps(NULL), coarseLevel(coarseLevel_), aTol(aTol_),
41 accumulatedEntropy(0), accumulatedOverhead(0),
42 accumulatedUncompressed(0), accumulatedCompressed(0)
55 return accumulatedEntropy;
61 accumulatedEntropy = 0;
69 return accumulatedOverhead;
74 accumulatedOverhead = 0;
83 return accumulatedCompressed;
88 accumulatedCompressed = 0 ;
95 return accumulatedCompressed + accumulatedOverhead;
101 accumulatedEntropy = 0;
102 accumulatedOverhead = 0;
103 accumulatedCompressed = 0;
104 accumulatedUncompressed = 0;
111 return accumulatedUncompressed;
116 accumulatedUncompressed = 0 ;
123 if( accumulatedCompressed + accumulatedOverhead > 0 )
124 return accumulatedUncompressed / (accumulatedCompressed + accumulatedOverhead );
133 boost::timer::cpu_timer timer;
134 if( ps != NULL )
delete ps;
141 maxLevel = grid.maxLevel();
143 typename Grid::GlobalIdSet
const& idSet = grid.globalIdSet();
148 for(
int level = maxLevel; level >= 0; --level )
151 for(
VertexLevelIterator it = grid.levelGridView( level ).template begin<dim>(); it != lEnd; ++it )
154 levelInfo[idSet.id( *it )] = level ;
177 void encode( Grid
const& grid, CoeffVector
const& sol, std::string fn,
double aTol_ = 0 )
179 boost::timer::cpu_timer timer;
180 boost::timer::cpu_timer fooTimer;
183 double aTolSave = aTol ;
184 if( aTol_ > 0 ) aTol = aTol_ ;
186 double overhead = 0, entropy = 0, compressed = 0 ;
187 size_t nNodes = grid.size(
dim);
189 std::ofstream out( fn.c_str(), std::ios::binary ) ;
192 typename Grid::GlobalIdSet
const& idSet = grid.globalIdSet();
193 typename Grid::LeafGridView::IndexSet
const& leafIndexSet = grid.leafIndexSet();
202 std::vector<
long int> indices ;
203 std::vector<double> predictionError ;
206 for(
VertexLevelIterator it = grid.levelGridView( coarseLevel ).template begin<dim>() ; it != itEnd; ++it )
208 predictionError.push_back( -sol[leafIndexSet.index(*it)] ) ;
220 quantize( predictionError, indices ) ;
221 reconstruct( predictionError, indices ) ;
230 CoeffVector reconstruction(grid.size(coarseLevel,
dim) ) ;
233 size_t nEntries = sol.size();
234 std::vector<
long int> intervalIndicesTmp( nEntries );
235 long int minIndex = 0;
237 size_t vertexCount = 0 ;
239 for(
VertexLevelIterator it = grid.levelGridView(coarseLevel).template begin<dim>(); it != itEnd; ++it)
241 reconstruction[vertexCount] = -predictionError[vertexCount] ;
243 long int tmpIndex = indices[vertexCount];
244 intervalIndicesTmp[leafIndexSet.index(*it)] = tmpIndex;
245 if( tmpIndex < minIndex ) minIndex = tmpIndex;
253 CoeffVector prediction;
255 double quantTime = 0, mvTime = 0, prepTime = 0, predErrTime = 0;
257 for(
int l = coarseLevel ; l < maxLevel ; l++ )
264 ps->mv(l, reconstruction, prediction) ;
278 mvTime += (double)(fooTimer.elapsed().user)/1e9 ;
280 std::vector<std::vector<size_t> > vertexInfo( prediction.size(), std::vector<size_t>(3) );
283 predictionError.clear();
284 predictionError.reserve( prediction.size() );
287 typename Grid::LevelGridView::IndexSet
const& levelIndexSet = grid.levelView(l+1).indexSet();
291 itEnd = grid.levelGridView(l+1).template end<dim>();
292 for(
VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
294 unsigned char vertexLevel = levelInfo[idSet.id( *it )] ;
295 if( vertexLevel == l+1 )
299 predictionError.push_back(prediction[levelIndexSet.index(*it)] - sol[leafIndexSet.index(*it)] );
301 vertexInfo[vertexCount][0] = levelIndexSet.index(*it) ;
302 vertexInfo[vertexCount][1] = leafIndexSet.index(*it) ;
303 vertexInfo[vertexCount][2] = vertexLevel ;
312 predErrTime += (double)(fooTimer.elapsed().user)/1e9;
322 quantize( predictionError, indices) ;
323 reconstruct( predictionError, indices ) ;
324 quantTime += (double)(fooTimer.elapsed().user)/1e9;
327 if( (l+1) < maxLevel )
330 reconstruction.resize( prediction.size() );
334 for(
size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
337 unsigned char vertexLevel = vertexInfo[ii][2];
338 IndexType levelIndex = vertexInfo[ii][0];
339 if( vertexLevel < l+1 )
341 reconstruction[levelIndex] = prediction[levelIndex];
345 long int tmpIndex = indices[vertexCount];
346 intervalIndicesTmp[vertexInfo[ii][1] ] = tmpIndex;
347 if( tmpIndex < minIndex ) minIndex = tmpIndex;
349 reconstruction[levelIndex] = prediction[levelIndex]-predictionError[vertexCount] ;
360 for(
size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
362 unsigned char vertexLevel = vertexInfo[ii][2];
363 if( vertexLevel < l+1 )
continue;
365 long int tmpIndex = indices[vertexCount];
366 intervalIndicesTmp[vertexInfo[ii][1] ] = tmpIndex;
367 if( tmpIndex < minIndex ) minIndex = tmpIndex;
371 prepTime += (double)(fooTimer.elapsed().user)/1e9;
390 std::vector<ULong> intervalIndices( nEntries ) ;
391 for(
size_t i = 0; i < nEntries; i++ ) intervalIndices[i] = intervalIndicesTmp[i] - minIndex ;
393 out.write(
reinterpret_cast<char*
>( &minIndex ),
sizeof(
long int) ) ;
394 overhead +=
sizeof(
long int);
399 std::map<unsigned long, unsigned long> freqMap;
400 unsigned int nnz = 0;
401 for(
size_t i = 0 ; i < nEntries ; i++ )
403 if( freqMap.find(intervalIndices[i] ) != freqMap.end() )
405 freqMap[intervalIndices[i]]++ ;
410 freqMap[intervalIndices[i]] = 1;
417 std::vector<unsigned long> frequenciesForRangeCoder(nnz);
422 std::map<unsigned long, unsigned long> dictMap;
423 unsigned long curNo = 0 ;
425 std::map<unsigned long,unsigned long>::iterator mapIt;
426 std::map<unsigned long,unsigned long>::iterator mapEnd = freqMap.end();
427 for( mapIt = freqMap.begin(); mapIt != mapEnd; ++mapIt )
429 unsigned long lv = mapIt->first;
430 frequenciesForRangeCoder[curNo] = mapIt->second;
431 dictMap.insert( dictMap.begin(), std::pair<unsigned long, unsigned long>(lv, curNo) ) ;
441 for(
unsigned int i = 0 ; i < nnz ; i++ )
446 double tmp = -ld(frequenciesForRangeCoder[i]/((
double)nNodes)) *
447 frequenciesForRangeCoder[i]/((double)nNodes);
451 accumulatedCompressed += compressed/8*nNodes;
459 std::cout <<
"\nEncoding " << intervalIndices.size() <<
" values, "
460 << frequenciesForRangeCoder.size() <<
" different values\n" ;
461 std::cout <<
"Overhead: " << overhead <<
" B\n";
472 size_t maxIntervals = *std::max_element(intervalIndices.begin(), intervalIndices.end()) + 1;
473 out.write(
reinterpret_cast<char*
>( &maxIntervals ),
sizeof(
size_t) ) ;
474 overhead +=
sizeof(size_t);
476 size_t symbolCounter = 0 ;
477 std::vector<unsigned long> count(maxIntervals, 1) ;
481 for(
size_t i = 0 ; i < nEntries; i++ )
483 encodeSymbol( encoder, alphabet, intervalIndices[i] );
484 ++
count[intervalIndices[i]];
486 if (symbolCounter>0.1*alphabet.
size())
493 accumulatedEntropy += entropy/8 ;
494 accumulatedOverhead += overhead;
495 accumulatedUncompressed += 8*nNodes;
511 void decode( Grid
const& gridState, CoeffVector& sol, std::string fn,
double aTol_ = 0 )
513 double aTolSave = aTol ;
514 if( aTol_ > 0 ) aTol = aTol_ ;
516 std::ifstream in( fn.c_str(), std::ios::binary ) ;
523 IndexSet const& indexSet = gridState.leafIndexSet();
525 std::vector<double> values ;
526 std::vector<long int> intervalIndices( gridState.size(
dim) );
539 in.read(
reinterpret_cast<char*
>( &minIndex ),
sizeof(
long int) ) ;
575 size_t symbolCounter = 0 ;
577 in.read(
reinterpret_cast<char*
>( &maxIntervals ),
sizeof(
size_t) ) ;
579 std::vector<unsigned long> symcount(maxIntervals, 1) ;
584 for(
size_t i = 0 ; i < intervalIndices.size() ; i++ )
586 unsigned long s = decodeSymbol(decoder,alphabet) ;
587 intervalIndices[i] = s + minIndex ;
590 if (symbolCounter>0.1*alphabet.
size())
592 alphabet.
update(symcount.begin(),symcount.end());
597 catch( std::ios_base::failure& e )
599 if (in.rdstate() & std::ifstream::eofbit)
601 std::cout <<
"EOF reached.\n";
605 std::cout <<
" Decoding error\n" << e.what() <<
"\n";
628 CoeffVector reconstruction( gridState.size(coarseLevel,
dim) );
629 sol.resize( gridState.size(
dim) );
636 for(
VertexLevelIterator it = gridState.levelGridView( coarseLevel ).template begin<dim>(); it != itEnd; ++it)
638 double recVal = reconstruct( intervalIndices[indexSet.index(*it)]);
639 reconstruction[gridState.levelView(coarseLevel).indexSet().index(*it)] = -recVal ;
644 sol[ indexSet.index(*it) ] = -recVal ;
651 CoeffVector prediction;
653 for(
int l = coarseLevel ; l < maxLevel ; l++ )
659 ps->mv( l, reconstruction, prediction ) ;
662 reconstruction.resize( prediction.size() );
665 typename Grid::LevelGridView::IndexSet
const& levelIndexSet = gridState.levelView(l+1).indexSet();
669 itEnd = gridState.levelGridView(l+1).template end<dim>();
670 for(
VertexLevelIterator it = gridState.levelGridView(l+1).template begin<dim>(); it != itEnd ; ++it)
672 IndexType levelIndex = levelIndexSet.index(*it);
673 if(levelInfo[gridState.globalIdSet().id(*it)] == l+1)
675 reconstruction[levelIndex] = prediction[levelIndex] -
676 reconstruct( intervalIndices[indexSet.index(*it)]);
680 reconstruction[levelIndex] = prediction[levelIndex];
682 sol[indexSet.index(*it)] = reconstruction[ levelIndex] ;
694 void encode( Grid
const& grid, CoeffVector
const& v, CoeffVector
const& w,
695 std::vector<std::vector<long int> > & intervalIndicesTmp,
698 boost::timer::cpu_timer timer;
699 boost::timer::cpu_timer fooTimer;
700 double aTolSave = aTol ;
701 if( aTol_ > 0 ) aTol = aTol_ ;
703 double overhead = 0, entropy = 0, compressed = 0 ;
704 size_t nNodes = grid.size(
dim);
711 typename Grid::GlobalIdSet
const& idSet = grid.globalIdSet();
712 typename Grid::LeafGridView::IndexSet
const& leafIndexSet = grid.leafIndexSet();
716 std::vector<long int> indices_v ;
717 std::vector<long int> indices_w ;
718 std::vector<double> predictionError_v ;
719 std::vector<double> predictionError_w ;
722 for(
VertexLevelIterator it = grid.levelGridView( coarseLevel ).template begin<dim>() ; it != itEnd; ++it )
724 predictionError_v.push_back( -v[leafIndexSet.index(*it)] ) ;
725 predictionError_w.push_back( -w[leafIndexSet.index(*it)] ) ;
728 quantize( predictionError_v, indices_v) ;
729 reconstruct( predictionError_v, indices_v) ;
731 quantize( predictionError_w, indices_w) ;
732 reconstruct( predictionError_w, indices_w) ;
734 CoeffVector reconstruction_v( grid.size(coarseLevel,
dim) ) ;
735 CoeffVector reconstruction_w( reconstruction_v.size() ) ;
738 size_t nEntries = v.size();
740 intervalIndicesTmp.resize( 2, std::vector<long int>(nEntries) );
743 size_t vertexCount = 0 ;
744 for(
VertexLevelIterator it = grid.levelGridView(coarseLevel).template begin<dim>(); it != itEnd; ++it)
746 reconstruction_v[vertexCount] = -predictionError_v[vertexCount] ;
747 reconstruction_w[vertexCount] = -predictionError_w[vertexCount] ;
750 long int tmpIndex = indices_v[vertexCount];
751 intervalIndicesTmp[0][leafIndexSet.index(*it)] = tmpIndex;
754 tmpIndex = indices_w[vertexCount];
755 intervalIndicesTmp[1][leafIndexSet.index(*it)] = tmpIndex;
763 CoeffVector prediction_v, prediction_w;
765 double quantTime = 0, mvTime = 0, prepTime = 0, predErrTime = 0, boundsTime = 0, allocTime = 0,
766 vertexInfoTime = 0, levelIndexTime = 0, leafIndexTime = 0, levelInfoTime = 0;
768 for(
int l = coarseLevel ; l < maxLevel ; l++ )
771 ps->mv(l, reconstruction_v, prediction_v) ;
772 ps->mv(l, reconstruction_w, prediction_w) ;
773 mvTime += (double)(fooTimer.elapsed().user)/1e9 ;
776 size_t levelSize = prediction_v.size() ;
777 std::vector<std::vector<size_t> > vertexInfo( levelSize, std::vector<size_t>(3) );
778 predictionError_v.clear();
779 predictionError_v.reserve( levelSize);
780 predictionError_w.clear();
781 predictionError_w.reserve( levelSize);
784 typename Grid::LevelGridView::IndexSet
const& levelIndexSet = grid.levelView(l+1).indexSet();
786 itEnd = grid.levelGridView(l+1).template end<dim>();
788 allocTime += (double)(fooTimer.elapsed().user)/1e9;
791 for(
VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
793 predErrTime += (double)(fooTimer.elapsed().user)/1e9;
796 vertexInfo[vertexCount][0] = levelIndexSet.index(*it) ;
797 levelIndexTime += (double)(fooTimer.elapsed().user)/1e9;
800 vertexInfo[vertexCount][2] = levelInfo[idSet.id( *it )] ;
801 levelInfoTime += (double)(fooTimer.elapsed().user)/1e9;
804 if( vertexInfo[vertexCount][2] == l+1 )
807 auto tmpLeafIxd = leafIndexSet.index(*it) ;
808 vertexInfo[vertexCount][1] = tmpLeafIxd;
809 leafIndexTime += (double)(fooTimer.elapsed().user)/1e9;
811 predictionError_v.push_back(prediction_v[vertexInfo[vertexCount][0]] - v[tmpLeafIxd] );
812 predictionError_w.push_back(prediction_w[vertexInfo[vertexCount][0]] - w[tmpLeafIxd] );
816 predErrTime += (double)(fooTimer.elapsed().user)/1e9;
819 quantize( predictionError_v, indices_v) ;
820 reconstruct( predictionError_v, indices_v) ;
821 quantize( predictionError_w, indices_w) ;
822 reconstruct( predictionError_w, indices_w) ;
823 quantTime += (double)(fooTimer.elapsed().user)/1e9;
827 if( (l+1) < maxLevel )
830 reconstruction_v.resize( prediction_v.size() );
831 reconstruction_w.resize( prediction_w.size() );
834 for(
size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
837 unsigned char vertexLevel = vertexInfo[ii][2];
838 IndexType levelIndex = vertexInfo[ii][0];
839 if( vertexLevel < l+1 )
841 reconstruction_v[levelIndex] = prediction_v[levelIndex];
842 reconstruction_w[levelIndex] = prediction_w[levelIndex];
846 long int tmpIndex = indices_v[vertexCount];
847 intervalIndicesTmp[0][vertexInfo[ii][1] ] = tmpIndex;
850 tmpIndex = indices_w[vertexCount];
851 intervalIndicesTmp[1][vertexInfo[ii][1] ] = tmpIndex;
854 reconstruction_v[levelIndex] = prediction_v[levelIndex]-predictionError_v[vertexCount] ;
855 reconstruction_w[levelIndex] = prediction_w[levelIndex]-predictionError_w[vertexCount] ;
864 for(
size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
866 unsigned char vertexLevel = vertexInfo[ii][2];
867 if( vertexLevel < l+1 )
continue;
869 long int tmpIndex = indices_v[vertexCount];
870 intervalIndicesTmp[0][vertexInfo[ii][1] ] = tmpIndex;
873 tmpIndex = indices_w[vertexCount];
874 intervalIndicesTmp[1][vertexInfo[ii][1] ] = tmpIndex;
880 prepTime += (double)(fooTimer.elapsed().user)/1e9;
902 void write(std::vector<std::vector<long int> >
const & intervalIndicesTmp,
903 std::string fn_v, std::string fn_w )
906 std::ofstream out[2];
907 out[0].open(fn_v.c_str(), std::ios::binary ) ;
908 out[1].open(fn_w.c_str(), std::ios::binary ) ;
910 size_t nFunctions = intervalIndices.size();
911 assert(nFunctions == 2);
913 size_t nEntries = intervalIndicesTmp[0].size();
915 boost::timer::cpu_timer timer;
916 boost::timer::cpu_timer fooTimer;
918 long int minIndex[2] = {0,0};
919 minIndex[0] = *std::min_element( intervalIndicesTmp[0].begin(), intervalIndicesTmp[0].end() ) ;
920 minIndex[1] = *std::min_element( intervalIndicesTmp[1].begin(), intervalIndicesTmp[1].end() ) ;
922 std::vector<ULong> intervalIndices( nEntries ) ;
925 for(
int function = 0 ; function < 2 ; function++ )
928 for(
size_t i = 0; i < nEntries; i++ )
929 intervalIndices[i] = intervalIndicesTmp[function][i] - minIndex[function] ;
931 out[function].write(
reinterpret_cast<char*
>( &minIndex[function] ),
sizeof(
long int) ) ;
932 overhead +=
sizeof(
long int);
937 std::map<unsigned long, unsigned long> freqMap;
938 unsigned int nnz = 0;
939 for(
size_t i = 0 ; i < nEntries ; i++ )
941 if( freqMap.find(intervalIndices[i] ) != freqMap.end() )
943 freqMap[intervalIndices[i]]++ ;
948 freqMap[intervalIndices[i]] = 1;
952 out[function].write(
reinterpret_cast<char*
>( &nnz ),
sizeof(
unsigned int) ) ;
953 overhead +=
sizeof(
unsigned int);
956 std::vector<unsigned long> frequenciesForRangeCoder(nnz);
961 std::map<unsigned long, unsigned long> dictMap;
962 unsigned long curNo = 0 ;
964 std::map<unsigned long,unsigned long>::iterator mapIt;
965 std::map<unsigned long,unsigned long>::iterator mapEnd = freqMap.end();
966 for( mapIt = freqMap.begin(); mapIt != mapEnd; ++mapIt )
968 unsigned long lv = mapIt->first;
969 frequenciesForRangeCoder[curNo] = mapIt->second;
970 dictMap.insert( dictMap.begin(), std::pair<unsigned long, unsigned long>(lv, curNo) ) ;
972 out[function].write(
reinterpret_cast<char*
>( &lv ),
sizeof(
unsigned long) ) ;
973 overhead +=
sizeof(
unsigned long);
978 for(
unsigned int i = 0 ; i < nnz ; i++ )
980 out[function].write(
reinterpret_cast<char*
>( &frequenciesForRangeCoder[i] ),
sizeof(
unsigned long) ) ;
981 overhead +=
sizeof(
unsigned long);
983 double tmp = -ld(frequenciesForRangeCoder[i]/((
double)nNodes)) *
984 frequenciesForRangeCoder[i]/((double)nNodes);
988 accumulatedCompressed += compressed/8*nNodes;
998 for(
size_t i = 0 ; i < nEntries; i++ )
1000 encodeSymbol( encoder, alphabet, dictMap.find(intervalIndices[i])->second );
1006 accumulatedEntropy += entropy/8 ;
1007 accumulatedOverhead += overhead;
1008 accumulatedUncompressed += 2*8*nNodes;
1014 std::vector<std::vector<long int> > & intervalIndices,
1015 std::string fn_v, std::string fn_w )
1017 std::ifstream in[2];
1018 in[0].open( fn_v.c_str(), std::ios::binary ) ;
1019 in[1].open( fn_w.c_str(), std::ios::binary ) ;
1021 intervalIndices.resize( 2, std::vector<long int>(nEntries) );
1023 long int minIndex[2];
1024 for(
int function = 0 ; function < 2 ; function++)
1027 in[function].read(
reinterpret_cast<char*
>( &minIndex[function] ),
sizeof(
long int) ) ;
1030 in[function].read(
reinterpret_cast<char*
>( &nnz ),
sizeof(
unsigned int) ) ;
1031 std::vector<ULong> dictionary( nnz ) ;
1032 for(
int i = 0 ; i < nnz ; i++ )
1034 in[function].read(
reinterpret_cast<char*
>( &dictionary[i] ),
sizeof(
unsigned long) ) ;
1036 std::vector<ULong> frequencies( nnz, 0 ) ;
1037 for(
int i = 0 ; i < nnz ; i++ )
1039 in[function].read(
reinterpret_cast<char*
>( &frequencies[i] ),
sizeof(
unsigned long) ) ;
1047 for(
int i = 0 ; i < nEntries ; i++ )
1049 unsigned long s = decodeSymbol(decoder,alphabet) ;
1050 intervalIndices[function][i] = dictionary[s] + minIndex[function] ;
1053 catch( std::ios_base::failure& e )
1055 if (in[function].rdstate() & std::ifstream::eofbit)
1057 std::cout <<
"EOF reached.\n";
1061 std::cout <<
" Decoding error\n" << e.what() <<
"\n";
1064 in[function].close() ;
1071 void decode( Grid
const& gridState, CoeffVector& v, CoeffVector& w,
1072 std::vector<std::vector<long int>
const& intervalIndices,
1075 double aTolSave = aTol ;
1076 if( aTol_ > 0 ) aTol = aTol_ ;
1078 assert( intervalIndices.size() == 2 );
1079 size_t nEntries = intervalIndices[0].size();
1080 assert( nEntries = gridState.size(
dim);
1083 IndexSet const& indexSet = gridState.leafIndexSet();
1084 std::vector<double> values_v, values_w ;
1089 int vertexCount = 0;
1091 CoeffVector reconstruction_v( gridState.size(coarseLevel,
dim) );
1092 v.resize( gridState.size(
dim) );
1093 CoeffVector reconstruction_w( gridState.size(coarseLevel,
dim) );
1094 w.resize( gridState.size(
dim) );
1097 for(
VertexLevelIterator it = gridState.levelGridView( coarseLevel ).template begin<dim>(); it != itEnd; ++it)
1099 double recVal = reconstruct( intervalIndices[0][indexSet.index(*it)]);
1100 reconstruction_v[gridState.levelView(coarseLevel).indexSet().index(*it)] = -recVal ;
1101 v[ indexSet.index(*it) ] = -recVal ;
1103 recVal = reconstruct( intervalIndices[1][indexSet.index(*it)]);
1104 reconstruction_w[gridState.levelView(coarseLevel).indexSet().index(*it)] = -recVal ;
1105 w[ indexSet.index(*it) ] = -recVal ;
1111 CoeffVector prediction_v, prediction_w;
1113 for(
int l = coarseLevel ; l < maxLevel ; l++ )
1115 ps->mv( l, reconstruction_v, prediction_v ) ;
1116 ps->mv( l, reconstruction_w, prediction_w ) ;
1118 reconstruction_v.resize( prediction_v.size() );
1119 reconstruction_w.resize( prediction_w.size() );
1122 typename Grid::LevelGridView::IndexSet const& levelIndexSet = gridState.levelView(l+1).indexSet();
1126 itEnd = gridState.levelGridView(l+1).template end<dim>();
1127 for( VertexLevelIterator it = gridState.levelGridView(l+1).template begin<dim>(); it != itEnd ; ++it)
1129 IndexType levelIndex = levelIndexSet.index(*it);
1130 if(levelInfo[gridState.globalIdSet().id(*it)] == l+1)
1132 reconstruction_v[levelIndex] = prediction_v[levelIndex] -
1133 reconstruct( intervalIndices[0][indexSet.index(*it)]);
1134 reconstruction_w[levelIndex] = prediction_w[levelIndex] -
1135 reconstruct( intervalIndices[1][indexSet.index(*it)]);
1140 reconstruction_v[levelIndex] = prediction_v[levelIndex];
1141 reconstruction_w[levelIndex] = prediction_w[levelIndex];
1143 v[indexSet.index(*it)] = reconstruction_v[ levelIndex] ;
1144 w[indexSet.index(*it)] = reconstruction_w[ levelIndex] ;
1158 void quantize( std::vector<double>
const& values, std::vector</*ULong*/long int>& indices,
double lb,
double ub )
1160 double range = ub-lb;
1161 long int nIntervals = (
long int) ceil( range / aTol / 2.0 ) ;
1162 if( nIntervals % 2 == 0 ) nIntervals += 1 ;
1167 indices.resize( values.size(), 0 ) ;
1172 for(
size_t i = 0 ; i < values.size() ; i++ )
1175 indices[i] = ((values[i] - lb) * nIntervals / range ) + 1 ;
1181 void quantize( std::vector<double>
const& values, std::vector</*ULong*/long int>& indices)
1184 indices.resize( values.size(), 0 ) ;
1186 for(
size_t i = 0 ; i < values.size() ; i++ )
1188 indices[i] = floor( values[i] / (2*aTol) + 0.5 ) ;
1193 void reconstruct( std::vector<double>& values, std::vector</*ULong*/long int>
const& indices,
double lb,
double ub )
1195 double range = ub-lb;
1196 long int nIntervals = (
long int) ceil( range / aTol / 2.0 ) ;
1197 if( nIntervals % 2 == 0 ) nIntervals += 1 ;
1200 if( nIntervals <= 0 ) delta = 0 ;
1201 else delta = range / nIntervals ;
1204 values.resize( indices.size() ) ;
1205 for(
size_t i = 0 ; i < indices.size() ; i++ )
1208 values[i] = lb + (indices[i]-0.5) * delta;
1213 void reconstruct( std::vector<double>& values, std::vector</*ULong*/long int>
const& indices)
1216 values.resize( indices.size() ) ;
1217 for(
size_t i = 0 ; i < indices.size() ; i++ )
1219 values[i] = indices[i] * 2* aTol ;
1224 double reconstruct(
long int const& index,
double lb,
double ub)
1226 double range = ub-lb;
1227 long int nIntervals = (
long int) ceil( range / aTol / 2.0 ) ;
1228 if( nIntervals % 2 == 0 ) nIntervals += 1 ;
1231 if( nIntervals <= 0 ) delta = 0 ;
1232 else delta = range / nIntervals ;
1234 return lb + (index-0.5) * delta;
1239 double reconstruct(
long int const& index )
1241 return index*2*aTol;
1245 double ld(
double val ) {
return log(val)/log(2.0) ; }
1249 std::map<IdType, unsigned char> levelInfo ;
1250 int coarseLevel, maxLevel ;
1252 double accumulatedEntropy, accumulatedOverhead, accumulatedUncompressed, accumulatedCompressed;
A simple alphabet of symbols with frequencies to be used with the range coder.
UInt size() const
Returns the number of symbols in the alphabet.
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
void decode(Grid const &gridState, CoeffVector &sol, std::string fn, double aTol_=0)
Grid::template Codim< dim >::LeafIterator VertexLeafIterator
double reportOverallSize()
void decode(Grid const &gridState, CoeffVector &v, CoeffVector &w, std::vector< std::vector< long int > const &intervalIndices, double aTol_=0)
void encode(Grid const &grid, CoeffVector const &sol, std::string fn, double aTol_=0)
Grid::Traits::GlobalIdSet::IdType IdType
double reportUncompressedSize()
Grid::LeafGridView::IndexSet::IndexType LeafIndexType
Grid::LevelGridView::IndexSet::IndexType IndexType
LossyStorage(int coarseLevel_, double aTol_)
void resetUncompressedSize()
Grid::LeafIndexSet IndexSet
void read(size_t nEntries, std::vector< std::vector< long int > > &intervalIndices, std::string fn_v, std::string fn_w)
double reportCompressedSize()
void write(std::vector< std::vector< long int > > const &intervalIndicesTmp, std::string fn_v, std::string fn_w)
void encode(Grid const &grid, CoeffVector const &v, CoeffVector const &w, std::vector< std::vector< long int > > &intervalIndicesTmp, double aTol_=0)
void resetCompressedSize()
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.