KASKADE 7 development version
lossystorageDUNE_level.hh
Go to the documentation of this file.
1#ifndef LOSSYSTORAGEDUNE_HH
2#define LOSSYSTORAGEDUNE_HH
3
4#include <map>
5#include <unordered_map>
6#include <cassert>
7
8#include "dune/grid/common/grid.hh"
9#include "dune/grid/common/entitypointer.hh"
10#include "dune/grid/common/entity.hh"
11#include "dune/grid/io/file/dgfparser/dgfparser.hh"
12#include "dune/istl/matrix.hh"
13#include "dune/istl/bcrsmatrix.hh"
14#include "dune/common/fmatrix.hh"
15#include "dune/common/iteratorfacades.hh"
16#include "dune/istl/matrixindexset.hh"
17
18#include "rangecoder.hh"
19
20#include "lossy_helper.hh"
21
22// #define USEBOUNDS
23//#define USEFREQ
24// #define LEVELWISE
25
26template <class Grid, class CoeffVector>
27class LossyStorage
28{
29public:
30 static const int dim = Grid::dimension ;
31 // degree of interpolation polynomials; currently only linear interpolation is supported
32 static const int order = 1 ;
33
34 // some typedefs used throughout the class
35 typedef typename Grid::template Codim<dim>::LevelIterator VertexLevelIterator ;
36 typedef typename Grid::template Codim<dim>::LeafIterator VertexLeafIterator ;
37 typedef typename Grid::LeafIndexSet IndexSet;
38 typedef typename Grid::LevelGridView::IndexSet::IndexType IndexType;
39 typedef typename Grid::LeafGridView::IndexSet::IndexType LeafIndexType;
40
41 typedef typename Grid::Traits::GlobalIdSet::IdType IdType;
42
43 typedef unsigned long int ULong;
44
45 LossyStorage( int coarseLevel_, double aTol_) : ps(nullptr), coarseLevel(coarseLevel_), aTol(aTol_),
46 accumulatedEntropy(0), accumulatedOverhead(0),
47 accumulatedUncompressed(0), accumulatedCompressed(0)
48 {
49// #ifdef USEFREQ
50// std::cout << "USES PRECOMPUTED FREQUENCIES FOR ENCODING A SINGLE FUNCTION!\nBEWARE OF THE OVERHEAD!\n";
51// std::cout << "Due to implementation, not using pre-computed frequencies fails, as the vectors used for\n"
52// << "Alphabet are too large.\n";
53// #else
54// std::cout << "Uses dictionary for encoding. Beware of the overhead!\n";
55// std::cout << "Due to implementation, not using the dictionary fails, as the vectors used for\n"
56// << "Alphabet would be too large for fine quantization.\n";
57// #endif
58 }
59
60 ~LossyStorage() { if( ps != NULL ) delete ps; }
61
62 // returns the entropy (summed since the last call to resetEntropy/resetSizes)
63 // of the data (= average symbol size in byte needed to store the file)
65 {
66 return accumulatedEntropy;
67 }
68
69
71 {
72 accumulatedEntropy = 0;
73 }
74
75
76 // reports the overhead (summed since last call to resetOverhead/resetSizes)
77 // i.e. interval bounds, frequencies, etc.
79 {
80 return accumulatedOverhead;
81 }
82
84 {
85 accumulatedOverhead = 0;
86 }
87
88 // returns the compressed size
89 // which is (in the best case) the size of the encoded, compressed file
90 // without the overhead from storing intervals etc.
92 {
93 return accumulatedCompressed;
94 }
95
97 {
98 accumulatedCompressed = 0 ;
99 }
100
101 // returns the compressed size + overhead, which is (in the best case/in the limit)
102 // the size of the compressed file (including all side information)
104 {
105 return accumulatedCompressed + accumulatedOverhead;
106 }
107
108 // reset everything
110 {
111 accumulatedEntropy = 0;
112 accumulatedOverhead = 0;
113 accumulatedCompressed = 0;
114 accumulatedUncompressed = 0;
115 }
116
117
118 // returns the size needed for uncompressed storage of the data
120 {
121 return accumulatedUncompressed;
122 }
123
125 {
126 accumulatedUncompressed = 0 ;
127 }
128
129
130 // returns the compression factor (=uncompressed size/compressed size)
131 double reportRatio()
132 {
133 if( accumulatedCompressed + accumulatedOverhead > 0 )
134 return accumulatedUncompressed / (accumulatedCompressed + accumulatedOverhead );
135
136 return -1;
137 }
138
139
140 // TODO: has to be faster for adaptive meshes!
141 void setup( Grid const& grid )
142 {
143// boost::timer::cpu_timer timer;
144 if( ps != NULL ) delete ps;
146// std::cerr << "time for ps: " << timer.format() << "\n";
147
148// auto foo = Lossy_Detail::ProlongationStack<Grid>(grid); // for debugging, should use ps = new Prolongation<Grid> instead!
149
150 typename Grid::GlobalIdSet const& idSet = grid.globalIdSet();
151
152 levelInfo.clear();
153 for( int level = grid.maxLevel(); level >= 0; --level )
154 {
155 auto lEnd = grid.levelGridView(level).template end<dim>();
156 for( auto it = grid.levelGridView(level).template begin<dim>(); it != lEnd; ++it )
157 {
158 levelInfo[idSet.id( *it )] = level ;
159 }
160 }
161
162 }
163
164
165
170 void encode( Grid const& grid, CoeffVector const& sol, std::string fn, double aTol_ = 0, int maxLevel_ = -1 )
171 {
172 std::ofstream out( fn.c_str(), std::ios::binary ) ;
173 encode( grid, sol, out, aTol_, maxLevel_);
174 out.close();
175 }
176
177// void encode( Grid const& grid, CoeffVector const& sol, std::ostream& out, double aTol_ = 0, int maxLevel_ = -1)
178// {
179// // use maxLevel_ instead of maxLevel member
180// int maxLevel = maxLevel_;
181// if( maxLevel < 0 || maxLevel >= grid.maxLevel() )
182// {
183// maxLevel = grid.maxLevel() ;
184// encode( grid, sol, out, aTol_, maxLevel, grid.leafIndexSet());
185// }
186// else
187// {
188// encode( grid, sol, out, aTol_, maxLevel, grid.levelIndexSet(maxLevel));
189// }
190// }
191//
192//
193// template<class SolutionIndexSet>
194 void encode( Grid const& grid, CoeffVector const& sol, std::ostream& out, double aTol_, int maxLevel_ = -1/*,
195 SolutionIndexSet const& solutionIndexSet*/)
196 {
197 double aTolSave = aTol ;
198 if( aTol_ > 0 ) aTol = aTol_ ;
199
200 // use maxLevel_ instead of maxLevel member
201 int maxLevel = maxLevel_;
202 if( maxLevel < 0 )
203 {
204 maxLevel = grid.maxLevel() ;
205 }
206
207 double overhead = 0, entropy = 0, compressed = 0 ;
208// size_t nNodes = solutionIndexSet.size(dim);
209 size_t nNodes = grid.size(maxLevel, dim);
210
211
212
213 typename Grid::GlobalIdSet const& idSet = grid.globalIdSet();
214// typename Grid::LeafGridView::IndexSet const& leafIndexSet = grid.leafIndexSet();
215
216
217 std::vector<long int> indices ;
218 std::vector<double> predictionError ;
219
220 std::vector<std::vector<long int> > levelIndices;
221
222// std::cout << "\nLEVEL 0\n";
223
224 //auto itEnd = grid.levelGridView(coarseLevel).template end<dim>();
225// for( auto it = grid.levelGridView(coarseLevel).template begin<dim>() ; it != itEnd; ++it )
226 for( size_t i = 0; i < grid.size(coarseLevel, dim); i++ )
227 {
228// predictionError.push_back( -sol[/*leafIndexSet*/solutionIndexSet.index(*it)] ) ;
229// std::cout << -sol[leafIndexSet.index(*it)] << " (" << leafIndexSet.index(*it) << ") ";
230 predictionError.push_back( -sol[ ps->getIndexOnLevel(coarseLevel, i, maxLevel) ] );
231 // if sol lives on leaf and leaf is not maxlevel (i.e. adaptive mesh) this is not covered yet!
232 }
233
234 quantize( predictionError, indices) ;
235 reconstruct( predictionError, indices ) ;
236
237 levelIndices.push_back(indices);
238
239 CoeffVector reconstruction(grid.size(coarseLevel, dim) ) ;
240
241 // assign reconstructed coarse grid values
242 size_t nEntries = sol.size();
243 std::vector<long int> intervalIndicesTmp( nEntries );
244 long int minIndex = 0;
245
246 size_t vertexCount = 0 ;
247// for( VertexLevelIterator it = grid.levelGridView(coarseLevel).template begin<dim>(); it != itEnd; ++it)
248 for( size_t i = 0; i < grid.size(coarseLevel, dim); i++ )
249 {
250 reconstruction[vertexCount] = -predictionError[vertexCount] ;
251
252 long int tmpIndex = indices[vertexCount];
253 intervalIndicesTmp[/*solutionIndexSet.index(*it)*/ps->getIndexOnLevel(coarseLevel, i, maxLevel)] = tmpIndex;
254 if( tmpIndex < minIndex ) minIndex = tmpIndex;
255
256 vertexCount++ ;
257 }
258
259 CoeffVector prediction;
260
261 for( int l = coarseLevel ; l < maxLevel ; l++ )
262 {
263 ps->mv(l, reconstruction, prediction) ;
264// std::cout << "\n\nTO LEVEL " << l+1 << "\n";
265
266 std::vector<std::vector<size_t> > vertexInfo;
267 std::vector<size_t> idxVec(3);
268 //std::vector<std::vector<size_t> > vertexInfo( prediction.size(), std::vector<size_t>(3) );
269
270 predictionError.clear();
271 predictionError.reserve( prediction.size() ); // avoid reallocations
272
273// vertexCount = 0 ;
274 typename Grid::LevelGridView::IndexSet const& levelIndexSet = grid.levelGridView(l+1).indexSet();
275
276 auto itEnd = grid.levelGridView(l+1).template end<dim>();
277 for( auto it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
278 {
279 unsigned char vertexLevel = levelInfo[idSet.id( *it )] ;
280 auto levelIdx = levelIndexSet.index(*it);
281 if( vertexLevel == l+1 )
282 {
283// auto solIdx = /*leafIndexSet*/solutionIndexSet.index(*it);
284// if( ! solutionIndexSet.contains(*it) )
285// std::cerr << "LEVEL " << l+1 << " -- Entity idx " << levelIndexSet.index(*it)
286// << " not contained in solutionIndexSet (determined idx = " << leafIdx << ")!\n" ;
287 auto solIdx = ps->getIndexOnLevel(l+1, levelIdx, maxLevel);
288 predictionError.push_back(prediction[/*levelIndexSet.index(*it)*/levelIdx] - sol[/*solutionIndexSet.index(*it)*/solIdx] );
289// std::cout << prediction[levelIndexSet.index(*it)] -sol[leafIndexSet.index(*it)] << " " ;
290 idxVec[0] = levelIdx;
291 idxVec[1] = solIdx;//ps->getIndexOnLevel(l+1, levelIdx, maxLevel);
292 idxVec[2] = vertexLevel;
293 vertexInfo.push_back(idxVec);
294 }
295 //vertexInfo[vertexCount][0] = levelIdx;
296 //vertexInfo[vertexCount][1] = /*solutionIndexSet.index(*it); */ps->getIndexOnLevel(l+1, levelIdx, maxLevel);
297 //vertexInfo[vertexCount][2] = vertexLevel ;
298
299
300// std::cout << sol[leafIndexSet.index(*it)] << " (" << leafIndexSet.index(*it) << ") ";
301
302// vertexCount++;
303 }
304
305 quantize( predictionError, indices) ;
306 levelIndices.push_back(indices);
307
308 if( (l+1) < maxLevel )
309 {
310 reconstruct( predictionError, indices) ;
311
312 // prepare prediction on next level -- use reconstructed values
313 //reconstruction.resize( prediction.size() );
314 reconstruction = prediction;
315 vertexCount = 0 ;
316
317 for( size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
318 {
319 // correction only for the nodes on level l+1
320 unsigned char vertexLevel = vertexInfo[ii][2];
321 IndexType levelIndex = vertexInfo[ii][0];
322 if( vertexLevel < l+1 )
323 {
324
325 ;
326 //reconstruction[levelIndex] = prediction[levelIndex];
327 }
328 else
329 {
330 long int tmpIndex = indices[vertexCount];
331 intervalIndicesTmp[vertexInfo[ii][1] ] = tmpIndex;
332 if( tmpIndex < minIndex ) minIndex = tmpIndex;
333 //reconstruction[levelIndex] = prediction[levelIndex]-predictionError[vertexCount] ;
334 reconstruction[levelIndex] -= predictionError[vertexCount] ;
335 vertexCount++;
336 }
337 }
338 }
339 else
340 {
341 vertexCount = 0 ;
342 for( size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
343 {
344 unsigned char vertexLevel = vertexInfo[ii][2];//levelInfo[idSet.id( *it )] ;
345 if( vertexLevel < l+1 ) { continue; }
346
347 long int tmpIndex = indices[vertexCount];
348 intervalIndicesTmp[vertexInfo[ii][1] ] = tmpIndex;
349 if( tmpIndex < minIndex ) minIndex = tmpIndex;
350 vertexCount++ ;
351 }
352 }
353 }
354
355
356 std::vector<ULong> intervalIndices( nEntries ) ;
357 for( size_t i = 0; i < nEntries; i++ ) intervalIndices[i] = intervalIndicesTmp[i] - minIndex ;
358
359 out.write(reinterpret_cast<char*>( &minIndex ), sizeof(long int) ) ;
360// overhead += sizeof(long int);
361// std::cout << "minIndex = " << minIndex << "\n"; std::cout.flush();
362
363 std::unordered_map<unsigned long, unsigned long> freqMap;
364 unsigned int nnz = 0;
365 for( size_t i = 0 ; i < nEntries ; i++ )
366 {
367 if( freqMap.find(intervalIndices[i] ) != freqMap.end() ) // already there, increment
368 {
369 freqMap[intervalIndices[i]]++ ;
370 }
371 else // new nonzero, add to map
372 {
373 nnz++;
374 freqMap[intervalIndices[i]] = 1;
375 }
376 }
377
378 out.write(reinterpret_cast<char*>( &nnz ), sizeof(unsigned int) ) ;
379// overhead += sizeof(unsigned int);
380// std::cout << "nnz = " << nnz << "\n"; std::cout.flush();
381
382#ifdef USEFREQ
383 std::vector<unsigned long> frequenciesForRangeCoder(nnz);
384#endif
385
386 std::unordered_map<unsigned long, unsigned long> dictMap;
387 unsigned long curNo = 0 ;
388
389 std::unordered_map<unsigned long,unsigned long>::iterator mapIt;
390 std::unordered_map<unsigned long,unsigned long>::iterator mapEnd = freqMap.end();
391 for( mapIt = freqMap.begin(); mapIt != mapEnd; ++mapIt )
392 {
393 unsigned long lv = mapIt->first;
394#ifdef USEFREQ
395 frequenciesForRangeCoder[curNo] = mapIt->second;
396#endif
397 dictMap.insert( dictMap.begin(), std::pair<unsigned long, unsigned long>(lv, curNo) ) ;
398 out.write(reinterpret_cast<char*>( &lv ), sizeof(unsigned long) ) ;
399// overhead += sizeof(unsigned long);
400 curNo++;
401 }
402
403
404 for( unsigned int i = 0 ; i < nnz ; i++ )
405 {
406#ifdef USEFREQ
407 out.write(reinterpret_cast<char*>( &frequenciesForRangeCoder[i] ), sizeof(unsigned long) ) ;
408// overhead += sizeof(unsigned long);
409#endif
410
411// double tmp = -ld(frequenciesForRangeCoder[i]/((double)nNodes)) *
412// frequenciesForRangeCoder[i]/((double)nNodes);
413// entropy += tmp;
414// compressed += tmp;
415 }
416// accumulatedCompressed += compressed/8*nNodes;
417// compressed = 0;
418
419//#ifndef USEFREQ
420// frequenciesForRangeCoder.clear();
421// freqMap.clear();
422//#endif
423
424 std::cout.flush();
425#ifdef USEFREQ
426 Alphabet<unsigned long> alphabet( frequenciesForRangeCoder.begin(), frequenciesForRangeCoder.end() ) ;
427 RangeEncoder<unsigned long> encoder( out ) ;
428
429 for( size_t i = 0 ; i < nEntries; i++ )
430 {
431 encodeSymbol( encoder, alphabet, dictMap.find(intervalIndices[i])->second/* dictMap[intervalIndices[i]]*/ );
432 }
433
434#else
435
436
437 size_t symbolCounter = 0 ;
438 std::vector<unsigned long> count(nnz, 1) ;
439 Alphabet<unsigned long> alphabet( count.begin(), count.end() ) ;
440
441 RangeEncoder<unsigned long> encoder(out) ;
442
443 for( size_t i = 0 ; i < nEntries; i++ )
444 {
445 encodeSymbol( encoder, alphabet, dictMap.at(intervalIndices[i])/*dictMap.find(intervalIndices[i])->second*/);
446 ++count[dictMap[intervalIndices[i]]];
447 ++symbolCounter ;
448 if (symbolCounter>0.1*nEntries)
449 {
450 alphabet.update(count.begin(),count.end());
451 symbolCounter=0;
452 }
453 }
454#endif
455
456// accumulatedEntropy += entropy/8 ;
457// accumulatedOverhead += overhead;
458// accumulatedUncompressed += 8*nNodes;
459
460 aTol = aTolSave;
461 }
462
467 void decode( Grid const& gridState, CoeffVector& sol, std::string fn, double aTol_ = 0, int maxLevel_ = -1 )
468 {
469 std::ifstream in( fn.c_str(), std::ios::binary ) ;
470 decode(gridState, sol, in, aTol_, maxLevel_);
471 in.close() ;
472 }
473
474// void decode( Grid const& gridState, CoeffVector& sol, std::istream& in, double aTol_ = 0, int maxLevel_ = -1 )
475// {
476// // use maxLevel_ instead of maxLevel member
477// int maxLevel = maxLevel_;
478// if( maxLevel < 0 || maxLevel >= gridState.maxLevel() )
479// {
480// maxLevel = gridState.maxLevel() ;
481// decode( gridState, sol, in, aTol_, maxLevel, gridState.leafIndexSet());
482// }
483// else
484// {
485// decode( gridState, sol, in, aTol_, maxLevel, gridState.levelIndexSet(maxLevel));
486// }
487// }
488//
489// template<class SolutionIndexSet>
490 void decode( Grid const& gridState, CoeffVector& sol, std::istream& in, double aTol_, int maxLevel_ = -1/*,
491 SolutionIndexSet const& solutionIndexSet */)
492 {
493 double aTolSave = aTol ;
494 if( aTol_ > 0 ) aTol = aTol_ ;
495
496 int maxLevel = maxLevel_;
497 if( maxLevel < 0 ) maxLevel = gridState.maxLevel();
498
499 // prepare prediction
500 typename Grid::GlobalIdSet const& idSet = gridState.globalIdSet();
501// IndexSet const& indexSet = gridState.leafIndexSet();
502
503 std::vector<double> values ;
504 std::vector<long int> intervalIndices( gridState.size(maxLevel, dim)/*solutionIndexSet.size(dim) */);
505 size_t nEntries = intervalIndices.size();
506
507 //auto itEnd = gridState.levelGridView(coarseLevel).template begin<dim>();
508
509 // read in indices from file
510 long int minIndex ;
511 in.read(reinterpret_cast<char*>( &minIndex ), sizeof(long int) ) ;
512// std::cout << "minIndex = " << minIndex << "\n"; std::cout.flush();
513
514 unsigned int nnz ;
515 in.read(reinterpret_cast<char*>( &nnz ), sizeof(unsigned int) ) ; // # non-empty intervals
516// std::cout << "nnz = " << nnz << "\n"; std::cout.flush();
517
518 std::vector<long int> dictionary( nnz ) ;
519 for( int i = 0 ; i < nnz ; i++ )
520 {
521 in.read(reinterpret_cast<char*>( &dictionary[i] ), sizeof(unsigned long) ) ; // existing intervals (dictionary)
522 }
523#ifdef USEFREQ
524 std::vector<unsigned long> frequencies( nnz, 0 ) ;
525 for( int i = 0 ; i < nnz ; i++ )
526 {
527 in.read(reinterpret_cast<char*>( &frequencies[i] ), sizeof(unsigned long) ) ; // frequencies
528 }
529
530 Alphabet<unsigned long> alphabet( frequencies.begin(), frequencies.end() ) ;
531 try
532 {
533 RangeDecoder<unsigned long> decoder( in ) ;
534 for( int i = 0 ; i < intervalIndices.size() ; i++ )
535 {
536 unsigned long s = decodeSymbol(decoder,alphabet) ;
537 intervalIndices[i] = dictionary[s] + minIndex ;
538 }
539 }
540 catch( std::ios_base::failure& e )
541 {
542 if (in.rdstate() & std::ifstream::eofbit)
543 {
544 std::cout << "EOF reached.\n";
545 }
546 else
547 {
548 std::cout << " Decoding error\n" << e.what() << "\n";
549 }
550 }
551#else
552 size_t symbolCounter = 0 ;
553
554 std::vector<unsigned long> symcount(nnz, 1) ;
555 Alphabet<unsigned long> alphabet( symcount.begin(), symcount.end() ) ;
556
557 try
558 {
559 RangeDecoder<unsigned long> decoder( in ) ;
560 for( size_t i = 0 ; i < nEntries ; i++ )
561 {
562 unsigned long s = decodeSymbol(decoder,alphabet) ;
563 intervalIndices[i] = dictionary[s] + minIndex ;
564
565 ++symcount[s];
566 ++symbolCounter ;
567
568 if (symbolCounter>0.1*nEntries)
569 {
570 alphabet.update(symcount.begin(),symcount.end());
571 symbolCounter=0;
572 }
573 }
574 }
575 catch( std::ios_base::failure& e )
576 {
577 if (in.rdstate() & std::ifstream::eofbit)
578 {
579 std::cout << "EOF reached.\n";
580 }
581 else
582 {
583 std::cout << " Decoding error\n" << e.what() << "\n";
584 }
585 }
586#endif
587
588// in.close() ;
589
590 // start reconstruction
591 int vertexCount = 0;
592
593 CoeffVector reconstruction( gridState.size(coarseLevel, dim) );
594 sol.resize( gridState.size(maxLevel, dim) ); // if on leaf this has to be handeled differently
595
596// auto const& leafIndexSet = gridState.leafGridView().indexSet();
597
598 std::vector<double> reconstructedValues;
599 reconstruct(reconstructedValues, intervalIndices);
600
601// std::cout << "\n\nLEVEL 0\n";
602// for( VertexLevelIterator it = gridState.levelGridView( coarseLevel ).template begin<dim>(); it != itEnd; ++it)
603 for( size_t i = 0; i < gridState.size(coarseLevel, dim); i++ )
604 {
605
606 auto solIdx = ps->getIndexOnLevel(coarseLevel, i, maxLevel);
607 //double recVal = reconstruct( intervalIndices[/*solutionIndexSet.index(*it)*//*ps->getIndexOnLevel(coarseLevel, i, maxLevel)*/solIdx]);
608 double recVal = reconstructedValues[solIdx];
609 reconstruction[/*gridState.levelGridView(coarseLevel).indexSet().index(*it)*/i] = -recVal ;
610
611 // store predicted values for the coarse grid in solution vector
612 sol[/*solutionIndexSet.index(*it)*/ /* ps->getIndexOnLevel(coarseLevel, i, maxLevel)*/solIdx] = -recVal ;
613// std::cout << -recVal << " " << " (" << leafIndexSet.index(*it) << ") ";
614 //vertexCount++ ;
615 }
616
617
618
619 CoeffVector prediction;
620 // perform prediction and correction
621 for( int l = coarseLevel ; l < maxLevel ; l++ )
622 {
623 ps->mv( l, reconstruction, prediction ) ;
624
625// std::cout << "\n\nDECODE TO LEVEL " << l+1 << "\n";
626
627 //reconstruction.resize( prediction.size() );
628 reconstruction = prediction;
629
630 typename Grid::LevelGridView::IndexSet const& levelIndexSet = gridState.levelGridView(l+1).indexSet();
631
632 //vertexCount = 0 ;
633
634 auto itEnd = gridState.levelGridView(l+1).template end<dim>();
635 for( auto it = gridState.levelGridView(l+1).template begin<dim>(); it != itEnd ; ++it)
636 {
637 IndexType levelIndex = levelIndexSet.index(*it);
638 auto solIdx = ps->getIndexOnLevel(l+1, levelIndex, maxLevel);
639 if(levelInfo[/*gridState.globalIdSet()*/idSet.id(*it)] == l+1) //correct only vertices on level l+1
640 {
641 // reconstruction[levelIndex] = prediction[levelIndex]
642 //- reconstruct( intervalIndices[/*solutionIndexSet.index(*it)*//*ps->getIndexOnLevel(l+1, levelIndex, maxLevel)*/solIdx]);
643 reconstruction[levelIndex] -= reconstructedValues[solIdx];
644// std::cout << prediction[levelIndex] - reconstruction[levelIndex] << " ";
645 }
646// else
647// {
648// reconstruction[levelIndex] = prediction[levelIndex];
649// }
650 sol[/*solutionIndexSet.index(*it)*//*ps->getIndexOnLevel(l+1, levelIndex, maxLevel)*/solIdx] = reconstruction[ levelIndex] ;
651// std::cout << sol[leafIndexSet.index(*it)] << " " << " (" << leafIndexSet.index(*it) << ") ";
652
653 //vertexCount++ ;
654 }
655 }
656
657 //exit(1);
658 aTol = aTolSave ;
659 }
660
661
662
663private:
664 LossyStorage( LossyStorage const& );
665 LossyStorage& operator=( LossyStorage const& ) ;
666
667
669 void quantize( std::vector<double> const& values, std::vector<long int>& indices)
670 {
671 indices.clear() ;
672 indices.resize( values.size(), 0 ) ;
673
674 for( size_t i = 0 ; i < values.size() ; i++ )
675 {
676 indices[i] = static_cast<long int>( floor( values[i] / (2*aTol) + 0.5 ) );
677 }
678 }
679
680
682 void reconstruct( std::vector<double>& values, std::vector<long int> const& indices)
683 {
684 values.clear() ;
685 values.resize( indices.size() ) ;
686 for( size_t i = 0 ; i < indices.size() ; i++ )
687 {
688 values[i] = indices[i] * 2* aTol ;
689 }
690 }
691
692
693
695 double reconstruct( long int const& index )
696 {
697 return index*2*aTol;
698 }
699
701 double ld( double val ) { return log(val)/log(2.0) ; }
702
704 std::unordered_map<IdType, unsigned char> levelInfo ;
705 int coarseLevel ;
706 double aTol ;
707 double accumulatedEntropy, accumulatedOverhead, accumulatedUncompressed, accumulatedCompressed;
708
709// std::vector<std::vector<int>> idToLevelIdx;
710} ;
711
712#endif
A simple alphabet of symbols with frequencies to be used with the range coder.
Definition: rangecoder.hh:342
void update(InIter first, InIter last)
Modifies the symbols' half-open ranges to represent the symbols' frequencies given in the range [firs...
Definition: rangecoder.hh:365
static const int dim
Definition: lossystorage.hh:62
void setup(Grid const &grid)
Grid::template Codim< dim >::LevelIterator VertexLevelIterator
void encode(Grid const &grid, CoeffVector const &sol, std::ostream &out, double aTol_, int maxLevel_=-1)
Grid::template Codim< dim >::LeafIterator VertexLeafIterator
unsigned long int ULong
void decode(Grid const &gridState, CoeffVector &sol, std::string fn, double aTol_=0, int maxLevel_=-1)
Grid::Traits::GlobalIdSet::IdType IdType
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_)
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())
Definition: lossystorage.hh:76
Grid::LeafIndexSet IndexSet
void decode(Grid const &gridState, CoeffVector &sol, std::istream &in, double aTol_, int maxLevel_=-1)
Entropy coding with range decoder.
Definition: rangecoder.hh:245
Entropy coding with range encoder.
Definition: rangecoder.hh:136
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition: fixdune.hh:716
Range coder for fast entropy coding.