KASKADE 7 development version
Loading...
Searching...
No Matches
lossystorageDUNE_time.hh
Go to the documentation of this file.
1#ifndef LOSSYSTORAGEDUNE_HH
2#define LOSSYSTORAGEDUNE_HH
3
4#include <map>
5
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"
15
16#include "rangecoder.hh"
17
18#include "lossy_helper.hh"
19
20template <class Grid, class CoeffVector>
21class LossyStorage
22{
23public:
24 static const int dim = Grid::dimension ;
25 // degree of interpolation polynomials; currently only linear interpolation is supported
26 static const int order = 1 ;
27
28 // some typedefs used throughout the class
29 typedef typename Grid::template Codim<dim>::LevelIterator VertexLevelIterator ;
30 typedef typename Grid::template Codim<dim>::LeafIterator VertexLeafIterator ;
31 typedef typename Grid::LeafIndexSet IndexSet;
32 typedef typename Grid::LevelGridView::IndexSet::IndexType IndexType;
33 typedef typename Grid::LeafGridView::IndexSet::IndexType LeafIndexType;
34
35
36 typedef typename Grid::Traits::GlobalIdSet::IdType IdType;
37
38 typedef unsigned long int ULong;
39
40 LossyStorage( int coarseLevel_, double aTol_ ) : ps(NULL), coarseLevel(coarseLevel_), aTol(aTol_),
41 accumulatedEntropy(0), accumulatedOverhead(0),
42 accumulatedUncompressed(0), accumulatedCompressed(0)
43 {
44// std::cout << "BEWARE: USES LB, UB FOR ENCODING TWO FUNCTIONS. USING ENCODE(X) NEEDS TO BE ADAPTED!!!\n";
45// std::cerr << "BEWARE: USES LB, UB FOR ENCODING TWO FUNCTIONS. USING ENCODE(X) NEEDS TO BE ADAPTED!!!\n";
46 }
47
48 ~LossyStorage() { if( ps != NULL ) delete ps; }
49
50
51 // returns the entropy (summed since the last call to resetEntropy/resetSizes)
52 // of the data (= average symbol size needed to store the file)
54 {
55 return accumulatedEntropy;
56 }
57
58
60 {
61 accumulatedEntropy = 0;
62 }
63
64
65 // reports the overhead (summed since last call to resetOverhead/resetSizes)
66 // i.e. interval bounds, frequencies, etc.
68 {
69 return accumulatedOverhead;
70 }
71
73 {
74 accumulatedOverhead = 0;
75 }
76
77
78 // returns the compressed size
79 // which is (in the best case) the size of the encoded, compressed file
80 // without the overhead from storing intervals etc.
82 {
83 return accumulatedCompressed;
84 }
85
87 {
88 accumulatedCompressed = 0 ;
89 }
90
91 // returns the compressed size + overhead, which is (in the best case/in the limit)
92 // the size of the compressed file (including all side information)
94 {
95 return accumulatedCompressed + accumulatedOverhead;
96 }
97
98 // reset everything
100 {
101 accumulatedEntropy = 0;
102 accumulatedOverhead = 0;
103 accumulatedCompressed = 0;
104 accumulatedUncompressed = 0;
105 }
106
107
108 // returns the size needed for uncompressed storage of the data
110 {
111 return accumulatedUncompressed;
112 }
113
115 {
116 accumulatedUncompressed = 0 ;
117 }
118
119
120 // returns the compression factor (=uncompressed size/compressed size)
121 double reportRatio()
122 {
123 if( accumulatedCompressed + accumulatedOverhead > 0 )
124 return accumulatedUncompressed / (accumulatedCompressed + accumulatedOverhead );
125
126 return -1;
127 }
128
129
130 // TODO: has to be faster for adaptive meshes!
131 void setup( Grid const& grid )
132 {
133 boost::timer::cpu_timer timer;
134 if( ps != NULL ) delete ps;
136// ps = new Lossy_Detail::ProlongationStack<Grid>(grid); // for debugging, should use ps = new Prolongation<Grid> instead!
137
138// std::cout << "ps: " << (double)(timer.elapsed().user)/1e9 << "s\n";
139
140 timer.start();
141 maxLevel = grid.maxLevel();
142
143 typename Grid::GlobalIdSet const& idSet = grid.globalIdSet();
144
145
146 levelInfo.clear();
147// IdType ID ;
148 for( int level = maxLevel; level >= 0; --level )
149 {
150 VertexLevelIterator lEnd = grid.levelGridView( level ).template end<dim>();
151 for( VertexLevelIterator it = grid.levelGridView( level ).template begin<dim>(); it != lEnd; ++it )
152 {
153// ID = idSet.id( *it ) ;
154 levelInfo[/*ID*/idSet.id( *it )] = level ;
155 }
156 }
157// std::cout << "levelInfo: " << (double)(timer.elapsed().user)/1e9 << "s\n";
158
159 }
160
161
162
168
172
177 void encode( Grid const& grid, CoeffVector const& sol, std::string fn, double aTol_ = 0 )
178 {
179 boost::timer::cpu_timer timer;
180 boost::timer::cpu_timer fooTimer;
181
182
183 double aTolSave = aTol ;
184 if( aTol_ > 0 ) aTol = aTol_ ;
185
186 double overhead = 0, entropy = 0, compressed = 0 ;
187 size_t nNodes = grid.size(dim);
188
189 std::ofstream out( fn.c_str(), std::ios::binary ) ;
190
191// int maxLevel = grid.maxLevel() ;
192 typename Grid::GlobalIdSet const& idSet = grid.globalIdSet();
193 typename Grid::LeafGridView::IndexSet const& leafIndexSet = grid.leafIndexSet();
194
195 // for adaptivity in space, prolongation matrices have to be recomputed for the current grid
196 // --> call setup(grid) first!
197// setup(grid);
198
199// std::cout << "init: " << (double)(timer.elapsed().user)/1e9 << "s\n";
200 timer.start();
201
202 std::vector</*ULong*/long int> indices ;
203 std::vector<double> predictionError ;
204
205 VertexLevelIterator itEnd = grid.levelGridView(coarseLevel).template end<dim>();
206 for( VertexLevelIterator it = grid.levelGridView( coarseLevel ).template begin<dim>() ; it != itEnd; ++it )
207 {
208 predictionError.push_back( -sol[leafIndexSet.index(*it)] ) ;
209 }
210
211// double ub, lb;
212// ub = *std::max_element( predictionError.begin(), predictionError.end(), Lossy_Detail::abscompare ) ;
213// if( ub < 0 ) { lb = ub ; ub = -lb ; }
214// else { lb = -ub ; }
215
216// // out.write(reinterpret_cast<char*>( &lb ), sizeof(double) ) ;
217// out.write(reinterpret_cast<char*>( &ub ), sizeof(double) ) ;
218// overhead += sizeof(double);
219
220 quantize( predictionError, indices/*, lb, ub*/ ) ;
221 reconstruct( predictionError, indices/*, lb, ub*/ ) ;
222
223// std::cout << "\nreconstructed prediction error for level 0:\n";
224// for( size_t i = 0; i < predictionError.size(); i++ )
225// std::cout << predictionError[i] << "\n";
226// std::cout << "\n";
227
228
229// CoeffVector reconstruction(ps->prolStack[coarseLevel].M()) ;
230 CoeffVector reconstruction(grid.size(coarseLevel, dim) ) ;
231
232 // assign reconstructed coarse grid values
233 size_t nEntries = sol.size();
234 std::vector<long int/*ULong*/> intervalIndicesTmp( nEntries );
235 long int minIndex = 0;
236
237 size_t vertexCount = 0 ;
238// itEnd = grid.levelGridView(coarseLevel).template end<dim>();
239 for( VertexLevelIterator it = grid.levelGridView(coarseLevel).template begin<dim>(); it != itEnd; ++it)
240 {
241 reconstruction[vertexCount] = -predictionError[vertexCount] ;
242
243 long int tmpIndex = indices[vertexCount];
244 intervalIndicesTmp[leafIndexSet.index(*it)] = tmpIndex;//indices[vertexCount];
245 if( tmpIndex < minIndex ) minIndex = tmpIndex;
246
247 vertexCount++ ;
248 }
249
250// std::cout << "coarse grid: " << (double)(timer.elapsed().user)/1e9 << "s\n";
251 timer.start();
252
253 CoeffVector prediction;
254
255 double quantTime = 0, mvTime = 0, prepTime = 0, predErrTime = 0;
256
257 for( int l = coarseLevel ; l < maxLevel ; l++ )
258 {
259// CoeffVector prediction( ps->prolStack[l].N() );
260// CoeffVector prediction( grid.size(l+1,dim) );
261// ps->prolStack[l].mv( reconstruction, prediction ) ;
262
263 fooTimer.start();
264 ps->mv(l, reconstruction, prediction) ;
265
266// std::cout << "prediction vs solution vs error for level " << l+1 << "\n";
267// for( VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
268// {
269// std::cout << prediction[grid.levelView(l+1).indexSet().index(*it)] << "\t\t" << sol[leafIndexSet.index(*it)]
270// << "\t\t" << prediction[grid.levelView(l+1).indexSet().index(*it)] - sol[leafIndexSet.index(*it)]<< "\n";
271// }
272// std::cout << "\n";
273
274// prediction.resize( ps->prolStack[l].N() );
275// prediction = 0;
276// ps->prolStack[l].mv( reconstruction, prediction ) ;
277
278 mvTime += (double)(fooTimer.elapsed().user)/1e9 ;
279
280 std::vector<std::vector<size_t> > vertexInfo( prediction.size(), std::vector<size_t>(3) );
281
282 fooTimer.start();
283 predictionError.clear();
284 predictionError.reserve( prediction.size() ); // avoid reallocations
285
286 vertexCount = 0 ;
287 typename Grid::LevelGridView::IndexSet const& levelIndexSet = grid.levelView(l+1).indexSet();
288
289// ub = 0 ;
290// double tmpval;
291 itEnd = grid.levelGridView(l+1).template end<dim>();
292 for( VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
293 {
294 unsigned char vertexLevel = levelInfo[idSet.id( *it )] ;
295 if( vertexLevel == l+1 )
296 {
297 // tmpval = prediction[levelIndexSet.index(*it)] - sol[leafIndexSet.index(*it)] ;
298 // if( fabs(tmpval) > fabs(ub) ) ub = tmpval;
299 predictionError.push_back(/*tmpval*/prediction[levelIndexSet.index(*it)] - sol[leafIndexSet.index(*it)] );
300 }
301 vertexInfo[vertexCount][0] = levelIndexSet.index(*it) ;
302 vertexInfo[vertexCount][1] = leafIndexSet.index(*it) ;
303 vertexInfo[vertexCount][2] = vertexLevel ;
304
305 vertexCount++;
306 }
307
308
309// // symmetric quantization
310// ub = *std::max_element( predictionError.begin(), predictionError.end(), Lossy_Detail::abscompare ) ;
311
312 predErrTime += (double)(fooTimer.elapsed().user)/1e9;
313
314// if( ub < 0 ) { lb = ub ; ub = -lb ; }
315// else { lb = -ub ; }
316// overhead += sizeof(double);
317
318// // out.write(reinterpret_cast<char*>( &lb ), sizeof(double) ) ;
319// out.write(reinterpret_cast<char*>( &ub ), sizeof(double) ) ;
320
321 fooTimer.start();
322 quantize( predictionError, indices/*, lb, ub*/) ;
323 reconstruct( predictionError, indices/*, lb, ub*/ ) ;
324 quantTime += (double)(fooTimer.elapsed().user)/1e9;
325
326 fooTimer.start();
327 if( (l+1) < maxLevel )
328 {
329 // prepare prediction on next level -- use reconstructed values
330 reconstruction.resize( prediction.size() );
331 vertexCount = 0 ;
332// itEnd = grid.levelGridView(l+1).template end<dim>();
333// for( VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd ; ++it)
334 for( size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
335 {
336 // correction only for the nodes on level l+1
337 unsigned char vertexLevel = vertexInfo[ii][2]; //levelInfo[idSet.id( *it )] ;
338 IndexType levelIndex = vertexInfo[ii][0];//levelIndexSet.index(*it) ;
339 if( vertexLevel < l+1 )
340 {
341 reconstruction[levelIndex] = prediction[levelIndex];
342 }
343 else
344 {
345 long int/*ULong*/ tmpIndex = indices[vertexCount];
346 intervalIndicesTmp[/*leafIndexSet.index(*it)*/vertexInfo[ii][1] ] = tmpIndex;//indices[vertexCount];
347 if( tmpIndex < minIndex ) minIndex = tmpIndex;
348
349 reconstruction[levelIndex] = prediction[levelIndex]-predictionError[vertexCount] ;
350
351 vertexCount++;
352 }
353 }
354 }
355 else
356 {
357 vertexCount = 0 ;
358// itEnd = grid.levelGridView(l+1).template end<dim>();
359// for( VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
360 for( size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
361 {
362 unsigned char vertexLevel = vertexInfo[ii][2];//levelInfo[idSet.id( *it )] ;
363 if( vertexLevel < l+1 ) continue;
364
365 long int/*ULong*/ tmpIndex = indices[vertexCount];
366 intervalIndicesTmp[/*leafIndexSet.index(*it)*/vertexInfo[ii][1] ] = tmpIndex;//indices[vertexCount];
367 if( tmpIndex < minIndex ) minIndex = tmpIndex;
368 vertexCount++ ;
369 }
370 }
371 prepTime += (double)(fooTimer.elapsed().user)/1e9;
372 }
373
374// std::cout << "prediction: " << (double)(timer.elapsed().user)/1e9 << "s\n";
375// std::cout << " mv: " << mvTime << "s\n"
376// << " predErr: " << predErrTime << "s\n"
377// << " quant: " << quantTime << "s\n"
378// << " prep: " << prepTime << "s\n";
379
380 // In order to maximize efficiency of the range coder, precalculate the
381 // frequency of each interval, befor encoding.
382
383
384 //TODO: check size/data type for nnz, maxIndex etc!
385
386 timer.start();
387 fooTimer.start();
388// long int minIndex = *std::min_element( intervalIndicesTmp.begin(), intervalIndicesTmp.end() ) ;
389
390 std::vector<ULong> intervalIndices( nEntries ) ;
391 for( size_t i = 0; i < nEntries; i++ ) intervalIndices[i] = intervalIndicesTmp[i] - minIndex ;
392
393 out.write(reinterpret_cast<char*>( &minIndex ), sizeof(long int) ) ;
394 overhead += sizeof(long int);
395// std::cout << " minIndex: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
396
397 fooTimer.start();
398
399 std::map<unsigned long, unsigned long> freqMap;
400 unsigned int nnz = 0;
401 for( size_t i = 0 ; i < nEntries ; i++ )
402 {
403 if( freqMap.find(intervalIndices[i] ) != freqMap.end() ) // already there, increment
404 {
405 freqMap[intervalIndices[i]]++ ;
406 }
407 else // new nonzero, add to map
408 {
409 nnz++;
410 freqMap[intervalIndices[i]] = 1;
411 }
412 }
413
414// out.write(reinterpret_cast<char*>( &nnz ), sizeof(unsigned int) ) ;
415// overhead += sizeof(unsigned int);
416
417 std::vector<unsigned long> frequenciesForRangeCoder(nnz);
418// std::cout << " freq: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
419
420 fooTimer.start();
421
422 std::map<unsigned long, unsigned long> dictMap;
423 unsigned long curNo = 0 ;
424
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 )
428 {
429 unsigned long lv = mapIt->first;
430 frequenciesForRangeCoder[curNo] = mapIt->second;
431 dictMap.insert( dictMap.begin(), std::pair<unsigned long, unsigned long>(lv, curNo) ) ;
432 curNo++;
433// out.write(reinterpret_cast<char*>( &lv ), sizeof(unsigned long) ) ;
434// overhead += sizeof(unsigned long);
435 }
436// std::cout << " dict: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
437
438
439 fooTimer.start();
440
441 for( unsigned int i = 0 ; i < nnz ; i++ )
442 {
443// out.write(reinterpret_cast<char*>( &frequenciesForRangeCoder[i] ), sizeof(unsigned long) ) ;
444// overhead += sizeof(unsigned long);
445
446 double tmp = -ld(frequenciesForRangeCoder[i]/((double)nNodes)) *
447 frequenciesForRangeCoder[i]/((double)nNodes);
448 entropy += tmp;
449 compressed += tmp;
450 }
451 accumulatedCompressed += compressed/8*nNodes;
452 compressed = 0;
453
454// std::cout << " write freq: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
455
456// std::cout << "preparation for rc: " << (double)(timer.elapsed().user)/1e9 << "s\n";
457 timer.start();
458
459 std::cout << "\nEncoding " << intervalIndices.size() << " values, "
460 << frequenciesForRangeCoder.size() << " different values\n" ;
461 std::cout << "Overhead: " << overhead << " B\n";
462//
463// Alphabet<unsigned long> alphabet( frequenciesForRangeCoder.begin(), frequenciesForRangeCoder.end() ) ;
464// RangeEncoder<unsigned long> encoder( out ) ;
465//
466// for( size_t i = 0 ; i < nEntries; i++ )
467// {
468// encodeSymbol( encoder, alphabet, dictMap.find(intervalIndices[i])->second/* dictMap[intervalIndices[i]]*/ );
469// }
470// // std::cout << "entropy coding: " << (double)(timer.elapsed().user)/1e9 << "s\n";
471
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);
475
476 size_t symbolCounter = 0 ;
477 std::vector<unsigned long> count(maxIntervals, 1) ;
478 Alphabet<unsigned long> alphabet( count.begin(), count.end() ) ;
479 RangeEncoder<unsigned long> encoder(out) ;
480
481 for( size_t i = 0 ; i < nEntries; i++ )
482 {
483 encodeSymbol( encoder, alphabet, intervalIndices[i] );
484 ++count[intervalIndices[i]];
485 ++symbolCounter ;
486 if (symbolCounter>0.1*alphabet.size())
487 {
488 alphabet.update(count.begin(),count.end());
489 symbolCounter=0;
490 }
491 }
492
493 accumulatedEntropy += entropy/8 ;
494 accumulatedOverhead += overhead;
495 accumulatedUncompressed += 8*nNodes;
496
497 aTol = aTolSave;
498 }
499
500
501
502 void flush(Grid const& /*grid*/)
503 {}
504
505
511 void decode( Grid const& gridState, CoeffVector& sol, std::string fn, double aTol_ = 0 )
512 {
513 double aTolSave = aTol ;
514 if( aTol_ > 0 ) aTol = aTol_ ;
515
516 std::ifstream in( fn.c_str(), std::ios::binary ) ;
517
518 // for adaptivity in space, prolongation matrices have to be recomputed for the current grid
519 // --> call setup(gridState)
520// setup(gridState);
521
522 // prepare prediction
523 IndexSet const& indexSet = gridState.leafIndexSet();
524
525 std::vector<double> values ;
526 std::vector<long int> intervalIndices( gridState.size(dim) );
527
528 // read lb, ub from file
529// std::vector<double> lb(maxLevel-coarseLevel+1), ub(maxLevel-coarseLevel+1) ;
530// for( int i = 0 ; i <= maxLevel-coarseLevel ; i++ )
531// {
532// // in.read( reinterpret_cast<char*>( &lb[i] ), sizeof(double) ) ;
533// in.read( reinterpret_cast<char*>( &ub[i] ), sizeof(double) ) ;
534// lb[i] = -ub[i];
535// }
536
537 // read in indices from file
538 long int minIndex ;
539 in.read(reinterpret_cast<char*>( &minIndex ), sizeof(long int) ) ;
540
541// unsigned int nnz ;
542// in.read(reinterpret_cast<char*>( &nnz ), sizeof(unsigned int) ) ; // # non-empty intervals
543// std::vector<long int> dictionary( nnz ) ;
544// for( int i = 0 ; i < nnz ; i++ )
545// {
546// in.read(reinterpret_cast<char*>( &dictionary[i] ), sizeof(unsigned long) ) ; // existing intervals (dictionary)
547// }
548// std::vector</*ULong*/unsigned long> frequencies( nnz, 0 ) ;
549// for( int i = 0 ; i < nnz ; i++ )
550// {
551// in.read(reinterpret_cast<char*>( &frequencies[i] ), sizeof(unsigned long) ) ; // frequencies
552// }
553//
554// Alphabet</*ULong*/unsigned long> alphabet( frequencies.begin(), frequencies.end() ) ;
555// try
556// {
557// RangeDecoder</*ULong*/unsigned long> decoder( in ) ;
558// for( int i = 0 ; i < intervalIndices.size() ; i++ )
559// {
560// /*ULong*/unsigned long s = decodeSymbol(decoder,alphabet) ;
561// intervalIndices[i] = dictionary[s] + minIndex ;
562// }
563// }
564// catch( std::ios_base::failure& e )
565// {
566// if (in.rdstate() & std::ifstream::eofbit)
567// {
568// std::cout << "EOF reached.\n";
569// }
570// else
571// {
572// std::cout << " Decoding error\n" << e.what() << "\n";
573// }
574// }
575 size_t symbolCounter = 0 ;
576 size_t maxIntervals;
577 in.read(reinterpret_cast<char*>( &maxIntervals ), sizeof(size_t) ) ;
578
579 std::vector<unsigned long> symcount(maxIntervals, 1) ;
580 Alphabet<unsigned long> alphabet( symcount.begin(), symcount.end() ) ;
581 try
582 {
583 RangeDecoder<unsigned long> decoder( in ) ;
584 for( size_t i = 0 ; i < intervalIndices.size() ; i++ )
585 {
586 unsigned long s = decodeSymbol(decoder,alphabet) ;
587 intervalIndices[i] = s + minIndex ;
588 ++symcount[s];
589 ++symbolCounter ;
590 if (symbolCounter>0.1*alphabet.size())
591 {
592 alphabet.update(symcount.begin(),symcount.end());
593 symbolCounter=0;
594 }
595 }
596 }
597 catch( std::ios_base::failure& e )
598 {
599 if (in.rdstate() & std::ifstream::eofbit)
600 {
601 std::cout << "EOF reached.\n";
602 }
603 else
604 {
605 std::cout << " Decoding error\n" << e.what() << "\n";
606 }
607 }
608
609 in.close() ;
610
611 VertexLevelIterator itEnd = gridState.levelGridView(coarseLevel).template end<dim>();
612// std::vector<std::vector<ULong> > allIndices( maxLevel+1-coarseLevel ) ;
613
614// for( int l = coarseLevel ; l <= maxLevel ; l++ )
615// {
616// itEnd = gridState.levelGridView(l).template end<dim>();
617// for ( VertexLevelIterator it = gridState.levelGridView(l).template begin<dim>(); it!= itEnd ; ++it)
618// {
619// allIndices[l-coarseLevel].push_back(intervalIndices[indexSet.index(*it)] ) ;
620// }
621// }
622
623 // start reconstruction
624 int vertexCount = 0;
625// reconstruct( values, allIndices[0], lb[0], ub[0]);
626
627// CoeffVector reconstruction( ps->prolStack[coarseLevel].M() );
628 CoeffVector reconstruction( gridState.size(coarseLevel, dim) );
629 sol.resize( gridState.size(dim) );
630
631// itEnd = gridState.levelGridView(coarseLevel).template end<dim>();
632
633// typename Grid::LevelGridView::IndexSet const& coarseIndexSet = gridState.levelIndexSet(coarseLevel);
634
635
636 for( VertexLevelIterator it = gridState.levelGridView( coarseLevel ).template begin<dim>(); it != itEnd; ++it)
637 {
638 double recVal = reconstruct( intervalIndices[indexSet.index(*it)]/*, lb[0], ub[0]*/);
639 reconstruction[gridState.levelView(coarseLevel).indexSet().index(*it)] = -recVal ;
640
641// reconstruction[/*coarseIndexSet*/gridState.levelView(coarseLevel).indexSet().index(*it)] = -values[vertexCount] ;
642
643 // store predicted values for the coarse grid in solution vector
644 sol[ indexSet.index(*it) ] = -recVal ;
645// sol[ indexSet.index(*it) ] = -values[vertexCount] ;
646
647 vertexCount++ ;
648 }
649
650
651 CoeffVector prediction;
652 // perform prediction and correction
653 for( int l = coarseLevel ; l < maxLevel ; l++ )
654 {
655// prediction.resize( ps->prolStack[l].N(), false );
656// prediction = 0;
657// CoeffVector prediction( gridState.size(l+1,dim) );
658// ps->prolStack[l].mv( reconstruction, prediction ) ;
659 ps->mv( l, reconstruction, prediction ) ;
660
661// reconstruct( values, allIndices[l-coarseLevel+1], lb[l+1-coarseLevel], ub[l+1-coarseLevel] );
662 reconstruction.resize( prediction.size() );
663
664// typename Grid::LevelGridView::IndexSet const& levelIndexSet = gridState.levelIndexSet(l+1);
665 typename Grid::LevelGridView::IndexSet const& levelIndexSet = gridState.levelView(l+1).indexSet();
666
667 vertexCount = 0 ;
668
669 itEnd = gridState.levelGridView(l+1).template end<dim>();
670 for( VertexLevelIterator it = gridState.levelGridView(l+1).template begin<dim>(); it != itEnd ; ++it)
671 {
672 IndexType levelIndex = /*gridState.levelView(l+1).indexSet()*/levelIndexSet.index(*it);
673 if(levelInfo[gridState.globalIdSet().id(*it)] == l+1) //correct only vertices on level l+1
674 {
675 reconstruction[levelIndex] = prediction[levelIndex] - //values[vertexCount] ;
676 reconstruct( intervalIndices[indexSet.index(*it)]/*, lb[l+1-coarseLevel], ub[l+1-coarseLevel]*/);
677 }
678 else
679 {
680 reconstruction[levelIndex] = prediction[levelIndex];
681 }
682 sol[indexSet.index(*it)] = reconstruction[ levelIndex] ;
683 vertexCount++ ;
684 }
685 }
686 aTol = aTolSave ;
687 }
688
689
693
694 void encode( Grid const& grid, CoeffVector const& v, CoeffVector const& w,
695 std::vector<std::vector<long int> > & intervalIndicesTmp,
696 /*std::string fn_v, std::string fn_w, */ double aTol_ = 0 )
697 {
698 boost::timer::cpu_timer timer;
699 boost::timer::cpu_timer fooTimer;
700 double aTolSave = aTol ;
701 if( aTol_ > 0 ) aTol = aTol_ ;
702
703 double overhead = 0, entropy = 0, compressed = 0 ;
704 size_t nNodes = grid.size(dim);
705
706
707// std::ofstream out[2];
708// out[0].open(fn_v.c_str(), std::ios::binary ) ;
709// out[1].open(fn_w.c_str(), std::ios::binary ) ;
710
711 typename Grid::GlobalIdSet const& idSet = grid.globalIdSet();
712 typename Grid::LeafGridView::IndexSet const& leafIndexSet = grid.leafIndexSet();
713
714 timer.start();
715
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 ;
720
721 VertexLevelIterator itEnd = grid.levelGridView(coarseLevel).template end<dim>();
722 for( VertexLevelIterator it = grid.levelGridView( coarseLevel ).template begin<dim>() ; it != itEnd; ++it )
723 {
724 predictionError_v.push_back( -v[leafIndexSet.index(*it)] ) ;
725 predictionError_w.push_back( -w[leafIndexSet.index(*it)] ) ;
726 }
727
728 quantize( predictionError_v, indices_v) ;
729 reconstruct( predictionError_v, indices_v) ;
730
731 quantize( predictionError_w, indices_w) ;
732 reconstruct( predictionError_w, indices_w) ;
733
734 CoeffVector reconstruction_v( grid.size(coarseLevel, dim) ) ;
735 CoeffVector reconstruction_w( reconstruction_v.size() ) ;
736
737 // assign reconstructed coarse grid values
738 size_t nEntries = v.size();
739// std::vector<std::vector<long int> > intervalIndicesTmp( 2, std::vector<long int>(nEntries) );
740 intervalIndicesTmp.resize( 2, std::vector<long int>(nEntries) );
741// long int minIndex[2] = {0,0};
742
743 size_t vertexCount = 0 ;
744 for( VertexLevelIterator it = grid.levelGridView(coarseLevel).template begin<dim>(); it != itEnd; ++it)
745 {
746 reconstruction_v[vertexCount] = -predictionError_v[vertexCount] ;
747 reconstruction_w[vertexCount] = -predictionError_w[vertexCount] ;
748
749
750 long int tmpIndex = indices_v[vertexCount];
751 intervalIndicesTmp[0][leafIndexSet.index(*it)] = tmpIndex;
752// if( tmpIndex < minIndex[0] ) minIndex[0] = tmpIndex;
753
754 tmpIndex = indices_w[vertexCount];
755 intervalIndicesTmp[1][leafIndexSet.index(*it)] = tmpIndex;
756// if( tmpIndex < minIndex[1] ) minIndex[1] = tmpIndex;
757
758 vertexCount++ ;
759 }
760
761 timer.start();
762
763 CoeffVector prediction_v, prediction_w;
764
765 double quantTime = 0, mvTime = 0, prepTime = 0, predErrTime = 0, boundsTime = 0, allocTime = 0,
766 vertexInfoTime = 0, levelIndexTime = 0, leafIndexTime = 0, levelInfoTime = 0;
767
768 for( int l = coarseLevel ; l < maxLevel ; l++ )
769 {
770 fooTimer.start();
771 ps->mv(l, reconstruction_v, prediction_v) ;
772 ps->mv(l, reconstruction_w, prediction_w) ;
773 mvTime += (double)(fooTimer.elapsed().user)/1e9 ;
774
775 fooTimer.start();
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); // avoid reallocations
780 predictionError_w.clear();
781 predictionError_w.reserve( levelSize); // avoid reallocations
782
783 vertexCount = 0 ;
784 typename Grid::LevelGridView::IndexSet const& levelIndexSet = grid.levelView(l+1).indexSet();
785
786 itEnd = grid.levelGridView(l+1).template end<dim>();
787
788 allocTime += (double)(fooTimer.elapsed().user)/1e9;
789 fooTimer.start();
790
791 for( VertexLevelIterator it = grid.levelGridView(l+1).template begin<dim>(); it != itEnd; ++it)
792 {
793 predErrTime += (double)(fooTimer.elapsed().user)/1e9;
794
795 fooTimer.start();
796 vertexInfo[vertexCount][0] = levelIndexSet.index(*it) ;
797 levelIndexTime += (double)(fooTimer.elapsed().user)/1e9;
798
799 fooTimer.start();
800 vertexInfo[vertexCount][2] = levelInfo[idSet.id( *it )] ;
801 levelInfoTime += (double)(fooTimer.elapsed().user)/1e9;
802
803 fooTimer.start();
804 if( vertexInfo[vertexCount][2] == l+1 )
805 {
806 fooTimer.start();
807 auto tmpLeafIxd = leafIndexSet.index(*it) ;
808 vertexInfo[vertexCount][1] = tmpLeafIxd;
809 leafIndexTime += (double)(fooTimer.elapsed().user)/1e9;
810
811 predictionError_v.push_back(prediction_v[vertexInfo[vertexCount][0]] - v[tmpLeafIxd/*vertexInfo[vertexCount][1]*/] );
812 predictionError_w.push_back(prediction_w[vertexInfo[vertexCount][0]] - w[tmpLeafIxd/*vertexInfo[vertexCount][1]*/] );
813 }
814 vertexCount++;
815 }
816 predErrTime += (double)(fooTimer.elapsed().user)/1e9;
817
818 fooTimer.start();
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;
824
825
826 fooTimer.start();
827 if( (l+1) < maxLevel )
828 {
829 // prepare prediction on next level -- use reconstructed values
830 reconstruction_v.resize( prediction_v.size() );
831 reconstruction_w.resize( prediction_w.size() );
832
833 vertexCount = 0 ;
834 for( size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
835 {
836 // correction only for the nodes on level l+1
837 unsigned char vertexLevel = vertexInfo[ii][2];
838 IndexType levelIndex = vertexInfo[ii][0];
839 if( vertexLevel < l+1 )
840 {
841 reconstruction_v[levelIndex] = prediction_v[levelIndex];
842 reconstruction_w[levelIndex] = prediction_w[levelIndex];
843 }
844 else
845 {
846 long int tmpIndex = indices_v[vertexCount];
847 intervalIndicesTmp[0][vertexInfo[ii][1] ] = tmpIndex;
848// if( tmpIndex < minIndex[0] ) minIndex[0] = tmpIndex;
849
850 tmpIndex = indices_w[vertexCount];
851 intervalIndicesTmp[1][vertexInfo[ii][1] ] = tmpIndex;
852// if( tmpIndex < minIndex[1] ) minIndex[1] = tmpIndex;
853
854 reconstruction_v[levelIndex] = prediction_v[levelIndex]-predictionError_v[vertexCount] ;
855 reconstruction_w[levelIndex] = prediction_w[levelIndex]-predictionError_w[vertexCount] ;
856
857 vertexCount++;
858 }
859 }
860 }
861 else
862 {
863 vertexCount = 0 ;
864 for( size_t ii = 0 ; ii < vertexInfo.size(); ii++ )
865 {
866 unsigned char vertexLevel = vertexInfo[ii][2];
867 if( vertexLevel < l+1 ) continue;
868
869 long int/*ULong*/ tmpIndex = indices_v[vertexCount];
870 intervalIndicesTmp[0][vertexInfo[ii][1] ] = tmpIndex;
871// if( tmpIndex < minIndex[0] ) minIndex[0] = tmpIndex;
872
873 tmpIndex = indices_w[vertexCount];
874 intervalIndicesTmp[1][vertexInfo[ii][1] ] = tmpIndex;
875// if( tmpIndex < minIndex[1] ) minIndex[1] = tmpIndex;
876
877 vertexCount++ ;
878 }
879 }
880 prepTime += (double)(fooTimer.elapsed().user)/1e9;
881 }
882
883
884// std::cout << "prediction: " << (double)(timer.elapsed().user)/1e9 << "s\n";
885// std::cout << " mv: " << mvTime << "s\n"
886// << " alloc: " << allocTime << "s\n"
887// << " levelIndex: " << levelIndexTime << "s\n"
888// << " leafIndex: " << leafIndexTime << "s\n"
889// << " levelInfo: " << levelInfoTime << "s\n"
890// // << " vertexInfo: " << vertexInfoTime << "s\n"
891// << " predErr: " << predErrTime << "s\n"
892// << " bounds: " << boundsTime << "s\n"
893// << " quant: " << quantTime << "s\n"
894// << " prep: " << prepTime << "s\n";
895
896 aTol = aTolSave;
897
898 // now build difference to previous indices in integrate()
899 }
900
902 void write(std::vector<std::vector<long int> > const & intervalIndicesTmp,
903 std::string fn_v, std::string fn_w )
904
905 {
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 ) ;
909
910 size_t nFunctions = intervalIndices.size();
911 assert(nFunctions == 2);
912
913 size_t nEntries = intervalIndicesTmp[0].size();
914
915 boost::timer::cpu_timer timer;
916 boost::timer::cpu_timer fooTimer;
917
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() ) ;
921
922 std::vector<ULong> intervalIndices( nEntries ) ;
923
924 // first v, then w
925 for( int function = 0 ; function < 2 ; function++ )
926 {
927
928 for( size_t i = 0; i < nEntries; i++ )
929 intervalIndices[i] = intervalIndicesTmp[function][i] - minIndex[function] ;
930
931 out[function].write(reinterpret_cast<char*>( &minIndex[function] ), sizeof(/*ULong*/long int) ) ;
932 overhead += sizeof(long int);
933// std::cout << " minIndex: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
934
935 fooTimer.start();
936
937 std::map<unsigned long, unsigned long> freqMap;
938 unsigned int nnz = 0;
939 for( size_t i = 0 ; i < nEntries ; i++ )
940 {
941 if( freqMap.find(intervalIndices[i] ) != freqMap.end() ) // already there, increment
942 {
943 freqMap[intervalIndices[i]]++ ;
944 }
945 else // new nonzero, add to map
946 {
947 nnz++;
948 freqMap[intervalIndices[i]] = 1;
949 }
950 }
951
952 out[function].write(reinterpret_cast<char*>( &nnz ), sizeof(unsigned int) ) ;
953 overhead += sizeof(unsigned int);
954// std::cout << "nonzero frequencies: " << nnz << "\n";
955
956 std::vector<unsigned long> frequenciesForRangeCoder(nnz);
957// std::cout << " freq: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
958
959 fooTimer.start();
960
961 std::map<unsigned long, unsigned long> dictMap;
962 unsigned long curNo = 0 ;
963
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 )
967 {
968 unsigned long lv = mapIt->first;
969 frequenciesForRangeCoder[curNo] = mapIt->second;
970 dictMap.insert( dictMap.begin(), std::pair<unsigned long, unsigned long>(lv, curNo) ) ;
971 curNo++;
972 out[function].write(reinterpret_cast<char*>( &lv ), sizeof(unsigned long) ) ;
973 overhead += sizeof(unsigned long);
974 }
975// std::cout << " dict: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
976
977 fooTimer.start();
978 for( unsigned int i = 0 ; i < nnz ; i++ )
979 {
980 out[function].write(reinterpret_cast<char*>( &frequenciesForRangeCoder[i] ), sizeof(unsigned long) ) ;
981 overhead += sizeof(unsigned long);
982
983 double tmp = -ld(frequenciesForRangeCoder[i]/((double)nNodes)) *
984 frequenciesForRangeCoder[i]/((double)nNodes);
985 entropy += tmp;
986 compressed += tmp;/*/8 * frequenciesForRangeCoder[i];*/
987 }
988 accumulatedCompressed += compressed/8*nNodes;
989 compressed = 0;
990// std::cout << " write freq: " << (double)(fooTimer.elapsed().user)/1e9 << "s\n";
991// std::cout << "preparation for rc: " << (double)(timer.elapsed().user)/1e9 << "s\n";
992
993 timer.start();
994
995 Alphabet<unsigned long> alphabet( frequenciesForRangeCoder.begin(), frequenciesForRangeCoder.end() ) ;
996 RangeEncoder<unsigned long> encoder( out[function] ) ;
997
998 for( size_t i = 0 ; i < /*2**/nEntries; i++ )
999 {
1000 encodeSymbol( encoder, alphabet, dictMap.find(intervalIndices[i])->second/*[intervalIndices[i]]*/ );
1001 }
1002
1003// std::cout << "range coder: " << (double)(timer.elapsed().user)/1e9 << "s\n";
1004 } // for( int function ...
1005
1006 accumulatedEntropy += entropy/8 ;
1007 accumulatedOverhead += overhead;
1008 accumulatedUncompressed += 2*8*nNodes;
1009 }
1010
1011
1013 void read( size_t nEntries, // degrees of freedom to read (given by the grid)
1014 std::vector<std::vector<long int> > & intervalIndices,
1015 std::string fn_v, std::string fn_w )
1016 {
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 ) ;
1020
1021 intervalIndices.resize( 2, std::vector<long int>(nEntries) );
1022
1023 long int minIndex[2];
1024 for( int function = 0 ; function < 2 ; function++)
1025 {
1026 // read in indices from file
1027 in[function].read(reinterpret_cast<char*>( &minIndex[function] ), sizeof(long int) ) ;
1028
1029 unsigned int nnz ;
1030 in[function].read(reinterpret_cast<char*>( &nnz ), sizeof(unsigned int) ) ; // # non-empty intervals
1031 std::vector<ULong> dictionary( nnz ) ;
1032 for( int i = 0 ; i < nnz ; i++ )
1033 {
1034 in[function].read(reinterpret_cast<char*>( &dictionary[i] ), sizeof(unsigned long) ) ;
1035 }
1036 std::vector<ULong> frequencies( nnz, 0 ) ;
1037 for( int i = 0 ; i < nnz ; i++ )
1038 {
1039 in[function].read(reinterpret_cast<char*>( &frequencies[i] ), sizeof(/*ULong*/unsigned long) ) ; // frequencies
1040 }
1041
1042 Alphabet<unsigned long> alphabet( frequencies.begin(), frequencies.end() ) ;
1043
1044 try
1045 {
1046 RangeDecoder<unsigned long> decoder( in[function] ) ;
1047 for( int i = 0 ; i < nEntries ; i++ )
1048 {
1049 unsigned long s = decodeSymbol(decoder,alphabet) ;
1050 intervalIndices[function][i] = dictionary[s] + minIndex[function] ;
1051 }
1052 }
1053 catch( std::ios_base::failure& e )
1054 {
1055 if (in[function].rdstate() & std::ifstream::eofbit)
1056 {
1057 std::cout << "EOF reached.\n";
1058 }
1059 else
1060 {
1061 std::cout << " Decoding error\n" << e.what() << "\n";
1062 }
1063 }
1064 in[function].close() ;
1065 } // for( int function = ...
1066 }
1067
1070
1071 void decode( Grid const& gridState, CoeffVector& v, CoeffVector& w,
1072 std::vector<std::vector<long int> const& intervalIndices,
1073 /*std::string fn_v, std::string fn_w,*/ double aTol_ = 0)
1074 {
1075 double aTolSave = aTol ;
1076 if( aTol_ > 0 ) aTol = aTol_ ;
1077
1078 assert( intervalIndices.size() == 2 );
1079 size_t nEntries = intervalIndices[0].size();
1080 assert( nEntries = gridState.size(dim);
1081
1082 // prepare prediction
1083 IndexSet const& indexSet = gridState.leafIndexSet();
1084 std::vector<double> values_v, values_w ;
1085
1086 VertexLevelIterator itEnd = gridState.levelGridView(coarseLevel).template end<dim>();
1087
1088 // start reconstruction
1089 int vertexCount = 0;
1090
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) );
1095
1096
1097 for( VertexLevelIterator it = gridState.levelGridView( coarseLevel ).template begin<dim>(); it != itEnd; ++it)
1098 {
1099 double recVal = reconstruct( intervalIndices[0][indexSet.index(*it)]/*, lb[0][0], ub[0][0]*/);
1100 reconstruction_v[gridState.levelView(coarseLevel).indexSet().index(*it)] = -recVal ;
1101 v[ indexSet.index(*it) ] = -recVal ;
1102
1103 recVal = reconstruct( intervalIndices[1][indexSet.index(*it)]/*, lb[1][0], ub[1][0]*/);
1104 reconstruction_w[gridState.levelView(coarseLevel).indexSet().index(*it)] = -recVal ;
1105 w[ indexSet.index(*it) ] = -recVal ;
1106
1107 vertexCount++ ;
1108 }
1109
1110
1111 CoeffVector prediction_v, prediction_w;
1112 // perform prediction and correction
1113 for( int l = coarseLevel ; l < maxLevel ; l++ )
1114 {
1115 ps->mv( l, reconstruction_v, prediction_v ) ;
1116 ps->mv( l, reconstruction_w, prediction_w ) ;
1117
1118 reconstruction_v.resize( prediction_v.size() );
1119 reconstruction_w.resize( prediction_w.size() );
1120
1121 // typename Grid::LevelGridView::IndexSet const& levelIndexSet = gridState.levelIndexSet(l+1);
1122 typename Grid::LevelGridView::IndexSet const& levelIndexSet = gridState.levelView(l+1).indexSet();
1123
1124 vertexCount = 0 ;
1125
1126 itEnd = gridState.levelGridView(l+1).template end<dim>();
1127 for( VertexLevelIterator it = gridState.levelGridView(l+1).template begin<dim>(); it != itEnd ; ++it)
1128 {
1129 IndexType levelIndex = levelIndexSet.index(*it);
1130 if(levelInfo[gridState.globalIdSet().id(*it)] == l+1) //correct only vertices on level l+1
1131 {
1132 reconstruction_v[levelIndex] = prediction_v[levelIndex] -
1133 reconstruct( intervalIndices[0][indexSet.index(*it)]/*, lb[0][l+1-coarseLevel], ub[0][l+1-coarseLevel]*/);
1134 reconstruction_w[levelIndex] = prediction_w[levelIndex] -
1135 reconstruct( intervalIndices[1][indexSet.index(*it)]/*, lb[1][l+1-coarseLevel], ub[1][l+1-coarseLevel]*/);
1136
1137 }
1138 else
1139 {
1140 reconstruction_v[levelIndex] = prediction_v[levelIndex];
1141 reconstruction_w[levelIndex] = prediction_w[levelIndex];
1142 }
1143 v[indexSet.index(*it)] = reconstruction_v[ levelIndex] ;
1144 w[indexSet.index(*it)] = reconstruction_w[ levelIndex] ;
1145 vertexCount++ ;
1146 }
1147 }
1148 aTol = aTolSave ;
1149 }
1150
1151
1152
1153private:
1154 LossyStorage( LossyStorage const& );
1155 LossyStorage& operator=( LossyStorage const& ) ;
1156
1158 void quantize( std::vector<double> const& values, std::vector</*ULong*/long int>& indices, double lb, double ub )
1159 {
1160 double range = ub-lb;
1161 long int nIntervals = (long int) ceil( range / aTol / 2.0 ) ;
1162 if( nIntervals % 2 == 0 ) nIntervals += 1 ;
1163//
1164// // std::cout << "[" << lb << ", " << ub << "] -> " << nIntervals << " intervals, " << values.size() << "nodes\n";
1165//
1166 indices.clear() ;
1167 indices.resize( values.size(), 0 ) ;
1168//
1169// // handle range == 0 !
1170 if( range > 0 )
1171 {
1172 for( size_t i = 0 ; i < values.size() ; i++ )
1173 {
1174// indices[i] = /*(ULong)*/ floor( values[i] / (2*aTol) + 0.5 ) ; // without lb/ub
1175 indices[i] = ((values[i] - lb) * nIntervals / range ) + 1 ;
1176 }
1177 }
1178 }
1179
1181 void quantize( std::vector<double> const& values, std::vector</*ULong*/long int>& indices)
1182 {
1183 indices.clear() ;
1184 indices.resize( values.size(), 0 ) ;
1185
1186 for( size_t i = 0 ; i < values.size() ; i++ )
1187 {
1188 indices[i] = /*(ULong)*/ floor( values[i] / (2*aTol) + 0.5 ) ; // without lb/ub
1189 }
1190 }
1191
1193 void reconstruct( std::vector<double>& values, std::vector</*ULong*/long int> const& indices, double lb, double ub )
1194 {
1195 double range = ub-lb;
1196 long int nIntervals = (long int) ceil( range / aTol / 2.0 ) ;
1197 if( nIntervals % 2 == 0 ) nIntervals += 1 ;
1198
1199 double delta;
1200 if( nIntervals <= 0 ) delta = 0 ;
1201 else delta = range / nIntervals ;
1202
1203 values.clear() ;
1204 values.resize( indices.size() ) ;
1205 for( size_t i = 0 ; i < indices.size() ; i++ )
1206 {
1207// values[i] = indices[i] * 2* aTol ;
1208 values[i] = lb + (/*2**/indices[i]-0.5/*1*/) * delta/*/2 */;
1209 }
1210 }
1211
1213 void reconstruct( std::vector<double>& values, std::vector</*ULong*/long int> const& indices)
1214 {
1215 values.clear() ;
1216 values.resize( indices.size() ) ;
1217 for( size_t i = 0 ; i < indices.size() ; i++ )
1218 {
1219 values[i] = indices[i] * 2* aTol ;
1220 }
1221 }
1222
1224 double reconstruct( /*ULong*/long int const& index, double lb, double ub)
1225 {
1226 double range = ub-lb;
1227 long int nIntervals = (long int) ceil( range / aTol / 2.0 ) ;
1228 if( nIntervals % 2 == 0 ) nIntervals += 1 ;
1229
1230 double delta;
1231 if( nIntervals <= 0 ) delta = 0 ;
1232 else delta = range / nIntervals ;
1233
1234 return lb + (index-0.5) * delta;
1235// return index*2*aTol;
1236 }
1237
1239 double reconstruct( /*ULong*/long int const& index )
1240 {
1241 return index*2*aTol;
1242 }
1243
1245 double ld( double val ) { return log(val)/log(2.0) ; }
1246
1247// Lossy_Detail::ProlongationStack<Grid> * ps; // for debugging
1249 std::map<IdType, unsigned char> levelInfo ;
1250 int coarseLevel, maxLevel ;
1251 double aTol ;
1252 double accumulatedEntropy, accumulatedOverhead, accumulatedUncompressed, accumulatedCompressed;
1253} ;
1254
1255#endif
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...
static const int dim
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
void decode(Grid const &gridState, CoeffVector &v, CoeffVector &w, std::vector< std::vector< long int > const &intervalIndices, double aTol_=0)
unsigned long int ULong
void flush(Grid const &)
void encode(Grid const &grid, CoeffVector const &sol, std::string fn, double aTol_=0)
Grid::Traits::GlobalIdSet::IdType IdType
Grid::LeafGridView::IndexSet::IndexType LeafIndexType
Grid::LevelGridView::IndexSet::IndexType IndexType
LossyStorage(int coarseLevel_, double aTol_)
void read(size_t nEntries, std::vector< std::vector< long int > > &intervalIndices, std::string fn_v, std::string fn_w)
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)
Grid::template Codim< dim >::LeafIndexSet IndexSet
Entropy coding with range decoder.
Entropy coding with range encoder.
double ld(double val)
Range coder for fast entropy coding.