LoAdSG
shift.h
1// ------------------------------------------------------------
2//
3// shift.h
4//
5// ------------------------------------------------------------
6#ifndef SHIFT_SG_H
7#define SHIFT_SG_H
8
9
10#include "../myAssert.h"
11#include "extempAlg.h"
12#include "../sgrid/sparseGrid.h"
13#include "../indices/index.h"
14#include "vector.h"
15
16
17
18
19//todo: welche functions inline?
20
21
22
24// 1. class ShiftExpr
25// 2. class ShiftOperator
26//
28
29
30
31
32
33
35// 1.) class ShiftExpr
37
47template <class A>
48class ShiftExpr : public ExprSparseG<ShiftExpr<A> >{
49friend VectorSparseG;
50
51
52 const A& a_;
53 int d_; //>>> Dimension
54 Direction s_; //>>> Indicates wether to shift left or right
55 int level_;
56 bool hier;
57
58public:
59 ShiftExpr(const A& a, int d, Direction s):a_(a),d_(d), s_(s){
60 hier = true;
61 }
62 ShiftExpr(const A& a, int d, Direction s, int level):a_(a),d_(d), s_(s), level_(level){
63 hier = false;
64 }
65
66
67 inline double getValue(int i, const IndexDimension& K)const;
68
69
70 ExpressionDescription getDescription() const { return ExpressionDescription(true); }
71 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); }
72};
73
74
75
76
77
78
79template<typename A>
80inline double ShiftExpr<A>::getValue(int i, const IndexDimension& K)const{
81
82 if(hier == true ){
83 // do hierarchical shift
84
85 AdaptiveSparseGrid_Base* sparseGrid = a_.getSparseGrid();
86 //unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
87
88
89 dataInteger* secondTable = sparseGrid->getSecondTable();
90 dataInteger* primeTable = sparseGrid->getPrimeTable();
91
92 if(secondTable[i]!=0) {
93 IndexDimension I = K;
94 bool nonext = true;
95
96 //if(I.isNotAtBoundary() == false) {
97 //return 0;
98 //return a_.getDataTableVector(i);
99 //}
100 IndexDimension II;
101 unsigned long kk;
102 while(nonext){
103 if (s_ == Left) {
104 if (sparseGrid->occupied(kk, I.nextLeft(d_)))
105 II = I.nextLeft(d_);
106 else return 0;
107 } else {
108 if (sparseGrid->occupied(kk, I.nextRight(d_)))
109 II = I.nextRight(d_);
110 else return 0;
111 }
112
113 unsigned long indArray = sparseGrid->hash(II);
114 unsigned long k = primeTable[indArray];
115
116 if(k == 0) {
117
118 return a_.getValue(i,K);
119
120
121 } else { // anaylse what is going on
122 k = k-1; // shift since data are stored with shift
123 if(II == sparseGrid->getIndexOfTable(k)) {
124 return a_.getValue(k,K);
125 } else { // search in second table;
126 unsigned long iNext = secondTable[k];
127 while(iNext > 1) {
128 k = iNext - 2;
129 if(II == sparseGrid->getIndexOfTable(k)) {
130 return a_.getValue(k,K) ;
131 nonext = false;
132 }
133 iNext = secondTable[k];
134 }
135 }
136 }
137 }
138 }
139 } else {
140 AdaptiveSparseGrid_Base* sparseGrid = a_.getSparseGrid();
141 //unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
142
143 dataInteger* secondTable = sparseGrid->getSecondTable();
144 dataInteger* primeTable = sparseGrid->getPrimeTable();
145
146 int maxdepth = sparseGrid->getMaxDepth(d_);
147
148
149 //assert( level_ <= maxdepth);
150 maxdepth = level_;
151
152 if(secondTable[i]!=0) {
153 IndexDimension I = sparseGrid->getIndexOfTable(i);
154 bool nonext = true;
155 // todo: andere If Bedingung
156 if(I.isNotAtBoundary() == false) {
157 return 0;
158 //return a_.getDataTableVector(i);
159 }
160 IndexDimension II;
161 while(nonext){
162 if (s_ == Left){
163 II = I.nextLeft(d_,maxdepth);
164 }else{
165 II = I.nextRight(d_,maxdepth);
166 }
167
168 unsigned long indArray = sparseGrid->hash(II);
169 unsigned long k = primeTable[indArray];
170
171 if(k == 0) {
172 maxdepth= maxdepth-1;
173 if (maxdepth == 0){
174 return a_.getValue(i,K);
175 }
176
177 } else { // anaylse what is going on
178 k = k-1; // shift since data are stored with shift
179 if(II == sparseGrid->getIndexOfTable(k)) {
180 return a_.getValue(k,K);
181 } else { // search in second table;
182 unsigned long iNext = secondTable[k];
183 while(iNext > 1) {
184 k = iNext - 2;
185 if(II == sparseGrid->getIndexOfTable(k)) {
186 return a_.getValue(k,K) ;
187 nonext = false;
188 }
189 iNext = secondTable[k];
190 }
191 }
192 }
193 }
194 }
195 }
196 return 0.0;
197}
198
199
200
201
202
203
204
205
207// 2.) class ShiftOperator
209
210
211
212/* @{ */
227public:
233 ShiftOperator(int d, Direction s):d_(d),s_(s){hier =true;}
234
242 ShiftOperator(int d, Direction s, int level):d_(d),s_(s), level_(level){hier =false;}
243
244 template <class A>
245 ShiftExpr<A> operator()(ExprSparseG<A>& a){
246 if ( hier == true ){
247 return ShiftExpr<A> (a,d_,s_);
248 } else {
249 return ShiftExpr<A> (a,d_,s_,level_);
250 }
251 }
252
253private:
254 int d_;
255 Direction s_;
256
257 bool hier;
258 int level_;
259};
261
263// 3.) ShiftExprDimension
265enum Compass {
266 North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest, Center
267};
268
269
270template <class A>
271class ShiftDimensionExpr : public ExprSparseG<ShiftDimensionExpr<A> >{
272friend VectorSparseG;
273
274
275 const A& a_;
276 Compass s_;
277
278 int level;
279 bool hier;
280
281public:
282 ShiftDimensionExpr(const A& a, Compass s, bool h):a_(a), s_(s),hier(h){}
283 ShiftDimensionExpr(const A& a, Compass s, int l, bool h):a_(a),s_(s),level(l), hier(h){}
284
285
286 inline double getValue(int i, const IndexDimension& K) const;
287
288
289 ExpressionDescription getDescription() const { return ExpressionDescription(true); }
290 AdaptiveSparseGrid* getSparseGrid() const { return a_.getSparseGrid(); }
291};
292
293
294template<typename A> double ShiftDimensionExpr<A>::getValue(int i, const IndexDimension& K) const
295{
296
297
298
299 if (hier == true){
300
301 AdaptiveSparseGrid* sparseGrid = a_.getSparseGrid();
302 //unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
303
304
305 dataInteger* secondTable = sparseGrid->getSecondTable();
306 dataInteger* primeTable = sparseGrid->getPrimeTable();
307
308 if(secondTable[i]!=0) {
309 IndexDimension I = K;
310 bool nonext = true;
311 //if(I.isNotAtBoundary() == false) return a_.getValue(i,K);
312 IndexDimension II;
313 while(nonext){
314 if (s_ == North)II = I.nextLeft(1);
315 if (s_== NorthEast) II = (I.nextLeft(1)).nextRight(0);
316 if (s_==East) II = I.nextRight(0);
317 if (s_ == SouthEast) II = (I.nextRight(0)).nextRight(1);
318 if (s_ == South) II = I.nextRight(1);
319 if (s_ == SouthWest) II = (I.nextRight(1)).nextLeft(0);
320 if (s_ == West) II = I.nextLeft(0);
321 if (s_ == NorthWest) II= (I.nextLeft(0)).nextLeft(1);
322
323
324 unsigned long indArray = sparseGrid->hash(II);
325 unsigned long k = primeTable[indArray];
326
327 if(k == 0) {
328 return 0.0;
329 //return a_.getValue(i,K);
330
331 } else {
332 // anaylse what is going on
333 k = k-1; // shift since data are stored with shift
334 if(II == sparseGrid->getIndexOfTable(k)) {
335 return a_.getValue(k,II);
336
337 } else {
338 // search in second table;
339 unsigned long iNext = secondTable[k];
340 while(iNext > 1) {
341 k = iNext - 2;
342 if(II == sparseGrid->getIndexOfTable(k)) {
343 return a_.getValue(k,II) ;
344 nonext = false;
345 }
346 iNext = secondTable[k];
347 }
348 }
349 }
350 }
351 }
352 } else {
353
354 AdaptiveSparseGrid* sparseGrid = a_.getSparseGrid();
355 //unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
356
357 dataInteger* secondTable = sparseGrid->getSecondTable();
358 dataInteger* primeTable = sparseGrid->getPrimeTable();
359
360
361
362
363 //assert( level_ <= maxdepth);
364 int maxdepth = level;
365
366 if(secondTable[i]!=0) {
367 IndexDimension I =K;
368 bool nonext = true;
369 // todo: andere If Bedingung
370 /*if(I.isNotAtBoundary() == false) {
371 return 0;
372 //return a_.getDataTableVector(i);
373 }*/
374 IndexDimension II;
375 while(nonext){
376
377 if (s_ == North)II = I.nextLeft(1,maxdepth);
378 if (s_== NorthEast) II = (I.nextLeft(1,maxdepth)).nextRight(0,maxdepth);
379 if (s_==East) II = I.nextRight(0,maxdepth);
380 if (s_ == SouthEast) II = (I.nextRight(0,maxdepth)).nextRight(1,maxdepth);
381 if (s_ == South) II = I.nextRight(1,maxdepth);
382 if (s_ == SouthWest) II = (I.nextRight(1,maxdepth)).nextLeft(0, maxdepth);
383 if (s_ == West){
384
385 II = I.nextLeft(0, maxdepth);
386
387 }
388 if (s_ == NorthWest) II= (I.nextLeft(0, maxdepth)).nextLeft(1, maxdepth);
389
390
391 unsigned long indArray = sparseGrid->hash(II);
392 unsigned long k = primeTable[indArray];
393
394 if(k == 0) {
395
396 return 0;
397 /*maxdepth= maxdepth-1;
398 if (maxdepth == 0){
399 return a_.getValue(i,K);
400 }*/
401
402 } else {
403 // anaylse what is going on
404 k = k-1; // shift since data are stored with shift
405 if(II == sparseGrid->getIndexOfTable(k)) {
406
407 return a_.getValue(k,II);
408 } else { // search in second table;
409 unsigned long iNext = secondTable[k];
410 while(iNext > 1) {
411 k = iNext - 2;
412 if(II == sparseGrid->getIndexOfTable(k)) {
413 return a_.getValue(k,II) ;
414 nonext = false;
415 }
416 iNext = secondTable[k];
417 }
418 }
419 }
420 }
421 }
422 }
423 return 0.0;
424}
425
426
427
428
429
431// 4.) class ShiftOperatorDimension
433
434
435
436/* @{ */
437/*class ShiftOperatorDimension{
438
439public:
440*//*
445 ShiftOperatorDimension(Compass s):s_(s){
446 hier = true;
447 myAssert(DimensionSparseGrid == 2);
448 }
449
450
451 ShiftOperatorDimension(Compass s, int l):s_(s), level(l){
452 hier = false;
453 myAssert(DimensionSparseGrid == 2);
454 }
455
456
457
458
459 template <class A>
460 ShiftDimensionExpr<A> operator()(ExprSparseG<A>& a){
461 if (hier == true){
462 return ShiftDimensionExpr<A> (a,s_, hier);
463 } else {
464 return ShiftDimensionExpr<A>(a,s_,level,hier);
465 }
466
467
468
469 }
470
471private:
472
473 Compass s_;
474 int level;
475 bool hier;
476
477 };*/
479
480
481#endif
Definition sparseGrid.h:86
IndexDimension getIndexOfTable(unsigned long i)
Definition sparseGrid.h:566
unsigned long hash(IndexDimension index)
Definition sparseGrid.h:395
bool occupied(unsigned long &indexOfData, IndexDimension I)
Definition sparseGrid.h:465
Definition sparseGrid.h:277
Definition shift.h:48
Definition shift.h:226
ShiftOperator(int d, Direction s)
Definition shift.h:233
ShiftOperator(int d, Direction s, int level)
Definition shift.h:242