FixVec.h 20.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file FixVec.h */

#ifndef AMDIS_FIXVEC_H
#define AMDIS_FIXVEC_H

// ===========================================================================
// ===== includes ============================================================
// ===========================================================================

#include "Global.h"
#include "MemoryManager.h"
#include <iostream>
#include "MatrixVector.h"

namespace AMDiS {

  // ===========================================================================
  // ===== forward declarations ================================================
  // ===========================================================================

  class Mesh;
  template<typename T> class WorldVector;
  template<typename T> class WorldMatrix;


  // ===========================================================================
  // ===== definitions =========================================================
  // ===========================================================================

  /** determines how to initialize a FixVec when constructed */
  enum InitType 
    {
      NO_INIT = 0,       /**< no initialisation */
      VALUE_LIST = 1,    /**< a complete value list is given */
      DEFAULT_VALUE = 2, /**< all values ar set to a given default value */
      UNIT_VECTOR = 3,   /**< the i-th value is 1, all other values are 0 */
      UNIT_MATRIX = 4    /**< values at the diagonal of a matrix are set to one */
    };

  // ===========================================================================
  // ===== class FixVec ========================================================
  // ===========================================================================

  /** \ingroup Common
   * \brief
   * A FixVec is a template vector of a fixed size. 
   *
   * The size is determined at construction time and depends on the dimension
   * and the template parameter d. So a FixVec<int, VERTEX> is a integer vector
   * with 3 entries in 2d and with 4 entries in 3d. The dimension and the way
   * the vector should be initialized are specified by the constructor call.
   */
  template<typename T,GeoIndex d>
  class FixVec : public Vector<T>
  {
  public:
    MEMORY_MANAGED(FixVec<T COMMA d>);

    /** \brief
     * constructor without initialisation. initType must be NO_INIT. If dim is
     * not spezified, a FixVec for DIM_OF_WORLD is created.
     */
    FixVec(int dim = -1, InitType initType = NO_INIT)
      : Vector<T>(calcSize(dim))
    { 
85
      TEST_EXIT_DBG(initType == NO_INIT)("wrong initType or missing initializer\n");
86
87
88
89
90
91
92
93
94
    }

    /** \brief
     * constructor with value list initialisation. initType must be VALUE_LIST.
     * ini is an array which contains the initialisation values.
     */
    FixVec(int dim, InitType initType, const T* ini)
      : Vector<T>(calcSize(dim))
    {
95
      TEST_EXIT_DBG(initType == VALUE_LIST)("wrong initType or wrong initializer\n");
96
97
98
99
100
101
102
103
104
105
      setValues(ini);
    };

    /** \brief
     * constructor with default value initialisation. initType must be
     * DEFAULT_VALUE. All vector entries are set to ini.
     */
    FixVec(int dim, InitType initType, const T& ini)
      : Vector<T>(calcSize(dim))
    {
106
      TEST_EXIT_DBG(initType == DEFAULT_VALUE)("wrong initType or wrong initializer\n");
107
108
109
110
111
112
113
114
      this->set(ini);
    }

    /** \brief
     * Initialisation for dim.
     */
    inline void init(int dim) 
    {
115
      this->resize(calcSize(dim));
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    };

    /** \brief
     * Initialisation for size
     */
    inline void initSize(int size) 
    {
      this->resize(size);
    };  

    /** \brief
     * Returns the \ref size_ of the FixVec.
     */
    inline int size() const { 
      return this->getSize(); 
    }; 

  protected:
    /** \brief
     * Determines needed vector size.
     */
    static int calcSize(int dim) {
      if (dim < 0) {
	return Global::getGeo(WORLD);
      } else {
	return Global::getGeo(d, dim);
      }
    };

  public:
    friend class GLWindow;
  };


  // ===========================================================================
  // ===== class VectorOfFixVecs ===============================================
  // ===========================================================================

  /** \ingroup Common
   * \brief
   * Contains an vector of FixVecs of same type. To simply allocate an array of 
   * FixVecs
   * isn't possible, because the FixVec constructor normally needs at least
   * the corresponding dimension. So you must create an array of FixVec pointers
   * and call the constructor of each FixVec manually. When you use 
   * VectorOfFixVecs, this work is done by VectorOfFixVecs's constructor.
   */
  template<typename FixVecType>
  class VectorOfFixVecs
  {
  public:
    MEMORY_MANAGED(VectorOfFixVecs<FixVecType>);

    /** \brief
     * constructs a VectorOfFixVecs without initialisation. dim is passed to 
     * FixVec's constructors. size_ is the number of contained FixVecs. initType
     * must be NO_INIT.
     */
174
175
    VectorOfFixVecs(int dim, int size, InitType initType) 
      : size_(size)
176
    {
177
      TEST_EXIT_DBG(initType == NO_INIT)("wrong initType or wrong initializer\n");
178

179
      vec = GET_MEMORY(FixVecType*, size_);
180
      for (FixVecType** i = &vec[0]; i < &vec[size_]; i++) {
181
182
183
184
185
186
187
188
189
190
191
192
	*i = NEW FixVecType(dim, NO_INIT);
      }
    }

    /** \brief
     * constructs a VectorOfFixVecs via an value list.  dim is passed to 
     * FixVec's constructors. size_ is the number of contained FixVecs. initType
     * must be VALUE_LIST. ini contains the initialisation values.
     */
    VectorOfFixVecs(int dim, int s, InitType initType, const FixVecType* ini)
      : size_(s)
    {
193
      TEST_EXIT_DBG(initType == VALUE_LIST)("wrong initType or wrong initializer\n");
194

195
      vec = GET_MEMORY(FixVecType*, size_);
196
      for (FixVecType** i = &vec[0]; i < &vec[size_]; i++) {
197
198
199
200
201
202
203
204
205
206
207
208
	*i = NEW FixVecType(ini[i]);
      }    
    }

    /** \brief
     * constructs a VectorOfFixVecs with an default value.  dim is passed to 
     * FixVec's constructors. size_ is the number of contained FixVecs. initType
     * must be DEFAULT_VALUE. All entries are set to ini.
     */
    VectorOfFixVecs(int /*dim*/, int s, InitType initType, const FixVecType& ini)
      : size_(s)
    {
209
      TEST_EXIT_DBG(initType == DEFAULT_VALUE)
210
211
	("wrong initType or wrong initializer\n");
      vec = GET_MEMORY(FixVecType*, size_);
212
      for (FixVecType** i = &vec[0]; i < &vec[size_]; i++) {
213
214
215
216
217
218
219
220
221
222
223
224
	*i = NEW FixVecType(ini);
      }    
    }

    /** \brief
     * copy constructor
     */
    VectorOfFixVecs(const VectorOfFixVecs<FixVecType>& rhs)
    {
      size_ = rhs.getSize();
      vec = GET_MEMORY(FixVecType*, size_);

225
      for (int i = 0; i < size_; i++) {
226
227
228
229
230
231
232
233
234
	vec[i] = NEW FixVecType(*(rhs.vec[i]));
      }
    };

    /** \brief
     * destructor
     */
    virtual ~VectorOfFixVecs()
    {
235
      for (FixVecType** i = &vec[0]; i<&vec[size_]; i++) {
236
237
238
239
240
241
242
243
244
245
246
	DELETE *i;
      }
      FREE_MEMORY(vec, FixVecType*, size_);
      //delete [] vec;
    };

    /** \brief
     * Allows the access like in a normal array via []. Used for const objects.
     */
    inline const FixVecType& operator[](int index) const
    {
247
      TEST_EXIT_DBG(index >= 0 && index < size_)("invalid index\n");
248
249
250
251
252
253
254
255
      return *(vec[index]);
    };

    /** \brief
     * Allows the access like in a normal array via []. 
     */
    inline FixVecType& operator[](int index)
    {
256
      TEST_EXIT_DBG(index >= 0 && index < size_)("invalid index\n");
257
258
259
260
261
262
263
264
265
      return *(vec[index]);
    };

    /** \brief
     * assignment operator
     */
    VectorOfFixVecs<FixVecType>& 
    operator=(const VectorOfFixVecs<FixVecType>& rhs)
    {
266
      TEST_EXIT_DBG(size_ == rhs.size_)("vectors of different size\n");
267
268
269
270
271
272
273
274
275
276
277
278
      if (this!=&rhs) {
	FixVecType **i, **j;
	for(i=&vec[0], j=&(rhs.vec[0]); i < &vec[size_]; i++, j++) { 
	  **i = **j; 
	}
      }
      return *this;
    };

    /** \brief
     * Returns the \ref size of this VectorOfFixVecs
     */
279
280
281
    inline int getSize() const { 
      return size_; 
    };
282
283
284
285

    /** \brief
     * Returns the size of the contained FixVecs
     */
286
287
288
    inline int getSizeOfFixVec() const { 
      return vec[0]->getSize(); 
    }
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

  protected:
    /** \brief
     * number of contained FixVecs
     */
    int size_;

    /** \brief
     * array of pointers to FixVecs
     */
    FixVecType **vec;
  };

  // ===========================================================================
  // ===== class MatrixOfFixVecs ===============================================
  // ===========================================================================

  /** \ingroup Common
   * \brief
   * Like the class VectorOfFixVecs contains a vector of FixVecs, this class
   * contains a matrix of FixVecs of same type.
   */
  template<typename FixVecType>
  class MatrixOfFixVecs
  {
  public:
    MEMORY_MANAGED(MatrixOfFixVecs<FixVecType>);

    /** \brief
     * Constructs the matrix without initialisation. r is the number of rows,
     * c is the number of columns. The other parameters are analog to the
     * VectorOfFixVecs constructors.
     */
    MatrixOfFixVecs(int dim, int r, int c, InitType initType)
      : rows(r), columns(c)
    {
325
      TEST_EXIT_DBG(initType == NO_INIT)("wrong initType or wrong initializer\n");
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
      vec = GET_MEMORY(VectorOfFixVecs<FixVecType>*, rows);
      for(VectorOfFixVecs<FixVecType>** i=&vec[0]; i<&vec[rows]; i++) {
	*i = NEW VectorOfFixVecs<FixVecType>(dim, columns, NO_INIT);
      }
    };


    /** \brief
     * destructor
     */
    virtual ~MatrixOfFixVecs()
    {
      for(VectorOfFixVecs<FixVecType>** i=&vec[0]; i<&vec[rows]; i++) {
	DELETE *i;
      }
      FREE_MEMORY(vec, VectorOfFixVecs<FixVecType>*, rows);
    };

    /** \brief
     * assignment operator
     */
    inline VectorOfFixVecs<FixVecType >& operator[](int index)
    {
349
      TEST_EXIT_DBG(index >= 0 && index < rows)("invalid index\n");
350
351
352
353
354
355
356
357
      return *(vec[index]);
    };

    /** \brief
     * assignment operator
     */
    inline const VectorOfFixVecs<FixVecType >& operator[](int index) const
    {
358
      TEST_EXIT_DBG(index >= 0 && index < rows)("invalid index\n");
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
      return *(vec[index]);
    };

    /** \brief
     * Returns \ref rows
     */
    inline int getNumberOfRows() const { 
      return rows; 
    };

    /** \brief
     * Returns \ref columns
     */
    inline int getNumberOfColumns() const { 
      return columns; 
    };

  private:
    /** \brief
     * number of rows of the matrix
     */

    int rows;

    /** \brief
     * number of columns of the matrix
     */
    int columns;

    /** \brief
     * array of pointers to VectorOfFixVecs
     */
    VectorOfFixVecs<FixVecType> **vec;
  };


  // ===========================================================================
  // ===== class DimVec ========================================================
  // ===========================================================================

  /** \ingroup Common
   * \brief
   * A DimVec is a FixVec with dim + 1 entries. It can be used for storing
   * barycentric coordinates or information associated to vertices or
   * parts of an element.
   */
  template<typename T>
  class DimVec : public FixVec<T,PARTS> {
  public:
    MEMORY_MANAGED(DimVec<T>);

    DimVec() {};

    /** \brief
     * Calls the corresponding constructor of FixVec
     */
    DimVec(int dim, InitType initType=NO_INIT)
      : FixVec<T,PARTS>(dim, initType)
    {};

    /** \brief
     * Calls the corresponding constructor of FixVec
     */
    DimVec(int dim, InitType initType, T* ini)
      : FixVec<T,PARTS>(dim, initType, ini)
    {};

    /** \brief
     * Calls the corresponding constructor of FixVec
     */
    DimVec(int dim, InitType initType, const T& ini)
      : FixVec<T,PARTS>(dim, initType, ini)
    {};
  };

  // ===========================================================================
  // ===== class DimMat ========================================================
  // ===========================================================================

  /** \ingroup Common
   * \brief
   * A DimMat is a VectorOfFixVecs of dim+1 DimVecs. 
   */
  template<typename T>
  class DimMat : public Matrix<T>
  {
  public:
    MEMORY_MANAGED(DimMat<T>);

    /** \brief
     * Calls the corresponding constructor of VectorOfFixVecs
     */
451
452
    DimMat(int dim, InitType initType = NO_INIT)
      : Matrix<T>(dim + 1, dim + 1)
453
454
455
456
457
458
    {};

    /** \brief
     * Calls the corresponding constructor of VectorOfFixVecs
     */
    DimMat(int dim, InitType initType, const T& ini)
459
      : Matrix<T>(dim + 1, dim + 1)
460
    {
461
      TEST_EXIT_DBG(initType == DEFAULT_VALUE)
462
463
464
465
466
467
468
469
	("wrong initType or wrong initializer\n");    
      this->set(ini);
    };

    /** \brief
     * Calls the corresponding constructor of VectorOfFixVecs
     */
    DimMat(int dim, InitType initType, T* ini)
470
      : Matrix<T>(dim + 1, dim + 1)
471
    {
472
      TEST_EXIT_DBG(initType == VALUE_LIST)("wrong initType or wrong initializer\n");
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
      setValues(ini);
    };
  };

  // ===========================================================================
  // ===== class WorldVector ===================================================
  // ===========================================================================

  /** \ingroup Common
   * \brief
   * A WorldVector is an AlgoVec with DIM_OF_WORLD entries of type double.
   * Can be used for storing world coordinates.
   */
  template<typename T>
  class WorldVector : public FixVec<T, WORLD>
  {
  public:
    MEMORY_MANAGED(WorldVector<T>);

    /** \brief
     * Calls the corresponding constructor of AlgoVec
     */
    WorldVector() 
      : FixVec<T, WORLD>(Global::getGeo(WORLD), NO_INIT) 
    {};

    /** \brief
     * Calls the corresponding constructor of AlgoVec
     */
    WorldVector(InitType initType, T* ini) 
      : FixVec<T, WORLD>(Global::getGeo(WORLD), initType, ini)
    {};

    /** \brief
     * Calls the corresponding constructor of AlgoVec
     */
    WorldVector(InitType initType, const T& ini)
      : FixVec<T, WORLD>(Global::getGeo(WORLD), initType, ini)
    {};

    /** \brief
     * Sets all entries to d
     */
    inline const WorldVector<T>& operator=(const T& d)
    {
      this->set(d);
      return (*this);
    };

522
523
524
525
526
527
528
529
530
531
532
533
534
    /** \brief
     * Sets the arrays value to the geometric midpoint of the points
     * p1 and p2;
     */
    inline void setMidpoint(const WorldVector<T> &p1, const WorldVector<T> &p2)
    {
      int dow = Global::getGeo(WORLD);

      for (int i = 0; i < dow; i++) {
	this->valArray[i] = 0.5 * (p1[i] + p2[i]);
      }
    }

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
    /** \brief
     * Multplication of a matrix with a vector.
     */
    void multMatrixVec(WorldMatrix<T> &m, WorldVector<T> &v);
  };


  // ===========================================================================
  // ===== class WorldMatrix ===================================================
  // ===========================================================================

  /** \ingroup Common
   * \brief
   * A WorldMatrix is a FixVec with DIM_OF_WORLD WorldVector entries.
   * So it can be seen as matrix with DIM_OF_WORLD x DIM_OF_WORLD double 
   * entries.
   * Here it's not necessary to use the VectorOfFixVecs class, because the 
   * FixVec constructor assumes dim = DIM_OF_WORLD, if no dim is spezified, 
   * what is the right assumption in this case.
   */
  template<typename T>
  class WorldMatrix : public Matrix<T>
  {
  public:
    MEMORY_MANAGED(WorldMatrix<T>);

    /** \brief
     * Calls the corresponding constructor of FixVec
     */
    WorldMatrix()
      : Matrix<T>(Global::getGeo(WORLD), Global::getGeo(WORLD))
    {};

    /** \brief
     * Calls the corresponding constructor of FixVec
     */
    WorldMatrix(InitType initType, T* ini)
      : Matrix<T>(Global::getGeo(WORLD), Global::getGeo(WORLD))
    {
574
      TEST_EXIT_DBG(initType == VALUE_LIST)("???\n");
575
576
577
578
579
580
581
582
583
584
      setValues(ini);
    };

    /** \brief
     * Calls the corresponding constructor of FixVec and sets all matrix entries
     * to ini
     */
    WorldMatrix(InitType initType, const T& ini)
      : Matrix<T>(Global::getGeo(WORLD), Global::getGeo(WORLD))
    {
585
      TEST_EXIT_DBG(initType == DEFAULT_VALUE)("wrong initType or wrong initializer\n");
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
      this->set(ini);
    };
  
    /** \brief
     * Returns true if the matrix is a diagonal matrix, returns false otherwise.
     */
    bool isDiagMatrix() const;

    /** \brief
     * Returns true if the matrix is symmetric, returns false otherwise.
     */
    bool isSymmetric() const;

    /** \brief
     * Sets the diagonal entries to the given value.
     */
    void setDiag(T value);

    /** \brief
     * Creates a matrix from the "multiplication" of two vectors.
     *
     * 2d-Example:
     *
     * /a\   /c\   /ac ad\
     * | | * | | = |     |
     * \b/   \d/   \bc bd/
     */
    void vecProduct(const WorldVector<T>& v1, const WorldVector<T>& v2);
  };


  // ===========================================================================
  // ===== global functions ====================================================
  // ===========================================================================


  /** \brief
   * returns the euclidian distance of a and b
   */
  template<typename T, GeoIndex d>
  double absteukl(const FixVec<T,d>& a,const FixVec<T,d>& b);

  /** \brief
   * FixVec operator for stream output
   */
  template<typename T, GeoIndex d>
632
  std::ostream& operator <<(std::ostream& out, const FixVec<T,d>& fixvec)
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
  {
    for(int i=0; i < fixvec.getSize()-1; i++) {
      out << fixvec[i] << " ";
    }
    out << fixvec[fixvec.getSize()-1];
    return out;
  }

  /** \brief
   * creates and inits a VectorOfFixVecs<DimVec<double> >
   */
  VectorOfFixVecs<DimVec<double> > *createAndInit(int dim, int size, ...);

  /** \brief
   * creates and inits and double array
   */
  double *createAndInitArray(int size, ...); 

  inline WorldVector<double> operator*(const WorldVector<double>& v, double d) {
    WorldVector<double> result = v;
    result *= d;
    return result;
  };

  inline WorldVector<double> operator+(const WorldVector<double>& v1,
				       const WorldVector<double>& v2) 
  {
    WorldVector<double> result = v1;
    result += v2;
    return result;
  };

  inline WorldVector<int> operator+(const WorldVector<int>& v1,
				    const WorldVector<int>& v2) 
  {
    WorldVector<int> result = v1;
    result += v2;
    return result;
  };

  inline WorldVector<double> operator-(const WorldVector<double>& v1,
				       const WorldVector<double>& v2) 
  {
    WorldVector<double> result = v1;
    result -= v2;
    return result;
  };

  inline bool operator<(const WorldVector<double>& v1,
			const WorldVector<double>& v2) 
  {
    int i, dow = Global::getGeo(WORLD);
    for(i = 0; i < dow; i++) {
      if(abs(v1[i] - v2[i]) < DBL_TOL) continue;
      return v1[i] < v2[i];
    }
    return false;
  };

  inline bool operator==(const WorldVector<double>& v1,
			 const WorldVector<double>& v2) 
  {
    int i, dow = Global::getGeo(WORLD);
    for(i = 0; i < dow; i++) {
      if(abs(v1[i] - v2[i]) > DBL_TOL) return false;
    }
    return true;
  };

  template<typename T>
  const WorldMatrix<T>& operator*=(WorldMatrix<T>& m, T scal);

  template<typename T>
    const WorldMatrix<T>& operator-=(WorldMatrix<T>& m1, const WorldMatrix<T>& m2);

  template<typename T>
    const WorldMatrix<T>& operator+=(WorldMatrix<T>& m1, const WorldMatrix<T>& m2);
}

#include "FixVec.hh"

#endif // AMDIS_FIXVEC_H