LoAdSG
vector.h
1/**********************************************************************************
2* Author: Christoph Pflaum, Riccarda Scherner-Griesshammer
3 * Department Informatik Lehrstuhl 10 - Systemsimulation
4 * Friedrich-Alexander Universität Erlangen-Nürnberg
5 *
6*********************************************/
7
8// ------------------------------------------------------------
9//
10// vector.h
11//
12// ------------------------------------------------------------
13
14#ifndef VECTOR_SG_H
15#define VECTOR_SG_H
16#define testNow
17
18#include "../abbrevi.h"
19#include "../myAssert.h"
20#include "../indices/index.h"
21
22#include "../primes/prime.h"
23#include "../sgrid/depth.h"
24#include "../sgrid/sparseGrid.h"
25#include "extempAlg.h"
26
27
28#include "../sgrid/ListOfDepthOrderedGrids.h"
29#include "../sgrid/multilevelSparseGrid.h"
30
31
32
33#include "../mympi.h"
34#include "../sgrid/multiDepthHashGrid.h"
35
36
37
38
39
40
42// Restriction
44
45/*
46template <class A>
47class ExprSparseG_Restriction_Index {
48 public:
49 ExprSparseG_Restriction_Index(const ExprSparseG<A>& a, const IterationDepth& iterDepth_)
50 : a_(a), iterDepth(iterDepth_) { }
51
52 // inline double getValue(double* data, const IndexDimension& I) const {
53 // return a_.getValue(data,I);
54 //}
55 inline double getValue(int i, const IndexDimension& I)const{
56 return a_.getValue(i,I);
57 }
58
59
60
61 ExpressionDescription getDescription() const {
62 return a_.getDescription();
63 }
64
65 AdaptiveSparseGrid_Base* getSparseGrid() const { return a_.getSparseGrid(); }
66
67 IterationDepth getIterDepth() const { return iterDepth; }
68
69 public:
70 //const reference to an object of class A
71 const A& a_;
72 const IterationDepth iterDepth;
73};
74
75template <class A>
76inline ExprSparseG_Restriction_Index<A> operator| ( const ExprSparseG<A>& a, const IterationDepth& iterDepth) {
77 return ExprSparseG_Restriction_Index<A>(a,iterDepth);
78}
79
80
81template <class A>
82inline ExprSparseG_Restriction_Index<A> operator| (const ExprSparseG<A>& a,int i) {
83 myAssert(i > -1);
84 IterationDepth iterDepth(i);
85 return ExprSparseG_Restriction_Index<A>(a,iterDepth);
86}
87*/
88
90// Restriction on a subgrid of certain depth
92
93
94template <class A>
95class ExprSparseG_Restriction_SubgridDepth {
96
97public:
98 ExprSparseG_Restriction_SubgridDepth(const ExprSparseG<A> &a, const SubgridFixedDepth &iterObj_)
99 : a_(a), iterObj(iterObj_) {}
100
101 inline double getValue(int i, const IndexDimension &I) const {
102
103 return a_.getValue(i, I);
104 }
105
106 ExpressionDescription getDescription() const {
107 return a_.getDescription();
108 }
109
110 AdaptiveSparseGrid *getSparseGrid() const { return a_.getSparseGrid(); }
111
112 SubgridFixedDepth getSubgridIterator() const { return iterObj; }
113
114public:
115 //const reference to an object of class A
116 const A &a_;
117 const SubgridFixedDepth iterObj;
118};
119
120template<class A>
121inline ExprSparseG_Restriction_SubgridDepth<A> operator|(const ExprSparseG<A> &a, const SubgridFixedDepth &iterObj) {
122
123 return ExprSparseG_Restriction_SubgridDepth<A>(a, iterObj);
124}
126//Restriction on fixed Index
127
128
129template<class A>
130class ExprSparseG_Restriction_FixedIndex {
131
132public:
133 ExprSparseG_Restriction_FixedIndex(const ExprSparseG<A> &a, const IndexDimension &iterObj_)
134 : a_(a), iterObj(iterObj_) {}
135
136 inline double getValue(int i, const IndexDimension &I) const {
137 return a_.getValue(i, I);
138 }
139
140 ExpressionDescription getDescription() const {
141 return a_.getDescription();
142 }
143
144 AdaptiveSparseGrid *getSparseGrid() const { return a_.getSparseGrid(); }
145
146 IndexDimension getIndex() const { return iterObj; }
147
148public:
149 //const reference to an object of class A
150 const A &a_;
151 const IndexDimension iterObj;
152};
153
154/*
155template<class A>
156inline ExprSparseG_Restriction_FixedIndex<A> operator|(const ExprSparseG<A> &a, const IndexDimension &iterObj) {
157 return ExprSparseG_Restriction_FixedIndex<A>(a, iterObj);
158}
159*/
160
161
162
163
164class DoubleSubgridExpr {
165
166public:
167 DoubleSubgridExpr(double d_, SubgridFixedDepth itObj_) : d(d_), itObj(itObj_) {};
168
169 SubgridFixedDepth getSubgrid() const { return itObj; };
170
171 double getValue() const { return d; };
172private:
173 double d;
174 SubgridFixedDepth itObj;
175
176};
177
178
179class DoubleDepthExpr {
180
181public:
182 DoubleDepthExpr(double d_, Depth itObj_) : d(d_), itObj(itObj_) {};
183
184 Depth getDepth() const { return itObj; };
185
186 double getValue() const { return d; };
187private:
188 double d;
189 Depth itObj;
190
191};
192
193
194inline DoubleSubgridExpr
195operator|(double d, const SubgridFixedDepth &itObj) {
196 return DoubleSubgridExpr(d, itObj);
197}
198
199
200inline DoubleDepthExpr
201operator|(double d, const Depth &itObj) {
202 return DoubleDepthExpr(d, itObj);
203}
204
205
206class DoubleIndexExpr {
207
208public:
209 DoubleIndexExpr(double d_, IndexDimension itObj_) : d(d_), itObj(itObj_) {};
210
211 IndexDimension getIndex() const { return itObj; };
212
213 double getValue() const { return d; };
214private:
215 double d;
216 IndexDimension itObj;
217
218};
219
220
221inline DoubleIndexExpr
222operator|(double d, const IndexDimension &itObj) {
223 return DoubleIndexExpr(d, itObj);
224}
225
226
227
228
229
230
231
233// Restriction with Level Object
234
235
236
237
238
240// 2. vector
242
243template<class B>
244class DWrapSim;
245
246
247 class VectorSparseG : public ExprSparseG<VectorSparseG> {
248 template<class B>
249 friend
250 class DWrapSim;
251
252
253 public:
254 int length=0;
255 VectorSparseG(){};
256
257 VectorSparseG(AdaptiveSparseGrid &sg);
258
259 VectorSparseG(AdaptiveSparseGrid *sg);
260
261 VectorSparseG(VectorSparseG &u);
262
263
264 ~VectorSparseG();
265
266
267 AdaptiveSparseGrid *getSparseGrid() { return sparseGrid; }
268
269 double *getDatatableVector() { return dataTableVector; }
270
271
272 template<class A>
273 void operator=(const ExprSparseG<A> &a);
274
275
276 template<class A>
277 void operator=(const ExprSparseG_Restriction_SubgridDepth<A> &a);
278
279 template<class A>
280 void operator=(const ExprSparseG_Restriction_FixedIndex<A> &a);
281
282 void operator=(const VectorSparseG &v);
283
284 void operator=(VectorSparseG &v);
285
286 inline void operator=(double x);
287
288 bool operator==(const VectorSparseG &v);
289
290 void setMultiLevelValues2(MultiLevelVector &a, Depth &T);
291
292 void addMultiLevelValues2(MultiLevelVector &a, Depth &T);
293 void setMultiLevelValues(MultiLevelVector &a, Depth &T);
294
295 inline void operator=(const DoubleSubgridExpr &a);
296
297 inline void operator=(const DoubleIndexExpr &a);
298
299 inline bool workonindex(unsigned long i) {
300
301 if ((sparseGrid->WorkOnHangingNodes) || (!(sparseGrid->WorkOnHangingNodes) && sparseGrid->getActiveTable()[i]))
302 return true;
303
304 return false;
305 }
306
307 ExpressionDescription getDescription() const { return ExpressionDescription(false); }
308
309 AdaptiveSparseGrid_Base *getSparseGrid() const { return sparseGrid; }
310
323 void Print(int level);
324
325 void PrintDouble(int level);
326
327 void PrintDoubleTwoD(int level, Depth T);
328
329
330 void PrintIntTwoD(int level);
331
332 void PrintDoubleTwoD(int level);
333
334
335 void PrintDoubleOneD(int level);
336
337 void PrintIntOneD(int level);
338
339 void Print_vtk(std::ostream &Datei);
340
341 void Print_gnu(string name);
342
343 void PrintIndexTwoD(int d, int level);
344
345
346 inline double getValue(int i, const IndexDimension &I) const { return dataTableVector[i]; };
347
348 inline double getValue(IndexDimension &I) const {
349 unsigned long k;
350 if (sparseGrid->occupied(k, I)) return dataTableVector[k];
351 return 0.0;
352
353 };
354
355 inline double getValue(unsigned long k) const {
356
357 return dataTableVector[k];
358
359
360 };
361
362 inline double getValue(const IndexDimension &I) const {
363 unsigned long k;
364 if (sparseGrid->occupied(k, I)) return dataTableVector[k];
365 return 0.0;
366
367 };
368
369 inline double getValue(unsigned long k) {
370 return dataTableVector[k];
371 }
372
373 inline double getValue(const IndexDimension &I, const SingleDepthHashGrid *singleDepthHashGrid) const {
374 unsigned long k;
375 if (singleDepthHashGrid->occupied(k, I)) return dataTableVector[k];
376 return 0.0;
377
378 };
379
380 inline bool setValue(IndexDimension &I, double x) const {
381 unsigned long k;
382 if (sparseGrid->occupied(k, I)) {
383 dataTableVector[k] = x;
384 return true;
385 }
386 return false;
387
388 };
389
390 inline bool setValue(unsigned long k, double x) const {
391
392 dataTableVector[k] = x;
393 return true;
394
395
396 };
397
398 inline bool addToValue(unsigned long k, double x) const {
399 #pragma omp atomic
400 dataTableVector[k] += x;
401 return true;
402 };
403
404 inline bool addToValueNoOMP(unsigned long k, double x) const {
405 dataTableVector[k] += x;
406 return true;
407 };
408 inline bool addToValue(IndexDimension &I, double x) const {
409 unsigned long k;
410 if (sparseGrid->occupied(k, I)) {
411 dataTableVector[k] += x;
412 return true;
413 }
414 return false;
415
416 };
417
418
419
421
422 void Broadcast(int rank);
423
424 bool mpi_doit();
425
426 void sendTo(int torank);
427
428 void ReduceSum(int rank);
429
430
431 Process *getProcess() { return sparseGrid->mpi; }
432
433 protected:
434 AdaptiveSparseGrid *sparseGrid;
435 double *dataTableVector;
436 private:
437
438 bool constructed=false;
439
440
441 };
442
443
444
445
446
448// 3. Other functions, max, ...
450
451template <class A, class B>
452double product(const ExprSparseG<A>& a, const ExprSparseG<B>& b );
453
454
455template <class A>
456double Maximum ( const ExprSparseG<A>& a );
457
458
459template <class A>
460double Minimum ( const ExprSparseG<A>& a );
461
462
463
465// and it's implementation
466template<typename A, typename B> double product(const ExprSparseG<A>& a, const ExprSparseG<B>& b)
467{
468 const A& ao (a);
469 const B& bo (b);
470
471 AdaptiveSparseGrid_Base* sparseGridA = ao.getSparseGrid();
472 AdaptiveSparseGrid_Base* sparseGridB = bo.getSparseGrid();
473
474 // todo check if a,b have same sparse grids
475
476 unsigned long endIndex = sparseGridA->maximalOccupiedSecondTable;
477 //double* dataTable = sparseGrid->dataTable;
478 dataInteger* secondTable = sparseGridA->secondTable;
479 //unsigned int numberOfData = sparseGrid->numberOfData;
480
481
482
483 if(ao.getDescription().isIndexNeeded()|| bo.getDescription().isIndexNeeded()) {
484 double value = 0.0;
485 for(unsigned long i = 0;i < endIndex; ++i) {
486 if(secondTable[i]!=0) {
487
488 //double x = ao.getValue(&dataTable[i * numberOfData],sparseGrid->getIndexOfTable(i));
489 double ax = ao.getValue(i,sparseGridA->getIndexOfTable(i));
490 double bx = bo.getValue(i,sparseGridB->getIndexOfTable(i));
491
492 value = value + ax*bx;
493 }
494 }
495 return value;
496 }
497 else {
498 double value = 0.0;
499 IndexDimension Idummy;
500 for(unsigned long i = 0;i < endIndex; ++i) {
501 if(secondTable[i]!=0) {
502 double ax = ao.getValue(i,Idummy);
503 double bx = bo.getValue(i,Idummy);
504
505 value = value + ax*bx;
506 }
507 }
508 return value;
509 }
510}
511
512
513
514
515
516
517
518
519
520
521
523
524
525template <class A>
526void VectorSparseG::operator=(const ExprSparseG<A>& a) {
527
528 if (mpi_doit()){
529 const A& ao ( a );
530
531 unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
532 //double* dataTable = sparseGrid->dataTable;
533 dataInteger* secondTable = sparseGrid->secondTable;
534 //unsigned int numberOfData = sparseGrid->numberOfData;
535
536 if(ao.getDescription().isIndexNeeded()) {
537
538 for(unsigned long i = 0;i < endIndex; ++i) {
539
540 if (workonindex(i))
541 dataTableVector[i] = ao.getValue(i, sparseGrid->getIndexOfTable(i));
542
543
544 }
545 }
546 else {
547 IndexDimension Idummy;
548 for(unsigned long i = 0;i < endIndex; ++i) {
549 if(secondTable[i]!=0)
550 //dataTable[number + i * numberOfData] = ao.getValue(&dataTable[i * numberOfData],Idummy);
551
552 dataTableVector[i]=ao.getValue(i,Idummy);
553 }
554 }
555 }
556}
557
558
559
560//test
561//IndexDimension I;
562//I = iteratorSG.getCurrentPoint();
563//cout << " IteratorSG::hasNext() " << I.coordinate(0)
564// << " " << I.coordinate(1) << " i: " << i << endl;
565
566
567
568
569
570
571template <class A>
572void VectorSparseG::operator=(const ExprSparseG_Restriction_SubgridDepth<A>& a) {
573
574 if (mpi_doit()) {
575 //unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
576 //double* dataTable = sparseGrid->dataTable;
577 dataInteger *secondTable = sparseGrid->secondTable;
578 //unsigned int numberOfData = sparseGrid->numberOfData;
579
580 //hier wird gesamtes subgrid kopiert. braucht man das?
581 SubgridFixedDepth subgrid = a.getSubgridIterator();
582 SubgridFixedDepth::iterator itobj(subgrid);
583
584 for (bool weiter = true; weiter; weiter = itobj.next()) {
585
586 unsigned long i = itobj.geti();
587 if (a.getDescription().isIndexNeeded()) {
588 if (secondTable[i] != 0)
589 if (workonindex(i))
590 dataTableVector[i] = a.getValue(i, itobj.getPoint());
591 } else {
592 IndexDimension Idummy;
593 if (secondTable[i] != 0)
594 if (workonindex(i))
595 dataTableVector[i] = a.getValue(i, Idummy);
596 }
597 }
598 }
599}
600
601
602template<class A>
603void VectorSparseG::operator=(const ExprSparseG_Restriction_FixedIndex<A> &a) {
604
605 if (mpi_doit()) {
606 //unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
607 //double* dataTable = sparseGrid->dataTable;
608 //dataInteger *secondTable = sparseGrid->secondTable;
609 //unsigned int numberOfData = sparseGrid->numberOfData;
610
611
612 IndexDimension Index = a.getIndex();
613 unsigned long k;
614 if (sparseGrid->occupied(k, Index)) {
615 if (workonindex(k))
616 dataTableVector[k] = a.getValue(k, Index);
617 }
618
619
620 /* for (int i = 0; i <= endIndex; i++) {
621
622 IndexDimension localIndex = sparseGrid->getIndexOfTable(i);
623 if (localIndex == Index)
624 if (a.getDescription().isIndexNeeded()) {
625 if (workonindex(i))
626 dataTableVector[i] = a.getValue(i, Index);
627 } else {
628 IndexDimension Idummy;
629 if (workonindex(i))
630 dataTableVector[i] = a.getValue(i, Idummy);
631 }
632 }*/
633 }
634}
635
636
637void VectorSparseG::operator=(const DoubleSubgridExpr &a) {
638
639 if (mpi_doit()) {
640 SubgridFixedDepth subgrid = a.getSubgrid();
641 SubgridFixedDepth::iterator itobj(subgrid);
642
643 double value = a.getValue();
644
645 for (bool weiter = true; weiter; weiter = itobj.next()) {
646 unsigned long i = itobj.geti();
647 if (workonindex(i))
648 dataTableVector[i] = value;
649 }
650 }
651}
652
653
654void VectorSparseG::operator=(const DoubleIndexExpr &a) {
655
656
657 if (mpi_doit()) {
658 IndexDimension Index = a.getIndex();
659 unsigned long k;
660 if (sparseGrid->occupied(k, Index)) {
661 if (workonindex(k))
662 dataTableVector[k] = a.getValue();
663 }
664
665 /* double value = a.getValue();
666
667 for (int i = 0; i <= sparseGrid->maximalOccupiedSecondTable; i++) {
668 IndexDimension localIndex = sparseGrid->getIndexOfTable(i);
669 if (Index == localIndex)
670 if (workonindex(i))
671 dataTableVector[i] = value;
672 }*/
673 }
674
675}
676
677
678
679
680
681
683
684inline void VectorSparseG::operator=(double x) {
685
686 if (mpi_doit()) {
687 unsigned long endIndex = sparseGrid->maximalOccupiedSecondTable;
688 //double* dataTable = sparseGrid->dataTable;
689 dataInteger *secondTable = sparseGrid->secondTable;
690 //unsigned int numberOfData = sparseGrid->numberOfData;
691
692
693
694 for (unsigned long i = 0; i <= endIndex; ++i) {
695 if (secondTable[i] != 0) {
696 if (sparseGrid->workonindex(i))
697 dataTableVector[i] = x;
698 }
699 }
700 }
701
702}
703
704
705
706#endif //VECTOR_SG_H
707
708
709
Definition sparseGrid.h:86
IndexDimension getIndexOfTable(unsigned long i)
Definition sparseGrid.h:566
unsigned long * secondTable
‍0 means empty; v>0 means v-1 is array index
Definition sparseGrid.h:232
Definition sparseGrid.h:277
Definition ListOfDepthOrderedGrids.h:82
Definition ListOfDepthOrderedGrids.h:31