SystemVector.h 13.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// ============================================================================
// ==                                                                        ==
// == 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.
       */
      Creator(const ::std::string &name_,
	      ::std::vector<FiniteElemSpace*> feSpace_, 
	      int size_) 
	: name(name_), feSpace(feSpace_), size(size_) 
      {};

      /** \brief
       * Destructor
       */
      virtual ~Creator() {};

      /** \brief
       * Implementation of CreatorInterface::create().
       */
      SystemVector *create() { 
	char number[10];
	::std::string numberedName;
	SystemVector *result = NEW SystemVector(name, feSpace, size);
76
	for (int i = 0; i < size; i++) {
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
	  sprintf(number, "[%d]", i);
	  numberedName = name + ::std::string(number);
	  result->setDOFVector(i, NEW DOFVector<double>(feSpace[i], numberedName));
	}
	return result; 
      };

      /** \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;
      };

    private:
      /** \brief
       * Name of the system vector
       */
      ::std::string name;

      /** \brief
       * Finite element space used for creation.
       */
      ::std::vector<FiniteElemSpace*> feSpace;

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

  public:
    /** \brief
     * Constructor.
     */
    SystemVector(const ::std::string& name_,
		 ::std::vector<FiniteElemSpace*> feSpace_, 
		 int size)
      : name(name_),
	feSpace(feSpace_),
	vectors(size)
      
    {
      vectors.set(NULL);
    };

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

141
    virtual ~SystemVector() {};
142
143
144
145
146

    /** \brief
     * Sets \ref vectors[index] = vec.
     */ 
    inline void setDOFVector(int index, DOFVector<double> *vec) {
147
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
148
149
150
151
152
153
      vectors[index] = vec;
    };

    /** \brief
     * Returns \ref vectors[index].
     */
154
155
    inline DOFVector<double> *getDOFVector(int index) {      
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
156
157
158
159
160
161
162
      return vectors[index];
    };

    /** \brief
     * Returns \ref vectors[index].
     */
    inline const DOFVector<double> *getDOFVector(int index) const {
163
      TEST_EXIT_DBG(index < vectors.getSize())("invalid index\n");
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
      return vectors[index];
    };

    /** \brief
     * Returns sum of used vector sizes.
     */
    inline int getUsedSize() const {
      int i, totalSize = 0, size = vectors.getSize();
      for(i = 0; i < size; i++) {
	totalSize += vectors[i]->getUsedSize();
      }
      return totalSize;
    };

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

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

    /** \brief
     * Returns \ref feSpace.
     */
192
193
194
    inline FiniteElemSpace *getFESpace(int i) const { 
      return feSpace[i]; 
    };
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

    /** \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;

      while(localIndex >= vectors[vectorIndex]->getUsedSize()) {
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
    };

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

      while(localIndex >= vectors[vectorIndex]->getUsedSize()) {
	localIndex -= vectors[vectorIndex++]->getUsedSize();
      }

      return (*(vectors[vectorIndex]))[localIndex];
    };

    /** \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);
      }
    };

    /** \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;
    };

    /** \brief
     * Assignement operator.
     */
    inline SystemVector& operator=(const SystemVector& rhs) {
252
      TEST_EXIT_DBG(rhs.vectors.getSize() == vectors.getSize())("invalied sizes\n");
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
      int i, size = vectors.getSize();
      for(i = 0; i < size; i++) {
	(*(vectors[i])) = (*(rhs.getDOFVector(i)));
      }
      return *this;
    };

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

    void deserialize(::std::istream &in) {
      int i, size, oldSize = vectors.getSize();
      in.read(reinterpret_cast<char*>(&size), sizeof(int));
      vectors.resize(size);
      for(i = oldSize; i < size; i++) {
	vectors[i] = NEW DOFVector<double>(feSpace[i], "");
      }
      for(i = 0; i < size; i++) {
	vectors[i]->deserialize(in);
      }  
    };

    void copy(const SystemVector& rhs) {
      int i, size = vectors.getSize();
282
      TEST_EXIT_DBG(size == rhs.getNumVectors())("invalid sizes\n");
283
284
285
286
287
288
289
290
291
292
293
294
295
      for(i = 0; i < size; i++) {
	vectors[i]->copy(*(const_cast<SystemVector&>(rhs).getDOFVector(i)));
      }
    };

    void interpol(::std::vector<AbstractFunction<double, WorldVector<double> >*> *f) {
      int i, size = vectors.getSize();
      for(i = 0; i < size; i++) {
	vectors[i]->interpol((*f)[i]);
      }
    };

    void print() {
296
297
      int size = vectors.getSize();
      for (int i = 0; i < size; i++) {
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
	vectors[i]->print();
      }
    };



  protected:
    /** \brief
     * Name of the system vector
     */
    ::std::string name;

    /** \brief
     * Finite element space.
     */
    ::std::vector<FiniteElemSpace*> feSpace;

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

  /** \brief
   * multiplication with scalar
   */
  inline const SystemVector& operator*=(SystemVector& x, double d) {
325
326
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
327
328
329
330
331
332
333
334
335
      *(x.getDOFVector(i)) *= d;
    }
    return x;
  };

  /** \brief
   * scalar product
   */
  inline double operator*(SystemVector& x, SystemVector& y) {
336
337
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
338
    double result = 0.0;
339
340
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
341
342
343
344
345
346
347
348
349
350
351
      result += (*x.getDOFVector(i)) * (*y.getDOFVector(i));
    };
    return result;
  };

  /** \brief
   * addition of two system vectors
   */
  inline const SystemVector& operator+=(SystemVector& x, 
					const SystemVector& y) 
  {
352
353
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
354
355
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
356
357
358
359
360
361
362
363
364
365
366
      (*(x.getDOFVector(i))) += (*(y.getDOFVector(i)));
    }
    return x;
  };

  /**
   * subtraction of two system vectors.
   */
  inline const SystemVector& operator-=(SystemVector& x, 
					SystemVector& y) 
  {
367
368
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
          ("invalid size\n");
369
370
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
371
372
373
374
375
376
377
378
379
380
      (*(x.getDOFVector(i))) -= (*(y.getDOFVector(i)));
    }
    return x;
  };

  /** \brief
   * multiplication with a scalar
   */
  inline SystemVector operator*(SystemVector& x, double d) {
    SystemVector result = x;
381
382
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
383
384
385
386
387
388
389
390
391
392
      (*(result.getDOFVector(i))) *= d;
    }
    return result;
  };

  /** \brief
   * multiplication with a scalar
   */
  inline SystemVector operator*(double d, SystemVector& x) {
    SystemVector result = x;
393
394
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
395
396
397
398
399
400
401
402
403
404
405
      (*(result.getDOFVector(i))) *= d;
    }
    return result;
  };

  /** \brief
   * addition of two system vectors
   */
  inline SystemVector operator+(const SystemVector& x,
				const SystemVector& y)
  {
406
    TEST_EXIT_DBG(x.getNumVectors() == y.getNumVectors())
407
408
      ("invalid size\n");
    SystemVector result = x;
409
410
    int size = x.getNumVectors();
    for (int i = 0; i < size; i++) {
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
      (*(result.getDOFVector(i))) += (*(y.getDOFVector(i)));
    }
    return result;
  };

  /** \brief
   * Calls SystemVector::set(). Used for solving.
   */
  inline void set(SystemVector& x, double value) {
    x.set(value);
  }; 

  /** \brief
   * Calls SystemVector::set(). Used for solving.
   */
  inline void setValue(SystemVector& x, double value) {
    x.set(value);
  }; 

  /** \brief
   * Norm of system vector.
   */
  inline double norm(SystemVector* x) {
    double result = 0.0;
435
436
    int size = x->getNumVectors();
    for (int i = 0; i < size; i++) {
437
438
439
440
441
442
443
444
445
446
      result += x->getDOFVector(i)->squareNrm2();
    }
    return sqrt(result);
  };

  /** \brief
   * L2 norm of system vector.
   */
  inline double L2Norm(SystemVector* x) {
    double result = 0.0;
447
448
    int size = x->getNumVectors();
    for (int i = 0; i < size; i++) {
449
450
451
452
453
454
455
456
457
458
      result += x->getDOFVector(i)->L2NormSquare();
    }
    return sqrt(result);
  };

  /** \brief
   * H1 norm of system vector.
   */
  inline double H1Norm(SystemVector* x) {
    double result = 0.0;
459
460
    int size = x->getNumVectors();
    for (int i = 0; i < size; i++) {
461
462
463
464
465
466
467
468
469
470
471
      result += x->getDOFVector(i)->H1NormSquare();
    }
    return sqrt(result);
  };

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

474
475
476
    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");
477

Thomas Witkowski's avatar
Thomas Witkowski committed
478
#ifdef _OPENMP
479
#pragma omp parallel for schedule(static, 1)    
Thomas Witkowski's avatar
Thomas Witkowski committed
480
#endif
481
    for (i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
482
483
484
485
      if (!add) 
	result.getDOFVector(i)->set(0.0);

      for (int j = 0; j < size; j++) {
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
	if (matrix[i][j]) {
	  mv<double>(NoTranspose, 
		     *(matrix[i][j]), 
		     *(x.getDOFVector(j)), 
		     *(result.getDOFVector(i)),
		     true);
	}
      }
    }
  };

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

Thomas Witkowski's avatar
Thomas Witkowski committed
505
    int size = x.getNumVectors();
506
    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
507

Thomas Witkowski's avatar
Thomas Witkowski committed
508
#ifdef _OPENMP
509
#pragma omp parallel for schedule(static, 1)
Thomas Witkowski's avatar
Thomas Witkowski committed
510
#endif
511
    for (i = 0; i < size; i++) {
512
513
514
515
516
517
518
519
520
      axpy(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
    }
  };

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

Thomas Witkowski's avatar
Thomas Witkowski committed
525
    for (int i = 0; i < size; i++) {
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
      xpay(a, *(x.getDOFVector(i)), *(y.getDOFVector(i)));
    }
  }

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

  inline void print(SystemVector* vec) {
    vec->print();
  };

}

#endif // AMDIS_SYSTEMVECTOR_H