SystemVector.h 14.7 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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file SystemVector.h */

#ifndef AMDIS_SYSTEMVECTOR_H
#define AMDIS_SYSTEMVECTOR_H

#include "MemoryManager.h"
#include "MatrixVector.h"
#include "DOFVector.h"
#include "CreatorInterface.h"
#include "Serializable.h"
#include "OpenMP.h"

namespace AMDiS {

  // ============================================================================
  // ===== class SystemVector ===================================================
  // ============================================================================

  /** \brief
   * A system vector is a vector of dof vectors used for vector valued
   * problems.
   */
  class SystemVector : public Serializable
  {
  public:
    MEMORY_MANAGED(SystemVector);

    /** \brief
     * Creator. 
     */
    class Creator 
      : public CreatorInterface<SystemVector> {
    public:
      MEMORY_MANAGED(Creator);

      /** \brief
       * Creators constructor.
       */
58
59
      Creator(const std::string &name_,
	      std::vector<FiniteElemSpace*> feSpace_, 
60
	      int size_) 
61
62
63
	: name(name_), 
	  feSpace(feSpace_), 
	  size(size_) 
64
      {}
65
66
67
68

      /** \brief
       * Destructor
       */
69
      virtual ~Creator() {}
70
71
72
73
74
75

      /** \brief
       * Implementation of CreatorInterface::create().
       */
      SystemVector *create() { 
	char number[10];
76
	std::string numberedName;
77
	SystemVector *result = NEW SystemVector(name, feSpace, size);
78
	for (int i = 0; i < size; i++) {
79
	  sprintf(number, "[%d]", i);
80
	  numberedName = name + std::string(number);
81
82
83
	  result->setDOFVector(i, NEW DOFVector<double>(feSpace[i], numberedName));
	}
	return result; 
84
      }
85
86
87
88
89
90
91
92
93
94

      /** \brief
       * Implementation of CreatorInterface::free().
       */ 
      void free(SystemVector *vec) { 
	for (int i = 0; i < size; i++) {
	  DELETE vec->getDOFVector(i);
	  vec->setDOFVector(i, NULL);
	}
	DELETE vec;
95
      }
96
97
98
99
100

    private:
      /** \brief
       * Name of the system vector
       */
101
      std::string name;
102
103
104
105

      /** \brief
       * Finite element space used for creation.
       */
106
      std::vector<FiniteElemSpace*> feSpace;
107
108
109
110
111
112
113
114
115
116
117

      /** \brief
       * Number of component vectors to be created
       */
      int size;
    };

  public:
    /** \brief
     * Constructor.
     */
118
119
    SystemVector(const std::string& name_,
		 std::vector<FiniteElemSpace*> feSpace_, 
120
121
122
123
124
125
126
		 int size)
      : name(name_),
	feSpace(feSpace_),
	vectors(size)
      
    {
      vectors.set(NULL);
127
    }
128
129
130
131
132
133
134
135
136
137

    /** \brief
     * Copy Constructor.
     */
    SystemVector(const SystemVector& rhs)
      : name(rhs.name),
	feSpace(rhs.feSpace),
	vectors(rhs.vectors.getSize())
      
    {
138
      for (int i = 0; i < vectors.getSize(); i++) {
139
	vectors[i] = NEW DOFVector<double>(*rhs.vectors[i]);
140
      }
141
142
143
144
    }

    virtual ~SystemVector() 
    {}
145

146
147
148
149
150
151
    void createNewDOFVectors(std::string name)
    {     
      for (int i = 0; i < vectors.getSize(); i++) {
	vectors[i] = NEW DOFVector<double>(feSpace[i], "tmp");
      }
    }
152
153
154
155
156

    /** \brief
     * Sets \ref vectors[index] = vec.
     */ 
    inline void setDOFVector(int index, DOFVector<double> *vec) {
157
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
158
      vectors[index] = vec;
159
    }
160
161
162
163

    /** \brief
     * Returns \ref vectors[index].
     */
164
165
    inline DOFVector<double> *getDOFVector(int index) {      
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
166
      return vectors[index];
167
    }
168
169
170
171
172

    /** \brief
     * Returns \ref vectors[index].
     */
    inline const DOFVector<double> *getDOFVector(int index) const {
173
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
174
      return vectors[index];
175
    }
176
177
178
179
180

    /** \brief
     * Returns sum of used vector sizes.
     */
    inline int getUsedSize() const {
181
182
      int totalSize = 0;
      int size = vectors.getSize();
183
      for (int i = 0; i < size; i++) {
184
185
186
	totalSize += vectors[i]->getUsedSize();
      }
      return totalSize;
187
    }
188
189
190
191
192
193

    /** \brief
     * Returns number of contained vectors.
     */
    inline int getNumVectors() const { 
      return vectors.getSize(); 
194
    }
195
196
197

    inline int getSize() const {
      return vectors.getSize();
198
    }
199
200

    /** \brief
201
     * Returns the fe space for a given component.
202
     */
203
204
    inline FiniteElemSpace *getFESpace(int i) const { 
      return feSpace[i]; 
205
    }
206

207
208
209
    /** \brief
     * Returns the fe spaces for all components.
     */
210
211
212
213
    inline std::vector<FiniteElemSpace*> getFESpaces() const {
      return feSpace;
    }

214
215
216
217
218
219
220
221
222
223
    /** \brief
     * Here the system vector is interpreted as one large vector. The given
     * is used as a global index which indicates a local vector number and
     * a local index on this vector. The operator returns this local vector
     * at the local index.
     */
    inline double& operator[](int index) {
      int localIndex = index;
      int vectorIndex = 0;

224
      while (localIndex >= vectors[vectorIndex]->getUsedSize()) {
225
226
227
228
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
229
    }
230
231
232
233
234
235
236
237

    /** \brief
     * For const access.
     */
    inline double operator[](int index) const {
      int localIndex = index;
      int vectorIndex = 0;

238
      while (localIndex >= vectors[vectorIndex]->getUsedSize()) {
239
240
241
242
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
243
    }
244
245
246
247
248
249
250
251
252

    /** \brief
     * Sets all entries in all vectors to value.
     */
    inline void set(double value) {
      int size = vectors.getSize();
      for (int i = 0; i < size; i++) {
	vectors[i]->set(value);
      }
253
    }
254

255
256
257
258
259
260
261
    inline void setCoarsenOperation(CoarsenOperation op) { 
      for (int i = 0; i < static_cast<int>(vectors.getSize()); i++) {
	vectors[i]->setCoarsenOperation(op); 
      }
    }


262
263
264
265
266
267
268
269
270
    /** \brief
     * Sets all entries in all vectors to value.
     */
    inline SystemVector& operator=(double value) {
      int size = vectors.getSize();
      for (int i = 0; i < size; i++) {
	(*(vectors[i])) = value;
      }
      return *this;
271
    }
272
273
274
275
276

    /** \brief
     * Assignement operator.
     */
    inline SystemVector& operator=(const SystemVector& rhs) {
277
      TEST_EXIT_DBG(rhs.vectors.getSize() == vectors.getSize())("invalied sizes\n");
278
279
      int size = vectors.getSize();
      for (int i = 0; i < size; i++) {
280
281
282
	(*(vectors[i])) = (*(rhs.getDOFVector(i)));
      }
      return *this;
283
    }
284

285
    void serialize(std::ostream &out) {
286
      int size = vectors.getSize();
287
      out.write(reinterpret_cast<const char*>(&size), sizeof(int));
288
      for (int i = 0; i < size; i++) {
289
290
	vectors[i]->serialize(out);
      }
291
    }
292

293
    void deserialize(std::istream &in) {
294
      int size, oldSize = vectors.getSize();
295
296
      in.read(reinterpret_cast<char*>(&size), sizeof(int));
      vectors.resize(size);
297
      for (int i = oldSize; i < size; i++) {
298
299
	vectors[i] = NEW DOFVector<double>(feSpace[i], "");
      }
300
      for (int i = 0; i < size; i++) {
301
302
	vectors[i]->deserialize(in);
      }  
303
    }
304
305

    void copy(const SystemVector& rhs) {
306
      int size = vectors.getSize();
307
      TEST_EXIT_DBG(size == rhs.getNumVectors())("invalid sizes\n");
308
      for (int i = 0; i < size; i++) {
309
310
	vectors[i]->copy(*(const_cast<SystemVector&>(rhs).getDOFVector(i)));
      }
311
    }
312

313
    void interpol(std::vector<AbstractFunction<double, WorldVector<double> >*> *f) {
314
315
      int size = vectors.getSize();
      for (int i = 0; i < size; i++) {
316
317
	vectors[i]->interpol((*f)[i]);
      }
318
319
320
321
322
323
324
    }

    void interpol(SystemVector *v, double factor) {
      for (int i = 0; i < v->getSize(); i++) {
	vectors[i]->interpol(v->getDOFVector(i), factor);
      }
    }
325
326

    void print() {
327
328
      int size = vectors.getSize();
      for (int i = 0; i < size; i++) {
329
330
	vectors[i]->print();
      }
331
    }
332

333
334
335
336
337
338
339
340
341
    int calcMemoryUsage() {
      int result = 0;

      for (int i = 0; i < static_cast<int>(vectors.getSize()); i++) {
	result += vectors[i]->calcMemoryUsage();
      }

      return result;
    }
342

343
      
344
345
346
347
348

  protected:
    /** \brief
     * Name of the system vector
     */
349
    std::string name;
350
351
352
353

    /** \brief
     * Finite element space.
     */
354
    std::vector<FiniteElemSpace*> feSpace;
355
356
357
358
359
360
361

    /** \brief
     * Local dof vectors.
     */
    Vector<DOFVector<double>*> vectors;
  };

362

363
364
365
366
  /** \brief
   * multiplication with scalar
   */
  inline const SystemVector& operator*=(SystemVector& x, double d) {
367
368
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
369
370
371
      *(x.getDOFVector(i)) *= d;
    }
    return x;
372
  }
373
374
375
376
377

  /** \brief
   * scalar product
   */
  inline double operator*(SystemVector& x, SystemVector& y) {
378
379
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
380
    double result = 0.0;
381
382
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
383
      result += (*x.getDOFVector(i)) * (*y.getDOFVector(i));
384
    }
385
    return result;
386
  }
387
388
389
390
391
392
393

  /** \brief
   * addition of two system vectors
   */
  inline const SystemVector& operator+=(SystemVector& x, 
					const SystemVector& y) 
  {
394
395
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
396
397
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
398
399
400
      (*(x.getDOFVector(i))) += (*(y.getDOFVector(i)));
    }
    return x;
401
  }
402
403
404
405
406
407
408

  /**
   * subtraction of two system vectors.
   */
  inline const SystemVector& operator-=(SystemVector& x, 
					SystemVector& y) 
  {
409
410
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
411
412
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
413
414
415
      (*(x.getDOFVector(i))) -= (*(y.getDOFVector(i)));
    }
    return x;
416
  }
417
418
419
420
421
422

  /** \brief
   * multiplication with a scalar
   */
  inline SystemVector operator*(SystemVector& x, double d) {
    SystemVector result = x;
423
424
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
425
426
427
      (*(result.getDOFVector(i))) *= d;
    }
    return result;
428
  }
429
430
431
432
433
434

  /** \brief
   * multiplication with a scalar
   */
  inline SystemVector operator*(double d, SystemVector& x) {
    SystemVector result = x;
435
436
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
437
438
439
      (*(result.getDOFVector(i))) *= d;
    }
    return result;
440
  }
441
442
443
444
445
446
447

  /** \brief
   * addition of two system vectors
   */
  inline SystemVector operator+(const SystemVector& x,
				const SystemVector& y)
  {
448
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
449
450
      ("invalid size\n");
    SystemVector result = x;
451
452
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
453
454
455
      (*(result.getDOFVector(i))) += (*(y.getDOFVector(i)));
    }
    return result;
456
  }
457
458
459
460
461
462

  /** \brief
   * Calls SystemVector::set(). Used for solving.
   */
  inline void set(SystemVector& x, double value) {
    x.set(value);
463
  } 
464
465
466
467
468
469

  /** \brief
   * Calls SystemVector::set(). Used for solving.
   */
  inline void setValue(SystemVector& x, double value) {
    x.set(value);
470
  }
471
472
473
474
475
476

  /** \brief
   * Norm of system vector.
   */
  inline double norm(SystemVector* x) {
    double result = 0.0;
477
478
    int size = x->getNumVectors();
    for (int i = 0; i < size; i++) {
479
480
481
      result += x->getDOFVector(i)->squareNrm2();
    }
    return sqrt(result);
482
  }
483
484
485
486
487
488

  /** \brief
   * L2 norm of system vector.
   */
  inline double L2Norm(SystemVector* x) {
    double result = 0.0;
489
490
    int size = x->getNumVectors();
    for (int i = 0; i < size; i++) {
491
492
493
      result += x->getDOFVector(i)->L2NormSquare();
    }
    return sqrt(result);
494
  }
495
496
497
498
499
500

  /** \brief
   * H1 norm of system vector.
   */
  inline double H1Norm(SystemVector* x) {
    double result = 0.0;
501
502
    int size = x->getNumVectors();
    for (int i = 0; i < size; i++) {
503
504
505
      result += x->getDOFVector(i)->H1NormSquare();
    }
    return sqrt(result);
506
  }
507
508
509
510
511
512
513

  inline void mv(Matrix<DOFMatrix*> &matrix,
		 const SystemVector &x,
		 SystemVector       &result,
		 bool               add = false)
  {
    int size = x.getNumVectors();
514
    int i;
515

516
517
518
    TEST_EXIT_DBG(size == result.getNumVectors())("incompatible sizes\n");
    TEST_EXIT_DBG(size == matrix.getNumRows())("incompatible sizes\n");
    TEST_EXIT_DBG(size == matrix.getNumCols())("incompatible sizes\n");
519

Thomas Witkowski's avatar
Thomas Witkowski committed
520
#ifdef _OPENMP
521
#pragma omp parallel for schedule(static, 1) num_threads(min(size, omp_get_max_threads()))
Thomas Witkowski's avatar
Thomas Witkowski committed
522
#endif
523
    for (i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
524
525
526
527
      if (!add) 
	result.getDOFVector(i)->set(0.0);

      for (int j = 0; j < size; j++) {
528
529
530
531
532
533
534
535
536
	if (matrix[i][j]) {
	  mv<double>(NoTranspose, 
		     *(matrix[i][j]), 
		     *(x.getDOFVector(j)), 
		     *(result.getDOFVector(i)),
		     true);
	}
      }
    }
537
  }
538
539
540
541
542
543

  /** \brief
   * y = a*x + y;
   */
  inline void axpy(double a, SystemVector& x, SystemVector& y)
  {
544
545
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
546

Thomas Witkowski's avatar
Thomas Witkowski committed
547
    int size = x.getNumVectors();
548
    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
549

Thomas Witkowski's avatar
Thomas Witkowski committed
550
#ifdef _OPENMP
551
#pragma omp parallel for schedule(static, 1) num_threads(min(size, omp_get_max_threads()))
Thomas Witkowski's avatar
Thomas Witkowski committed
552
#endif
553
    for (i = 0; i < size; i++) {
554
555
      axpy(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
    }
556
  }
557
558
559
560
561
562

  /** \brief
   * y = x + a*y
   */
  inline void xpay(double a, SystemVector& x, SystemVector& y)
  {
563
564
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
565
    int size = x.getNumVectors();
566

Thomas Witkowski's avatar
Thomas Witkowski committed
567
    for (int i = 0; i < size; i++) {
568
569
570
571
572
573
574
575
576
      xpay(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
    }
  }

  /** \brief
   * Returns SystemVector::getUsedSize().
   */
  inline int size(SystemVector* vec) {
    return vec->getUsedSize();
577
  }
578
579
580

  inline void print(SystemVector* vec) {
    vec->print();
581
  }
582
583
584
585

}

#endif // AMDIS_SYSTEMVECTOR_H